You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/01/26 03:14:45 UTC
[servicecomb-service-center] branch master updated: [fix] add SyncAllLockKey in SyncAll func (#1247)
This is an automated email from the ASF dual-hosted git repository.
littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push:
new aee412b [fix] add SyncAllLockKey in SyncAll func (#1247)
aee412b is described below
commit aee412b21fb7dbc610d49b3ab19d46e7fdcb7b1a
Author: robotljw <79...@qq.com>
AuthorDate: Wed Jan 26 11:14:37 2022 +0800
[fix] add SyncAllLockKey in SyncAll func (#1247)
---
datasource/etcd/sync.go | 17 ++++++++++++++++-
datasource/etcd/sync_test.go | 17 +++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/datasource/etcd/sync.go b/datasource/etcd/sync.go
index 0c0c5d4..0f00c5c 100644
--- a/datasource/etcd/sync.go
+++ b/datasource/etcd/sync.go
@@ -37,7 +37,11 @@ import (
)
const (
- SyncAllKey = "/cse-sr/sync-all"
+ SyncAllKey = "/cse-sr/sync-all"
+ SyncAllLockKey = "/cse-sr/sync-all-lock"
+
+ // one minutes
+ defaultLockTime = 60
)
var (
@@ -61,6 +65,17 @@ func (s *SyncManager) SyncAll(ctx context.Context) error {
log.Info(fmt.Sprintf("%s key already exists, do not need to do tasks", SyncAllKey))
return datasource.ErrSyncAllKeyExists
}
+ lock, err := etcdadpt.TryLock(SyncAllLockKey, defaultLockTime)
+ if err != nil || lock == nil {
+ log.Info(fmt.Sprintf("%s lock not acquired", SyncAllLockKey))
+ return nil
+ }
+ defer func(lock *etcdadpt.DLock) {
+ err := lock.Unlock()
+ if err != nil {
+ log.Error(fmt.Sprintf("fail to unlock the %s key", SyncAllLockKey), err)
+ }
+ }(lock)
err = syncAllAccounts(ctx)
if err != nil {
return err
diff --git a/datasource/etcd/sync_test.go b/datasource/etcd/sync_test.go
index f4400b0..09ed470 100644
--- a/datasource/etcd/sync_test.go
+++ b/datasource/etcd/sync_test.go
@@ -63,6 +63,23 @@ func TestSyncAll(t *testing.T) {
assert.Nil(t, err)
})
+ t.Run("enableOnstart is true and syncAllKey not exists but SyncAllLockKey is lock will not do sync", func(t *testing.T) {
+ _ = archaius.Set("sync.enableOnStart", true)
+ lock, err := etcdadpt.TryLock(etcd.SyncAllLockKey, 600)
+ assert.Nil(t, err)
+ err = datasource.GetSyncManager().SyncAll(syncAllContext())
+ assert.Nil(t, err)
+ listTaskReq := model.ListTaskRequest{
+ Domain: "sync-all",
+ Project: "sync-all",
+ }
+ tasks, err := task.List(syncAllContext(), &listTaskReq)
+ assert.NoError(t, err)
+ assert.Equal(t, 0, len(tasks))
+ err = lock.Unlock()
+ assert.Nil(t, err)
+ })
+
t.Run("enableOnStart is true and syncAllKey not exists will do sync", func(t *testing.T) {
_ = archaius.Set("sync.enableOnStart", true)
var serviceID string