neutron/neutron/objects/quota.py
Ihar Hrachyshka 6f83466307 Allow objects to opt in new engine facade
New facade is enabled by setting new_facade = True for the object of
interest. With new_facade on, all OVO actions will use the new reader /
writer decorator to activate sessions.

There are two new facade decorators added to OVO: db_context_reader and
db_context_write that should be used instead of explicit
autonested_transaction / reader.using / writer.using in OVO context.

All neutron.objects.db.api helpers now receive OVO classes / objects
instead of model classes, since they need to know which type of engine
facade to use for which object. While it means we change signatures for
those helper functions, they are not used anywhere outside neutron tree
except vmware-nsx unit tests, and the latter pass anyway because the
tests completely mock out them disregarding their signatures.

This patch also adds several new OVO objects to be able to continue
using neutron.objects.db.api helpers to persist models that previously
didn't have corresponding OVO classes.

Finally, the patch adds registration for missing options in
neutron/tests/unit/extensions/test_qos_fip.py to be able to debug
failures in those unit tests. Strictly speaking, this change doesn't
belong to the patch, but I include it nevertheless to speed up merge in
time close to release.

There are several non-obvious changes included, specifically:

- in neutron.objects.base, decorator() that refreshes / expunges models
from the active session now opens a subtransaction for the whole span of
call / refresh / expunge, so that we can safely refresh model regardless
of whether caller opened another parent subtransaction (it was not the
case for create_subnetpool in base db plugin code).

- in neutron.db.l3_fip_qos, removed code that updates obj.db_model
relationship directly after corresponding insertions for child policy
binding model. This code is not needed because the only caller to the
_process_extra_fip_qos_update method refetches latest state of floating
ip OVO object anyway, and this code triggers several unit test failures.

- unit tests checking that a single commit happens for get_object and
get_objects are no longer valid for new facade objects that use reader
decorator that doesn't commit but close. This change is as intended, so
unit tests were tweaked to check close for new facade objects.

Change-Id: I15ec238c18a464f977f7d1079605b82965052311
Related-Bug: #1746996
2018-02-09 04:07:34 +00:00

151 lines
5.1 KiB
Python

# Copyright (c) 2016 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_versionedobjects import fields as obj_fields
import sqlalchemy as sa
from sqlalchemy import sql
from neutron.db.quota import models
from neutron.objects import base
from neutron.objects import common_types
@base.NeutronObjectRegistry.register
class ResourceDelta(base.NeutronDbObject):
# Version 1.0: Initial version
VERSION = '1.0'
db_model = models.ResourceDelta
primary_keys = ['resource', 'reservation_id']
foreign_keys = {'Reservation': {'reservation_id': 'id'}}
fields = {
'resource': obj_fields.StringField(),
'reservation_id': common_types.UUIDField(),
'amount': obj_fields.IntegerField(nullable=True),
}
@base.NeutronObjectRegistry.register
class Reservation(base.NeutronDbObject):
# Version 1.0: Initial version
VERSION = '1.0'
db_model = models.Reservation
fields = {
'id': common_types.UUIDField(),
'project_id': obj_fields.StringField(nullable=True),
'expiration': obj_fields.DateTimeField(tzinfo_aware=False,
nullable=True),
'resource_deltas': obj_fields.ListOfObjectsField(
ResourceDelta.__name__, nullable=True),
}
synthetic_fields = ['resource_deltas']
def create(self):
deltas = self.resource_deltas
with self.db_context_writer(self.obj_context):
super(Reservation, self).create()
if deltas:
for delta in deltas:
delta.reservation_id = self.id
delta.create()
self.resource_deltas.append(delta)
self.obj_reset_changes(['resource_deltas'])
@classmethod
def delete_expired(cls, context, now, project_id):
resv_query = context.session.query(models.Reservation)
if project_id:
project_expr = (models.Reservation.project_id == project_id)
else:
project_expr = sql.true()
# TODO(manjeets) Fetch and delete objects using
# object/db/api.py once comparison operations are
# supported
resv_query = resv_query.filter(sa.and_(
project_expr, models.Reservation.expiration < now))
return resv_query.delete()
@classmethod
def get_total_reservations_map(cls, context, now, project_id,
resources, expired):
if not resources:
return
resv_query = context.session.query(
models.ResourceDelta.resource,
models.Reservation.expiration,
sql.func.sum(models.ResourceDelta.amount)).join(
models.Reservation)
if expired:
exp_expr = (models.Reservation.expiration < now)
else:
exp_expr = (models.Reservation.expiration >= now)
resv_query = resv_query.filter(sa.and_(
models.Reservation.project_id == project_id,
models.ResourceDelta.resource.in_(resources),
exp_expr)).group_by(
models.ResourceDelta.resource,
models.Reservation.expiration)
return dict((resource, total_reserved)
for (resource, exp, total_reserved) in resv_query)
@base.NeutronObjectRegistry.register
class Quota(base.NeutronDbObject):
# Version 1.0: Initial version
VERSION = '1.0'
db_model = models.Quota
fields = {
'id': common_types.UUIDField(),
'project_id': obj_fields.StringField(nullable=True),
'resource': obj_fields.StringField(nullable=True),
'limit': obj_fields.IntegerField(nullable=True),
}
@base.NeutronObjectRegistry.register
class QuotaUsage(base.NeutronDbObject):
# Version 1.0: Initial version
VERSION = '1.0'
db_model = models.QuotaUsage
primary_keys = ['resource', 'project_id']
fields = {
'resource': obj_fields.StringField(),
'project_id': obj_fields.StringField(),
'dirty': obj_fields.BooleanField(default=False),
'in_use': obj_fields.IntegerField(default=0),
'reserved': obj_fields.IntegerField(default=0),
}
@classmethod
def get_object_dirty_protected(cls, context, **kwargs):
query = context.session.query(cls.db_model)
query = query.filter_by(**cls.modify_fields_to_db(kwargs))
# NOTE(manjeets) as lock mode was just for protecting dirty bits
# an update on dirty will prevent the race.
query.filter_by(dirty=True).update({'dirty': True})
res = query.first()
if res:
return cls._load_object(context, res)