db: Remove unnecessary use of '_mapping'

Now that we've addressed the SQLAlchemy 2.0 issues, we can focus on
cleaning things up. Replace uses of '_mapping' with explicit references
to the fields.

Change-Id: I3da80bebac4a2013a3c7cbf128615c19eb836735
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane
2021-07-15 14:04:48 +01:00
parent e5b89581c5
commit c68d472dca
4 changed files with 31 additions and 26 deletions

View File

@@ -163,12 +163,11 @@ def _check_capacity_exceeded(ctx, allocs):
usage_map = {}
provs_with_inv = set()
for record in records:
record = record._mapping
map_key = (record['uuid'], record['resource_class_id'])
map_key = (record.uuid, record.resource_class_id)
if map_key in usage_map:
raise KeyError("%s already in usage_map, bad query" % str(map_key))
usage_map[map_key] = record
provs_with_inv.add(record["uuid"])
provs_with_inv.add(record.uuid)
# Ensure that all providers have existing inventory
missing_provs = provider_uuids - provs_with_inv
if missing_provs:
@@ -199,10 +198,10 @@ def _check_capacity_exceeded(ctx, allocs):
raise exception.InvalidInventory(
resource_class=alloc.resource_class,
resource_provider=rp_uuid)
allocation_ratio = usage['allocation_ratio']
min_unit = usage['min_unit']
max_unit = usage['max_unit']
step_size = usage['step_size']
allocation_ratio = usage.allocation_ratio
min_unit = usage.min_unit
max_unit = usage.max_unit
step_size = usage.step_size
# check min_unit, max_unit, step_size
if (amount_needed < min_unit or amount_needed > max_unit or
@@ -222,9 +221,9 @@ def _check_capacity_exceeded(ctx, allocs):
resource_class=alloc.resource_class,
resource_provider=rp_uuid)
# usage["used"] can be returned as None
used = usage['used'] or 0
capacity = (usage['total'] - usage['reserved']) * allocation_ratio
# usage.used can be returned as None
used = usage.used or 0
capacity = (usage.total - usage.reserved) * allocation_ratio
if (capacity < (used + amount_needed) or
capacity < (used + rp_resource_class_sum[rp_uuid][rc_id])):
LOG.warning(

View File

@@ -526,8 +526,7 @@ def _build_provider_summaries(context, rw_ctx, root_ids, prov_traits):
# ProviderSummary objects containing one or more ProviderSummaryResource
# objects representing the resources the provider has inventory for.
for usage in usages:
usage = usage._mapping
rp_id = usage['resource_provider_id']
rp_id = usage.resource_provider_id
summary = rw_ctx.summaries_by_id.get(rp_id)
if not summary:
pids = provider_ids[rp_id]
@@ -550,7 +549,7 @@ def _build_provider_summaries(context, rw_ctx, root_ids, prov_traits):
summary.traits = prov_traits[rp_id]
rw_ctx.summaries_by_id[rp_id] = summary
rc_id = usage['resource_class_id']
rc_id = usage.resource_class_id
if rc_id is None:
# NOTE(tetsuro): This provider doesn't have any inventory itself.
# But we include this provider in summaries since another
@@ -558,20 +557,20 @@ def _build_provider_summaries(context, rw_ctx, root_ids, prov_traits):
# Let's skip the following and leave "ProviderSummary.resources"
# field empty.
continue
# NOTE(jaypipes): usage['used'] may be None due to the LEFT JOIN of
# NOTE(jaypipes): usage.used may be None due to the LEFT JOIN of
# the usages subquery, so we coerce NULL values to 0 here. It may
# also be a Decimal, as that's the type that mysql tends to return
# when func.sum is used in a query. We need an int, otherwise later
# JSON serialization will not work.
used = int(usage['used'] or 0)
allocation_ratio = usage['allocation_ratio']
cap = int((usage['total'] - usage['reserved']) * allocation_ratio)
used = int(usage.used or 0)
allocation_ratio = usage.allocation_ratio
cap = int((usage.total - usage.reserved) * allocation_ratio)
rc_name = context.rc_cache.string_from_id(rc_id)
rpsr = ProviderSummaryResource(
resource_class=rc_name,
capacity=cap,
used=used,
max_unit=usage['max_unit'],
max_unit=usage.max_unit,
)
# Construct a dict, keyed by resource provider + resource class, of
# ProviderSummaryResource. This will be used to do a final capacity
@@ -945,5 +944,5 @@ def _provider_ids_from_root_ids(context, root_ids):
ret = {}
for r in context.session.execute(sel, {'root_ids': list(root_ids)}):
ret[r._mapping['id']] = r
ret[r.id] = r
return ret

View File

@@ -68,9 +68,14 @@ class ResourceClass(object):
:raises: ResourceClassNotFound if no such resource class was found
"""
rc = context.rc_cache.all_from_string(name)._mapping
obj = cls(context, id=rc['id'], name=rc['name'],
updated_at=rc['updated_at'], created_at=rc['created_at'])
rc = context.rc_cache.all_from_string(name)
obj = cls(
context,
id=rc.id,
name=rc.name,
updated_at=rc.updated_at,
created_at=rc.created_at,
)
return obj
@staticmethod

View File

@@ -140,10 +140,12 @@ def _update_inventory_for_provider(ctx, rp, inv_list, to_update):
sa.and_(
_ALLOC_TBL.c.resource_provider_id == rp.id,
_ALLOC_TBL.c.resource_class_id == rc_id))
allocations = ctx.session.execute(allocation_query).first()._mapping
if (allocations and
allocations['usage'] is not None and
allocations['usage'] > inv_record.capacity):
allocations = ctx.session.execute(allocation_query).first()
if (
allocations and
allocations.usage is not None and
allocations.usage > inv_record.capacity
):
exceeded.append((rp.uuid, rc_str))
upd_stmt = _INV_TBL.update().where(
sa.and_(