2019-05-30 12:22:59 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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-11-01 13:03:04 +01:00
|
|
|
from synapse.api.constants import EventContentFields
|
2020-07-16 17:32:19 +02:00
|
|
|
from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause
|
2020-08-05 22:38:57 +02:00
|
|
|
from synapse.storage.database import DatabasePool
|
2019-05-30 12:22:59 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-12-04 16:09:36 +01:00
|
|
|
class EventsBackgroundUpdatesStore(SQLBaseStore):
|
2019-05-30 12:22:59 +02:00
|
|
|
|
|
|
|
EVENT_ORIGIN_SERVER_TS_NAME = "event_origin_server_ts"
|
|
|
|
EVENT_FIELDS_SENDER_URL_UPDATE_NAME = "event_fields_sender_url"
|
2019-05-30 12:24:42 +02:00
|
|
|
DELETE_SOFT_FAILED_EXTREMITIES = "delete_soft_failed_extremities"
|
2019-05-30 12:22:59 +02:00
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
def __init__(self, database: DatabasePool, db_conn, hs):
|
2020-09-18 15:56:44 +02:00
|
|
|
super().__init__(database, db_conn, hs)
|
2019-05-30 12:22:59 +02:00
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-05-30 12:22:59 +02:00
|
|
|
self.EVENT_ORIGIN_SERVER_TS_NAME, self._background_reindex_origin_server_ts
|
|
|
|
)
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-05-30 12:22:59 +02:00
|
|
|
self.EVENT_FIELDS_SENDER_URL_UPDATE_NAME,
|
|
|
|
self._background_reindex_fields_sender,
|
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_index_update(
|
2019-05-30 12:22:59 +02:00
|
|
|
"event_contains_url_index",
|
|
|
|
index_name="event_contains_url_index",
|
|
|
|
table="events",
|
|
|
|
columns=["room_id", "topological_ordering", "stream_ordering"],
|
|
|
|
where_clause="contains_url = true AND outlier = false",
|
|
|
|
)
|
|
|
|
|
|
|
|
# an event_id index on event_search is useful for the purge_history
|
|
|
|
# api. Plus it means we get to enforce some integrity with a UNIQUE
|
|
|
|
# clause
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_index_update(
|
2019-05-30 12:22:59 +02:00
|
|
|
"event_search_event_id_idx",
|
|
|
|
index_name="event_search_event_id_idx",
|
|
|
|
table="event_search",
|
|
|
|
columns=["event_id"],
|
|
|
|
unique=True,
|
|
|
|
psql_only=True,
|
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-06-20 11:32:02 +02:00
|
|
|
self.DELETE_SOFT_FAILED_EXTREMITIES, self._cleanup_extremities_bg_update
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-10-01 14:23:34 +02:00
|
|
|
"redactions_received_ts", self._redactions_received_ts
|
|
|
|
)
|
|
|
|
|
2019-10-09 17:03:24 +02:00
|
|
|
# This index gets deleted in `event_fix_redactions_bytes` update
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_index_update(
|
2019-10-09 17:03:24 +02:00
|
|
|
"event_fix_redactions_bytes_create_index",
|
|
|
|
index_name="redactions_censored_redacts",
|
|
|
|
table="redactions",
|
|
|
|
columns=["redacts"],
|
|
|
|
where_clause="have_censored",
|
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-10-09 17:03:24 +02:00
|
|
|
"event_fix_redactions_bytes", self._event_fix_redactions_bytes
|
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_update_handler(
|
2019-10-31 15:41:28 +01:00
|
|
|
"event_store_labels", self._event_store_labels
|
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates.register_background_index_update(
|
2019-12-09 16:09:13 +01:00
|
|
|
"redactions_have_censored_ts_idx",
|
|
|
|
index_name="redactions_have_censored_ts",
|
|
|
|
table="redactions",
|
|
|
|
columns=["received_ts"],
|
|
|
|
where_clause="NOT have_censored",
|
|
|
|
)
|
|
|
|
|
2020-10-27 15:12:31 +01:00
|
|
|
self.db_pool.updates.register_background_index_update(
|
|
|
|
"users_have_local_media",
|
|
|
|
index_name="users_have_local_media",
|
|
|
|
table="local_media_repository",
|
|
|
|
columns=["user_id", "created_ts"],
|
|
|
|
)
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
async def _background_reindex_fields_sender(self, progress, batch_size):
|
2019-05-30 12:22:59 +02:00
|
|
|
target_min_stream_id = progress["target_min_stream_id_inclusive"]
|
|
|
|
max_stream_id = progress["max_stream_id_exclusive"]
|
|
|
|
rows_inserted = progress.get("rows_inserted", 0)
|
|
|
|
|
|
|
|
INSERT_CLUMP_SIZE = 1000
|
|
|
|
|
|
|
|
def reindex_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT stream_ordering, event_id, json FROM events"
|
|
|
|
" INNER JOIN event_json USING (event_id)"
|
|
|
|
" WHERE ? <= stream_ordering AND stream_ordering < ?"
|
|
|
|
" ORDER BY stream_ordering DESC"
|
|
|
|
" LIMIT ?"
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute(sql, (target_min_stream_id, max_stream_id, batch_size))
|
|
|
|
|
|
|
|
rows = txn.fetchall()
|
|
|
|
if not rows:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
min_stream_id = rows[-1][0]
|
|
|
|
|
|
|
|
update_rows = []
|
|
|
|
for row in rows:
|
|
|
|
try:
|
|
|
|
event_id = row[1]
|
2020-07-16 17:32:19 +02:00
|
|
|
event_json = db_to_json(row[2])
|
2019-05-30 12:22:59 +02:00
|
|
|
sender = event_json["sender"]
|
|
|
|
content = event_json["content"]
|
|
|
|
|
|
|
|
contains_url = "url" in content
|
|
|
|
if contains_url:
|
2020-06-16 14:51:47 +02:00
|
|
|
contains_url &= isinstance(content["url"], str)
|
2019-05-30 12:22:59 +02:00
|
|
|
except (KeyError, AttributeError):
|
|
|
|
# If the event is missing a necessary field then
|
|
|
|
# skip over it.
|
|
|
|
continue
|
|
|
|
|
|
|
|
update_rows.append((sender, contains_url, event_id))
|
|
|
|
|
|
|
|
sql = "UPDATE events SET sender = ?, contains_url = ? WHERE event_id = ?"
|
|
|
|
|
|
|
|
for index in range(0, len(update_rows), INSERT_CLUMP_SIZE):
|
|
|
|
clump = update_rows[index : index + INSERT_CLUMP_SIZE]
|
|
|
|
txn.executemany(sql, clump)
|
|
|
|
|
|
|
|
progress = {
|
|
|
|
"target_min_stream_id_inclusive": target_min_stream_id,
|
|
|
|
"max_stream_id_exclusive": min_stream_id,
|
|
|
|
"rows_inserted": rows_inserted + len(rows),
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates._background_update_progress_txn(
|
2019-05-30 12:22:59 +02:00
|
|
|
txn, self.EVENT_FIELDS_SENDER_URL_UPDATE_NAME, progress
|
|
|
|
)
|
|
|
|
|
|
|
|
return len(rows)
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
result = await self.db_pool.runInteraction(
|
2019-05-30 12:22:59 +02:00
|
|
|
self.EVENT_FIELDS_SENDER_URL_UPDATE_NAME, reindex_txn
|
|
|
|
)
|
|
|
|
|
|
|
|
if not result:
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.updates._end_background_update(
|
2019-12-04 16:09:36 +01:00
|
|
|
self.EVENT_FIELDS_SENDER_URL_UPDATE_NAME
|
|
|
|
)
|
2019-05-30 12:22:59 +02:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return result
|
2019-05-30 12:22:59 +02:00
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
async def _background_reindex_origin_server_ts(self, progress, batch_size):
|
2019-05-30 12:22:59 +02:00
|
|
|
target_min_stream_id = progress["target_min_stream_id_inclusive"]
|
|
|
|
max_stream_id = progress["max_stream_id_exclusive"]
|
|
|
|
rows_inserted = progress.get("rows_inserted", 0)
|
|
|
|
|
|
|
|
INSERT_CLUMP_SIZE = 1000
|
|
|
|
|
|
|
|
def reindex_search_txn(txn):
|
|
|
|
sql = (
|
|
|
|
"SELECT stream_ordering, event_id FROM events"
|
|
|
|
" WHERE ? <= stream_ordering AND stream_ordering < ?"
|
|
|
|
" ORDER BY stream_ordering DESC"
|
|
|
|
" LIMIT ?"
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute(sql, (target_min_stream_id, max_stream_id, batch_size))
|
|
|
|
|
|
|
|
rows = txn.fetchall()
|
|
|
|
if not rows:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
min_stream_id = rows[-1][0]
|
|
|
|
event_ids = [row[1] for row in rows]
|
|
|
|
|
|
|
|
rows_to_update = []
|
|
|
|
|
|
|
|
chunks = [event_ids[i : i + 100] for i in range(0, len(event_ids), 100)]
|
|
|
|
for chunk in chunks:
|
2020-08-05 22:38:57 +02:00
|
|
|
ev_rows = self.db_pool.simple_select_many_txn(
|
2019-05-30 12:22:59 +02:00
|
|
|
txn,
|
|
|
|
table="event_json",
|
|
|
|
column="event_id",
|
|
|
|
iterable=chunk,
|
|
|
|
retcols=["event_id", "json"],
|
|
|
|
keyvalues={},
|
|
|
|
)
|
|
|
|
|
|
|
|
for row in ev_rows:
|
|
|
|
event_id = row["event_id"]
|
2020-07-16 17:32:19 +02:00
|
|
|
event_json = db_to_json(row["json"])
|
2019-05-30 12:22:59 +02:00
|
|
|
try:
|
|
|
|
origin_server_ts = event_json["origin_server_ts"]
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
# If the event is missing a necessary field then
|
|
|
|
# skip over it.
|
|
|
|
continue
|
|
|
|
|
|
|
|
rows_to_update.append((origin_server_ts, event_id))
|
|
|
|
|
|
|
|
sql = "UPDATE events SET origin_server_ts = ? WHERE event_id = ?"
|
|
|
|
|
|
|
|
for index in range(0, len(rows_to_update), INSERT_CLUMP_SIZE):
|
|
|
|
clump = rows_to_update[index : index + INSERT_CLUMP_SIZE]
|
|
|
|
txn.executemany(sql, clump)
|
|
|
|
|
|
|
|
progress = {
|
|
|
|
"target_min_stream_id_inclusive": target_min_stream_id,
|
|
|
|
"max_stream_id_exclusive": min_stream_id,
|
|
|
|
"rows_inserted": rows_inserted + len(rows_to_update),
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates._background_update_progress_txn(
|
2019-05-30 12:22:59 +02:00
|
|
|
txn, self.EVENT_ORIGIN_SERVER_TS_NAME, progress
|
|
|
|
)
|
|
|
|
|
|
|
|
return len(rows_to_update)
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
result = await self.db_pool.runInteraction(
|
2019-05-30 12:22:59 +02:00
|
|
|
self.EVENT_ORIGIN_SERVER_TS_NAME, reindex_search_txn
|
|
|
|
)
|
|
|
|
|
|
|
|
if not result:
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.updates._end_background_update(
|
2019-12-04 16:09:36 +01:00
|
|
|
self.EVENT_ORIGIN_SERVER_TS_NAME
|
|
|
|
)
|
2019-05-30 12:22:59 +02:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return result
|
2019-05-30 12:22:59 +02:00
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
async def _cleanup_extremities_bg_update(self, progress, batch_size):
|
2019-05-30 12:22:59 +02:00
|
|
|
"""Background update to clean out extremities that should have been
|
|
|
|
deleted previously.
|
|
|
|
|
|
|
|
Mainly used to deal with the aftermath of #5269.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# This works by first copying all existing forward extremities into the
|
|
|
|
# `_extremities_to_check` table at start up, and then checking each
|
|
|
|
# event in that table whether we have any descendants that are not
|
|
|
|
# soft-failed/rejected. If that is the case then we delete that event
|
|
|
|
# from the forward extremities table.
|
|
|
|
#
|
|
|
|
# For efficiency, we do this in batches by recursively pulling out all
|
|
|
|
# descendants of a batch until we find the non soft-failed/rejected
|
|
|
|
# events, i.e. the set of descendants whose chain of prev events back
|
|
|
|
# to the batch of extremities are all soft-failed or rejected.
|
|
|
|
# Typically, we won't find any such events as extremities will rarely
|
|
|
|
# have any descendants, but if they do then we should delete those
|
|
|
|
# extremities.
|
|
|
|
|
|
|
|
def _cleanup_extremities_bg_update_txn(txn):
|
|
|
|
# The set of extremity event IDs that we're checking this round
|
|
|
|
original_set = set()
|
|
|
|
|
|
|
|
# A dict[str, set[str]] of event ID to their prev events.
|
|
|
|
graph = {}
|
|
|
|
|
|
|
|
# The set of descendants of the original set that are not rejected
|
|
|
|
# nor soft-failed. Ancestors of these events should be removed
|
|
|
|
# from the forward extremities table.
|
|
|
|
non_rejected_leaves = set()
|
|
|
|
|
|
|
|
# Set of event IDs that have been soft failed, and for which we
|
|
|
|
# should check if they have descendants which haven't been soft
|
|
|
|
# failed.
|
|
|
|
soft_failed_events_to_lookup = set()
|
|
|
|
|
|
|
|
# First, we get `batch_size` events from the table, pulling out
|
2019-05-30 15:06:42 +02:00
|
|
|
# their successor events, if any, and the successor events'
|
2019-05-30 12:22:59 +02:00
|
|
|
# rejection status.
|
|
|
|
txn.execute(
|
|
|
|
"""SELECT prev_event_id, event_id, internal_metadata,
|
|
|
|
rejections.event_id IS NOT NULL, events.outlier
|
|
|
|
FROM (
|
|
|
|
SELECT event_id AS prev_event_id
|
|
|
|
FROM _extremities_to_check
|
|
|
|
LIMIT ?
|
|
|
|
) AS f
|
|
|
|
LEFT JOIN event_edges USING (prev_event_id)
|
|
|
|
LEFT JOIN events USING (event_id)
|
|
|
|
LEFT JOIN event_json USING (event_id)
|
|
|
|
LEFT JOIN rejections USING (event_id)
|
2019-06-20 11:32:02 +02:00
|
|
|
""",
|
|
|
|
(batch_size,),
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for prev_event_id, event_id, metadata, rejected, outlier in txn:
|
|
|
|
original_set.add(prev_event_id)
|
|
|
|
|
|
|
|
if not event_id or outlier:
|
|
|
|
# Common case where the forward extremity doesn't have any
|
|
|
|
# descendants.
|
|
|
|
continue
|
|
|
|
|
|
|
|
graph.setdefault(event_id, set()).add(prev_event_id)
|
|
|
|
|
|
|
|
soft_failed = False
|
|
|
|
if metadata:
|
2020-07-16 17:32:19 +02:00
|
|
|
soft_failed = db_to_json(metadata).get("soft_failed")
|
2019-05-30 12:22:59 +02:00
|
|
|
|
|
|
|
if soft_failed or rejected:
|
|
|
|
soft_failed_events_to_lookup.add(event_id)
|
|
|
|
else:
|
|
|
|
non_rejected_leaves.add(event_id)
|
|
|
|
|
|
|
|
# Now we recursively check all the soft-failed descendants we
|
|
|
|
# found above in the same way, until we have nothing left to
|
|
|
|
# check.
|
|
|
|
while soft_failed_events_to_lookup:
|
|
|
|
# We only want to do 100 at a time, so we split given list
|
|
|
|
# into two.
|
|
|
|
batch = list(soft_failed_events_to_lookup)
|
|
|
|
to_check, to_defer = batch[:100], batch[100:]
|
|
|
|
soft_failed_events_to_lookup = set(to_defer)
|
|
|
|
|
|
|
|
sql = """SELECT prev_event_id, event_id, internal_metadata,
|
|
|
|
rejections.event_id IS NOT NULL
|
|
|
|
FROM event_edges
|
|
|
|
INNER JOIN events USING (event_id)
|
|
|
|
INNER JOIN event_json USING (event_id)
|
|
|
|
LEFT JOIN rejections USING (event_id)
|
|
|
|
WHERE
|
2019-10-02 20:07:07 +02:00
|
|
|
NOT events.outlier
|
|
|
|
AND
|
|
|
|
"""
|
|
|
|
clause, args = make_in_list_sql_clause(
|
|
|
|
self.database_engine, "prev_event_id", to_check
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
2019-10-02 20:07:07 +02:00
|
|
|
txn.execute(sql + clause, list(args))
|
2019-05-30 12:22:59 +02:00
|
|
|
|
|
|
|
for prev_event_id, event_id, metadata, rejected in txn:
|
|
|
|
if event_id in graph:
|
|
|
|
# Already handled this event previously, but we still
|
|
|
|
# want to record the edge.
|
|
|
|
graph[event_id].add(prev_event_id)
|
|
|
|
continue
|
|
|
|
|
|
|
|
graph[event_id] = {prev_event_id}
|
|
|
|
|
2020-07-16 17:32:19 +02:00
|
|
|
soft_failed = db_to_json(metadata).get("soft_failed")
|
2019-05-30 12:22:59 +02:00
|
|
|
if soft_failed or rejected:
|
|
|
|
soft_failed_events_to_lookup.add(event_id)
|
|
|
|
else:
|
|
|
|
non_rejected_leaves.add(event_id)
|
|
|
|
|
|
|
|
# We have a set of non-soft-failed descendants, so we recurse up
|
|
|
|
# the graph to find all ancestors and add them to the set of event
|
|
|
|
# IDs that we can delete from forward extremities table.
|
|
|
|
to_delete = set()
|
|
|
|
while non_rejected_leaves:
|
|
|
|
event_id = non_rejected_leaves.pop()
|
|
|
|
prev_event_ids = graph.get(event_id, set())
|
|
|
|
non_rejected_leaves.update(prev_event_ids)
|
|
|
|
to_delete.update(prev_event_ids)
|
|
|
|
|
|
|
|
to_delete.intersection_update(original_set)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
deleted = self.db_pool.simple_delete_many_txn(
|
2019-05-30 12:22:59 +02:00
|
|
|
txn=txn,
|
|
|
|
table="event_forward_extremities",
|
|
|
|
column="event_id",
|
|
|
|
iterable=to_delete,
|
|
|
|
keyvalues={},
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Deleted %d forward extremities of %d checked, to clean up #5269",
|
|
|
|
deleted,
|
|
|
|
len(original_set),
|
|
|
|
)
|
|
|
|
|
|
|
|
if deleted:
|
|
|
|
# We now need to invalidate the caches of these rooms
|
2020-08-05 22:38:57 +02:00
|
|
|
rows = self.db_pool.simple_select_many_txn(
|
2019-05-30 12:22:59 +02:00
|
|
|
txn,
|
|
|
|
table="events",
|
|
|
|
column="event_id",
|
|
|
|
iterable=to_delete,
|
|
|
|
keyvalues={},
|
2019-06-20 11:32:02 +02:00
|
|
|
retcols=("room_id",),
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
2020-02-21 13:15:07 +01:00
|
|
|
room_ids = {row["room_id"] for row in rows}
|
2019-05-30 12:22:59 +02:00
|
|
|
for room_id in room_ids:
|
|
|
|
txn.call_after(
|
2019-06-20 11:32:02 +02:00
|
|
|
self.get_latest_event_ids_in_room.invalidate, (room_id,)
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.simple_delete_many_txn(
|
2019-05-30 12:22:59 +02:00
|
|
|
txn=txn,
|
|
|
|
table="_extremities_to_check",
|
|
|
|
column="event_id",
|
|
|
|
iterable=original_set,
|
|
|
|
keyvalues={},
|
|
|
|
)
|
|
|
|
|
|
|
|
return len(original_set)
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
num_handled = await self.db_pool.runInteraction(
|
2019-06-20 11:32:02 +02:00
|
|
|
"_cleanup_extremities_bg_update", _cleanup_extremities_bg_update_txn
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not num_handled:
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.updates._end_background_update(
|
2019-12-04 16:09:36 +01:00
|
|
|
self.DELETE_SOFT_FAILED_EXTREMITIES
|
|
|
|
)
|
2019-05-30 12:22:59 +02:00
|
|
|
|
|
|
|
def _drop_table_txn(txn):
|
|
|
|
txn.execute("DROP TABLE _extremities_to_check")
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.runInteraction(
|
2019-06-20 11:32:02 +02:00
|
|
|
"_cleanup_extremities_bg_update_drop_table", _drop_table_txn
|
2019-05-30 12:22:59 +02:00
|
|
|
)
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return num_handled
|
2019-10-01 14:23:34 +02:00
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
async def _redactions_received_ts(self, progress, batch_size):
|
2019-10-01 14:23:34 +02:00
|
|
|
"""Handles filling out the `received_ts` column in redactions.
|
|
|
|
"""
|
|
|
|
last_event_id = progress.get("last_event_id", "")
|
|
|
|
|
|
|
|
def _redactions_received_ts_txn(txn):
|
|
|
|
# Fetch the set of event IDs that we want to update
|
|
|
|
sql = """
|
|
|
|
SELECT event_id FROM redactions
|
|
|
|
WHERE event_id > ?
|
|
|
|
ORDER BY event_id ASC
|
|
|
|
LIMIT ?
|
|
|
|
"""
|
|
|
|
|
|
|
|
txn.execute(sql, (last_event_id, batch_size))
|
|
|
|
|
|
|
|
rows = txn.fetchall()
|
|
|
|
if not rows:
|
|
|
|
return 0
|
|
|
|
|
2019-10-31 16:43:24 +01:00
|
|
|
(upper_event_id,) = rows[-1]
|
2019-10-01 14:23:34 +02:00
|
|
|
|
|
|
|
# Update the redactions with the received_ts.
|
|
|
|
#
|
|
|
|
# Note: Not all events have an associated received_ts, so we
|
|
|
|
# fallback to using origin_server_ts. If we for some reason don't
|
|
|
|
# have an origin_server_ts, lets just use the current timestamp.
|
|
|
|
#
|
|
|
|
# We don't want to leave it null, as then we'll never try and
|
|
|
|
# censor those redactions.
|
|
|
|
sql = """
|
|
|
|
UPDATE redactions
|
|
|
|
SET received_ts = (
|
|
|
|
SELECT COALESCE(received_ts, origin_server_ts, ?) FROM events
|
|
|
|
WHERE events.event_id = redactions.event_id
|
|
|
|
)
|
|
|
|
WHERE ? <= event_id AND event_id <= ?
|
|
|
|
"""
|
|
|
|
|
|
|
|
txn.execute(sql, (self._clock.time_msec(), last_event_id, upper_event_id))
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates._background_update_progress_txn(
|
2019-10-01 14:23:34 +02:00
|
|
|
txn, "redactions_received_ts", {"last_event_id": upper_event_id}
|
|
|
|
)
|
|
|
|
|
|
|
|
return len(rows)
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
count = await self.db_pool.runInteraction(
|
2019-10-01 14:23:34 +02:00
|
|
|
"_redactions_received_ts", _redactions_received_ts_txn
|
|
|
|
)
|
|
|
|
|
|
|
|
if not count:
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.updates._end_background_update("redactions_received_ts")
|
2019-10-01 14:23:34 +02:00
|
|
|
|
|
|
|
return count
|
2019-10-09 17:03:24 +02:00
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
async def _event_fix_redactions_bytes(self, progress, batch_size):
|
2019-10-09 17:03:24 +02:00
|
|
|
"""Undoes hex encoded censored redacted event JSON.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _event_fix_redactions_bytes_txn(txn):
|
|
|
|
# This update is quite fast due to new index.
|
|
|
|
txn.execute(
|
|
|
|
"""
|
|
|
|
UPDATE event_json
|
|
|
|
SET
|
|
|
|
json = convert_from(json::bytea, 'utf8')
|
|
|
|
FROM redactions
|
|
|
|
WHERE
|
|
|
|
redactions.have_censored
|
|
|
|
AND event_json.event_id = redactions.redacts
|
|
|
|
AND json NOT LIKE '{%';
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute("DROP INDEX redactions_censored_redacts")
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.runInteraction(
|
2019-10-09 17:03:24 +02:00
|
|
|
"_event_fix_redactions_bytes", _event_fix_redactions_bytes_txn
|
|
|
|
)
|
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.updates._end_background_update("event_fix_redactions_bytes")
|
2019-10-09 17:03:24 +02:00
|
|
|
|
|
|
|
return 1
|
2019-10-31 15:41:28 +01:00
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
async def _event_store_labels(self, progress, batch_size):
|
2019-11-07 18:01:53 +01:00
|
|
|
"""Background update handler which will store labels for existing events."""
|
2019-10-31 15:41:28 +01:00
|
|
|
last_event_id = progress.get("last_event_id", "")
|
|
|
|
|
|
|
|
def _event_store_labels_txn(txn):
|
|
|
|
txn.execute(
|
|
|
|
"""
|
|
|
|
SELECT event_id, json FROM event_json
|
2019-10-31 15:49:41 +01:00
|
|
|
LEFT JOIN event_labels USING (event_id)
|
|
|
|
WHERE event_id > ? AND label IS NULL
|
2019-11-01 13:03:04 +01:00
|
|
|
ORDER BY event_id LIMIT ?
|
2019-10-31 15:41:28 +01:00
|
|
|
""",
|
2019-10-31 16:01:29 +01:00
|
|
|
(last_event_id, batch_size),
|
2019-10-31 15:41:28 +01:00
|
|
|
)
|
|
|
|
|
2019-11-07 17:47:45 +01:00
|
|
|
results = list(txn)
|
|
|
|
|
2019-11-07 17:47:15 +01:00
|
|
|
nbrows = 0
|
|
|
|
last_row_event_id = ""
|
2019-11-07 17:47:45 +01:00
|
|
|
for (event_id, event_json_raw) in results:
|
2019-11-25 14:26:59 +01:00
|
|
|
try:
|
2020-07-16 17:32:19 +02:00
|
|
|
event_json = db_to_json(event_json_raw)
|
2019-11-25 14:26:59 +01:00
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.simple_insert_many_txn(
|
2019-11-25 14:26:59 +01:00
|
|
|
txn=txn,
|
|
|
|
table="event_labels",
|
|
|
|
values=[
|
|
|
|
{
|
|
|
|
"event_id": event_id,
|
|
|
|
"label": label,
|
|
|
|
"room_id": event_json["room_id"],
|
|
|
|
"topological_ordering": event_json["depth"],
|
|
|
|
}
|
|
|
|
for label in event_json["content"].get(
|
|
|
|
EventContentFields.LABELS, []
|
|
|
|
)
|
|
|
|
if isinstance(label, str)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning(
|
|
|
|
"Unable to load event %s (no labels will be imported): %s",
|
|
|
|
event_id,
|
|
|
|
e,
|
|
|
|
)
|
2019-10-31 15:41:28 +01:00
|
|
|
|
2019-11-07 17:47:15 +01:00
|
|
|
nbrows += 1
|
|
|
|
last_row_event_id = event_id
|
|
|
|
|
2020-08-05 22:38:57 +02:00
|
|
|
self.db_pool.updates._background_update_progress_txn(
|
2019-11-07 17:47:15 +01:00
|
|
|
txn, "event_store_labels", {"last_event_id": last_row_event_id}
|
2019-10-31 15:41:28 +01:00
|
|
|
)
|
|
|
|
|
2019-11-07 17:47:15 +01:00
|
|
|
return nbrows
|
2019-10-31 15:41:28 +01:00
|
|
|
|
2020-08-14 16:05:19 +02:00
|
|
|
num_rows = await self.db_pool.runInteraction(
|
2019-10-31 15:41:28 +01:00
|
|
|
desc="event_store_labels", func=_event_store_labels_txn
|
|
|
|
)
|
|
|
|
|
2019-11-07 16:25:27 +01:00
|
|
|
if not num_rows:
|
2020-08-14 16:05:19 +02:00
|
|
|
await self.db_pool.updates._end_background_update("event_store_labels")
|
2019-10-31 15:41:28 +01:00
|
|
|
|
2019-11-01 13:07:05 +01:00
|
|
|
return num_rows
|