Skip to content

Commit

Permalink
enhance: [2.4] support to config the default db properties
Browse files Browse the repository at this point in the history
Signed-off-by: SimFG <[email protected]>
  • Loading branch information
SimFG committed Dec 5, 2024
1 parent 2655d26 commit d5b55fe
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 24 deletions.
4 changes: 2 additions & 2 deletions internal/metastore/model/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func NewDatabase(id int64, name string, state pb.DatabaseState, properties []*co
}
}

func NewDefaultDatabase() *Database {
return NewDatabase(util.DefaultDBID, util.DefaultDBName, pb.DatabaseState_DatabaseCreated, nil)
func NewDefaultDatabase(prop []*commonpb.KeyValuePair) *Database {
return NewDatabase(util.DefaultDBID, util.DefaultDBName, pb.DatabaseState_DatabaseCreated, prop)
}

func (c *Database) Available() bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/metastore/model/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func TestDatabaseCloneAndEqual(t *testing.T) {

func TestDatabaseAvailable(t *testing.T) {
assert.True(t, dbModel.Available())
assert.True(t, NewDefaultDatabase().Available())
assert.True(t, NewDefaultDatabase(nil).Available())
}
4 changes: 2 additions & 2 deletions internal/rootcoord/create_collection_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ func Test_createCollectionTask_Prepare(t *testing.T) {
mock.Anything,
mock.Anything,
mock.Anything,
).Return(model.NewDefaultDatabase(), nil)
).Return(model.NewDefaultDatabase(nil), nil)
meta.On("ListAllAvailCollections",
mock.Anything,
).Return(map[int64][]int64{
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func Test_createCollectionTask_PartitionKey(t *testing.T) {
mock.Anything,
mock.Anything,
mock.Anything,
).Return(model.NewDefaultDatabase(), nil)
).Return(model.NewDefaultDatabase(nil), nil)
meta.On("ListAllAvailCollections",
mock.Anything,
).Return(map[int64][]int64{
Expand Down
4 changes: 2 additions & 2 deletions internal/rootcoord/create_db_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Test_CreateDBTask_Prepare(t *testing.T) {
cfgMaxDatabaseNum := Params.RootCoordCfg.MaxDatabaseNum.GetAsInt()
dbs := make([]*model.Database, 0, cfgMaxDatabaseNum)
for i := 0; i < cfgMaxDatabaseNum; i++ {
dbs = append(dbs, model.NewDefaultDatabase())
dbs = append(dbs, model.NewDefaultDatabase(nil))
}
meta.On("ListDatabases",
mock.Anything,
Expand All @@ -81,7 +81,7 @@ func Test_CreateDBTask_Prepare(t *testing.T) {
meta.On("ListDatabases",
mock.Anything,
mock.Anything).
Return([]*model.Database{model.NewDefaultDatabase()}, nil)
Return([]*model.Database{model.NewDefaultDatabase(nil)}, nil)

core := newTestCore(withMeta(meta), withValidIDAllocator())
paramtable.Get().Save(Params.RootCoordCfg.MaxDatabaseNum.Key, strconv.Itoa(10))
Expand Down
2 changes: 1 addition & 1 deletion internal/rootcoord/describe_db_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_describeDatabaseTask_Execute(t *testing.T) {
t.Run("describe with empty database name", func(t *testing.T) {
meta := mockrootcoord.NewIMetaTable(t)
meta.EXPECT().GetDatabaseByName(mock.Anything, mock.Anything, mock.Anything).
Return(model.NewDefaultDatabase(), nil)
Return(model.NewDefaultDatabase(nil), nil)
core := newTestCore(withMeta(meta))

task := &describeDBTask{
Expand Down
4 changes: 2 additions & 2 deletions internal/rootcoord/list_db_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Test_ListDBTask(t *testing.T) {
})

t.Run("ok", func(t *testing.T) {
ret := []*model.Database{model.NewDefaultDatabase()}
ret := []*model.Database{model.NewDefaultDatabase(nil)}
meta := mockrootcoord.NewIMetaTable(t)
meta.On("ListDatabases",
mock.Anything,
Expand Down Expand Up @@ -89,7 +89,7 @@ func Test_ListDBTask(t *testing.T) {
t.Run("list db with auth", func(t *testing.T) {
Params.Save(Params.CommonCfg.AuthorizationEnabled.Key, "true")
defer Params.Reset(Params.CommonCfg.AuthorizationEnabled.Key)
ret := []*model.Database{model.NewDefaultDatabase()}
ret := []*model.Database{model.NewDefaultDatabase(nil)}
meta := mockrootcoord.NewIMetaTable(t)

core := newTestCore(withMeta(meta))
Expand Down
8 changes: 7 additions & 1 deletion internal/rootcoord/meta_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,13 @@ func (mt *MetaTable) createDefaultDb() error {
return err
}

return mt.createDatabasePrivate(mt.ctx, model.NewDefaultDatabase(), ts)
s := Params.RootCoordCfg.DefaultDBProperties.GetValue()
defaultProperties, err := funcutil.String2KeyValuePair(s)
if err != nil {
return err
}

return mt.createDatabasePrivate(mt.ctx, model.NewDefaultDatabase(defaultProperties), ts)
}

func (mt *MetaTable) CreateDatabase(ctx context.Context, db *model.Database, ts typeutil.Timestamp) error {
Expand Down
26 changes: 13 additions & 13 deletions internal/rootcoord/meta_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ func TestMetaTable_getCollectionByIDInternal(t *testing.T) {
meta := &MetaTable{
catalog: catalog,
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
collID2Meta: map[typeutil.UniqueID]*model.Collection{},
}
Expand All @@ -480,7 +480,7 @@ func TestMetaTable_getCollectionByIDInternal(t *testing.T) {
meta := &MetaTable{
catalog: catalog,
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
collID2Meta: map[typeutil.UniqueID]*model.Collection{},
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func TestMetaTable_GetCollectionByName(t *testing.T) {
).Return(nil, errors.New("error mock GetCollectionByName"))
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
names: newNameDb(),
aliases: newNameDb(),
Expand All @@ -639,7 +639,7 @@ func TestMetaTable_GetCollectionByName(t *testing.T) {
).Return(&model.Collection{State: pb.CollectionState_CollectionDropped}, nil)
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
names: newNameDb(),
aliases: newNameDb(),
Expand Down Expand Up @@ -669,7 +669,7 @@ func TestMetaTable_GetCollectionByName(t *testing.T) {

meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
names: newNameDb(),
aliases: newNameDb(),
Expand Down Expand Up @@ -1206,7 +1206,7 @@ func TestMetaTable_reload(t *testing.T) {
catalog.On("ListDatabases",
mock.Anything,
mock.Anything,
).Return([]*model.Database{model.NewDefaultDatabase()}, nil)
).Return([]*model.Database{model.NewDefaultDatabase(nil)}, nil)
catalog.On("ListCollections",
mock.Anything,
mock.Anything,
Expand Down Expand Up @@ -1508,7 +1508,7 @@ func TestMetaTable_RenameCollection(t *testing.T) {
t.Run("new collection name already exist-1", func(t *testing.T) {
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
names: newNameDb(),
aliases: newNameDb(),
Expand All @@ -1535,7 +1535,7 @@ func TestMetaTable_RenameCollection(t *testing.T) {
).Return(nil, errors.New("error mock GetCollectionByID"))
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
catalog: catalog,
names: newNameDb(),
Expand Down Expand Up @@ -1565,7 +1565,7 @@ func TestMetaTable_RenameCollection(t *testing.T) {

meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
catalog: catalog,
names: newNameDb(),
Expand All @@ -1592,7 +1592,7 @@ func TestMetaTable_RenameCollection(t *testing.T) {
).Return(nil, merr.WrapErrCollectionNotFound("error"))
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
"db1": model.NewDatabase(2, "db1", pb.DatabaseState_DatabaseCreated, nil),
},
catalog: catalog,
Expand Down Expand Up @@ -1622,7 +1622,7 @@ func TestMetaTable_RenameCollection(t *testing.T) {
).Return(nil, merr.WrapErrCollectionNotFound("error"))
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
"db1": model.NewDatabase(2, "db1", pb.DatabaseState_DatabaseCreated, nil),
},
catalog: catalog,
Expand Down Expand Up @@ -1659,7 +1659,7 @@ func TestMetaTable_RenameCollection(t *testing.T) {
).Return(nil, merr.WrapErrCollectionNotFound("error"))
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
},
catalog: catalog,
names: newNameDb(),
Expand Down Expand Up @@ -1925,7 +1925,7 @@ func TestMetaTable_EmtpyDatabaseName(t *testing.T) {
mt := &MetaTable{
names: newNameDb(),
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
util.DefaultDBName: model.NewDefaultDatabase(nil),
"db2": model.NewDatabase(2, "db2", pb.DatabaseState_DatabaseCreated, nil),
},
collID2Meta: map[typeutil.UniqueID]*model.Collection{
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/funcutil/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ func GetVecFieldIDs(schema *schemapb.CollectionSchema) []int64 {
return vecFieldIDs
}

func String2KeyValuePair(v string) ([]*commonpb.KeyValuePair, error) {
m := make(map[string]string)
err := json.Unmarshal([]byte(v), &m)
if err != nil {
return nil, err
}
return Map2KeyValuePair(m), nil
}

func Map2KeyValuePair(datas map[string]string) []*commonpb.KeyValuePair {
results := make([]*commonpb.KeyValuePair, len(datas))
offset := 0
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/funcutil/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,24 @@ func (s *NumRowsWithSchemaSuite) TestErrorCases() {
func TestNumRowsWithSchema(t *testing.T) {
suite.Run(t, new(NumRowsWithSchemaSuite))
}

func TestString2KeyValuePair(t *testing.T) {
t.Run("normal", func(t *testing.T) {
kvs, err := String2KeyValuePair("{\"key\": \"value\"}")
assert.NoError(t, err)
assert.Len(t, kvs, 1)
assert.Equal(t, "key", kvs[0].Key)
assert.Equal(t, "value", kvs[0].Value)
})

t.Run("err", func(t *testing.T) {
_, err := String2KeyValuePair("{aa}")
assert.Error(t, err)
})

t.Run("empty", func(t *testing.T) {
kvs, err := String2KeyValuePair("{}")
assert.NoError(t, err)
assert.Len(t, kvs, 0)
})
}
10 changes: 10 additions & 0 deletions pkg/util/paramtable/component_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ type rootCoordConfig struct {
MaxGeneralCapacity ParamItem `refreshable:"true"`
GracefulStopTimeout ParamItem `refreshable:"true"`
UseLockScheduler ParamItem `refreshable:"true"`
DefaultDBProperties ParamItem `refreshable:"false"`
}

func (p *rootCoordConfig) init(base *BaseTable) {
Expand Down Expand Up @@ -1217,6 +1218,15 @@ Segments with smaller size than this parameter will not be indexed, and will be
Export: false,
}
p.UseLockScheduler.Init(base.mgr)

p.DefaultDBProperties = ParamItem{
Key: "rootCoord.defaultDBProperties",
Version: "2.4.16",
DefaultValue: "{}",
Doc: "default db properties, should be a json string",
Export: false,
}
p.DefaultDBProperties.Init(base.mgr)
}

// /////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/paramtable/component_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func TestComponentParam(t *testing.T) {
params.Save("rootCoord.gracefulStopTimeout", "100")
assert.Equal(t, 100*time.Second, Params.GracefulStopTimeout.GetAsDuration(time.Second))

assert.Equal(t, "{}", Params.DefaultDBProperties.GetValue())
params.Save("rootCoord.defaultDBProperties", "{\"key\":\"value\"}")
assert.Equal(t, "{\"key\":\"value\"}", Params.DefaultDBProperties.GetValue())

SetCreateTime(time.Now())
SetUpdateTime(time.Now())
})
Expand Down

0 comments on commit d5b55fe

Please sign in to comment.