Browse Source

Fix database loading for some resources

Whenever we use share network subnets, we'd
always need the associated share networks; so
'joined' loading them makes sense rather than
lazy loading. Same is true for Share Group
Snapshots, since share group attributes are
relevant in contexts that snapshots are
referenced.

Fix the ORM relationship loading technique
and queries appropriately. Also convert share
network name into a property of the share
network subnet class.

Co-Authored-By: Carlos Eduardo <ces.eduardo98@gmail.com>
Closes-Bug: #1862833
Change-Id: I8c29760479f9c874d618e2c15ebab75bffeb3cbb
changes/82/707282/6
Goutham Pacha Ravi 1 week ago
parent
commit
ef6b9a0c72
4 changed files with 17 additions and 5 deletions
  1. +1
    -1
      manila/api/views/share_network_subnets.py
  2. +3
    -2
      manila/db/sqlalchemy/api.py
  3. +8
    -2
      manila/db/sqlalchemy/models.py
  4. +5
    -0
      releasenotes/notes/bug-1862833-fix-backref-by-eager-loading-2d897976e7598625.yaml

+ 1
- 1
manila/api/views/share_network_subnets.py View File

@@ -37,7 +37,7 @@ class ViewBuilder(common.ViewBuilder):
'id': share_network_subnet.get('id'),
'availability_zone': share_network_subnet.get('availability_zone'),
'share_network_id': share_network_subnet.get('share_network_id'),
'share_network_name': share_network_subnet.share_network['name'],
'share_network_name': share_network_subnet['share_network_name'],
'created_at': share_network_subnet.get('created_at'),
'segmentation_id': share_network_subnet.get('segmentation_id'),
'neutron_subnet_id': share_network_subnet.get('neutron_subnet_id'),

+ 3
- 2
manila/db/sqlalchemy/api.py View File

@@ -43,6 +43,7 @@ import six
from sqlalchemy import MetaData
from sqlalchemy import or_
from sqlalchemy.orm import joinedload
from sqlalchemy.orm import subqueryload
from sqlalchemy.sql.expression import literal
from sqlalchemy.sql.expression import true
from sqlalchemy.sql import func
@@ -3400,7 +3401,7 @@ def _network_get_query(context, session=None):
return (model_query(context, models.ShareNetwork, session=session).
options(joinedload('share_instances'),
joinedload('security_services'),
joinedload('share_network_subnets')))
subqueryload('share_network_subnets')))


@require_context
@@ -3543,7 +3544,7 @@ def _network_subnet_get_query(context, session=None):
if session is None:
session = get_session()
return (model_query(context, models.ShareNetworkSubnet, session=session).
options(joinedload('share_servers')))
options(joinedload('share_servers'), joinedload('share_network')))


@require_context

+ 8
- 2
manila/db/sqlalchemy/models.py View File

@@ -938,7 +938,9 @@ class ShareNetwork(BASE, ManilaBase):
'ShareNetwork.id == ShareInstance.share_network_id,'
'ShareInstance.deleted == "False")')
share_network_subnets = orm.relationship(
"ShareNetworkSubnet", backref='share_network', lazy='immediate',
"ShareNetworkSubnet",
lazy='joined',
backref=orm.backref('share_network', lazy='joined'),
primaryjoin='and_'
'(ShareNetwork.id == ShareNetworkSubnet.share_network_id,'
'ShareNetworkSubnet.deleted == "False")')
@@ -990,6 +992,10 @@ class ShareNetworkSubnet(BASE, ManilaBase):
def is_default(self):
return self.availability_zone_id is None

@property
def share_network_name(self):
return self.share_network['name']


class ShareServer(BASE, ManilaBase):
"""Represents share server used by share."""
@@ -1216,7 +1222,7 @@ class ShareGroupSnapshot(BASE, ManilaBase):
status = Column(String(255))
share_group = orm.relationship(
ShareGroup,
backref="snapshots",
backref=orm.backref("snapshots", lazy='joined'),
foreign_keys=share_group_id,
primaryjoin=('and_('
'ShareGroupSnapshot.share_group_id == ShareGroup.id,'

+ 5
- 0
releasenotes/notes/bug-1862833-fix-backref-by-eager-loading-2d897976e7598625.yaml View File

@@ -0,0 +1,5 @@
---
fixes:
- |
Some resources will be eagerly loaded from the database to avoid cyclical
references and faulty results if their retrieval is deferred.

Loading…
Cancel
Save