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

Commit 3e0536c

Browse files
authored
Replace uses of simple_insert_many with simple_insert_many_values. (#11742)
This should be (slightly) more efficient and it is simpler to have a single method for inserting multiple values.
1 parent d70169b commit 3e0536c

19 files changed

+263
-298
lines changed

changelog.d/11742.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Minor efficiency improvements when inserting many values into the database.

synapse/rest/admin/background_updates.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,41 +123,33 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
123123
job_name = body["job_name"]
124124

125125
if job_name == "populate_stats_process_rooms":
126-
jobs = [
127-
{
128-
"update_name": "populate_stats_process_rooms",
129-
"progress_json": "{}",
130-
},
131-
]
126+
jobs = [("populate_stats_process_rooms", "{}", "")]
132127
elif job_name == "regenerate_directory":
133128
jobs = [
134-
{
135-
"update_name": "populate_user_directory_createtables",
136-
"progress_json": "{}",
137-
"depends_on": "",
138-
},
139-
{
140-
"update_name": "populate_user_directory_process_rooms",
141-
"progress_json": "{}",
142-
"depends_on": "populate_user_directory_createtables",
143-
},
144-
{
145-
"update_name": "populate_user_directory_process_users",
146-
"progress_json": "{}",
147-
"depends_on": "populate_user_directory_process_rooms",
148-
},
149-
{
150-
"update_name": "populate_user_directory_cleanup",
151-
"progress_json": "{}",
152-
"depends_on": "populate_user_directory_process_users",
153-
},
129+
("populate_user_directory_createtables", "{}", ""),
130+
(
131+
"populate_user_directory_process_rooms",
132+
"{}",
133+
"populate_user_directory_createtables",
134+
),
135+
(
136+
"populate_user_directory_process_users",
137+
"{}",
138+
"populate_user_directory_process_rooms",
139+
),
140+
(
141+
"populate_user_directory_cleanup",
142+
"{}",
143+
"populate_user_directory_process_users",
144+
),
154145
]
155146
else:
156147
raise SynapseError(HTTPStatus.BAD_REQUEST, "Invalid job_name")
157148

158149
try:
159150
await self._store.db_pool.simple_insert_many(
160151
table="background_updates",
152+
keys=("update_name", "progress_json", "depends_on"),
161153
values=jobs,
162154
desc=f"admin_api_run_{job_name}",
163155
)

synapse/storage/database.py

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -934,56 +934,6 @@ def simple_insert_txn(
934934
txn.execute(sql, vals)
935935

936936
async def simple_insert_many(
937-
self, table: str, values: List[Dict[str, Any]], desc: str
938-
) -> None:
939-
"""Executes an INSERT query on the named table.
940-
941-
The input is given as a list of dicts, with one dict per row.
942-
Generally simple_insert_many_values should be preferred for new code.
943-
944-
Args:
945-
table: string giving the table name
946-
values: dict of new column names and values for them
947-
desc: description of the transaction, for logging and metrics
948-
"""
949-
await self.runInteraction(desc, self.simple_insert_many_txn, table, values)
950-
951-
@staticmethod
952-
def simple_insert_many_txn(
953-
txn: LoggingTransaction, table: str, values: List[Dict[str, Any]]
954-
) -> None:
955-
"""Executes an INSERT query on the named table.
956-
957-
The input is given as a list of dicts, with one dict per row.
958-
Generally simple_insert_many_values_txn should be preferred for new code.
959-
960-
Args:
961-
txn: The transaction to use.
962-
table: string giving the table name
963-
values: dict of new column names and values for them
964-
"""
965-
if not values:
966-
return
967-
968-
# This is a *slight* abomination to get a list of tuples of key names
969-
# and a list of tuples of value names.
970-
#
971-
# i.e. [{"a": 1, "b": 2}, {"c": 3, "d": 4}]
972-
# => [("a", "b",), ("c", "d",)] and [(1, 2,), (3, 4,)]
973-
#
974-
# The sort is to ensure that we don't rely on dictionary iteration
975-
# order.
976-
keys, vals = zip(
977-
*(zip(*(sorted(i.items(), key=lambda kv: kv[0]))) for i in values if i)
978-
)
979-
980-
for k in keys:
981-
if k != keys[0]:
982-
raise RuntimeError("All items must have the same keys")
983-
984-
return DatabasePool.simple_insert_many_values_txn(txn, table, keys[0], vals)
985-
986-
async def simple_insert_many_values(
987937
self,
988938
table: str,
989939
keys: Collection[str],
@@ -1002,11 +952,11 @@ async def simple_insert_many_values(
1002952
desc: description of the transaction, for logging and metrics
1003953
"""
1004954
await self.runInteraction(
1005-
desc, self.simple_insert_many_values_txn, table, keys, values
955+
desc, self.simple_insert_many_txn, table, keys, values
1006956
)
1007957

1008958
@staticmethod
1009-
def simple_insert_many_values_txn(
959+
def simple_insert_many_txn(
1010960
txn: LoggingTransaction,
1011961
table: str,
1012962
keys: Collection[str],

synapse/storage/databases/main/account_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,9 @@ def _add_account_data_for_user(
536536
self.db_pool.simple_insert_many_txn(
537537
txn,
538538
table="ignored_users",
539+
keys=("ignorer_user_id", "ignored_user_id"),
539540
values=[
540-
{"ignorer_user_id": user_id, "ignored_user_id": u}
541-
for u in currently_ignored_users - previously_ignored_users
541+
(user_id, u) for u in currently_ignored_users - previously_ignored_users
542542
],
543543
)
544544

synapse/storage/databases/main/deviceinbox.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,21 @@ def add_messages_txn(txn, now_ms, stream_id):
432432
self.db_pool.simple_insert_many_txn(
433433
txn,
434434
table="device_federation_outbox",
435+
keys=(
436+
"destination",
437+
"stream_id",
438+
"queued_ts",
439+
"messages_json",
440+
"instance_name",
441+
),
435442
values=[
436-
{
437-
"destination": destination,
438-
"stream_id": stream_id,
439-
"queued_ts": now_ms,
440-
"messages_json": json_encoder.encode(edu),
441-
"instance_name": self._instance_name,
442-
}
443+
(
444+
destination,
445+
stream_id,
446+
now_ms,
447+
json_encoder.encode(edu),
448+
self._instance_name,
449+
)
443450
for destination, edu in remote_messages_by_destination.items()
444451
],
445452
)
@@ -571,14 +578,9 @@ def _add_messages_to_local_device_inbox_txn(
571578
self.db_pool.simple_insert_many_txn(
572579
txn,
573580
table="device_inbox",
581+
keys=("user_id", "device_id", "stream_id", "message_json", "instance_name"),
574582
values=[
575-
{
576-
"user_id": user_id,
577-
"device_id": device_id,
578-
"stream_id": stream_id,
579-
"message_json": message_json,
580-
"instance_name": self._instance_name,
581-
}
583+
(user_id, device_id, stream_id, message_json, self._instance_name)
582584
for user_id, messages_by_device in local_by_user_then_device.items()
583585
for device_id, message_json in messages_by_device.items()
584586
],

synapse/storage/databases/main/devices.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,12 +1386,9 @@ def _update_remote_device_list_cache_txn(
13861386
self.db_pool.simple_insert_many_txn(
13871387
txn,
13881388
table="device_lists_remote_cache",
1389+
keys=("user_id", "device_id", "content"),
13891390
values=[
1390-
{
1391-
"user_id": user_id,
1392-
"device_id": content["device_id"],
1393-
"content": json_encoder.encode(content),
1394-
}
1391+
(user_id, content["device_id"], json_encoder.encode(content))
13951392
for content in devices
13961393
],
13971394
)
@@ -1479,8 +1476,9 @@ def _add_device_change_to_stream_txn(
14791476
self.db_pool.simple_insert_many_txn(
14801477
txn,
14811478
table="device_lists_stream",
1479+
keys=("stream_id", "user_id", "device_id"),
14821480
values=[
1483-
{"stream_id": stream_id, "user_id": user_id, "device_id": device_id}
1481+
(stream_id, user_id, device_id)
14841482
for stream_id, device_id in zip(stream_ids, device_ids)
14851483
],
14861484
)
@@ -1507,18 +1505,27 @@ def _add_device_outbound_poke_to_stream_txn(
15071505
self.db_pool.simple_insert_many_txn(
15081506
txn,
15091507
table="device_lists_outbound_pokes",
1508+
keys=(
1509+
"destination",
1510+
"stream_id",
1511+
"user_id",
1512+
"device_id",
1513+
"sent",
1514+
"ts",
1515+
"opentracing_context",
1516+
),
15101517
values=[
1511-
{
1512-
"destination": destination,
1513-
"stream_id": next(next_stream_id),
1514-
"user_id": user_id,
1515-
"device_id": device_id,
1516-
"sent": False,
1517-
"ts": now,
1518-
"opentracing_context": json_encoder.encode(context)
1518+
(
1519+
destination,
1520+
next(next_stream_id),
1521+
user_id,
1522+
device_id,
1523+
False,
1524+
now,
1525+
json_encoder.encode(context)
15191526
if whitelisted_homeserver(destination)
15201527
else "{}",
1521-
}
1528+
)
15221529
for destination in hosts
15231530
for device_id in device_ids
15241531
],

synapse/storage/databases/main/directory.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ def alias_txn(txn: LoggingTransaction) -> None:
112112
self.db_pool.simple_insert_many_txn(
113113
txn,
114114
table="room_alias_servers",
115-
values=[
116-
{"room_alias": room_alias.to_string(), "server": server}
117-
for server in servers
118-
],
115+
keys=("room_alias", "server"),
116+
values=[(room_alias.to_string(), server) for server in servers],
119117
)
120118

121119
self._invalidate_cache_and_stream(

synapse/storage/databases/main/e2e_room_keys.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ async def add_e2e_room_keys(
110110
values = []
111111
for (room_id, session_id, room_key) in room_keys:
112112
values.append(
113-
{
114-
"user_id": user_id,
115-
"version": version_int,
116-
"room_id": room_id,
117-
"session_id": session_id,
118-
"first_message_index": room_key["first_message_index"],
119-
"forwarded_count": room_key["forwarded_count"],
120-
"is_verified": room_key["is_verified"],
121-
"session_data": json_encoder.encode(room_key["session_data"]),
122-
}
113+
(
114+
user_id,
115+
version_int,
116+
room_id,
117+
session_id,
118+
room_key["first_message_index"],
119+
room_key["forwarded_count"],
120+
room_key["is_verified"],
121+
json_encoder.encode(room_key["session_data"]),
122+
)
123123
)
124124
log_kv(
125125
{
@@ -131,7 +131,19 @@ async def add_e2e_room_keys(
131131
)
132132

133133
await self.db_pool.simple_insert_many(
134-
table="e2e_room_keys", values=values, desc="add_e2e_room_keys"
134+
table="e2e_room_keys",
135+
keys=(
136+
"user_id",
137+
"version",
138+
"room_id",
139+
"session_id",
140+
"first_message_index",
141+
"forwarded_count",
142+
"is_verified",
143+
"session_data",
144+
),
145+
values=values,
146+
desc="add_e2e_room_keys",
135147
)
136148

137149
@trace

synapse/storage/databases/main/end_to_end_keys.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,16 @@ def _add_e2e_one_time_keys(txn: LoggingTransaction) -> None:
387387
self.db_pool.simple_insert_many_txn(
388388
txn,
389389
table="e2e_one_time_keys_json",
390+
keys=(
391+
"user_id",
392+
"device_id",
393+
"algorithm",
394+
"key_id",
395+
"ts_added_ms",
396+
"key_json",
397+
),
390398
values=[
391-
{
392-
"user_id": user_id,
393-
"device_id": device_id,
394-
"algorithm": algorithm,
395-
"key_id": key_id,
396-
"ts_added_ms": time_now,
397-
"key_json": json_bytes,
398-
}
399+
(user_id, device_id, algorithm, key_id, time_now, json_bytes)
399400
for algorithm, key_id, json_bytes in new_keys
400401
],
401402
)
@@ -1186,15 +1187,22 @@ async def store_e2e_cross_signing_signatures(
11861187
"""
11871188
await self.db_pool.simple_insert_many(
11881189
"e2e_cross_signing_signatures",
1189-
[
1190-
{
1191-
"user_id": user_id,
1192-
"key_id": item.signing_key_id,
1193-
"target_user_id": item.target_user_id,
1194-
"target_device_id": item.target_device_id,
1195-
"signature": item.signature,
1196-
}
1190+
keys=(
1191+
"user_id",
1192+
"key_id",
1193+
"target_user_id",
1194+
"target_device_id",
1195+
"signature",
1196+
),
1197+
values=[
1198+
(
1199+
user_id,
1200+
item.signing_key_id,
1201+
item.target_user_id,
1202+
item.target_device_id,
1203+
item.signature,
1204+
)
11971205
for item in signatures
11981206
],
1199-
"add_e2e_signing_key",
1207+
desc="add_e2e_signing_key",
12001208
)

0 commit comments

Comments
 (0)