Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sliding Sync: Track whether we have sent rooms down to clients #17447

Merged
merged 49 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
30263b4
Add SlidingSyncStreamToken
erikjohnston Jul 17, 2024
1ad1cce
Pass throught SlidingSyncStreamToken
erikjohnston Jul 16, 2024
e8df0d7
Don't create tokens manually in SSS tests
erikjohnston Jul 16, 2024
f3030af
Fix to use new token format
erikjohnston Jul 16, 2024
f3a4cfb
Newsfile
erikjohnston Jul 17, 2024
d44f7e1
WIP/PoC of storing whether we have sent rooms down to clients
erikjohnston Jul 15, 2024
53273db
Add conn_id field
erikjohnston Jul 17, 2024
e2a88e4
Use new room store to track if we've sent a room down
erikjohnston Jul 16, 2024
1858317
Handle initial flag correctly
erikjohnston Jul 17, 2024
de6e3bd
Handle state deltas in non-initial rooms
erikjohnston Jul 17, 2024
e2c47bf
Fix tests
erikjohnston Jul 17, 2024
a90c408
Newsfile
erikjohnston Jul 17, 2024
2968f2e
Bump typing-extensions for 'assert_never'
erikjohnston Jul 17, 2024
2784881
Add tests
erikjohnston Jul 17, 2024
d689204
Add docstring
erikjohnston Jul 18, 2024
560087b
Remove '_token' prefix
erikjohnston Jul 18, 2024
40d9587
Apply suggestions from code review
erikjohnston Jul 18, 2024
14eb781
Reword doc
erikjohnston Jul 18, 2024
9ae2551
Keyword args
erikjohnston Jul 18, 2024
51f7602
Rename bounds
erikjohnston Jul 18, 2024
eab092b
Add context to conn_id
erikjohnston Jul 18, 2024
605b358
Refactor to avoid SyncConfig.connection_id()
erikjohnston Jul 18, 2024
37c4463
Add some unit tests for 'get_room_sync_data'
erikjohnston Jul 18, 2024
7f26fc4
Merge remote-tracking branch 'origin/develop' into erikj/ss_room_store
erikjohnston Jul 18, 2024
532594e
Fix linting in tests
erikjohnston Jul 18, 2024
1230a51
Merge remote-tracking branch 'origin/develop' into erikj/ss_tokens
erikjohnston Jul 19, 2024
01f1dca
Merge branch 'erikj/ss_tokens' into erikj/ss_room_store
erikjohnston Jul 19, 2024
6f738a4
Apply suggestions from code review
erikjohnston Jul 23, 2024
588dfb8
Merge remote-tracking branch 'origin/develop' into erikj/ss_tokens
erikjohnston Jul 23, 2024
0c4e633
Fix tests
erikjohnston Jul 23, 2024
60790d6
Change token names again
erikjohnston Jul 23, 2024
4ce3e51
Ensure there is only one SlidingSyncHandler
erikjohnston Jul 23, 2024
602b6c8
Add test for cache being cleared
erikjohnston Jul 23, 2024
e2f3d48
Merge branch 'erikj/ss_tokens' into erikj/ss_room_store
erikjohnston Jul 23, 2024
81b2162
Merge remote-tracking branch 'origin/develop' into erikj/ss_room_store
erikjohnston Jul 24, 2024
0812894
Update comments
erikjohnston Jul 25, 2024
66f80cc
Merge remote-tracking branch 'origin/develop' into erikj/ss_room_store
erikjohnston Jul 25, 2024
2c48784
Use do_sync in tests
erikjohnston Jul 25, 2024
abd7a5b
Move to integration tests
erikjohnston Jul 25, 2024
470c5d3
Review comments
erikjohnston Jul 26, 2024
90f2184
Previously state test
erikjohnston Jul 26, 2024
e7a6c19
Add never test
erikjohnston Jul 26, 2024
e2e7280
Apply suggestions from code review
erikjohnston Jul 29, 2024
1f19e1d
connection_position
erikjohnston Jul 29, 2024
531a775
Fix test naming and add new test
erikjohnston Jul 29, 2024
caa6fa6
Apply suggestions from code review
erikjohnston Jul 29, 2024
6d8cb98
Fix up tests
erikjohnston Jul 29, 2024
1d5367d
Fix lint
erikjohnston Jul 29, 2024
91d4087
Fix lint
erikjohnston Jul 29, 2024
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
1 change: 1 addition & 0 deletions changelog.d/17447.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Track which rooms have been sent to clients in the experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ netaddr = ">=0.7.18"
# add a lower bound to the Jinja2 dependency.
Jinja2 = ">=3.0"
bleach = ">=1.4.3"
# We use `Self`, which were added in `typing-extensions` 4.0.
typing-extensions = ">=4.0"
# We use `assert_never`, which were added in `typing-extensions` 4.1.
typing-extensions = ">=4.1"
# We enforce that we have a `cryptography` version that bundles an `openssl`
# with the latest security patches.
cryptography = ">=3.4.7"
Expand Down
350 changes: 314 additions & 36 deletions synapse/handlers/sliding_sync.py

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions synapse/rest/client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,6 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
)

