-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Split up txn for fetching device keys #15215
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Refactor database transaction for query users' devices to reduce database pool contention. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,9 +244,7 @@ async def get_e2e_device_keys_and_signatures( | |
set_tag("include_all_devices", include_all_devices) | ||
set_tag("include_deleted_devices", include_deleted_devices) | ||
|
||
result = await self.db_pool.runInteraction( | ||
"get_e2e_device_keys", | ||
self._get_e2e_device_keys_txn, | ||
result = await self._get_e2e_device_keys( | ||
query_list, | ||
include_all_devices, | ||
include_deleted_devices, | ||
|
@@ -285,9 +283,8 @@ async def get_e2e_device_keys_and_signatures( | |
log_kv(result) | ||
return result | ||
|
||
def _get_e2e_device_keys_txn( | ||
async def _get_e2e_device_keys( | ||
self, | ||
txn: LoggingTransaction, | ||
query_list: Collection[Tuple[str, Optional[str]]], | ||
include_all_devices: bool = False, | ||
include_deleted_devices: bool = False, | ||
|
@@ -319,7 +316,7 @@ def _get_e2e_device_keys_txn( | |
|
||
if user_list: | ||
user_id_in_list_clause, user_args = make_in_list_sql_clause( | ||
txn.database_engine, "user_id", user_list | ||
self.database_engine, "user_id", user_list | ||
) | ||
query_clauses.append(user_id_in_list_clause) | ||
query_params_list.append(user_args) | ||
|
@@ -332,13 +329,16 @@ def _get_e2e_device_keys_txn( | |
user_device_id_in_list_clause, | ||
user_device_args, | ||
) = make_tuple_in_list_sql_clause( | ||
txn.database_engine, ("user_id", "device_id"), user_device_batch | ||
self.database_engine, ("user_id", "device_id"), user_device_batch | ||
) | ||
query_clauses.append(user_device_id_in_list_clause) | ||
query_params_list.append(user_device_args) | ||
|
||
result: Dict[str, Dict[str, Optional[DeviceKeyLookupResult]]] = {} | ||
for query_clause, query_params in zip(query_clauses, query_params_list): | ||
|
||
def get_e2e_device_keys_txn( | ||
txn: LoggingTransaction, query_clause: str, query_params: list | ||
) -> None: | ||
sql = ( | ||
"SELECT user_id, device_id, " | ||
" d.display_name, " | ||
|
@@ -361,6 +361,14 @@ def _get_e2e_device_keys_txn( | |
display_name, db_to_json(key_json) if key_json else None | ||
) | ||
|
||
for query_clause, query_params in zip(query_clauses, query_params_list): | ||
await self.db_pool.runInteraction( | ||
"_get_e2e_device_keys", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if you purposefully used a new transaction description or not (it used to not have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, doesn't really matter. Though unintended consequence of this means we're not going to confuse the two types of transactions in the graphs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah sometimes I feel like it is desirable to re-use it, even if it isn't apples-to-apples. I guess that's more important with cache names, maybe. 🤷 |
||
get_e2e_device_keys_txn, | ||
query_clause, | ||
query_params, | ||
) | ||
|
||
if include_deleted_devices: | ||
for user_id, device_id in deleted_devices: | ||
if device_id is None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this just something you found while debugging your code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't really understand what is going on here either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh, sorry missed this comment.
It's because we had the following construct in the new code:
Where if
a
is false,b
is unbound and socell.cell_contents
raises aValueError: cell is empty