Skip to content

Commit

Permalink
Remove DBDuplicateEntry columns check
Browse files Browse the repository at this point in the history
DBDuplicateEntry exception does not provide columns for db2.
we have to remove this exception columns check in neutron/
db/agents_db.py. It is safe to remove columns check here, since
only the duplication of host and agent_type can raise this
exception.

Change-Id: I00d7b7c64de2912dda9fa57c08d90b1ef1c3aed7
Closes-bug: #1391766
  • Loading branch information
Wei Hu committed Jan 9, 2015
1 parent 6df7f5a commit a7eead2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
32 changes: 14 additions & 18 deletions neutron/db/agents_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from oslo.db import exception as db_exc
from oslo import messaging
from oslo.serialization import jsonutils
from oslo.utils import excutils
from oslo.utils import timeutils
import sqlalchemy as sa
from sqlalchemy.orm import exc
Expand Down Expand Up @@ -196,23 +195,20 @@ def create_or_update_agent(self, context, agent):

try:
return self._create_or_update_agent(context, agent)
except db_exc.DBDuplicateEntry as e:
with excutils.save_and_reraise_exception() as ctxt:
if e.columns == ['agent_type', 'host']:
# It might happen that two or more concurrent transactions
# are trying to insert new rows having the same value of
# (agent_type, host) pair at the same time (if there has
# been no such entry in the table and multiple agent status
# updates are being processed at the moment). In this case
# having a unique constraint on (agent_type, host) columns
# guarantees that only one transaction will succeed and
# insert a new agent entry, others will fail and be rolled
# back. That means we must retry them one more time: no
# INSERTs will be issued, because
# _get_agent_by_type_and_host() will return the existing
# agent entry, which will be updated multiple times
ctxt.reraise = False
return self._create_or_update_agent(context, agent)
except db_exc.DBDuplicateEntry:
# It might happen that two or more concurrent transactions
# are trying to insert new rows having the same value of
# (agent_type, host) pair at the same time (if there has
# been no such entry in the table and multiple agent status
# updates are being processed at the moment). In this case
# having a unique constraint on (agent_type, host) columns
# guarantees that only one transaction will succeed and
# insert a new agent entry, others will fail and be rolled
# back. That means we must retry them one more time: no
# INSERTs will be issued, because
# _get_agent_by_type_and_host() will return the existing
# agent entry, which will be updated multiple times
return self._create_or_update_agent(context, agent)


class AgentExtRpcCallback(object):
Expand Down
2 changes: 1 addition & 1 deletion neutron/tests/unit/db/test_agent_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_create_or_update_agent_concurrent_insert(self):
# attempt on fail
with mock.patch('sqlalchemy.orm.Session.add') as add_mock:
add_mock.side_effect = [
exc.DBDuplicateEntry(columns=['agent_type', 'host']),
exc.DBDuplicateEntry(),
None
]

Expand Down

0 comments on commit a7eead2

Please sign in to comment.