user = requester.user
device_id = requester.device_id

timeout = parse_integer(request, "timeout", default=0)
# Position in the stream
Expand All @@ -902,11 +901,12 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:

sync_config = SlidingSyncConfig(
user=user,
device_id=device_id,
requester=requester,
# FIXME: Currently, we're just manually copying the fields from the
# `SlidingSyncBody` into the config. How can we gurantee into the future
# `SlidingSyncBody` into the config. How can we guarantee into the future
# that we don't forget any? I would like something more structured like
# `copy_attributes(from=body, to=config)`
conn_id=body.conn_id,
lists=body.lists,
room_subscriptions=body.room_subscriptions,
extensions=body.extensions,
Expand Down
1 change: 1 addition & 0 deletions synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ def get_jwt_handler(self) -> "JwtHandler":
def get_sync_handler(self) -> SyncHandler:
return SyncHandler(self)

@cache_in_self
def get_sliding_sync_handler(self) -> SlidingSyncHandler:
return SlidingSyncHandler(self)

Expand Down
37 changes: 37 additions & 0 deletions synapse/storage/databases/main/state_deltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

from synapse.storage._base import SQLBaseStore
from synapse.storage.database import LoggingTransaction
from synapse.storage.databases.main.stream import _filter_results_by_stream
from synapse.types import RoomStreamToken
from synapse.util.caches.stream_change_cache import StreamChangeCache

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -156,3 +158,38 @@ async def get_max_stream_id_in_current_state_deltas(self) -> int:
"get_max_stream_id_in_current_state_deltas",
self._get_max_stream_id_in_current_state_deltas_txn,
)

async def get_current_state_deltas_for_room(
self, room_id: str, from_token: RoomStreamToken, to_token: RoomStreamToken
) -> List[StateDelta]:
"""Get the state deltas between two tokens."""

def get_current_state_deltas_for_room_txn(
txn: LoggingTransaction,
) -> List[StateDelta]:
sql = """
SELECT instance_name, stream_id, type, state_key, event_id, prev_event_id
FROM current_state_delta_stream
WHERE room_id = ? AND ? < stream_id AND stream_id <= ?
ORDER BY stream_id ASC
"""
txn.execute(
sql, (room_id, from_token.stream, to_token.get_max_stream_pos())
)

return [
StateDelta(
stream_id=row[1],
room_id=room_id,
event_type=row[2],
state_key=row[3],
event_id=row[4],
prev_event_id=row[5],
)
for row in txn
if _filter_results_by_stream(from_token, to_token, row[0], row[1])
]

return await self.db_pool.runInteraction(
"get_current_state_deltas_for_room", get_current_state_deltas_for_room_txn
)
3 changes: 2 additions & 1 deletion synapse/types/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
DeviceListUpdates,
JsonDict,
JsonMapping,
Requester,
SlidingSyncStreamToken,
StreamToken,
UserID,
Expand Down Expand Up @@ -109,7 +110,7 @@ class SlidingSyncConfig(SlidingSyncBody):
"""

user: UserID
device_id: Optional[str]
requester: Requester

# Pydantic config
class Config:
Expand Down
5 changes: 5 additions & 0 deletions synapse/types/rest/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class SlidingSyncBody(RequestBodyModel):
Sliding Sync API request body.

Attributes:
conn_id: An optional string to identify this connection to the server.
Only one sliding sync connection is allowed per given conn_id (empty
or not).
lists: Sliding window API. A map of list key to list information
(:class:`SlidingSyncList`). Max lists: 100. The list keys should be
arbitrary strings which the client is using to refer to the list. Keep this
Expand Down Expand Up @@ -343,6 +346,8 @@ class AccountDataExtension(RequestBodyModel):
e2ee: Optional[E2eeExtension] = None
account_data: Optional[AccountDataExtension] = None

conn_id: Optional[str]

# mypy workaround via https://github.com/pydantic/pydantic/issues/156#issuecomment-1130883884
if TYPE_CHECKING:
lists: Optional[Dict[str, SlidingSyncList]] = None
Expand Down
Loading
Loading