[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
This commit is contained in:
Miguel de la Cruz
2020-05-13 14:00:57 +02:00
committed by GitHub
parent 288ed40e8f
commit 4fde004a5b
6 changed files with 47 additions and 22 deletions

View File

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

View File

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

View File

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

View File

@@ -121,6 +121,12 @@ func (s SearchPostStore) SearchPostsInTeamForUser(paramsList []*model.SearchPara
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")
return s.PostStore.SearchPostsInTeamForUser(paramsList, userId, teamId, isOrSearch, includeDeletedChannels, page, perPage)
}

View File

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

View File

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