removed db_exc.DBDuplicateEntry in bw_usage_update

BandwidthUsage model has no UniqueConstraints.
In 'bw_usage_cache' table in nova db there is single primary
autoincrement key. So the duplicate entry problem is solved by
db itself and db_exc.DBDuplicateEntry could not be raised in Nova.

Ideally we should add UniqueConstraint to prevent multiple bw usage
records existing for the same date range and UUID. That fix for this
will mean we should be able to remove the .first() call and instead
use .one(). The current code that uses .first() is not correct
because there is no order_by() applied on the SQL query and
therefore the returned "first record" is indeterminate.

This workaround fix removed misleading note and exception and
added order_by() to ensure that the same record is updated every time.

Co-Authored-By: Sergey Nikitin <snikitin@mirantis.com>

Closes-bug: #1614561

Change-Id: I408bc3a3e5623965a619d8c7241e4e77c8bf44f5
This commit is contained in:
Pavel Kholkin 2015-11-27 16:19:46 +03:00
parent d1fae77967
commit ebd6193614

@ -5482,11 +5482,15 @@ def bw_usage_update(context, uuid, mac, start_period, bw_in, bw_out,
'last_ctr_out': last_ctr_out,
'bw_in': bw_in,
'bw_out': bw_out}
# NOTE(pkholkin): order_by() is needed here to ensure that the
# same record is updated every time. It can be removed after adding
# unique constraint to this model.
bw_usage = model_query(context, models.BandwidthUsage,
read_deleted='yes').\
filter_by(start_period=ts_values['start_period']).\
filter_by(uuid=uuid).\
filter_by(mac=mac).first()
filter_by(mac=mac).\
order_by(asc(models.BandwidthUsage.id)).first()
if bw_usage:
bw_usage.update(values)
@ -5501,12 +5505,8 @@ def bw_usage_update(context, uuid, mac, start_period, bw_in, bw_out,
bwusage.bw_out = bw_out
bwusage.last_ctr_in = last_ctr_in
bwusage.last_ctr_out = last_ctr_out
try:
bwusage.save(context.session)
except db_exc.DBDuplicateEntry:
# NOTE(sirp): Possible race if two greenthreads attempt to create
# the usage entry at the same time. First one wins.
pass
bwusage.save(context.session)
return bwusage