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 8343022  [feat] fix delete tombstone data bug (#1261)
8343022 is described below

commit 834302205d7eeb253a37132087eb6088adc093ca
Author: xiaoluoluo <[email protected]>
AuthorDate: Tue Feb 15 14:52:59 2022 +0800

    [feat] fix delete tombstone data bug (#1261)
    
    Co-authored-by: [email protected] <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>
---
 eventbase/datasource/etcd/key/key.go               | 17 +++++--
 .../datasource/etcd/tombstone/tombstone_dao.go     | 21 +++++++++
 syncer/service/tombstone/tombstone_test.go         | 52 ++++++++++++++--------
 3 files changed, 68 insertions(+), 22 deletions(-)

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 @@ import (
 )
 
 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 TombstoneList(domain, project string) string {
 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 @@ func (d *Dao) List(ctx context.Context, options 
...datasource.TombstoneFindOptio
                if !filterMatch(&tombstone, opts) {
                        continue
                }
+               formatTombstoneKey(&tombstone, kv.Key)
                tombstones = append(tombstones, &tombstone)
        }
        return tombstones, nil
@@ -111,3 +112,23 @@ func filterMatch(tombstone *sync.Tombstone, options 
datasource.TombstoneFindOpti
        }
        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 @@ package tombstone_test
 
 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 @@ func init() {
 }
 
 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 @@ func TestDeleteExpireTombStone(t *testing.T) {
 
        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
+}

Reply via email to