Skip to content

fix:block_cache_size#3169

Closed
chejinge wants to merge 2 commits intoOpenAtomFoundation:unstablefrom
chejinge:block_size
Closed

fix:block_cache_size#3169
chejinge wants to merge 2 commits intoOpenAtomFoundation:unstablefrom
chejinge:block_size

Conversation

@chejinge
Copy link
Copy Markdown
Collaborator

@chejinge chejinge commented Sep 24, 2025

Summary by CodeRabbit

  • New Features

    • Expanded cache options: explicitly support disabling cache, using a shared global cache, or using a per-instance cache for flexible deployment.
  • Improvements

    • Safer initialization with validation for shared cache and clearer behavior when switching cache modes.
    • More predictable performance and memory usage due to explicit cache handling and resets.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Sep 24, 2025

Walkthrough

Adds tri-state block cache handling in src/pika_server.cc: disable cache when block_cache_size == 0, use shared global cache when share_block_cache is set (asserts shared cache initialized), or create a per-instance LRU cache otherwise; preserves rate limiter and other storage option setup.

Changes

Cohort / File(s) Summary
Storage options and cache init
src/pika_server.cc
Implements tri-state block cache handling: disable when block_cache_size == 0 (clear per-instance), use shared global cache if share_block_cache is true (assert initialized), otherwise create per-instance LRU cache. Adds explicit no_block_cache handling; preserves rate limiter and other options.

Sequence Diagram(s)

sequenceDiagram
    participant Server
    participant StorageOptions
    participant SharedCache as Shared Block Cache
    participant LRU as Per-Instance LRU Cache

    Server->>StorageOptions: InitializeStorageOptions()
    alt block_cache_size == 0
        StorageOptions->>StorageOptions: no_block_cache = true\nreset per-instance block_cache
    else share_block_cache == true
        StorageOptions->>SharedCache: assert initialized
        StorageOptions->>StorageOptions: no_block_cache = false\nuse shared_block_cache_
    else
        StorageOptions->>LRU: create(size=block_cache_size)
        StorageOptions->>StorageOptions: no_block_cache = false\nset per-instance block_cache
    end
    Server->>StorageOptions: apply rate_limiter and other options
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I thump my paws on caches three,
Zero size — I sleep, no blocks for me.
Shared burrow ready? I peek and board,
Else build an LRU nest adored.
Hops and bytes, the server sings.

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Title Check ⚠️ Warning The title "fix:block_cache_size" is excessively terse and uses punctuation and underscores, which reduces readability and fails to form a clear sentence summarizing the main change. It does identify block_cache_size but does not explain what is fixed or the context of the update. Title guidelines recommend a short, descriptive sentence that highlights the key change in human-readable form. Consider revising the PR title to a clear, concise sentence that captures the main change. For example, "Fix block cache handling based on block_cache_size and shared cache flag" or "Explicitly disable block cache when block_cache_size is zero in InitStorageOptions." This will improve readability and clarity for reviewers scanning the change history.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added the ☢️ Bug Something isn't working label Sep 24, 2025
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between abfaa02 and e179629.

