diff --git a/components/configstores/nacos/change_listener.go b/components/configstores/nacos/change_listener.go index 41236ac0c2..94e648f87f 100644 --- a/components/configstores/nacos/change_listener.go +++ b/components/configstores/nacos/change_listener.go @@ -24,7 +24,6 @@ type subscriberHolder struct { type subscriberKey struct { group string key string - label string } func newSubscriberHolder() *subscriberHolder { diff --git a/components/configstores/nacos/configstore.go b/components/configstores/nacos/configstore.go index 65ca3653db..e057926d42 100644 --- a/components/configstores/nacos/configstore.go +++ b/components/configstores/nacos/configstore.go @@ -63,7 +63,7 @@ func (n *ConfigStore) Init(config *configstores.StoreConfig) (err error) { } // the nacos's addresses, required if not using acm mode. - if len(config.Address) == 0 && metadata.OpenKMS == false { + if len(config.Address) == 0 && !metadata.OpenKMS { return errConfigMissingField("address") } @@ -182,31 +182,65 @@ func (n *ConfigStore) initWithACM(timeoutMs uint64, metadata *Metadata) (config_ // Get gets configuration from configuration store. func (n *ConfigStore) Get(ctx context.Context, request *configstores.GetRequest) ([]*configstores.ConfigurationItem, error) { - // todo: pagenation // use the configuration's app_name instead of the app_id in request // 0. check if illegal if request.Group == "" && len(request.Keys) > 0 { request.Group = defaultGroup } - // 1. app level + // 1. get pagination information + pagination := n.getPagination(request.Metadata) + + // 2. app level if request.Group == "" { - return n.getAllWithAppId(ctx) + return n.getAllWithAppId(ctx, pagination) } - // 2.group level + // 3.group level if len(request.Keys) == 0 { - return n.getAllWithGroup(ctx, request.Group) + return n.getAllWithGroup(ctx, request.Group, pagination) } - // 3.key level + // 4.key level return n.getAllWithKeys(ctx, request.Group, request.Keys) } -func (n *ConfigStore) getAllWithAppId(ctx context.Context) ([]*configstores.ConfigurationItem, error) { +const ( + PageNo = "PageNo" + PageSize = "PageSize" +) + +type Pagination struct { + PageNo int + PageSize int +} + +func (n *ConfigStore) getPagination(metadata map[string]string) *Pagination { + res := &Pagination{} + if v, ok := metadata[PageNo]; ok { + pageNo, err := strconv.Atoi(v) + if err != nil { + return &Pagination{0, 0} + } + res.PageNo = pageNo + } + if v, ok := metadata[PageSize]; ok { + pageSize, err := strconv.Atoi(v) + if err != nil { + return &Pagination{0, 0} + } + res.PageSize = pageSize + } + + return res +} + +func (n *ConfigStore) getAllWithAppId(ctx context.Context, pagination *Pagination) ([]*configstores.ConfigurationItem, error) { values, err := n.client.SearchConfig(vo.SearchConfigParam{ - Search: "accurate", - AppName: n.appName, + Search: "accurate", + AppName: n.appName, + PageNo: pagination.PageNo, + PageSize: pagination.PageSize, }) if err != nil { log.DefaultLogger.Errorf("fail get all app_id key-value,err: %+v", err) @@ -226,11 +260,13 @@ func (n *ConfigStore) getAllWithAppId(ctx context.Context) ([]*configstores.Conf return res, nil } -func (n *ConfigStore) getAllWithGroup(ctx context.Context, group string) ([]*configstores.ConfigurationItem, error) { +func (n *ConfigStore) getAllWithGroup(ctx context.Context, group string, pagination *Pagination) ([]*configstores.ConfigurationItem, error) { values, err := n.client.SearchConfig(vo.SearchConfigParam{ - Search: "accurate", - AppName: n.appName, - Group: group, + Search: "accurate", + AppName: n.appName, + Group: group, + PageNo: pagination.PageNo, + PageSize: pagination.PageSize, }) if err != nil { log.DefaultLogger.Errorf("fail get all group key-value,err: %+v", err) diff --git a/components/configstores/nacos/configstore_test.go b/components/configstores/nacos/configstore_test.go index 11928b7864..967c1f1d0c 100644 --- a/components/configstores/nacos/configstore_test.go +++ b/components/configstores/nacos/configstore_test.go @@ -289,6 +289,104 @@ func TestNacosConfigStore_Get(t *testing.T) { } assert.EqualValues(t, expect, get) }) + + t.Run("test get with pagination", func(t *testing.T) { + mockClient := getMockNacosClient(t) + params := &configstores.GetRequest{ + AppId: appName, // different from app stored in the nacos instance + Metadata: map[string]string{ + PageNo: "10", + PageSize: "2", + }, + } + + mockClient.EXPECT().SearchConfig(gomock.Eq(vo.SearchConfigParam{ + Search: "accurate", + AppName: appName, // app name that stored in the store instance + PageNo: 10, + PageSize: 2, + })).Return(&model.ConfigPage{ + PageItems: []model.ConfigItem{ + { + DataId: "key1", + Group: "group1", + Appname: appName, + Content: content, + }, + { + DataId: "key2", + Group: "group2", + Appname: appName, + Content: content, + }, + }, + }, nil) + store := setup(t, mockClient) + get, err := store.Get(context.Background(), params) + assert.Nil(t, err) + expect := []*configstores.ConfigurationItem{ + { + Key: "key1", + Group: "group1", + Content: content, + }, + { + Key: "key2", + Group: "group2", + Content: content, + }, + } + assert.EqualValues(t, expect, get) + }) + + t.Run("test get with wrong pagination", func(t *testing.T) { + mockClient := getMockNacosClient(t) + params := &configstores.GetRequest{ + AppId: appName, // different from app stored in the nacos instance + Metadata: map[string]string{ + PageNo: "10a", + PageSize: "2", + }, + } + + mockClient.EXPECT().SearchConfig(gomock.Eq(vo.SearchConfigParam{ + Search: "accurate", + AppName: appName, // app name that stored in the store instance + PageNo: 0, + PageSize: 0, + })).Return(&model.ConfigPage{ + PageItems: []model.ConfigItem{ + { + DataId: "key1", + Group: "group1", + Appname: appName, + Content: content, + }, + { + DataId: "key2", + Group: "group2", + Appname: appName, + Content: content, + }, + }, + }, nil) + store := setup(t, mockClient) + get, err := store.Get(context.Background(), params) + assert.Nil(t, err) + expect := []*configstores.ConfigurationItem{ + { + Key: "key1", + Group: "group1", + Content: content, + }, + { + Key: "key2", + Group: "group2", + Content: content, + }, + } + assert.EqualValues(t, expect, get) + }) } func TestNacosConfigStore_GetDefaultGroup(t *testing.T) {