Skip to content

Commit

Permalink
[fix]tombstone's key path in etcd is not correct
Browse files Browse the repository at this point in the history
  • Loading branch information
tornado-ssy committed Mar 29, 2024
1 parent 8d41759 commit f5c64e6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 35 deletions.
20 changes: 0 additions & 20 deletions server/datasource/etcd/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,8 @@ const (
keyCounter = "counter"
keyHistory = "kv-history"
keyTrack = "track"
syncer = "syncer"
task = "task"
tombstone = "tombstone"
)

func getSyncRootKey() string {
return split + syncer + split + task
}

func getTombstoneRootKey() string {
return split + tombstone
}

func TaskKey(domain, project, taskID string, timestamp int64) string {
strTimestamp := strconv.FormatInt(timestamp, 10)
return strings.Join([]string{getSyncRootKey(), domain, project, strTimestamp, taskID}, split)
}

func TombstoneKey(domain, project, resourceType, resourceID string) string {
return strings.Join([]string{getTombstoneRootKey(), domain, project, resourceType, resourceID}, split)
}

func KV(domain, project, kvID string) string {
return strings.Join([]string{keyKV, domain, project, kvID}, split)
}
Expand Down
14 changes: 8 additions & 6 deletions server/datasource/etcd/kv/kv_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"github.com/apache/servicecomb-kie/server/datasource"
"github.com/apache/servicecomb-kie/server/datasource/auth"
"github.com/apache/servicecomb-kie/server/datasource/etcd/key"

eventbase "github.com/apache/servicecomb-service-center/eventbase/datasource/etcd/key"
)

// Dao operate data in mongodb
Expand Down Expand Up @@ -95,7 +97,7 @@ func txnCreate(ctx context.Context, kv *model.KVDoc) (bool, error) {
return false, err
}
kvOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.KV(kv.Domain, kv.Project, kv.ID)), etcdadpt.WithValue(kvBytes))
taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.TaskKey(kv.Domain, kv.Project, task.ID, task.Timestamp)), etcdadpt.WithValue(taskBytes))
taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(eventbase.TaskKey(kv.Domain, kv.Project, task.ID, task.Timestamp)), etcdadpt.WithValue(taskBytes))
resp, err := etcdadpt.TxnWithCmp(ctx, []etcdadpt.OpOptions{kvOpPut, taskOpPut},
etcdadpt.If(etcdadpt.NotExistKey(string(kvOpPut.Key)), etcdadpt.NotExistKey(string(taskOpPut.Key))), nil)
if err != nil {
Expand Down Expand Up @@ -165,7 +167,7 @@ func txnUpdate(ctx context.Context, kv *model.KVDoc) error {
return err
}
kvOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(keyKV), etcdadpt.WithValue(kvBytes))
taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.TaskKey(kv.Domain, kv.Project, task.ID, task.Timestamp)), etcdadpt.WithValue(taskBytes))
taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(eventbase.TaskKey(kv.Domain, kv.Project, task.ID, task.Timestamp)), etcdadpt.WithValue(taskBytes))
return etcdadpt.Txn(ctx, []etcdadpt.OpOptions{kvOpPut, taskOpPut})
}

Expand Down Expand Up @@ -295,9 +297,9 @@ func txnFindOneAndDelete(ctx context.Context, kvID, project, domain string) (*mo
return nil, err
}
kvOpDel := etcdadpt.OpDel(etcdadpt.WithStrKey(kvKey))
taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.TaskKey(domain, project,
taskOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(eventbase.TaskKey(domain, project,
task.ID, task.Timestamp)), etcdadpt.WithValue(taskBytes))
tombstoneOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(key.TombstoneKey(domain, project, tombstone.ResourceType, tombstone.ResourceID)), etcdadpt.WithValue(tombstoneBytes))
tombstoneOpPut := etcdadpt.OpPut(etcdadpt.WithStrKey(eventbase.TombstoneKey(domain, project, tombstone.ResourceType, tombstone.ResourceID)), etcdadpt.WithValue(tombstoneBytes))
err = etcdadpt.Txn(ctx, []etcdadpt.OpOptions{kvOpDel, taskOpPut, tombstoneOpPut})
if err != nil {
openlog.Error("find and delete error", openlog.WithTags(openlog.Tags{
Expand Down Expand Up @@ -427,7 +429,7 @@ func txnFindManyAndDelete(ctx context.Context, kvIDs []string, project, domain s
openlog.Error("fail to marshal task" + err.Error())
return nil, 0, err
}
opOptions = append(opOptions, etcdadpt.OpPut(etcdadpt.WithStrKey(key.TaskKey(domain, project,
opOptions = append(opOptions, etcdadpt.OpPut(etcdadpt.WithStrKey(eventbase.TaskKey(domain, project,
task.ID, task.Timestamp)), etcdadpt.WithValue(taskBytes)))
}
for _, tombstone := range tombstones {
Expand All @@ -436,7 +438,7 @@ func txnFindManyAndDelete(ctx context.Context, kvIDs []string, project, domain s
openlog.Error("fail to marshal tombstone" + err.Error())
return nil, 0, err
}
opOptions = append(opOptions, etcdadpt.OpPut(etcdadpt.WithStrKey(key.TombstoneKey(domain, project,
opOptions = append(opOptions, etcdadpt.OpPut(etcdadpt.WithStrKey(eventbase.TombstoneKey(domain, project,
tombstone.ResourceType, tombstone.ResourceID)), etcdadpt.WithValue(tombstoneBytes)))
}
err := etcdadpt.Txn(ctx, opOptions)
Expand Down
5 changes: 0 additions & 5 deletions server/datasource/kv_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/apache/servicecomb-kie/server/datasource"
kvsvc "github.com/apache/servicecomb-kie/server/service/kv"
"github.com/apache/servicecomb-kie/server/service/sync"
"github.com/apache/servicecomb-kie/test"
emodel "github.com/apache/servicecomb-service-center/eventbase/model"
"github.com/apache/servicecomb-service-center/eventbase/service/task"
"github.com/apache/servicecomb-service-center/eventbase/service/tombstone"
Expand Down Expand Up @@ -109,10 +108,6 @@ func TestList(t *testing.T) {
}

func TestWithSync(t *testing.T) {
if test.IsEmbeddedetcdMode() {
return
}

t.Run("create kv with sync enabled", func(t *testing.T) {
t.Run("creating a kv will create a task should pass", func(t *testing.T) {
// set the sync enabled
Expand Down
4 changes: 0 additions & 4 deletions test/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,3 @@ func randomListenAddress() string {
addr := fmt.Sprintf("127.0.0.1:%d", port)
return addr
}

func IsEmbeddedetcdMode() bool {
return kind == "embedded_etcd" || kind == "embedded_etcd_with_localstorage"
}

0 comments on commit f5c64e6

Please sign in to comment.