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

[bug fix] fixup config listen cache empty #697

Merged
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
34 changes: 20 additions & 14 deletions clients/config_client/config_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ func initKmsV3Client(clientConfig constant.ClientConfig) (*nacos_inner_encryptio
}

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

Expand Down Expand Up @@ -214,10 +217,10 @@ func (client *ConfigClient) encrypt(dataId, content, kmsKeyId string) (string, e
return cipherContent, nil
}

func (client *ConfigClient) getConfigInner(param *vo.ConfigParam) (content string, err error) {
func (client *ConfigClient) getConfigInner(param vo.ConfigParam) (content, encryptedDataKey string, err error) {
if len(param.DataId) <= 0 {
err = errors.New("[client.GetConfig] param.dataId can not be empty")
return "", err
return "", "", err
}
if len(param.Group) <= 0 {
param.Group = constant.DEFAULT_GROUP
Expand All @@ -228,7 +231,8 @@ 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)
return content, nil
//todo: get fial over encryptedDataKey
return content, "", nil
}
response, err := client.configProxy.queryConfig(param.DataId, param.Group, clientConfig.NamespaceId,
clientConfig.TimeoutMs, false, client)
Expand All @@ -237,24 +241,26 @@ func (client *ConfigClient) getConfigInner(param *vo.ConfigParam) (content strin
param.DataId, param.Group, clientConfig.NamespaceId)

if clientConfig.DisableUseSnapShot {
return "", errors.Errorf("get config from remote nacos server fail, and is not allowed to read local file, err:%v", err)
return "", "", errors.Errorf("get config from remote nacos server fail, and is not allowed to read local file, err:%v", err)
}

cacheContent, cacheErr := cache.ReadConfigFromFile(cacheKey, client.configCacheDir)
//todo get encryptedDataKey from file
if cacheErr != nil {
return "", errors.Errorf("read config from both server and cache fail, err=%v,dataId=%s, group=%s, namespaceId=%s",
return "", "", errors.Errorf("read config from both server and cache fail, err=%v,dataId=%s, group=%s, namespaceId=%s",
cacheErr, param.DataId, param.Group, clientConfig.NamespaceId)
}

logger.Warnf("read config from cache success, dataId=%s, group=%s, namespaceId=%s", param.DataId, param.Group, clientConfig.NamespaceId)
return cacheContent, nil
//todo return encrypted data key
return cacheContent, encryptedDataKey, nil
}
if response != nil && response.Response != nil && !response.IsSuccess() {
return response.Content, errors.New(response.GetMessage())
return response.Content, response.EncryptedDataKey, errors.New(response.GetMessage())
}
param.EncryptedDataKey = response.EncryptedDataKey
param.Content = response.Content
return response.Content, nil
encryptedDataKey = response.EncryptedDataKey
content = response.Content
return response.Content, response.EncryptedDataKey, nil
}

func (client *ConfigClient) PublishConfig(param vo.ConfigParam) (published bool, err error) {
Expand Down
4 changes: 2 additions & 2 deletions clients/config_client/config_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (m *MockConfigProxy) queryConfig(dataId, group, tenant string, timeout uint
if IsLimited(cacheKey) {
return nil, errors.New("request is limited")
}
return &rpc_response.ConfigQueryResponse{Content: "hello world"}, nil
return &rpc_response.ConfigQueryResponse{Content: "hello world", Response: &rpc_response.Response{Success: true}}, nil
}
func (m *MockConfigProxy) searchConfigProxy(param vo.SearchConfigParam, tenant, accessKey, secretKey string) (*model.ConfigPage, error) {
return &model.ConfigPage{TotalCount: 1}, nil
Expand All @@ -94,7 +94,7 @@ func Test_GetConfig(t *testing.T) {

content, err := client.GetConfig(vo.ConfigParam{
DataId: localConfigTest.DataId,
Group: "group"})
Group: localConfigTest.Group})

assert.Nil(t, err)
assert.Equal(t, "hello world", content)
Expand Down
9 changes: 9 additions & 0 deletions vo/config_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ type ConfigParam struct {
OnChange func(namespace, group, dataId, data string)
}

func (this *ConfigParam) DeepCopy() *ConfigParam {
if this == nil {
return nil
}
result := new(ConfigParam)
*result = *this
return result
}

type UsageType string

const (
Expand Down
40 changes: 40 additions & 0 deletions vo/config_param_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vo

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestConfigParamDeepCopy(t *testing.T) {
param := &ConfigParam{
DataId: "dataId",
Group: "",
Content: "common content",
UsageType: RequestType,
OnChange: func(namespace, group, dataId, data string) {
//do nothing
},
}
paramDeepCopied := param.DeepCopy()

assert.Equal(t, param.DataId, paramDeepCopied.DataId)
assert.Equal(t, param.Content, paramDeepCopied.Content)
assert.NotEqual(t, &param.OnChange, &paramDeepCopied.OnChange)
assert.NotEqual(t, &param, &paramDeepCopied)
}
Loading