2016-01-28 11:37:41 -05: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 02:09:20 -04:00
|
|
|
import logging
|
2016-01-28 11:37:41 -05:00
|
|
|
|
2018-10-19 20:16:55 -04:00
|
|
|
from six import integer_types
|
|
|
|
|
2018-06-05 10:13:57 -04:00
|
|
|
from sortedcontainers import SortedDict
|
2016-01-28 11:37:41 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.util import caches
|
2016-01-28 11:37:41 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class StreamChangeCache(object):
|
|
|
|
"""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 10:13:57 -04:00
|
|
|
|
|
|
|
def __init__(self, name, current_stream_pos, max_size=10000, prefilled_cache=None):
|
|
|
|
self._max_size = int(max_size * caches.CACHE_SIZE_FACTOR)
|
2016-01-28 11:37:41 -05:00
|
|
|
self._entity_to_key = {}
|
2018-06-05 10:13:57 -04:00
|
|
|
self._cache = SortedDict()
|
2016-01-28 11:37:41 -05:00
|
|
|
self._earliest_known_stream_pos = current_stream_pos
|
|
|
|
self.name = name
|
2018-06-05 10:13:57 -04:00
|
|
|
self.metrics = caches.register_cache("cache", self.name, self._cache)
|
2016-01-28 11:37:41 -05:00
|
|
|
|
2018-06-05 10:13:57 -04:00
|
|
|
if prefilled_cache:
|
|
|
|
for entity, stream_pos in prefilled_cache.items():
|
|
|
|
self.entity_has_changed(entity, stream_pos)
|
2016-01-29 09:37:59 -05:00
|
|
|
|
2016-01-28 11:39:18 -05:00
|
|
|
def has_entity_changed(self, entity, stream_pos):
|
|
|
|
"""Returns True if the entity may have been updated since stream_pos
|
|
|
|
"""
|
2018-10-19 20:16:55 -04:00
|
|
|
assert type(stream_pos) in integer_types
|
2016-01-28 11:37:41 -05:00
|
|
|
|
2016-01-28 12:44:04 -05:00
|
|
|
if stream_pos < self._earliest_known_stream_pos:
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_misses()
|
2016-01-28 11:37:41 -05:00
|
|
|
return True
|
|
|
|
|
|
|
|
latest_entity_change_pos = self._entity_to_key.get(entity, None)
|
|
|
|
if latest_entity_change_pos is None:
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_hits()
|
2016-01-29 11:24:34 -05:00
|
|
|
return False
|
2016-01-28 11:37:41 -05:00
|
|
|
|
|
|
|
if stream_pos < latest_entity_change_pos:
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_misses()
|
2016-01-28 11:37:41 -05:00
|
|
|
return True
|
|
|
|
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_hits()
|
2016-01-28 11:37:41 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
def get_entities_changed(self, entities, stream_pos):
|
2018-06-05 10:13:57 -04: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 11:37:41 -05:00
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
2016-01-28 12:44:04 -05:00
|
|
|
if stream_pos >= self._earliest_known_stream_pos:
|
2018-07-17 05:27:51 -04:00
|
|
|
changed_entities = {
|
2018-07-12 06:37:44 -04:00
|
|
|
self._cache[k] for k in self._cache.islice(
|
|
|
|
start=self._cache.bisect_right(stream_pos),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-17 06:07:04 -04:00
|
|
|
result = changed_entities.intersection(entities)
|
2018-07-17 05:27:51 -04:00
|
|
|
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_hits()
|
2016-01-28 11:37:41 -05:00
|
|
|
else:
|
2018-06-05 10:13:57 -04:00
|
|
|
result = set(entities)
|
2016-06-02 06:29:44 -04:00
|
|
|
self.metrics.inc_misses()
|
2016-01-28 11:37:41 -05:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2017-05-31 10:46:36 -04:00
|
|
|
def has_any_entity_changed(self, stream_pos):
|
|
|
|
"""Returns if any entity has changed
|
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
2018-06-05 10:13:57 -04:00
|
|
|
if not self._cache:
|
|
|
|
# If we have no cache, nothing can have changed.
|
|
|
|
return False
|
|
|
|
|
2017-05-31 10:46:36 -04:00
|
|
|
if stream_pos >= self._earliest_known_stream_pos:
|
|
|
|
self.metrics.inc_hits()
|
2018-06-05 10:13:57 -04:00
|
|
|
return self._cache.bisect_right(stream_pos) < len(self._cache)
|
2017-05-31 10:46:36 -04:00
|
|
|
else:
|
|
|
|
self.metrics.inc_misses()
|
|
|
|
return True
|
|
|
|
|
2016-02-23 09:48:23 -05:00
|
|
|
def get_all_entities_changed(self, stream_pos):
|
|
|
|
"""Returns all entites that have had new things since the given
|
|
|
|
position. If the position is too old it will return None.
|
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
|
|
|
if stream_pos >= self._earliest_known_stream_pos:
|
2018-06-28 06:04:18 -04:00
|
|
|
return [self._cache[k] for k in self._cache.islice(
|
|
|
|
start=self._cache.bisect_right(stream_pos))]
|
2016-02-23 09:48:23 -05:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2016-01-28 13:05:43 -05:00
|
|
|
def entity_has_changed(self, entity, stream_pos):
|
|
|
|
"""Informs the cache that the entity has been changed at the given
|
2016-01-28 11:37:41 -05:00
|
|
|
position.
|
|
|
|
"""
|
|
|
|
assert type(stream_pos) is int
|
|
|
|
|
|
|
|
if stream_pos > self._earliest_known_stream_pos:
|
2016-01-28 13:05:43 -05:00
|
|
|
old_pos = self._entity_to_key.get(entity, None)
|
2016-01-29 11:24:34 -05:00
|
|
|
if old_pos is not None:
|
2016-01-28 11:37:41 -05:00
|
|
|
stream_pos = max(stream_pos, old_pos)
|
|
|
|
self._cache.pop(old_pos, None)
|
2016-01-28 13:05:43 -05:00
|
|
|
self._cache[stream_pos] = entity
|
|
|
|
self._entity_to_key[entity] = stream_pos
|
2016-01-28 11:37:41 -05:00
|
|
|
|
|
|
|
while len(self._cache) > self._max_size:
|
2018-06-05 10:13:57 -04:00
|
|
|
k, r = self._cache.popitem(0)
|
|
|
|
self._earliest_known_stream_pos = max(
|
|
|
|
k, self._earliest_known_stream_pos,
|
|
|
|
)
|
2016-01-28 11:37:41 -05:00
|
|
|
self._entity_to_key.pop(r, None)
|
2016-09-15 09:27:15 -04:00
|
|
|
|
2016-09-15 10:12:07 -04:00
|
|
|
def get_max_pos_of_last_change(self, entity):
|
|
|
|
"""Returns an upper bound of the stream id of the last change to an
|
|
|
|
entity.
|
2016-09-15 09:27:15 -04:00
|
|
|
"""
|
2016-09-15 10:12:07 -04:00
|
|
|
return self._entity_to_key.get(entity, self._earliest_known_stream_pos)
|