2014-10-28 10:42:35 -06:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-10-28 10:42:35 -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.
|
2019-09-26 04:47:53 -06:00
|
|
|
|
import itertools
|
2018-07-09 00:09:20 -06:00
|
|
|
|
import logging
|
2020-06-16 06:51:47 -06:00
|
|
|
|
from queue import Empty, PriorityQueue
|
2020-08-21 03:06:45 -06:00
|
|
|
|
from typing import Dict, Iterable, List, Set, Tuple
|
2015-05-14 06:45:48 -06:00
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
|
from synapse.api.errors import StoreError
|
2020-08-21 03:06:45 -06:00
|
|
|
|
from synapse.events import EventBase
|
2020-10-09 05:37:51 -06:00
|
|
|
|
from synapse.metrics.background_process_metrics import wrap_as_background_process
|
2019-10-02 12:07:07 -06:00
|
|
|
|
from synapse.storage._base import SQLBaseStore, make_in_list_sql_clause
|
2020-08-21 03:06:45 -06:00
|
|
|
|
from synapse.storage.database import DatabasePool, LoggingTransaction
|
2020-08-05 14:38:57 -06:00
|
|
|
|
from synapse.storage.databases.main.events_worker import EventsWorkerStore
|
|
|
|
|
from synapse.storage.databases.main.signatures import SignatureWorkerStore
|
2020-08-21 03:06:45 -06:00
|
|
|
|
from synapse.types import Collection
|
2015-08-11 10:59:32 -06:00
|
|
|
|
from synapse.util.caches.descriptors import cached
|
2020-02-19 08:47:11 -07:00
|
|
|
|
from synapse.util.iterutils import batch_iter
|
2014-10-28 10:42:35 -06:00
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
|
class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBaseStore):
|
2020-10-09 05:37:51 -06:00
|
|
|
|
def __init__(self, database: DatabasePool, db_conn, hs):
|
|
|
|
|
super().__init__(database, db_conn, hs)
|
|
|
|
|
|
|
|
|
|
if hs.config.run_background_tasks:
|
|
|
|
|
hs.get_clock().looping_call(
|
|
|
|
|
self._delete_old_forward_extrem_cache, 60 * 60 * 1000
|
|
|
|
|
)
|
|
|
|
|
|
2020-08-21 03:06:45 -06:00
|
|
|
|
async def get_auth_chain(
|
|
|
|
|
self, event_ids: Collection[str], include_given: bool = False
|
|
|
|
|
) -> List[EventBase]:
|
2017-05-24 07:22:41 -06:00
|
|
|
|
"""Get auth events for given event_ids. The events *must* be state events.
|
2014-11-07 08:35:53 -07:00
|
|
|
|
|
2017-05-24 07:22:41 -06:00
|
|
|
|
Args:
|
2020-08-21 03:06:45 -06:00
|
|
|
|
event_ids: state events
|
|
|
|
|
include_given: include the given events in result
|
2017-05-24 07:22:41 -06:00
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list of events
|
|
|
|
|
"""
|
2020-08-18 14:20:49 -06:00
|
|
|
|
event_ids = await self.get_auth_chain_ids(
|
2019-04-03 03:07:29 -06:00
|
|
|
|
event_ids, include_given=include_given
|
2020-08-18 14:20:49 -06:00
|
|
|
|
)
|
|
|
|
|
return await self.get_events_as_list(event_ids)
|
2017-05-24 07:22:41 -06:00
|
|
|
|
|
2020-08-21 03:06:45 -06:00
|
|
|
|
async def get_auth_chain_ids(
|
|
|
|
|
self, event_ids: Collection[str], include_given: bool = False,
|
|
|
|
|
) -> List[str]:
|
2017-05-24 07:22:41 -06:00
|
|
|
|
"""Get auth events for given event_ids. The events *must* be state events.
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-02-19 08:04:47 -07:00
|
|
|
|
event_ids: state events
|
|
|
|
|
include_given: include the given events in result
|
2017-05-24 07:22:41 -06:00
|
|
|
|
|
|
|
|
|
Returns:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
An awaitable which resolve to a list of event_ids
|
2017-05-24 07:22:41 -06:00
|
|
|
|
"""
|
2020-08-21 03:06:45 -06:00
|
|
|
|
return await self.db_pool.runInteraction(
|
2020-02-19 08:04:47 -07:00
|
|
|
|
"get_auth_chain_ids",
|
|
|
|
|
self._get_auth_chain_ids_txn,
|
|
|
|
|
event_ids,
|
|
|
|
|
include_given,
|
2014-11-07 08:35:53 -07:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-21 03:06:45 -06:00
|
|
|
|
def _get_auth_chain_ids_txn(
|
|
|
|
|
self, txn: LoggingTransaction, event_ids: Collection[str], include_given: bool
|
|
|
|
|
) -> List[str]:
|
2020-02-19 02:39:27 -07:00
|
|
|
|
if include_given:
|
|
|
|
|
results = set(event_ids)
|
|
|
|
|
else:
|
|
|
|
|
results = set()
|
|
|
|
|
|
2020-08-21 03:06:45 -06:00
|
|
|
|
base_sql = "SELECT DISTINCT auth_id FROM event_auth WHERE "
|
2014-11-07 03:42:31 -07:00
|
|
|
|
|
2014-12-16 11:57:36 -07:00
|
|
|
|
front = set(event_ids)
|
2014-11-07 03:42:31 -07:00
|
|
|
|
while front:
|
2015-02-12 07:39:31 -07:00
|
|
|
|
new_front = set()
|
2020-02-19 08:47:11 -07:00
|
|
|
|
for chunk in batch_iter(front, 100):
|
2019-10-02 12:07:07 -06:00
|
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
|
txn.database_engine, "event_id", chunk
|
|
|
|
|
)
|
2020-02-19 08:47:11 -07:00
|
|
|
|
txn.execute(base_sql + clause, args)
|
|
|
|
|
new_front.update(r[0] for r in txn)
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
|
|
|
|
new_front -= results
|
|
|
|
|
|
2015-02-12 07:39:31 -07:00
|
|
|
|
front = new_front
|
2014-11-07 04:21:20 -07:00
|
|
|
|
results.update(front)
|
2014-11-07 03:42:31 -07:00
|
|
|
|
|
2014-11-07 08:35:53 -07:00
|
|
|
|
return list(results)
|
2014-11-07 03:42:31 -07:00
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_auth_chain_difference(self, state_sets: List[Set[str]]) -> Set[str]:
|
2020-03-18 10:46:41 -06:00
|
|
|
|
"""Given sets of state events figure out the auth chain difference (as
|
|
|
|
|
per state res v2 algorithm).
|
|
|
|
|
|
|
|
|
|
This equivalent to fetching the full auth chain for each set of state
|
|
|
|
|
and returning the events that don't appear in each and every auth
|
|
|
|
|
chain.
|
|
|
|
|
|
|
|
|
|
Returns:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
The set of the difference in auth chains.
|
2020-03-18 10:46:41 -06:00
|
|
|
|
"""
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
return await self.db_pool.runInteraction(
|
2020-03-18 10:46:41 -06:00
|
|
|
|
"get_auth_chain_difference",
|
|
|
|
|
self._get_auth_chain_difference_txn,
|
|
|
|
|
state_sets,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _get_auth_chain_difference_txn(
|
|
|
|
|
self, txn, state_sets: List[Set[str]]
|
|
|
|
|
) -> Set[str]:
|
|
|
|
|
|
|
|
|
|
# Algorithm Description
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
#
|
|
|
|
|
# The idea here is to basically walk the auth graph of each state set in
|
|
|
|
|
# tandem, keeping track of which auth events are reachable by each state
|
|
|
|
|
# set. If we reach an auth event we've already visited (via a different
|
|
|
|
|
# state set) then we mark that auth event and all ancestors as reachable
|
|
|
|
|
# by the state set. This requires that we keep track of the auth chains
|
|
|
|
|
# in memory.
|
|
|
|
|
#
|
|
|
|
|
# Doing it in a such a way means that we can stop early if all auth
|
|
|
|
|
# events we're currently walking are reachable by all state sets.
|
|
|
|
|
#
|
|
|
|
|
# *Note*: We can't stop walking an event's auth chain if it is reachable
|
|
|
|
|
# by all state sets. This is because other auth chains we're walking
|
|
|
|
|
# might be reachable only via the original auth chain. For example,
|
|
|
|
|
# given the following auth chain:
|
|
|
|
|
#
|
|
|
|
|
# A -> C -> D -> E
|
|
|
|
|
# / /
|
|
|
|
|
# B -´---------´
|
|
|
|
|
#
|
|
|
|
|
# and state sets {A} and {B} then walking the auth chains of A and B
|
|
|
|
|
# would immediately show that C is reachable by both. However, if we
|
|
|
|
|
# stopped at C then we'd only reach E via the auth chain of B and so E
|
|
|
|
|
# would errornously get included in the returned difference.
|
|
|
|
|
#
|
|
|
|
|
# The other thing that we do is limit the number of auth chains we walk
|
|
|
|
|
# at once, due to practical limits (i.e. we can only query the database
|
|
|
|
|
# with a limited set of parameters). We pick the auth chains we walk
|
|
|
|
|
# each iteration based on their depth, in the hope that events with a
|
|
|
|
|
# lower depth are likely reachable by those with higher depths.
|
|
|
|
|
#
|
|
|
|
|
# We could use any ordering that we believe would give a rough
|
|
|
|
|
# topological ordering, e.g. origin server timestamp. If the ordering
|
|
|
|
|
# chosen is not topological then the algorithm still produces the right
|
|
|
|
|
# result, but perhaps a bit more inefficiently. This is why it is safe
|
|
|
|
|
# to use "depth" here.
|
|
|
|
|
|
|
|
|
|
initial_events = set(state_sets[0]).union(*state_sets[1:])
|
|
|
|
|
|
|
|
|
|
# Dict from events in auth chains to which sets *cannot* reach them.
|
|
|
|
|
# I.e. if the set is empty then all sets can reach the event.
|
|
|
|
|
event_to_missing_sets = {
|
|
|
|
|
event_id: {i for i, a in enumerate(state_sets) if event_id not in a}
|
|
|
|
|
for event_id in initial_events
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 03:16:35 -06:00
|
|
|
|
# The sorted list of events whose auth chains we should walk.
|
|
|
|
|
search = [] # type: List[Tuple[int, str]]
|
|
|
|
|
|
2020-03-18 10:46:41 -06:00
|
|
|
|
# We need to get the depth of the initial events for sorting purposes.
|
|
|
|
|
sql = """
|
|
|
|
|
SELECT depth, event_id FROM events
|
|
|
|
|
WHERE %s
|
|
|
|
|
"""
|
2020-04-15 03:16:35 -06:00
|
|
|
|
# the list can be huge, so let's avoid looking them all up in one massive
|
|
|
|
|
# query.
|
|
|
|
|
for batch in batch_iter(initial_events, 1000):
|
|
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
|
txn.database_engine, "event_id", batch
|
|
|
|
|
)
|
|
|
|
|
txn.execute(sql % (clause,), args)
|
|
|
|
|
|
|
|
|
|
# I think building a temporary list with fetchall is more efficient than
|
|
|
|
|
# just `search.extend(txn)`, but this is unconfirmed
|
|
|
|
|
search.extend(txn.fetchall())
|
|
|
|
|
|
|
|
|
|
# sort by depth
|
|
|
|
|
search.sort()
|
2020-03-18 10:46:41 -06:00
|
|
|
|
|
|
|
|
|
# Map from event to its auth events
|
|
|
|
|
event_to_auth_events = {} # type: Dict[str, Set[str]]
|
|
|
|
|
|
|
|
|
|
base_sql = """
|
|
|
|
|
SELECT a.event_id, auth_id, depth
|
|
|
|
|
FROM event_auth AS a
|
|
|
|
|
INNER JOIN events AS e ON (e.event_id = a.auth_id)
|
|
|
|
|
WHERE
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
while search:
|
|
|
|
|
# Check whether all our current walks are reachable by all state
|
|
|
|
|
# sets. If so we can bail.
|
|
|
|
|
if all(not event_to_missing_sets[eid] for _, eid in search):
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Fetch the auth events and their depths of the N last events we're
|
|
|
|
|
# currently walking
|
|
|
|
|
search, chunk = search[:-100], search[-100:]
|
|
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
|
txn.database_engine, "a.event_id", [e_id for _, e_id in chunk]
|
|
|
|
|
)
|
|
|
|
|
txn.execute(base_sql + clause, args)
|
|
|
|
|
|
|
|
|
|
for event_id, auth_event_id, auth_event_depth in txn:
|
|
|
|
|
event_to_auth_events.setdefault(event_id, set()).add(auth_event_id)
|
|
|
|
|
|
|
|
|
|
sets = event_to_missing_sets.get(auth_event_id)
|
|
|
|
|
if sets is None:
|
|
|
|
|
# First time we're seeing this event, so we add it to the
|
|
|
|
|
# queue of things to fetch.
|
|
|
|
|
search.append((auth_event_depth, auth_event_id))
|
|
|
|
|
|
|
|
|
|
# Assume that this event is unreachable from any of the
|
|
|
|
|
# state sets until proven otherwise
|
|
|
|
|
sets = event_to_missing_sets[auth_event_id] = set(
|
|
|
|
|
range(len(state_sets))
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
# We've previously seen this event, so look up its auth
|
|
|
|
|
# events and recursively mark all ancestors as reachable
|
|
|
|
|
# by the current event's state set.
|
|
|
|
|
a_ids = event_to_auth_events.get(auth_event_id)
|
|
|
|
|
while a_ids:
|
|
|
|
|
new_aids = set()
|
|
|
|
|
for a_id in a_ids:
|
|
|
|
|
event_to_missing_sets[a_id].intersection_update(
|
|
|
|
|
event_to_missing_sets[event_id]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
b = event_to_auth_events.get(a_id)
|
|
|
|
|
if b:
|
|
|
|
|
new_aids.update(b)
|
|
|
|
|
|
|
|
|
|
a_ids = new_aids
|
|
|
|
|
|
|
|
|
|
# Mark that the auth event is reachable by the approriate sets.
|
|
|
|
|
sets.intersection_update(event_to_missing_sets[event_id])
|
|
|
|
|
|
|
|
|
|
search.sort()
|
|
|
|
|
|
|
|
|
|
# Return all events where not all sets can reach them.
|
|
|
|
|
return {eid for eid, n in event_to_missing_sets.items() if n}
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_oldest_events_with_depth_in_room(self, room_id):
|
|
|
|
|
return await self.db_pool.runInteraction(
|
2015-05-11 11:01:31 -06:00
|
|
|
|
"get_oldest_events_with_depth_in_room",
|
|
|
|
|
self.get_oldest_events_with_depth_in_room_txn,
|
|
|
|
|
room_id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_oldest_events_with_depth_in_room_txn(self, txn, room_id):
|
|
|
|
|
sql = (
|
|
|
|
|
"SELECT b.event_id, MAX(e.depth) FROM events as e"
|
|
|
|
|
" INNER JOIN event_edges as g"
|
2018-07-26 06:19:08 -06:00
|
|
|
|
" ON g.event_id = e.event_id"
|
2015-05-11 11:01:31 -06:00
|
|
|
|
" INNER JOIN event_backward_extremities as b"
|
2018-07-26 06:19:08 -06:00
|
|
|
|
" ON g.prev_event_id = b.event_id"
|
2015-05-11 11:01:31 -06:00
|
|
|
|
" WHERE b.room_id = ? AND g.is_state is ?"
|
|
|
|
|
" GROUP BY b.event_id"
|
|
|
|
|
)
|
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
|
txn.execute(sql, (room_id, False))
|
2015-05-11 11:01:31 -06:00
|
|
|
|
|
2017-03-23 11:53:49 -06:00
|
|
|
|
return dict(txn)
|
2015-05-11 11:01:31 -06:00
|
|
|
|
|
2020-08-11 15:21:13 -06:00
|
|
|
|
async def get_max_depth_of(self, event_ids: List[str]) -> int:
|
2019-01-25 10:19:31 -07:00
|
|
|
|
"""Returns the max depth of a set of event IDs
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-08-11 15:21:13 -06:00
|
|
|
|
event_ids: The event IDs to calculate the max depth of.
|
2019-01-25 10:19:31 -07:00
|
|
|
|
"""
|
2020-08-11 15:21:13 -06:00
|
|
|
|
rows = await self.db_pool.simple_select_many_batch(
|
2019-01-25 10:19:31 -07:00
|
|
|
|
table="events",
|
|
|
|
|
column="event_id",
|
|
|
|
|
iterable=event_ids,
|
|
|
|
|
retcols=("depth",),
|
|
|
|
|
desc="get_max_depth_of",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not rows:
|
2019-07-23 07:00:55 -06:00
|
|
|
|
return 0
|
2019-01-25 10:19:31 -07:00
|
|
|
|
else:
|
2019-07-23 07:00:55 -06:00
|
|
|
|
return max(row["depth"] for row in rows)
|
2019-01-25 10:19:31 -07:00
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_prev_events_for_room(self, room_id: str) -> List[str]:
|
2020-01-03 09:09:24 -07:00
|
|
|
|
"""
|
|
|
|
|
Gets a subset of the current forward extremities in the given room.
|
|
|
|
|
|
|
|
|
|
Limits the result to 10 extremities, so that we can avoid creating
|
|
|
|
|
events which refer to hundreds of prev_events.
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
room_id: room_id
|
2020-01-03 09:09:24 -07:00
|
|
|
|
|
|
|
|
|
Returns:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
The event ids of the forward extremities.
|
2020-01-03 09:09:24 -07:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
return await self.db_pool.runInteraction(
|
2020-01-03 09:09:24 -07:00
|
|
|
|
"get_prev_events_for_room", self._get_prev_events_for_room_txn, room_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _get_prev_events_for_room_txn(self, txn, room_id: str):
|
|
|
|
|
# we just use the 10 newest events. Older events will become
|
|
|
|
|
# prev_events of future events.
|
|
|
|
|
|
|
|
|
|
sql = """
|
|
|
|
|
SELECT e.event_id FROM event_forward_extremities AS f
|
|
|
|
|
INNER JOIN events AS e USING (event_id)
|
|
|
|
|
WHERE f.room_id = ?
|
|
|
|
|
ORDER BY e.depth DESC
|
|
|
|
|
LIMIT 10
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
txn.execute(sql, (room_id,))
|
|
|
|
|
|
|
|
|
|
return [row[0] for row in txn]
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_rooms_with_many_extremities(
|
|
|
|
|
self, min_count: int, limit: int, room_id_filter: Iterable[str]
|
|
|
|
|
) -> List[str]:
|
2019-06-17 11:04:42 -06:00
|
|
|
|
"""Get the top rooms with at least N extremities.
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
min_count: The minimum number of extremities
|
|
|
|
|
limit: The maximum number of rooms to return.
|
|
|
|
|
room_id_filter: room_ids to exclude from the results
|
2019-06-17 11:04:42 -06:00
|
|
|
|
|
|
|
|
|
Returns:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
At most `limit` room IDs that have at least `min_count` extremities,
|
|
|
|
|
sorted by extremity count.
|
2019-06-17 11:04:42 -06:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def _get_rooms_with_many_extremities_txn(txn):
|
2019-09-26 04:47:53 -06:00
|
|
|
|
where_clause = "1=1"
|
|
|
|
|
if room_id_filter:
|
|
|
|
|
where_clause = "room_id NOT IN (%s)" % (
|
|
|
|
|
",".join("?" for _ in room_id_filter),
|
|
|
|
|
)
|
|
|
|
|
|
2019-06-17 11:04:42 -06:00
|
|
|
|
sql = """
|
|
|
|
|
SELECT room_id FROM event_forward_extremities
|
2019-09-26 04:47:53 -06:00
|
|
|
|
WHERE %s
|
2019-06-17 11:04:42 -06:00
|
|
|
|
GROUP BY room_id
|
|
|
|
|
HAVING count(*) > ?
|
|
|
|
|
ORDER BY count(*) DESC
|
|
|
|
|
LIMIT ?
|
2019-09-26 04:47:53 -06:00
|
|
|
|
""" % (
|
|
|
|
|
where_clause,
|
|
|
|
|
)
|
2019-06-17 11:04:42 -06:00
|
|
|
|
|
2019-09-26 04:47:53 -06:00
|
|
|
|
query_args = list(itertools.chain(room_id_filter, [min_count, limit]))
|
|
|
|
|
txn.execute(sql, query_args)
|
2019-06-17 11:04:42 -06:00
|
|
|
|
return [room_id for room_id, in txn]
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
return await self.db_pool.runInteraction(
|
2019-06-20 03:32:02 -06:00
|
|
|
|
"get_rooms_with_many_extremities", _get_rooms_with_many_extremities_txn
|
2019-06-17 11:04:42 -06:00
|
|
|
|
)
|
|
|
|
|
|
2017-02-01 03:39:41 -07:00
|
|
|
|
@cached(max_entries=5000, iterable=True)
|
2020-08-27 05:08:38 -06:00
|
|
|
|
async def get_latest_event_ids_in_room(self, room_id: str) -> List[str]:
|
|
|
|
|
return await self.db_pool.simple_select_onecol(
|
2015-05-01 03:17:19 -06:00
|
|
|
|
table="event_forward_extremities",
|
2019-04-03 03:07:29 -06:00
|
|
|
|
keyvalues={"room_id": room_id},
|
2015-05-01 03:17:19 -06:00
|
|
|
|
retcol="event_id",
|
2015-05-05 03:24:10 -06:00
|
|
|
|
desc="get_latest_event_ids_in_room",
|
2015-05-01 03:17:19 -06:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_min_depth(self, room_id: str) -> int:
|
|
|
|
|
"""For the given room, get the minimum depth we have seen for it.
|
2014-11-12 08:02:31 -07:00
|
|
|
|
"""
|
2020-08-28 05:54:27 -06:00
|
|
|
|
return await self.db_pool.runInteraction(
|
2019-04-03 03:07:29 -06:00
|
|
|
|
"get_min_depth", self._get_min_depth_interaction, room_id
|
2014-10-31 04:47:34 -06:00
|
|
|
|
)
|
|
|
|
|
|
2014-10-28 10:42:35 -06:00
|
|
|
|
def _get_min_depth_interaction(self, txn, room_id):
|
2020-08-05 14:38:57 -06:00
|
|
|
|
min_depth = self.db_pool.simple_select_one_onecol_txn(
|
2014-10-28 10:42:35 -06:00
|
|
|
|
txn,
|
|
|
|
|
table="room_depth",
|
2014-11-10 06:46:44 -07:00
|
|
|
|
keyvalues={"room_id": room_id},
|
2014-10-28 10:42:35 -06:00
|
|
|
|
retcol="min_depth",
|
|
|
|
|
allow_none=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return int(min_depth) if min_depth is not None else None
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_forward_extremeties_for_room(
|
|
|
|
|
self, room_id: str, stream_ordering: int
|
|
|
|
|
) -> List[str]:
|
2017-02-14 06:59:50 -07:00
|
|
|
|
"""For a given room_id and stream_ordering, return the forward
|
|
|
|
|
extremeties of the room at that point in "time".
|
|
|
|
|
|
|
|
|
|
Throws a StoreError if we have since purged the index for
|
|
|
|
|
stream_orderings from that point.
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
room_id:
|
|
|
|
|
stream_ordering:
|
2017-02-14 06:59:50 -07:00
|
|
|
|
|
|
|
|
|
Returns:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
A list of event_ids
|
2017-02-14 06:59:50 -07:00
|
|
|
|
"""
|
2016-09-15 07:27:15 -06:00
|
|
|
|
# We want to make the cache more effective, so we clamp to the last
|
|
|
|
|
# change before the given ordering.
|
2016-09-15 08:12:07 -06:00
|
|
|
|
last_change = self._events_stream_cache.get_max_pos_of_last_change(room_id)
|
2016-09-15 10:34:59 -06:00
|
|
|
|
|
|
|
|
|
# We don't always have a full stream_to_exterm_id table, e.g. after
|
|
|
|
|
# the upgrade that introduced it, so we make sure we never ask for a
|
2017-02-14 06:59:50 -07:00
|
|
|
|
# stream_ordering from before a restart
|
2016-09-15 10:34:59 -06:00
|
|
|
|
last_change = max(self._stream_order_on_start, last_change)
|
|
|
|
|
|
2017-02-14 06:59:50 -07:00
|
|
|
|
# provided the last_change is recent enough, we now clamp the requested
|
|
|
|
|
# stream_ordering to it.
|
2016-09-15 10:34:59 -06:00
|
|
|
|
if last_change > self.stream_ordering_month_ago:
|
|
|
|
|
stream_ordering = min(last_change, stream_ordering)
|
2016-09-15 07:27:15 -06:00
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
return await self._get_forward_extremeties_for_room(room_id, stream_ordering)
|
2016-09-15 07:27:15 -06:00
|
|
|
|
|
|
|
|
|
@cached(max_entries=5000, num_args=2)
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def _get_forward_extremeties_for_room(self, room_id, stream_ordering):
|
2016-09-14 09:09:32 -06:00
|
|
|
|
"""For a given room_id and stream_ordering, return the forward
|
|
|
|
|
extremeties of the room at that point in "time".
|
|
|
|
|
|
|
|
|
|
Throws a StoreError if we have since purged the index for
|
|
|
|
|
stream_orderings from that point.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if stream_ordering <= self.stream_ordering_month_ago:
|
2020-09-14 03:16:41 -06:00
|
|
|
|
raise StoreError(400, "stream_ordering too old %s" % (stream_ordering,))
|
2016-09-14 09:09:32 -06:00
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
|
sql = """
|
2016-09-14 09:09:32 -06:00
|
|
|
|
SELECT event_id FROM stream_ordering_to_exterm
|
|
|
|
|
INNER JOIN (
|
|
|
|
|
SELECT room_id, MAX(stream_ordering) AS stream_ordering
|
|
|
|
|
FROM stream_ordering_to_exterm
|
2016-09-16 03:19:32 -06:00
|
|
|
|
WHERE stream_ordering <= ? GROUP BY room_id
|
2016-09-14 09:09:32 -06:00
|
|
|
|
) AS rms USING (room_id, stream_ordering)
|
|
|
|
|
WHERE room_id = ?
|
2019-04-03 03:07:29 -06:00
|
|
|
|
"""
|
2016-09-14 09:09:32 -06:00
|
|
|
|
|
|
|
|
|
def get_forward_extremeties_for_room_txn(txn):
|
2016-09-14 10:01:02 -06:00
|
|
|
|
txn.execute(sql, (stream_ordering, room_id))
|
2017-03-23 11:53:49 -06:00
|
|
|
|
return [event_id for event_id, in txn]
|
2016-09-14 09:09:32 -06:00
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
return await self.db_pool.runInteraction(
|
2019-04-03 03:07:29 -06:00
|
|
|
|
"get_forward_extremeties_for_room", get_forward_extremeties_for_room_txn
|
2016-09-14 09:09:32 -06:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def get_backfill_events(self, room_id: str, event_list: list, limit: int):
|
2014-11-12 08:02:31 -07:00
|
|
|
|
"""Get a list of Events for a given topic that occurred before (and
|
|
|
|
|
including) the events in event_list. Return a list of max size `limit`
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
|
|
|
|
Args:
|
2020-08-28 05:54:27 -06:00
|
|
|
|
room_id
|
|
|
|
|
event_list
|
|
|
|
|
limit
|
2014-10-31 03:59:02 -06:00
|
|
|
|
"""
|
2020-08-18 14:20:49 -06:00
|
|
|
|
event_ids = await self.db_pool.runInteraction(
|
|
|
|
|
"get_backfill_events",
|
|
|
|
|
self._get_backfill_events,
|
|
|
|
|
room_id,
|
|
|
|
|
event_list,
|
|
|
|
|
limit,
|
2015-05-21 08:57:35 -06:00
|
|
|
|
)
|
2020-08-18 14:20:49 -06:00
|
|
|
|
events = await self.get_events_as_list(event_ids)
|
|
|
|
|
return sorted(events, key=lambda e: -e.depth)
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
|
|
|
|
def _get_backfill_events(self, txn, room_id, event_list, limit):
|
2019-10-24 11:43:13 -06:00
|
|
|
|
logger.debug("_get_backfill_events: %s, %r, %s", room_id, event_list, limit)
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
2015-05-21 08:44:05 -06:00
|
|
|
|
event_results = set()
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
2015-05-20 05:57:00 -06:00
|
|
|
|
# We want to make sure that we do a breadth-first, "depth" ordered
|
|
|
|
|
# search.
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
|
|
|
|
query = (
|
2015-05-20 05:57:00 -06:00
|
|
|
|
"SELECT depth, prev_event_id FROM event_edges"
|
|
|
|
|
" INNER JOIN events"
|
|
|
|
|
" ON prev_event_id = events.event_id"
|
2018-07-26 06:19:08 -06:00
|
|
|
|
" WHERE event_edges.event_id = ?"
|
2015-05-21 08:52:29 -06:00
|
|
|
|
" AND event_edges.is_state = ?"
|
2015-05-20 05:57:00 -06:00
|
|
|
|
" LIMIT ?"
|
2014-10-31 03:59:02 -06:00
|
|
|
|
)
|
|
|
|
|
|
2015-05-20 05:57:00 -06:00
|
|
|
|
queue = PriorityQueue()
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
2015-05-20 05:57:00 -06:00
|
|
|
|
for event_id in event_list:
|
2020-08-05 14:38:57 -06:00
|
|
|
|
depth = self.db_pool.simple_select_one_onecol_txn(
|
2015-05-21 08:46:07 -06:00
|
|
|
|
txn,
|
|
|
|
|
table="events",
|
2019-04-03 03:07:29 -06:00
|
|
|
|
keyvalues={"event_id": event_id, "room_id": room_id},
|
2015-07-06 02:31:40 -06:00
|
|
|
|
retcol="depth",
|
|
|
|
|
allow_none=True,
|
2015-05-21 08:46:07 -06:00
|
|
|
|
)
|
|
|
|
|
|
2015-07-06 02:31:40 -06:00
|
|
|
|
if depth:
|
|
|
|
|
queue.put((-depth, event_id))
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
2015-05-20 05:57:00 -06:00
|
|
|
|
while not queue.empty() and len(event_results) < limit:
|
2015-05-21 08:37:43 -06:00
|
|
|
|
try:
|
|
|
|
|
_, event_id = queue.get_nowait()
|
|
|
|
|
except Empty:
|
|
|
|
|
break
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
2015-05-21 08:48:56 -06:00
|
|
|
|
if event_id in event_results:
|
|
|
|
|
continue
|
|
|
|
|
|
2015-05-20 05:57:00 -06:00
|
|
|
|
event_results.add(event_id)
|
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
|
txn.execute(query, (event_id, False, limit - len(event_results)))
|
2015-05-20 05:57:00 -06:00
|
|
|
|
|
2017-03-23 11:53:49 -06:00
|
|
|
|
for row in txn:
|
2015-05-21 08:40:22 -06:00
|
|
|
|
if row[1] not in event_results:
|
|
|
|
|
queue.put((-row[0], row[1]))
|
2014-10-31 03:59:02 -06:00
|
|
|
|
|
2015-05-14 06:45:48 -06:00
|
|
|
|
return event_results
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
2020-08-11 15:21:13 -06:00
|
|
|
|
async def get_missing_events(self, room_id, earliest_events, latest_events, limit):
|
|
|
|
|
ids = await self.db_pool.runInteraction(
|
2015-02-19 10:24:14 -07:00
|
|
|
|
"get_missing_events",
|
|
|
|
|
self._get_missing_events,
|
2019-04-03 03:07:29 -06:00
|
|
|
|
room_id,
|
|
|
|
|
earliest_events,
|
|
|
|
|
latest_events,
|
|
|
|
|
limit,
|
2015-02-19 10:24:14 -07:00
|
|
|
|
)
|
2020-08-18 14:20:49 -06:00
|
|
|
|
return await self.get_events_as_list(ids)
|
2015-05-14 06:45:48 -06:00
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
|
def _get_missing_events(self, txn, room_id, earliest_events, latest_events, limit):
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
2018-10-16 13:37:16 -06:00
|
|
|
|
seen_events = set(earliest_events)
|
|
|
|
|
front = set(latest_events) - seen_events
|
|
|
|
|
event_results = []
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
|
|
|
|
query = (
|
|
|
|
|
"SELECT prev_event_id FROM event_edges "
|
2018-10-16 13:37:16 -06:00
|
|
|
|
"WHERE room_id = ? AND event_id = ? AND is_state = ? "
|
2015-02-19 10:24:14 -07:00
|
|
|
|
"LIMIT ?"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
while front and len(event_results) < limit:
|
|
|
|
|
new_front = set()
|
|
|
|
|
for event_id in front:
|
|
|
|
|
txn.execute(
|
2019-04-03 03:07:29 -06:00
|
|
|
|
query, (room_id, event_id, False, limit - len(event_results))
|
2015-02-19 10:24:14 -07:00
|
|
|
|
)
|
|
|
|
|
|
2020-02-21 05:15:07 -07:00
|
|
|
|
new_results = {t[0] for t in txn} - seen_events
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
2018-10-16 13:37:16 -06:00
|
|
|
|
new_front |= new_results
|
|
|
|
|
seen_events |= new_results
|
|
|
|
|
event_results.extend(new_results)
|
2015-02-19 10:24:14 -07:00
|
|
|
|
|
|
|
|
|
front = new_front
|
|
|
|
|
|
2018-10-16 13:37:16 -06:00
|
|
|
|
# we built the list working backwards from latest_events; we now need to
|
|
|
|
|
# reverse it so that the events are approximately chronological.
|
|
|
|
|
event_results.reverse()
|
2015-05-14 06:45:48 -06:00
|
|
|
|
return event_results
|
2015-03-18 05:19:47 -06:00
|
|
|
|
|
2020-08-11 15:21:13 -06:00
|
|
|
|
async def get_successor_events(self, event_ids: Iterable[str]) -> List[str]:
|
2019-02-20 09:54:35 -07:00
|
|
|
|
"""Fetch all events that have the given events as a prev event
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-08-11 15:21:13 -06:00
|
|
|
|
event_ids: The events to use as the previous events.
|
2019-02-20 09:54:35 -07:00
|
|
|
|
"""
|
2020-08-11 15:21:13 -06:00
|
|
|
|
rows = await self.db_pool.simple_select_many_batch(
|
2019-02-20 09:54:35 -07:00
|
|
|
|
table="event_edges",
|
|
|
|
|
column="prev_event_id",
|
|
|
|
|
iterable=event_ids,
|
|
|
|
|
retcols=("event_id",),
|
2019-04-03 03:07:29 -06:00
|
|
|
|
desc="get_successor_events",
|
2019-02-20 09:54:35 -07:00
|
|
|
|
)
|
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
|
return [row["event_id"] for row in rows]
|
2019-02-20 09:54:35 -07:00
|
|
|
|
|
2020-10-09 05:37:51 -06:00
|
|
|
|
@wrap_as_background_process("delete_old_forward_extrem_cache")
|
|
|
|
|
async def _delete_old_forward_extrem_cache(self) -> None:
|
|
|
|
|
def _delete_old_forward_extrem_cache_txn(txn):
|
|
|
|
|
# Delete entries older than a month, while making sure we don't delete
|
|
|
|
|
# the only entries for a room.
|
|
|
|
|
sql = """
|
|
|
|
|
DELETE FROM stream_ordering_to_exterm
|
|
|
|
|
WHERE
|
|
|
|
|
room_id IN (
|
|
|
|
|
SELECT room_id
|
|
|
|
|
FROM stream_ordering_to_exterm
|
|
|
|
|
WHERE stream_ordering > ?
|
|
|
|
|
) AND stream_ordering < ?
|
|
|
|
|
"""
|
|
|
|
|
txn.execute(
|
|
|
|
|
sql, (self.stream_ordering_month_ago, self.stream_ordering_month_ago)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await self.db_pool.runInteraction(
|
|
|
|
|
"_delete_old_forward_extrem_cache", _delete_old_forward_extrem_cache_txn,
|
|
|
|
|
)
|
|
|
|
|
|
2018-03-01 07:16:02 -07:00
|
|
|
|
|
|
|
|
|
class EventFederationStore(EventFederationWorkerStore):
|
|
|
|
|
""" Responsible for storing and serving up the various graphs associated
|
|
|
|
|
with an event. Including the main event graph and the auth chains for an
|
|
|
|
|
event.
|
|
|
|
|
|
|
|
|
|
Also has methods for getting the front (latest) and back (oldest) edges
|
|
|
|
|
of the event graphs. These are used to generate the parents for new events
|
|
|
|
|
and backfilling from another server respectively.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
EVENT_AUTH_STATE_ONLY = "event_auth_state_only"
|
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
|
def __init__(self, database: DatabasePool, db_conn, hs):
|
2020-09-18 07:56:44 -06:00
|
|
|
|
super().__init__(database, db_conn, hs)
|
2018-03-01 07:16:02 -07:00
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-04-03 03:07:29 -06:00
|
|
|
|
self.EVENT_AUTH_STATE_ONLY, self._background_delete_non_state_event_auth
|
2018-03-01 07:16:02 -07:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-28 05:54:27 -06:00
|
|
|
|
async def clean_room_for_join(self, room_id):
|
|
|
|
|
return await self.db_pool.runInteraction(
|
2019-04-03 03:07:29 -06:00
|
|
|
|
"clean_room_for_join", self._clean_room_for_join_txn, room_id
|
2015-03-18 05:19:47 -06:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _clean_room_for_join_txn(self, txn, room_id):
|
|
|
|
|
query = "DELETE FROM event_forward_extremities WHERE room_id = ?"
|
|
|
|
|
|
|
|
|
|
txn.execute(query, (room_id,))
|
2015-08-07 04:52:21 -06:00
|
|
|
|
txn.call_after(self.get_latest_event_ids_in_room.invalidate, (room_id,))
|
2017-05-24 07:58:13 -06:00
|
|
|
|
|
2020-08-11 15:21:13 -06:00
|
|
|
|
async def _background_delete_non_state_event_auth(self, progress, batch_size):
|
2017-05-24 07:58:13 -06:00
|
|
|
|
def delete_event_auth(txn):
|
|
|
|
|
target_min_stream_id = progress.get("target_min_stream_id_inclusive")
|
|
|
|
|
max_stream_id = progress.get("max_stream_id_exclusive")
|
|
|
|
|
|
|
|
|
|
if not target_min_stream_id or not max_stream_id:
|
|
|
|
|
txn.execute("SELECT COALESCE(MIN(stream_ordering), 0) FROM events")
|
|
|
|
|
rows = txn.fetchall()
|
|
|
|
|
target_min_stream_id = rows[0][0]
|
|
|
|
|
|
|
|
|
|
txn.execute("SELECT COALESCE(MAX(stream_ordering), 0) FROM events")
|
|
|
|
|
rows = txn.fetchall()
|
|
|
|
|
max_stream_id = rows[0][0]
|
|
|
|
|
|
|
|
|
|
min_stream_id = max_stream_id - batch_size
|
|
|
|
|
|
|
|
|
|
sql = """
|
|
|
|
|
DELETE FROM event_auth
|
|
|
|
|
WHERE event_id IN (
|
|
|
|
|
SELECT event_id FROM events
|
|
|
|
|
LEFT JOIN state_events USING (room_id, event_id)
|
|
|
|
|
WHERE ? <= stream_ordering AND stream_ordering < ?
|
|
|
|
|
AND state_key IS null
|
|
|
|
|
)
|
|
|
|
|
"""
|
|
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
|
txn.execute(sql, (min_stream_id, max_stream_id))
|
2017-05-24 07:58:13 -06:00
|
|
|
|
|
|
|
|
|
new_progress = {
|
|
|
|
|
"target_min_stream_id_inclusive": target_min_stream_id,
|
|
|
|
|
"max_stream_id_exclusive": min_stream_id,
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
|
self.db_pool.updates._background_update_progress_txn(
|
2017-05-24 07:58:13 -06:00
|
|
|
|
txn, self.EVENT_AUTH_STATE_ONLY, new_progress
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return min_stream_id >= target_min_stream_id
|
|
|
|
|
|
2020-08-11 15:21:13 -06:00
|
|
|
|
result = await self.db_pool.runInteraction(
|
2017-05-24 07:58:13 -06:00
|
|
|
|
self.EVENT_AUTH_STATE_ONLY, delete_event_auth
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not result:
|
2020-08-11 15:21:13 -06:00
|
|
|
|
await self.db_pool.updates._end_background_update(
|
2020-08-05 14:38:57 -06:00
|
|
|
|
self.EVENT_AUTH_STATE_ONLY
|
|
|
|
|
)
|
2017-05-24 07:58:13 -06:00
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
|
return batch_size
|