You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2022/01/06 07:04:08 UTC

[GitHub] [servicecomb-service-center] robotLJW opened a new pull request #1204: [feat] add tag sync func and ut when db mode is etcd

robotLJW opened a new pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204


   Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/SCB) filed for the change (usually before you start working on it).  Trivial changes like typos do not require a JIRA issue.  Your pull request should address just this issue, without pulling in other changes.
    - [ ] Each commit in the pull request should have a meaningful subject line and body.
    - [ ] Format the pull request title like `[SCB-XXX] Fixes bug in ApproximateQuantiles`, where you replace `SCB-XXX` with the appropriate JIRA issue.
    - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [ ] Run `go build` `go test` `go fmt` `go vet` to make sure basic checks pass. A more thorough check will be performed on your pull request automatically.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
    - [ ] Never comment source code, delete it.
    - [ ] UT should has "context, subject, expected result" result as test case name, when you call t.Run().
   ---
   


-- 
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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779382789



##########
File path: datasource/etcd/ms.go
##########
@@ -1737,10 +1749,34 @@ func (ds *MetadataManager) DeleteTags(ctx context.Context, request *pb.DeleteSer
 
 	key := path.GenerateServiceTagKey(domainProject, request.ServiceId)
 
-	resp, err := etcdadpt.TxnWithCmp(ctx,
-		etcdadpt.Ops(etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data))),
-		etcdadpt.If(etcdadpt.NotEqualVer(path.GenerateServiceKey(domainProject, request.ServiceId), 0)),
-		nil)
+	opts := etcdadpt.Ops(etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data)))
+	if datasource.EnableSync {
+		domain := util.ParseDomain(ctx)
+		project := util.ParseProject(ctx)
+		taskOpt, err := eutil.GenTaskOpts(&eutil.TaskOpt{
+			Domain:       domain,
+			Project:      project,
+			Action:       sync.DeleteAction,
+			ResourceType: datasource.ResourceKV,
+			Resource:     data,
+			Opts:         map[string]string{"key": key},
+		})
+		if err != nil {
+			return &pb.DeleteServiceTagsResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, err
+		}
+		tombstoneOpt, err := eutil.GenTombstoneOpts(domain, project, datasource.ResourceKV, key)

Review comment:
       Delete事务落盘,必须伴随墓碑,是否能进步一抽取函数,而不是分开写

##########
File path: datasource/etcd/dep.go
##########
@@ -31,7 +31,7 @@ import (
 	"github.com/apache/servicecomb-service-center/datasource/etcd/event"
 	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
 	"github.com/apache/servicecomb-service-center/datasource/etcd/sd"
-	serviceUtil "github.com/apache/servicecomb-service-center/datasource/etcd/util"
+	eutil "github.com/apache/servicecomb-service-center/datasource/etcd/util"

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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang merged pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
tianxiaoliang merged pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204


   


-- 
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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] robotLJW commented on a change in pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779385304



##########
File path: datasource/etcd/dep.go
##########
@@ -31,7 +31,7 @@ import (
 	"github.com/apache/servicecomb-service-center/datasource/etcd/event"
 	"github.com/apache/servicecomb-service-center/datasource/etcd/path"
 	"github.com/apache/servicecomb-service-center/datasource/etcd/sd"
-	serviceUtil "github.com/apache/servicecomb-service-center/datasource/etcd/util"
+	eutil "github.com/apache/servicecomb-service-center/datasource/etcd/util"

Review comment:
       这个我记得如果重名了,我记得之前看到建议修改名为前一个package名的首字母+同名的package,https://stackoverflow.com/questions/10408646/how-to-import-and-use-different-packages-of-the-same-name




-- 
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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] robotLJW commented on a change in pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779979639



##########
File path: datasource/etcd/account.go
##########
@@ -194,20 +195,15 @@ func (ds *RbacDAO) DeleteAccount(ctx context.Context, names []string) (bool, err
 			continue //do not fail if some account is invalid
 
 		}
-		if datasource.EnableSync {
-			taskOpt, err := GenTaskOpts("", "", sync.DeleteAction, datasource.ResourceAccount, a)
-			if err != nil {
-				log.Error("", err)
-				return false, err
-			}
-			tombstoneOpt, err := GenTombstoneOpts("", "", datasource.ResourceAccount, a.Name)
-			if err != nil {
-				log.Error("", err)
-				return false, err
-			}
-			opts = append(opts, tombstoneOpt, taskOpt)
+		syncOpts, err := esync.GenSyncOpts(ctx, sync.DeleteAction, datasource.ResourceAccount, a, esync.WithResourceID(a.Name))

Review comment:
       华哥这个resourceID是删除的时候需要必填的,其他操作时不用填写的




-- 
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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] robotLJW commented on a change in pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
robotLJW commented on a change in pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779387657



##########
File path: datasource/etcd/ms.go
##########
@@ -1737,10 +1749,34 @@ func (ds *MetadataManager) DeleteTags(ctx context.Context, request *pb.DeleteSer
 
 	key := path.GenerateServiceTagKey(domainProject, request.ServiceId)
 
-	resp, err := etcdadpt.TxnWithCmp(ctx,
-		etcdadpt.Ops(etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data))),
-		etcdadpt.If(etcdadpt.NotEqualVer(path.GenerateServiceKey(domainProject, request.ServiceId), 0)),
-		nil)
+	opts := etcdadpt.Ops(etcdadpt.OpPut(etcdadpt.WithStrKey(key), etcdadpt.WithValue(data)))
+	if datasource.EnableSync {
+		domain := util.ParseDomain(ctx)
+		project := util.ParseProject(ctx)
+		taskOpt, err := eutil.GenTaskOpts(&eutil.TaskOpt{
+			Domain:       domain,
+			Project:      project,
+			Action:       sync.DeleteAction,
+			ResourceType: datasource.ResourceKV,
+			Resource:     data,
+			Opts:         map[string]string{"key": key},
+		})
+		if err != nil {
+			return &pb.DeleteServiceTagsResponse{
+				Response: pb.CreateResponse(pb.ErrInternal, err.Error()),
+			}, err
+		}
+		tombstoneOpt, err := eutil.GenTombstoneOpts(domain, project, datasource.ResourceKV, key)

Review comment:
       这个 GenTombstoneOpts,并没有写db,而是伴随后面会加入 opts ,最终 Txn 一同写入




-- 
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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] tianxiaoliang commented on a change in pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
tianxiaoliang commented on a change in pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779483865



