Fix some other list reifications to use nested subqueries for performance and query size safety reasons.
This commit is contained in:
parent
34ceb274c3
commit
246ff556b9
1 changed files with 26 additions and 15 deletions
|
@ -1668,6 +1668,7 @@ def _garbage_collect_storage(storage_id_whitelist):
|
||||||
logger.debug('Garbage collecting derived storage from candidates: %s', storage_id_whitelist)
|
logger.debug('Garbage collecting derived storage from candidates: %s', storage_id_whitelist)
|
||||||
with config.app_config['DB_TRANSACTION_FACTORY'](db):
|
with config.app_config['DB_TRANSACTION_FACTORY'](db):
|
||||||
# Find out which derived storages will be removed, and add them to the whitelist
|
# Find out which derived storages will be removed, and add them to the whitelist
|
||||||
|
# The comma after ImageStorage.id is VERY important, it makes it a tuple, which is a sequence
|
||||||
orphaned_from_candidates = list(orphaned_storage_query(ImageStorage.select(ImageStorage.id),
|
orphaned_from_candidates = list(orphaned_storage_query(ImageStorage.select(ImageStorage.id),
|
||||||
storage_id_whitelist,
|
storage_id_whitelist,
|
||||||
(ImageStorage.id,)))
|
(ImageStorage.id,)))
|
||||||
|
@ -1707,22 +1708,32 @@ def _garbage_collect_storage(storage_id_whitelist):
|
||||||
paths_to_remove = placements_query_to_paths_set(placements_to_remove.clone())
|
paths_to_remove = placements_query_to_paths_set(placements_to_remove.clone())
|
||||||
|
|
||||||
# Remove the placements for orphaned storages
|
# Remove the placements for orphaned storages
|
||||||
placements_subquery = list(placements_to_remove.clone().select(ImageStoragePlacement.id))
|
placements_subquery = (placements_to_remove
|
||||||
if len(placements_subquery) > 0:
|
.clone()
|
||||||
(ImageStoragePlacement
|
.select(ImageStoragePlacement.id)
|
||||||
.delete()
|
.alias('ps'))
|
||||||
.where(ImageStoragePlacement.id << list(placements_subquery))
|
inner = (ImageStoragePlacement
|
||||||
.execute())
|
.select(placements_subquery.c.id)
|
||||||
|
.from_(placements_subquery))
|
||||||
|
placements_removed = (ImageStoragePlacement
|
||||||
|
.delete()
|
||||||
|
.where(ImageStoragePlacement.id << inner)
|
||||||
|
.execute())
|
||||||
|
logger.debug('Removed %s image storage placements', placements_removed)
|
||||||
|
|
||||||
# Remove the all orphaned storages
|
# Remove all orphaned storages
|
||||||
orphaned_storages = list(orphaned_storage_query(ImageStorage.select(ImageStorage.id),
|
# The comma after ImageStorage.id is VERY important, it makes it a tuple, which is a sequence
|
||||||
storage_id_whitelist,
|
orphaned_storages = orphaned_storage_query(ImageStorage.select(ImageStorage.id),
|
||||||
(ImageStorage.id,)))
|
storage_id_whitelist,
|
||||||
if len(orphaned_storages) > 0:
|
(ImageStorage.id,)).alias('osq')
|
||||||
(ImageStorage
|
orphaned_storage_inner = (ImageStorage
|
||||||
.delete()
|
.select(orphaned_storages.c.id)
|
||||||
.where(ImageStorage.id << orphaned_storages)
|
.from_(orphaned_storages))
|
||||||
.execute())
|
storages_removed = (ImageStorage
|
||||||
|
.delete()
|
||||||
|
.where(ImageStorage.id << orphaned_storage_inner)
|
||||||
|
.execute())
|
||||||
|
logger.debug('Removed %s image storage records', storages_removed)
|
||||||
|
|
||||||
# We are going to make the conscious decision to not delete image storage blobs inside
|
# We are going to make the conscious decision to not delete image storage blobs inside
|
||||||
# transactions.
|
# transactions.
|
||||||
|
|
Reference in a new issue