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

Commit 9c59e11

Browse files
Run _upgrade_existing_database on workers if at current schema_version (#11346)
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
1 parent b596a1e commit 9c59e11

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed

changelog.d/11346.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in v1.47.0rc1 which caused worker processes to not halt startup in the presence of outstanding database migrations.

synapse/storage/prepare_database.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,16 @@ def prepare_database(
131131
"config==None in prepare_database, but database is not empty"
132132
)
133133

134-
# if it's a worker app, refuse to upgrade the database, to avoid multiple
135-
# workers doing it at once.
136-
if config.worker.worker_app is None:
137-
_upgrade_existing_database(
138-
cur,
139-
version_info,
140-
database_engine,
141-
config,
142-
databases=databases,
143-
)
144-
elif version_info.current_version < SCHEMA_VERSION:
145-
# If the DB is on an older version than we expect then we refuse
146-
# to start the worker (as the main process needs to run first to
147-
# update the schema).
148-
raise UpgradeDatabaseException(
149-
OUTDATED_SCHEMA_ON_WORKER_ERROR
150-
% (SCHEMA_VERSION, version_info.current_version)
151-
)
134+
# This should be run on all processes, master or worker. The master will
135+
# apply the deltas, while workers will check if any outstanding deltas
136+
# exist and raise an PrepareDatabaseException if they do.
137+
_upgrade_existing_database(
138+
cur,
139+
version_info,
140+
database_engine,
141+
config,
142+
databases=databases,
143+
)
152144

153145
else:
154146
logger.info("%r: Initialising new database", databases)
@@ -358,6 +350,18 @@ def _upgrade_existing_database(
358350

359351
is_worker = config and config.worker.worker_app is not None
360352

353+
# If the schema version needs to be updated, and we are on a worker, we immediately
354+
# know to bail out as workers cannot update the database schema. Only one process
355+
# must update the database at the time, therefore we delegate this task to the master.
356+
if is_worker and current_schema_state.current_version < SCHEMA_VERSION:
357+
# If the DB is on an older version than we expect then we refuse
358+
# to start the worker (as the main process needs to run first to
359+
# update the schema).
360+
raise UpgradeDatabaseException(
361+
OUTDATED_SCHEMA_ON_WORKER_ERROR
362+
% (SCHEMA_VERSION, current_schema_state.current_version)
363+
)
364+
361365
if (
362366
current_schema_state.compat_version is not None
363367
and current_schema_state.compat_version > SCHEMA_VERSION

tests/storage/test_rollback_worker.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from typing import List
15+
from unittest import mock
16+
1417
from synapse.app.generic_worker import GenericWorkerServer
1518
from synapse.storage.database import LoggingDatabaseConnection
1619
from synapse.storage.prepare_database import PrepareDatabaseException, prepare_database
@@ -19,6 +22,22 @@
1922
from tests.unittest import HomeserverTestCase
2023

2124

25+
def fake_listdir(filepath: str) -> List[str]:
26+
"""
27+
A fake implementation of os.listdir which we can use to mock out the filesystem.
28+
29+
Args:
30+
filepath: The directory to list files for.
31+
32+
Returns:
33+
A list of files and folders in the directory.
34+
"""
35+
if filepath.endswith("full_schemas"):
36+
return [SCHEMA_VERSION]
37+
38+
return ["99_add_unicorn_to_database.sql"]
39+
40+
2241
class WorkerSchemaTests(HomeserverTestCase):
2342
def make_homeserver(self, reactor, clock):
2443
hs = self.setup_test_homeserver(
@@ -51,7 +70,7 @@ def test_rolling_back(self):
5170

5271
prepare_database(db_conn, db_pool.engine, self.hs.config)
5372

54-
def test_not_upgraded(self):
73+
def test_not_upgraded_old_schema_version(self):
5574
"""Test that workers don't start if the DB has an older schema version"""
5675
db_pool = self.hs.get_datastore().db_pool
5776
db_conn = LoggingDatabaseConnection(
@@ -67,3 +86,34 @@ def test_not_upgraded(self):
6786

6887
with self.assertRaises(PrepareDatabaseException):
6988
prepare_database(db_conn, db_pool.engine, self.hs.config)
89+
90+
def test_not_upgraded_current_schema_version_with_outstanding_deltas(self):
91+
"""
92+
Test that workers don't start if the DB is on the current schema version,
93+
but there are still outstanding delta migrations to run.
94+
"""
95+
db_pool = self.hs.get_datastore().db_pool
96+
db_conn = LoggingDatabaseConnection(
97+
db_pool._db_pool.connect(),
98+
db_pool.engine,
99+
"tests",
100+
)
101+
102+
# Set the schema version of the database to the current version
103+
cur = db_conn.cursor()
104+
cur.execute("UPDATE schema_version SET version = ?", (SCHEMA_VERSION,))
105+
106+
db_conn.commit()
107+
108+
# Path `os.listdir` here to make synapse think that there is a migration
109+
# file ready to be run.
110+
# Note that we can't patch this function for the whole method, else Synapse
111+
# will try to find the file when building the database initially.
112+
with mock.patch("os.listdir", mock.Mock(side_effect=fake_listdir)):
113+
with self.assertRaises(PrepareDatabaseException):
114+
# Synapse should think that there is an outstanding migration file due to
115+
# patching 'os.listdir' in the function decorator.
116+
#
117+
# We expect Synapse to raise an exception to indicate the master process
118+
# needs to apply this migration file.
119+
prepare_database(db_conn, db_pool.engine, self.hs.config)

0 commit comments

Comments
 (0)