forked from MirrorHub/synapse
Merge pull request #1089 from matrix-org/markjh/direct_to_device_stream
Track the max device stream_id in a separate table,
This commit is contained in:
commit
6b54fa81de
4 changed files with 38 additions and 4 deletions
|
@ -23,7 +23,7 @@ class SlavedDeviceInboxStore(BaseSlavedStore):
|
|||
def __init__(self, db_conn, hs):
|
||||
super(SlavedDeviceInboxStore, self).__init__(db_conn, hs)
|
||||
self._device_inbox_id_gen = SlavedIdTracker(
|
||||
db_conn, "device_inbox", "stream_id",
|
||||
db_conn, "device_max_stream_id", "stream_id",
|
||||
)
|
||||
self._device_inbox_stream_cache = StreamChangeCache(
|
||||
"DeviceInboxStreamChangeCache",
|
||||
|
|
|
@ -111,7 +111,7 @@ class DataStore(RoomMemberStore, RoomStore,
|
|||
db_conn, "presence_stream", "stream_id"
|
||||
)
|
||||
self._device_inbox_id_gen = StreamIdGenerator(
|
||||
db_conn, "device_inbox", "stream_id"
|
||||
db_conn, "device_max_stream_id", "stream_id"
|
||||
)
|
||||
|
||||
self._transaction_id_gen = IdGenerator(db_conn, "sent_transactions", "id")
|
||||
|
|
|
@ -130,6 +130,13 @@ class DeviceInboxStore(SQLBaseStore):
|
|||
|
||||
def _add_messages_to_local_device_inbox_txn(self, txn, stream_id,
|
||||
messages_by_user_then_device):
|
||||
sql = (
|
||||
"UPDATE device_max_stream_id"
|
||||
" SET stream_id = ?"
|
||||
" WHERE stream_id < ?"
|
||||
)
|
||||
txn.execute(sql, (stream_id, stream_id))
|
||||
|
||||
local_by_user_then_device = {}
|
||||
for user_id, messages_by_device in messages_by_user_then_device.items():
|
||||
messages_json_for_user = {}
|
||||
|
@ -148,6 +155,8 @@ class DeviceInboxStore(SQLBaseStore):
|
|||
device = row[0]
|
||||
messages_json_for_user[device] = message_json
|
||||
else:
|
||||
if not devices:
|
||||
continue
|
||||
sql = (
|
||||
"SELECT device_id FROM devices"
|
||||
" WHERE user_id = ? AND device_id IN ("
|
||||
|
@ -164,8 +173,12 @@ class DeviceInboxStore(SQLBaseStore):
|
|||
message_json = ujson.dumps(messages_by_device[device])
|
||||
messages_json_for_user[device] = message_json
|
||||
|
||||
if messages_json_for_user:
|
||||
local_by_user_then_device[user_id] = messages_json_for_user
|
||||
|
||||
if not local_by_user_then_device:
|
||||
return
|
||||
|
||||
sql = (
|
||||
"INSERT INTO device_inbox"
|
||||
" (user_id, device_id, stream_id, message_json)"
|
||||
|
@ -301,7 +314,7 @@ class DeviceInboxStore(SQLBaseStore):
|
|||
has_changed = self._device_federation_outbox_stream_cache.has_entity_changed(
|
||||
destination, last_stream_id
|
||||
)
|
||||
if not has_changed:
|
||||
if not has_changed or last_stream_id == current_stream_id:
|
||||
return defer.succeed(([], current_stream_id))
|
||||
|
||||
def get_new_messages_for_remote_destination_txn(txn):
|
||||
|
|
21
synapse/storage/schema/delta/35/device_stream_id.sql
Normal file
21
synapse/storage/schema/delta/35/device_stream_id.sql
Normal file
|
@ -0,0 +1,21 @@
|
|||
/* Copyright 2016 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.
|
||||
*/
|
||||
|
||||
CREATE TABLE device_max_stream_id (
|
||||
stream_id BIGINT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO device_max_stream_id (stream_id)
|
||||
SELECT COALESCE(MAX(stream_id), 0) FROM device_inbox;
|
Loading…
Reference in a new issue