Browse Source

use sqla functions from neutron-lib

The public db apis are available in neutron-lib. This patch consumes
the sqla_listen, sqla_remove and sqla_remove_all functions from lib
by removing them from neutron and using lib's version.

There's also a TODO added here so we can eventually rehome the
check_no_sqlalchemy_event_import hacking check to neutron-lib.

NeutronLibImpact

Change-Id: I3b3862016125303fb227b4ff41420a89bb33c917
changes/33/583633/4
Boden R 3 years ago
committed by Slawek Kaplonski
parent
commit
c774930aed
  1. 30
      neutron/db/api.py
  2. 13
      neutron/db/db_base_plugin_v2.py
  3. 5
      neutron/hacking/checks.py
  4. 15
      neutron/quota/resource.py
  5. 3
      neutron/services/loki/loki_plugin.py
  6. 2
      neutron/services/revisions/revision_plugin.py
  7. 2
      neutron/services/timestamp/timestamp_db.py
  8. 2
      neutron/tests/base.py
  9. 3
      neutron/tests/unit/db/test_db_base_plugin_v2.py
  10. 6
      neutron/tests/unit/plugins/ml2/test_plugin.py
  11. 2
      neutron/tests/unit/services/revisions/test_revision_plugin.py

30
neutron/db/api.py

@ -31,7 +31,6 @@ from pecan import util as p_util
import six
import sqlalchemy
from sqlalchemy import event # noqa
from sqlalchemy import exc as sql_exc
from sqlalchemy import orm
from sqlalchemy.orm import exc
@ -148,35 +147,6 @@ def autonested_transaction(sess):
yield tx
_REGISTERED_SQLA_EVENTS = []
def sqla_listen(*args):
"""Wrapper to track subscribers for test teardowns.
SQLAlchemy has no "unsubscribe all" option for its event listener
framework so we need to keep track of the subscribers by having
them call through here for test teardowns.
"""
event.listen(*args)
_REGISTERED_SQLA_EVENTS.append(args)
def sqla_remove(*args):
event.remove(*args)
_REGISTERED_SQLA_EVENTS.remove(args)
def sqla_remove_all():
for args in _REGISTERED_SQLA_EVENTS:
try:
event.remove(*args)
except sql_exc.InvalidRequestError:
# already removed
pass
del _REGISTERED_SQLA_EVENTS[:]
@event.listens_for(orm.session.Session, "after_flush")
def add_to_rel_load_list(session, flush_context=None):
# keep track of new items to load relationships on during commit

13
neutron/db/db_base_plugin_v2.py

