Batch notebook diff cache reads per file collection

What does this MR do and why?

Diff file collections now read the render cache entries of all their notebook (.ipynb) files with one Rails.cache.read_multi, instead of one read per notebook. The collection decorated each file one by one, so a page with many notebooks made one Redis call per notebook. Nothing changes for callers that decorate a single file (DiffNote#fetch_diff_file), and the rendered output is the same.

Design notes

Two passes in Gitlab::Diff::FileCollection::Base#raw_diff_files. decorate_diff! now only builds the Gitlab::Diff::File. When use_extra_viewer_as_main is set, render_notebooks! runs after every file exists: it prefetches the cache entries of the files for which Gitlab::Diff::File#renderable_notebook? holds (the guard that #rendered used inline), then swaps each renderable notebook for its rendered file, as decorate_diff! did. The stored merge request diffs (Gitlab::Git::Diff::SERIALIZE_KEYS) carry no blob IDs, so the cache keys can only be built once the blobs are loaded; the blobs come from the Blob.lazy batch that the Gitlab::Diff::File constructor fills, and building every file before the first blob read also puts all blobs of the collection in one Gitaly call (measured locally on a commit with three notebooks: three GetBlobs calls on master, one with this change).

Prefetch. Gitlab::Diff::Rendered::Notebook::DiffCache.prefetch(repository, diff_files) builds the key of each file and loads the entries with Gitlab::SafeRequestLoader (one read_multi for the keys the request store does not hold yet, absent keys stored as nil). #fetch reads the prefetched entry from the request store and falls back to Rails.cache.read when the key was not prefetched, so single-file callers behave as before. A write (an entry or a marker) also updates the request store, so two files of the same blobs in one collection still render once. Without an active request store, or with cache_notebook_diffs disabled, prefetch does nothing.

Redis Cluster. The keys of different blob pairs hash to different slots. Gitlab::Patch::RedisCacheStore#read_multi_entries pipelines the read on a cluster, so the call runs inside RedisClusterValidator.allow_cross_slot_commands, as Gitlab::RepositoryCache::Preloader does.

Unflagged. The change is a pure read batching with the same output; rollback is a revert. cache_notebook_diffs still gates all cache use.

Specs. spec/lib/gitlab/diff/file_collection/base_spec.rb: a commit with three notebooks (changed, added, deleted) performs one read_multi with the three keys and no read, and the batched files equal (class, diff text, highlighted lines) the files rendered through single reads. spec/lib/gitlab/diff/rendered/notebook/diff_cache_spec.rb: .prefetch with and without a request store, hits and misses, write-through to the request store, and the three cases that read nothing. spec/requests/projects/merge_requests/notebook_diff_render_cache_spec.rb: diffs_batch (anonymous and signed in) and diffs_stream read the two notebook entries in one call.

Parent MRs, sibling MR, ownership
  • Parents: !263925 (merged) (render cache, cache_notebook_diffs), !263894 (merged) (reduce_notebook_diff_rendering), !263933 (merged) (stop_notebook_text_diff_at_timeout).
  • Sibling from the same follow-up issue: !268426 (merged) bounds the rendered notebook size inside gems/ipynbdiff (cap_rendered_notebook_size). It changes lib/gitlab/diff/rendered/notebook/diff_file.rb and the render_and_write rescue in diff_cache.rb; this MR changes fetch, so the two do not conflict in code and the merge order does not matter.
  • Ownership: Gitlab::Diff::Rendered::Notebook belongs to MLOps; Gitlab::Diff::File and the file collections belong to Code Review (code_review_workflow), so Code Review should co-review.
  • No database changes.

References

Screenshots or screen recordings

No UI change.

How to set up and validate locally

  1. In a GDK project, upload two Jupyter notebooks to the default branch (+ > Upload file), then edit one cell's source in each on a new branch and click "Create merge request".
  2. In a terminal in the GDK gitlab directory, run tail -f log/application_json.log | grep IPYNB_DIFF_.
  3. Open the merge request's Changes tab. Expect: both rendered notebooks, and one IPYNB_DIFF_GENERATED line per notebook.
  4. Reload the page. Expect: the same rendered notebooks, and two IPYNB_DIFF_CACHE_HIT lines.
  5. In the Rails console, run Gitlab::Redis::Cache.with { |r| r.monitor { |line| puts line if line.include?('notebook_diff') } }, then reload the page. Expect: one MGET (or one pipelined read) with both notebook_diff: keys, and no GET for them.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Eduardo Bonet

Merge request reports

Loading
Loading