Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
96312536
Unverified
Commit
96312536
authored
4 years ago
by
Richard van der Hoff
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Refuse to upgrade database on worker processes (#8266)
parent
f25af1f9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
changelog.d/8266.misc
+1
-0
1 addition, 0 deletions
changelog.d/8266.misc
synapse/storage/prepare_database.py
+61
-17
61 additions, 17 deletions
synapse/storage/prepare_database.py
with
62 additions
and
17 deletions
changelog.d/8266.misc
0 → 100644
+
1
−
0
View file @
96312536
Do not attempt to upgrade upgrade database schema on worker processes.
This diff is collapsed.
Click to expand it.
synapse/storage/prepare_database.py
+
61
−
17
View file @
96312536
...
...
@@ -47,6 +47,22 @@ class UpgradeDatabaseException(PrepareDatabaseException):
pass
OUTDATED_SCHEMA_ON_WORKER_ERROR
=
(
"
Expected database schema version %i but got %i: run the main synapse process to
"
"
upgrade the database schema before starting worker processes.
"
)
EMPTY_DATABASE_ON_WORKER_ERROR
=
(
"
Uninitialised database: run the main synapse process to prepare the database
"
"
schema before starting worker processes.
"
)
UNAPPLIED_DELTA_ON_WORKER_ERROR
=
(
"
Database schema delta %s has not been applied: run the main synapse process to
"
"
upgrade the database schema before starting worker processes.
"
)
def
prepare_database
(
db_conn
,
database_engine
,
config
,
databases
=
[
"
main
"
,
"
state
"
]):
"""
Prepares a physical database for usage. Will either create all necessary tables
or upgrade from an older schema version.
...
...
@@ -71,25 +87,35 @@ def prepare_database(db_conn, database_engine, config, databases=["main", "state
if
version_info
:
user_version
,
delta_files
,
upgraded
=
version_info
# config should only be None when we are preparing an in-memory SQLite db,
# which should be empty.
if
config
is
None
:
if
user_version
!=
SCHEMA_VERSION
:
# If we don't pass in a config file then we are expecting to
# have already upgraded the DB.
raise
UpgradeDatabaseException
(
"
Expected database schema version %i but got %i
"
%
(
SCHEMA_VERSION
,
user_version
)
)
else
:
_upgrade_existing_database
(
cur
,
user_version
,
delta_files
,
upgraded
,
database_engine
,
config
,
databases
=
databases
,
raise
ValueError
(
"
config==None in prepare_database, but databse is not empty
"
)
# if it's a worker app, refuse to upgrade the database, to avoid multiple
# workers doing it at once.
if
config
.
worker_app
is
not
None
and
user_version
!=
SCHEMA_VERSION
:
raise
UpgradeDatabaseException
(
OUTDATED_SCHEMA_ON_WORKER_ERROR
%
(
SCHEMA_VERSION
,
user_version
)
)
_upgrade_existing_database
(
cur
,
user_version
,
delta_files
,
upgraded
,
database_engine
,
config
,
databases
=
databases
,
)
else
:
# if it's a worker app, refuse to upgrade the database, to avoid multiple
# workers doing it at once.
if
config
and
config
.
worker_app
is
not
None
:
raise
UpgradeDatabaseException
(
EMPTY_DATABASE_ON_WORKER_ERROR
)
_setup_new_database
(
cur
,
database_engine
,
databases
=
databases
)
# check if any of our configured dynamic modules want a database
...
...
@@ -295,6 +321,8 @@ def _upgrade_existing_database(
else
:
assert
config
is_worker
=
config
and
config
.
worker_app
is
not
None
if
current_version
>
SCHEMA_VERSION
:
raise
ValueError
(
"
Cannot use this database as it is too
"
...
...
@@ -322,7 +350,7 @@ def _upgrade_existing_database(
specific_engine_extensions
=
(
"
.sqlite
"
,
"
.postgres
"
)
for
v
in
range
(
start_ver
,
SCHEMA_VERSION
+
1
):
logger
.
info
(
"
Upgrad
ing schema
to
v%d
"
,
v
)
logger
.
info
(
"
Apply
ing schema
deltas for
v%d
"
,
v
)
# We need to search both the global and per data store schema
# directories for schema updates.
...
...
@@ -382,9 +410,15 @@ def _upgrade_existing_database(
continue
root_name
,
ext
=
os
.
path
.
splitext
(
file_name
)
if
ext
==
"
.py
"
:
# This is a python upgrade module. We need to import into some
# package and then execute its `run_upgrade` function.
if
is_worker
:
raise
PrepareDatabaseException
(
UNAPPLIED_DELTA_ON_WORKER_ERROR
%
relative_path
)
module_name
=
"
synapse.storage.v%d_%s
"
%
(
v
,
root_name
)
with
open
(
absolute_path
)
as
python_file
:
module
=
imp
.
load_source
(
module_name
,
absolute_path
,
python_file
)
...
...
@@ -399,10 +433,18 @@ def _upgrade_existing_database(
continue
elif
ext
==
"
.sql
"
:
# A plain old .sql file, just read and execute it
if
is_worker
:
raise
PrepareDatabaseException
(
UNAPPLIED_DELTA_ON_WORKER_ERROR
%
relative_path
)
logger
.
info
(
"
Applying schema %s
"
,
relative_path
)
executescript
(
cur
,
absolute_path
)
elif
ext
==
specific_engine_extension
and
root_name
.
endswith
(
"
.sql
"
):
# A .sql file specific to our engine; just read and execute it
if
is_worker
:
raise
PrepareDatabaseException
(
UNAPPLIED_DELTA_ON_WORKER_ERROR
%
relative_path
)
logger
.
info
(
"
Applying engine-specific schema %s
"
,
relative_path
)
executescript
(
cur
,
absolute_path
)
elif
ext
in
specific_engine_extensions
and
root_name
.
endswith
(
"
.sql
"
):
...
...
@@ -432,6 +474,8 @@ def _upgrade_existing_database(
(
v
,
True
),
)
logger
.
info
(
"
Schema now up to date
"
)
def
_apply_module_schemas
(
txn
,
database_engine
,
config
):
"""
Apply the module schemas for the dynamic modules, if any
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment