Syncronize access when updating config on search layer (#16393)
* Syncronize access when updating config on search layer * Simplify validity of race condition on tests * Add License header on new file * Use atomic.Value for config access on search layer * Apply PR suggestions
Этот коммит содержится в:
@@ -4,6 +4,8 @@
|
||||
package searchlayer
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/services/searchengine"
|
||||
@@ -17,15 +19,15 @@ type SearchStore struct {
|
||||
team *SearchTeamStore
|
||||
channel *SearchChannelStore
|
||||
post *SearchPostStore
|
||||
config *model.Config
|
||||
configValue atomic.Value
|
||||
}
|
||||
|
||||
func NewSearchLayer(baseStore store.Store, searchEngine *searchengine.Broker, cfg *model.Config) *SearchStore {
|
||||
searchStore := &SearchStore{
|
||||
Store: baseStore,
|
||||
searchEngine: searchEngine,
|
||||
config: cfg,
|
||||
}
|
||||
searchStore.configValue.Store(cfg)
|
||||
searchStore.channel = &SearchChannelStore{ChannelStore: baseStore.Channel(), rootStore: searchStore}
|
||||
searchStore.post = &SearchPostStore{PostStore: baseStore.Post(), rootStore: searchStore}
|
||||
searchStore.team = &SearchTeamStore{TeamStore: baseStore.Team(), rootStore: searchStore}
|
||||
@@ -35,7 +37,11 @@ func NewSearchLayer(baseStore store.Store, searchEngine *searchengine.Broker, cf
|
||||
}
|
||||
|
||||
func (s *SearchStore) UpdateConfig(cfg *model.Config) {
|
||||
s.config = cfg
|
||||
s.configValue.Store(cfg)
|
||||
}
|
||||
|
||||
func (s *SearchStore) getConfig() *model.Config {
|
||||
return s.configValue.Load().(*model.Config)
|
||||
}
|
||||
|
||||
func (s *SearchStore) Channel() store.ChannelStore {
|
||||
|
||||
45
store/searchlayer/layer_test.go
Обычный файл
45
store/searchlayer/layer_test.go
Обычный файл
@@ -0,0 +1,45 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package searchlayer_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/services/searchengine"
|
||||
"github.com/mattermost/mattermost-server/v5/store/searchlayer"
|
||||
"github.com/mattermost/mattermost-server/v5/store/sqlstore"
|
||||
"github.com/mattermost/mattermost-server/v5/store/storetest"
|
||||
"github.com/mattermost/mattermost-server/v5/testlib"
|
||||
)
|
||||
|
||||
// Test to verify race condition on UpdateConfig. The test must run with -race flag in order to verify
|
||||
// that there is no race. Ref: (#MM-30868)
|
||||
func TestUpdateConfigRace(t *testing.T) {
|
||||
driverName := os.Getenv("MM_SQLSETTINGS_DRIVERNAME")
|
||||
if driverName == "" {
|
||||
driverName = model.DATABASE_DRIVER_POSTGRES
|
||||
}
|
||||
settings := storetest.MakeSqlSettings(driverName)
|
||||
store := sqlstore.NewSqlSupplier(*settings, nil)
|
||||
|
||||
cfg := &model.Config{}
|
||||
cfg.SetDefaults()
|
||||
cfg.ClusterSettings.MaxIdleConns = model.NewInt(1)
|
||||
searchEngine := searchengine.NewBroker(cfg, nil)
|
||||
layer := searchlayer.NewSearchLayer(&testlib.TestStore{Store: store}, searchEngine, cfg)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(5)
|
||||
for i := 0; i < 5; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
layer.UpdateConfig(cfg.Clone())
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -186,7 +186,7 @@ func (s SearchPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchPara
|
||||
}
|
||||
}
|
||||
|
||||
if *s.rootStore.config.SqlSettings.DisableDatabaseSearch {
|
||||
if *s.rootStore.getConfig().SqlSettings.DisableDatabaseSearch {
|
||||
mlog.Debug("Returning empty results for post SearchPostsInTeam as the database search is disabled")
|
||||
return &model.PostSearchResults{PostList: model.NewPostList(), Matches: model.PostSearchMatches{}}, nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user