Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1085 from matrix-org/erikj/reindex_state_groups
Browse files Browse the repository at this point in the history
Reindex state_groups_state after pruning
  • Loading branch information
erikjohnston authored Sep 8, 2016
2 parents 91ec972 + 5c68873 commit 023143f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions synapse/storage/background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ def do_next_background_update(self, desired_duration_ms):
updates = yield self._simple_select_list(
"background_updates",
keyvalues=None,
retcols=("update_name",),
retcols=("update_name", "depends_on"),
)
in_flight = set(update["update_name"] for update in updates)
for update in updates:
self._background_update_queue.append(update['update_name'])
if update["depends_on"] not in in_flight:
self._background_update_queue.append(update['update_name'])

if not self._background_update_queue:
# no work left to do
Expand Down
20 changes: 20 additions & 0 deletions synapse/storage/schema/delta/35/add_state_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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.
*/


ALTER TABLE background_updates ADD COLUMN depends_on TEXT;

INSERT into background_updates (update_name, progress_json, depends_on)
VALUES ('state_group_state_type_index', '{}', 'state_group_state_deduplication');
33 changes: 33 additions & 0 deletions synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ class StateStore(SQLBaseStore):
"""

STATE_GROUP_DEDUPLICATION_UPDATE_NAME = "state_group_state_deduplication"
STATE_GROUP_INDEX_UPDATE_NAME = "state_group_state_type_index"

def __init__(self, hs):
super(StateStore, self).__init__(hs)
self.register_background_update_handler(
self.STATE_GROUP_DEDUPLICATION_UPDATE_NAME,
self._background_deduplicate_state,
)
self.register_background_update_handler(
self.STATE_GROUP_INDEX_UPDATE_NAME,
self._background_index_state,
)

@defer.inlineCallbacks
def get_state_groups_ids(self, room_id, event_ids):
Expand Down Expand Up @@ -793,3 +798,31 @@ def reindex_txn(txn):
yield self._end_background_update(self.STATE_GROUP_DEDUPLICATION_UPDATE_NAME)

defer.returnValue(result * BATCH_SIZE_SCALE_FACTOR)

@defer.inlineCallbacks
def _background_index_state(self, progress, batch_size):
def reindex_txn(txn):
if isinstance(self.database_engine, PostgresEngine):
txn.execute(
"CREATE INDEX state_groups_state_type_idx"
" ON state_groups_state(state_group, type, state_key)"
)
txn.execute(
"DROP INDEX IF EXISTS state_groups_state_id"
)
else:
txn.execute(
"CREATE INDEX state_groups_state_type_idx"
" ON state_groups_state(state_group, type, state_key)"
)
txn.execute(
"DROP INDEX IF EXISTS state_groups_state_id"
)

yield self.runInteraction(
self.STATE_GROUP_INDEX_UPDATE_NAME, reindex_txn
)

yield self._end_background_update(self.STATE_GROUP_INDEX_UPDATE_NAME)

defer.returnValue(1)

0 comments on commit 023143f

Please sign in to comment.