2016-04-06 15:12:51 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2018-02-23 12:14:35 +01:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2016-04-06 15:12:51 +02: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 20:00:20 +01:00
|
|
|
import logging
|
2016-04-06 15:12:51 +02:00
|
|
|
|
|
|
|
from synapse.api.constants import EventTypes
|
2019-04-02 13:42:39 +02:00
|
|
|
from synapse.replication.tcp.streams.events import (
|
|
|
|
EventsStreamCurrentStateRow,
|
|
|
|
EventsStreamEventRow,
|
|
|
|
)
|
2019-10-21 13:56:42 +02:00
|
|
|
from synapse.storage.data_stores.main.event_federation import EventFederationWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.event_push_actions import (
|
|
|
|
EventPushActionsWorkerStore,
|
|
|
|
)
|
|
|
|
from synapse.storage.data_stores.main.events_worker import EventsWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.relations import RelationsWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.roommember import RoomMemberWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.signatures import SignatureWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.state import StateGroupWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.stream import StreamWorkerStore
|
|
|
|
from synapse.storage.data_stores.main.user_erasure_store import UserErasureWorkerStore
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2017-11-09 20:00:20 +01:00
|
|
|
from ._base import BaseSlavedStore
|
|
|
|
from ._slaved_id_tracker import SlavedIdTracker
|
2016-11-17 16:46:44 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-04-06 15:12:51 +02: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.
|
|
|
|
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
class SlavedEventStore(
|
|
|
|
EventFederationWorkerStore,
|
|
|
|
RoomMemberWorkerStore,
|
|
|
|
EventPushActionsWorkerStore,
|
|
|
|
StreamWorkerStore,
|
|
|
|
StateGroupWorkerStore,
|
|
|
|
EventsWorkerStore,
|
|
|
|
SignatureWorkerStore,
|
|
|
|
UserErasureWorkerStore,
|
|
|
|
RelationsWorkerStore,
|
|
|
|
BaseSlavedStore,
|
|
|
|
):
|
2016-04-06 15:12:51 +02:00
|
|
|
def __init__(self, db_conn, hs):
|
2019-06-20 11:32:02 +02:00
|
|
|
self._stream_id_gen = SlavedIdTracker(db_conn, "events", "stream_ordering")
|
2016-04-06 15:12:51 +02:00
|
|
|
self._backfill_id_gen = SlavedIdTracker(
|
|
|
|
db_conn, "events", "stream_ordering", step=-1
|
|
|
|
)
|
|
|
|
|
2018-03-01 14:56:03 +01:00
|
|
|
super(SlavedEventStore, self).__init__(db_conn, hs)
|
2016-09-15 12:47:23 +02:00
|
|
|
|
2016-04-06 15:12:51 +02: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 12:46:24 +01:00
|
|
|
|
2018-03-01 14:56:03 +01:00
|
|
|
def get_room_max_stream_ordering(self):
|
|
|
|
return self._stream_id_gen.get_current_token()
|
2016-11-17 16:46:44 +01:00
|
|
|
|
2018-03-01 14:56:03 +01:00
|
|
|
def get_room_min_stream_ordering(self):
|
|
|
|
return self._backfill_id_gen.get_current_token()
|
2016-11-21 12:28:37 +01:00
|
|
|
|
2016-04-06 15:12:51 +02:00
|
|
|
def stream_positions(self):
|
|
|
|
result = super(SlavedEventStore, self).stream_positions()
|
|
|
|
result["events"] = self._stream_id_gen.get_current_token()
|
2016-04-27 16:38:43 +02:00
|
|
|
result["backfill"] = -self._backfill_id_gen.get_current_token()
|
2016-04-06 15:12:51 +02:00
|
|
|
return result
|
|
|
|
|
2017-03-27 17:32:07 +02:00
|
|
|
def process_replication_rows(self, stream_name, token, rows):
|
|
|
|
if stream_name == "events":
|
|
|
|
self._stream_id_gen.advance(token)
|
|
|
|
for row in rows:
|
2019-04-02 13:42:39 +02:00
|
|
|
self._process_event_stream_row(token, row)
|
2017-03-27 17:32:07 +02:00
|
|
|
elif stream_name == "backfill":
|
|
|
|
self._backfill_id_gen.advance(-token)
|
|
|
|
for row in rows:
|
|
|
|
self.invalidate_caches_for_event(
|
2019-06-20 11:32:02 +02:00
|
|
|
-token,
|
|
|
|
row.event_id,
|
|
|
|
row.room_id,
|
|
|
|
row.type,
|
|
|
|
row.state_key,
|
|
|
|
row.redacts,
|
|
|
|
row.relates_to,
|
2017-03-27 17:32:07 +02:00
|
|
|
backfilled=True,
|
2016-04-06 15:12:51 +02:00
|
|
|
)
|
2017-03-27 17:32:07 +02:00
|
|
|
return super(SlavedEventStore, self).process_replication_rows(
|
|
|
|
stream_name, token, rows
|
2016-04-06 15:12:51 +02:00
|
|
|
)
|
|
|
|
|
2019-04-02 13:42:39 +02:00
|
|
|
def _process_event_stream_row(self, token, row):
|
|
|
|
data = row.data
|
|
|
|
|
|
|
|
if row.type == EventsStreamEventRow.TypeId:
|
|
|
|
self.invalidate_caches_for_event(
|
2019-06-20 11:32:02 +02:00
|
|
|
token,
|
|
|
|
data.event_id,
|
|
|
|
data.room_id,
|
|
|
|
data.type,
|
|
|
|
data.state_key,
|
|
|
|
data.redacts,
|
|
|
|
data.relates_to,
|
2019-04-02 13:42:39 +02:00
|
|
|
backfilled=False,
|
|
|
|
)
|
|
|
|
elif row.type == EventsStreamCurrentStateRow.TypeId:
|
|
|
|
if data.type == EventTypes.Member:
|
|
|
|
self.get_rooms_for_user_with_stream_ordering.invalidate(
|
2019-06-20 11:32:02 +02:00
|
|
|
(data.state_key,)
|
2019-04-02 13:42:39 +02:00
|
|
|
)
|
|
|
|
else:
|
2019-06-20 11:32:02 +02:00
|
|
|
raise Exception("Unknown events stream row type %s" % (row.type,))
|
|
|
|
|
|
|
|
def invalidate_caches_for_event(
|
|
|
|
self,
|
|
|
|
stream_ordering,
|
|
|
|
event_id,
|
|
|
|
room_id,
|
|
|
|
etype,
|
|
|
|
state_key,
|
|
|
|
redacts,
|
|
|
|
relates_to,
|
|
|
|
backfilled,
|
|
|
|
):
|
2017-03-17 16:47:51 +01:00
|
|
|
self._invalidate_get_event_cache(event_id)
|
2016-04-06 15:12:51 +02:00
|
|
|
|
2017-03-17 16:47:51 +01:00
|
|
|
self.get_latest_event_ids_in_room.invalidate((room_id,))
|
2016-04-07 14:17:56 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
self.get_unread_event_push_actions_by_room_for_user.invalidate_many((room_id,))
|
2016-04-21 16:25:47 +02:00
|
|
|
|
2016-04-06 15:12:51 +02:00
|
|
|
if not backfilled:
|
2019-06-20 11:32:02 +02:00
|
|
|
self._events_stream_cache.entity_has_changed(room_id, stream_ordering)
|
2016-04-06 15:12:51 +02:00
|
|
|
|
2017-03-17 16:47:51 +01:00
|
|
|
if redacts:
|
|
|
|
self._invalidate_get_event_cache(redacts)
|
2016-04-06 15:12:51 +02:00
|
|
|
|
2017-03-17 16:47:51 +01:00
|
|
|
if etype == EventTypes.Member:
|
2019-06-20 11:32:02 +02:00
|
|
|
self._membership_stream_cache.entity_has_changed(state_key, stream_ordering)
|
2017-03-17 16:47:51 +01:00
|
|
|
self.get_invited_rooms_for_user.invalidate((state_key,))
|
2019-05-16 11:18:53 +02:00
|
|
|
|
|
|
|
if relates_to:
|
|
|
|
self.get_relations_for_event.invalidate_many((relates_to,))
|
|
|
|
self.get_aggregation_groups_for_event.invalidate_many((relates_to,))
|
2019-05-17 16:48:04 +02:00
|
|
|
self.get_applicable_edit.invalidate((relates_to,))
|