groups.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 import GroupServerStream
  18. from synapse.storage.database import DatabasePool
  19. from synapse.storage.databases.main.group_server import GroupServerWorkerStore
  20. from synapse.util.caches.stream_change_cache import StreamChangeCache
  21. class SlavedGroupServerStore(GroupServerWorkerStore, BaseSlavedStore):
  22. def __init__(self, database: DatabasePool, db_conn, hs):
  23. super(SlavedGroupServerStore, self).__init__(database, db_conn, hs)
  24. self.hs = hs
  25. self._group_updates_id_gen = SlavedIdTracker(
  26. db_conn, "local_group_updates", "stream_id"
  27. )
  28. self._group_updates_stream_cache = StreamChangeCache(
  29. "_group_updates_stream_cache",
  30. self._group_updates_id_gen.get_current_token(),
  31. )
  32. def get_group_stream_token(self):
  33. return self._group_updates_id_gen.get_current_token()
  34. def process_replication_rows(self, stream_name, instance_name, token, rows):
  35. if stream_name == GroupServerStream.NAME:
  36. self._group_updates_id_gen.advance(token)
  37. for row in rows:
  38. self._group_updates_stream_cache.entity_has_changed(row.user_id, token)
  39. return super().process_replication_rows(stream_name, instance_name, token, rows)