Switch QueueItem state_id to be unique after a backfill

This commit is contained in:
Joseph Schorr 2017-01-18 15:19:45 -05:00
parent e2748fccd9
commit 71ec23b550
3 changed files with 35 additions and 6 deletions

View file

@ -263,17 +263,13 @@ class WorkQueue(object):
# performing the update. Previously, we would check all these columns, resulting in a bunch
# of lock contention. This change mitigates the problem significantly by only checking two
# columns (id and state_id), both of which should be absolutely unique at all times.
#
# TODO(jschorr): Remove the extra `processing_expires` check once this has been pushed to
# production and every box is updating state_id.
set_unavailable_query = (QueueItem
.update(available=False,
processing_expires=now + timedelta(seconds=processing_time),
retries_remaining=QueueItem.retries_remaining - 1,
state_id=str(uuid.uuid4()))
.where(QueueItem.id == db_item.id,
QueueItem.state_id == db_item.state_id,
QueueItem.processing_expires == db_item.processing_expires))
QueueItem.state_id == db_item.state_id))
changed = set_unavailable_query.execute()
return changed == 1