From 82bdf9a6a80a7e271def0df87fb0952b79e99ceb Mon Sep 17 00:00:00 2001 From: congqixia Date: Wed, 15 Jan 2025 19:23:01 +0800 Subject: [PATCH] fix: Add index param duplication check (#39289) Related to #39288 Signed-off-by: Congqi Xia --- internal/datacoord/index_service.go | 20 ++++++++++ internal/datacoord/index_service_test.go | 51 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/internal/datacoord/index_service.go b/internal/datacoord/index_service.go index 6139306e2b..d039bf20ae 100644 --- a/internal/datacoord/index_service.go +++ b/internal/datacoord/index_service.go @@ -282,6 +282,15 @@ func (s *Server) CreateIndex(ctx context.Context, req *indexpb.CreateIndexReques } func ValidateIndexParams(index *model.Index) error { + if err := CheckDuplidateKey(index.IndexParams, "indexParams"); err != nil { + return err + } + if err := CheckDuplidateKey(index.UserIndexParams, "userIndexParams"); err != nil { + return err + } + if err := CheckDuplidateKey(index.TypeParams, "typeParams"); err != nil { + return err + } indexType := GetIndexType(index.IndexParams) indexParams := funcutil.KeyValuePair2Map(index.IndexParams) userIndexParams := funcutil.KeyValuePair2Map(index.UserIndexParams) @@ -300,6 +309,17 @@ func ValidateIndexParams(index *model.Index) error { return nil } +func CheckDuplidateKey(kvs []*commonpb.KeyValuePair, tag string) error { + keySet := typeutil.NewSet[string]() + for _, kv := range kvs { + if keySet.Contain(kv.GetKey()) { + return merr.WrapErrParameterInvalidMsg("duplicate %s key in %s params", kv.GetKey(), tag) + } + keySet.Insert(kv.GetKey()) + } + return nil +} + func UpdateParams(index *model.Index, from []*commonpb.KeyValuePair, updates []*commonpb.KeyValuePair) []*commonpb.KeyValuePair { params := make(map[string]string) for _, param := range from { diff --git a/internal/datacoord/index_service_test.go b/internal/datacoord/index_service_test.go index 040fcfe124..b551a892f2 100644 --- a/internal/datacoord/index_service_test.go +++ b/internal/datacoord/index_service_test.go @@ -2510,4 +2510,55 @@ func TestValidateIndexParams(t *testing.T) { err := ValidateIndexParams(index) assert.Error(t, err) }) + + t.Run("duplicated_index_params", func(t *testing.T) { + index := &model.Index{ + IndexParams: []*commonpb.KeyValuePair{ + { + Key: common.IndexTypeKey, + Value: indexparamcheck.AutoIndex, + }, + { + Key: common.IndexTypeKey, + Value: indexparamcheck.AutoIndex, + }, + }, + } + err := ValidateIndexParams(index) + assert.Error(t, err) + }) + + t.Run("duplicated_user_index_params", func(t *testing.T) { + index := &model.Index{ + UserIndexParams: []*commonpb.KeyValuePair{ + { + Key: common.IndexTypeKey, + Value: indexparamcheck.AutoIndex, + }, + { + Key: common.IndexTypeKey, + Value: indexparamcheck.AutoIndex, + }, + }, + } + err := ValidateIndexParams(index) + assert.Error(t, err) + }) + + t.Run("duplicated_user_index_params", func(t *testing.T) { + index := &model.Index{ + TypeParams: []*commonpb.KeyValuePair{ + { + Key: common.IndexTypeKey, + Value: indexparamcheck.AutoIndex, + }, + { + Key: common.IndexTypeKey, + Value: indexparamcheck.AutoIndex, + }, + }, + } + err := ValidateIndexParams(index) + assert.Error(t, err) + }) }