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

Commit fb58611

Browse files
Refactor backfilled into specific behavior function arguments (_persist_events_and_state_updates) (#11417)
Part of #11300 Call stack: - `_persist_events_and_state_updates` (added `use_negative_stream_ordering`) - `_persist_events_txn` - `_update_room_depths_txn` (added `update_room_forward_stream_ordering`) - `_update_metadata_tables_txn` - `_store_room_members_txn` (added `inhibit_local_membership_updates`) Using keyword-only arguments (`*`) to reduce the mistakes from `backfilled` being left as a positional argument somewhere and being interpreted wrong by our new arguments.
1 parent a4521ce commit fb58611

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

changelog.d/11417.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor `backfilled` into specific behavior function arguments (`_persist_events_and_state_updates` and downstream calls).

synapse/storage/databases/main/events.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ def __init__(
124124
async def _persist_events_and_state_updates(
125125
self,
126126
events_and_contexts: List[Tuple[EventBase, EventContext]],
127+
*,
127128
current_state_for_room: Dict[str, StateMap[str]],
128129
state_delta_for_room: Dict[str, DeltaState],
129130
new_forward_extremeties: Dict[str, List[str]],
130-
backfilled: bool = False,
131+
use_negative_stream_ordering: bool = False,
132+
inhibit_local_membership_updates: bool = False,
131133
) -> None:
132134
"""Persist a set of events alongside updates to the current state and
133135
forward extremities tables.
@@ -140,7 +142,14 @@ async def _persist_events_and_state_updates(
140142
room state
141143
new_forward_extremities: Map from room_id to list of event IDs
142144
that are the new forward extremities of the room.
143-
backfilled
145+
use_negative_stream_ordering: Whether to start stream_ordering on
146+
the negative side and decrement. This should be set as True
147+
for backfilled events because backfilled events get a negative
148+
stream ordering so they don't come down incremental `/sync`.
149+
inhibit_local_membership_updates: Stop the local_current_membership
150+
from being updated by these events. This should be set to True
151+
for backfilled events because backfilled events in the past do
152+
not affect the current local state.
144153
145154
Returns:
146155
Resolves when the events have been persisted
@@ -162,7 +171,7 @@ async def _persist_events_and_state_updates(
162171
#
163172
# Note: Multiple instances of this function cannot be in flight at
164173
# the same time for the same room.
165-
if backfilled:
174+
if use_negative_stream_ordering:
166175
stream_ordering_manager = self._backfill_id_gen.get_next_mult(
167176
len(events_and_contexts)
168177
)
@@ -179,13 +188,13 @@ async def _persist_events_and_state_updates(
179188
"persist_events",
180189
self._persist_events_txn,
181190
events_and_contexts=events_and_contexts,
182-
backfilled=backfilled,
191+
inhibit_local_membership_updates=inhibit_local_membership_updates,
183192
state_delta_for_room=state_delta_for_room,
184193
new_forward_extremeties=new_forward_extremeties,
185194
)
186195
persist_event_counter.inc(len(events_and_contexts))
187196

188-
if not backfilled:
197+
if stream < 0:
189198
# backfilled events have negative stream orderings, so we don't
190199
# want to set the event_persisted_position to that.
191200
synapse.metrics.event_persisted_position.set(
@@ -319,8 +328,9 @@ def _get_prevs_before_rejected_txn(txn, batch):
319328
def _persist_events_txn(
320329
self,
321330
txn: LoggingTransaction,
331+
*,
322332
events_and_contexts: List[Tuple[EventBase, EventContext]],
323-
backfilled: bool,
333+
inhibit_local_membership_updates: bool = False,
324334
state_delta_for_room: Optional[Dict[str, DeltaState]] = None,
325335
new_forward_extremeties: Optional[Dict[str, List[str]]] = None,
326336
):
@@ -333,7 +343,10 @@ def _persist_events_txn(
333343
Args:
334344
txn
335345
events_and_contexts: events to persist
336-
backfilled: True if the events were backfilled
346+
inhibit_local_membership_updates: Stop the local_current_membership
347+
from being updated by these events. This should be set to True
348+
for backfilled events because backfilled events in the past do
349+
not affect the current local state.
337350
delete_existing True to purge existing table rows for the events
338351
from the database. This is useful when retrying due to
339352
IntegrityError.
@@ -366,9 +379,7 @@ def _persist_events_txn(
366379
events_and_contexts
367380
)
368381

369-
self._update_room_depths_txn(
370-
txn, events_and_contexts=events_and_contexts, backfilled=backfilled
371-
)
382+
self._update_room_depths_txn(txn, events_and_contexts=events_and_contexts)
372383

373384
# _update_outliers_txn filters out any events which have already been
374385
# persisted, and returns the filtered list.
@@ -401,7 +412,7 @@ def _persist_events_txn(
401412
txn,
402413
events_and_contexts=events_and_contexts,
403414
all_events_and_contexts=all_events_and_contexts,
404-
backfilled=backfilled,
415+
inhibit_local_membership_updates=inhibit_local_membership_updates,
405416
)
406417

407418
# We call this last as it assumes we've inserted the events into
@@ -1203,21 +1214,25 @@ def _update_room_depths_txn(
12031214
self,
12041215
txn,
12051216
events_and_contexts: List[Tuple[EventBase, EventContext]],
1206-
backfilled: bool,
12071217
):
12081218
"""Update min_depth for each room
12091219
12101220
Args:
12111221
txn (twisted.enterprise.adbapi.Connection): db connection
12121222
events_and_contexts (list[(EventBase, EventContext)]): events
12131223
we are persisting
1214-
backfilled (bool): True if the events were backfilled
12151224
"""
12161225
depth_updates: Dict[str, int] = {}
12171226
for event, context in events_and_contexts:
12181227
# Remove the any existing cache entries for the event_ids
12191228
txn.call_after(self.store._invalidate_get_event_cache, event.event_id)
1220-
if not backfilled:
1229+
# Then update the `stream_ordering` position to mark the latest
1230+
# event as the front of the room. This should not be done for
1231+
# backfilled events because backfilled events have negative
1232+
# stream_ordering and happened in the past so we know that we don't
1233+
# need to update the stream_ordering tip/front for the room.
1234+
assert event.internal_metadata.stream_ordering is not None
1235+
if event.internal_metadata.stream_ordering >= 0:
12211236
txn.call_after(
12221237
self.store._events_stream_cache.entity_has_changed,
12231238
event.room_id,
@@ -1430,7 +1445,12 @@ def _store_rejected_events_txn(self, txn, events_and_contexts):
14301445
return [ec for ec in events_and_contexts if ec[0] not in to_remove]
14311446

14321447
def _update_metadata_tables_txn(
1433-
self, txn, events_and_contexts, all_events_and_contexts, backfilled
1448+
self,
1449+
txn,
1450+
*,
1451+
events_and_contexts,
1452+
all_events_and_contexts,
1453+
inhibit_local_membership_updates: bool = False,
14341454
):
14351455
"""Update all the miscellaneous tables for new events
14361456
@@ -1442,7 +1462,10 @@ def _update_metadata_tables_txn(
14421462
events that we were going to persist. This includes events
14431463
we've already persisted, etc, that wouldn't appear in
14441464
events_and_context.
1445-
backfilled (bool): True if the events were backfilled
1465+
inhibit_local_membership_updates: Stop the local_current_membership
1466+
from being updated by these events. This should be set to True
1467+
for backfilled events because backfilled events in the past do
1468+
not affect the current local state.
14461469
"""
14471470

14481471
# Insert all the push actions into the event_push_actions table.
@@ -1516,7 +1539,7 @@ def _update_metadata_tables_txn(
15161539
for event, _ in events_and_contexts
15171540
if event.type == EventTypes.Member
15181541
],
1519-
backfilled=backfilled,
1542+
inhibit_local_membership_updates=inhibit_local_membership_updates,
15201543
)
15211544

15221545
# Insert event_reference_hashes table.
@@ -1643,8 +1666,19 @@ def _store_event_reference_hashes_txn(self, txn, events):
16431666
txn, table="event_reference_hashes", values=vals
16441667
)
16451668

1646-
def _store_room_members_txn(self, txn, events, backfilled):
1647-
"""Store a room member in the database."""
1669+
def _store_room_members_txn(
1670+
self, txn, events, *, inhibit_local_membership_updates: bool = False
1671+
):
1672+
"""
1673+
Store a room member in the database.
1674+
Args:
1675+
txn: The transaction to use.
1676+
events: List of events to store.
1677+
inhibit_local_membership_updates: Stop the local_current_membership
1678+
from being updated by these events. This should be set to True
1679+
for backfilled events because backfilled events in the past do
1680+
not affect the current local state.
1681+
"""
16481682

16491683
def non_null_str_or_none(val: Any) -> Optional[str]:
16501684
return val if isinstance(val, str) and "\u0000" not in val else None
@@ -1687,7 +1721,7 @@ def non_null_str_or_none(val: Any) -> Optional[str]:
16871721
# band membership", like a remote invite or a rejection of a remote invite.
16881722
if (
16891723
self.is_mine_id(event.state_key)
1690-
and not backfilled
1724+
and not inhibit_local_membership_updates
16911725
and event.internal_metadata.is_outlier()
16921726
and event.internal_metadata.is_out_of_band_membership()
16931727
):

synapse/storage/persist_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ async def _persist_event_batch(
583583
current_state_for_room=current_state_for_room,
584584
state_delta_for_room=state_delta_for_room,
585585
new_forward_extremeties=new_forward_extremeties,
586-
backfilled=backfilled,
586+
use_negative_stream_ordering=backfilled,
587+
inhibit_local_membership_updates=backfilled,
587588
)
588589

589590
await self._handle_potentially_left_users(potentially_left_users)

0 commit comments

Comments
 (0)