2014-08-12 16:10:52 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2019-05-17 20:37:31 +02:00
|
|
|
# Copyright 2017-2018 New Vector Ltd
|
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
2014-08-12 16:10:52 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
import logging
|
2019-05-28 17:47:42 +02:00
|
|
|
import random
|
2020-01-07 15:18:43 +01:00
|
|
|
from abc import ABCMeta
|
2020-01-22 11:37:00 +01:00
|
|
|
from typing import Any, Optional
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2019-12-04 14:52:46 +01:00
|
|
|
from six import PY2
|
|
|
|
from six.moves import builtins
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2018-08-30 16:19:58 +02:00
|
|
|
from canonicaljson import json
|
2015-04-09 12:41:36 +02:00
|
|
|
|
2019-12-04 14:52:46 +01:00
|
|
|
from synapse.storage.database import LoggingTransaction # noqa: F401
|
|
|
|
from synapse.storage.database import make_in_list_sql_clause # noqa: F401
|
|
|
|
from synapse.storage.database import Database
|
2020-01-22 11:37:00 +01:00
|
|
|
from synapse.types import Collection, get_domain_from_id
|
2019-06-19 22:04:58 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-02-10 15:50:53 +01:00
|
|
|
|
2020-01-07 15:18:43 +01:00
|
|
|
# some of our subclasses have abstract methods, so we use the ABCMeta metaclass.
|
|
|
|
class SQLBaseStore(metaclass=ABCMeta):
|
2019-12-04 15:00:29 +01:00
|
|
|
"""Base class for data stores that holds helper functions.
|
|
|
|
|
|
|
|
Note that multiple instances of this class will exist as there will be one
|
|
|
|
per data store (and not one per physical database).
|
|
|
|
"""
|
|
|
|
|
2019-12-06 14:08:40 +01:00
|
|
|
def __init__(self, database: Database, db_conn, hs):
|
2014-08-13 19:15:23 +02:00
|
|
|
self.hs = hs
|
2016-06-02 16:23:09 +02:00
|
|
|
self._clock = hs.get_clock()
|
2019-12-18 11:45:12 +01:00
|
|
|
self.database_engine = database.engine
|
2019-12-06 14:40:02 +01:00
|
|
|
self.db = database
|
2019-05-28 17:47:42 +02:00
|
|
|
self.rand = random.SystemRandom()
|
|
|
|
|
2020-05-07 14:51:08 +02:00
|
|
|
def process_replication_rows(self, stream_name, instance_name, token, rows):
|
|
|
|
pass
|
|
|
|
|
2019-02-18 18:53:31 +01:00
|
|
|
def _invalidate_state_caches(self, room_id, members_changed):
|
|
|
|
"""Invalidates caches that are based on the current state, but does
|
|
|
|
not stream invalidations down replication.
|
|
|
|
|
|
|
|
Args:
|
2019-02-19 12:24:59 +01:00
|
|
|
room_id (str): Room where state changed
|
2019-02-19 12:38:40 +01:00
|
|
|
members_changed (iterable[str]): The user_ids of members that have
|
|
|
|
changed
|
2019-02-18 18:53:31 +01:00
|
|
|
"""
|
2020-02-21 13:15:07 +01:00
|
|
|
for host in {get_domain_from_id(u) for u in members_changed}:
|
2019-04-03 11:07:29 +02:00
|
|
|
self._attempt_to_invalidate_cache("is_host_joined", (room_id, host))
|
|
|
|
self._attempt_to_invalidate_cache("was_host_joined", (room_id, host))
|
2019-02-22 15:38:14 +01:00
|
|
|
|
2019-04-03 11:07:29 +02:00
|
|
|
self._attempt_to_invalidate_cache("get_users_in_room", (room_id,))
|
|
|
|
self._attempt_to_invalidate_cache("get_room_summary", (room_id,))
|
|
|
|
self._attempt_to_invalidate_cache("get_current_state_ids", (room_id,))
|
2019-02-22 15:38:14 +01:00
|
|
|
|
2020-01-22 11:37:00 +01:00
|
|
|
def _attempt_to_invalidate_cache(
|
|
|
|
self, cache_name: str, key: Optional[Collection[Any]]
|
|
|
|
):
|
2019-02-22 15:38:14 +01:00
|
|
|
"""Attempts to invalidate the cache of the given name, ignoring if the
|
|
|
|
cache doesn't exist. Mainly used for invalidating caches on workers,
|
|
|
|
where they may not have the cache.
|
2019-02-18 18:53:31 +01:00
|
|
|
|
2019-02-22 15:38:14 +01:00
|
|
|
Args:
|
2020-01-22 11:37:00 +01:00
|
|
|
cache_name
|
|
|
|
key: Entry to invalidate. If None then invalidates the entire
|
|
|
|
cache.
|
2019-02-22 15:38:14 +01:00
|
|
|
"""
|
2020-01-22 11:37:00 +01:00
|
|
|
|
2019-02-22 15:38:14 +01:00
|
|
|
try:
|
2020-01-22 11:37:00 +01:00
|
|
|
if key is None:
|
|
|
|
getattr(self, cache_name).invalidate_all()
|
|
|
|
else:
|
|
|
|
getattr(self, cache_name).invalidate(tuple(key))
|
2019-02-22 15:38:14 +01:00
|
|
|
except AttributeError:
|
|
|
|
# We probably haven't pulled in the cache in this worker,
|
|
|
|
# which is fine.
|
|
|
|
pass
|
2019-02-18 18:53:31 +01:00
|
|
|
|
2018-08-30 16:19:58 +02:00
|
|
|
|
|
|
|
def db_to_json(db_content):
|
|
|
|
"""
|
|
|
|
Take some data from a database row and return a JSON-decoded object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
db_content (memoryview|buffer|bytes|bytearray|unicode)
|
|
|
|
"""
|
|
|
|
# psycopg2 on Python 3 returns memoryview objects, which we need to
|
|
|
|
# cast to bytes to decode
|
|
|
|
if isinstance(db_content, memoryview):
|
|
|
|
db_content = db_content.tobytes()
|
|
|
|
|
|
|
|
# psycopg2 on Python 2 returns buffer objects, which we need to cast to
|
|
|
|
# bytes to decode
|
2018-10-20 02:16:55 +02:00
|
|
|
if PY2 and isinstance(db_content, builtins.buffer):
|
2018-08-30 16:19:58 +02:00
|
|
|
db_content = bytes(db_content)
|
|
|
|
|
|
|
|
# Decode it to a Unicode string before feeding it to json.loads, so we
|
|
|
|
# consistenty get a Unicode-containing object out.
|
|
|
|
if isinstance(db_content, (bytes, bytearray)):
|
2019-06-20 11:32:02 +02:00
|
|
|
db_content = db_content.decode("utf8")
|
2018-08-30 16:19:58 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
return json.loads(db_content)
|
|
|
|
except Exception:
|
|
|
|
logging.warning("Tried to decode '%r' as JSON and failed", db_content)
|
|
|
|
raise
|