tianxiaoliang commented on a change in pull request #228: URL: https://github.com/apache/servicecomb-kie/pull/228#discussion_r757261257
########## File path: server/datasource/etcd/kv/kv_dao_trans.go ########## @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kv + +import ( + "context" + "encoding/json" + + "github.com/go-chassis/cari/sync" + "github.com/go-chassis/openlog" + "github.com/little-cui/etcdadpt" + + "github.com/apache/servicecomb-kie/pkg/model" + "github.com/apache/servicecomb-kie/server/datasource" + "github.com/apache/servicecomb-kie/server/datasource/etcd/key" +) + +// CreateWithTask is to start transaction when creating KV, will create task in a transaction operation +func (s *Dao) CreateWithTask(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) { + kvBytes, err := json.Marshal(kv) + if err != nil { + openlog.Error("marshal kv ") + return nil, err + } + task, err := datasource.NewTask(sync.CreateAction, datasource.ConfigResource) + if err != nil { + openlog.Error("fail to create task" + err.Error()) + return nil, err + } + task.Data = kv + taskBytes, err := json.Marshal(task) + if err != nil { + openlog.Error("fail to marshal task ") + return nil, err + } + kvOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.KV(kv.Domain, kv.Project, kv.ID)), etcdadpt.WithValue(kvBytes)) + taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.TaskKey(kv.Domain, kv.Project, task.TaskID, task.Timestamp)), etcdadpt.WithValue(taskBytes)) + err = etcdadpt.Txn(ctx, []etcdadpt.OpOptions{kvOpPut, taskOpPut}) Review comment: 其实区别应该就只是这一行上下的部分代码时独立的分支 ########## File path: server/datasource/etcd/kv/kv_dao_trans.go ########## @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kv + +import ( + "context" + "encoding/json" + + "github.com/go-chassis/cari/sync" + "github.com/go-chassis/openlog" + "github.com/little-cui/etcdadpt" + + "github.com/apache/servicecomb-kie/pkg/model" + "github.com/apache/servicecomb-kie/server/datasource" + "github.com/apache/servicecomb-kie/server/datasource/etcd/key" +) + +// CreateWithTask is to start transaction when creating KV, will create task in a transaction operation +func (s *Dao) CreateWithTask(ctx context.Context, kv *model.KVDoc) (*model.KVDoc, error) { + kvBytes, err := json.Marshal(kv) Review comment: 这个文件内的函数应该尽可能复用他对应的原版的没有事务落盘的函数 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
