2016-04-06 07:12:51 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2018-02-23 04:14:35 -07:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2016-04-06 07:12:51 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2017-11-09 12:00:20 -07:00
|
|
|
import logging
|
2016-04-06 07:12:51 -06:00
|
|
|
|
|
|
|
from synapse.api.constants import EventTypes
|
2018-03-01 07:16:02 -07:00
|
|
|
from synapse.storage.event_federation import EventFederationWorkerStore
|
2018-02-21 04:01:13 -07:00
|
|
|
from synapse.storage.event_push_actions import EventPushActionsWorkerStore
|
2018-02-23 04:01:21 -07:00
|
|
|
from synapse.storage.events_worker import EventsWorkerStore
|
2018-02-21 05:01:08 -07:00
|
|
|
from synapse.storage.roommember import RoomMemberWorkerStore
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.storage.signatures import SignatureWorkerStore
|
2018-02-06 07:31:24 -07:00
|
|
|
from synapse.storage.state import StateGroupWorkerStore
|
2018-03-01 06:56:03 -07:00
|
|
|
from synapse.storage.stream import StreamWorkerStore
|
2018-06-25 07:22:24 -06:00
|
|
|
from synapse.storage.user_erasure_store import UserErasureWorkerStore
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2017-11-09 12:00:20 -07:00
|
|
|
from ._base import BaseSlavedStore
|
|
|
|
from ._slaved_id_tracker import SlavedIdTracker
|
2016-11-17 08:46:44 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
|
|
|
|
# So, um, we want to borrow a load of functions intended for reading from
|
|
|
|
# a DataStore, but we don't want to take functions that either write to the
|
|
|
|
# DataStore or are cached and don't have cache invalidation logic.
|
|
|
|
#
|
|
|
|
# Rather than write duplicate versions of those functions, or lift them to
|
|
|
|
# a common base class, we going to grab the underlying __func__ object from
|
|
|
|
# the method descriptor on the DataStore and chuck them into our class.
|
|
|
|
|
|
|
|
|
2018-03-01 07:16:02 -07:00
|
|
|
class SlavedEventStore(EventFederationWorkerStore,
|
|
|
|
RoomMemberWorkerStore,
|
|
|
|
EventPushActionsWorkerStore,
|
2018-03-01 09:59:39 -07:00
|
|
|
StreamWorkerStore,
|
2018-03-01 07:16:02 -07:00
|
|
|
StateGroupWorkerStore,
|
2018-07-25 15:10:39 -06:00
|
|
|
EventsWorkerStore,
|
2018-03-01 07:16:02 -07:00
|
|
|
SignatureWorkerStore,
|
2018-06-25 07:22:24 -06:00
|
|
|
UserErasureWorkerStore,
|
2018-02-23 04:46:24 -07:00
|
|
|
BaseSlavedStore):
|
2016-04-06 07:12:51 -06:00
|
|
|
|
|
|
|
def __init__(self, db_conn, hs):
|
|
|
|
self._stream_id_gen = SlavedIdTracker(
|
|
|
|
db_conn, "events", "stream_ordering",
|
|
|
|
)
|
|
|
|
self._backfill_id_gen = SlavedIdTracker(
|
|
|
|
db_conn, "events", "stream_ordering", step=-1
|
|
|
|
)
|
|
|
|
|
2018-03-01 06:56:03 -07:00
|
|
|
super(SlavedEventStore, self).__init__(db_conn, hs)
|
2016-09-15 04:47:23 -06:00
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
# Cached functions can't be accessed through a class instance so we need
|
|
|
|
# to reach inside the __dict__ to extract them.
|
2018-02-23 04:46:24 -07:00
|
|
|
|
2018-03-01 06:56:03 -07:00
|
|
|
def get_room_max_stream_ordering(self):
|
|
|
|
return self._stream_id_gen.get_current_token()
|
2016-11-17 08:46:44 -07:00
|
|
|
|
2018-03-01 06:56:03 -07:00
|
|
|
def get_room_min_stream_ordering(self):
|
|
|
|
return self._backfill_id_gen.get_current_token()
|
2016-11-21 04:28:37 -07:00
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
def stream_positions(self):
|
|
|
|
result = super(SlavedEventStore, self).stream_positions()
|
|
|
|
result["events"] = self._stream_id_gen.get_current_token()
|
2016-04-27 08:38:43 -06:00
|
|
|
result["backfill"] = -self._backfill_id_gen.get_current_token()
|
2016-04-06 07:12:51 -06:00
|
|
|
return result
|
|
|
|
|
2017-03-27 09:32:07 -06:00
|
|
|
def process_replication_rows(self, stream_name, token, rows):
|
|
|
|
if stream_name == "events":
|
|
|
|
self._stream_id_gen.advance(token)
|
|
|
|
for row in rows:
|
|
|
|
self.invalidate_caches_for_event(
|
|
|
|
token, row.event_id, row.room_id, row.type, row.state_key,
|
|
|
|
row.redacts,
|
|
|
|
backfilled=False,
|
2016-04-06 07:12:51 -06:00
|
|
|
)
|
2017-03-27 09:32:07 -06:00
|
|
|
elif stream_name == "backfill":
|
|
|
|
self._backfill_id_gen.advance(-token)
|
|
|
|
for row in rows:
|
|
|
|
self.invalidate_caches_for_event(
|
|
|
|
-token, row.event_id, row.room_id, row.type, row.state_key,
|
|
|
|
row.redacts,
|
|
|
|
backfilled=True,
|
2016-04-06 07:12:51 -06:00
|
|
|
)
|
2017-03-27 09:32:07 -06:00
|
|
|
return super(SlavedEventStore, self).process_replication_rows(
|
|
|
|
stream_name, token, rows
|
2016-04-06 07:12:51 -06:00
|
|
|
)
|
|
|
|
|
2017-03-17 09:47:51 -06:00
|
|
|
def invalidate_caches_for_event(self, stream_ordering, event_id, room_id,
|
|
|
|
etype, state_key, redacts, backfilled):
|
|
|
|
self._invalidate_get_event_cache(event_id)
|
2016-04-06 07:12:51 -06:00
|
|
|
|
2017-03-17 09:47:51 -06:00
|
|
|
self.get_latest_event_ids_in_room.invalidate((room_id,))
|
2016-04-07 06:17:56 -06:00
|
|
|
|
2016-04-21 08:25:47 -06:00
|
|
|
self.get_unread_event_push_actions_by_room_for_user.invalidate_many(
|
2017-03-17 09:47:51 -06:00
|
|
|
(room_id,)
|
2016-04-21 08:25:47 -06:00
|
|
|
)
|
|
|
|
|
2016-04-06 07:12:51 -06:00
|
|
|
if not backfilled:
|
|
|
|
self._events_stream_cache.entity_has_changed(
|
2017-03-17 09:47:51 -06:00
|
|
|
room_id, stream_ordering
|
2016-04-06 07:12:51 -06:00
|
|
|
)
|
|
|
|
|
2017-03-17 09:47:51 -06:00
|
|
|
if redacts:
|
|
|
|
self._invalidate_get_event_cache(redacts)
|
2016-04-06 07:12:51 -06:00
|
|
|
|
2017-03-17 09:47:51 -06:00
|
|
|
if etype == EventTypes.Member:
|
2016-06-03 04:19:27 -06:00
|
|
|
self._membership_stream_cache.entity_has_changed(
|
2017-03-17 09:47:51 -06:00
|
|
|
state_key, stream_ordering
|
2016-06-03 04:19:27 -06:00
|
|
|
)
|
2017-03-17 09:47:51 -06:00
|
|
|
self.get_invited_rooms_for_user.invalidate((state_key,))
|