|
| 1 | +# Copyright 2022 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import logging |
| 16 | +from collections import defaultdict |
| 17 | +from typing import Collection, Dict, Set |
| 18 | + |
| 19 | +from twisted.internet import defer |
| 20 | +from twisted.internet.defer import Deferred |
| 21 | + |
| 22 | +from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable |
| 23 | +from synapse.storage.databases.main.events_worker import EventsWorkerStore |
| 24 | +from synapse.util import unwrapFirstError |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +class PartialStateEventsTracker: |
| 30 | + """Keeps track of which events have partial state, after a partial-state join""" |
| 31 | + |
| 32 | + def __init__(self, store: EventsWorkerStore): |
| 33 | + self._store = store |
| 34 | + self._observers: Dict[str, Set[Deferred]] = defaultdict(set) |
| 35 | + |
| 36 | + def notify_un_partial_stated(self, event_id: str) -> None: |
| 37 | + """Notify that we now have full state for a given event |
| 38 | +
|
| 39 | + Called by the state-resynchronization loop whenever we resynchronize the state |
| 40 | + for a particular event. Unblocks any callers to await_full_state() for that |
| 41 | + event. |
| 42 | +
|
| 43 | + Args: |
| 44 | + event_id: the event that now has full state. |
| 45 | + """ |
| 46 | + observers = self._observers.pop(event_id, None) |
| 47 | + if not observers: |
| 48 | + return |
| 49 | + logger.info( |
| 50 | + "Notifying %i things waiting for un-partial-stating of event %s", |
| 51 | + len(observers), |
| 52 | + event_id, |
| 53 | + ) |
| 54 | + with PreserveLoggingContext(): |
| 55 | + for o in observers: |
| 56 | + o.callback(None) |
| 57 | + |
| 58 | + async def await_full_state(self, event_ids: Collection[str]) -> None: |
| 59 | + """Wait for all the given events to have full state. |
| 60 | +
|
| 61 | + Args: |
| 62 | + event_ids: the list of event ids that we want full state for |
| 63 | + """ |
| 64 | + # first try the happy path: if there are no partial-state events, we can return |
| 65 | + # quickly |
| 66 | + partial_state_event_ids = [ |
| 67 | + ev |
| 68 | + for ev, p in (await self._store.get_partial_state_events(event_ids)).items() |
| 69 | + if p |
| 70 | + ] |
| 71 | + |
| 72 | + if not partial_state_event_ids: |
| 73 | + return |
| 74 | + |
| 75 | + logger.info( |
| 76 | + "Awaiting un-partial-stating of events %s", |
| 77 | + partial_state_event_ids, |
| 78 | + stack_info=True, |
| 79 | + ) |
| 80 | + |
| 81 | + # create an observer for each lazy-joined event |
| 82 | + observers = {event_id: Deferred() for event_id in partial_state_event_ids} |
| 83 | + for event_id, observer in observers.items(): |
| 84 | + self._observers[event_id].add(observer) |
| 85 | + |
| 86 | + try: |
| 87 | + # some of them may have been un-lazy-joined between us checking the db and |
| 88 | + # registering the observer, in which case we'd wait forever for the |
| 89 | + # notification. Call back the observers now. |
| 90 | + for event_id, partial in ( |
| 91 | + await self._store.get_partial_state_events(observers.keys()) |
| 92 | + ).items(): |
| 93 | + if not partial: |
| 94 | + observers[event_id].callback(None) |
| 95 | + |
| 96 | + await make_deferred_yieldable( |
| 97 | + defer.gatherResults( |
| 98 | + observers.values(), |
| 99 | + consumeErrors=True, |
| 100 | + ) |
| 101 | + ).addErrback(unwrapFirstError) |
| 102 | + logger.info("Events %s all un-partial-stated", observers.keys()) |
| 103 | + finally: |
| 104 | + # remove any observers we created. This should happen when the notification |
| 105 | + # is received, but that might not happen for two reasons: |
| 106 | + # (a) we're bailing out early on an exception (including us being |
| 107 | + # cancelled during the await) |
| 108 | + # (b) the event got de-lazy-joined before we set up the observer. |
| 109 | + for event_id, observer in observers.items(): |
| 110 | + observer_set = self._observers.get(event_id) |
| 111 | + if observer_set: |
| 112 | + observer_set.discard(observer) |
| 113 | + if not observer_set: |
| 114 | + del self._observers[event_id] |
0 commit comments