summary refs log tree commit diff
path: root/synapse/replication/tcp/commands.py
blob: 2098c32a77ea7b017fe2980915ecd2339a85899c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
# Copyright 2017 Vector Creations Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Defines the various valid commands

The VALID_SERVER_COMMANDS and VALID_CLIENT_COMMANDS define which commands are
allowed to be sent by which side.
"""

import logging
import platform

if platform.python_implementation() == "PyPy":
    import json
    _json_encoder = json.JSONEncoder()
else:
    import simplejson as json
    _json_encoder = json.JSONEncoder(namedtuple_as_object=False)

logger = logging.getLogger(__name__)


class Command(object):
    """The base command class.

    All subclasses must set the NAME variable which equates to the name of the
    command on the wire.

    A full command line on the wire is constructed from `NAME + " " + to_line()`

    The default implementation creates a command of form `<NAME> <data>`
    """
    NAME = None

    def __init__(self, data):
        self.data = data

    @classmethod
    def from_line(cls, line):
        """Deserialises a line from the wire into this command. `line` does not
        include the command.
        """
        return cls(line)

    def to_line(self):
        """Serialises the comamnd for the wire. Does not include the command
        prefix.
        """
        return self.data

    def get_logcontext_id(self):
        """Get a suitable string for the logcontext when processing this command"""

        # by default, we just use the command name.
        return self.NAME


class ServerCommand(Command):
    """Sent by the server on new connection and includes the server_name.

    Format::

        SERVER <server_name>
    """
    NAME = "SERVER"


class RdataCommand(Command):
    """Sent by server when a subscribed stream has an update.

    Format::

        RDATA <stream_name> <token> <row_json>

    The `<token>` may either be a numeric stream id OR "batch". The latter case
    is used to support sending multiple updates with the same stream ID. This
    is done by sending an RDATA for each row, with all but the last RDATA having
    a token of "batch" and the last having the final stream ID.

    The client should batch all incoming RDATA with a token of "batch" (per
    stream_name) until it sees an RDATA with a numeric stream ID.

    `<token>` of "batch" maps to the instance variable `token` being None.

    An example of a batched series of RDATA::

        RDATA presence batch ["@foo:example.com", "online", ...]
        RDATA presence batch ["@bar:example.com", "online", ...]
        RDATA presence 59 ["@baz:example.com", "online", ...]
    """
    NAME = "RDATA"

    def __init__(self, stream_name, token, row):
        self.stream_name = stream_name
        self.token = token
        self.row = row

    @classmethod
    def from_line(cls, line):
        stream_name, token, row_json = line.split(" ", 2)
        return cls(
            stream_name,
            None if token == "batch" else int(token),
            json.loads(row_json)
        )

    def to_line(self):
        return " ".join((
            self.stream_name,
            str(self.token) if self.token is not None else "batch",
            _json_encoder.encode(self.row),
        ))

    def get_logcontext_id(self):
        return "RDATA-" + self.stream_name


class PositionCommand(Command):
    """Sent by the server to tell the client the stream postition without
    needing to send an RDATA.

    Sent to the client after all missing updates for a stream have been sent
    to the client and they're now up to date.
    """
    NAME = "POSITION"

    def __init__(self, stream_name, token):
        self.stream_name = stream_name
        self.token = token

    @classmethod
    def from_line(cls, line):
        stream_name, token = line.split(" ", 1)
        return cls(stream_name, int(token))

    def to_line(self):
        return " ".join((self.stream_name, str(self.token),))


class ErrorCommand(Command):
    """Sent by either side if there was an ERROR. The data is a string describing
    the error.
    """
    NAME = "ERROR"


class PingCommand(Command):
    """Sent by either side as a keep alive. The data is arbitary (often timestamp)
    """
    NAME = "PING"


class NameCommand(Command):
    """Sent by client to inform the server of the client's identity. The data
    is the name
    """
    NAME = "NAME"


class ReplicateCommand(Command):
    """Sent by the client to subscribe to the stream.

    Format::

        REPLICATE <stream_name> <token>

    Where <token> may be either:
        * a numeric stream_id to stream updates from
        * "NOW" to stream all subsequent updates.

    The <stream_name> can be "ALL" to subscribe to all known streams, in which
    case the <token> must be set to "NOW", i.e.::

        REPLICATE ALL NOW
    """
    NAME = "REPLICATE"

    def __init__(self, stream_name, token):
        self.stream_name = stream_name
        self.token = token

    @classmethod
    def from_line(cls, line):
        stream_name, token = line.split(" ", 1)
        if token in ("NOW", "now"):
            token = "NOW"
        else:
            token = int(token)
        return cls(stream_name, token)

    def to_line(self):
        return " ".join((self.stream_name, str(self.token),))

    def get_logcontext_id(self):
        return "REPLICATE-" + self.stream_name


class UserSyncCommand(Command):
    """Sent by the client to inform the server that a user has started or
    stopped syncing. Used to calculate presence on the master.

    Includes a timestamp of when the last user sync was.

    Format::

        USER_SYNC <user_id> <state> <last_sync_ms>

    Where <state> is either "start" or "stop"
    """
    NAME = "USER_SYNC"

    def __init__(self, user_id, is_syncing, last_sync_ms):
        self.user_id = user_id
        self.is_syncing = is_syncing
        self.last_sync_ms = last_sync_ms

    @classmethod
    def from_line(cls, line):
        user_id, state, last_sync_ms = line.split(" ", 2)

        if state not in ("start", "end"):
            raise Exception("Invalid USER_SYNC state %r" % (state,))

        return cls(user_id, state == "start", int(last_sync_ms))

    def to_line(self):
        return " ".join((
            self.user_id, "start" if self.is_syncing else "end", str(self.last_sync_ms),
        ))


class FederationAckCommand(Command):
    """Sent by the client when it has processed up to a given point in the
    federation stream. This allows the master to drop in-memory caches of the
    federation stream.

    This must only be sent from one worker (i.e. the one sending federation)

    Format::

        FEDERATION_ACK <token>
    """
    NAME = "FEDERATION_ACK"

    def __init__(self, token):
        self.token = token

    @classmethod
    def from_line(cls, line):
        return cls(int(line))

    def to_line(self):
        return str(self.token)


class SyncCommand(Command):
    """Used for testing. The client protocol implementation allows waiting
    on a SYNC command with a specified data.
    """
    NAME = "SYNC"


class RemovePusherCommand(Command):
    """Sent by the client to request the master remove the given pusher.

    Format::

        REMOVE_PUSHER <app_id> <push_key> <user_id>
    """
    NAME = "REMOVE_PUSHER"

    def __init__(self, app_id, push_key, user_id):
        self.user_id = user_id
        self.app_id = app_id
        self.push_key = push_key

    @classmethod
    def from_line(cls, line):
        app_id, push_key, user_id = line.split(" ", 2)

        return cls(app_id, push_key, user_id)

    def to_line(self):
        return " ".join((self.app_id, self.push_key, self.user_id))


class InvalidateCacheCommand(Command):
    """Sent by the client to invalidate an upstream cache.

    THIS IS NOT RELIABLE, AND SHOULD *NOT* BE USED ACCEPT FOR THINGS THAT ARE
    NOT DISASTROUS IF WE DROP ON THE FLOOR.

    Mainly used to invalidate destination retry timing caches.

    Format::

        INVALIDATE_CACHE <cache_func> <keys_json>

    Where <keys_json> is a json list.
    """
    NAME = "INVALIDATE_CACHE"

    def __init__(self, cache_func, keys):
        self.cache_func = cache_func
        self.keys = keys

    @classmethod
    def from_line(cls, line):
        cache_func, keys_json = line.split(" ", 1)

        return cls(cache_func, json.loads(keys_json))

    def to_line(self):
        return " ".join((
            self.cache_func, _json_encoder.encode(self.keys),
        ))


class UserIpCommand(Command):
    """Sent periodically when a worker sees activity from a client.

    Format::

        USER_IP <user_id>, <access_token>, <ip>, <device_id>, <last_seen>, <user_agent>
    """
    NAME = "USER_IP"

    def __init__(self, user_id, access_token, ip, user_agent, device_id, last_seen):
        self.user_id = user_id
        self.access_token = access_token
        self.ip = ip
        self.user_agent = user_agent
        self.device_id = device_id
        self.last_seen = last_seen

    @classmethod
    def from_line(cls, line):
        user_id, jsn = line.split(" ", 1)

        access_token, ip, user_agent, device_id, last_seen = json.loads(jsn)

        return cls(
            user_id, access_token, ip, user_agent, device_id, last_seen
        )

    def to_line(self):
        return self.user_id + " " + _json_encoder.encode((
            self.access_token, self.ip, self.user_agent, self.device_id,
            self.last_seen,
        ))


# Map of command name to command type.
COMMAND_MAP = {
    cmd.NAME: cmd
    for cmd in (
        ServerCommand,
        RdataCommand,
        PositionCommand,
        ErrorCommand,
        PingCommand,
        NameCommand,
        ReplicateCommand,
        UserSyncCommand,
        FederationAckCommand,
        SyncCommand,
        RemovePusherCommand,
        InvalidateCacheCommand,
        UserIpCommand,
    )
}

# The commands the server is allowed to send
VALID_SERVER_COMMANDS = (
    ServerCommand.NAME,
    RdataCommand.NAME,
    PositionCommand.NAME,
    ErrorCommand.NAME,
    PingCommand.NAME,
    SyncCommand.NAME,
)

# The commands the client is allowed to send
VALID_CLIENT_COMMANDS = (
    NameCommand.NAME,
    ReplicateCommand.NAME,
    PingCommand.NAME,
    UserSyncCommand.NAME,
    FederationAckCommand.NAME,
    RemovePusherCommand.NAME,
    InvalidateCacheCommand.NAME,
    UserIpCommand.NAME,
    ErrorCommand.NAME,
)