Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Add metrics for length of new extremities persisted. #5476

Merged
merged 8 commits into from Jun 21, 2019
Merged
1 change: 1 addition & 0 deletions changelog.d/5476.misc
@@ -0,0 +1 @@
Add new metrics for number of forward extremities being persisted and number of state groups involved in resolution.
11 changes: 11 additions & 0 deletions synapse/state/__init__.py
Expand Up @@ -21,6 +21,7 @@

import attr
from frozendict import frozendict
from prometheus_client import Histogram

from twisted.internet import defer

Expand All @@ -37,6 +38,14 @@
logger = logging.getLogger(__name__)


# Metrics for number of state groups involved in a resolution.
state_groups_histogram = Histogram(
"synapse_state_number_state_groups_in_resolution",
"Number of state groups used when performing a state resolution",
buckets=(1, 2, 3, 5, 7, 10, 15, 20, 50, 100, 200, 500, "+Inf"),
)


KeyStateTuple = namedtuple("KeyStateTuple", ("context", "type", "state_key"))


Expand Down Expand Up @@ -476,6 +485,8 @@ def resolve_state_groups(
"Resolving state for %s with %d groups", room_id, len(state_groups_ids)
)

state_groups_histogram.observe(len(state_groups_ids))

# start by assuming we won't have any conflicted state, and build up the new
# state map by iterating through the state groups. If we discover a conflict,
# we give up and instead use `resolve_events_with_store`.
Expand Down
26 changes: 25 additions & 1 deletion synapse/storage/events.py
Expand Up @@ -24,7 +24,7 @@
from six.moves import range

from canonicaljson import json
from prometheus_client import Counter
from prometheus_client import Counter, Histogram

from twisted.internet import defer

Expand Down Expand Up @@ -74,6 +74,21 @@
"synapse_storage_events_state_delta_reuse_delta", ""
)

# The number of forward extremities for each new event.
forward_extremities_counter = Histogram(
"synapse_storage_events_forward_extremities_persisted",
"Number of forward extremities for each new event",
buckets=(1, 2, 3, 5, 7, 10, 15, 20, 50, 100, 200, 500, "+Inf"),
)

# The number of stale forward extremities for each new event. Stale extremities
# are those that were in the previous set of extremities as well as the new.
stale_forward_extremities_counter = Histogram(
"synapse_storage_events_stale_forward_extremities_persisted",
"Number of unchanged forward extremities for each new event",
buckets=(0, 1, 2, 3, 5, 7, 10, 15, 20, 50, 100, 200, 500, "+Inf"),
)


def encode_json(json_object):
"""
Expand Down Expand Up @@ -541,6 +556,8 @@ def _calculate_new_extremities(self, room_id, event_contexts, latest_event_ids):
and not event.internal_metadata.is_soft_failed()
]

latest_event_ids = set(latest_event_ids)

# start with the existing forward extremities
result = set(latest_event_ids)

Expand All @@ -564,6 +581,13 @@ def _calculate_new_extremities(self, room_id, event_contexts, latest_event_ids):
)
result.difference_update(existing_prevs)

# We only update metrics for events that change forward extremities
# (e.g. we ignore backfill/outliers/etc)
if result != latest_event_ids:
forward_extremities_counter.observe(len(result))
stale = latest_event_ids & result
stale_forward_extremities_counter.observe(len(stale))

defer.returnValue(result)

@defer.inlineCallbacks
Expand Down