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

Backfill remote event fetched by MSC3030 so we can paginate from it later #13205

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
93bbac7
Backfill remote event fetched by MSC3030 so we can paginate from it…
MadLittleMods Jul 6, 2022
f645864
Working logs
MadLittleMods Jul 6, 2022
6c4b618
Working Complement test by getting new raw event which isn't an outlier
MadLittleMods Jul 6, 2022
2e01535
Add changelog
MadLittleMods Jul 6, 2022
b335d44
Debug logs
MadLittleMods Jul 8, 2022
dab7ad9
More debugging
MadLittleMods Jul 9, 2022
f3072c9
Working with get_pdu
MadLittleMods Jul 11, 2022
3db79a1
Merge branch 'develop' into madlittlemods/msc3030-backfill-at-remote-…
MadLittleMods Jul 11, 2022
3f0da1b
Install tabulate back and working
MadLittleMods Jul 11, 2022
b9c936c
Clean up debug logs
MadLittleMods Jul 11, 2022
e76fbc0
More cleanup
MadLittleMods Jul 11, 2022
7d9c20a
Better order and fix lints
MadLittleMods Jul 11, 2022
48ca870
Revert fast complement changes
MadLittleMods Jul 11, 2022
6543bd5
Remove unused persist_events_store
MadLittleMods Jul 11, 2022
8b0dd8c
Better logging
MadLittleMods Jul 11, 2022
ba344b4
Add comment why _process_pulled_events
MadLittleMods Jul 11, 2022
caa0fce
Add docstring
MadLittleMods Jul 11, 2022
05dc230
Fix logic error
MadLittleMods Jul 11, 2022
7a316a5
Fix wrong scope in log
MadLittleMods Jul 15, 2022
682399f
Remove whitespace changes
MadLittleMods Jul 15, 2022
4df2f0c
Use shorthand
MadLittleMods Jul 15, 2022
ce447f0
Merge branch 'develop' into madlittlemods/msc3030-backfill-at-remote-…
MadLittleMods Jul 15, 2022
2beeccd
Remove duplicate information from error
MadLittleMods Jul 15, 2022
7f866f4
Log what the remote event is closer than
MadLittleMods Jul 15, 2022
337d8be
Explain why no persisting outliers in _process_pulled_event
MadLittleMods Jul 15, 2022
efaf434
get_pdu returns pristine EventBase
MadLittleMods Jul 15, 2022
344e63e
Fix lints
MadLittleMods Jul 15, 2022
cf5a324
Fix unused ex lint
MadLittleMods Jul 15, 2022
b3743c2
Fix lint
MadLittleMods Jul 15, 2022
b2be2bc
Use timestamp from the event we backfilled instead of trusting the re…
MadLittleMods Jul 16, 2022
7dbc4f7
Merge branch 'develop' into madlittlemods/msc3030-backfill-at-remote-…
MadLittleMods Jul 20, 2022
2d1a84b
Restore whitespace
MadLittleMods Jul 20, 2022
bf4e5d6
Add ideas the comment
MadLittleMods Jul 22, 2022
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
4 changes: 2 additions & 2 deletions scripts-dev/complement.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ if [ -z "$skip_docker_build" ]; then
# Build the unified Complement image (from the worker Synapse image we just built).
echo_if_github "::group::Build Docker image: complement/Dockerfile"
docker build -t complement-synapse \
-f "docker/complement/Dockerfile" "docker/complement"
-f "docker/complement/Dockerfile" "docker/complement"
echo_if_github "::endgroup::"
fi

Expand Down Expand Up @@ -161,4 +161,4 @@ fi
echo "Images built; running complement"
cd "$COMPLEMENT_DIR"

go test -v -tags $test_tags -count=1 "${extra_test_args[@]}" "$@" ./tests/...
go test -v -tags $test_tags -count=1 "${extra_test_args[@]}" "$@" ./tests/
16 changes: 14 additions & 2 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ async def get_pdu_from_destination_raw(
destination, event_id, timeout=timeout
)

