Skip to content

Commit

Permalink
feat: nacos configstore component support pagination.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb0225 committed Jun 16, 2023
1 parent 1e36ff0 commit 2f3ce66
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 15 deletions.
1 change: 0 additions & 1 deletion components/configstores/nacos/change_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type subscriberHolder struct {
type subscriberKey struct {
group string
key string
label string
}

func newSubscriberHolder() *subscriberHolder {
Expand Down
64 changes: 50 additions & 14 deletions components/configstores/nacos/configstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
98 changes: 98 additions & 0 deletions components/configstores/nacos/configstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2f3ce66

Please sign in to comment.