Skip to content

Commit

Permalink
Fix context image chunks removal for updated honeypot frames
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiltsov-max committed Dec 19, 2024
1 parent 5c3522f commit 68d1771
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
27 changes: 26 additions & 1 deletion cvat/apps/engine/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,23 @@ def remove_segment_chunk(
self._make_chunk_key(db_segment, chunk_number=chunk_number, quality=quality)
)

def remove_segment_chunks(self, params: Sequence[dict[str, Any]]) -> None:
def remove_context_images_chunk(self, db_data: models.Data, frame_number: str) -> None:
self._delete_cache_item(
self._make_frame_context_images_chunk_key(db_data, frame_number=frame_number)
)

def remove_segments_chunks(self, params: Sequence[dict[str, Any]]) -> None:
"""
Removes several segment chunks from the cache.
The function expects a sequence of remove_segment_chunk() parameters as dicts.
"""
# TODO: add a version of this function
# that removes related cache elements as well (context images, previews, ...)
# to provide encapsulation

# TODO: add a generic bulk cleanup function for different objects, including related ones
# (likely a bulk key aggregator should be used inside to reduce requests count)

keys_to_remove = []
for item_params in params:
Expand All @@ -495,6 +506,20 @@ def remove_segment_chunks(self, params: Sequence[dict[str, Any]]) -> None:

self._bulk_delete_cache_items(keys_to_remove)

def remove_context_images_chunks(self, params: Sequence[dict[str, Any]]) -> None:
"""
Removes several context image chunks from the cache.
The function expects a sequence of remove_context_images_chunk() parameters as dicts.
"""

keys_to_remove = []
for item_params in params:
db_obj = item_params.pop("db_data")
keys_to_remove.append(self._make_frame_context_images_chunk_key(db_obj, **item_params))

self._bulk_delete_cache_items(keys_to_remove)

def get_cloud_preview(self, db_storage: models.CloudStorage) -> Optional[DataWithMime]:
return self._to_data_with_mime(self._get_cache_item(self._make_preview_key(db_storage)))

Expand Down
16 changes: 14 additions & 2 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,11 +1198,20 @@ def _to_abs_frame(rel_frame: int) -> int:
{'db_segment': db_segment, 'chunk_number': chunk_id, 'quality': quality}
)

context_image_chunks_to_be_removed = [
{"db_data": db_data, "frame_number": f} for f in updated_honeypots
]

if bulk_context:
bulk_context.chunks_to_be_removed.extend(chunks_to_be_removed)
bulk_context.context_image_chunks_to_be_removed.extend(
context_image_chunks_to_be_removed
)
bulk_context.segments_with_updated_chunks.append(db_segment.id)
else:
MediaCache().remove_segment_chunks(chunks_to_be_removed)
media_cache = MediaCache()
media_cache.remove_segments_chunks(chunks_to_be_removed)
media_cache.remove_context_images_chunks(context_image_chunks_to_be_removed)

db_segment.chunks_updated_date = timezone.now()
db_segment.save(update_fields=['chunks_updated_date'])
Expand Down Expand Up @@ -1352,6 +1361,7 @@ def __init__(
self.updated_honeypots: dict[int, models.Image] = {}
self.updated_segments: list[int] = []
self.chunks_to_be_removed: list[dict[str, Any]] = []
self.context_image_chunks_to_be_removed: list[dict[str, Any]] = []
self.segments_with_updated_chunks: list[int] = []

self.all_db_frames = all_db_frames
Expand Down Expand Up @@ -1525,7 +1535,9 @@ def _perform_bulk_updates(

# Import it here to avoid circular import
from cvat.apps.engine.cache import MediaCache
MediaCache().remove_segment_chunks(bulk_context.chunks_to_be_removed)
media_cache = MediaCache()
media_cache.remove_segments_chunks(bulk_context.chunks_to_be_removed)
media_cache.remove_context_images_chunks(bulk_context.context_image_chunks_to_be_removed)

# Update segments
updated_date = timezone.now()
Expand Down

0 comments on commit 68d1771

Please sign in to comment.