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

Commit 878bdda

Browse files
committed
Only define state at start of floating chain and derive rest
Context: #12083 (comment)
1 parent f3e234e commit 878bdda

File tree

3 files changed

+56
-51
lines changed

3 files changed

+56
-51
lines changed

synapse/handlers/message.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,12 @@ async def create_event(
626626
state_event_ids=state_event_ids,
627627
depth=depth,
628628
)
629+
logger.info(
630+
"create_new_client_event %s event=%s state_group=%s",
631+
event.type,
632+
event.event_id,
633+
context._state_group,
634+
)
629635

630636
# In an ideal world we wouldn't need the second part of this condition. However,
631637
# this behaviour isn't spec'd yet, meaning we should be able to deactivate this

synapse/handlers/room_batch.py

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -151,24 +151,21 @@ async def persist_state_events_at_start(
151151
self,
152152
state_events_at_start: List[JsonDict],
153153
room_id: str,
154-
initial_state_ids_at_event: List[str],
154+
initial_state_event_ids: List[str],
155155
app_service_requester: Requester,
156156
) -> List[str]:
157157
"""Takes all `state_events_at_start` event dictionaries and creates/persists
158-
them as floating state events which don't resolve into the current room state.
159-
They are floating because they reference a fake prev_event which doesn't connect
160-
to the normal DAG at all.
158+
them in a floating state event chain which don't resolve into the current room
159+
state. They are floating because they reference no prev_events and are marked
160+
as outliers which disconnects them from the normal DAG.
161161
162162
Args:
163163
state_events_at_start:
164164
room_id: Room where you want the events persisted in.
165-
initial_state_ids_at_event:
166-
The base set of state of the historical batch. When persisting each of
167-
the events in state_events_at_start, the state will be stripped down to
168-
only what's necessary to auth the given event and set as the
169-
auth_event_ids. After each state event from state_events_at_start is
170-
persisted, it will be added to the ongoing list of state_event_ids for
171-
the next state event to be persisted with.
165+
initial_state_event_ids:
166+
The base set of state for the historical batch which the floating
167+
state chain will derive from. This should probably be the state
168+
from the `prev_event` defined by `/batch_send?prev_event_id=$abc`.
172169
app_service_requester: The requester of an application service.
173170
174171
Returns:
@@ -177,7 +174,6 @@ async def persist_state_events_at_start(
177174
assert app_service_requester.app_service
178175

179176
state_event_ids_at_start = []
180-
state_event_ids = initial_state_ids_at_event.copy()
181177

182178
# Make the state events float off on their own by specifying no
183179
# prev_events for the first one in the chain so we don't have a bunch of
@@ -190,9 +186,7 @@ async def persist_state_events_at_start(
190186
)
191187

192188
logger.debug(
193-
"RoomBatchSendEventRestServlet inserting state_event=%s, state_event_ids=%s",
194-
state_event,
195-
state_event_ids,
189+
"RoomBatchSendEventRestServlet inserting state_event=%s", state_event
196190
)
197191

198192
event_dict = {
@@ -220,14 +214,14 @@ async def persist_state_events_at_start(
220214
content=event_dict["content"],
221215
outlier=True,
222216
historical=True,
223-
# Only the first event in the chain should be floating.
217+
# Only the first event in the state chain should be floating.
224218
# The rest should hang off each other in a chain.
225219
allow_no_prev_events=index == 0,
226220
prev_event_ids=prev_event_ids_for_state_chain,
227-
# Make sure to use a copy of this list because we modify it
228-
# later in the loop here. Otherwise it will be the same
229-
# reference and also update in the event when we append later.
230-
state_event_ids=state_event_ids.copy(),
221+
# Since the first event in the state chain is floating with
222+
# no `prev_events`, it can't derive state from anywhere
223+
# automatically. So we need to set some state explicitly.
224+
state_event_ids=initial_state_event_ids if index == 0 else None,
231225
)
232226
else:
233227
# TODO: Add some complement tests that adds state that is not member joins
@@ -243,19 +237,18 @@ async def persist_state_events_at_start(
243237
event_dict,
244238
outlier=True,
245239
historical=True,
246-
# Only the first event in the chain should be floating.
240+
# Only the first event in the state chain should be floating.
247241
# The rest should hang off each other in a chain.
248242
allow_no_prev_events=index == 0,
249243
prev_event_ids=prev_event_ids_for_state_chain,
250-
# Make sure to use a copy of this list because we modify it
251-
# later in the loop here. Otherwise it will be the same
252-
# reference and also update in the event when we append later.
253-
state_event_ids=state_event_ids.copy(),
244+
# Since the first event in the state chain is floating with
245+
# no `prev_events`, it can't derive state from anywhere
246+
# automatically. So we need to set some state explicitly.
247+
state_event_ids=initial_state_event_ids if index == 0 else None,
254248
)
255249
event_id = event.event_id
256250

257251
state_event_ids_at_start.append(event_id)
258-
state_event_ids.append(event_id)
259252
# Connect all the state in a floating chain
260253
prev_event_ids_for_state_chain = [event_id]
261254

@@ -266,7 +259,7 @@ async def persist_historical_events(
266259
events_to_create: List[JsonDict],
267260
room_id: str,
268261
inherited_depth: int,
269-
state_event_ids: List[str],
262+
initial_state_event_ids: List[str],
270263
app_service_requester: Requester,
271264
) -> List[str]:
272265
"""Create and persists all events provided sequentially. Handles the
@@ -282,20 +275,22 @@ async def persist_historical_events(
282275
room_id: Room where you want the events persisted in.
283276
inherited_depth: The depth to create the events at (you will
284277
probably by calling inherit_depth_from_prev_ids(...)).
285-
state_event_ids:
286-
The full state at a given event. We share the same state across the whole
287-
historical batch. The state events will be stripped down to only what's
288-
necessary to auth a given event and set as the auth_event_ids. For
289-
insertion events, we will add all of these state events as the explicit
290-
state so the rest of the historical batch can inherit the same state and
291-
state_group.
278+
initial_state_event_ids:
279+
This is used to set explicit state for the insertion event at
280+
the start of the historical batch since it's floating with no
281+
prev_events to derive state from automatically.
292282
app_service_requester: The requester of an application service.
293283
294284
Returns:
295285
List of persisted event IDs
296286
"""
297287
assert app_service_requester.app_service
298288

289+
# We expect the first event in a historical batch to be an insertion event
290+
assert events_to_create[0]["type"] == EventTypes.MSC2716_INSERTION
291+
# We expect the last event in a historical batch to be an batch event
292+
assert events_to_create[-1]["type"] == EventTypes.MSC2716_BATCH
293+
299294
# Make the historical event chain float off on its own by specifying no
300295
# prev_events for the first event in the chain which causes the HS to
301296
# ask for the state at the start of the batch later.
@@ -327,11 +322,16 @@ async def persist_historical_events(
327322
ev["sender"], app_service_requester.app_service
328323
),
329324
event_dict,
330-
# Only the first event in the chain should be floating.
331-
# The rest should hang off each other in a chain.
325+
# Only the first event (which is the insertion event) in the
326+
# chain should be floating. The rest should hang off each other
327+
# in a chain.
332328
allow_no_prev_events=index == 0,
333329
prev_event_ids=event_dict.get("prev_events"),
334-
state_event_ids=state_event_ids,
330+
# Since the first event (which is the insertion event) in the
331+
# chain is floating with no `prev_events`, it can't derive state
332+
# from anywhere automatically. So we need to set some state
333+
# explicitly.
334+
state_event_ids=initial_state_event_ids if index == 0 else None,
335335
historical=True,
336336
depth=inherited_depth,
337337
)
@@ -349,10 +349,9 @@ async def persist_historical_events(
349349
)
350350

351351
logger.debug(
352-
"RoomBatchSendEventRestServlet inserting event=%s, prev_event_ids=%s, state_event_ids=%s",
352+
"RoomBatchSendEventRestServlet inserting event=%s, prev_event_ids=%s",
353353
event,
354354
prev_event_ids,
355-
state_event_ids,
356355
)
357356

358357
events_to_persist.append((event, context))
@@ -382,12 +381,12 @@ async def handle_batch_of_events(
382381
room_id: str,
383382
batch_id_to_connect_to: str,
384383
inherited_depth: int,
385-
state_event_ids: List[str],
384+
initial_state_event_ids: List[str],
386385
app_service_requester: Requester,
387386
) -> Tuple[List[str], str]:
388387
"""
389-
Handles creating and persisting all of the historical events as well
390-
as insertion and batch meta events to make the batch navigable in the DAG.
388+
Handles creating and persisting all of the historical events as well as
389+
insertion and batch meta events to make the batch navigable in the DAG.
391390
392391
Args:
393392
events_to_create: List of historical events to create in JSON
@@ -397,13 +396,13 @@ async def handle_batch_of_events(
397396
want this batch to connect to.
398397
inherited_depth: The depth to create the events at (you will
399398
probably by calling inherit_depth_from_prev_ids(...)).
400-
state_event_ids:
401-
The full state at a given event. We share the same state across the whole
402-
historical batch. The state events will be stripped down to only what's
403-
necessary to auth a given event and set as the auth_event_ids. For
404-
insertion events, we will add all of these state events as the explicit
405-
state so the rest of the historical batch can inherit the same state and
406-
state_group.
399+
initial_state_event_ids:
400+
This is used to set explicit state for the insertion event at
401+
the start of the historical batch since it's floating with no
402+
prev_events to derive state from automatically. This should
403+
probably be the state from the `prev_event` defined by
404+
`/batch_send?prev_event_id=$abc` plus the outcome of
405+
`persist_state_events_at_start`
407406
app_service_requester: The requester of an application service.
408407
409408
Returns:
@@ -449,7 +448,7 @@ async def handle_batch_of_events(
449448
events_to_create=events_to_create,
450449
room_id=room_id,
451450
inherited_depth=inherited_depth,
452-
state_event_ids=state_event_ids,
451+
initial_state_event_ids=initial_state_event_ids,
453452
app_service_requester=app_service_requester,
454453
)
455454

synapse/rest/client/room_batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ async def on_POST(
148148
await self.room_batch_handler.persist_state_events_at_start(
149149
state_events_at_start=body["state_events_at_start"],
150150
room_id=room_id,
151-
initial_state_ids_at_event=state_event_ids,
151+
initial_state_event_ids=state_event_ids,
152152
app_service_requester=requester,
153153
)
154154
)

0 commit comments

Comments
 (0)