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

Commit

Permalink
Time how long it takes us to do backfill processing
Browse files Browse the repository at this point in the history
The after timing can be influenced by slow `/state_ids` requests
when proessing the pulled events.

Part of #13356
  • Loading branch information
MadLittleMods committed Aug 16, 2022
1 parent 1b09b08 commit 94fb46e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
26 changes: 26 additions & 0 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)

import attr
from prometheus_client import Histogram
from signedjson.key import decode_verify_key_bytes
from signedjson.sign import verify_signed_json
from unpaddedbase64 import decode_base64
Expand Down Expand Up @@ -80,6 +81,24 @@
logger = logging.getLogger(__name__)


backfill_processing_before_timer = Histogram(
"synapse_federation_backfill_processing_before_time_seconds",
"sec",
[],
buckets=(
1.0,
5.0,
10.0,
20.0,
30.0,
40.0,
60.0,
80.0,
"+Inf",
),
)


def get_domains_from_state(state: StateMap[EventBase]) -> List[Tuple[str, int]]:
"""Get joined domains from state
Expand Down Expand Up @@ -138,6 +157,7 @@ class FederationHandler:
def __init__(self, hs: "HomeServer"):
self.hs = hs

self.clock = hs.get_clock()
self.store = hs.get_datastores().main
self._storage_controllers = hs.get_storage_controllers()
self._state_storage_controller = self._storage_controllers.state
Expand Down Expand Up @@ -203,6 +223,7 @@ async def maybe_backfill(
async def _maybe_backfill_inner(
self, room_id: str, current_depth: int, limit: int
) -> bool:
processing_start_time = self.clock.time_msec()
backwards_extremities = [
_BackfillPoint(event_id, depth, _BackfillPointType.BACKWARDS_EXTREMITY)
for event_id, depth in await self.store.get_oldest_event_ids_with_depth_in_room(
Expand Down Expand Up @@ -425,6 +446,11 @@ async def try_backfill(domains: List[str]) -> bool:

return False

processing_end_time = self.clock.time_msec()
backfill_processing_before_timer.observe(
(processing_start_time - processing_end_time) / 1000
)

success = await try_backfill(likely_domains)
if success:
return True
Expand Down
48 changes: 34 additions & 14 deletions synapse/handlers/federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Tuple,
)

from prometheus_client import Counter
from prometheus_client import Counter, Histogram

from synapse import event_auth
from synapse.api.constants import (
Expand Down Expand Up @@ -92,6 +92,25 @@
"Events received over federation that we marked as soft_failed",
)

backfill_processing_after_timer = Histogram(
"synapse_federation_backfill_processing_after_time_seconds",
"sec",
[],
buckets=(
1.0,
5.0,
10.0,
20.0,
30.0,
40.0,
60.0,
80.0,
120.0,
180.0,
"+Inf",
),
)


class FederationEventHandler:
"""Handles events that originated from federation.
Expand Down Expand Up @@ -597,20 +616,21 @@ async def backfill(
if not events:
return

# if there are any events in the wrong room, the remote server is buggy and
# should not be trusted.
for ev in events:
if ev.room_id != room_id:
raise InvalidResponseError(
f"Remote server {dest} returned event {ev.event_id} which is in "
f"room {ev.room_id}, when we were backfilling in {room_id}"
)
with backfill_processing_after_timer.time():
# if there are any events in the wrong room, the remote server is buggy and
# should not be trusted.
for ev in events:
if ev.room_id != room_id:
raise InvalidResponseError(
f"Remote server {dest} returned event {ev.event_id} which is in "
f"room {ev.room_id}, when we were backfilling in {room_id}"
)

await self._process_pulled_events(
dest,
events,
backfilled=True,
)
await self._process_pulled_events(
dest,
events,
backfilled=True,
)

@trace
async def _get_missing_events_for_pdu(
Expand Down

0 comments on commit 94fb46e

Please sign in to comment.