From a514d91b6bcd1623a89dd9094cfc5f0129911469 Mon Sep 17 00:00:00 2001
From: rambohe <linbo.hlb@alibaba-inc.com>
Date: Thu, 29 Jul 2021 01:06:34 +0800
Subject: [PATCH] bugfix: the same prefix key lock error (#396)

---
 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{