matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.83k stars 2.13k forks source link

Reduce spurious replication catchup #16555

Closed erikjohnston closed 1 year ago

erikjohnston commented 1 year ago

The POSITION command includes two tokens, the "prev" and "new" tokens. The "new" token is what we should set the current token for the instance to be, and the "prev" token was where it was previously (i.e. the instance produced no rows between the "prev" and "new" tokens).

This means that we can safely relax the condition to only check for missing updates if the current token for the writer is strictly less than the "prev" token.

A common case is receiving something like POSITIONS event_persister1 events 15 16, which indicates the writer advanced their events stream token from 15 to 16 (usually as a response to another instance persisting an event with stream ID 16). Usually, the other instances would already have inferred that event_persister1 must have advanced their token, so the current_token would already be set to 16, thus triggering a lookup for missing updates between tokens 16 (cmd.new_token) and 16 (current_token), which is silly.

We also move the logging back into the loop so that we don't log in the common case.