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

Commit 6f68e32

Browse files
Mathieu Veltenreivilibre
Mathieu Velten
andauthored
to_device updates could be dropped when consuming the replication stream (#15349)
Co-authored-by: reivilibre <[email protected]>
1 parent 91c3f32 commit 6f68e32

File tree

5 files changed

+98
-15
lines changed

5 files changed

+98
-15
lines changed

changelog.d/15349.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a long-standing bug where some to_device messages could be dropped when using workers.

synapse/storage/databases/main/deviceinbox.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -617,14 +617,14 @@ def get_all_new_device_messages_txn(
617617
# We limit like this as we might have multiple rows per stream_id, and
618618
# we want to make sure we always get all entries for any stream_id
619619
# we return.
620-
upper_pos = min(current_id, last_id + limit)
620+
upto_token = min(current_id, last_id + limit)
621621
sql = (
622622
"SELECT max(stream_id), user_id"
623623
" FROM device_inbox"
624624
" WHERE ? < stream_id AND stream_id <= ?"
625625
" GROUP BY user_id"
626626
)
627-
txn.execute(sql, (last_id, upper_pos))
627+
txn.execute(sql, (last_id, upto_token))
628628
updates = [(row[0], row[1:]) for row in txn]
629629

630630
sql = (
@@ -633,19 +633,13 @@ def get_all_new_device_messages_txn(
633633
" WHERE ? < stream_id AND stream_id <= ?"
634634
" GROUP BY destination"
635635
)
636-
txn.execute(sql, (last_id, upper_pos))
636+
txn.execute(sql, (last_id, upto_token))
637637
updates.extend((row[0], row[1:]) for row in txn)
638638

639639
# Order by ascending stream ordering
640640
updates.sort()
641641

642-
limited = False
643-
upto_token = current_id
644-
if len(updates) >= limit:
645-
upto_token = updates[-1][0]
646-
limited = True
647-
648-
return updates, upto_token, limited
642+
return updates, upto_token, upto_token < current_id
649643

650644
return await self.db_pool.runInteraction(
651645
"get_all_new_device_messages", get_all_new_device_messages_txn

tests/replication/_base.py

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class BaseStreamTestCase(unittest.HomeserverTestCase):
5454
if not hiredis:
5555
skip = "Requires hiredis"
5656

57+
if not USE_POSTGRES_FOR_TESTS:
58+
# Redis replication only takes place on Postgres
59+
skip = "Requires Postgres"
60+
5761
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
5862
# build a replication server
5963
server_factory = ReplicationStreamProtocolFactory(hs)

tests/replication/tcp/streams/test_account_data.py

-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ def test_update_function_room_account_data_limit(self) -> None:
3737
# also one global update
3838
self.get_success(store.add_account_data_for_user("test_user", "m.global", {}))
3939

40-
# tell the notifier to catch up to avoid duplicate rows.
41-
# workaround for https://github.com/matrix-org/synapse/issues/7360
42-
# FIXME remove this when the above is fixed
43-
self.replicate()
44-
4540
# check we're testing what we think we are: no rows should yet have been
4641
# received
4742
self.assertEqual([], self.test_handler.received_rdata_rows)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright 2023 The Matrix.org Foundation C.I.C.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import logging
15+
16+
import synapse
17+
from synapse.replication.tcp.streams._base import _STREAM_UPDATE_TARGET_ROW_COUNT
18+
from synapse.types import JsonDict
19+
20+
from tests.replication._base import BaseStreamTestCase
21+
22+
logger = logging.getLogger(__name__)
23+
24+
25+
class ToDeviceStreamTestCase(BaseStreamTestCase):
26+
servlets = [
27+
synapse.rest.admin.register_servlets,
28+
synapse.rest.client.login.register_servlets,
29+
]
30+
31+
def test_to_device_stream(self) -> None:
32+
store = self.hs.get_datastores().main
33+
34+
user1 = self.register_user("user1", "pass")
35+
self.login("user1", "pass", "device")
36+
user2 = self.register_user("user2", "pass")
37+
self.login("user2", "pass", "device")
38+
39+
# connect to pull the updates related to users creation/login
40+
self.reconnect()
41+
self.replicate()
42+
self.test_handler.received_rdata_rows.clear()
43+
# disconnect so we can accumulate the updates without pulling them
44+
self.disconnect()
45+
46+
msg: JsonDict = {}
47+
msg["sender"] = "@sender:example.org"
48+
msg["type"] = "m.new_device"
49+
50+
# add messages to the device inbox for user1 up until the
51+
# limit defined for a stream update batch
52+
for i in range(0, _STREAM_UPDATE_TARGET_ROW_COUNT):
53+
msg["content"] = {"device": {}}
54+
messages = {user1: {"device": msg}}
55+
56+
self.get_success(
57+
store.add_messages_from_remote_to_device_inbox(
58+
"example.org",
59+
f"{i}",
60+
messages,
61+
)
62+
)
63+
64+
# add one more message, for user2 this time
65+
# this message would be dropped before fixing #15335
66+
msg["content"] = {"device": {}}
67+
messages = {user2: {"device": msg}}
68+
69+
self.get_success(
70+
store.add_messages_from_remote_to_device_inbox(
71+
"example.org",
72+
f"{_STREAM_UPDATE_TARGET_ROW_COUNT}",
73+
messages,
74+
)
75+
)
76+
77+
# replication is disconnected so we shouldn't get any updates yet
78+
self.assertEqual([], self.test_handler.received_rdata_rows)
79+
80+
# now reconnect to pull the updates
81+
self.reconnect()
82+
self.replicate()
83+
84+
# we should receive the fact that we have to_device updates
85+
# for user1 and user2
86+
received_rows = self.test_handler.received_rdata_rows
87+
self.assertEqual(len(received_rows), 2)
88+
self.assertEqual(received_rows[0][2].entity, user1)
89+
self.assertEqual(received_rows[1][2].entity, user2)

0 commit comments

Comments
 (0)