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

Add type hints to state handler. #10482

Merged
merged 2 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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/10482.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Additional type hints in the state handler.
26 changes: 15 additions & 11 deletions synapse/state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
from collections import defaultdict, namedtuple
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
Expand Down Expand Up @@ -52,6 +53,10 @@
from synapse.util.caches.expiringcache import ExpiringCache
from synapse.util.metrics import Measure, measure_func

if TYPE_CHECKING:
from synapse.server import HomeServer
from synapse.storage.databases.main import DataStore

logger = logging.getLogger(__name__)
metrics_logger = logging.getLogger("synapse.state.metrics")

Expand All @@ -74,7 +79,7 @@
POWER_KEY = (EventTypes.PowerLevels, "")


def _gen_state_id():
def _gen_state_id() -> str:
global _NEXT_STATE_ID
s = "X%d" % (_NEXT_STATE_ID,)
_NEXT_STATE_ID += 1
Expand Down Expand Up @@ -109,7 +114,7 @@ def __init__(
# `state_id` is either a state_group (and so an int) or a string. This
# ensures we don't accidentally persist a state_id as a stateg_group
if state_group:
self.state_id = state_group
self.state_id: Union[str, int] = state_group
else:
self.state_id = _gen_state_id()

Expand All @@ -122,7 +127,7 @@ class StateHandler:
where necessary
"""

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
self.clock = hs.get_clock()
self.store = hs.get_datastore()
self.state_store = hs.get_storage().state
Expand Down Expand Up @@ -507,7 +512,7 @@ class StateResolutionHandler:
be storage-independent.
"""

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
self.clock = hs.get_clock()

self.resolve_linearizer = Linearizer(name="state_resolve_lock")
Expand Down Expand Up @@ -653,13 +658,15 @@ async def resolve_events_with_store(
finally:
self._record_state_res_metrics(room_id, m.get_resource_usage())

def _record_state_res_metrics(self, room_id: str, rusage: ContextResourceUsage):
def _record_state_res_metrics(
self, room_id: str, rusage: ContextResourceUsage
) -> None:
room_metrics = self._state_res_metrics[room_id]
room_metrics.cpu_time += rusage.ru_utime + rusage.ru_stime
room_metrics.db_time += rusage.db_txn_duration_sec
room_metrics.db_events += rusage.evt_db_fetch_count

def _report_metrics(self):
def _report_metrics(self) -> None:
if not self._state_res_metrics:
# no state res has happened since the last iteration: don't bother logging.
return
Expand Down Expand Up @@ -769,16 +776,13 @@ def _make_state_cache_entry(
)


@attr.s(slots=True)
@attr.s(slots=True, auto_attribs=True)
class StateResolutionStore:
"""Interface that allows state resolution algorithms to access the database
in well defined way.

Args:
store (DataStore)
"""

store = attr.ib()
store: "DataStore"

def get_events(
self, event_ids: Iterable[str], allow_rejected: bool = False
Expand Down
17 changes: 11 additions & 6 deletions synapse/storage/databases/state/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,18 +372,23 @@ def _insert_into_cache(
)

async def store_state_group(
self, event_id, room_id, prev_group, delta_ids, current_state_ids
self,
event_id: str,
room_id: str,
prev_group: Optional[int],
delta_ids: Optional[StateMap[str]],
current_state_ids: StateMap[str],
) -> int:
"""Store a new set of state, returning a newly assigned state group.

Args:
event_id (str): The event ID for which the state was calculated
room_id (str)
prev_group (int|None): A previous state group for the room, optional.
delta_ids (dict|None): The delta between state at `prev_group` and
event_id: The event ID for which the state was calculated
room_id
prev_group: A previous state group for the room, optional.
delta_ids: The delta between state at `prev_group` and
`current_state_ids`, if `prev_group` was given. Same format as
`current_state_ids`.
current_state_ids (dict): The state to store. Map of (type, state_key)
current_state_ids: The state to store. Map of (type, state_key)
to event_id.

Returns:
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ async def store_state_group(
event_id: str,
room_id: str,
prev_group: Optional[int],
delta_ids: Optional[dict],
current_state_ids: dict,
delta_ids: Optional[StateMap[str]],
current_state_ids: StateMap[str],
) -> int:
"""Store a new set of state, returning a newly assigned state group.

Expand Down