Skip to content

Commit

Permalink
fix: configuration API returns wrong store_name (#641)
Browse files Browse the repository at this point in the history
* fix: configuration API returns wrong app_id

* fix: configuration API returns wrong store_name

* Update etcdv3.go

* Update etcdv3_test.go

* Update types.go

* Update api_configuration.go

* Update types.go

* Update etcdv3.go

* fix: configuration API returns wrong app_id

* fix: apollo configuration API returns wrong store_name

* delete unused const

* delete: redundant assign line

* fix: unit test of etcdv3 subscribe

* delete: subscribereq redundant storename field

Co-authored-by: seeflood <[email protected]>
Co-authored-by: Xunzhuo <[email protected]>
Co-authored-by: wenxuwan <[email protected]>
  • Loading branch information
4 people authored Jun 21, 2022
1 parent eb34d2a commit 66e64e9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion components/configstores/apollo/change_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type RepoForListener interface {
splitKey(keyWithLabel string) (key string, label string)
getAllTags(group string, keyWithLabel string) (tags map[string]string, err error)
GetAppId() string
GetStoreName() string
}

func newChangeListener(c RepoForListener) *changeListener {
Expand Down Expand Up @@ -79,7 +80,7 @@ func (lis *changeListener) notify(s *subscriber, keyWithLabel string, change *st
}
}()
// 2 prepare response
res := &configstores.SubscribeResp{StoreName: storename, AppId: lis.store.GetAppId()}
res := &configstores.SubscribeResp{StoreName: lis.store.GetStoreName(), AppId: lis.store.GetAppId()}
item := &configstores.ConfigurationItem{}
item.Group = s.group
item.Key, item.Label = lis.store.splitKey(keyWithLabel)
Expand Down
11 changes: 9 additions & 2 deletions components/configstores/apollo/change_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
"mosn.io/layotto/components/configstores"
)

const testAppId = "test_app"
const (
testAppId = "test_app"
testStoreName = "test_storename"
)

type MockRepo struct {
c *ConfigStore
Expand All @@ -45,6 +48,10 @@ func (m *MockRepo) GetAppId() string {
return testAppId
}

func (m *MockRepo) GetStoreName() string {
return testStoreName
}

const ns = "application"

func setupChangeListener() *changeListener {
Expand All @@ -69,7 +76,7 @@ func Test_changeListener_OnChange(t *testing.T) {
go func() {
select {
case c2 := <-ch:
assert.Equal(t, c2.StoreName, "apollo")
assert.Equal(t, c2.StoreName, testStoreName)
assert.Equal(t, c2.AppId, testAppId)
assert.True(t, len(c2.Items) == 1)
assert.True(t, c2.Items[0].Key == "key1")
Expand Down
8 changes: 8 additions & 0 deletions components/configstores/apollo/configstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (c *ConfigStore) doInit(config *configstores.StoreConfig) error {
kvRepoConfig := &repoConfig{
addr: addr,
appId: appId,
storeName: config.StoreName,
env: c.env,
cluster: metadata["cluster"],
namespaceName: metadata["namespace_name"],
Expand Down Expand Up @@ -209,6 +210,13 @@ func (c *ConfigStore) GetAppId() string {
return c.kvConfig.appId
}

func (c *ConfigStore) GetStoreName() string {
if c.kvConfig == nil {
return ""
}
return c.kvConfig.storeName
}

// Get gets configuration from configuration store.
func (c *ConfigStore) Get(ctx context.Context, req *configstores.GetRequest) ([]*configstores.ConfigurationItem, error) {
// TODO forced pagination
Expand Down
5 changes: 5 additions & 0 deletions components/configstores/apollo/configstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func TestConfigStore_read(t *testing.T) {
assert.True(t, store.GetAppId() == "testApplication_yang")
assert.True(t, store.GetDefaultGroup() != "")
assert.True(t, store.GetDefaultLabel() == "")

// get storeName
assert.True(t, store.GetStoreName() == "config_demo")

// get key
var req configstores.GetRequest
req.AppId = appId
Expand Down Expand Up @@ -255,6 +259,7 @@ func setup(t *testing.T) (*ConfigStore, *configstores.StoreConfig) {
"address": [
"http://106.54.227.205:8080"
],
"store_name": "config_demo",
"metadata": {
"app_id": "testApplication_yang",
"cluster": "default",
Expand Down
1 change: 0 additions & 1 deletion components/configstores/apollo/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package apollo

const (
storename = "apollo"
defaultTagsNamespace = "sidecar_config_tags"
defaultDelimiter = "@$"
defaultNamespace = "application"
Expand Down
1 change: 1 addition & 0 deletions components/configstores/apollo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Repository interface {
type repoConfig struct {
addr string
appId string
storeName string
env string
cluster string
namespaceName string
Expand Down
4 changes: 3 additions & 1 deletion components/configstores/etcdv3/etcdv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type EtcdV3ConfigStore struct {
sync.RWMutex
subscribeKey map[string]string
appIdKey string
storeName string
// cancel is the func, call cancel will stop watching on the appIdKey
cancel context.CancelFunc
watchStarted bool
Expand Down Expand Up @@ -70,6 +71,7 @@ func (c *EtcdV3ConfigStore) Init(config *configstores.StoreConfig) error {
Endpoints: config.Address,
DialTimeout: time.Duration(t) * time.Second,
})
c.storeName = config.StoreName
return err
}

Expand Down Expand Up @@ -172,7 +174,7 @@ func (c *EtcdV3ConfigStore) Delete(ctx context.Context, req *configstores.Delete
}

func (c *EtcdV3ConfigStore) processWatchResponse(resp *clientv3.WatchResponse) {
res := &configstores.SubscribeResp{StoreName: "etcd", AppId: c.appIdKey}
res := &configstores.SubscribeResp{StoreName: c.storeName, AppId: c.appIdKey}
item := &configstores.ConfigurationItem{}
if len(resp.Events) == 0 {
return
Expand Down
3 changes: 2 additions & 1 deletion components/configstores/etcdv3/etcdv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,13 @@ func (suite *ClientTestSuite) Subscribe() {
if i == 0 {
assert.Equal(suite.T(), event.Items[0].Key, "sofa")
assert.Equal(suite.T(), event.Items[0].Content, "v1")
assert.Equal(suite.T(), event.StoreName, "etcd")
i++
continue
}
assert.Equal(suite.T(), event.Items[0].Key, "sofa")
assert.Equal(suite.T(), event.Items[0].Content, "")

assert.Equal(suite.T(), event.StoreName, "etcd")
}
wg.Done()
}
Expand Down
1 change: 1 addition & 0 deletions pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func (m *MosnRuntime) initConfigStores(configStores ...*configstores.StoreFactor
m.errInt(err, "create configstore's component %s failed", name)
return err
}
config.StoreName = name
if err := c.Init(&config); err != nil {
m.errInt(err, "init configstore's component %s failed", name)
return err
Expand Down

0 comments on commit 66e64e9

Please sign in to comment.