mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 10:56:06 -04:00
Make event persisters periodically announce position over replication. (#8499)
Currently background proccesses stream the events stream use the "minimum persisted position" (i.e. `get_current_token()`) rather than the vector clock style tokens. This is broadly fine as it doesn't matter if the background processes lag a small amount. However, in extreme cases (i.e. SyTests) where we only write to one event persister the background processes will never make progress. This PR changes it so that the `MultiWriterIDGenerator` keeps the current position of a given instance as up to date as possible (i.e using the latest token it sees if its not in the process of persisting anything), and then periodically announces that over replication. This then allows the "minimum persisted position" to advance, albeit with a small lag.
This commit is contained in:
parent
6905f5751a
commit
8de3703d21
9 changed files with 128 additions and 34 deletions
|
@ -23,7 +23,9 @@ from prometheus_client import Counter
|
|||
from twisted.internet.protocol import Factory
|
||||
|
||||
from synapse.metrics.background_process_metrics import run_as_background_process
|
||||
from synapse.replication.tcp.commands import PositionCommand
|
||||
from synapse.replication.tcp.protocol import ServerReplicationStreamProtocol
|
||||
from synapse.replication.tcp.streams import EventsStream
|
||||
from synapse.util.metrics import Measure
|
||||
|
||||
stream_updates_counter = Counter(
|
||||
|
@ -84,6 +86,23 @@ class ReplicationStreamer:
|
|||
# Set of streams to replicate.
|
||||
self.streams = self.command_handler.get_streams_to_replicate()
|
||||
|
||||
# If we have streams then we must have redis enabled or on master
|
||||
assert (
|
||||
not self.streams
|
||||
or hs.config.redis.redis_enabled
|
||||
or not hs.config.worker.worker_app
|
||||
)
|
||||
|
||||
# If we are replicating an event stream we want to periodically check if
|
||||
# we should send updated POSITIONs. We do this as a looping call rather
|
||||
# explicitly poking when the position advances (without new data to
|
||||
# replicate) to reduce replication traffic (otherwise each writer would
|
||||
# likely send a POSITION for each new event received over replication).
|
||||
#
|
||||
# Note that if the position hasn't advanced then we won't send anything.
|
||||
if any(EventsStream.NAME == s.NAME for s in self.streams):
|
||||
self.clock.looping_call(self.on_notifier_poke, 1000)
|
||||
|
||||
def on_notifier_poke(self):
|
||||
"""Checks if there is actually any new data and sends it to the
|
||||
connections if there are.
|
||||
|
@ -91,7 +110,7 @@ class ReplicationStreamer:
|
|||
This should get called each time new data is available, even if it
|
||||
is currently being executed, so that nothing gets missed
|
||||
"""
|
||||
if not self.command_handler.connected():
|
||||
if not self.command_handler.connected() or not self.streams:
|
||||
# Don't bother if nothing is listening. We still need to advance
|
||||
# the stream tokens otherwise they'll fall behind forever
|
||||
for stream in self.streams:
|
||||
|
@ -136,6 +155,8 @@ class ReplicationStreamer:
|
|||
self._replication_torture_level / 1000.0
|
||||
)
|
||||
|
||||
last_token = stream.last_token
|
||||
|
||||
logger.debug(
|
||||
"Getting stream: %s: %s -> %s",
|
||||
stream.NAME,
|
||||
|
@ -159,6 +180,30 @@ class ReplicationStreamer:
|
|||
)
|
||||
stream_updates_counter.labels(stream.NAME).inc(len(updates))
|
||||
|
||||
else:
|
||||
# The token has advanced but there is no data to
|
||||
# send, so we send a `POSITION` to inform other
|
||||
# workers of the updated position.
|
||||
if stream.NAME == EventsStream.NAME:
|
||||
# XXX: We only do this for the EventStream as it
|
||||
# turns out that e.g. account data streams share
|
||||
# their "current token" with each other, meaning
|
||||
# that it is *not* safe to send a POSITION.
|
||||
logger.info(
|
||||
"Sending position: %s -> %s",
|
||||
stream.NAME,
|
||||
current_token,
|
||||
)
|
||||
self.command_handler.send_command(
|
||||
PositionCommand(
|
||||
stream.NAME,
|
||||
self._instance_name,
|
||||
last_token,
|
||||
current_token,
|
||||
)
|
||||
)
|
||||
continue
|
||||
|
||||
# Some streams return multiple rows with the same stream IDs,
|
||||
# we need to make sure they get sent out in batches. We do
|
||||
# this by setting the current token to all but the last of
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue