2016-03-01 14:49:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2015 OpenMarket 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.
|
|
|
|
|
|
|
|
from synapse.http.servlet import parse_integer, parse_string
|
|
|
|
from synapse.http.server import request_handler, finish_request
|
2016-04-21 15:33:05 +00:00
|
|
|
from synapse.replication.pusher_resource import PusherResource
|
2016-06-02 14:20:15 +00:00
|
|
|
from synapse.replication.presence_resource import PresenceResource
|
2016-11-22 17:45:44 +00:00
|
|
|
from synapse.replication.expire_cache import ExpireCacheResource
|
2016-09-23 14:31:47 +00:00
|
|
|
from synapse.api.errors import SynapseError
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
from twisted.web.resource import Resource
|
|
|
|
from twisted.web.server import NOT_DONE_YET
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
import ujson as json
|
|
|
|
|
|
|
|
import collections
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
REPLICATION_PREFIX = "/_synapse/replication"
|
|
|
|
|
|
|
|
STREAM_NAMES = (
|
|
|
|
("events",),
|
|
|
|
("presence",),
|
|
|
|
("typing",),
|
|
|
|
("receipts",),
|
|
|
|
("user_account_data", "room_account_data", "tag_account_data",),
|
|
|
|
("backfill",),
|
2016-03-02 17:26:20 +00:00
|
|
|
("push_rules",),
|
2016-03-15 17:01:43 +00:00
|
|
|
("pushers",),
|
2016-08-15 10:16:45 +00:00
|
|
|
("caches",),
|
2016-08-31 09:38:58 +00:00
|
|
|
("to_device",),
|
2016-09-15 10:47:23 +00:00
|
|
|
("public_rooms",),
|
2016-11-16 17:34:44 +00:00
|
|
|
("federation",),
|
2016-03-01 14:49:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ReplicationResource(Resource):
|
|
|
|
"""
|
|
|
|
HTTP endpoint for extracting data from synapse.
|
|
|
|
|
|
|
|
The streams of data returned by the endpoint are controlled by the
|
|
|
|
parameters given to the API. To return a given stream pass a query
|
|
|
|
parameter with a position in the stream to return data from or the
|
|
|
|
special value "-1" to return data from the start of the stream.
|
|
|
|
|
|
|
|
If there is no data for any of the supplied streams after the given
|
|
|
|
position then the request will block until there is data for one
|
|
|
|
of the streams. This allows clients to long-poll this API.
|
|
|
|
|
|
|
|
The possible streams are:
|
|
|
|
|
|
|
|
* "streams": A special stream returing the positions of other streams.
|
|
|
|
* "events": The new events seen on the server.
|
|
|
|
* "presence": Presence updates.
|
|
|
|
* "typing": Typing updates.
|
|
|
|
* "receipts": Receipt updates.
|
|
|
|
* "user_account_data": Top-level per user account data.
|
|
|
|
* "room_account_data: Per room per user account data.
|
|
|
|
* "tag_account_data": Per room per user tags.
|
|
|
|
* "backfill": Old events that have been backfilled from other servers.
|
2016-03-02 17:26:20 +00:00
|
|
|
* "push_rules": Per user changes to push rules.
|
2016-03-15 17:01:43 +00:00
|
|
|
* "pushers": Per user changes to their pushers.
|
2016-08-15 10:16:45 +00:00
|
|
|
* "caches": Cache invalidations.
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
The API takes two additional query parameters:
|
|
|
|
|
|
|
|
* "timeout": How long to wait before returning an empty response.
|
|
|
|
* "limit": The maximum number of rows to return for the selected streams.
|
|
|
|
|
|
|
|
The response is a JSON object with keys for each stream with updates. Under
|
|
|
|
each key is a JSON object with:
|
|
|
|
|
2016-03-25 23:38:19 +00:00
|
|
|
* "position": The current position of the stream.
|
2016-03-01 14:49:41 +00:00
|
|
|
* "field_names": The names of the fields in each row.
|
|
|
|
* "rows": The updates as an array of arrays.
|
|
|
|
|
|
|
|
There are a number of ways this API could be used:
|
|
|
|
|
|
|
|
1) To replicate the contents of the backing database to another database.
|
|
|
|
2) To be notified when the contents of a shared backing database changes.
|
|
|
|
3) To "tail" the activity happening on a server for debugging.
|
|
|
|
|
|
|
|
In the first case the client would track all of the streams and store it's
|
|
|
|
own copy of the data.
|
|
|
|
|
|
|
|
In the second case the client might theoretically just be able to follow
|
|
|
|
the "streams" stream to track where the other streams are. However in
|
|
|
|
practise it will probably need to get the contents of the streams in
|
|
|
|
order to expire the any in-memory caches. Whether it gets the contents
|
|
|
|
of the streams from this replication API or directly from the backing
|
|
|
|
store is a matter of taste.
|
|
|
|
|
|
|
|
In the third case the client would use the "streams" stream to find what
|
|
|
|
streams are available and their current positions. Then it can start
|
|
|
|
long-polling this replication API for new data on those streams.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
Resource.__init__(self) # Resource is old-style, so no super()
|
|
|
|
|
|
|
|
self.version_string = hs.version_string
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.sources = hs.get_event_sources()
|
2016-05-16 17:56:37 +00:00
|
|
|
self.presence_handler = hs.get_presence_handler()
|
2016-05-17 14:58:46 +00:00
|
|
|
self.typing_handler = hs.get_typing_handler()
|
2016-11-16 17:34:44 +00:00
|
|
|
self.federation_sender = hs.get_federation_sender()
|
2016-03-01 14:49:41 +00:00
|
|
|
self.notifier = hs.notifier
|
2016-04-28 09:57:49 +00:00
|
|
|
self.clock = hs.get_clock()
|
2016-11-16 17:34:44 +00:00
|
|
|
self.config = hs.get_config()
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-04-21 15:33:05 +00:00
|
|
|
self.putChild("remove_pushers", PusherResource(hs))
|
2016-06-02 14:20:15 +00:00
|
|
|
self.putChild("syncing_users", PresenceResource(hs))
|
2016-11-22 17:45:44 +00:00
|
|
|
self.putChild("expire_cache", ExpireCacheResource(hs))
|
2016-04-21 15:33:05 +00:00
|
|
|
|
2016-03-01 14:49:41 +00:00
|
|
|
def render_GET(self, request):
|
|
|
|
self._async_render_GET(request)
|
|
|
|
return NOT_DONE_YET
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def current_replication_token(self):
|
|
|
|
stream_token = yield self.sources.get_current_token()
|
|
|
|
backfill_token = yield self.store.get_current_backfill_token()
|
2016-03-02 17:26:20 +00:00
|
|
|
push_rules_token, room_stream_token = self.store.get_push_rules_stream_token()
|
2016-03-15 17:01:43 +00:00
|
|
|
pushers_token = self.store.get_pushers_stream_token()
|
2016-08-15 10:16:45 +00:00
|
|
|
caches_token = self.store.get_cache_stream_token()
|
2016-09-15 10:47:23 +00:00
|
|
|
public_rooms_token = self.store.get_current_public_room_stream_id()
|
2016-11-16 17:34:44 +00:00
|
|
|
federation_token = self.federation_sender.get_current_token()
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
defer.returnValue(_ReplicationToken(
|
2016-03-02 17:26:20 +00:00
|
|
|
room_stream_token,
|
2016-03-01 14:49:41 +00:00
|
|
|
int(stream_token.presence_key),
|
|
|
|
int(stream_token.typing_key),
|
|
|
|
int(stream_token.receipt_key),
|
|
|
|
int(stream_token.account_data_key),
|
|
|
|
backfill_token,
|
2016-03-02 17:26:20 +00:00
|
|
|
push_rules_token,
|
2016-03-15 17:01:43 +00:00
|
|
|
pushers_token,
|
2016-08-30 15:51:36 +00:00
|
|
|
0, # State stream is no longer a thing
|
2016-08-15 10:16:45 +00:00
|
|
|
caches_token,
|
2016-08-31 09:38:58 +00:00
|
|
|
int(stream_token.to_device_key),
|
2016-09-15 10:47:23 +00:00
|
|
|
int(public_rooms_token),
|
2016-11-16 17:34:44 +00:00
|
|
|
int(federation_token),
|
2016-03-01 14:49:41 +00:00
|
|
|
))
|
|
|
|
|
2016-04-28 09:57:49 +00:00
|
|
|
@request_handler()
|
2016-03-01 14:49:41 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _async_render_GET(self, request):
|
|
|
|
limit = parse_integer(request, "limit", 100)
|
|
|
|
timeout = parse_integer(request, "timeout", 10 * 1000)
|
|
|
|
|
|
|
|
request.setHeader(b"Content-Type", b"application/json")
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
request_streams = {
|
|
|
|
name: parse_integer(request, name)
|
|
|
|
for names in STREAM_NAMES for name in names
|
|
|
|
}
|
|
|
|
request_streams["streams"] = parse_string(request, "streams")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-11-23 10:40:44 +00:00
|
|
|
federation_ack = parse_integer(request, "federation_ack", None)
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
def replicate():
|
2016-11-23 10:40:44 +00:00
|
|
|
return self.replicate(
|
|
|
|
request_streams, limit,
|
|
|
|
federation_ack=federation_ack
|
|
|
|
)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
writer = yield self.notifier.wait_for_replication(replicate, timeout)
|
|
|
|
result = writer.finish()
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-05-05 09:29:21 +00:00
|
|
|
for stream_name, stream_content in result.items():
|
|
|
|
logger.info(
|
|
|
|
"Replicating %d rows of %s from %s -> %s",
|
|
|
|
len(stream_content["rows"]),
|
|
|
|
stream_name,
|
|
|
|
request_streams.get(stream_name),
|
2016-05-13 10:54:44 +00:00
|
|
|
stream_content["position"],
|
2016-05-05 09:29:21 +00:00
|
|
|
)
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
request.write(json.dumps(result, ensure_ascii=False))
|
|
|
|
finish_request(request)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
@defer.inlineCallbacks
|
2016-11-23 10:40:44 +00:00
|
|
|
def replicate(self, request_streams, limit, federation_ack=None):
|
2016-04-05 10:23:57 +00:00
|
|
|
writer = _Writer()
|
|
|
|
current_token = yield self.current_replication_token()
|
2016-09-09 13:05:01 +00:00
|
|
|
logger.debug("Replicating up to %r", current_token)
|
2016-04-05 10:23:57 +00:00
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if limit == 0:
|
|
|
|
raise SynapseError(400, "Limit cannot be 0")
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
yield self.account_data(writer, current_token, limit, request_streams)
|
|
|
|
yield self.events(writer, current_token, limit, request_streams)
|
|
|
|
# TODO: implement limit
|
|
|
|
yield self.presence(writer, current_token, request_streams)
|
|
|
|
yield self.typing(writer, current_token, request_streams)
|
|
|
|
yield self.receipts(writer, current_token, limit, request_streams)
|
|
|
|
yield self.push_rules(writer, current_token, limit, request_streams)
|
|
|
|
yield self.pushers(writer, current_token, limit, request_streams)
|
2016-08-15 10:16:45 +00:00
|
|
|
yield self.caches(writer, current_token, limit, request_streams)
|
2016-08-31 09:38:58 +00:00
|
|
|
yield self.to_device(writer, current_token, limit, request_streams)
|
2016-09-15 10:47:23 +00:00
|
|
|
yield self.public_rooms(writer, current_token, limit, request_streams)
|
2016-11-23 10:40:44 +00:00
|
|
|
self.federation(writer, current_token, limit, request_streams, federation_ack)
|
2016-04-05 10:23:57 +00:00
|
|
|
self.streams(writer, current_token, request_streams)
|
|
|
|
|
2016-09-09 13:05:01 +00:00
|
|
|
logger.debug("Replicated %d rows", writer.total)
|
2016-09-23 14:31:47 +00:00
|
|
|
defer.returnValue(writer)
|
2016-04-05 10:23:57 +00:00
|
|
|
|
|
|
|
def streams(self, writer, current_token, request_streams):
|
|
|
|
request_token = request_streams.get("streams")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
streams = []
|
|
|
|
|
|
|
|
if request_token is not None:
|
|
|
|
if request_token == "-1":
|
|
|
|
for names, position in zip(STREAM_NAMES, current_token):
|
|
|
|
streams.extend((name, position) for name in names)
|
|
|
|
else:
|
|
|
|
items = zip(
|
|
|
|
STREAM_NAMES,
|
|
|
|
current_token,
|
|
|
|
_ReplicationToken(request_token)
|
|
|
|
)
|
|
|
|
for names, current_id, last_id in items:
|
|
|
|
if last_id < current_id:
|
|
|
|
streams.extend((name, current_id) for name in names)
|
|
|
|
|
|
|
|
if streams:
|
|
|
|
writer.write_header_and_rows(
|
|
|
|
"streams", streams, ("name", "position"),
|
|
|
|
position=str(current_token)
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def events(self, writer, current_token, limit, request_streams):
|
|
|
|
request_events = request_streams.get("events")
|
|
|
|
request_backfill = request_streams.get("backfill")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
if request_events is not None or request_backfill is not None:
|
2016-10-11 12:57:22 +00:00
|
|
|
if request_events is None:
|
2016-03-01 14:49:41 +00:00
|
|
|
request_events = current_token.events
|
|
|
|
if request_backfill is None:
|
|
|
|
request_backfill = current_token.backfill
|
2016-09-23 14:31:47 +00:00
|
|
|
|
|
|
|
no_new_tokens = (
|
|
|
|
request_events == current_token.events
|
|
|
|
and request_backfill == current_token.backfill
|
|
|
|
)
|
|
|
|
if no_new_tokens:
|
|
|
|
return
|
|
|
|
|
2016-03-31 09:33:02 +00:00
|
|
|
res = yield self.store.get_all_new_events(
|
2016-03-01 14:49:41 +00:00
|
|
|
request_backfill, request_events,
|
|
|
|
current_token.backfill, current_token.events,
|
|
|
|
limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
|
|
|
|
upto_events_token = _position_from_rows(
|
|
|
|
res.new_forward_events, current_token.events
|
|
|
|
)
|
|
|
|
|
|
|
|
upto_backfill_token = _position_from_rows(
|
|
|
|
res.new_backfill_events, current_token.backfill
|
|
|
|
)
|
|
|
|
|
|
|
|
if request_events != upto_events_token:
|
|
|
|
writer.write_header_and_rows("events", res.new_forward_events, (
|
|
|
|
"position", "internal", "json", "state_group"
|
|
|
|
), position=upto_events_token)
|
|
|
|
|
|
|
|
if request_backfill != upto_backfill_token:
|
|
|
|
writer.write_header_and_rows("backfill", res.new_backfill_events, (
|
|
|
|
"position", "internal", "json", "state_group",
|
|
|
|
), position=upto_backfill_token)
|
|
|
|
|
2016-03-30 16:19:56 +00:00
|
|
|
writer.write_header_and_rows(
|
2016-03-31 09:33:02 +00:00
|
|
|
"forward_ex_outliers", res.forward_ex_outliers,
|
2016-09-23 14:31:47 +00:00
|
|
|
("position", "event_id", "state_group"),
|
2016-03-30 16:19:56 +00:00
|
|
|
)
|
|
|
|
writer.write_header_and_rows(
|
2016-03-31 09:33:02 +00:00
|
|
|
"backward_ex_outliers", res.backward_ex_outliers,
|
2016-09-23 14:31:47 +00:00
|
|
|
("position", "event_id", "state_group"),
|
2016-03-30 16:19:56 +00:00
|
|
|
)
|
|
|
|
writer.write_header_and_rows(
|
2016-09-23 14:31:47 +00:00
|
|
|
"state_resets", res.state_resets, ("position",),
|
2016-03-30 16:19:56 +00:00
|
|
|
)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def presence(self, writer, current_token, request_streams):
|
2016-03-01 14:49:41 +00:00
|
|
|
current_position = current_token.presence
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
request_presence = request_streams.get("presence")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if request_presence is not None and request_presence != current_position:
|
2016-03-01 14:49:41 +00:00
|
|
|
presence_rows = yield self.presence_handler.get_all_presence_updates(
|
|
|
|
request_presence, current_position
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(presence_rows, current_position)
|
2016-03-01 14:49:41 +00:00
|
|
|
writer.write_header_and_rows("presence", presence_rows, (
|
|
|
|
"position", "user_id", "state", "last_active_ts",
|
|
|
|
"last_federation_update_ts", "last_user_sync_ts",
|
|
|
|
"status_msg", "currently_active",
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def typing(self, writer, current_token, request_streams):
|
2016-09-09 15:44:26 +00:00
|
|
|
current_position = current_token.typing
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
request_typing = request_streams.get("typing")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if request_typing is not None and request_typing != current_position:
|
2016-09-09 15:44:26 +00:00
|
|
|
# If they have a higher token than current max, we can assume that
|
|
|
|
# they had been talking to a previous instance of the master. Since
|
|
|
|
# we reset the token on restart, the best (but hacky) thing we can
|
|
|
|
# do is to simply resend down all the typing notifications.
|
|
|
|
if request_typing > current_position:
|
|
|
|
request_typing = 0
|
|
|
|
|
2016-03-01 14:49:41 +00:00
|
|
|
typing_rows = yield self.typing_handler.get_all_typing_updates(
|
|
|
|
request_typing, current_position
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(typing_rows, current_position)
|
2016-03-01 14:49:41 +00:00
|
|
|
writer.write_header_and_rows("typing", typing_rows, (
|
|
|
|
"position", "room_id", "typing"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def receipts(self, writer, current_token, limit, request_streams):
|
2016-03-01 14:49:41 +00:00
|
|
|
current_position = current_token.receipts
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
request_receipts = request_streams.get("receipts")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if request_receipts is not None and request_receipts != current_position:
|
2016-03-01 14:49:41 +00:00
|
|
|
receipts_rows = yield self.store.get_all_updated_receipts(
|
|
|
|
request_receipts, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(receipts_rows, current_position)
|
2016-03-01 14:49:41 +00:00
|
|
|
writer.write_header_and_rows("receipts", receipts_rows, (
|
|
|
|
"position", "room_id", "receipt_type", "user_id", "event_id", "data"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def account_data(self, writer, current_token, limit, request_streams):
|
2016-03-01 14:49:41 +00:00
|
|
|
current_position = current_token.account_data
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
user_account_data = request_streams.get("user_account_data")
|
|
|
|
room_account_data = request_streams.get("room_account_data")
|
|
|
|
tag_account_data = request_streams.get("tag_account_data")
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
if user_account_data is not None or room_account_data is not None:
|
|
|
|
if user_account_data is None:
|
|
|
|
user_account_data = current_position
|
|
|
|
if room_account_data is None:
|
|
|
|
room_account_data = current_position
|
2016-09-23 14:31:47 +00:00
|
|
|
|
|
|
|
no_new_tokens = (
|
|
|
|
user_account_data == current_position
|
|
|
|
and room_account_data == current_position
|
|
|
|
)
|
|
|
|
if no_new_tokens:
|
|
|
|
return
|
|
|
|
|
2016-03-01 14:49:41 +00:00
|
|
|
user_rows, room_rows = yield self.store.get_all_updated_account_data(
|
|
|
|
user_account_data, room_account_data, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
|
|
|
|
upto_users_token = _position_from_rows(user_rows, current_position)
|
|
|
|
upto_rooms_token = _position_from_rows(room_rows, current_position)
|
|
|
|
|
2016-03-01 14:49:41 +00:00
|
|
|
writer.write_header_and_rows("user_account_data", user_rows, (
|
|
|
|
"position", "user_id", "type", "content"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_users_token)
|
2016-03-01 14:49:41 +00:00
|
|
|
writer.write_header_and_rows("room_account_data", room_rows, (
|
|
|
|
"position", "user_id", "room_id", "type", "content"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_rooms_token)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
if tag_account_data is not None:
|
|
|
|
tag_rows = yield self.store.get_all_updated_tags(
|
|
|
|
tag_account_data, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_tag_token = _position_from_rows(tag_rows, current_position)
|
2016-03-01 14:49:41 +00:00
|
|
|
writer.write_header_and_rows("tag_account_data", tag_rows, (
|
|
|
|
"position", "user_id", "room_id", "tags"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_tag_token)
|
2016-03-01 14:49:41 +00:00
|
|
|
|
2016-03-02 17:26:20 +00:00
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def push_rules(self, writer, current_token, limit, request_streams):
|
2016-03-02 17:26:20 +00:00
|
|
|
current_position = current_token.push_rules
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
push_rules = request_streams.get("push_rules")
|
2016-03-02 17:26:20 +00:00
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if push_rules is not None and push_rules != current_position:
|
2016-03-02 17:26:20 +00:00
|
|
|
rows = yield self.store.get_all_push_rule_updates(
|
|
|
|
push_rules, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(rows, current_position)
|
2016-03-02 17:26:20 +00:00
|
|
|
writer.write_header_and_rows("push_rules", rows, (
|
2016-03-04 16:15:23 +00:00
|
|
|
"position", "event_stream_ordering", "user_id", "rule_id", "op",
|
2016-03-02 17:26:20 +00:00
|
|
|
"priority_class", "priority", "conditions", "actions"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-03-02 17:26:20 +00:00
|
|
|
|
2016-03-15 17:01:43 +00:00
|
|
|
@defer.inlineCallbacks
|
2016-04-05 10:23:57 +00:00
|
|
|
def pushers(self, writer, current_token, limit, request_streams):
|
2016-03-15 17:01:43 +00:00
|
|
|
current_position = current_token.pushers
|
|
|
|
|
2016-04-05 10:23:57 +00:00
|
|
|
pushers = request_streams.get("pushers")
|
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if pushers is not None and pushers != current_position:
|
2016-03-15 17:01:43 +00:00
|
|
|
updated, deleted = yield self.store.get_all_updated_pushers(
|
|
|
|
pushers, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(updated, current_position)
|
2016-03-15 17:01:43 +00:00
|
|
|
writer.write_header_and_rows("pushers", updated, (
|
|
|
|
"position", "user_id", "access_token", "profile_tag", "kind",
|
|
|
|
"app_id", "app_display_name", "device_display_name", "pushkey",
|
|
|
|
"ts", "lang", "data"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-04-21 15:33:05 +00:00
|
|
|
writer.write_header_and_rows("deleted_pushers", deleted, (
|
2016-03-15 17:01:43 +00:00
|
|
|
"position", "user_id", "app_id", "pushkey"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-03-15 17:01:43 +00:00
|
|
|
|
2016-08-15 10:16:45 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def caches(self, writer, current_token, limit, request_streams):
|
|
|
|
current_position = current_token.caches
|
|
|
|
|
|
|
|
caches = request_streams.get("caches")
|
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if caches is not None and caches != current_position:
|
2016-08-15 10:16:45 +00:00
|
|
|
updated_caches = yield self.store.get_all_updated_caches(
|
|
|
|
caches, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(updated_caches, current_position)
|
2016-08-15 10:16:45 +00:00
|
|
|
writer.write_header_and_rows("caches", updated_caches, (
|
|
|
|
"position", "cache_func", "keys", "invalidation_ts"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-08-15 10:16:45 +00:00
|
|
|
|
2016-08-31 09:38:58 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def to_device(self, writer, current_token, limit, request_streams):
|
|
|
|
current_position = current_token.to_device
|
|
|
|
|
|
|
|
to_device = request_streams.get("to_device")
|
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if to_device is not None and to_device != current_position:
|
2016-08-31 09:38:58 +00:00
|
|
|
to_device_rows = yield self.store.get_all_new_device_messages(
|
|
|
|
to_device, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(to_device_rows, current_position)
|
2016-08-31 09:38:58 +00:00
|
|
|
writer.write_header_and_rows("to_device", to_device_rows, (
|
2016-11-17 15:46:44 +00:00
|
|
|
"position", "entity",
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-08-31 09:38:58 +00:00
|
|
|
|
2016-09-15 10:47:23 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def public_rooms(self, writer, current_token, limit, request_streams):
|
|
|
|
current_position = current_token.public_rooms
|
|
|
|
|
|
|
|
public_rooms = request_streams.get("public_rooms")
|
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
if public_rooms is not None and public_rooms != current_position:
|
2016-09-15 10:47:23 +00:00
|
|
|
public_rooms_rows = yield self.store.get_all_new_public_rooms(
|
|
|
|
public_rooms, current_position, limit
|
|
|
|
)
|
2016-09-23 14:31:47 +00:00
|
|
|
upto_token = _position_from_rows(public_rooms_rows, current_position)
|
2016-09-15 10:47:23 +00:00
|
|
|
writer.write_header_and_rows("public_rooms", public_rooms_rows, (
|
|
|
|
"position", "room_id", "visibility"
|
2016-09-23 14:31:47 +00:00
|
|
|
), position=upto_token)
|
2016-09-15 10:47:23 +00:00
|
|
|
|
2016-11-23 10:40:44 +00:00
|
|
|
def federation(self, writer, current_token, limit, request_streams, federation_ack):
|
2016-11-16 17:34:44 +00:00
|
|
|
if self.config.send_federation:
|
|
|
|
return
|
|
|
|
|
|
|
|
current_position = current_token.federation
|
|
|
|
|
|
|
|
federation = request_streams.get("federation")
|
|
|
|
|
|
|
|
if federation is not None and federation != current_position:
|
|
|
|
federation_rows = self.federation_sender.get_replication_rows(
|
2016-11-23 10:40:44 +00:00
|
|
|
federation, limit, federation_ack=federation_ack,
|
2016-11-16 17:34:44 +00:00
|
|
|
)
|
|
|
|
upto_token = _position_from_rows(federation_rows, current_position)
|
|
|
|
writer.write_header_and_rows("federation", federation_rows, (
|
|
|
|
"position", "type", "content",
|
|
|
|
), position=upto_token)
|
|
|
|
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
class _Writer(object):
|
|
|
|
"""Writes the streams as a JSON object as the response to the request"""
|
2016-04-05 10:23:57 +00:00
|
|
|
def __init__(self):
|
2016-03-01 14:49:41 +00:00
|
|
|
self.streams = {}
|
|
|
|
self.total = 0
|
|
|
|
|
|
|
|
def write_header_and_rows(self, name, rows, fields, position=None):
|
|
|
|
if position is None:
|
2016-09-23 14:31:47 +00:00
|
|
|
if rows:
|
|
|
|
position = rows[-1][0]
|
|
|
|
else:
|
|
|
|
return
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
self.streams[name] = {
|
2016-04-27 14:38:43 +00:00
|
|
|
"position": position if type(position) is int else str(position),
|
2016-03-01 14:49:41 +00:00
|
|
|
"field_names": fields,
|
|
|
|
"rows": rows,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.total += len(rows)
|
|
|
|
|
2016-09-23 14:31:47 +00:00
|
|
|
def __nonzero__(self):
|
|
|
|
return bool(self.total)
|
|
|
|
|
2016-03-01 14:49:41 +00:00
|
|
|
def finish(self):
|
2016-04-05 10:23:57 +00:00
|
|
|
return self.streams
|
2016-03-01 14:49:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _ReplicationToken(collections.namedtuple("_ReplicationToken", (
|
|
|
|
"events", "presence", "typing", "receipts", "account_data", "backfill",
|
2016-09-15 10:47:23 +00:00
|
|
|
"push_rules", "pushers", "state", "caches", "to_device", "public_rooms",
|
2016-11-16 17:34:44 +00:00
|
|
|
"federation",
|
2016-03-01 14:49:41 +00:00
|
|
|
))):
|
|
|
|
__slots__ = []
|
|
|
|
|
|
|
|
def __new__(cls, *args):
|
|
|
|
if len(args) == 1:
|
2016-03-02 17:26:20 +00:00
|
|
|
streams = [int(value) for value in args[0].split("_")]
|
|
|
|
if len(streams) < len(cls._fields):
|
|
|
|
streams.extend([0] * (len(cls._fields) - len(streams)))
|
|
|
|
return cls(*streams)
|
2016-03-01 14:49:41 +00:00
|
|
|
else:
|
|
|
|
return super(_ReplicationToken, cls).__new__(cls, *args)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "_".join(str(value) for value in self)
|
2016-09-23 14:31:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _position_from_rows(rows, current_position):
|
|
|
|
"""Calculates a position to return for a stream. Ideally we want to return the
|
|
|
|
position of the last row, as that will be the most correct. However, if there
|
|
|
|
are no rows we fall back to using the current position to stop us from
|
|
|
|
repeatedly hitting the storage layer unncessarily thinking there are updates.
|
|
|
|
(Not all advances of the token correspond to an actual update)
|
|
|
|
|
|
|
|
We can't just always return the current position, as we often limit the
|
|
|
|
number of rows we replicate, and so the stream may lag. The assumption is
|
|
|
|
that if the storage layer returns no new rows then we are not lagging and
|
|
|
|
we are at the `current_position`.
|
|
|
|
"""
|
|
|
|
if rows:
|
|
|
|
return rows[-1][0]
|
|
|
|
return current_position
|