📒 Files selected for processing (1)
  • src/pika_server.cc (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: build_on_macos
  • GitHub Check: build_on_ubuntu
  • GitHub Check: build_on_centos
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
src/pika_server.cc (1)

1482-1496: LGTM! Block cache configuration logic is correctly implemented.

The implementation properly handles all three cache modes with clear branching logic. The assertion for shared cache initialization is a good defensive programming practice to catch configuration errors early.

Comment thread src/pika_server.cc
Comment on lines +1482 to +1496
if (storage_options_.block_cache_size == 0) {
storage_options_.table_options.no_block_cache = true;
storage_options_.table_options.block_cache.reset();
} else if (storage_options_.share_block_cache) {
// 共享模式:直接复用已创建好的全局 cache(PikaServer 成员)
storage_options_.table_options.no_block_cache = false;
assert(share_block_cache_ && "shared_block_cache_ must be initialized before InitStorageOptions()");
storage_options_.table_options.block_cache = shared_block_cache_;
} else {
// 非共享模式:为当前 CF/实例单独创建
storage_options_.table_options.no_block_cache = false;
storage_options_.table_options.block_cache =
rocksdb::NewLRUCache(storage_options_.block_cache_size,
static_cast<int>(g_pika_conf->num_shard_bits()));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

🧩 Analysis chain

Verify shared_block_cache_ initialization order.

The code assumes shared_block_cache_ is initialized before InitStorageOptions() is called. Please verify that the shared cache is properly initialized in the constructor or startup sequence before this method is invoked.


🏁 Script executed:

#!/bin/bash
# Search for shared_block_cache_ initialization in the codebase
rg -n "shared_block_cache_.*=" --type cpp -A 3 -B 3

# Search for InitStorageOptions calls to understand initialization order
rg -n "InitStorageOptions" --type cpp -A 3 -B 3

# Look for shared_block_cache_ declaration
ast-grep --pattern 'shared_ptr<$_> shared_block_cache_'

Length of output: 1539


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Searching for share_block_cache_ / shared_block_cache_ occurrences..."
rg -n "share_block_cache_|shared_block_cache_" -S || true

echo
echo "Show declaration area in include/pika_server.h (lines 1-260):"
sed -n '1,260p' include/pika_server.h || true

echo
echo "Show top of src/pika_server.cc (lines 1-140) to see constructor/init order:"
sed -n '1,140p' src/pika_server.cc || true

echo
echo "Search for places that create or assign a block cache (NewLRUCache or MakeLRUCache or Assign to *_block_cache):"
rg -n "NewLRUCache|MakeLRUCache|_block_cache" -S || true

echo
echo "Search for functions/methods that might initialize a shared cache (InitShared|InitBlockCache|CreateSharedCache):"
rg -n "Init.*Block|Init.*Cache|Create.*Cache|shared.*cache" -S || true

Length of output: 126768


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Search for 'shared_block_cache' variants across the repo:"
rg -n "shared_block_cache|share_block_cache_|shared_block_cache_" -S || true

echo
echo "Show remainder of include/pika_server.h to find member declarations (lines 260-999):"
sed -n '260,999p' include/pika_server.h || true

echo
echo "Search for any member declarations referencing rocksdb::Cache or Cache pointer in headers:"
rg -n "rocksdb::Cache|std::shared_ptr<.*Cache>" -S || true

echo
echo "Show PikaServer private members area by locating closing of class (search for 'private:' within header):"
rg -n "class PikaServer|private:" include/pika_server.h -n -n -S || true
sed -n '420,999p' include/pika_server.h || true

Length of output: 18930


Fix shared_block_cache_ initialization and correct the assert typo.

InitStorageOptions() is called from the constructor (src/pika_server.cc:57). Inside InitStorageOptions (src/pika_server.cc:1426-1490) you assign storage_options_.table_options.block_cache = shared_block_cache_ but there is no observable declaration/initialization of shared_block_cache_ before that call, and the assert checks share_block_cache_ (typo). Initialize shared_block_cache_ before InitStorageOptions() (or move the InitStorageOptions() call) and change the assert to assert(shared_block_cache_ && ...) or otherwise guard the assignment.

🤖 Prompt for AI Agents
In src/pika_server.cc around lines 1482-1496, InitStorageOptions() assigns
storage_options_.table_options.block_cache = shared_block_cache_ but
shared_block_cache_ is not guaranteed initialized and the assert erroneously
checks share_block_cache_; fix by correcting the assert to
assert(shared_block_cache_ && "shared_block_cache_ must be initialized before
InitStorageOptions()"), and ensure shared_block_cache_ is created/initialized
before InitStorageOptions() is called (either initialize it in the constructor
prior to calling InitStorageOptions() or move the InitStorageOptions() call
after shared_block_cache_ creation); alternatively add a runtime guard so you
only assign shared_block_cache_ when it is non-null to avoid dereferencing an
uninitialized pointer.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e179629 and 878e918.

📒 Files selected for processing (1)
  • src/pika_server.cc (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: build_on_ubuntu
  • GitHub Check: build_on_macos
  • GitHub Check: build_on_centos
  • GitHub Check: Analyze (go)

Comment thread src/pika_server.cc
Comment on lines +1485 to +1493
} else if (storage_options_.share_block_cache) {
if (!g_pika_conf->share_block_cache()) {
assert(false && "shared_block_cache_ must be initialized before InitStorageOptions()");
}

if (storage_options_.block_cache_size == 0) {
storage_options_.table_options.no_block_cache = true;
} else if (storage_options_.share_block_cache) {
storage_options_.table_options.block_cache =
rocksdb::NewLRUCache(storage_options_.block_cache_size, static_cast<int>(g_pika_conf->num_shard_bits()));
}
storage_options_.table_options.no_block_cache = false;

storage_options_.table_options.block_cache = 0;
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Shared cache branch never attaches the shared cache

When share_block_cache is enabled we still leave block_cache as nullptr. RocksDB will silently fall back to its default per-instance cache instead of reusing the intended global cache, so the “shared” mode never actually shares anything. The guard also re-checks the config flag instead of validating that shared_block_cache_ has been initialized.

Wire the shared cache through (and assert it exists) before we flip no_block_cache off:

-    } else if (storage_options_.share_block_cache) {
-        if (!g_pika_conf->share_block_cache()) {
-            assert(false && "shared_block_cache_ must be initialized before InitStorageOptions()");
-        }
-
-        storage_options_.table_options.no_block_cache = false;
-
-        storage_options_.table_options.block_cache = 0;
+    } else if (storage_options_.share_block_cache) {
+        storage_options_.table_options.no_block_cache = false;
+        assert(shared_block_cache_ && "shared_block_cache_ must be initialized before InitStorageOptions()");
+        storage_options_.table_options.block_cache = shared_block_cache_;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} else if (storage_options_.share_block_cache) {
if (!g_pika_conf->share_block_cache()) {
assert(false && "shared_block_cache_ must be initialized before InitStorageOptions()");
}
if (storage_options_.block_cache_size == 0) {
storage_options_.table_options.no_block_cache = true;
} else if (storage_options_.share_block_cache) {
storage_options_.table_options.block_cache =
rocksdb::NewLRUCache(storage_options_.block_cache_size, static_cast<int>(g_pika_conf->num_shard_bits()));
}
storage_options_.table_options.no_block_cache = false;
storage_options_.table_options.block_cache = 0;
} else {
} else if (storage_options_.share_block_cache) {
storage_options_.table_options.no_block_cache = false;
assert(shared_block_cache_ && "shared_block_cache_ must be initialized before InitStorageOptions()");
storage_options_.table_options.block_cache = shared_block_cache_;
} else {
🤖 Prompt for AI Agents
In src/pika_server.cc around lines 1485-1493, the shared-cache branch flips
table_options.no_block_cache to false but leaves table_options.block_cache as
nullptr and re-checks g_pika_conf instead of validating the actual
shared_block_cache_ object; update this branch to assert that
shared_block_cache_ is initialized (not the config flag) and assign the shared
cache to storage_options_.table_options.block_cache before clearing
no_block_cache so RocksDB uses the intended global cache.

@chejinge chejinge closed this Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants