Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use a chain cover index to efficiently calculate auth chain difference #8868

Merged
merged 48 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
49e888d
Change alg
erikjohnston Dec 1, 2020
8c760ff
Calculate chain ID/seq no on event insertion
erikjohnston Dec 2, 2020
85348e1
Add some docs about the chain cover
erikjohnston Dec 2, 2020
02d1198
Handle old rooms
erikjohnston Dec 3, 2020
61ab47e
Fix schema for sqlite
erikjohnston Dec 3, 2020
6141825
Fix up _get_auth_chain_difference_using_chains_txn
erikjohnston Dec 3, 2020
c7e2ce5
Newsfile
erikjohnston Dec 3, 2020
66e779d
Add type
erikjohnston Dec 3, 2020
cf2243f
Fixup
erikjohnston Dec 3, 2020
bd30c9e
Fix take1
erikjohnston Dec 4, 2020
55f03b9
Fixup
erikjohnston Dec 4, 2020
3e98fb7
More fixups
erikjohnston Dec 4, 2020
9087033
Newsfile
erikjohnston Dec 4, 2020
21b3ef0
Test both new and old methods
erikjohnston Dec 4, 2020
fdaf4da
Note
erikjohnston Dec 4, 2020
7f5ac13
isort
erikjohnston Dec 4, 2020
afb7f80
Don't add links where start and end chain are the same
erikjohnston Dec 7, 2020
dec1f74
Have exists_path_from handle same chain case correctly
erikjohnston Dec 7, 2020
9279940
Add some tests
erikjohnston Dec 7, 2020
6a74e21
Fix unit tests on postgres
erikjohnston Dec 7, 2020
654eff1
Add missing 'auth'
erikjohnston Dec 7, 2020
dbecefd
Fixup typing for execute_values
erikjohnston Dec 8, 2020
123b431
Rename _get_auth_chain_difference_using_chains_txn and add comment
erikjohnston Dec 8, 2020
883e922
Add some definitions
erikjohnston Dec 8, 2020
988f25a
Fixup link confusion
erikjohnston Dec 8, 2020
08ec78b
Make para less dense (hopefully)
erikjohnston Dec 8, 2020
024c802
Add note about auth chain
erikjohnston Dec 8, 2020
4cc769f
Be explicit
erikjohnston Dec 8, 2020
7d75efb
rm variant
erikjohnston Dec 8, 2020
92b5e4b
Add note about current algo
erikjohnston Dec 8, 2020
a9552c2
Update docs/auth_chain_difference_algorithm.md
erikjohnston Dec 8, 2020
7cc6d7e
Fix up _LinkMap
erikjohnston Dec 8, 2020
5fa05f2
Fix up event_chain tests
erikjohnston Dec 8, 2020
8dac80c
Merge remote-tracking branch 'origin/develop' into erikj/auth_chains_…
erikjohnston Dec 9, 2020
e3d0be4
Make sorted_topologically stable and add tests
erikjohnston Dec 9, 2020
cdb88c2
Make _LinkMap use tuples
erikjohnston Dec 9, 2020
0f91c86
Review comments
erikjohnston Dec 9, 2020
888450a
Fix typo
erikjohnston Dec 9, 2020
c9422b6
Handle rooms the server used to be in correctly.
erikjohnston Jan 5, 2021
c8758af
Handle case where we don't have chain info for an event
erikjohnston Jan 6, 2021
b2ac553
Merge remote-tracking branch 'origin/develop' into erikj/auth_chains_…
erikjohnston Jan 6, 2021
d96264d
Merge remote-tracking branch 'origin/develop' into erikj/auth_chains_…
erikjohnston Jan 6, 2021
d64f5f8
Typo
erikjohnston Jan 11, 2021
6071eff
Split out a has_auth_chain_index
erikjohnston Jan 11, 2021
368d3b8
Update docstring
erikjohnston Jan 11, 2021
bea2c47
Move to schema 59
erikjohnston Jan 11, 2021
03dd636
Merge remote-tracking branch 'origin/develop' into erikj/auth_chains_…
erikjohnston Jan 11, 2021
8c1e32c
Fix tests after merge from develop
erikjohnston Jan 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from synapse.storage.databases.main.events_worker import EventsWorkerStore
from synapse.storage.databases.main.signatures import SignatureWorkerStore
from synapse.storage.engines import PostgresEngine
from synapse.storage.types import Cursor
from synapse.types import Collection
from synapse.util.caches.descriptors import cached
from synapse.util.caches.lrucache import LruCache
Expand All @@ -33,6 +34,11 @@
logger = logging.getLogger(__name__)


class _NoChainCoverIndex(Exception):
def __init__(self, room_id: str):
super().__init__("Unexpectedly no chain cover for events in %s" % (room_id,))


class EventFederationWorkerStore(EventsWorkerStore, SignatureWorkerStore, SQLBaseStore):
def __init__(self, database: DatabasePool, db_conn, hs):
super().__init__(database, db_conn, hs)
Expand Down Expand Up @@ -156,20 +162,26 @@ async def get_auth_chain_difference(
# algorithm.
room = await self.get_room(room_id)
if room["has_auth_chain_index"]:
return await self.db_pool.runInteraction(
"get_auth_chain_difference_chains",
self._get_auth_chain_difference_using_cover_index_txn,
state_sets,
)
else:
return await self.db_pool.runInteraction(
"get_auth_chain_difference",
self._get_auth_chain_difference_txn,
state_sets,
)
try:
return await self.db_pool.runInteraction(
"get_auth_chain_difference_chains",
self._get_auth_chain_difference_using_cover_index_txn,
room_id,
state_sets,
)
except _NoChainCoverIndex:
# For whatever reason we don't actually have a chain cover index
# for the events in question, so we fall back to the old method.
pass

return await self.db_pool.runInteraction(
"get_auth_chain_difference",
self._get_auth_chain_difference_txn,
state_sets,
)

def _get_auth_chain_difference_using_cover_index_txn(
self, txn, state_sets: List[Set[str]]
self, txn: Cursor, room_id: str, state_sets: List[Set[str]]
) -> Set[str]:
"""Calculates the auth chain difference using the chain index.

Expand Down Expand Up @@ -207,6 +219,18 @@ def _get_auth_chain_difference_using_cover_index_txn(
seen_chains.add(chain_id)
chain_to_event.setdefault(chain_id, {})[sequence_number] = event_id

# Check that we actually have a chain ID for all the events.
events_missing_chain_info = initial_events.difference(chain_info)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest using initial_events >= chain_info as we don't care about the difference, just if there is one. But chain_info is a dict and it would probably be more expensive to convert it to a set first. 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷

if events_missing_chain_info:
# This can happen due to e.g. downgrade/upgrade of the server. We
# raise an exception and fall back to the previous algorithm.
logger.info(
"Unexpectedly found that events don't have chain IDs in room %s: %s",
room_id,
events_missing_chain_info,
)
Comment on lines +227 to +231
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to recover from this? Will it happen automatically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should mostly happen automatically, unless there are events we don't have the full auth chain for (I have a couple of those on jki.re somehow). There's not really a way to recover from that failure mode, so I'm tempted to just leave it as is and revisit if it turns out that it happens a lot.

raise _NoChainCoverIndex(room_id)

# Corresponds to `state_sets`, except as a map from chain ID to max
# sequence number reachable from the state set.
set_to_chain = [] # type: List[Dict[int, int]]
Expand Down
163 changes: 163 additions & 0 deletions tests/storage/test_event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,169 @@ def insert_event(txn):
)
self.assertSetEqual(difference, set())

def test_auth_difference_partial_cover(self):
"""Test that we correctly handle rooms where not all events have a chain
cover calculated. This can happen due to a downgrade/upgrade.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move these to schema 59 is the downgrade really a potential cause of this? It is likely worth keeping the code to handle this case regardless.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've updated it

"""

room_id = "@ROOM:local"

# The silly auth graph we use to test the auth difference algorithm,
# where the top are the most recent events.
#
# A B
# \ /
# D E
# \ |
# ` F C
# | /|
# G ´ |
# | \ |
# H I
# | |
# K J

auth_graph = {
"a": ["e"],
"b": ["e"],
"c": ["g", "i"],
"d": ["f"],
"e": ["f"],
"f": ["g"],
"g": ["h", "i"],
"h": ["k"],
"i": ["j"],
"k": [],
"j": [],
}

depth_map = {
"a": 7,
"b": 7,
"c": 4,
"d": 6,
"e": 6,
"f": 5,
"g": 3,
"h": 2,
"i": 2,
"k": 1,
"j": 1,
}

# We rudely fiddle with the appropriate tables directly, as that's much
# easier than constructing events properly.

def insert_event(txn):
# First insert the room and mark it has having a chain cover.
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
self.store.db_pool.simple_insert_txn(
txn,
"rooms",
{
"room_id": room_id,
"creator": "room_creator_user_id",
"is_public": True,
"room_version": "6",
"has_auth_chain_index": True,
},
)

stream_ordering = 0

for event_id in auth_graph:
stream_ordering += 1
depth = depth_map[event_id]

self.store.db_pool.simple_insert_txn(
txn,
table="events",
values={
"event_id": event_id,
"room_id": room_id,
"depth": depth,
"topological_ordering": depth,
"type": "m.test",
"processed": True,
"outlier": False,
"stream_ordering": stream_ordering,
},
)

# Insert all events apart from 'B'
self.hs.datastores.persist_events._persist_event_auth_chain_txn(
txn,
[
FakeEvent(event_id, room_id, auth_graph[event_id])
for event_id in auth_graph
if event_id != "b"
],
)

# Now we insert the event 'B' without a chain cover, by temporarily
# pretending the room doesn't have a chain cover.

self.store.db_pool.simple_update_txn(
txn,
table="rooms",
keyvalues={"room_id": room_id},
updatevalues={"has_auth_chain_index": False},
)

self.hs.datastores.persist_events._persist_event_auth_chain_txn(
txn, [FakeEvent("b", room_id, auth_graph["b"])],
)

self.store.db_pool.simple_update_txn(
txn,
table="rooms",
keyvalues={"room_id": room_id},
updatevalues={"has_auth_chain_index": True},
)

self.get_success(self.store.db_pool.runInteraction("insert", insert_event,))

# Now actually test that various combinations give the right result:

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a"}, {"b"}])
)
self.assertSetEqual(difference, {"a", "b"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a"}, {"b"}, {"c"}])
)
self.assertSetEqual(difference, {"a", "b", "c", "e", "f"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a", "c"}, {"b"}])
)
self.assertSetEqual(difference, {"a", "b", "c"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a", "c"}, {"b", "c"}])
)
self.assertSetEqual(difference, {"a", "b"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a"}, {"b"}, {"d"}])
)
self.assertSetEqual(difference, {"a", "b", "d", "e"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a"}, {"b"}, {"c"}, {"d"}])
)
self.assertSetEqual(difference, {"a", "b", "c", "d", "e", "f"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a"}, {"b"}, {"e"}])
)
self.assertSetEqual(difference, {"a", "b"})

difference = self.get_success(
self.store.get_auth_chain_difference(room_id, [{"a"}])
)
self.assertSetEqual(difference, set())


@attr.s
class FakeEvent:
Expand Down