From 2fed65f2c8d7e1109bdb322c49f44a3d14617da0 Mon Sep 17 00:00:00 2001 From: Roman Podolyaka Date: Wed, 24 Jul 2013 17:13:07 +0300 Subject: [PATCH] Call get_session() only when necessary A Session instance should be retrieved only when it's really needed, e. g. to make a few DB queries inside a single transaction or to pass the session to a private DB API method, etc. At the same time, many queries are simple and can be expressed by the means of auxiliary model_query() function. Blueprint: db-session-cleanup Change-Id: I2ddc0cda42a84fe600c04f4624db9c2933799bfc --- cinder/db/sqlalchemy/api.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 749b36eda..2a2bed9e9 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -1425,9 +1425,8 @@ def snapshot_data_get_for_project(context, project_id, volume_type_id=None, @require_context def snapshot_get_active_by_window(context, begin, end=None, project_id=None): """Return snapshots that were active during window.""" - session = get_session() - query = session.query(models.Snapshot) + query = model_query(context, models.Snapshot, read_deleted="yes") query = query.filter(or_(models.Snapshot.deleted_at == None, models.Snapshot.deleted_at > begin)) if end: @@ -1668,9 +1667,7 @@ def volume_get_active_by_window(context, end=None, project_id=None): """Return volumes that were active during window.""" - session = get_session() - query = session.query(models.Volume) - + query = model_query(context, models.Volume, read_deleted="yes") query = query.filter(or_(models.Volume.deleted_at == None, models.Volume.deleted_at > begin)) if end: @@ -1894,10 +1891,8 @@ def volume_glance_metadata_copy_to_volume(context, volume_id, snapshot_id): @require_context def volume_glance_metadata_delete_by_volume(context, volume_id): - session = get_session() - session.query(models.VolumeGlanceMetadata).\ + model_query(context, models.VolumeGlanceMetadata, read_deleted='no').\ filter_by(volume_id=volume_id).\ - filter_by(deleted=False).\ update({'deleted': True, 'deleted_at': timeutils.utcnow(), 'updated_at': literal_column('updated_at')}) @@ -1905,10 +1900,8 @@ def volume_glance_metadata_delete_by_volume(context, volume_id): @require_context def volume_glance_metadata_delete_by_snapshot(context, snapshot_id): - session = get_session() - session.query(models.VolumeGlanceMetadata).\ + model_query(context, models.VolumeGlanceMetadata, read_deleted='no').\ filter_by(snapshot_id=snapshot_id).\ - filter_by(deleted=False).\ update({'deleted': True, 'deleted_at': timeutils.utcnow(), 'updated_at': literal_column('updated_at')}) @@ -1976,14 +1969,12 @@ def backup_update(context, backup_id, values): @require_admin_context def backup_destroy(context, backup_id): - session = get_session() - with session.begin(): - session.query(models.Backup).\ - filter_by(id=backup_id).\ - update({'status': 'deleted', - 'deleted': True, - 'deleted_at': timeutils.utcnow(), - 'updated_at': literal_column('updated_at')}) + model_query(context, models.Backup).\ + filter_by(id=backup_id).\ + update({'status': 'deleted', + 'deleted': True, + 'deleted_at': timeutils.utcnow(), + 'updated_at': literal_column('updated_at')}) ############################### -- 2.45.2