fix: dead lock while getting configs (#30319)

issue: https://github.com/milvus-io/milvus/issues/30295
pr: #30318

Signed-off-by: jaime <yun.zhang@zilliz.com>
This commit is contained in:
jaime 2024-01-26 20:15:01 +08:00 committed by GitHub
parent 26df754514
commit 650dcc512e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/spf13/cast"
"github.com/spf13/viper"
"go.uber.org/zap"
"golang.org/x/exp/maps"
"github.com/milvus-io/milvus/pkg/log"
)
@ -69,9 +70,7 @@ func (fs *FileSource) GetConfigurations() (map[string]string, error) {
fs.configRefresher.start(fs.GetSourceName())
fs.RLock()
for k, v := range fs.configs {
configMap[k] = v
}
maps.Copy(configMap, fs.configs)
fs.RUnlock()
return configMap, nil
}
@ -154,13 +153,18 @@ func (fs *FileSource) loadFromFile() error {
}
}
fs.Lock()
defer fs.Unlock()
err := fs.configRefresher.fireEvents(fs.GetSourceName(), fs.configs, newConfig)
fs.RLock()
source := make(map[string]string)
maps.Copy(source, fs.configs)
fs.RUnlock()
err := fs.configRefresher.fireEvents(fs.GetSourceName(), source, newConfig)
if err != nil {
return err
}
fs.configs = newConfig
fs.Lock()
defer fs.Unlock()
fs.configs = newConfig
return nil
}

View File

@ -22,6 +22,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/maps"
)
func TestLoadFromFileSource(t *testing.T) {
@ -48,5 +49,8 @@ func TestLoadFromFileSource(t *testing.T) {
assert.Equal(t, "3", v1)
v2, _ := fs.GetConfigurationByKey("c.d")
assert.Equal(t, "2", v2)
ret, err := fs.GetConfigurations()
assert.ElementsMatch(t, maps.Keys(ret), []string{"a.b", "ab", "c.d", "cd"})
assert.Nil(t, err)
})
}