Make reindex happen in bg

This commit is contained in:
Erik Johnston 2016-09-12 12:36:36 +01:00
parent b91e2833b3
commit 15ca0c6a4d
2 changed files with 32 additions and 3 deletions

View file

@ -17,6 +17,7 @@ from ._base import SQLBaseStore
from twisted.internet import defer from twisted.internet import defer
from synapse.util.caches.descriptors import cachedInlineCallbacks from synapse.util.caches.descriptors import cachedInlineCallbacks
from synapse.types import RoomStreamToken from synapse.types import RoomStreamToken
from synapse.storage.engines import PostgresEngine
from .stream import lower_bound from .stream import lower_bound
import logging import logging
@ -26,10 +27,17 @@ logger = logging.getLogger(__name__)
class EventPushActionsStore(SQLBaseStore): class EventPushActionsStore(SQLBaseStore):
EPA_HIGHLIGHT_INDEX = "epa_highlight_index"
def __init__(self, hs): def __init__(self, hs):
self.stream_ordering_month_ago = None self.stream_ordering_month_ago = None
super(EventPushActionsStore, self).__init__(hs) super(EventPushActionsStore, self).__init__(hs)
self.register_background_update_handler(
self.EPA_HIGHLIGHT_INDEX,
self._background_index_epa_highlight,
)
def _set_push_actions_for_event_and_users_txn(self, txn, event, tuples): def _set_push_actions_for_event_and_users_txn(self, txn, event, tuples):
""" """
Args: Args:
@ -500,6 +508,28 @@ class EventPushActionsStore(SQLBaseStore):
return range_end return range_end
@defer.inlineCallbacks
def _background_index_epa_highlight(self, progress, batch_size):
def reindex_txn(txn):
if isinstance(self.database_engine, PostgresEngine):
txn.execute(
"CREATE INDEX CONCURRENTLY event_push_actions_u_highlight"
" on event_push_actions(user_id, highlight, stream_ordering)"
)
else:
txn.execute(
"CREATE INDEX event_push_actions_u_highlight"
" on event_push_actions(user_id, highlight, stream_ordering)"
)
yield self.runInteraction(
self.EPA_HIGHLIGHT_INDEX, reindex_txn
)
yield self._end_background_update(self.EPA_HIGHLIGHT_INDEX)
defer.returnValue(1)
def _action_has_highlight(actions): def _action_has_highlight(actions):
for action in actions: for action in actions:

View file

@ -13,6 +13,5 @@
* limitations under the License. * limitations under the License.
*/ */
CREATE INDEX event_push_actions_user_id_highlight_stream_ordering on event_push_actions( INSERT into background_updates (update_name, progress_json)
user_id, highlight, stream_ordering VALUES ('epa_highlight_index', '{}');
);