From f0533170b314a4ec34e462e5cb78503ba8349728 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 7 Sep 2020 14:46:13 +0100 Subject: [PATCH 1/4] Avoid table-scanning users at startup --- changelog.d/8271.bugfix | 1 + synapse/storage/databases/main/__init__.py | 23 ++++++++++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 changelog.d/8271.bugfix diff --git a/changelog.d/8271.bugfix b/changelog.d/8271.bugfix new file mode 100644 index 000000000000..2de6d95fc586 --- /dev/null +++ b/changelog.d/8271.bugfix @@ -0,0 +1 @@ +Remove slow table scan of `users` table from startup code. diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index b315ae66d279..0ee7b1cfc0fc 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -29,6 +29,7 @@ MultiWriterIdGenerator, StreamIdGenerator, ) +from synapse.types import UserID from synapse.util.caches.stream_change_cache import StreamChangeCache from .account_data import AccountDataStore @@ -591,22 +592,24 @@ def check_database_before_upgrade(cur, database_engine, config: HomeServerConfig """Called before upgrading an existing database to check that it is broadly sane compared with the configuration. """ - logger.info("Running sanity-checks on database...") - domain = config.server_name + logger.info("Checking database for consistency with configuration...") - sql = database_engine.convert_param_style( - "SELECT COUNT(*) FROM users WHERE name NOT LIKE ?" - ) - pat = "%:" + domain - cur.execute(sql, (pat,)) - num_not_matching = cur.fetchall()[0][0] - if num_not_matching == 0: + # if there are any users in the database, check that the username matches our + # configured server name. + + cur.execute("SELECT name FROM users LIMIT 1") + rows = cur.fetchall() + if not rows: + return + + mxid = UserID.from_string(rows[0][0]) + if mxid.domain == config.server_name: return raise Exception( "Found users in database not native to %s!\n" "You cannot changed a synapse server_name after it's been configured" - % (domain,) + % (config.server_name,) ) From e1c51fcbd7a20bedd88ccce434e949fcda399ca8 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 7 Sep 2020 15:00:37 +0100 Subject: [PATCH 2/4] Update changelog.d/8271.bugfix Co-authored-by: Erik Johnston --- changelog.d/8271.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/8271.bugfix b/changelog.d/8271.bugfix index 2de6d95fc586..b75f07b03c8a 100644 --- a/changelog.d/8271.bugfix +++ b/changelog.d/8271.bugfix @@ -1 +1 @@ -Remove slow table scan of `users` table from startup code. +Fix slow start times for large servers by removing a table scan of the `users` table from startup code. From 79163f37ddd18bf6bdcb06c76a339f5357678b95 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 7 Sep 2020 15:02:02 +0100 Subject: [PATCH 3/4] get_domain_from_id --- synapse/storage/databases/main/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index 0ee7b1cfc0fc..07ae348cf02b 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -29,7 +29,7 @@ MultiWriterIdGenerator, StreamIdGenerator, ) -from synapse.types import UserID +from synapse.types import get_domain_from_id from synapse.util.caches.stream_change_cache import StreamChangeCache from .account_data import AccountDataStore @@ -602,8 +602,8 @@ def check_database_before_upgrade(cur, database_engine, config: HomeServerConfig if not rows: return - mxid = UserID.from_string(rows[0][0]) - if mxid.domain == config.server_name: + user_domain = get_domain_from_id(rows[0][0]) + if user_domain == config.server_name: return raise Exception( From 377db23eb70a13ef7d136359067dc7b1f712dc59 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 7 Sep 2020 16:48:33 +0100 Subject: [PATCH 4/4] Update synapse/storage/databases/main/__init__.py Co-authored-by: Brendan Abolivier --- synapse/storage/databases/main/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/storage/databases/main/__init__.py b/synapse/storage/databases/main/__init__.py index 07ae348cf02b..2ae2fbd5d75f 100644 --- a/synapse/storage/databases/main/__init__.py +++ b/synapse/storage/databases/main/__init__.py @@ -608,7 +608,7 @@ def check_database_before_upgrade(cur, database_engine, config: HomeServerConfig raise Exception( "Found users in database not native to %s!\n" - "You cannot changed a synapse server_name after it's been configured" + "You cannot change a synapse server_name after it's been configured" % (config.server_name,) )