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

Commit 536a13f

Browse files
committed
Correctly handle multiple rows per server/key
1 parent 8b5013d commit 536a13f

File tree

1 file changed

+11
-6
lines changed
  • synapse/storage/databases/main

1 file changed

+11
-6
lines changed

synapse/storage/databases/main/keys.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,17 @@ def _get_keys(txn: Cursor, batch: Tuple[Tuple[str, str], ...]) -> None:
221221
"""Processes a batch of keys to fetch, and adds the result to `keys`."""
222222

223223
# batch_iter always returns tuples so it's safe to do len(batch)
224-
sql = """
225-
SELECT server_name, key_id, key_json, ts_valid_until_ms
226-
FROM server_keys_json WHERE 1=0
227-
""" + " OR (server_name=? AND key_id=?)" * len(
228-
batch
229-
)
224+
where_clause = " OR (server_name=? AND key_id=?)" * len(batch)
225+
226+
# `server_keys_json` can have multiple entries per server (one per
227+
# remote server we fetched from, if using perspectives). Order by
228+
# `ts_added_ms` so the most recently fetched one always wins.
229+
sql = f"""
230+
SELECT server_name, key_id, key_json, ts_valid_until_ms
231+
FROM server_keys_json WHERE 1=0
232+
{where_clause}
233+
ORDER BY ts_added_ms
234+
"""
230235

231236
txn.execute(sql, tuple(itertools.chain.from_iterable(batch)))
232237

0 commit comments

Comments
 (0)