[MM-23535] Add disable database search flag and return empty posts results if set (#14245)

* [MM-23535] Add disable database search flag and return empty posts results if set

* Add UpdateConfig function for the SearchStore and hook it into the app lifecycle

* Add the config listener in the server instance instead of using FakeApp

* Instantiate searchlayer as a pointer to avoid passing around copies of it
Этот коммит содержится в:
Miguel de la Cruz
2020-05-13 14:00:57 +02:00
коммит произвёл GitHub
родитель 288ed40e8f
Коммит 4fde004a5b
6 изменённых файлов: 47 добавлений и 22 удалений

Просмотреть файл

@@ -62,16 +62,23 @@ func (s *Server) RunOldAppInitialization() error {
if s.newStore == nil { if s.newStore == nil {
s.newStore = func() store.Store { s.newStore = func() store.Store {
s.sqlStore = sqlstore.NewSqlSupplier(s.Config().SqlSettings, s.Metrics) s.sqlStore = sqlstore.NewSqlSupplier(s.Config().SqlSettings, s.Metrics)
return store.NewTimerLayer( searchStore := searchlayer.NewSearchLayer(
searchlayer.NewSearchLayer( localcachelayer.NewLocalCacheLayer(
localcachelayer.NewLocalCacheLayer( s.sqlStore,
s.sqlStore, s.Metrics,
s.Metrics, s.Cluster,
s.Cluster, s.CacheProvider,
s.CacheProvider,
),
s.SearchEngine,
), ),
s.SearchEngine,
s.Config(),
)
s.AddConfigListener(func(prevCfg, cfg *model.Config) {
searchStore.UpdateConfig(cfg)
})
return store.NewTimerLayer(
searchStore,
s.Metrics, s.Metrics,
) )
} }

Просмотреть файл

@@ -970,6 +970,7 @@ type SqlSettings struct {
Trace *bool `restricted:"true"` Trace *bool `restricted:"true"`
AtRestEncryptKey *string `restricted:"true"` AtRestEncryptKey *string `restricted:"true"`
QueryTimeout *int `restricted:"true"` QueryTimeout *int `restricted:"true"`
DisableDatabaseSearch *bool `restricted:"true"`
} }
func (s *SqlSettings) SetDefaults(isUpdate bool) { func (s *SqlSettings) SetDefaults(isUpdate bool) {
@@ -1018,6 +1019,10 @@ func (s *SqlSettings) SetDefaults(isUpdate bool) {
if s.QueryTimeout == nil { if s.QueryTimeout == nil {
s.QueryTimeout = NewInt(30) s.QueryTimeout = NewInt(30)
} }
if s.DisableDatabaseSearch == nil {
s.DisableDatabaseSearch = NewBool(false)
}
} }
type LogSettings struct { type LogSettings struct {

Просмотреть файл

@@ -17,38 +17,44 @@ type SearchStore struct {
team *SearchTeamStore team *SearchTeamStore
channel *SearchChannelStore channel *SearchChannelStore
post *SearchPostStore post *SearchPostStore
config *model.Config
} }
func NewSearchLayer(baseStore store.Store, searchEngine *searchengine.Broker) SearchStore { func NewSearchLayer(baseStore store.Store, searchEngine *searchengine.Broker, cfg *model.Config) *SearchStore {
searchStore := SearchStore{ searchStore := &SearchStore{
Store: baseStore, Store: baseStore,
searchEngine: searchEngine, searchEngine: searchEngine,
config: cfg,
} }
searchStore.channel = &SearchChannelStore{ChannelStore: baseStore.Channel(), rootStore: &searchStore} searchStore.channel = &SearchChannelStore{ChannelStore: baseStore.Channel(), rootStore: searchStore}
searchStore.post = &SearchPostStore{PostStore: baseStore.Post(), rootStore: &searchStore} searchStore.post = &SearchPostStore{PostStore: baseStore.Post(), rootStore: searchStore}
searchStore.team = &SearchTeamStore{TeamStore: baseStore.Team(), rootStore: &searchStore} searchStore.team = &SearchTeamStore{TeamStore: baseStore.Team(), rootStore: searchStore}
searchStore.user = &SearchUserStore{UserStore: baseStore.User(), rootStore: &searchStore} searchStore.user = &SearchUserStore{UserStore: baseStore.User(), rootStore: searchStore}
return searchStore return searchStore
} }
func (s SearchStore) Channel() store.ChannelStore { func (s *SearchStore) UpdateConfig(cfg *model.Config) {
s.config = cfg
}
func (s *SearchStore) Channel() store.ChannelStore {
return s.channel return s.channel
} }
func (s SearchStore) Post() store.PostStore { func (s *SearchStore) Post() store.PostStore {
return s.post return s.post
} }
func (s SearchStore) Team() store.TeamStore { func (s *SearchStore) Team() store.TeamStore {
return s.team return s.team
} }
func (s SearchStore) User() store.UserStore { func (s *SearchStore) User() store.UserStore {
return s.user return s.user
} }
func (s SearchStore) indexUserFromID(userId string) { func (s *SearchStore) indexUserFromID(userId string) {
user, err := s.User().Get(userId) user, err := s.User().Get(userId)
if err != nil { if err != nil {
return return
@@ -56,7 +62,7 @@ func (s SearchStore) indexUserFromID(userId string) {
s.indexUser(user) s.indexUser(user)
} }
func (s SearchStore) indexUser(user *model.User) { func (s *SearchStore) indexUser(user *model.User) {
for _, engine := range s.searchEngine.GetActiveEngines() { for _, engine := range s.searchEngine.GetActiveEngines() {
if engine.IsIndexingEnabled() { if engine.IsIndexingEnabled() {
runIndexFn(engine, func(engineCopy searchengine.SearchEngineInterface) { runIndexFn(engine, func(engineCopy searchengine.SearchEngineInterface) {

Просмотреть файл

@@ -121,6 +121,12 @@ func (s SearchPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchPara
return results, err return results, err
} }
} }
if *s.rootStore.config.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
}
mlog.Debug("Using database search because no other search engine is available") mlog.Debug("Using database search because no other search engine is available")
return s.PostStore.SearchPostsInTeamForUser(paramsList, userId, teamId, isOrSearch, includeDeletedChannels, page, perPage) return s.PostStore.SearchPostsInTeamForUser(paramsList, userId, teamId, isOrSearch, includeDeletedChannels, page, perPage)
} }

Просмотреть файл

@@ -61,6 +61,7 @@ func (s *SearchUserStore) Search(teamId, term string, options *model.UserSearchO
return users, nil return users, nil
} }
} }
mlog.Debug("Using database search because no other search engine is available") mlog.Debug("Using database search because no other search engine is available")
return s.UserStore.Search(teamId, term, options) return s.UserStore.Search(teamId, term, options)

Просмотреть файл

@@ -111,7 +111,7 @@ func (h *MainHelper) setupStore() {
h.SQLSupplier = sqlstore.NewSqlSupplier(*h.Settings, nil) h.SQLSupplier = sqlstore.NewSqlSupplier(*h.Settings, nil)
h.Store = searchlayer.NewSearchLayer(&TestStore{ h.Store = searchlayer.NewSearchLayer(&TestStore{
h.SQLSupplier, h.SQLSupplier,
}, h.SearchEngine) }, h.SearchEngine, config)
} }
func (h *MainHelper) setupResources() { func (h *MainHelper) setupResources() {