diff options
author | Jesse Hallam <jesse.hallam@gmail.com> | 2018-09-13 13:47:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-13 13:47:17 -0400 |
commit | 8b17bf9e42dd56ecd0fe8300da90bea5ee8684ef (patch) | |
tree | d9f3341228a2fe8f5994f56d524943f8702c5365 /store/sqlstore/supplier.go | |
parent | 0a5f792d2d6ceaa6c9bdb3050acbc4050c0c02f5 (diff) | |
download | chat-8b17bf9e42dd56ecd0fe8300da90bea5ee8684ef.tar.gz chat-8b17bf9e42dd56ecd0fe8300da90bea5ee8684ef.tar.bz2 chat-8b17bf9e42dd56ecd0fe8300da90bea5ee8684ef.zip |
MM-11886: materialize channel search (#9349)
* materialize PublicChannels table
Introduce triggers for each supported database that automatically maintain a subset of the Channels table corresponding to only public channels. This improves corresponding queries that no longer need to filter out 99% DM channels.
This initial commit modifies the channel store directly for easier code reviewing, but the next wraps an experimental version around it to enable a kill switch in case there are unforeseen performance regressions.
This addresses [MM-11886](https://mattermost.atlassian.net/browse/MM-11886) and [MM-11945](https://mattermost.atlassian.net/browse/MM-11945).
* extract the experimental public channels materialization
Wrap the original channel store with an experimental version that
leverages the materialized public channels, but can be disabled to
fallback to the original implementation.
This addresses MM-11947.
* s/ExperimentalPublicChannelsMaterialization/EnablePublicChannelsMaterialization/
* simplify error handling
* move experimental config listener until after store is initialized
Diffstat (limited to 'store/sqlstore/supplier.go')
-rw-r--r-- | store/sqlstore/supplier.go | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/store/sqlstore/supplier.go b/store/sqlstore/supplier.go index 6c49d91fb..d1d7564f7 100644 --- a/store/sqlstore/supplier.go +++ b/store/sqlstore/supplier.go @@ -33,6 +33,7 @@ const ( ) const ( + EXIT_GENERIC_FAILURE = 1 EXIT_CREATE_TABLE = 100 EXIT_DB_OPEN = 101 EXIT_PING = 102 @@ -116,8 +117,13 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter supplier.initConnection() + enableExperimentalPublicChannelsMaterialization := true + if settings.EnablePublicChannelsMaterialization != nil && !*settings.EnablePublicChannelsMaterialization { + enableExperimentalPublicChannelsMaterialization = false + } + supplier.oldStores.team = NewSqlTeamStore(supplier) - supplier.oldStores.channel = NewSqlChannelStore(supplier, metrics) + supplier.oldStores.channel = NewSqlChannelStoreExperimental(supplier, metrics, enableExperimentalPublicChannelsMaterialization) supplier.oldStores.post = NewSqlPostStore(supplier, metrics) supplier.oldStores.user = NewSqlUserStore(supplier, metrics) supplier.oldStores.audit = NewSqlAuditStore(supplier) @@ -151,10 +157,19 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter os.Exit(EXIT_CREATE_TABLE) } + // This store's triggers should exist before the migration is run to ensure the + // corresponding tables stay in sync. Whether or not a trigger should be created before + // or after a migration is likely to be decided on a case-by-case basis. + if err := supplier.oldStores.channel.(*SqlChannelStoreExperimental).CreateTriggersIfNotExists(); err != nil { + mlog.Critical("Error creating triggers", mlog.Err(err)) + time.Sleep(time.Second) + os.Exit(EXIT_GENERIC_FAILURE) + } + UpgradeDatabase(supplier) supplier.oldStores.team.(*SqlTeamStore).CreateIndexesIfNotExists() - supplier.oldStores.channel.(*SqlChannelStore).CreateIndexesIfNotExists() + supplier.oldStores.channel.(*SqlChannelStoreExperimental).CreateIndexesIfNotExists() supplier.oldStores.post.(*SqlPostStore).CreateIndexesIfNotExists() supplier.oldStores.user.(*SqlUserStore).CreateIndexesIfNotExists() supplier.oldStores.audit.(*SqlAuditStore).CreateIndexesIfNotExists() @@ -461,6 +476,52 @@ func (ss *SqlSupplier) DoesColumnExist(tableName string, columnName string) bool } } +func (ss *SqlSupplier) DoesTriggerExist(triggerName string) bool { + if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES { + count, err := ss.GetMaster().SelectInt(` + SELECT + COUNT(0) + FROM + pg_trigger + WHERE + tgname = $1 + `, triggerName) + + if err != nil { + mlog.Critical(fmt.Sprintf("Failed to check if trigger exists %v", err)) + time.Sleep(time.Second) + os.Exit(EXIT_GENERIC_FAILURE) + } + + return count > 0 + + } else if ss.DriverName() == model.DATABASE_DRIVER_MYSQL { + count, err := ss.GetMaster().SelectInt(` + SELECT + COUNT(0) + FROM + information_schema.triggers + WHERE + trigger_schema = DATABASE() + AND trigger_name = ? + `, triggerName) + + if err != nil { + mlog.Critical(fmt.Sprintf("Failed to check if trigger exists %v", err)) + time.Sleep(time.Second) + os.Exit(EXIT_GENERIC_FAILURE) + } + + return count > 0 + + } else { + mlog.Critical("Failed to check if column exists because of missing driver") + time.Sleep(time.Second) + os.Exit(EXIT_GENERIC_FAILURE) + return false + } +} + func (ss *SqlSupplier) CreateColumnIfNotExists(tableName string, columnName string, mySqlColType string, postgresColType string, defaultValue string) bool { if ss.DoesColumnExist(tableName, columnName) { |