2016-01-28 09:37:41 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2016 OpenMarket 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.
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
2020-05-11 11:45:23 -06:00
|
|
|
import math
|
2020-05-05 10:07:59 -06:00
|
|
|
from typing import Dict, FrozenSet, List, Mapping, Optional, Set, Union
|
2016-01-28 09:37:41 -07:00
|
|
|
|
2018-10-19 18:16:55 -06:00
|
|
|
from six import integer_types
|
|
|
|
|
2018-06-05 08:13:57 -06:00
|
|
|
from sortedcontainers import SortedDict
|
2016-01-28 09:37:41 -07:00
|
|
|
|
2020-05-05 10:07:59 -06:00
|
|
|
from synapse.types import Collection
|
2018-07-09 00:09:20 -06:00
|
|
|
from synapse.util import caches
|
2016-01-28 09:37:41 -07:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
# for now, assume all entities in the cache are strings
|
|
|
|
EntityType = str
|
2016-01-28 09:37:41 -07:00
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
|
|
|
|
class StreamChangeCache:
|
2016-01-28 09:37:41 -07:00
|
|
|
"""Keeps track of the stream positions of the latest change in a set of entities.
|
|
|
|
|
|
|
|
Typically the entity will be a room or user id.
|
|
|
|
|
|
|
|
Given a list of entities and a stream position, it will give a subset of
|
|
|
|
entities that may have changed since that position. If position key is too
|
|
|
|
old then the cache will simply return all given entities.
|
|
|
|
"""
|
2018-06-05 08:13:57 -06:00
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
current_stream_pos: int,
|
|
|
|
max_size=10000,
|
|
|
|
prefilled_cache: Optional[Mapping[EntityType, int]] = None,
|
|
|
|
):
|
2020-05-11 11:45:23 -06:00
|
|
|
self._original_max_size = max_size
|
|
|
|
self._max_size = math.floor(max_size)
|
2020-04-22 06:45:40 -06:00
|
|
|
self._entity_to_key = {} # type: Dict[EntityType, int]
|
|
|
|
|
|
|
|
# map from stream id to the a set of entities which changed at that stream id.
|
|
|
|
self._cache = SortedDict() # type: SortedDict[int, Set[EntityType]]
|
|
|
|
|
|
|
|
# the earliest stream_pos for which we can reliably answer
|
|
|
|
# get_all_entities_changed. In other words, one less than the earliest
|
|
|
|
# stream_pos for which we know _cache is valid.
|
|
|
|
#
|
2016-01-28 09:37:41 -07:00
|
|
|
self._earliest_known_stream_pos = current_stream_pos
|
|
|
|
self.name = name
|
2020-05-11 11:45:23 -06:00
|
|
|
self.metrics = caches.register_cache(
|
|
|
|
"cache", self.name, self._cache, resize_callback=self.set_cache_factor
|
|
|
|
)
|
2016-01-28 09:37:41 -07:00
|
|
|
|
2018-06-05 08:13:57 -06:00
|
|
|
if prefilled_cache:
|
|
|
|
for entity, stream_pos in prefilled_cache.items():
|
|
|
|
self.entity_has_changed(entity, stream_pos)
|
2016-01-29 07:37:59 -07:00
|
|
|
|
2020-05-11 11:45:23 -06:00
|
|
|
def set_cache_factor(self, factor: float) -> bool:
|
|
|
|
"""
|
|
|
|
Set the cache factor for this individual cache.
|
|
|
|
|
|
|
|
This will trigger a resize if it changes, which may require evicting
|
|
|
|
items from the cache.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: Whether the cache changed size or not.
|
|
|
|
"""
|
|
|
|
new_size = math.floor(self._original_max_size * factor)
|
|
|
|
if new_size != self._max_size:
|
|
|
|
self.max_size = new_size
|
|
|
|
self._evict()
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
def has_entity_changed(self, entity: EntityType, stream_pos: int) -> bool:
|
2016-01-28 09:39:18 -07:00
|
|
|
"""Returns True if the entity may have been updated since stream_pos
|
|
|
|
"""
|
2018-10-19 18:16:55 -06:00
|
|
|
assert type(stream_pos) in integer_types
|
2016-01-28 09:37:41 -07:00
|
|
|
|
2016-01-28 10:44:04 -07:00
|
|
|
if stream_pos < self._earliest_known_stream_pos:
|
2016-06-02 04:29:44 -06:00
|
|
|
self.metrics.inc_misses()
|
2016-01-28 09:37:41 -07:00
|
|
|
return True
|
|
|
|
|
|
|
|
latest_entity_change_pos = self._entity_to_key.get(entity, None)
|
|
|
|
if latest_entity_change_pos is None:
|
2016-06-02 04:29:44 -06:00
|
|
|
self.metrics.inc_hits()
|
2016-01-29 09:24:34 -07:00
|
|
|
return False
|
2016-01-28 09:37:41 -07:00
|
|
|
|
|
|
|
if stream_pos < latest_entity_change_pos:
|
2016-06-02 04:29:44 -06:00
|
|
|
self.metrics.inc_misses()
|
2016-01-28 09:37:41 -07:00
|
|
|
return True
|
|
|
|
|
2016-06-02 04:29:44 -06:00
|
|
|
self.metrics.inc_hits()
|
2016-01-28 09:37:41 -07:00
|
|
|
return False
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
def get_entities_changed(
|
2020-05-05 10:07:59 -06:00
|
|
|
self, entities: Collection[EntityType], stream_pos: int
|
|
|
|
) -> Union[Set[EntityType], FrozenSet[EntityType]]:
|
2018-06-05 08:13:57 -06:00
|
|
|
"""
|
|
|
|
Returns subset of entities that have had new things since the given
|
|
|
|
position. Entities unknown to the cache will be returned. If the
|
|
|
|
position is too old it will just return the given list.
|
2016-01-28 09:37:41 -07:00
|
|
|
"""
|
2020-04-22 06:45:40 -06:00
|
|
|
changed_entities = self.get_all_entities_changed(stream_pos)
|
|
|
|
if changed_entities is not None:
|
2020-05-05 10:07:59 -06:00
|
|
|
# We now do an intersection, trying to do so in the most efficient
|
|
|
|
# way possible (some of these sets are *large*). First check in the
|
|
|
|
# given iterable is already set that we can reuse, otherwise we
|
|
|
|
# create a set of the *smallest* of the two iterables and call
|
|
|
|
# `intersection(..)` on it (this can be twice as fast as the reverse).
|
|
|
|
if isinstance(entities, (set, frozenset)):
|
|
|
|
result = entities.intersection(changed_entities)
|
|
|
|
elif len(changed_entities) < len(entities):
|
|
|
|
result = set(changed_entities).intersection(entities)
|
|
|
|
else:
|
|
|
|
result = set(entities).intersection(changed_entities)
|
2016-06-02 04:29:44 -06:00
|
|
|
self.metrics.inc_hits()
|
2016-01-28 09:37:41 -07:00
|
|
|
else:
|
2018-06-05 08:13:57 -06:00
|
|
|
result = set(entities)
|
2016-06-02 04:29:44 -06:00
|
|
|
self.metrics.inc_misses()
|
2016-01-28 09:37:41 -07:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
def has_any_entity_changed(self, stream_pos: int) -> bool:
|
2017-05-31 08:46:36 -06:00
|
|
|
"""Returns if any entity has changed
|
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
2018-06-05 08:13:57 -06:00
|
|
|
if not self._cache:
|
2020-04-22 06:45:40 -06:00
|
|
|
# If the cache is empty, nothing can have changed.
|
2018-06-05 08:13:57 -06:00
|
|
|
return False
|
|
|
|
|
2017-05-31 08:46:36 -06:00
|
|
|
if stream_pos >= self._earliest_known_stream_pos:
|
|
|
|
self.metrics.inc_hits()
|
2018-06-05 08:13:57 -06:00
|
|
|
return self._cache.bisect_right(stream_pos) < len(self._cache)
|
2017-05-31 08:46:36 -06:00
|
|
|
else:
|
|
|
|
self.metrics.inc_misses()
|
|
|
|
return True
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
def get_all_entities_changed(self, stream_pos: int) -> Optional[List[EntityType]]:
|
|
|
|
"""Returns all entities that have had new things since the given
|
2016-02-23 07:48:23 -07:00
|
|
|
position. If the position is too old it will return None.
|
2020-04-22 06:45:40 -06:00
|
|
|
|
|
|
|
Returns the entities in the order that they were changed.
|
2016-02-23 07:48:23 -07:00
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
if stream_pos < self._earliest_known_stream_pos:
|
2016-02-23 07:48:23 -07:00
|
|
|
return None
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
changed_entities = [] # type: List[EntityType]
|
|
|
|
|
|
|
|
for k in self._cache.islice(start=self._cache.bisect_right(stream_pos)):
|
|
|
|
changed_entities.extend(self._cache[k])
|
|
|
|
return changed_entities
|
|
|
|
|
|
|
|
def entity_has_changed(self, entity: EntityType, stream_pos: int) -> None:
|
2016-01-28 11:05:43 -07:00
|
|
|
"""Informs the cache that the entity has been changed at the given
|
2016-01-28 09:37:41 -07:00
|
|
|
position.
|
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
if stream_pos <= self._earliest_known_stream_pos:
|
|
|
|
return
|
|
|
|
|
|
|
|
old_pos = self._entity_to_key.get(entity, None)
|
|
|
|
if old_pos is not None:
|
|
|
|
if old_pos >= stream_pos:
|
|
|
|
# nothing to do
|
|
|
|
return
|
|
|
|
e = self._cache[old_pos]
|
|
|
|
e.remove(entity)
|
|
|
|
if not e:
|
|
|
|
# cache at this point is now empty
|
|
|
|
del self._cache[old_pos]
|
|
|
|
|
|
|
|
e1 = self._cache.get(stream_pos)
|
|
|
|
if e1 is None:
|
|
|
|
e1 = self._cache[stream_pos] = set()
|
|
|
|
e1.add(entity)
|
|
|
|
self._entity_to_key[entity] = stream_pos
|
2020-05-11 11:45:23 -06:00
|
|
|
self._evict()
|
2020-04-22 06:45:40 -06:00
|
|
|
|
|
|
|
# if the cache is too big, remove entries
|
|
|
|
while len(self._cache) > self._max_size:
|
|
|
|
k, r = self._cache.popitem(0)
|
|
|
|
self._earliest_known_stream_pos = max(k, self._earliest_known_stream_pos)
|
|
|
|
for entity in r:
|
|
|
|
del self._entity_to_key[entity]
|
|
|
|
|
2020-05-11 11:45:23 -06:00
|
|
|
def _evict(self):
|
|
|
|
while len(self._cache) > self._max_size:
|
|
|
|
k, r = self._cache.popitem(0)
|
|
|
|
self._earliest_known_stream_pos = max(k, self._earliest_known_stream_pos)
|
|
|
|
for entity in r:
|
|
|
|
self._entity_to_key.pop(entity, None)
|
|
|
|
|
2020-04-22 06:45:40 -06:00
|
|
|
def get_max_pos_of_last_change(self, entity: EntityType) -> int:
|
|
|
|
|
2016-09-15 08:12:07 -06:00
|
|
|
"""Returns an upper bound of the stream id of the last change to an
|
|
|
|
entity.
|
2016-09-15 07:27:15 -06:00
|
|
|
"""
|
2016-09-15 08:12:07 -06:00
|
|
|
return self._entity_to_key.get(entity, self._earliest_known_stream_pos)
|