2016-04-06 09:12:51 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2018-02-23 05:52:10 -05:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2016-04-06 09:12:51 -04: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 14:00:20 -05:00
|
|
|
import logging
|
2016-04-06 09:12:51 -04:00
|
|
|
|
|
|
|
from synapse.api.constants import EventTypes
|
|
|
|
from synapse.storage import DataStore
|
|
|
|
from synapse.storage.event_federation import EventFederationStore
|
2018-02-21 06:01:13 -05:00
|
|
|
from synapse.storage.event_push_actions import EventPushActionsWorkerStore
|
2018-02-23 06:01:21 -05:00
|
|
|
from synapse.storage.events_worker import EventsWorkerStore
|
2017-11-09 14:00:20 -05:00
|
|
|
from synapse.storage.roommember import RoomMemberStore
|
2018-02-06 09:31:24 -05:00
|
|
|
from synapse.storage.state import StateGroupWorkerStore
|
2016-06-03 06:19:27 -04:00
|
|
|
from synapse.storage.stream import StreamStore
|
2018-02-06 05:55:40 -05:00
|
|
|
from synapse.storage.signatures import SignatureStore
|
2016-04-06 09:12:51 -04:00
|
|
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
2017-11-09 14:00:20 -05:00
|
|
|
from ._base import BaseSlavedStore
|
|
|
|
from ._slaved_id_tracker import SlavedIdTracker
|
2016-11-17 10:46:44 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-04-06 09:12:51 -04: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-02-23 06:26:31 -05:00
|
|
|
class SlavedEventStore(EventPushActionsWorkerStore, EventsWorkerStore,
|
|
|
|
StateGroupWorkerStore, BaseSlavedStore):
|
2016-04-06 09:12:51 -04:00
|
|
|
|
|
|
|
def __init__(self, db_conn, hs):
|
|
|
|
super(SlavedEventStore, self).__init__(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
|
|
|
|
)
|
|
|
|
events_max = self._stream_id_gen.get_current_token()
|
|
|
|
event_cache_prefill, min_event_val = self._get_cache_dict(
|
|
|
|
db_conn, "events",
|
|
|
|
entity_column="room_id",
|
|
|
|
stream_column="stream_ordering",
|
|
|
|
max_value=events_max,
|
|
|
|
)
|
|
|
|
self._events_stream_cache = StreamChangeCache(
|
|
|
|
"EventsRoomStreamChangeCache", min_event_val,
|
|
|
|
prefilled_cache=event_cache_prefill,
|
|
|
|
)
|
2016-06-03 06:19:27 -04:00
|
|
|
self._membership_stream_cache = StreamChangeCache(
|
|
|
|
"MembershipStreamChangeCache", events_max,
|
|
|
|
)
|
2016-04-06 09:12:51 -04:00
|
|
|
|
2016-09-15 06:47:23 -04:00
|
|
|
self.stream_ordering_month_ago = 0
|
2016-09-16 03:37:39 -04:00
|
|
|
self._stream_order_on_start = self.get_room_max_stream_ordering()
|
2016-09-15 06:47:23 -04:00
|
|
|
|
2016-04-06 09:12:51 -04:00
|
|
|
# Cached functions can't be accessed through a class instance so we need
|
|
|
|
# to reach inside the __dict__ to extract them.
|
|
|
|
get_rooms_for_user = RoomMemberStore.__dict__["get_rooms_for_user"]
|
|
|
|
get_users_in_room = RoomMemberStore.__dict__["get_users_in_room"]
|
2017-04-10 11:48:30 -04:00
|
|
|
get_hosts_in_room = RoomMemberStore.__dict__["get_hosts_in_room"]
|
2017-02-02 10:02:03 -05:00
|
|
|
get_users_who_share_room_with_user = (
|
|
|
|
RoomMemberStore.__dict__["get_users_who_share_room_with_user"]
|
|
|
|
)
|
2016-04-06 09:12:51 -04:00
|
|
|
get_latest_event_ids_in_room = EventFederationStore.__dict__[
|
|
|
|
"get_latest_event_ids_in_room"
|
|
|
|
]
|
2016-04-19 10:22:14 -04:00
|
|
|
get_invited_rooms_for_user = RoomMemberStore.__dict__[
|
|
|
|
"get_invited_rooms_for_user"
|
|
|
|
]
|
2016-06-03 06:19:27 -04:00
|
|
|
get_recent_event_ids_for_room = (
|
|
|
|
StreamStore.__dict__["get_recent_event_ids_for_room"]
|
|
|
|
)
|
2017-05-25 12:08:41 -04:00
|
|
|
_get_joined_hosts_cache = RoomMemberStore.__dict__["_get_joined_hosts_cache"]
|
2017-03-13 12:24:54 -04:00
|
|
|
has_room_changed_since = DataStore.has_room_changed_since.__func__
|
2016-04-06 09:12:51 -04:00
|
|
|
|
|
|
|
get_rooms_for_user_where_membership_is = (
|
|
|
|
DataStore.get_rooms_for_user_where_membership_is.__func__
|
|
|
|
)
|
|
|
|
get_membership_changes_for_user = (
|
|
|
|
DataStore.get_membership_changes_for_user.__func__
|
|
|
|
)
|
|
|
|
get_room_events_max_id = DataStore.get_room_events_max_id.__func__
|
|
|
|
get_room_events_stream_for_room = (
|
|
|
|
DataStore.get_room_events_stream_for_room.__func__
|
|
|
|
)
|
2016-05-13 11:58:14 -04:00
|
|
|
get_events_around = DataStore.get_events_around.__func__
|
2016-08-31 09:45:04 -04:00
|
|
|
get_joined_users_from_state = DataStore.get_joined_users_from_state.__func__
|
2016-08-30 04:55:17 -04:00
|
|
|
get_joined_users_from_context = DataStore.get_joined_users_from_context.__func__
|
|
|
|
_get_joined_users_from_context = (
|
|
|
|
RoomMemberStore.__dict__["_get_joined_users_from_context"]
|
|
|
|
)
|
|
|
|
|
2017-05-04 09:29:03 -04:00
|
|
|
get_joined_hosts = DataStore.get_joined_hosts.__func__
|
|
|
|
_get_joined_hosts = RoomMemberStore.__dict__["_get_joined_hosts"]
|
|
|
|
|
2016-06-03 06:19:27 -04:00
|
|
|
get_recent_events_for_room = DataStore.get_recent_events_for_room.__func__
|
|
|
|
get_room_events_stream_for_rooms = (
|
|
|
|
DataStore.get_room_events_stream_for_rooms.__func__
|
|
|
|
)
|
2017-06-13 04:56:18 -04:00
|
|
|
is_host_joined = RoomMemberStore.__dict__["is_host_joined"]
|
2016-06-03 06:19:27 -04:00
|
|
|
get_stream_token_for_event = DataStore.get_stream_token_for_event.__func__
|
2016-04-19 10:22:14 -04:00
|
|
|
|
2016-06-03 06:19:27 -04:00
|
|
|
_set_before_and_after = staticmethod(DataStore._set_before_and_after)
|
2016-04-06 09:12:51 -04:00
|
|
|
|
2016-04-06 11:17:15 -04:00
|
|
|
_get_rooms_for_user_where_membership_is_txn = (
|
|
|
|
DataStore._get_rooms_for_user_where_membership_is_txn.__func__
|
|
|
|
)
|
2016-05-13 11:58:14 -04:00
|
|
|
_get_events_around_txn = DataStore._get_events_around_txn.__func__
|
2016-04-06 09:12:51 -04:00
|
|
|
|
2016-07-21 12:37:44 -04:00
|
|
|
get_backfill_events = DataStore.get_backfill_events.__func__
|
|
|
|
_get_backfill_events = DataStore._get_backfill_events.__func__
|
|
|
|
get_missing_events = DataStore.get_missing_events.__func__
|
|
|
|
_get_missing_events = DataStore._get_missing_events.__func__
|
|
|
|
|
2016-07-28 11:36:28 -04:00
|
|
|
get_auth_chain = DataStore.get_auth_chain.__func__
|
|
|
|
get_auth_chain_ids = DataStore.get_auth_chain_ids.__func__
|
|
|
|
_get_auth_chain_ids_txn = DataStore._get_auth_chain_ids_txn.__func__
|
|
|
|
|
2016-09-15 06:47:23 -04:00
|
|
|
get_room_max_stream_ordering = DataStore.get_room_max_stream_ordering.__func__
|
|
|
|
|
|
|
|
get_forward_extremeties_for_room = (
|
2016-09-15 09:27:15 -04:00
|
|
|
DataStore.get_forward_extremeties_for_room.__func__
|
|
|
|
)
|
|
|
|
_get_forward_extremeties_for_room = (
|
|
|
|
EventFederationStore.__dict__["_get_forward_extremeties_for_room"]
|
2016-09-15 06:47:23 -04:00
|
|
|
)
|
|
|
|
|
2016-11-17 10:46:44 -05:00
|
|
|
get_all_new_events_stream = DataStore.get_all_new_events_stream.__func__
|
|
|
|
|
2016-11-21 06:28:37 -05:00
|
|
|
get_federation_out_pos = DataStore.get_federation_out_pos.__func__
|
|
|
|
update_federation_out_pos = DataStore.update_federation_out_pos.__func__
|
|
|
|
|
2018-02-06 05:55:40 -05:00
|
|
|
get_latest_event_ids_and_hashes_in_room = (
|
|
|
|
DataStore.get_latest_event_ids_and_hashes_in_room.__func__
|
|
|
|
)
|
|
|
|
_get_latest_event_ids_and_hashes_in_room = (
|
|
|
|
DataStore._get_latest_event_ids_and_hashes_in_room.__func__
|
|
|
|
)
|
|
|
|
_get_event_reference_hashes_txn = (
|
|
|
|
DataStore._get_event_reference_hashes_txn.__func__
|
|
|
|
)
|
|
|
|
add_event_hashes = (
|
|
|
|
DataStore.add_event_hashes.__func__
|
|
|
|
)
|
|
|
|
get_event_reference_hashes = (
|
|
|
|
SignatureStore.__dict__["get_event_reference_hashes"]
|
|
|
|
)
|
|
|
|
get_event_reference_hash = (
|
|
|
|
SignatureStore.__dict__["get_event_reference_hash"]
|
|
|
|
)
|
|
|
|
|
2016-04-06 09:12:51 -04:00
|
|
|
def stream_positions(self):
|
|
|
|
result = super(SlavedEventStore, self).stream_positions()
|
|
|
|
result["events"] = self._stream_id_gen.get_current_token()
|
2016-04-27 10:38:43 -04:00
|
|
|
result["backfill"] = -self._backfill_id_gen.get_current_token()
|
2016-04-06 09:12:51 -04:00
|
|
|
return result
|
|
|
|
|
2017-03-27 11:32:07 -04: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 09:12:51 -04:00
|
|
|
)
|
2017-03-27 11:32:07 -04: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 09:12:51 -04:00
|
|
|
)
|
2017-03-27 11:32:07 -04:00
|
|
|
return super(SlavedEventStore, self).process_replication_rows(
|
|
|
|
stream_name, token, rows
|
2016-04-06 09:12:51 -04:00
|
|
|
)
|
|
|
|
|
2017-03-17 11:47:51 -04: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 09:12:51 -04:00
|
|
|
|
2017-03-17 11:47:51 -04:00
|
|
|
self.get_latest_event_ids_in_room.invalidate((room_id,))
|
2016-04-07 08:17:56 -04:00
|
|
|
|
2016-04-21 10:25:47 -04:00
|
|
|
self.get_unread_event_push_actions_by_room_for_user.invalidate_many(
|
2017-03-17 11:47:51 -04:00
|
|
|
(room_id,)
|
2016-04-21 10:25:47 -04:00
|
|
|
)
|
|
|
|
|
2016-04-06 09:12:51 -04:00
|
|
|
if not backfilled:
|
|
|
|
self._events_stream_cache.entity_has_changed(
|
2017-03-17 11:47:51 -04:00
|
|
|
room_id, stream_ordering
|
2016-04-06 09:12:51 -04:00
|
|
|
)
|
|
|
|
|
2017-03-17 11:47:51 -04:00
|
|
|
if redacts:
|
|
|
|
self._invalidate_get_event_cache(redacts)
|
2016-04-06 09:12:51 -04:00
|
|
|
|
2017-03-17 11:47:51 -04:00
|
|
|
if etype == EventTypes.Member:
|
2016-06-03 06:19:27 -04:00
|
|
|
self._membership_stream_cache.entity_has_changed(
|
2017-03-17 11:47:51 -04:00
|
|
|
state_key, stream_ordering
|
2016-06-03 06:19:27 -04:00
|
|
|
)
|
2017-03-17 11:47:51 -04:00
|
|
|
self.get_invited_rooms_for_user.invalidate((state_key,))
|