Implement clearing cache after deleting forward extremities

Also run linter.

Signed-off-by: Jason Robinson <jasonr@matrix.org>
This commit is contained in:
Jason Robinson 2021-01-09 21:57:41 +02:00
parent 85c0999bfb
commit 90ad4d443a
2 changed files with 42 additions and 20 deletions

View file

@ -524,18 +524,20 @@ class ForwardExtremitiesRestServlet(RestServlet):
async def resolve_room_id(self, room_identifier: str) -> str:
"""Resolve to a room ID, if necessary."""
if RoomID.is_valid(room_identifier):
room_id = room_identifier
resolved_room_id = room_identifier
elif RoomAlias.is_valid(room_identifier):
room_alias = RoomAlias.from_string(room_identifier)
room_id, _ = await self.room_member_handler.lookup_room_alias(room_alias)
room_id = room_id.to_string()
resolved_room_id = room_id.to_string()
else:
raise SynapseError(
400, "%s was not legal room ID or room alias" % (room_identifier,)
)
if not room_id:
raise SynapseError(400, "Unknown room ID or room alias %s" % room_identifier)
return room_id
if not resolved_room_id:
raise SynapseError(
400, "Unknown room ID or room alias %s" % room_identifier
)
return resolved_room_id
async def on_DELETE(self, request, room_identifier):
requester = await self.auth.get_user_by_req(request)
@ -544,9 +546,7 @@ class ForwardExtremitiesRestServlet(RestServlet):
room_id = await self.resolve_room_id(room_identifier)
deleted_count = await self.store.delete_forward_extremities_for_room(room_id)
return 200, {
"deleted": deleted_count,
}
return 200, {"deleted": deleted_count}
async def on_GET(self, request, room_identifier):
requester = await self.auth.get_user_by_req(request)
@ -555,7 +555,4 @@ class ForwardExtremitiesRestServlet(RestServlet):
room_id = await self.resolve_room_id(room_identifier)
extremities = await self.store.get_forward_extremities_for_room(room_id)
return 200, {
"count": len(extremities),
"results": extremities,
}
return 200, {"count": len(extremities), "results": extremities}

View file

@ -1,15 +1,22 @@
from typing import List, Dict
import logging
from typing import Dict, List
from synapse.api.errors import SynapseError
from synapse.storage._base import SQLBaseStore
logger = logging.getLogger(__name__)
class EventForwardExtremitiesStore(SQLBaseStore):
async def delete_forward_extremities_for_room(self, room_id: str) -> int:
"""Delete any extra forward extremities for a room.
Invalidates the "get_latest_event_ids_in_room" cache if any forward
extremities were deleted.
Returns count deleted.
"""
def delete_forward_extremities_for_room_txn(txn):
# First we need to get the event_id to not delete
sql = (
@ -27,9 +34,17 @@ class EventForwardExtremitiesStore(SQLBaseStore):
)
txn.execute(sql, (room_id,))
rows = txn.fetchall()
# TODO: should this raise a SynapseError instead of better to blow?
try:
event_id = rows[0][0]
logger.debug(
"Found event_id %s as the forward extremity to keep for room %s",
event_id,
room_id,
)
except KeyError:
msg = f"No forward extremity event found for room {room_id}"
logger.warning(msg)
raise SynapseError(400, msg)
# Now delete the extra forward extremities
sql = (
@ -39,19 +54,29 @@ class EventForwardExtremitiesStore(SQLBaseStore):
" AND room_id = ?"
)
# TODO we should not commit yet
txn.execute(sql, (event_id, room_id))
logger.info(
"Deleted %s extra forward extremities for room %s",
txn.rowcount,
room_id,
)
# TODO flush the cache then commit
if txn.rowcount > 0:
# Invalidate the cache
self._invalidate_cache_and_stream(
txn, self.get_latest_event_ids_in_room, (room_id,),
)
return txn.rowcount
return await self.db_pool.runInteraction(
"delete_forward_extremities_for_room", delete_forward_extremities_for_room_txn,
"delete_forward_extremities_for_room",
delete_forward_extremities_for_room_txn,
)
async def get_forward_extremities_for_room(self, room_id: str) -> List[Dict]:
"""Get list of forward extremities for a room."""
def get_forward_extremities_for_room_txn(txn):
sql = (
"SELECT event_id, state_group FROM event_forward_extremities NATURAL JOIN event_to_state_groups "