def _get_dangling_storage_count(): storage_ids = set([current.id for current in ImageStorage.select()]) referenced_by_image = set([image.storage_id for image in Image.select()]) referenced_by_manifest = set([blob.blob_id for blob in ManifestBlob.select()]) referenced_by_derived = set( [derived.derivative_id for derived in DerivedStorageForImage.select()] ) return len(storage_ids - referenced_by_image - referenced_by_derived - referenced_by_manifest)
def _garbage_collect_legacy_image(legacy_image_id, context): assert legacy_image_id is not None # Check if the image is referenced. if _check_image_used(legacy_image_id): return False # We have an unreferenced image. We can now delete it. # Grab any derived storage for the image. for derived in DerivedStorageForImage.select().where( DerivedStorageForImage.source_image == legacy_image_id): context.add_blob_id(derived.derivative_id) try: image = Image.select().where(Image.id == legacy_image_id).get() except Image.DoesNotExist: return False assert image.repository_id == context.repository.id # Add the image's blob to be GCed. context.add_blob_id(image.storage_id) # If the image has a parent ID, add the parent for GC. if image.parent_id is not None: context.add_legacy_image_id(image.parent_id) # Delete the image. with db_transaction(): if _check_image_used(legacy_image_id): return False try: image = Image.select().where(Image.id == legacy_image_id).get() except Image.DoesNotExist: return False assert image.id == legacy_image_id assert image.repository_id == context.repository.id # Delete any derived storage for the image. deleted_derived_storage = (DerivedStorageForImage.delete().where( DerivedStorageForImage.source_image == legacy_image_id).execute()) # Delete the image itself. image.delete_instance() context.mark_legacy_image_removed(image) gc_table_rows_deleted.labels(table="Image").inc() gc_table_rows_deleted.labels( table="DerivedStorageForImage").inc(deleted_derived_storage) if config.image_cleanup_callbacks: for callback in config.image_cleanup_callbacks: callback([image]) return True
def find_derived_storage_for_image(source_image, transformation_name, varying_metadata=None): uniqueness_hash = _get_uniqueness_hash(varying_metadata) try: found = (DerivedStorageForImage.select( ImageStorage, DerivedStorageForImage).join(ImageStorage).switch( DerivedStorageForImage).join(ImageStorageTransformation).where( DerivedStorageForImage.source_image == source_image, ImageStorageTransformation.name == transformation_name, DerivedStorageForImage.uniqueness_hash == uniqueness_hash, ).get()) return found except DerivedStorageForImage.DoesNotExist: return None