From 27f8a02d5887d5e7a3f74823f5ecb5f18912e33d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 16 Dec 2019 14:07:17 +0000 Subject: [PATCH 1/4] Add delta file to fix missing default table data --- .../schema/delta/56/device_stream_id_insert.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql diff --git a/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql b/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql new file mode 100644 index 000000000000..73e43299de6a --- /dev/null +++ b/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql @@ -0,0 +1,17 @@ +/* Copyright 2019 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +INSERT INTO device_max_stream_id (stream_id) SELECT 0 WHERE NOT EXISTS ( + SELECT * from device_max_stream_id +); \ No newline at end of file From c4e94b80bab340b045bba0f4407b302af8db376b Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 16 Dec 2019 14:22:09 +0000 Subject: [PATCH 2/4] Revert "Replace UPDATE with UPSERT on device_max_stream_id table (#6363)" This reverts commit 657d614f6a53f3dbfd2858bd85d0f81563db0041. --- synapse/storage/data_stores/main/deviceinbox.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/synapse/storage/data_stores/main/deviceinbox.py b/synapse/storage/data_stores/main/deviceinbox.py index 85cfa1685058..0613b49f4a8a 100644 --- a/synapse/storage/data_stores/main/deviceinbox.py +++ b/synapse/storage/data_stores/main/deviceinbox.py @@ -358,21 +358,8 @@ def add_messages_txn(txn, now_ms, stream_id): def _add_messages_to_local_device_inbox_txn( self, txn, stream_id, messages_by_user_then_device ): - # Compatible method of performing an upsert - sql = "SELECT stream_id FROM device_max_stream_id" - - txn.execute(sql) - rows = txn.fetchone() - if rows: - db_stream_id = rows[0] - if db_stream_id < stream_id: - # Insert the new stream_id - sql = "UPDATE device_max_stream_id SET stream_id = ?" - else: - # No rows, perform an insert - sql = "INSERT INTO device_max_stream_id (stream_id) VALUES (?)" - - txn.execute(sql, (stream_id,)) + sql = "UPDATE device_max_stream_id" " SET stream_id = ?" " WHERE stream_id < ?" + txn.execute(sql, (stream_id, stream_id)) local_by_user_then_device = {} for user_id, messages_by_device in messages_by_user_then_device.items(): From 8568704d2aaf9257641e0995a73669a40681b1c5 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 16 Dec 2019 14:23:33 +0000 Subject: [PATCH 3/4] Add changelog --- changelog.d/6555.bugfix | 1 + .../data_stores/main/schema/full_schemas/54/stream_positions.sql | 1 + 2 files changed, 2 insertions(+) create mode 100644 changelog.d/6555.bugfix diff --git a/changelog.d/6555.bugfix b/changelog.d/6555.bugfix new file mode 100644 index 000000000000..86a5a56cf66a --- /dev/null +++ b/changelog.d/6555.bugfix @@ -0,0 +1 @@ +Fix missing row in device_max_stream_id that could cause unable to decrypt errors after server restart. \ No newline at end of file diff --git a/synapse/storage/data_stores/main/schema/full_schemas/54/stream_positions.sql b/synapse/storage/data_stores/main/schema/full_schemas/54/stream_positions.sql index c265fd20e250..91d21b2921fe 100644 --- a/synapse/storage/data_stores/main/schema/full_schemas/54/stream_positions.sql +++ b/synapse/storage/data_stores/main/schema/full_schemas/54/stream_positions.sql @@ -5,3 +5,4 @@ INSERT INTO federation_stream_position (type, stream_id) SELECT 'events', coales INSERT INTO user_directory_stream_pos (stream_id) VALUES (0); INSERT INTO stats_stream_pos (stream_id) VALUES (0); INSERT INTO event_push_summary_stream_ordering (stream_ordering) VALUES (0); +-- device_max_stream_id is handled separately in 56/device_stream_id_insert.sql \ No newline at end of file From f353d4494db8bb9081af2706c5f90b338741b0ab Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 16 Dec 2019 17:03:06 +0000 Subject: [PATCH 4/4] Add contextual comment --- .../main/schema/delta/56/device_stream_id_insert.sql | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql b/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql index 73e43299de6a..c2f557fde9c8 100644 --- a/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql +++ b/synapse/storage/data_stores/main/schema/delta/56/device_stream_id_insert.sql @@ -12,6 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +-- This line already existed in deltas/35/device_stream_id but was not included in the +-- 54 full schema SQL. Add some SQL here to insert the missing row if it does not exist INSERT INTO device_max_stream_id (stream_id) SELECT 0 WHERE NOT EXISTS ( SELECT * from device_max_stream_id ); \ No newline at end of file