When unique constraint error occured, exception of NovaBase.save() raising cause is sqlalchemy.exc.IntegrityError.

Bug #1067683 reported by ryuta sekine
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
OpenStack Compute (nova)
Fix Released
High
Boris Pavlovic

Bug Description

Now, when unique constraint error occured, sqlalchemy.exc.IntegrityError is raised without entering through the else branch if statement.
Exception eventually receive nova.exception.DBError, because it is rapped in get_session() of nova/db/sqlalchemy/session.py

def save(self, session=None):
    """Save this object."""
    if not session:
        session = get_session()
    session.add(self)
    try:
        session.flush()
    except IntegrityError, e:
        if str(e).endswith('is not unique'):
            raise exception.Duplicate(str(e))
        else:
            raise

[error log]

======================================================================
FAIL: test_session_flush_on_integrity_violation()
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/stack/tempest/tempest/manual_tests/negative_st/test_negative_sqlalchemy.py", line 159, in test_session_flush_on_integrity_violation
    self.assertEqual(exception.Duplicate, inner_ex.__class__)
AssertionError: <class 'nova.exception.Duplicate'> != <class 'sqlalchemy.exc.IntegrityError'>
-------------------- >> begin captured logging << --------------------
tempest.config: INFO: Using tempest config file /opt/stack/tempest/etc/tempest.conf
nova.exception: ERROR: DB exception wrapped.
Traceback (most recent call last):
  File "/opt/stack/nova/nova/exception.py", line 68, in _wrap
    return f(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1547, in flush
    self._flush(objects)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1616, in _flush
    flush_context.execute()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 328, in execute
    rec.execute(self)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 472, in execute
    uow
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2264, in _save_obj
    execute(statement, multiparams)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1405, in execute
    params)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1538, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1646, in _execute_context
    context)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1639, in _execute_context
    context)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 330, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (IntegrityError) (1062, "Duplicate entry '9' for key 'PRIMARY'") 'INSERT INTO services (created_at,
updated_at, deleted_at, deleted, id, host, `binary`, topic, report_count, disabled, availability_zone) VALUES (%s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' (datetime.datetime(2012, 9, 13, 2, 16, 22, 105645), None, None, 0, 9L,
None, None, None, 0, 0, 'nova')

Reason of bug seems to be to coming out of sqlarchemy error without being rapped in a nova.
I think should return nova.exception.Duplicate.

Tags: db
ryuta sekine (r-sekine)
description: updated
description: updated
description: updated
ryuta sekine (r-sekine)
Changed in openstack-ci:
status: New → Invalid
status: Invalid → New
affects: openstack-ci → nova
Revision history for this message
Chuck Short (zulcss) wrote :

Which version is this with?

Changed in nova:
status: New → Incomplete
Revision history for this message
ryuta sekine (r-sekine) wrote :
Thierry Carrez (ttx)
Changed in nova:
status: Incomplete → New
tags: added: db
Revision history for this message
Vish Ishaya (vishvananda) wrote :

This was fixed in master:

https://review.openstack.org/#/c/16339/

needs a backport.

Changed in nova:
status: New → Fix Released
importance: Undecided → High
assignee: nobody → Boris Pavlovic (boris-42)
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to nova (stable/folsom)

Fix proposed to branch: stable/folsom
Review: https://review.openstack.org/23051

Thierry Carrez (ttx)
Changed in nova:
milestone: none → grizzly-2
Thierry Carrez (ttx)
Changed in nova:
milestone: grizzly-2 → 2013.1
Sean Dague (sdague)
no longer affects: nova/folsom
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.