This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Replace HTTP replication with TCP replication (Server side part) #2082
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
24d35ab
Add new storage functions for new replication
erikjohnston 7984708
Add a simple hook to wait for replication traffic
erikjohnston 1188010
Make federation send queue take the current position
erikjohnston 8da6f0b
Define the various streams we will replicate
erikjohnston 7450693
Initial TCP protocol implementation
erikjohnston 4d7fc7f
Add server side resource for tcp replication
erikjohnston e9dd837
Add functions to presence to support remote syncs
erikjohnston 3ba2859
Add tcp replication listener type and hook it up
erikjohnston 31e0fe9
Fix indentation in docs/
erikjohnston 63fcc42
Remove user from process_presence when stops syncing
erikjohnston bfcf016
Fix up docs
erikjohnston b4276a3
Add a brief list of commands to docs
erikjohnston 9d0170a
Fix up presence
erikjohnston 36d2b66
Add a timestamp to USER_SYNC command
erikjohnston 1df7c28
Use callbacks to notify tcp replication rather than deferreds
erikjohnston 0a6a966
Always advance stream tokens
erikjohnston 62b89da
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/re…
erikjohnston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
TCP Replication | ||
=============== | ||
|
||
Motivation | ||
---------- | ||
|
||
Previously the workers used an HTTP long poll mechanism to get updates from the | ||
master, which had the problem of causing a lot of duplicate work on the server. | ||
This TCP protocol replaces those APIs with the aim of increased efficiency. | ||
|
||
|
||
|
||
Overview | ||
-------- | ||
|
||
The protocol is based on fire and forget, line based commands. An example flow | ||
would be (where '>' indicates master to worker and '<' worker to master flows):: | ||
|
||
> SERVER example.com | ||
< REPLICATE events 53 | ||
> RDATA events 54 ["$foo1:bar.com", ...] | ||
> RDATA events 55 ["$foo4:bar.com", ...] | ||
|
||
The example shows the server accepting a new connection and sending its identity | ||
with the ``SERVER`` command, followed by the client asking to subscribe to the | ||
``events`` stream from the token ``53``. The server then periodically sends ``RDATA`` | ||
commands which have the format ``RDATA <stream_name> <token> <row>``, where the | ||
format of ``<row>`` is defined by the individual streams. | ||
|
||
Error reporting happens by either the client or server sending an `ERROR` | ||
command, and usually the connection will be closed. | ||
|
||
|
||
Since the protocol is a simple line based, its possible to manually connect to | ||
the server using a tool like netcat. A few things should be noted when manually | ||
using the protocol: | ||
|
||
* When subscribing to a stream using ``REPLICATE``, the special token ``NOW`` can | ||
be used to get all future updates. The special stream name ``ALL`` can be used | ||
with ``NOW`` to subscribe to all available streams. | ||
* The federation stream is only available if federation sending has been | ||
disabled on the main process. | ||
* The server will only time connections out that have sent a ``PING`` command. | ||
If a ping is sent then the connection will be closed if no further commands | ||
are receieved within 15s. Both the client and server protocol implementations | ||
will send an initial PING on connection and ensure at least one command every | ||
5s is sent (not necessarily ``PING``). | ||
* ``RDATA`` commands *usually* include a numeric token, however if the stream | ||
has multiple rows to replicate per token the server will send multiple | ||
``RDATA`` commands, with all but the last having a token of ``batch``. See | ||
the documentation on ``commands.RdataCommand`` for further details. | ||
|
||
|
||
Architecture | ||
------------ | ||
|
||
The basic structure of the protocol is line based, where the initial word of | ||
each line specifies the command. The rest of the line is parsed based on the | ||
command. For example, the `RDATA` command is defined as:: | ||
|
||
RDATA <stream_name> <token> <row_json> | ||
|
||
(Note that `<row_json>` may contains spaces, but cannot contain newlines.) | ||
|
||
Blank lines are ignored. | ||
|
||
|
||
Keep alives | ||
~~~~~~~~~~~ | ||
|
||
Both sides are expected to send at least one command every 5s or so, and | ||
should send a ``PING`` command if necessary. If either side do not receive a | ||
command within e.g. 15s then the connection should be closed. | ||
|
||
Because the server may be connected to manually using e.g. netcat, the timeouts | ||
aren't enabled until an initial ``PING`` command is seen. Both the client and | ||
server implementations below send a ``PING`` command immediately on connection to | ||
ensure the timeouts are enabled. | ||
|
||
This ensures that both sides can quickly realize if the tcp connection has gone | ||
and handle the situation appropriately. | ||
|
||
|
||
Start up | ||
~~~~~~~~ | ||
|
||
When a new connection is made, the server: | ||
|
||
* Sends a ``SERVER`` command, which includes the identity of the server, allowing | ||
the client to detect if its connected to the expected server | ||
* Sends a ``PING`` command as above, to enable the client to time out connections | ||
promptly. | ||
|
||
The client: | ||
|
||
* Sends a ``NAME`` command, allowing the server to associate a human friendly | ||
name with the connection. This is optional. | ||
* Sends a ``PING`` as above | ||
* For each stream the client wishes to subscribe to it sends a ``REPLICATE`` | ||
with the stream_name and token it wants to subscribe from. | ||
* On receipt of a ``SERVER`` command, checks that the server name matches the | ||
expected server name. | ||
|
||
|
||
Error handling | ||
~~~~~~~~~~~~~~ | ||
|
||
If either side detects an error it can send an ``ERROR`` command and close the | ||
connection. | ||
|
||
If the client side loses the connection to the server it should reconnect, | ||
following the steps above. | ||
|
||
|
||
Congestion | ||
~~~~~~~~~~ | ||
|
||
If the server sends messages faster than the client can consume them the server | ||
will first buffer a (fairly large) number of commands and then disconnect the | ||
client. This ensures that we don't queue up an unbounded number of commands in | ||
memory and gives us a potential oppurtunity to squawk loudly. When/if the client | ||
recovers it can reconnect to the server and ask for missed messages. | ||
|
||
|
||
Reliability | ||
~~~~~~~~~~~ | ||
|
||
In general the replication stream should be considered an unreliable transport | ||
since e.g. commands are not resent if the connection disappears. | ||
|
||
The exception to that are the replication streams, i.e. RDATA commands, since | ||
these include tokens which can be used to restart the stream on connection | ||
errors. | ||
|
||
The client should keep track of the token in the last RDATA command received | ||
for each stream so that on reconneciton it can start streaming from the correct | ||
place. Note: not all RDATA have valid tokens due to batching. See | ||
``RdataCommand`` for more details. | ||
|
||
|
||
Example | ||
~~~~~~~ | ||
|
||
An example iteraction is shown below. Each line is prefixed with '>' or '<' to | ||
indicate which side is sending, these are *not* included on the wire:: | ||
|
||
* connection established * | ||
> SERVER localhost:8823 | ||
> PING 1490197665618 | ||
< NAME synapse.app.appservice | ||
< PING 1490197665618 | ||
< REPLICATE events 1 | ||
< REPLICATE backfill 1 | ||
< REPLICATE caches 1 | ||
> POSITION events 1 | ||
> POSITION backfill 1 | ||
> POSITION caches 1 | ||
> RDATA caches 2 ["get_user_by_id",["@01register-user:localhost:8823"],1490197670513] | ||
> RDATA events 14 ["$149019767112vOHxz:localhost:8823", | ||
"!AFDCvgApUmpdfVjIXm:localhost:8823","m.room.guest_access","",null] | ||
< PING 1490197675618 | ||
> ERROR server stopping | ||
* connection closed by server * | ||
|
||
The ``POSITION`` command sent by the server is used to set the clients position | ||
without needing to send data with the ``RDATA`` command. | ||
|
||
|
||
An example of a batched set of ``RDATA`` is:: | ||
|
||
> RDATA caches batch ["get_user_by_id",["@test:localhost:8823"],1490197670513] | ||
> RDATA caches batch ["get_user_by_id",["@test2:localhost:8823"],1490197670513] | ||
> RDATA caches batch ["get_user_by_id",["@test3:localhost:8823"],1490197670513] | ||
> RDATA caches 54 ["get_user_by_id",["@test4:localhost:8823"],1490197670513] | ||
|
||
In this case the client shouldn't advance their caches token until it sees the | ||
the last ``RDATA``. | ||
|
||
|
||
List of commands | ||
~~~~~~~~~~~~~~~~ | ||
|
||
The list of valid commands, with which side can send it: server (S) or client (C): | ||
|
||
SERVER (S) | ||
Sent at the start to identify which server the client is talking to | ||
|
||
RDATA (S) | ||
A single update in a stream | ||
|
||
POSITION (S) | ||
The position of the stream has been updated | ||
|
||
ERROR (S, C) | ||
There was an error | ||
|
||
PING (S, C) | ||
Sent periodically to ensure the connection is still alive | ||
|
||
NAME (C) | ||
Sent at the start by client to inform the server who they are | ||
|
||
REPLICATE (C) | ||
Asks the server to replicate a given stream | ||
|
||
USER_SYNC (C) | ||
A user has started or stopped syncing | ||
|
||
FEDERATION_ACK (C) | ||
Acknowledge receipt of some federation data | ||
|
||
REMOVE_PUSHER (C) | ||
Inform the server a pusher should be removed | ||
|
||
INVALIDATE_CACHE (C) | ||
Inform the server a cache should be invalidated | ||
|
||
SYNC (S, C) | ||
Used exclusively in tests | ||
|
||
|
||
See ``synapse/replication/tcp/commands.py`` for a detailed description and the | ||
format of each command. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to give a complete list of the commands here, with the command syntax, the direction of transmission, a quick summary and a reference to the section where it is explained in more detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.