From 0146b8c757fe877fdef7c23ffeab0333d9df2c52 Mon Sep 17 00:00:00 2001 From: rambohe-ch Date: Fri, 28 May 2021 18:23:28 +0800 Subject: [PATCH] bugfix: the same prefix key lock error --- pkg/yurthub/storage/disk/storage.go | 7 +++++-- pkg/yurthub/storage/disk/storage_test.go | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/yurthub/storage/disk/storage.go b/pkg/yurthub/storage/disk/storage.go index 69e55f6801d..4128938c25d 100644 --- a/pkg/yurthub/storage/disk/storage.go +++ b/pkg/yurthub/storage/disk/storage.go @@ -460,16 +460,19 @@ func (ds *diskStorage) lockKey(key string) bool { ds.Lock() defer ds.Unlock() if _, ok := ds.keyPendingStatus[key]; ok { + klog.Infof("key(%s) storage is pending, just skip it", key) return false } for pendingKey := range ds.keyPendingStatus { if len(key) > len(pendingKey) { - if strings.Contains(key, pendingKey) { + if strings.Contains(key, fmt.Sprintf("%s/", pendingKey)) { + klog.Infof("key(%s) storage is pending, skip to store key(%s)", pendingKey, key) return false } } else { - if strings.Contains(pendingKey, key) { + if strings.Contains(pendingKey, fmt.Sprintf("%s/", key)) { + klog.Infof("key(%s) storage is pending, skip to store key(%s)", pendingKey, key) return false } } diff --git a/pkg/yurthub/storage/disk/storage_test.go b/pkg/yurthub/storage/disk/storage_test.go index 78dad3495a4..fe0ab229f2d 100644 --- a/pkg/yurthub/storage/disk/storage_test.go +++ b/pkg/yurthub/storage/disk/storage_test.go @@ -685,6 +685,12 @@ func TestLockKey(t *testing.T) { lockResult: true, lockVerifyResult: false, }, + "lock same prefix key with pre-locked key ": { + preLockedKeys: []string{"kubelet/pods/kube-system/foo"}, + lockKey: "kubelet/pods/kube-system/foo2", + lockResult: true, + lockVerifyResult: false, + }, } s := &diskStorage{