Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

76 linhas
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.replication.slave.storage._base import BaseSlavedStore
  16. from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker
  17. from synapse.replication.tcp.streams._base import DeviceListsStream, UserSignatureStream
  18. from synapse.storage.database import DatabasePool
  19. from synapse.storage.databases.main.devices import DeviceWorkerStore
  20. from synapse.storage.databases.main.end_to_end_keys import EndToEndKeyWorkerStore
  21. from synapse.util.caches.stream_change_cache import StreamChangeCache
  22. class SlavedDeviceStore(EndToEndKeyWorkerStore, DeviceWorkerStore, BaseSlavedStore):
  23. def __init__(self, database: DatabasePool, db_conn, hs):
  24. super(SlavedDeviceStore, self).__init__(database, db_conn, hs)
  25. self.hs = hs
  26. self._device_list_id_gen = SlavedIdTracker(
  27. db_conn,
  28. "device_lists_stream",
  29. "stream_id",
  30. extra_tables=[
  31. ("user_signature_stream", "stream_id"),
  32. ("device_lists_outbound_pokes", "stream_id"),
  33. ],
  34. )
  35. device_list_max = self._device_list_id_gen.get_current_token()
  36. self._device_list_stream_cache = StreamChangeCache(
  37. "DeviceListStreamChangeCache", device_list_max
  38. )
  39. self._user_signature_stream_cache = StreamChangeCache(
  40. "UserSignatureStreamChangeCache", device_list_max
  41. )
  42. self._device_list_federation_stream_cache = StreamChangeCache(
  43. "DeviceListFederationStreamChangeCache", device_list_max
  44. )
  45. def process_replication_rows(self, stream_name, instance_name, token, rows):
  46. if stream_name == DeviceListsStream.NAME:
  47. self._device_list_id_gen.advance(token)
  48. self._invalidate_caches_for_devices(token, rows)
  49. elif stream_name == UserSignatureStream.NAME:
  50. self._device_list_id_gen.advance(token)
  51. for row in rows:
  52. self._user_signature_stream_cache.entity_has_changed(row.user_id, token)
  53. return super().process_replication_rows(stream_name, instance_name, token, rows)
  54. def _invalidate_caches_for_devices(self, token, rows):
  55. for row in rows:
  56. # The entities are either user IDs (starting with '@') whose devices
  57. # have changed, or remote servers that we need to tell about
  58. # changes.
  59. if row.entity.startswith("@"):
  60. self._device_list_stream_cache.entity_has_changed(row.entity, token)
  61. self.get_cached_devices_for_user.invalidate((row.entity,))
  62. self._get_cached_user_device.invalidate_many((row.entity,))
  63. self.get_device_list_last_stream_id_for_remote.invalidate((row.entity,))
  64. else:
  65. self._device_list_federation_stream_cache.entity_has_changed(
  66. row.entity, token
  67. )