Skip to content

Commit

Permalink
fix: dead lock while getting configs (#30319)
Browse files Browse the repository at this point in the history
issue: #30295
pr: #30318

Signed-off-by: jaime <[email protected]>
  • Loading branch information
jaime0815 authored Jan 26, 2024
1 parent 26df754 commit 650dcc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pkg/config/file_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/config/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/maps"
)

func TestLoadFromFileSource(t *testing.T) {
Expand All @@ -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)
})
}

0 comments on commit 650dcc5

Please sign in to comment.