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

Convert simple_select_list_paginate_txn to return tuples. #16433

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16433.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce memory allocations.
16 changes: 13 additions & 3 deletions synapse/api/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Optional
from typing import Any, Optional, Tuple

import attr

Expand Down Expand Up @@ -81,8 +81,18 @@ def as_dict(self) -> JsonDict:
return attr.asdict(self)

@staticmethod
def from_dict(d: JsonDict) -> "UserPresenceState":
return UserPresenceState(**d)
def from_db(
row: Tuple[str, str, int, int, int, Optional[str], bool]
) -> "UserPresenceState":
return UserPresenceState(
user_id=row[0],
state=row[1],
last_active_ts=row[2],
last_federation_update_ts=row[3],
last_user_sync_ts=row[4],
status_msg=row[5],
currently_active=bool(row[6]),
)
clokep marked this conversation as resolved.
Show resolved Hide resolved

def copy_and_replace(self, **kwargs: Any) -> "UserPresenceState":
return attr.evolve(self, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion synapse/federation/send_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class PresenceDestinationsRow(BaseFederationRow):
@staticmethod
def from_data(data: JsonDict) -> "PresenceDestinationsRow":
return PresenceDestinationsRow(
state=UserPresenceState.from_dict(data["state"]), destinations=data["dests"]
state=UserPresenceState(**data["state"]), destinations=data["dests"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really need to nuke this, but it seemed like a useless method?

)

def to_data(self) -> JsonDict:
Expand Down
8 changes: 7 additions & 1 deletion synapse/rest/admin/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ async def on_GET(
rooms, total = await self._store.get_destination_rooms_paginate(
destination, start, limit, direction
)
response = {"rooms": rooms, "total": total}
response = {
"rooms": [
{"room_id": room_id, "stream_ordering": stream_ordering}
for room_id, stream_ordering in rooms
],
"total": total,
}
if (start + limit) < total:
response["next_token"] = str(start + len(rooms))

Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2418,7 +2418,7 @@ def simple_select_list_paginate_txn(
keyvalues: Optional[Dict[str, Any]] = None,
exclude_keyvalues: Optional[Dict[str, Any]] = None,
order_direction: str = "ASC",
) -> List[Dict[str, Any]]:
) -> List[Tuple[Any, ...]]:
"""
Executes a SELECT query on the named table with start and limit,
of row numbers, which may return zero or number of rows from start to limit,
Expand Down Expand Up @@ -2474,7 +2474,7 @@ def simple_select_list_paginate_txn(
)
txn.execute(sql, arg_list + [limit, start])

return cls.cursor_to_dict(txn)
return txn.fetchall()

async def simple_search_list(
self,
Expand Down
39 changes: 21 additions & 18 deletions synapse/storage/databases/main/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,28 +385,31 @@ async def get_presence_for_all_users(
limit = 100
offset = 0
while True:
rows = await self.db_pool.runInteraction(
"get_presence_for_all_users",
self.db_pool.simple_select_list_paginate_txn,
"presence_stream",
orderby="stream_id",
start=offset,
limit=limit,
exclude_keyvalues=exclude_keyvalues,
retcols=(
"user_id",
"state",
"last_active_ts",
"last_federation_update_ts",
"last_user_sync_ts",
"status_msg",
"currently_active",
rows = cast(
List[Tuple[str, str, int, int, int, Optional[str], bool]],
await self.db_pool.runInteraction(
"get_presence_for_all_users",
self.db_pool.simple_select_list_paginate_txn,
"presence_stream",
orderby="stream_id",
start=offset,
limit=limit,
exclude_keyvalues=exclude_keyvalues,
retcols=(
"user_id",
"state",
"last_active_ts",
"last_federation_update_ts",
"last_user_sync_ts",
"status_msg",
"currently_active",
),
order_direction="ASC",
),
order_direction="ASC",
)

for row in rows:
users_to_state[row["user_id"]] = UserPresenceState(**row)
users_to_state[row[0]] = UserPresenceState.from_db(row)

# We've run out of updates to query
if len(rows) < limit:
Expand Down
27 changes: 16 additions & 11 deletions synapse/storage/databases/main/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ async def get_destination_rooms_paginate(
start: int,
limit: int,
direction: Direction = Direction.FORWARDS,
) -> Tuple[List[JsonDict], int]:
) -> Tuple[List[Tuple[str, int]], int]:
"""Function to retrieve a paginated list of destination's rooms.
This will return a json list of rooms and the
total number of rooms.
Expand All @@ -537,12 +537,14 @@ async def get_destination_rooms_paginate(
limit: number of rows to retrieve
direction: sort ascending or descending by room_id
Returns:
A tuple of a dict of rooms and a count of total rooms.
A tuple of a list of room tuples and a count of total rooms.

Each room tuple is room_id, stream_ordering.
"""

def get_destination_rooms_paginate_txn(
txn: LoggingTransaction,
) -> Tuple[List[JsonDict], int]:
) -> Tuple[List[Tuple[str, int]], int]:
if direction == Direction.BACKWARDS:
order = "DESC"
else:
Expand All @@ -556,14 +558,17 @@ def get_destination_rooms_paginate_txn(
txn.execute(sql, [destination])
count = cast(Tuple[int], txn.fetchone())[0]

rooms = self.db_pool.simple_select_list_paginate_txn(
txn=txn,
table="destination_rooms",
orderby="room_id",
start=start,
limit=limit,
retcols=("room_id", "stream_ordering"),
order_direction=order,
rooms = cast(
List[Tuple[str, int]],
self.db_pool.simple_select_list_paginate_txn(
txn=txn,
table="destination_rooms",
orderby="room_id",
start=start,
limit=limit,
retcols=("room_id", "stream_ordering"),
order_direction=order,
),
)
return rooms, count

Expand Down
Loading