[feat] fix delete tombstone data bug (#1261)
Co-authored-by: 1162641716@qq.com <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>
diff --git a/eventbase/datasource/etcd/key/key.go b/eventbase/datasource/etcd/key/key.go
index 02f55df..b2c954c 100644
--- a/eventbase/datasource/etcd/key/key.go
+++ b/eventbase/datasource/etcd/key/key.go
@@ -23,10 +23,11 @@
)
const (
- split = "/"
- syncer = "syncer"
- task = "task"
- tombstone = "tombstone"
+ split = "/"
+ syncer = "syncer"
+ task = "task"
+ tombstone = "tombstone"
+ TombstoneKeyLen = 6
)
func getSyncRootKey() string {
@@ -65,3 +66,11 @@
func TombstoneKey(domain, project, resourceType, resourceID string) string {
return strings.Join([]string{getTombstoneRootKey(), domain, project, resourceType, resourceID}, split)
}
+
+func SplitTombstoneKey(keyInfo []byte) []string {
+ return strings.Split(string(keyInfo), split)
+}
+
+func JoinResourceID(infos []string) string {
+ return strings.Join(infos, split)
+}
diff --git a/eventbase/datasource/etcd/tombstone/tombstone_dao.go b/eventbase/datasource/etcd/tombstone/tombstone_dao.go
index 3cf12d6..d00e79d 100644
--- a/eventbase/datasource/etcd/tombstone/tombstone_dao.go
+++ b/eventbase/datasource/etcd/tombstone/tombstone_dao.go
@@ -97,6 +97,7 @@
if !filterMatch(&tombstone, opts) {
continue
}
+ formatTombstoneKey(&tombstone, kv.Key)
tombstones = append(tombstones, &tombstone)
}
return tombstones, nil
@@ -111,3 +112,23 @@
}
return true
}
+
+func formatTombstoneKey(tombstone *sync.Tombstone, k []byte) {
+ keyInfo := key.SplitTombstoneKey(k)
+ if len(keyInfo) <= key.TombstoneKeyLen {
+ return
+ }
+ if len(tombstone.Domain) <= 0 {
+ tombstone.Domain = keyInfo[3]
+ }
+ if len(tombstone.Project) <= 0 {
+ tombstone.Project = keyInfo[4]
+ }
+ if len(tombstone.ResourceType) <= 0 {
+ tombstone.ResourceType = keyInfo[5]
+ }
+ if len(tombstone.ResourceID) <= 0 {
+ tombstone.ResourceID = key.JoinResourceID(keyInfo[key.TombstoneKeyLen:])
+ }
+ return
+}
diff --git a/syncer/service/tombstone/tombstone_test.go b/syncer/service/tombstone/tombstone_test.go
index d01a626..61a30f6 100644
--- a/syncer/service/tombstone/tombstone_test.go
+++ b/syncer/service/tombstone/tombstone_test.go
@@ -19,13 +19,13 @@
import (
"context"
+ "github.com/apache/servicecomb-service-center/eventbase/test"
"testing"
"time"
"github.com/apache/servicecomb-service-center/eventbase/datasource"
"github.com/apache/servicecomb-service-center/eventbase/model"
"github.com/apache/servicecomb-service-center/eventbase/service/tombstone"
- "github.com/apache/servicecomb-service-center/eventbase/test"
synctombstone "github.com/apache/servicecomb-service-center/syncer/service/tombstone"
"github.com/go-chassis/cari/sync"
"github.com/stretchr/testify/assert"
@@ -42,18 +42,19 @@
}
const (
- testDomain = "expireTombstone"
- testProject = "expireProject"
+ testDomain = "expireTombstone"
+ testProject = "expireProject"
+ testResourceType = "config"
)
func TestDeleteExpireTombStone(t *testing.T) {
- tombstoneOne := sync.NewTombstone(testDomain, testProject, "config", "1")
+ tombstoneOne := sync.NewTombstone(testDomain, testProject, testResourceType, "1")
tombstoneOne.Timestamp = time.Now().Add(-time.Hour * 24).UnixNano()
- tombstoneTwo := sync.NewTombstone(testDomain, testProject, "config", "2")
+ tombstoneTwo := sync.NewTombstone(testDomain, testProject, testResourceType, "2")
tombstoneTwo.Timestamp = time.Now().Add(-time.Hour * 23).UnixNano()
- tombstoneThree := sync.NewTombstone(testDomain, testProject, "config", "3")
+ tombstoneThree := sync.NewTombstone(testDomain, testProject, testResourceType, "3")
tombstoneThree.Timestamp = time.Now().Add(-time.Hour * 25).UnixNano()
t.Run("to create three tasks for next get delete and list operations, should pass", func(t *testing.T) {
@@ -67,39 +68,54 @@
t.Run("list tombstone service", func(t *testing.T) {
listReq := model.ListTombstoneRequest{
- Domain: testDomain,
- Project: testProject,
BeforeTimestamp: time.Now().Add(-time.Hour * 24).UnixNano(),
}
tombstones, err := tombstone.List(context.Background(), &listReq)
assert.Nil(t, err)
assert.Equal(t, 2, len(tombstones))
+ assert.Equal(t, true, checkTombstoneData(tombstones))
})
t.Run("delete expire tombstone service", func(t *testing.T) {
err := synctombstone.DeleteExpireTombStone()
assert.Nil(t, err)
- listReq := model.ListTombstoneRequest{
- Domain: testDomain,
- Project: testProject,
- }
+ listReq := model.ListTombstoneRequest{}
tombstones, err := tombstone.List(context.Background(), &listReq)
assert.Nil(t, err)
assert.Equal(t, 1, len(tombstones))
-
+ assert.Equal(t, true, checkTombstoneData(tombstones))
})
t.Run("delete all tombstones test data", func(t *testing.T) {
- listReq := model.ListTombstoneRequest{
- Domain: testDomain,
- Project: testProject,
- }
+ listReq := model.ListTombstoneRequest{}
tombstones, err := tombstone.List(context.Background(), &listReq)
assert.Nil(t, err)
-
+ assert.Equal(t, true, checkTombstoneData(tombstones))
err = tombstone.Delete(context.Background(), tombstones...)
assert.Nil(t, err)
})
}
+
+func checkTombstoneData(tombstones []*sync.Tombstone) bool {
+ if len(tombstones) <= 0 {
+ return true
+ }
+
+ for _, tombstone := range tombstones {
+ if tombstone.Domain != testDomain {
+ return false
+ }
+ if tombstone.Project != testProject {
+ return false
+ }
+ if tombstone.ResourceType != testResourceType {
+ return false
+ }
+ if len(tombstone.ResourceID) <= 0 {
+ return false
+ }
+ }
+ return true
+}