@ -162,12 +162,13 @@ class NeutronDbPluginV2(db_base_plugin_common.DbBasePluginCommon,
# NOTE(arosen) These event listeners are here to hook into when
# port status changes and notify nova about their change.
self.nova_notifier = nova.Notifier.get_instance()
db_api.sqla_listen(models_v2.Port, 'after_insert',
self.nova_notifier.send_port_status)
db_api.sqla_listen(models_v2.Port, 'after_update',
self.nova_notifier.send_port_status)
db_api.sqla_listen(models_v2.Port.status, 'set',
self.nova_notifier.record_port_status_changed)
lib_db_api.sqla_listen(models_v2.Port, 'after_insert',
self.nova_notifier.send_port_status)
lib_db_api.sqla_listen(models_v2.Port, 'after_update',
self.nova_notifier.send_port_status)
lib_db_api.sqla_listen(
models_v2.Port.status, 'set',
self.nova_notifier.record_port_status_changed)
@registry.receives(resources.RBAC_POLICY, [events.BEFORE_CREATE,
events.BEFORE_UPDATE,

5
neutron/hacking/checks.py

@ -243,6 +243,7 @@ def check_python3_no_filter(logical_line):
yield(0, msg)
# TODO(boden): rehome this check to neutron-lib
@flake8ext
def check_no_sqlalchemy_event_import(logical_line, filename, noqa):
"""N346 - Use neutron.db.api.sqla_listen instead of sqlalchemy event."""
@ -256,8 +257,8 @@ def check_no_sqlalchemy_event_import(logical_line, filename, noqa):
if kw not in logical_line:
return
yield (0, "N346: Register sqlalchemy events through "
"neutron.db.api.sqla_listen so they can be cleaned up between "
"unit tests")
"neutron_lib.db.api.sqla_listen so they can be cleaned up "
"between unit tests")
def factory(register):

15
neutron/quota/resource.py

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.db import api as lib_db_api
from neutron_lib.plugins import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
@ -318,19 +319,19 @@ class TrackedResource(BaseResource):
self._model_class)
def register_events(self):
listen = db_api.sqla_listen
listen = lib_db_api.sqla_listen
listen(self._model_class, 'after_insert', self._db_event_handler)
listen(self._model_class, 'after_delete', self._db_event_handler)
listen(se.Session, 'after_bulk_delete', self._except_bulk_delete)
def unregister_events(self):
try:
db_api.sqla_remove(self._model_class, 'after_insert',
self._db_event_handler)
db_api.sqla_remove(self._model_class, 'after_delete',
self._db_event_handler)
db_api.sqla_remove(se.Session, 'after_bulk_delete',
self._except_bulk_delete)
lib_db_api.sqla_remove(self._model_class, 'after_insert',
self._db_event_handler)
lib_db_api.sqla_remove(self._model_class, 'after_delete',
self._db_event_handler)
lib_db_api.sqla_remove(se.Session, 'after_bulk_delete',
self._except_bulk_delete)
except sql_exc.InvalidRequestError:
LOG.warning("No sqlalchemy event for resource %s found",
self.name)

3
neutron/services/loki/loki_plugin.py

@ -14,8 +14,7 @@
import random
import time
from neutron.db import api as db_api
from neutron_lib.db import api as db_api
from neutron_lib.services import base as service_base
from oslo_db import exception as db_exc
from oslo_log import log as logging

2
neutron/services/revisions/revision_plugin.py

@ -11,6 +11,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.db import api as db_api
from neutron_lib.services import base as service_base
from oslo_log import log as logging
import sqlalchemy
@ -20,7 +21,6 @@ import webob.exc
from neutron._i18n import _
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db import standard_attr
LOG = logging.getLogger(__name__)

2
neutron/services/timestamp/timestamp_db.py

@ -12,13 +12,13 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron_lib.db import api as db_api
from neutron_lib import exceptions as n_exc
from oslo_utils import timeutils
from sqlalchemy.orm import session as se
from neutron.db import _model_query as model_query
from neutron.db import _resource_extend as resource_extend
from neutron.db import api as db_api
from neutron.db import standard_attr
CHANGED_SINCE = 'changed_since'

2
neutron/tests/base.py

@ -27,6 +27,7 @@ import eventlet.timeout
import fixtures
import mock
from neutron_lib.callbacks import manager as registry_manager
from neutron_lib.db import api as db_api
from neutron_lib import fixture
from oslo_concurrency.fixture import lockutils
from oslo_config import cfg
@ -49,7 +50,6 @@ from neutron.conf.agent import common as agent_config
from neutron.db import _model_query as model_query
from neutron.db import _resource_extend as resource_extend
from neutron.db import agentschedulers_db
from neutron.db import api as db_api
from neutron import manager
from neutron import policy
from neutron.quota import resource_registry

3
neutron/tests/unit/db/test_db_base_plugin_v2.py

@ -25,6 +25,7 @@ from neutron_lib.callbacks import exceptions
from neutron_lib.callbacks import registry
from neutron_lib import constants
from neutron_lib import context
from neutron_lib.db import api as lib_db_api
from neutron_lib import exceptions as lib_exc
from neutron_lib import fixture
from neutron_lib.plugins import directory
@ -6699,7 +6700,7 @@ class DbOperationBoundMixin(object):
self._recorded_statements.append(str(clauseelement))
engine = db_api.context_manager.writer.get_engine()
db_api.sqla_listen(engine, 'after_execute', _event_incrementer)
lib_db_api.sqla_listen(engine, 'after_execute', _event_incrementer)
def _get_context(self):
if self.admin:

6
neutron/tests/unit/plugins/ml2/test_plugin.py

@ -29,6 +29,7 @@ from neutron_lib.callbacks import registry
from neutron_lib.callbacks import resources
from neutron_lib import constants
from neutron_lib import context
from neutron_lib.db import api as lib_db_api
from neutron_lib import exceptions as exc
from neutron_lib import fixture
from neutron_lib.plugins import constants as plugin_constants
@ -1282,8 +1283,9 @@ class TestMl2PortsV2(test_plugin.TestPortsV2, Ml2PluginV2TestCase):
listener = IPAllocationsGrenade()
engine = db_api.context_manager.writer.get_engine()
db_api.sqla_listen(engine, 'before_cursor_execute', listener.execute)
db_api.sqla_listen(engine, 'commit', listener.commit)
lib_db_api.sqla_listen(engine, 'before_cursor_execute',
listener.execute)
lib_db_api.sqla_listen(engine, 'commit', listener.commit)
func()
# make sure that the grenade went off during the commit
self.assertTrue(listener.except_raised)

2
neutron/tests/unit/services/revisions/test_revision_plugin.py

@ -15,6 +15,7 @@
import netaddr
from neutron_lib import context as nctx
from neutron_lib.db import api as db_api
from neutron_lib.plugins import constants
from neutron_lib.plugins import directory
from oslo_config import cfg
@ -23,7 +24,6 @@ from oslo_utils import uuidutils
from sqlalchemy.orm import session as se
from webob import exc
from neutron.db import api as db_api
from neutron.db import models_v2
from neutron.objects import ports as port_obj
from neutron.tests.unit.plugins.ml2 import test_plugin

Loading…
Cancel
Save