logger.debug(
"retrieved event id %s from %s: %r",
logger.info(
"get_pdu_raw retrieved event id %s from %s: %r",
event_id,
destination,
transaction_data,
Expand All @@ -319,6 +319,12 @@ async def get_pdu_from_destination_raw(
event_from_pdu_json(p, room_version) for p in transaction_data["pdus"]
]

logger.info(
"get_pdu_raw pdu_list[0]=%s outlier=%s",
pdu_list[0],
pdu_list[0].internal_metadata.outlier,
)

if pdu_list and pdu_list[0]:
pdu = pdu_list[0]

Expand Down Expand Up @@ -361,6 +367,12 @@ async def get_pdu(
# TODO: Rate limit the number of times we try and get the same event.

ev = self._get_pdu_cache.get(event_id)
logger.info(
"get_pdu from cache event_id=%s ev=%s outlier=%s",
event_id,
ev,
ev and ev.internal_metadata.outlier,
)
if ev:
return ev

Expand Down
41 changes: 41 additions & 0 deletions synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ async def _process_pulled_event(
"""
logger.info("Processing pulled event %s", event)

MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
# TODO: Why does this matter? The whole point of this function is to
# persist random PDU's from backfill. It shouldn't matter whether we saw
# them somewhere else first as an outlier, then during backfill. This
# function handles de-outliering anyway.
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
#
# these should not be outliers.
assert (
not event.internal_metadata.is_outlier()
Expand Down Expand Up @@ -1315,6 +1320,42 @@ async def _handle_marker_event(self, origin: str, marker_event: EventBase) -> No
marker_event,
)

async def backfill_event(
self, destination: str, room_id: str, event_id: str
) -> None:
room_version = await self._store.get_room_version(room_id)

logger.info("backfill_event event_id=%s", event_id)

eventAsdf = await self._federation_client.get_pdu(
[destination],
event_id,
room_version,
)
event = await self._federation_client.get_pdu_from_destination_raw(
destination,
event_id,
room_version,
)
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
logger.info(
"backfill_event get_pdu=%s get_pdu_from_destination_raw=%s",
eventAsdf,
event,
)
# # FIXME: Too sketchy?
# event.internal_metadata.outlier = False

logger.info(
"backfill_event event=%s outlier=%s", event, event.internal_metadata.outlier
)

await self._process_pulled_events(
destination,
[event],
# Prevent notifications going to clients
backfilled=True,
)

async def _get_events_and_persist(
self, destination: str, room_id: str, event_ids: Collection[str]
) -> None:
Expand Down
38 changes: 29 additions & 9 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@ def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastores().main
self.state_handler = hs.get_state_handler()
self.federation_client = hs.get_federation_client()
self.federation_event_handler = hs.get_federation_event_handler()
self._storage_controllers = hs.get_storage_controllers()

async def get_event_for_timestamp(
Expand Down Expand Up @@ -1365,7 +1366,7 @@ async def get_event_for_timestamp(
local_event_id = await self.store.get_event_id_for_timestamp(
room_id, timestamp, direction
)
logger.debug(
logger.info(
"get_event_for_timestamp: locally, we found event_id=%s closest to timestamp=%s",
local_event_id,
timestamp,
Expand All @@ -1375,6 +1376,7 @@ async def get_event_for_timestamp(
# the timestamp given and the event we were able to find locally
is_event_next_to_backward_gap = False
is_event_next_to_forward_gap = False
local_event = None
if local_event_id:
local_event = await self.store.get_event(
local_event_id, allow_none=False, allow_rejected=False
Expand All @@ -1400,7 +1402,7 @@ async def get_event_for_timestamp(
or is_event_next_to_backward_gap
or is_event_next_to_forward_gap
):
logger.debug(
logger.info(
"get_event_for_timestamp: locally, we found event_id=%s closest to timestamp=%s which is next to a gap in event history so we're asking other homeservers first",
local_event_id,
timestamp,
Expand All @@ -1421,31 +1423,48 @@ async def get_event_for_timestamp(
remote_response = await self.federation_client.timestamp_to_event(
domain, room_id, timestamp, direction
)
logger.debug(
logger.info(
"get_event_for_timestamp: response from domain(%s)=%s",
domain,
remote_response,
)

remote_event_id = remote_response.event_id
origin_server_ts = remote_response.origin_server_ts

# TODO: Do we want to persist this as an extremity?
# TODO: I think ideally, we would try to backfill from
# this event and run this whole
# `get_event_for_timestamp` function again to make sure
# they didn't give us an event from their gappy history.
remote_event_id = remote_response.event_id
origin_server_ts = remote_response.origin_server_ts
# await self.federation_event_handler._get_events_and_persist(
# domain,
# room_id,
# [remote_event_id],
# )
await self.federation_event_handler.backfill_event(
domain, room_id, remote_event_id
)
logger.info(
"get_event_for_timestamp: asdf %s",
remote_event_id,
)

# Only return the remote event if it's closer than the local event
if not local_event or (
abs(origin_server_ts - timestamp)
< abs(local_event.origin_server_ts - timestamp)
):
logger.info(
"get_event_for_timestamp: returning remote_event_id=%s since it's closer",
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
remote_event_id,
)
return remote_event_id, origin_server_ts
except (HttpResponseException, InvalidResponseError) as ex:
# Let's not put a high priority on some other homeserver
# failing to respond or giving a random response
logger.debug(
"Failed to fetch /timestamp_to_event from %s because of exception(%s) %s args=%s",
logger.info(
"get_event_for_timestamp: Failed to fetch /timestamp_to_event from %s because of exception(%s) %s args=%s",
domain,
type(ex).__name__,
ex,
Expand All @@ -1454,14 +1473,15 @@ async def get_event_for_timestamp(
except Exception as ex:
# But we do want to see some exceptions in our code
logger.warning(
"Failed to fetch /timestamp_to_event from %s because of exception(%s) %s args=%s",
"get_event_for_timestamp: Failed to fetch /timestamp_to_event from %s because of exception(%s) %s args=%s",
domain,
type(ex).__name__,
ex,
ex.args,
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
exc_info=(type(ex), ex, ex.__traceback__),
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
)

if not local_event_id:
if not local_event_id or not local_event:
raise SynapseError(
404,
"Unable to find event from %s in direction %s" % (timestamp, direction),
Expand Down