From 7a8415be5c53511e04330360ffc03f5922edd745 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 12 Feb 2018 14:11:11 +0000 Subject: [PATCH] Move db MAX constants to own file The "MAX" constants in the db api are useful to import without needing to import the rest of the api, so they are moved to a constants.py file in the same package. api.py then imports the symbols from that file for backwards compatibility. The handler and schema for placement inventories (which use the MAX constants) are updated to point to the constants file directly as they do not need the db api. Change-Id: Ifab10e023bcf4389f6514b477755c4cced2d1598 --- .../openstack/placement/handlers/inventory.py | 4 +-- .../openstack/placement/schemas/inventory.py | 14 +++++------ nova/db/api.py | 16 ++++-------- nova/db/constants.py | 25 +++++++++++++++++++ 4 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 nova/db/constants.py diff --git a/nova/api/openstack/placement/handlers/inventory.py b/nova/api/openstack/placement/handlers/inventory.py index 93637f1d22b8..ad3a8ba7c7c9 100644 --- a/nova/api/openstack/placement/handlers/inventory.py +++ b/nova/api/openstack/placement/handlers/inventory.py @@ -22,7 +22,7 @@ from nova.api.openstack.placement import microversion from nova.api.openstack.placement.schemas import inventory as schema from nova.api.openstack.placement import util from nova.api.openstack.placement import wsgi_wrapper -from nova import db +from nova.db import constants as db_const from nova import exception from nova.i18n import _ from nova.objects import resource_provider as rp_obj @@ -42,7 +42,7 @@ OUTPUT_INVENTORY_FIELDS = [ INVENTORY_DEFAULTS = { 'reserved': 0, 'min_unit': 1, - 'max_unit': db.MAX_INT, + 'max_unit': db_const.MAX_INT, 'step_size': 1, 'allocation_ratio': 1.0 } diff --git a/nova/api/openstack/placement/schemas/inventory.py b/nova/api/openstack/placement/schemas/inventory.py index 89d4232f514f..78f1c45d340d 100644 --- a/nova/api/openstack/placement/schemas/inventory.py +++ b/nova/api/openstack/placement/schemas/inventory.py @@ -13,7 +13,7 @@ import copy -from nova import db +from nova.db import constants as db_const RESOURCE_CLASS_IDENTIFIER = "^[A-Z0-9_]+$" @@ -25,32 +25,32 @@ BASE_INVENTORY_SCHEMA = { }, "total": { "type": "integer", - "maximum": db.MAX_INT, + "maximum": db_const.MAX_INT, "minimum": 1, }, "reserved": { "type": "integer", - "maximum": db.MAX_INT, + "maximum": db_const.MAX_INT, "minimum": 0, }, "min_unit": { "type": "integer", - "maximum": db.MAX_INT, + "maximum": db_const.MAX_INT, "minimum": 1 }, "max_unit": { "type": "integer", - "maximum": db.MAX_INT, + "maximum": db_const.MAX_INT, "minimum": 1 }, "step_size": { "type": "integer", - "maximum": db.MAX_INT, + "maximum": db_const.MAX_INT, "minimum": 1 }, "allocation_ratio": { "type": "number", - "maximum": db.SQL_SP_FLOAT_MAX + "maximum": db_const.SQL_SP_FLOAT_MAX }, }, "required": [ diff --git a/nova/db/api.py b/nova/db/api.py index 68e21c069536..8f07f4049b55 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -32,9 +32,14 @@ from oslo_log import log as logging from nova.cells import rpcapi as cells_rpcapi import nova.conf +from nova.db import constants CONF = nova.conf.CONF +# NOTE(cdent): These constants are re-defined in this module to preserve +# existing references to them. +MAX_INT = constants.MAX_INT +SQL_SP_FLOAT_MAX = constants.SQL_SP_FLOAT_MAX _BACKEND_MAPPING = {'sqlalchemy': 'nova.db.sqlalchemy.api'} @@ -43,17 +48,6 @@ IMPL = concurrency.TpoolDbapiWrapper(CONF, backend_mapping=_BACKEND_MAPPING) LOG = logging.getLogger(__name__) -# The maximum value a signed INT type may have -MAX_INT = 0x7FFFFFFF - -# NOTE(dosaboy): This is supposed to represent the maximum value that we can -# place into a SQL single precision float so that we can check whether values -# are oversize. Postgres and MySQL both define this as their max whereas Sqlite -# uses dynamic typing so this would not apply. Different dbs react in different -# ways to oversize values e.g. postgres will raise an exception while mysql -# will round off the value. Nevertheless we may still want to know prior to -# insert whether the value is oversize or not. -SQL_SP_FLOAT_MAX = 3.40282e+38 ################### diff --git a/nova/db/constants.py b/nova/db/constants.py new file mode 100644 index 000000000000..a082fba4877b --- /dev/null +++ b/nova/db/constants.py @@ -0,0 +1,25 @@ +# 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. +"""Useful db-related constants. In their own file so they can be imported +cleanly.""" + +# The maximum value a signed INT type may have +MAX_INT = 0x7FFFFFFF + +# NOTE(dosaboy): This is supposed to represent the maximum value that we can +# place into a SQL single precision float so that we can check whether values +# are oversize. Postgres and MySQL both define this as their max whereas Sqlite +# uses dynamic typing so this would not apply. Different dbs react in different +# ways to oversize values e.g. postgres will raise an exception while mysql +# will round off the value. Nevertheless we may still want to know prior to +# insert whether the value is oversize or not. +SQL_SP_FLOAT_MAX = 3.40282e+38