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

Commit

Permalink
Give the correct next event when the message timestamps are the same
Browse files Browse the repository at this point in the history
Discovered while working on #13589
and I had all the messages at the same timestamp

Part of matrix-org/matrix-spec-proposals#3030
  • Loading branch information
MadLittleMods committed Aug 29, 2022
1 parent 4f6de33 commit 6329017
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions synapse/storage/databases/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2111,10 +2111,19 @@ async def get_event_id_for_timestamp(
AND room_id = ?
/* Make sure event is not rejected */
AND rejections.event_id IS NULL
ORDER BY origin_server_ts %s
/**
* First sort by the message timestamp. If the message timestamps are the
* same, we want the message that logically comes "next" (before/after
* the given timestamp) based on the DAG and its topological order (`depth`).
* Finally, we can tie-break based on when it was received on the server
* (`stream_ordering`).
*/
ORDER BY origin_server_ts %s, depth %s, stream_ordering %s
LIMIT 1;
"""

#

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

txn.execute(
sql_template % (comparison_operator, order), (timestamp, room_id)
sql_template % (comparison_operator, order, order, order),
(timestamp, room_id),
)
row = txn.fetchone()
if row:
Expand Down

0 comments on commit 6329017

Please sign in to comment.