From 319935e368821d722f5aa1b218fb47e81376c22e Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Fri, 8 Mar 2024 11:57:46 +0800 Subject: [PATCH 1/8] =?UTF-8?q?fix:=20=E8=A7=A6=E5=8F=91=E6=87=92=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E6=97=B6=EF=BC=8C=E9=9C=80=E8=A6=81=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0notify=E5=86=85=E5=AE=B9=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0component=E7=9B=91=E5=90=AC=E5=8F=98=E6=9B=B4=E7=9A=84?= =?UTF-8?q?ns=EF=BC=9B=E5=9B=A0=E4=B8=BAsync=E4=B9=9F=E4=BC=9A=E8=A7=A6?= =?UTF-8?q?=E5=8F=91component=E8=BF=94=E5=9B=9E=EF=BC=8C=E6=89=80=E4=BB=A5?= =?UTF-8?q?component=E4=B8=8D=E9=9C=80=E8=A6=81=E9=87=8D=E6=96=B0=20start?= =?UTF-8?q?=EF=BC=8C=E6=9C=9F=E9=97=B4=E4=B8=A4=E7=A7=92=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=B6=B3=E5=A4=9FconfigComponent=E5=8F=98=E6=9B=B4?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 15 ++++++++++----- env/config/config.go | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index c2b8ecb..42925b4 100644 --- a/client.go +++ b/client.go @@ -20,7 +20,6 @@ package agollo import ( "container/list" "errors" - "github.com/apolloconfig/agollo/v4/agcache" "github.com/apolloconfig/agollo/v4/agcache/memory" "github.com/apolloconfig/agollo/v4/cluster/roundrobin" @@ -158,19 +157,25 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config { return nil } - config := c.cache.GetConfig(namespace) + cfg := c.cache.GetConfig(namespace) - if config == nil { + if cfg == nil { //sync config apolloConfig := syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig) if apolloConfig != nil { + // update appConfig + c.appConfig.NamespaceName = c.appConfig.NamespaceName + config.Comma + namespace + c.appConfig.GetNotificationsMap().UpdateNotify(namespace, 0) + // update cache c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig) + // update configComponent + c.configComponent.SetAppConfig(c.getAppConfig) } } - config = c.cache.GetConfig(namespace) + cfg = c.cache.GetConfig(namespace) - return config + return cfg } // GetConfigCache 根据namespace获取apollo配置的缓存 diff --git a/env/config/config.go b/env/config/config.go index cea37e4..ebfde50 100644 --- a/env/config/config.go +++ b/env/config/config.go @@ -29,17 +29,17 @@ import ( var ( defaultNotificationID = int64(-1) - comma = "," + Comma = "," ) -//File 读写配置文件 +// File 读写配置文件 type File interface { Load(fileName string, unmarshal func([]byte) (interface{}, error)) (interface{}, error) Write(content interface{}, configPath string) error } -//AppConfig 配置文件 +// AppConfig 配置文件 type AppConfig struct { AppID string `json:"appId"` Cluster string `json:"cluster"` @@ -56,7 +56,7 @@ type AppConfig struct { currentConnApolloConfig *CurrentApolloConfig } -//ServerInfo 服务器信息 +// ServerInfo 服务器信息 type ServerInfo struct { AppName string `json:"appName"` InstanceID string `json:"instanceId"` @@ -64,19 +64,19 @@ type ServerInfo struct { IsDown bool `json:"-"` } -//GetIsBackupConfig whether backup config after fetch config from apollo -//false : no -//true : yes (default) +// GetIsBackupConfig whether backup config after fetch config from apollo +// false : no +// true : yes (default) func (a *AppConfig) GetIsBackupConfig() bool { return a.IsBackupConfig } -//GetBackupConfigPath GetBackupConfigPath +// GetBackupConfigPath GetBackupConfigPath func (a *AppConfig) GetBackupConfigPath() string { return a.BackupConfigPath } -//GetHost GetHost +// GetHost GetHost func (a *AppConfig) GetHost() string { u, err := url.Parse(a.IP) if err != nil { @@ -108,10 +108,10 @@ func (a *AppConfig) initAllNotifications(callback func(namespace string)) { } } -//SplitNamespaces 根据namespace字符串分割后,并执行callback函数 +// SplitNamespaces 根据namespace字符串分割后,并执行callback函数 func SplitNamespaces(namespacesStr string, callback func(namespace string)) sync.Map { namespaces := sync.Map{} - split := strings.Split(namespacesStr, comma) + split := strings.Split(namespacesStr, Comma) for _, namespace := range split { if callback != nil { callback(namespace) @@ -126,7 +126,7 @@ func (a *AppConfig) GetNotificationsMap() *notificationsMap { return a.notificationsMap } -//GetServicesConfigURL 获取服务器列表url +// GetServicesConfigURL 获取服务器列表url func (a *AppConfig) GetServicesConfigURL() string { return fmt.Sprintf("%sservices/config?appId=%s&ip=%s", a.GetHost(), From aae41953b3b4f9f1d848cd5180e6548ec6268c8f Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Fri, 8 Mar 2024 13:44:10 +0800 Subject: [PATCH 2/8] fix: ut fix --- client_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client_test.go b/client_test.go index 41ffc31..1edc765 100644 --- a/client_test.go +++ b/client_test.go @@ -20,6 +20,7 @@ package agollo import ( "encoding/json" "fmt" + "github.com/apolloconfig/agollo/v4/component/notify" "net/http" "net/http/httptest" "reflect" @@ -65,6 +66,7 @@ func createMockApolloConfig(expireTime int) *internalClient { configs["intSlice"] = []int{1, 2} client.cache.UpdateApolloConfigCache(configs, expireTime, storage.GetDefaultNamespace()) + client.configComponent = ¬ify.ConfigComponent{} return client } From 7a344b4597896cf8ec0c500155a2ec53baee5a71 Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Fri, 8 Mar 2024 13:47:12 +0800 Subject: [PATCH 3/8] fix: ut fix --- client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_test.go b/client_test.go index 1edc765..6121717 100644 --- a/client_test.go +++ b/client_test.go @@ -20,7 +20,6 @@ package agollo import ( "encoding/json" "fmt" - "github.com/apolloconfig/agollo/v4/component/notify" "net/http" "net/http/httptest" "reflect" @@ -30,6 +29,7 @@ import ( "github.com/agiledragon/gomonkey/v2" "github.com/apolloconfig/agollo/v4/agcache/memory" + "github.com/apolloconfig/agollo/v4/component/notify" "github.com/apolloconfig/agollo/v4/component/remote" "github.com/apolloconfig/agollo/v4/env/config" "github.com/apolloconfig/agollo/v4/env/server" From 5c0e64017677fd54a6d6f0b94ce3f6fc10951803 Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Mon, 25 Mar 2024 14:51:28 +0800 Subject: [PATCH 4/8] =?UTF-8?q?fix:=20=E5=8E=BB=E6=8E=89=20configComponent?= =?UTF-8?q?=20=E7=9A=84=E5=A4=9A=E4=BD=99=E8=B5=8B=E5=80=BC=EF=BC=88?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=9A=84=E6=97=B6=E5=80=99=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E5=85=B3=E8=81=94=E4=BA=86client=20=E7=9A=84=20appconfig?= =?UTF-8?q?=EF=BC=89&=20ut=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 2 -- client_test.go | 6 ++++-- component/notify/componet_notify_test.go | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 42925b4..296620f 100644 --- a/client.go +++ b/client.go @@ -168,8 +168,6 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config { c.appConfig.GetNotificationsMap().UpdateNotify(namespace, 0) // update cache c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig) - // update configComponent - c.configComponent.SetAppConfig(c.getAppConfig) } } diff --git a/client_test.go b/client_test.go index 6121717..418ee28 100644 --- a/client_test.go +++ b/client_test.go @@ -29,7 +29,6 @@ import ( "github.com/agiledragon/gomonkey/v2" "github.com/apolloconfig/agollo/v4/agcache/memory" - "github.com/apolloconfig/agollo/v4/component/notify" "github.com/apolloconfig/agollo/v4/component/remote" "github.com/apolloconfig/agollo/v4/env/config" "github.com/apolloconfig/agollo/v4/env/server" @@ -66,7 +65,6 @@ func createMockApolloConfig(expireTime int) *internalClient { configs["intSlice"] = []int{1, 2} client.cache.UpdateApolloConfigCache(configs, expireTime, storage.GetDefaultNamespace()) - client.configComponent = ¬ify.ConfigComponent{} return client } @@ -375,6 +373,10 @@ func TestGetConfigAndInitValNotNil(t *testing.T) { client := createMockApolloConfig(120) cf := client.GetConfig("testNotFound") Assert(t, cf, NotNilVal()) + + // appConfig notificationsMap appConfig should be updated + Assert(t, client.appConfig.GetNotificationsMap().GetNotify("testNotFound"), Equal(int64(0))) + // cache should be updated Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal()) Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testValue")) diff --git a/component/notify/componet_notify_test.go b/component/notify/componet_notify_test.go index ec3b04a..c05b58b 100644 --- a/component/notify/componet_notify_test.go +++ b/component/notify/componet_notify_test.go @@ -102,3 +102,22 @@ func getTestAppConfig() *config.AppConfig { appConfig.Init() return appConfig } + +func TestSetAppConfig(t *testing.T) { + var a *config.AppConfig = getTestAppConfig() + mockAppConfig := func() config.AppConfig { + return *a + } + + c := &ConfigComponent{} + c.SetAppConfig(mockAppConfig) + + // appConfig should be equal + Assert(t, c.appConfigFunc(), Equal(*a)) + + // appConfig value is be replaced + a.AppID = "test1" + a.NamespaceName = a.NamespaceName + config.Comma + "abc" + Assert(t, c.appConfigFunc().AppID, Equal("test1")) + Assert(t, c.appConfigFunc().NamespaceName, Equal("application,abc")) +} From a79a21aeaf35aed3a8f6b7122c5fc75b40faf4aa Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Mon, 25 Mar 2024 14:59:09 +0800 Subject: [PATCH 5/8] fix: ut fix --- component/notify/componet_notify_test.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/component/notify/componet_notify_test.go b/component/notify/componet_notify_test.go index c05b58b..bc172e7 100644 --- a/component/notify/componet_notify_test.go +++ b/component/notify/componet_notify_test.go @@ -103,21 +103,19 @@ func getTestAppConfig() *config.AppConfig { return appConfig } -func TestSetAppConfig(t *testing.T) { - var a *config.AppConfig = getTestAppConfig() - mockAppConfig := func() config.AppConfig { - return *a - } - +func TestConfigComponent_SetAppConfig_UpdatesAppConfigCorrectly(t *testing.T) { + expectedAppConfig := getTestAppConfig() c := &ConfigComponent{} - c.SetAppConfig(mockAppConfig) + c.SetAppConfig(func() config.AppConfig { + return *expectedAppConfig + }) // appConfig should be equal - Assert(t, c.appConfigFunc(), Equal(*a)) + Assert(t, c.appConfigFunc(), Equal(*expectedAppConfig)) // appConfig value is be replaced - a.AppID = "test1" - a.NamespaceName = a.NamespaceName + config.Comma + "abc" + expectedAppConfig.AppID = "test1" + expectedAppConfig.NamespaceName = expectedAppConfig.NamespaceName + config.Comma + "abc" Assert(t, c.appConfigFunc().AppID, Equal("test1")) Assert(t, c.appConfigFunc().NamespaceName, Equal("application,abc")) } From 1272a39ce86e2a0d15834ac2524e8eb7db17b2de Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Tue, 26 Mar 2024 10:56:06 +0800 Subject: [PATCH 6/8] fix: --- client.go | 7 ++++++- client_test.go | 6 +++--- component/notify/componet_notify_test.go | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 296620f..04c700a 100644 --- a/client.go +++ b/client.go @@ -20,6 +20,8 @@ package agollo import ( "container/list" "errors" + "strings" + "github.com/apolloconfig/agollo/v4/agcache" "github.com/apolloconfig/agollo/v4/agcache/memory" "github.com/apolloconfig/agollo/v4/cluster/roundrobin" @@ -164,7 +166,10 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config { apolloConfig := syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig) if apolloConfig != nil { // update appConfig - c.appConfig.NamespaceName = c.appConfig.NamespaceName + config.Comma + namespace + if !strings.Contains(c.appConfig.NamespaceName, namespace) { + c.appConfig.NamespaceName = c.appConfig.NamespaceName + config.Comma + namespace + } + // update notification c.appConfig.GetNotificationsMap().UpdateNotify(namespace, 0) // update cache c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig) diff --git a/client_test.go b/client_test.go index 418ee28..799a2f0 100644 --- a/client_test.go +++ b/client_test.go @@ -365,7 +365,7 @@ func TestGetConfigAndInitValNotNil(t *testing.T) { AppID: "testID", NamespaceName: "testNotFound", }, - Configurations: map[string]interface{}{"testKey": "testValue"}, + Configurations: map[string]interface{}{"testKey": "testUpdatedValue"}, } }) defer patch.Reset() @@ -377,9 +377,9 @@ func TestGetConfigAndInitValNotNil(t *testing.T) { // appConfig notificationsMap appConfig should be updated Assert(t, client.appConfig.GetNotificationsMap().GetNotify("testNotFound"), Equal(int64(0))) - // cache should be updated + // cache should be updated with new configuration Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal()) - Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testValue")) + Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testUpdatedValue")) } func TestGetConfigAndInitValNil(t *testing.T) { diff --git a/component/notify/componet_notify_test.go b/component/notify/componet_notify_test.go index bc172e7..bb5c4ef 100644 --- a/component/notify/componet_notify_test.go +++ b/component/notify/componet_notify_test.go @@ -106,6 +106,7 @@ func getTestAppConfig() *config.AppConfig { func TestConfigComponent_SetAppConfig_UpdatesAppConfigCorrectly(t *testing.T) { expectedAppConfig := getTestAppConfig() c := &ConfigComponent{} + // set appConfigFunc c.SetAppConfig(func() config.AppConfig { return *expectedAppConfig }) From 6116cb44f4c05d91ce1c0fd76cbfe0ac6a506d1f Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Tue, 26 Mar 2024 11:15:45 +0800 Subject: [PATCH 7/8] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 04c700a..09426e2 100644 --- a/client.go +++ b/client.go @@ -165,14 +165,7 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config { //sync config apolloConfig := syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig) if apolloConfig != nil { - // update appConfig - if !strings.Contains(c.appConfig.NamespaceName, namespace) { - c.appConfig.NamespaceName = c.appConfig.NamespaceName + config.Comma + namespace - } - // update notification - c.appConfig.GetNotificationsMap().UpdateNotify(namespace, 0) - // update cache - c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig) + c.SyncAndUpdate(namespace, apolloConfig) } } @@ -181,6 +174,19 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config { return cfg } +func (c *internalClient) SyncAndUpdate(namespace string, apolloConfig *config.ApolloConfig) { + // update appConfig only if namespace does not exist yet + if !strings.Contains(c.appConfig.NamespaceName, namespace) { + c.appConfig.NamespaceName += "," + namespace + } + + // update notification + c.appConfig.GetNotificationsMap().UpdateNotify(namespace, 0) + + // update cache + c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig) +} + // GetConfigCache 根据namespace获取apollo配置的缓存 func (c *internalClient) GetConfigCache(namespace string) agcache.CacheInterface { config := c.GetConfigAndInit(namespace) From 2deb5edb8b863ee68cf9faa38567648a22724330 Mon Sep 17 00:00:00 2001 From: "dongsheng.qi" Date: Tue, 26 Mar 2024 16:58:51 +0800 Subject: [PATCH 8/8] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 10 +++++++++- client_test.go | 21 ++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 09426e2..5f2d6e5 100644 --- a/client.go +++ b/client.go @@ -176,7 +176,15 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config { func (c *internalClient) SyncAndUpdate(namespace string, apolloConfig *config.ApolloConfig) { // update appConfig only if namespace does not exist yet - if !strings.Contains(c.appConfig.NamespaceName, namespace) { + namespaces := strings.Split(c.appConfig.NamespaceName, ",") + exists := false + for _, n := range namespaces { + if n == namespace { + exists = true + break + } + } + if !exists { c.appConfig.NamespaceName += "," + namespace } diff --git a/client_test.go b/client_test.go index 799a2f0..8a2d7d0 100644 --- a/client_test.go +++ b/client_test.go @@ -368,7 +368,6 @@ func TestGetConfigAndInitValNotNil(t *testing.T) { Configurations: map[string]interface{}{"testKey": "testUpdatedValue"}, } }) - defer patch.Reset() client := createMockApolloConfig(120) cf := client.GetConfig("testNotFound") @@ -380,6 +379,26 @@ func TestGetConfigAndInitValNotNil(t *testing.T) { // cache should be updated with new configuration Assert(t, client.cache.GetConfig("testNotFound"), NotNilVal()) Assert(t, client.cache.GetConfig("testNotFound").GetValue("testKey"), Equal("testUpdatedValue")) + Assert(t, client.appConfig.NamespaceName, Equal("application,testNotFound")) + patch.Reset() + + // second replace + patch1 := gomonkey.ApplyMethod(reflect.TypeOf(apc), "SyncWithNamespace", func(_ *remote.AbsApolloConfig, namespace string, appConfigFunc func() config.AppConfig) *config.ApolloConfig { + return &config.ApolloConfig{ + ApolloConnConfig: config.ApolloConnConfig{ + AppID: "testID", + NamespaceName: "testNotFound1", + }, + Configurations: map[string]interface{}{"testKey": "testUpdatedValue"}, + } + }) + defer patch1.Reset() + client.appConfig.NamespaceName = "testNotFound1" + cf1 := client.GetConfig("testNotFound1") + Assert(t, cf1, NotNilVal()) + Assert(t, client.cache.GetConfig("testNotFound1"), NotNilVal()) + // appConfig namespace existed, should not be appended + Assert(t, client.appConfig.NamespaceName, Equal("testNotFound1")) } func TestGetConfigAndInitValNil(t *testing.T) {