Skip to content

[python] Share video metadata cache across collators - #11438

Open
XiaoHongbo-Hope wants to merge 4 commits into
apache:masterfrom
XiaoHongbo-Hope:python/shared-video-metadata-cache
Open

XiaoHongbo-Hope wants to merge 4 commits into
apache:masterfrom
XiaoHongbo-Hope:python/shared-video-metadata-cache

Conversation

@XiaoHongbo-Hope

Copy link
Copy Markdown
Contributor

What changed

  • Expose VideoMetadataCache so collators in the same process can share parsed MP4 metadata while retaining the existing per-collator default.
  • Coalesce concurrent first loads of the same video metadata key and reuse the cache in the built-in indexed PyAV and TorchCodec paths.
  • Keep shared cache ownership with the caller; a collator close or DataLoader process boundary does not clear another collator's metadata.
  • Document the process-local scope and add concurrent reader and cache lifecycle tests.

Why

Multiple VideoFrameCollator instances serving requests for the same physical video can each parse its MP4 sample table. Sharing a bounded cache avoids that repeated metadata work. The cache remains process-local; applications with separate DataLoader processes need a shared media service if they want reuse across those processes.

Tests

  • python -m pytest -q paimon-python/pypaimon/tests/multimodal_video_test.py paimon-python/pypaimon/tests/mp4_test.py paimon-python/pypaimon/tests/indexed_torchcodec_test.py (74 passed, 37 skipped; optional TorchCodec/PyTorch cases unavailable in this environment)
  • git diff --check

@XiaoHongbo-Hope
XiaoHongbo-Hope force-pushed the python/shared-video-metadata-cache branch from f443f00 to 76eea65 Compare November 25, 2026 08:34
@XiaoHongbo-Hope
XiaoHongbo-Hope marked this pull request as draft November 25, 2026 09:08
@XiaoHongbo-Hope
XiaoHongbo-Hope marked this pull request as ready for review November 25, 2026 10:48

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the cache key before merging. The documentation and test comments are suggestions.

self._decode_pool = _VideoDecodePool()
self._decoders = OrderedDict()
self._video_metadata_cache = _VideoMetadataCache()
self._video_metadata_cache = (metadata_cache if metadata_cache is not None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Fix before merge

The shared cache uses the video descriptor as its key, while each collator resolves that descriptor through its own FileIO. Two S3-compatible endpoints can serve different MP4s for the same URI and byte range. In a local repro with two FileIO test doubles and real MP4s, the second PyAV collator returned frame ordinal 7 for a request for ordinal 5 after reusing the first video's index.

Please include the resolved storage namespace in the cache key. When a stable namespace is unavailable, scope entries to the FileIO instance. A regression test should cover two sources that resolve the same descriptor to different media.

Comment thread docs/docs/pypaimon/video.md Outdated
in each DataLoader worker. `decoder_factory(stream)` opens any codec library;
`decode_fn(decoder, frame_index, row)` receives the embedded frame ordinal.

To share parsed MP4 indexes across collators in one process, pass the same

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Suggestion

The SequentialPyAvDecoder example calls av.open() for every decoder session, so passing metadata_cache to these collators still repeats MP4 parsing. The public LeRobot and window dataset APIs also create private per-collator caches. Consider passing metadata_cache through an existing public indexed reader API and showing two collators reuse one index in the same process.

stream.video_metadata_cache = cache
stream.video_metadata_key = 'same-video'
stream.video_read_ranges = lambda ranges: [mp4[o:o + n] for o, n in ranges]
barrier.wait(timeout=5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Suggestion

The barrier releases the callers together, so the first parse can finish before any other caller checks the cache. All three new concurrency test cases passed locally with get_or_load replaced by a plain lookup, load, and store. Consider holding the first loader with an event until the other calls are waiting on it, then releasing it and asserting that the loader ran once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants