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

Commit ec50c99

Browse files
committed
Merge commit '9b7ac03af' into anoa/dinsic_release_1_21_x
* commit '9b7ac03af': Convert calls of async database methods to async (#8166) simple_search_list_txn should return None, not 0. (#8187) Fix missing _add_persisted_position (#8179)
2 parents 118f41d + 9b7ac03 commit ec50c99

File tree

19 files changed

+171
-93
lines changed

19 files changed

+171
-93
lines changed

changelog.d/8166.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert various parts of the codebase to async/await.

changelog.d/8179.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add functions to `MultiWriterIdGen` used by events stream.

changelog.d/8187.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `synapse.storage.database`.

synapse/federation/persistence.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
import logging
2323

24+
from synapse.federation.units import Transaction
2425
from synapse.logging.utils import log_function
26+
from synapse.types import JsonDict
2527

2628
logger = logging.getLogger(__name__)
2729

@@ -49,15 +51,15 @@ def have_responded(self, origin, transaction):
4951
return self.store.get_received_txn_response(transaction.transaction_id, origin)
5052

5153
@log_function
52-
def set_response(self, origin, transaction, code, response):
54+
async def set_response(
55+
self, origin: str, transaction: Transaction, code: int, response: JsonDict
56+
) -> None:
5357
""" Persist how we responded to a transaction.
54-
55-
Returns:
56-
Deferred
5758
"""
58-
if not transaction.transaction_id:
59+
transaction_id = transaction.transaction_id # type: ignore
60+
if not transaction_id:
5961
raise RuntimeError("Cannot persist a transaction with no transaction_id")
6062

61-
return self.store.set_received_txn_response(
62-
transaction.transaction_id, origin, code, response
63+
await self.store.set_received_txn_response(
64+
transaction_id, origin, code, response
6365
)

synapse/federation/units.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ def __init__(self, transaction_id=None, pdus=[], **kwargs):
107107
if "edus" in kwargs and not kwargs["edus"]:
108108
del kwargs["edus"]
109109

110-
super(Transaction, self).__init__(
111-
transaction_id=transaction_id, pdus=pdus, **kwargs
112-
)
110+
super().__init__(transaction_id=transaction_id, pdus=pdus, **kwargs)
113111

114112
@staticmethod
115113
def create_new(pdus, **kwargs):

synapse/storage/database.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
Optional,
2929
Tuple,
3030
TypeVar,
31-
Union,
3231
overload,
3332
)
3433

@@ -1655,7 +1654,7 @@ def simple_search_list_txn(
16551654
term: Optional[str],
16561655
col: str,
16571656
retcols: Iterable[str],
1658-
) -> Union[List[Dict[str, Any]], int]:
1657+
) -> Optional[List[Dict[str, Any]]]:
16591658
"""Executes a SELECT query on the named table, which may return zero or
16601659
more rows, returning the result as a list of dicts.
16611660
@@ -1667,14 +1666,14 @@ def simple_search_list_txn(
16671666
retcols: the names of the columns to return
16681667
16691668
Returns:
1670-
0 if no term is given, otherwise a list of dictionaries.
1669+
None if no term is given, otherwise a list of dictionaries.
16711670
"""
16721671
if term:
16731672
sql = "SELECT %s FROM %s WHERE %s LIKE ?" % (", ".join(retcols), table, col)
16741673
termvalues = ["%%" + term + "%%"]
16751674
txn.execute(sql, termvalues)
16761675
else:
1677-
return 0
1676+
return None
16781677

16791678
return cls.cursor_to_dict(txn)
16801679

synapse/storage/databases/main/appservice.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,14 @@ async def get_appservice_state(self, service):
161161
return result.get("state")
162162
return None
163163

