mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-12-30 08:56:13 -05:00
Merge pull request #836 from matrix-org/erikj/change_event_cache
Change the way we cache events
This commit is contained in:
commit
3210f4c385
@ -139,6 +139,9 @@ class _EventPeristenceQueue(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
_EventCacheEntry = namedtuple("_EventCacheEntry", ("event", "redacted_event"))
|
||||||
|
|
||||||
|
|
||||||
class EventsStore(SQLBaseStore):
|
class EventsStore(SQLBaseStore):
|
||||||
EVENT_ORIGIN_SERVER_TS_NAME = "event_origin_server_ts"
|
EVENT_ORIGIN_SERVER_TS_NAME = "event_origin_server_ts"
|
||||||
|
|
||||||
@ -738,53 +741,65 @@ class EventsStore(SQLBaseStore):
|
|||||||
event_id_list = event_ids
|
event_id_list = event_ids
|
||||||
event_ids = set(event_ids)
|
event_ids = set(event_ids)
|
||||||
|
|
||||||
event_map = self._get_events_from_cache(
|
event_entry_map = self._get_events_from_cache(
|
||||||
event_ids,
|
event_ids,
|
||||||
check_redacted=check_redacted,
|
|
||||||
get_prev_content=get_prev_content,
|
|
||||||
allow_rejected=allow_rejected,
|
allow_rejected=allow_rejected,
|
||||||
)
|
)
|
||||||
|
|
||||||
missing_events_ids = [e for e in event_ids if e not in event_map]
|
missing_events_ids = [e for e in event_ids if e not in event_entry_map]
|
||||||
|
|
||||||
if missing_events_ids:
|
if missing_events_ids:
|
||||||
missing_events = yield self._enqueue_events(
|
missing_events = yield self._enqueue_events(
|
||||||
missing_events_ids,
|
missing_events_ids,
|
||||||
check_redacted=check_redacted,
|
check_redacted=check_redacted,
|
||||||
get_prev_content=get_prev_content,
|
|
||||||
allow_rejected=allow_rejected,
|
allow_rejected=allow_rejected,
|
||||||
)
|
)
|
||||||
|
|
||||||
event_map.update(missing_events)
|
event_entry_map.update(missing_events)
|
||||||
|
|
||||||
defer.returnValue([
|
events = []
|
||||||
event_map[e_id] for e_id in event_id_list
|
for event_id in event_id_list:
|
||||||
if e_id in event_map and event_map[e_id]
|
entry = event_entry_map.get(event_id, None)
|
||||||
])
|
if not entry:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if allow_rejected or not entry.event.rejected_reason:
|
||||||
|
if check_redacted and entry.redacted_event:
|
||||||
|
event = entry.redacted_event
|
||||||
|
else:
|
||||||
|
event = entry.event
|
||||||
|
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
if get_prev_content:
|
||||||
|
if "replaces_state" in event.unsigned:
|
||||||
|
prev = yield self.get_event(
|
||||||
|
event.unsigned["replaces_state"],
|
||||||
|
get_prev_content=False,
|
||||||
|
allow_none=True,
|
||||||
|
)
|
||||||
|
if prev:
|
||||||
|
event.unsigned = dict(event.unsigned)
|
||||||
|
event.unsigned["prev_content"] = prev.content
|
||||||
|
event.unsigned["prev_sender"] = prev.sender
|
||||||
|
|
||||||
|
defer.returnValue(events)
|
||||||
|
|
||||||
def _invalidate_get_event_cache(self, event_id):
|
def _invalidate_get_event_cache(self, event_id):
|
||||||
for check_redacted in (False, True):
|
self._get_event_cache.invalidate((event_id,))
|
||||||
for get_prev_content in (False, True):
|
|
||||||
self._get_event_cache.invalidate(
|
|
||||||
(event_id, check_redacted, get_prev_content)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _get_events_from_cache(self, events, check_redacted, get_prev_content,
|
def _get_events_from_cache(self, events, allow_rejected):
|
||||||
allow_rejected):
|
|
||||||
event_map = {}
|
event_map = {}
|
||||||
|
|
||||||
for event_id in events:
|
for event_id in events:
|
||||||
try:
|
ret = self._get_event_cache.get((event_id,), None)
|
||||||
ret = self._get_event_cache.get(
|
if not ret:
|
||||||
(event_id, check_redacted, get_prev_content,)
|
continue
|
||||||
)
|
|
||||||
|
|
||||||
if allow_rejected or not ret.rejected_reason:
|
if allow_rejected or not ret.event.rejected_reason:
|
||||||
event_map[event_id] = ret
|
event_map[event_id] = ret
|
||||||
else:
|
else:
|
||||||
event_map[event_id] = None
|
event_map[event_id] = None
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return event_map
|
return event_map
|
||||||
|
|
||||||
@ -855,8 +870,7 @@ class EventsStore(SQLBaseStore):
|
|||||||
reactor.callFromThread(fire, event_list)
|
reactor.callFromThread(fire, event_list)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _enqueue_events(self, events, check_redacted=True,
|
def _enqueue_events(self, events, check_redacted=True, allow_rejected=False):
|
||||||
get_prev_content=False, allow_rejected=False):
|
|
||||||
"""Fetches events from the database using the _event_fetch_list. This
|
"""Fetches events from the database using the _event_fetch_list. This
|
||||||
allows batch and bulk fetching of events - it allows us to fetch events
|
allows batch and bulk fetching of events - it allows us to fetch events
|
||||||
without having to create a new transaction for each request for events.
|
without having to create a new transaction for each request for events.
|
||||||
@ -894,8 +908,6 @@ class EventsStore(SQLBaseStore):
|
|||||||
[
|
[
|
||||||
preserve_fn(self._get_event_from_row)(
|
preserve_fn(self._get_event_from_row)(
|
||||||
row["internal_metadata"], row["json"], row["redacts"],
|
row["internal_metadata"], row["json"], row["redacts"],
|
||||||
check_redacted=check_redacted,
|
|
||||||
get_prev_content=get_prev_content,
|
|
||||||
rejected_reason=row["rejects"],
|
rejected_reason=row["rejects"],
|
||||||
)
|
)
|
||||||
for row in rows
|
for row in rows
|
||||||
@ -904,7 +916,7 @@ class EventsStore(SQLBaseStore):
|
|||||||
)
|
)
|
||||||
|
|
||||||
defer.returnValue({
|
defer.returnValue({
|
||||||
e.event_id: e
|
e.event.event_id: e
|
||||||
for e in res if e
|
for e in res if e
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -936,7 +948,6 @@ class EventsStore(SQLBaseStore):
|
|||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _get_event_from_row(self, internal_metadata, js, redacted,
|
def _get_event_from_row(self, internal_metadata, js, redacted,
|
||||||
check_redacted=True, get_prev_content=False,
|
|
||||||
rejected_reason=None):
|
rejected_reason=None):
|
||||||
d = json.loads(js)
|
d = json.loads(js)
|
||||||
internal_metadata = json.loads(internal_metadata)
|
internal_metadata = json.loads(internal_metadata)
|
||||||
@ -946,26 +957,27 @@ class EventsStore(SQLBaseStore):
|
|||||||
table="rejections",
|
table="rejections",
|
||||||
keyvalues={"event_id": rejected_reason},
|
keyvalues={"event_id": rejected_reason},
|
||||||
retcol="reason",
|
retcol="reason",
|
||||||
desc="_get_event_from_row",
|
desc="_get_event_from_row_rejected_reason",
|
||||||
)
|
)
|
||||||
|
|
||||||
ev = FrozenEvent(
|
original_ev = FrozenEvent(
|
||||||
d,
|
d,
|
||||||
internal_metadata_dict=internal_metadata,
|
internal_metadata_dict=internal_metadata,
|
||||||
rejected_reason=rejected_reason,
|
rejected_reason=rejected_reason,
|
||||||
)
|
)
|
||||||
|
|
||||||
if check_redacted and redacted:
|
redacted_event = None
|
||||||
ev = prune_event(ev)
|
if redacted:
|
||||||
|
redacted_event = prune_event(original_ev)
|
||||||
|
|
||||||
redaction_id = yield self._simple_select_one_onecol(
|
redaction_id = yield self._simple_select_one_onecol(
|
||||||
table="redactions",
|
table="redactions",
|
||||||
keyvalues={"redacts": ev.event_id},
|
keyvalues={"redacts": redacted_event.event_id},
|
||||||
retcol="event_id",
|
retcol="event_id",
|
||||||
desc="_get_event_from_row",
|
desc="_get_event_from_row_redactions",
|
||||||
)
|
)
|
||||||
|
|
||||||
ev.unsigned["redacted_by"] = redaction_id
|
redacted_event.unsigned["redacted_by"] = redaction_id
|
||||||
# Get the redaction event.
|
# Get the redaction event.
|
||||||
|
|
||||||
because = yield self.get_event(
|
because = yield self.get_event(
|
||||||
@ -977,23 +989,16 @@ class EventsStore(SQLBaseStore):
|
|||||||
if because:
|
if because:
|
||||||
# It's fine to do add the event directly, since get_pdu_json
|
# It's fine to do add the event directly, since get_pdu_json
|
||||||
# will serialise this field correctly
|
# will serialise this field correctly
|
||||||
ev.unsigned["redacted_because"] = because
|
redacted_event.unsigned["redacted_because"] = because
|
||||||
|
|
||||||
if get_prev_content and "replaces_state" in ev.unsigned:
|
cache_entry = _EventCacheEntry(
|
||||||
prev = yield self.get_event(
|
event=original_ev,
|
||||||
ev.unsigned["replaces_state"],
|
redacted_event=redacted_event,
|
||||||
get_prev_content=False,
|
|
||||||
allow_none=True,
|
|
||||||
)
|
|
||||||
if prev:
|
|
||||||
ev.unsigned["prev_content"] = prev.content
|
|
||||||
ev.unsigned["prev_sender"] = prev.sender
|
|
||||||
|
|
||||||
self._get_event_cache.prefill(
|
|
||||||
(ev.event_id, check_redacted, get_prev_content), ev
|
|
||||||
)
|
)
|
||||||
|
|
||||||
defer.returnValue(ev)
|
self._get_event_cache.prefill((original_ev.event_id,), cache_entry)
|
||||||
|
|
||||||
|
defer.returnValue(cache_entry)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def count_daily_messages(self):
|
def count_daily_messages(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user