2019-03-25 03:37:08 -06:00
|
|
|
# Copyright 2018 Vector Creations Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import logging
|
2023-10-16 05:35:22 -06:00
|
|
|
from typing import List, Optional, Tuple
|
|
|
|
|
|
|
|
import attr
|
2020-03-02 09:52:15 -07:00
|
|
|
|
2019-03-25 03:37:08 -06:00
|
|
|
from synapse.storage._base import SQLBaseStore
|
2021-11-12 13:24:12 -07:00
|
|
|
from synapse.storage.database import LoggingTransaction
|
|
|
|
from synapse.util.caches.stream_change_cache import StreamChangeCache
|
2019-03-25 03:37:08 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-10-16 05:35:22 -06:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class StateDelta:
|
|
|
|
stream_id: int
|
|
|
|
room_id: str
|
|
|
|
event_type: str
|
|
|
|
state_key: str
|
|
|
|
|
|
|
|
event_id: Optional[str]
|
|
|
|
"""new event_id for this state key. None if the state has been deleted."""
|
|
|
|
|
|
|
|
prev_event_id: Optional[str]
|
|
|
|
"""previous event_id for this state key. None if it's new state."""
|
|
|
|
|
|
|
|
|
2019-03-25 03:37:08 -06:00
|
|
|
class StateDeltasStore(SQLBaseStore):
|
2021-11-12 13:24:12 -07:00
|
|
|
# This class must be mixed in with a child class which provides the following
|
|
|
|
# attribute. TODO: can we get static analysis to enforce this?
|
|
|
|
_curr_state_delta_stream_cache: StreamChangeCache
|
|
|
|
|
2022-06-01 09:02:53 -06:00
|
|
|
async def get_partial_current_state_deltas(
|
2020-08-28 07:37:55 -06:00
|
|
|
self, prev_stream_id: int, max_stream_id: int
|
2023-10-16 05:35:22 -06:00
|
|
|
) -> Tuple[int, List[StateDelta]]:
|
2019-04-26 04:13:16 -06:00
|
|
|
"""Fetch a list of room state changes since the given stream id
|
|
|
|
|
2022-06-01 09:02:53 -06:00
|
|
|
This may be the partial state if we're lazy joining the room.
|
|
|
|
|
2019-04-26 04:13:16 -06:00
|
|
|
Args:
|
2020-08-28 07:37:55 -06:00
|
|
|
prev_stream_id: point to get changes since (exclusive)
|
|
|
|
max_stream_id: the point that we know has been correctly persisted
|
2023-10-16 05:35:22 -06:00
|
|
|
- ie, an upper limit to return changes from.
|
2019-04-26 04:13:16 -06:00
|
|
|
|
|
|
|
Returns:
|
2020-08-28 07:37:55 -06:00
|
|
|
A tuple consisting of:
|
2023-10-16 05:35:22 -06:00
|
|
|
- the stream id which these results go up to
|
|
|
|
- list of current_state_delta_stream rows. If it is empty, we are
|
|
|
|
up to date.
|
2019-04-26 04:13:16 -06:00
|
|
|
"""
|
2019-03-25 03:37:08 -06:00
|
|
|
prev_stream_id = int(prev_stream_id)
|
2019-10-10 04:29:01 -06:00
|
|
|
|
|
|
|
# check we're not going backwards
|
2021-12-08 07:15:14 -07:00
|
|
|
assert (
|
|
|
|
prev_stream_id <= max_stream_id
|
|
|
|
), f"New stream id {max_stream_id} is smaller than prev stream id {prev_stream_id}"
|
2019-10-10 04:29:01 -06:00
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
if not self._curr_state_delta_stream_cache.has_any_entity_changed(
|
|
|
|
prev_stream_id
|
|
|
|
):
|
2019-10-10 04:29:01 -06:00
|
|
|
# if the CSDs haven't changed between prev_stream_id and now, we
|
|
|
|
# know for certain that they haven't changed between prev_stream_id and
|
|
|
|
# max_stream_id.
|
2021-09-23 04:59:07 -06:00
|
|
|
return max_stream_id, []
|
2019-03-25 03:37:08 -06:00
|
|
|
|
2021-11-12 13:24:12 -07:00
|
|
|
def get_current_state_deltas_txn(
|
|
|
|
txn: LoggingTransaction,
|
2023-10-16 05:35:22 -06:00
|
|
|
) -> Tuple[int, List[StateDelta]]:
|
2019-03-25 03:37:08 -06:00
|
|
|
# First we calculate the max stream id that will give us less than
|
|
|
|
# N results.
|
2021-02-12 09:01:48 -07:00
|
|
|
# We arbitrarily limit to 100 stream_id entries to ensure we don't
|
2019-03-25 03:37:08 -06:00
|
|
|
# select toooo many.
|
|
|
|
sql = """
|
|
|
|
SELECT stream_id, count(*)
|
|
|
|
FROM current_state_delta_stream
|
2019-10-10 04:29:01 -06:00
|
|
|
WHERE stream_id > ? AND stream_id <= ?
|
2019-03-25 03:37:08 -06:00
|
|
|
GROUP BY stream_id
|
|
|
|
ORDER BY stream_id ASC
|
|
|
|
LIMIT 100
|
|
|
|
"""
|
2019-10-10 04:29:01 -06:00
|
|
|
txn.execute(sql, (prev_stream_id, max_stream_id))
|
2019-03-25 03:37:08 -06:00
|
|
|
|
|
|
|
total = 0
|
2019-10-10 04:29:01 -06:00
|
|
|
|
|
|
|
for stream_id, count in txn:
|
2019-03-25 03:37:08 -06:00
|
|
|
total += count
|
|
|
|
if total > 100:
|
2021-02-12 09:01:48 -07:00
|
|
|
# We arbitrarily limit to 100 entries to ensure we don't
|
2019-03-25 03:37:08 -06:00
|
|
|
# select toooo many.
|
2019-10-10 04:29:01 -06:00
|
|
|
logger.debug(
|
|
|
|
"Clipping current_state_delta_stream rows to stream_id %i",
|
|
|
|
stream_id,
|
|
|
|
)
|
|
|
|
clipped_stream_id = stream_id
|
2019-03-25 03:37:08 -06:00
|
|
|
break
|
2019-10-10 04:29:01 -06:00
|
|
|
else:
|
|
|
|
# if there's no problem, we may as well go right up to the max_stream_id
|
|
|
|
clipped_stream_id = max_stream_id
|
2019-03-25 03:37:08 -06:00
|
|
|
|
|
|
|
# Now actually get the deltas
|
|
|
|
sql = """
|
|
|
|
SELECT stream_id, room_id, type, state_key, event_id, prev_event_id
|
|
|
|
FROM current_state_delta_stream
|
|
|
|
WHERE ? < stream_id AND stream_id <= ?
|
|
|
|
ORDER BY stream_id ASC
|
|
|
|
"""
|
2019-10-10 04:29:01 -06:00
|
|
|
txn.execute(sql, (prev_stream_id, clipped_stream_id))
|
2023-10-16 05:35:22 -06:00
|
|
|
return clipped_stream_id, [
|
|
|
|
StateDelta(
|
|
|
|
stream_id=row[0],
|
|
|
|
room_id=row[1],
|
|
|
|
event_type=row[2],
|
|
|
|
state_key=row[3],
|
|
|
|
event_id=row[4],
|
|
|
|
prev_event_id=row[5],
|
|
|
|
)
|
|
|
|
for row in txn.fetchall()
|
|
|
|
]
|
2019-03-25 03:37:08 -06:00
|
|
|
|
2020-08-28 07:37:55 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-03-25 03:37:08 -06:00
|
|
|
"get_current_state_deltas", get_current_state_deltas_txn
|
|
|
|
)
|
|
|
|
|
2021-11-12 13:24:12 -07:00
|
|
|
def _get_max_stream_id_in_current_state_deltas_txn(
|
|
|
|
self, txn: LoggingTransaction
|
|
|
|
) -> int:
|
2020-08-05 14:38:57 -06:00
|
|
|
return self.db_pool.simple_select_one_onecol_txn(
|
2019-05-21 10:36:50 -06:00
|
|
|
txn,
|
2019-03-25 03:37:08 -06:00
|
|
|
table="current_state_delta_stream",
|
|
|
|
keyvalues={},
|
|
|
|
retcol="COALESCE(MAX(stream_id), -1)",
|
2019-05-21 10:36:50 -06:00
|
|
|
)
|
|
|
|
|
2021-11-12 13:24:12 -07:00
|
|
|
async def get_max_stream_id_in_current_state_deltas(self) -> int:
|
2020-08-28 07:37:55 -06:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-05-21 10:36:50 -06:00
|
|
|
"get_max_stream_id_in_current_state_deltas",
|
|
|
|
self._get_max_stream_id_in_current_state_deltas_txn,
|
2019-03-25 03:37:08 -06:00
|
|
|
)
|