Simplify query and handle finishing correctly

This commit is contained in:
Erik Johnston 2016-04-22 09:37:16 +01:00
parent ae571810f2
commit 183cacac90
2 changed files with 16 additions and 17 deletions

View file

@ -173,11 +173,12 @@ class BackgroundUpdateStore(SQLBaseStore):
logger.info( logger.info(
"Updating %r. Updated %r items in %rms." "Updating %r. Updated %r items in %rms."
" (total_rate=%r/ms, current_rate=%r/ms, total_updated=%r)", " (total_rate=%r/ms, current_rate=%r/ms, total_updated=%r, batch_size=%r)",
update_name, items_updated, duration_ms, update_name, items_updated, duration_ms,
performance.total_items_per_ms(), performance.total_items_per_ms(),
performance.average_items_per_ms(), performance.average_items_per_ms(),
performance.total_item_count, performance.total_item_count,
batch_size,
) )
performance.update(items_updated, duration_ms) performance.update(items_updated, duration_ms)

View file

@ -169,28 +169,26 @@ class SearchStore(BackgroundUpdateStore):
yield self.runInteraction( yield self.runInteraction(
self.EVENT_SEARCH_ORDER_UPDATE_NAME, self.EVENT_SEARCH_ORDER_UPDATE_NAME,
self._background_update_progress_txn, self._background_update_progress_txn,
self.EVENT_SEARCH_ORDER_UPDATE_NAME, progress, self.EVENT_SEARCH_ORDER_UPDATE_NAME, pg,
) )
def reindex_search_txn(txn): def reindex_search_txn(txn):
events_sql = (
"SELECT stream_ordering, origin_server_ts, event_id FROM events"
" WHERE ? <= stream_ordering AND stream_ordering < ?"
" ORDER BY stream_ordering DESC"
" LIMIT ?"
)
sql = ( sql = (
"UPDATE event_search AS es SET stream_ordering = e.stream_ordering," "UPDATE event_search AS es SET stream_ordering = e.stream_ordering,"
" origin_server_ts = e.origin_server_ts" " origin_server_ts = e.origin_server_ts"
" FROM (%s) AS e" " FROM events AS e"
" WHERE e.event_id = es.event_id" " WHERE e.event_id = es.event_id"
" AND ? <= e.stream_ordering AND e.stream_ordering < ?"
" RETURNING es.stream_ordering" " RETURNING es.stream_ordering"
) % (events_sql,) )
txn.execute(sql, (target_min_stream_id, max_stream_id, batch_size)) min_stream_id = max_stream_id - batch_size
txn.execute(sql, (min_stream_id, max_stream_id))
rows = txn.fetchall() rows = txn.fetchall()
min_stream_id = rows[-1][0]
if min_stream_id < target_min_stream_id:
# We've recached the end.
return len(rows), False
progress = { progress = {
"target_min_stream_id_inclusive": target_min_stream_id, "target_min_stream_id_inclusive": target_min_stream_id,
@ -203,16 +201,16 @@ class SearchStore(BackgroundUpdateStore):
txn, self.EVENT_SEARCH_ORDER_UPDATE_NAME, progress txn, self.EVENT_SEARCH_ORDER_UPDATE_NAME, progress
) )
return len(rows) return len(rows), True
result = yield self.runInteraction( num_rows, finished = yield self.runInteraction(
self.EVENT_SEARCH_ORDER_UPDATE_NAME, reindex_search_txn self.EVENT_SEARCH_ORDER_UPDATE_NAME, reindex_search_txn
) )
if not result: if not finished:
yield self._end_background_update(self.EVENT_SEARCH_ORDER_UPDATE_NAME) yield self._end_background_update(self.EVENT_SEARCH_ORDER_UPDATE_NAME)
defer.returnValue(result) defer.returnValue(num_rows)
@defer.inlineCallbacks @defer.inlineCallbacks
def search_msgs(self, room_ids, search_term, keys): def search_msgs(self, room_ids, search_term, keys):