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

Commit 6329017

Browse files
committed
Give the correct next event when the message timestamps are the same
Discovered while working on #13589 and I had all the messages at the same timestamp Part of matrix-org/matrix-spec-proposals#3030
1 parent 4f6de33 commit 6329017

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

synapse/storage/databases/main/events_worker.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -2111,10 +2111,19 @@ async def get_event_id_for_timestamp(
21112111
AND room_id = ?
21122112
/* Make sure event is not rejected */
21132113
AND rejections.event_id IS NULL
2114-
ORDER BY origin_server_ts %s
2114+
/**
2115+
* First sort by the message timestamp. If the message timestamps are the
2116+
* same, we want the message that logically comes "next" (before/after
2117+
* the given timestamp) based on the DAG and its topological order (`depth`).
2118+
* Finally, we can tie-break based on when it was received on the server
2119+
* (`stream_ordering`).
2120+
*/
2121+
ORDER BY origin_server_ts %s, depth %s, stream_ordering %s
21152122
LIMIT 1;
21162123
"""
21172124

2125+
#
2126+
21182127
def get_event_id_for_timestamp_txn(txn: LoggingTransaction) -> Optional[str]:
21192128
if direction == "b":
21202129
# Find closest event *before* a given timestamp. We use descending
@@ -2130,7 +2139,8 @@ def get_event_id_for_timestamp_txn(txn: LoggingTransaction) -> Optional[str]:
21302139
order = "ASC"
21312140

21322141
txn.execute(
2133-
sql_template % (comparison_operator, order), (timestamp, room_id)
2142+
sql_template % (comparison_operator, order, order, order),
2143+
(timestamp, room_id),
21342144
)
21352145
row = txn.fetchone()
21362146
if row:

0 commit comments

Comments
 (0)