##########
File path: datasource/etcd/account.go
##########
@@ -84,7 +85,13 @@ func (ds *RbacDAO) CreateAccount(ctx context.Context, a *rbacmodel.Account) erro
 		return err
 	}
 	if datasource.EnableSync {
-		op, err := GenTaskOpts("", "", sync.CreateAction, datasource.ResourceAccount, a)
+		op, err := eutil.GenTaskOpts(&eutil.TaskOpt{
+			Domain:       "",

Review comment:
       空是不必写出来的

##########
File path: datasource/etcd/account.go
##########
@@ -195,12 +202,18 @@ func (ds *RbacDAO) DeleteAccount(ctx context.Context, names []string) (bool, err
 
 		}
 		if datasource.EnableSync {
-			taskOpt, err := GenTaskOpts("", "", sync.DeleteAction, datasource.ResourceAccount, a)
+			taskOpt, err := eutil.GenTaskOpts(&eutil.TaskOpt{
+				Domain:       "",

Review comment:
       删

##########
File path: datasource/etcd/account.go
##########
@@ -248,7 +261,13 @@ func (ds *RbacDAO) UpdateAccount(ctx context.Context, name string, account *rbac
 		opts = append(opts, opt)
 	}
 	if datasource.EnableSync {
-		op, err := GenTaskOpts("", "", sync.UpdateAction, datasource.ResourceAccount, account)
+		op, err := eutil.GenTaskOpts(&eutil.TaskOpt{
+			Domain:       "",

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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [servicecomb-service-center] little-cui commented on a change in pull request #1204: [feat] add tag sync func and ut when db mode is etcd

Posted by GitBox <gi...@apache.org>.
little-cui commented on a change in pull request #1204:
URL: https://github.com/apache/servicecomb-service-center/pull/1204#discussion_r779972260



##########
File path: datasource/etcd/account.go
##########
@@ -83,13 +84,13 @@ func (ds *RbacDAO) CreateAccount(ctx context.Context, a *rbacmodel.Account) erro
 		log.Error("", err)
 		return err
 	}
-	if datasource.EnableSync {
-		op, err := GenTaskOpts("", "", sync.CreateAction, datasource.ResourceAccount, a)
-		if err != nil {
-			log.Error("", err)
-			return err
-		}
-		opts = append(opts, op)
+	syncOpts, err := esync.GenSyncOpts(ctx, sync.CreateAction, datasource.ResourceAccount, a)
+	if err != nil {
+		log.Error("fail to create sync opts", err)
+		return err
+	}
+	if len(syncOpts) != 0 {
+		opts = append(opts, syncOpts...)

Review comment:
       不需要判断长度,go内部已经优化

##########
File path: datasource/etcd/account.go
##########
@@ -194,20 +195,15 @@ func (ds *RbacDAO) DeleteAccount(ctx context.Context, names []string) (bool, err
 			continue //do not fail if some account is invalid
 
 		}
-		if datasource.EnableSync {
-			taskOpt, err := GenTaskOpts("", "", sync.DeleteAction, datasource.ResourceAccount, a)
-			if err != nil {
-				log.Error("", err)
-				return false, err
-			}
-			tombstoneOpt, err := GenTombstoneOpts("", "", datasource.ResourceAccount, a.Name)
-			if err != nil {
-				log.Error("", err)
-				return false, err
-			}
-			opts = append(opts, tombstoneOpt, taskOpt)
+		syncOpts, err := esync.GenSyncOpts(ctx, sync.DeleteAction, datasource.ResourceAccount, a, esync.WithResourceID(a.Name))

Review comment:
       resourceID是必填字段,不做optional,value是可选

##########
File path: datasource/etcd/sync/sync.go
##########
@@ -0,0 +1,111 @@
+/*
+ * 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 sync
+
+import (
+	"context"
+	"encoding/json"
+
+	"github.com/go-chassis/cari/sync"
+	"github.com/little-cui/etcdadpt"
+
+	"github.com/apache/servicecomb-service-center/datasource"
+	"github.com/apache/servicecomb-service-center/eventbase/datasource/etcd/key"
+	"github.com/apache/servicecomb-service-center/pkg/util"
+)
+
+type Options struct {
+	ResourceID string
+	Opts       map[string]string
+}
+
+type Option func(options *Options)
+
+func NewSyncOptions() Options {
+	return Options{}
+}
+
+func WithResourceID(resourceID string) Option {
+	return func(options *Options) {
+		options.ResourceID = resourceID
+	}
+}
+
+func WithOpts(opts map[string]string) Option {
+	return func(options *Options) {
+		options.Opts = opts
+	}
+}
+
+func GenSyncOpts(ctx context.Context, action string, resourceType string, resource interface{},
+	options ...Option) ([]etcdadpt.OpOptions, error) {
+	opts := make([]etcdadpt.OpOptions, 0)
+	if !datasource.EnableSync {
+		return opts, nil
+	}
+	syncOpts := NewSyncOptions()
+	for _, option := range options {
+		option(&syncOpts)
+	}
+	taskOpt, err := genTaskOpt(ctx, action, resourceType, resource, &syncOpts)
+	if err != nil {
+		return opts, err
+	}
+	opts = append(opts, taskOpt)
+	if action == sync.DeleteAction {

Review comment:
       取反,提前return,减少复杂度




-- 
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: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org