You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ha...@apache.org on 2023/05/09 00:15:00 UTC

[skywalking-banyandb] branch main updated: Fix resource corrupts caused by update indexRule operation (#272)

This is an automated email from the ASF dual-hosted git repository.

hanahmily pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git


The following commit(s) were added to refs/heads/main by this push:
     new b544c55b Fix resource corrupts caused by update indexRule operation (#272)
b544c55b is described below

commit b544c55b0fdad6044e1e14810a11f3a2663550ad
Author: hailin0 <wa...@apache.org>
AuthorDate: Tue May 9 08:14:53 2023 +0800

    Fix resource corrupts caused by update indexRule operation (#272)
---
 CHANGES.md                           |  1 +
 banyand/metadata/schema/etcd_test.go | 61 ++++++++++++++++++++++++++++++++++++
 banyand/metadata/schema/index.go     |  7 +++++
 3 files changed, 69 insertions(+)

diff --git a/CHANGES.md b/CHANGES.md
index e4510ffb..8b520926 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -20,6 +20,7 @@ Release Notes.
 ### Bugs
 
 - Fix iterator leaks and ensure proper closure and introduce a closer to guarantee all iterators are closed
+- Fix resource corrupts caused by update indexRule operation
 
 ### Chores
 
diff --git a/banyand/metadata/schema/etcd_test.go b/banyand/metadata/schema/etcd_test.go
index 22bb5a01..4b8136a8 100644
--- a/banyand/metadata/schema/etcd_test.go
+++ b/banyand/metadata/schema/etcd_test.go
@@ -521,3 +521,64 @@ func Test_Notify(t *testing.T) {
 		})
 	}
 }
+
+func Test_Etcd_Entity_Update(t *testing.T) {
+	tester := assert.New(t)
+	registry, err := NewEtcdSchemaRegistry(useRandomPort(), useRandomTempDir())
+	tester.NoError(err)
+	tester.NotNil(registry)
+	defer registry.Close()
+
+	err = preloadSchema(registry)
+	tester.NoError(err)
+
+	tests := []struct {
+		updateFunc     func(context.Context, Registry) error
+		validationFunc func(context.Context, Registry) bool
+		name           string
+	}{
+		{
+			name: "update indexRule when none metadata-id",
+			updateFunc: func(ctx context.Context, r Registry) error {
+				ir, err := r.GetIndexRule(ctx, &commonv1.Metadata{
+					Name:  "db.instance",
+					Group: "default",
+				})
+				if err != nil {
+					return err
+				}
+				// reset
+				ir.Metadata.Id = 0
+				ir.Type = databasev1.IndexRule_TYPE_TREE
+				return r.UpdateIndexRule(ctx, ir)
+			},
+			validationFunc: func(ctx context.Context, r Registry) bool {
+				ir, err := r.GetIndexRule(ctx, &commonv1.Metadata{
+					Name:  "db.instance",
+					Group: "default",
+				})
+				if err != nil {
+					return false
+				}
+				if ir.Metadata.Id != 1 {
+					return false
+				}
+				if ir.Type != databasev1.IndexRule_TYPE_TREE {
+					return false
+				}
+				return true
+			},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			req := require.New(t)
+
+			err := tt.updateFunc(context.TODO(), registry)
+			req.NoError(err)
+
+			req.True(tt.validationFunc(context.TODO(), registry))
+		})
+	}
+}
diff --git a/banyand/metadata/schema/index.go b/banyand/metadata/schema/index.go
index f8e38809..cfda6c6c 100644
--- a/banyand/metadata/schema/index.go
+++ b/banyand/metadata/schema/index.go
@@ -145,6 +145,13 @@ func (e *etcdSchemaRegistry) CreateIndexRule(ctx context.Context, indexRule *dat
 }
 
 func (e *etcdSchemaRegistry) UpdateIndexRule(ctx context.Context, indexRule *databasev1.IndexRule) error {
+	if indexRule.Metadata.Id == 0 {
+		existingIndexRule, err := e.GetIndexRule(ctx, indexRule.Metadata)
+		if err != nil {
+			return err
+		}
+		indexRule.Metadata.Id = existingIndexRule.Metadata.Id
+	}
 	return e.update(ctx, Metadata{
 		TypeMeta: TypeMeta{
 			Kind:  KindIndexRule,