Only garbage collect storage when it might result in something. Return the count of the removed images to fulfill the contract expected by some callers.

This commit is contained in:
Jake Moshenko 2014-10-17 16:13:32 -04:00
parent b3292f8549
commit acd5a1e07e

View file

@ -1394,6 +1394,13 @@ def garbage_collect_repository(namespace_name, repository_name):
logger.info('Cleaning up unreferenced images: %s', to_remove)
Image.delete().where(Image.id << list(to_remove)).execute()
if len(storage_id_whitelist) > 0:
garbage_collect_storage(storage_id_whitelist)
return len(to_remove)
def garbage_collect_storage(storage_id_whitelist):
# We are going to make the conscious decision to not delete image storage inside the transaction
# This may end up producing garbage in s3, trading off for higher availability in the database
def placements_query_to_paths_set(placements_query):
@ -1411,6 +1418,7 @@ def garbage_collect_repository(namespace_name, repository_name):
.group_by(ImageStorage)
.having((fn.Count(Image.id) == 0) & (fn.Count(DerivedImageStorage.id) == 0)))
logger.debug('Garbage collecting storage from candidates: %s', storage_id_whitelist)
with config.app_config['DB_TRANSACTION_FACTORY'](db):
# Find out which derived storages will be removed, and add them to the whitelist
orphaned_from_candidates = orphaned_storage_query(ImageStorage.select(), storage_id_whitelist)