Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 0dae7d8

Browse files
authored
Add more logging to debug slow startup (#8264)
I'm hoping this will provide some pointers for debugging #7968.
1 parent 9631253 commit 0dae7d8

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

changelog.d/8264.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add more logging to debug slow startup.

synapse/storage/databases/__init__.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,24 @@ def __init__(self, main_store_class, hs):
4747
engine = create_engine(database_config.config)
4848

4949
with make_conn(database_config, engine) as db_conn:
50-
logger.info("Preparing database %r...", db_name)
51-
50+
logger.info("[database config %r]: Checking database server", db_name)
5251
engine.check_database(db_conn)
52+
53+
logger.info(
54+
"[database config %r]: Preparing for databases %r",
55+
db_name,
56+
database_config.databases,
57+
)
5358
prepare_database(
5459
db_conn, engine, hs.config, databases=database_config.databases,
5560
)
5661

5762
database = DatabasePool(hs, database_config, engine)
5863

5964
if "main" in database_config.databases:
60-
logger.info("Starting 'main' data store")
65+
logger.info(
66+
"[database config %r]: Starting 'main' database", db_name
67+
)
6168

6269
# Sanity check we don't try and configure the main store on
6370
# multiple databases.
@@ -72,7 +79,9 @@ def __init__(self, main_store_class, hs):
7279
persist_events = PersistEventsStore(hs, database, main)
7380

7481
if "state" in database_config.databases:
75-
logger.info("Starting 'state' data store")
82+
logger.info(
83+
"[database config %r]: Starting 'state' database", db_name
84+
)
7685

7786
# Sanity check we don't try and configure the state store on
7887
# multiple databases.
@@ -85,7 +94,7 @@ def __init__(self, main_store_class, hs):
8594

8695
self.databases.append(database)
8796

88-
logger.info("Database %r prepared", db_name)
97+
logger.info("[database config %r]: prepared", db_name)
8998

9099
# Closing the context manager doesn't close the connection.
91100
# psycopg will close the connection when the object gets GCed, but *only*
@@ -98,10 +107,10 @@ def __init__(self, main_store_class, hs):
98107

99108
# Sanity check that we have actually configured all the required stores.
100109
if not main:
101-
raise Exception("No 'main' data store configured")
110+
raise Exception("No 'main' database configured")
102111

103112
if not state:
104-
raise Exception("No 'state' data store configured")
113+
raise Exception("No 'state' database configured")
105114

106115
# We use local variables here to ensure that the databases do not have
107116
# optional types.

synapse/storage/databases/main/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ def check_database_before_upgrade(cur, database_engine, config: HomeServerConfig
591591
"""Called before upgrading an existing database to check that it is broadly sane
592592
compared with the configuration.
593593
"""
594+
logger.info("Running sanity-checks on database...")
594595
domain = config.server_name
595596

596597
sql = database_engine.convert_param_style(

synapse/storage/prepare_database.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,18 @@ def prepare_database(db_conn, database_engine, config, databases=["main", "state
8282

8383
try:
8484
cur = db_conn.cursor()
85+
86+
logger.info("%r: Checking existing schema version", databases)
8587
version_info = _get_or_create_schema_state(cur, database_engine)
8688

8789
if version_info:
8890
user_version, delta_files, upgraded = version_info
91+
logger.info(
92+
"%r: Existing schema is %i (+%i deltas)",
93+
databases,
94+
user_version,
95+
len(delta_files),
96+
)
8997

9098
# config should only be None when we are preparing an in-memory SQLite db,
9199
# which should be empty.
@@ -111,6 +119,8 @@ def prepare_database(db_conn, database_engine, config, databases=["main", "state
111119
databases=databases,
112120
)
113121
else:
122+
logger.info("%r: Initialising new database", databases)
123+
114124
# if it's a worker app, refuse to upgrade the database, to avoid multiple
115125
# workers doing it at once.
116126
if config and config.worker_app is not None:

synapse/storage/util/id_generators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import contextlib
1717
import heapq
18+
import logging
1819
import threading
1920
from collections import deque
2021
from typing import Dict, List, Set
@@ -24,6 +25,8 @@
2425
from synapse.storage.database import DatabasePool, LoggingTransaction
2526
from synapse.storage.util.sequence import PostgresSequenceGenerator
2627

28+
logger = logging.getLogger(__name__)
29+
2730

2831
class IdGenerator:
2932
def __init__(self, db_conn, table, column):
@@ -48,6 +51,8 @@ def _load_current_id(db_conn, table, column, step=1):
4851
Returns:
4952
int
5053
"""
54+
# debug logging for https://github.com/matrix-org/synapse/issues/7968
55+
logger.info("initialising stream generator for %s(%s)", table, column)
5156
cur = db_conn.cursor()
5257
if step == 1:
5358
cur.execute("SELECT MAX(%s) FROM %s" % (column, table))

0 commit comments

Comments
 (0)