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

Commit 17fa4c7

Browse files
reivilibrerichvdh
andauthored
Catch up after Federation Outage (split, 2): Track last successful stream ordering after transmission (#8247)
Co-authored-by: Richard van der Hoff <[email protected]>
1 parent 58f61f1 commit 17fa4c7

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

changelog.d/8247.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Track the `stream_ordering` of the last successfully-sent event to every destination, so we can use this information to 'catch up' a remote server after an outage.

synapse/federation/sender/per_destination_queue.py

+11
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,17 @@ async def _transaction_transmission_loop(self) -> None:
325325

326326
self._last_device_stream_id = device_stream_id
327327
self._last_device_list_stream_id = dev_list_id
328+
329+
if pending_pdus:
330+
# we sent some PDUs and it was successful, so update our
331+
# last_successful_stream_ordering in the destinations table.
332+
final_pdu = pending_pdus[-1]
333+
last_successful_stream_ordering = (
334+
final_pdu.internal_metadata.stream_ordering
335+
)
336+
await self._store.set_destination_last_successful_stream_ordering(
337+
self._destination, last_successful_stream_ordering
338+
)
328339
else:
329340
break
330341
except NotRetryingDestination as e:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* Copyright 2020 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+
*/
15+
16+
-- This column tracks the stream_ordering of the event that was most recently
17+
-- successfully transmitted to the destination.
18+
-- A value of NULL means that we have not sent an event successfully yet
19+
-- (at least, not since the introduction of this column).
20+
ALTER TABLE destinations
21+
ADD COLUMN last_successful_stream_ordering BIGINT;

synapse/storage/databases/main/transactions.py

+38
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,41 @@ def _store_destination_rooms_entries_txn(
333333
["stream_ordering"],
334334
[(stream_ordering,)] * len(rows),
335335
)
336+
337+
async def get_destination_last_successful_stream_ordering(
338+
self, destination: str
339+
) -> Optional[int]:
340+
"""
341+
Gets the stream ordering of the PDU most-recently successfully sent
342+
to the specified destination, or None if this information has not been
343+
tracked yet.
344+
345+
Args:
346+
destination: the destination to query
347+
"""
348+
return await self.db_pool.simple_select_one_onecol(
349+
"destinations",
350+
{"destination": destination},
351+
"last_successful_stream_ordering",
352+
allow_none=True,
353+
desc="get_last_successful_stream_ordering",
354+
)
355+
356+
async def set_destination_last_successful_stream_ordering(
357+
self, destination: str, last_successful_stream_ordering: int
358+
) -> None:
359+
"""
360+
Marks that we have successfully sent the PDUs up to and including the
361+
one specified.
362+
363+
Args:
364+
destination: the destination we have successfully sent to
365+
last_successful_stream_ordering: the stream_ordering of the most
366+
recent successfully-sent PDU
367+
"""
368+
return await self.db_pool.simple_upsert(
369+
"destinations",
370+
keyvalues={"destination": destination},
371+
values={"last_successful_stream_ordering": last_successful_stream_ordering},
372+
desc="set_last_successful_stream_ordering",
373+
)

0 commit comments

Comments
 (0)