Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #692. cache not working when get err in queryConfig #693

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func (client *ConfigClient) getConfigInner(param *vo.ConfigParam) (content strin
content = cache.GetFailover(cacheKey, client.configCacheDir)
if len(content) > 0 {
logger.Warnf("%s %s %s is using failover content!", clientConfig.NamespaceId, param.Group, param.DataId)
param.Content = content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do need to set param

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nacos-group/nacos-sdk-go/blob/5fedf574d9d87221e51c1102423604a734698f0f/clients/config_client/config_client.go#L180C1-L180C1

func (client *ConfigClient) GetConfig(param vo.ConfigParam) (content string, err error) {
	content, err = client.getConfigInner(&param)
	if err != nil {
		return "", err
	}
	param.UsageType = vo.ResponseType
	if err = filter.GetDefaultConfigFilterChainManager().DoFilters(&param); err != nil {
		return "", err
	}
	content = param.Content
	return content, nil
}

GetConfig call client.getConfigInner(&param). GetConfigInner return content without err when failover or read from cache, and param.Content is set only in the normal process. if don't set param.Content, which content is returned by GetConfig will be empty when failover or cache :

	if response != nil && response.Response != nil && !response.IsSuccess() {
		return response.Content, errors.New(response.GetMessage())
	}
	param.EncryptedDataKey = response.EncryptedDataKey
	param.Content = response.Content
	return response.Content, nil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nacos-group/nacos-sdk-go/pull/668,there is a problem with the changes in this, and look at it after repair

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: #697

return content, nil
}
response, err := client.configProxy.queryConfig(param.DataId, param.Group, clientConfig.NamespaceId,
Expand All @@ -247,6 +248,7 @@ func (client *ConfigClient) getConfigInner(param *vo.ConfigParam) (content strin
}

logger.Warnf("read config from cache success, dataId=%s, group=%s, namespaceId=%s", param.DataId, param.Group, clientConfig.NamespaceId)
param.Content = cacheContent
return cacheContent, nil
}
if response != nil && response.Response != nil && !response.IsSuccess() {
Expand Down
38 changes: 38 additions & 0 deletions clients/config_client/config_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package config_client
import (
"context"
"errors"
"os"
"testing"

"github.com/nacos-group/nacos-sdk-go/v2/util"
Expand Down Expand Up @@ -56,6 +57,7 @@ func createConfigClientTest() *ConfigClient {
_ = nc.SetHttpAgent(&http_agent.HttpAgent{})
client, _ := NewConfigClient(&nc)
client.configProxy = &MockConfigProxy{}

return client
}

Expand Down Expand Up @@ -100,6 +102,42 @@ func Test_GetConfig(t *testing.T) {
assert.Equal(t, "hello world", content)
}

func Test_GetConfigWithCache(t *testing.T) {
client := createConfigClientTest()
success, err := client.PublishConfig(vo.ConfigParam{
DataId: localConfigTest.DataId,
Group: localConfigTest.Group,
Content: "hello world"})

assert.Nil(t, err)
assert.True(t, success)

dir, err := os.MkdirTemp(os.TempDir(), "nacos")
assert.Nil(t, err)

err = os.Mkdir(dir+"/config", 0700)
assert.Nil(t, err)

client.configCacheDir = dir
cacheKey := util.GetConfigCacheKey(localConfigTest.DataId, "group", "")
f, err := os.Create(dir + "/" + cacheKey)
assert.Nil(t, err)
_, err = f.WriteString("hello world")
assert.Nil(t, err)
_ = f.Close()
defer func() {
err := os.RemoveAll(dir)
assert.Nil(t, err)
}()
for i := 0; i < 10; i++ {
content, err := client.GetConfig(vo.ConfigParam{
DataId: localConfigTest.DataId,
Group: "group"})
assert.Nil(t, err)
assert.Equal(t, "hello world", content)
}
}

func Test_SearchConfig(t *testing.T) {
client := createConfigClientTest()
_, _ = client.PublishConfig(vo.ConfigParam{
Expand Down