164-
def set_appservice_state(self, service, state):
164+
async def set_appservice_state(self, service, state) -> None:
165165
"""Set the application service state.
166166
167167
Args:
168168
service(ApplicationService): The service whose state to set.
169169
state(ApplicationServiceState): The connectivity state to apply.
170-
Returns:
171-
An Awaitable which resolves when the state was set successfully.
172170
"""
173-
return self.db_pool.simple_upsert(
171+
await self.db_pool.simple_upsert(
174172
"application_services_state", {"as_id": service.id}, {"state": state}
175173
)
176174

synapse/storage/databases/main/devices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,11 @@ async def get_user_ids_requiring_device_list_resync(
716716

717717
return {row["user_id"] for row in rows}
718718

719-
def mark_remote_user_device_cache_as_stale(self, user_id: str):
719+
async def mark_remote_user_device_cache_as_stale(self, user_id: str) -> None:
720720
"""Records that the server has reason to believe the cache of the devices
721721
for the remote users is out of date.
722722
"""
723-
return self.db_pool.simple_upsert(
723+
await self.db_pool.simple_upsert(
724724
table="device_lists_remote_resync",
725725
keyvalues={"user_id": user_id},
726726
values={},

synapse/storage/databases/main/group_server.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,13 @@ def remove_room_from_summary(self, group_id, room_id, category_id):
742742
desc="remove_room_from_summary",
743743
)
744744

745-
def upsert_group_category(self, group_id, category_id, profile, is_public):
745+
async def upsert_group_category(
746+
self,
747+
group_id: str,
748+
category_id: str,
749+
profile: Optional[JsonDict],
750+
is_public: Optional[bool],
751+
) -> None:
746752
"""Add/update room category for group
747753
"""
748754
insertion_values = {}
@@ -758,7 +764,7 @@ def upsert_group_category(self, group_id, category_id, profile, is_public):
758764
else:
759765
update_values["is_public"] = is_public
760766

761-
return self.db_pool.simple_upsert(
767+
await self.db_pool.simple_upsert(
762768
table="group_room_categories",
763769
keyvalues={"group_id": group_id, "category_id": category_id},
764770
values=update_values,
@@ -773,7 +779,13 @@ def remove_group_category(self, group_id, category_id):
773779
desc="remove_group_category",
774780
)
775781

776-
def upsert_group_role(self, group_id, role_id, profile, is_public):
782+
async def upsert_group_role(
783+
self,
784+
group_id: str,
785+
role_id: str,
786+
profile: Optional[JsonDict],
787+
is_public: Optional[bool],
788+
) -> None:
777789
"""Add/remove user role
778790
"""
779791
insertion_values = {}
@@ -789,7 +801,7 @@ def upsert_group_role(self, group_id, role_id, profile, is_public):
789801
else:
790802
update_values["is_public"] = is_public
791803

792-
return self.db_pool.simple_upsert(
804+
await self.db_pool.simple_upsert(
793805
table="group_roles",
794806
keyvalues={"group_id": group_id, "role_id": role_id},
795807
values=update_values,
@@ -938,10 +950,10 @@ def remove_user_from_summary(self, group_id, user_id, role_id):
938950
desc="remove_user_from_summary",
939951
)
940952

941-
def add_group_invite(self, group_id, user_id):
953+
async def add_group_invite(self, group_id: str, user_id: str) -> None:
942954
"""Record that the group server has invited a user
943955
"""
944-
return self.db_pool.simple_insert(
956+
await self.db_pool.simple_insert(
945957
table="group_invites",
946958
values={"group_id": group_id, "user_id": user_id},
947959
desc="add_group_invite",
@@ -1044,8 +1056,10 @@ def _remove_user_from_group_txn(txn):
10441056
"remove_user_from_group", _remove_user_from_group_txn
10451057
)
10461058

1047-
def add_room_to_group(self, group_id, room_id, is_public):
1048-
return self.db_pool.simple_insert(
1059+
async def add_room_to_group(
1060+
self, group_id: str, room_id: str, is_public: bool
1061+
) -> None:
1062+
await self.db_pool.simple_insert(
10491063
table="group_rooms",
10501064
values={"group_id": group_id, "room_id": room_id, "is_public": is_public},
10511065
desc="add_room_to_group",

synapse/storage/databases/main/keys.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,28 @@ async def store_server_verify_keys(
140140
for i in invalidations:
141141
invalidate((i,))
142142

143-
def store_server_keys_json(
144-
self, server_name, key_id, from_server, ts_now_ms, ts_expires_ms, key_json_bytes
145-
):
143+
async def store_server_keys_json(
144+
self,
145+
server_name: str,
146+
key_id: str,
147+
from_server: str,
148+
ts_now_ms: int,
149+
ts_expires_ms: int,
150+
key_json_bytes: bytes,
151+
) -> None:
146152
"""Stores the JSON bytes for a set of keys from a server
147153
The JSON should be signed by the originating server, the intermediate
148154
server, and by this server. Updates the value for the
149155
(server_name, key_id, from_server) triplet if one already existed.
150156
Args:
151-
server_name (str): The name of the server.
152-
key_id (str): The identifer of the key this JSON is for.
153-
from_server (str): The server this JSON was fetched from.
154-
ts_now_ms (int): The time now in milliseconds.
155-
ts_valid_until_ms (int): The time when this json stops being valid.
156-
key_json (bytes): The encoded JSON.
157+
server_name: The name of the server.
158+
key_id: The identifer of the key this JSON is for.
159+
from_server: The server this JSON was fetched from.
160+
ts_now_ms: The time now in milliseconds.
161+
ts_valid_until_ms: The time when this json stops being valid.
162+
key_json_bytes: The encoded JSON.
157163
"""
158-
return self.db_pool.simple_upsert(
164+
await self.db_pool.simple_upsert(
159165
table="server_keys_json",
160166
keyvalues={
161167
"server_name": server_name,

0 commit comments

Comments
 (0)