Skip to content

[ISSUE #10432] Optimize RocksDB ConsumeQueue sequential reads with a bounded iterator - #11596

Open
Drifter-yh wants to merge 1 commit into
apache:developfrom
Drifter-yh:optimize-rocksdb-cq-sequential-read-10432
Open

Drifter-yh wants to merge 1 commit into
apache:developfrom
Drifter-yh:optimize-rocksdb-cq-sequential-read-10432

Conversation

@Drifter-yh

@Drifter-yh Drifter-yh commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Which Issue(s) This PR Fixes

Brief Description

RocksDBConsumeQueueTable#rangeQuery currently builds one RocksDB key per ConsumeQueue offset and calls multiGet with all of them. The common read path (iteratorWhenUseRocksdbConsumeQueue=true, RocksDBReusableIterator) requests a contiguous batch, so those keys are adjacent in the ConsumeQueue column family.

This change replaces the N point lookups with one bounded iterator scan:

  • ConsumeQueueRocksDBStorage#scanCQ creates a RocksIterator, seeks to the start key once, and iterates values in key order. ReadOptions#setIterateUpperBound bounds the scan at startIndex + num, so it cannot walk into a neighbouring queue or topic even when the next queue's offset would line up.
  • rangeQuery preserves the existing gap semantics: it stops at the first offset that is not exactly startIndex + returnedCount. Values are therefore still consecutive CQ units starting at startIndex, and callers can keep advancing offsets by one per returned unit.
  • A request for a single entry (num == 1) still uses a point get, because iterator setup costs more than one lookup.
  • The external iterator contract (iteratorWhenUseRocksdbConsumeQueue, RocksDBReusableIterator, LMQ behavior) is unchanged.

Benchmark

Local harness in a single JVM (storage + table, no running broker): 500,000 consecutive CQ entries for one topic/queue (28-byte values), written through the production buildAndPutCQByteBuffer path, flushed and compacted. Each measured request uses a random start offset ("random") or advances by the batch size ("sequential"). 3,000 warmup + 10,000 measured calls per data point, mean latency. JDK 17.0.14, Windows 11, i9-13900HX, RocketMQ 5.5.1 develop @ 8764dd7. "Before" is the removed multiGet implementation executed against the same DB in the same JVM; "after" is this PR.

Warm block cache (dataset is smaller than the 1 GB CQ block cache):

batch before random (µs) after random (µs) before sequential (µs) after sequential (µs)
1 10.39 7.78 10.26 5.56
2 14.93 13.96 12.16 12.00
4 21.67 13.89 17.89 12.16
8 34.99 16.04 32.03 13.80
16 58.55 19.73 59.13 15.52
32 117.95 26.17 112.12 25.14
64 211.11 40.77 211.34 38.12
128 410.36 67.09 411.41 67.09
256 826.65 119.96 827.89 119.66

The default consumer batch size is 32, where this is roughly a 4.5x latency reduction (78% lower). Batch 1 uses the point-get path.

After a RocksDB restart with a fresh block cache (500 random queries, OS page cache warm): batch 32 went from 178.96 µs to 96.22 µs, and batch 64 from 302.94 µs to 129.56 µs. So the iterator path also wins when blocks are not cached, and does not add read-ahead that hurts random reads.

How Did You Test This Change?

  • New RocksDBConsumeQueueTableRangeQueryTest (10 cases, real RocksDB): consecutive offsets and order, gap at the start, gap in the middle, stop at requested count, stop at end of available offsets, no crossing into another queue (with an intentionally colliding offset), no crossing into another topic, offsets larger than Integer.MAX_VALUE, single-entry and zero-count requests.
  • mvn -B -ntp -pl store -am -Dtest=RocksDBConsumeQueueTableRangeQueryTest,RocksDBConsumeQueueTest,RocksDBConsumeQueueStoreTest,RocksDBConsumeQueueTableTest,RocksDBConsumeQueueOffsetTableTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false test on Windows 11, Microsoft OpenJDK 17.0.14: 27 tests passed, 0 failures/errors/skips.
  • mvn -B -ntp -pl store -am -DskipTests package succeeds; Checkstyle and SpotBugs pass.
  • git diff --check passes.

The benchmark harness is a local test, not committed, to avoid adding a slow and machine-dependent test to CI. I can attach it if you want to reproduce the numbers.

AI assistance was used for analysis, implementation, and tests; the changes were reviewed and tested as described above.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.84848% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.24%. Comparing base (8764dd7) to head (ab37308).
⚠️ Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
...etmq/store/rocksdb/ConsumeQueueRocksDBStorage.java 72.22% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop   #11596   +/-   ##
==========================================
  Coverage      50.23%   50.24%           
- Complexity     14598    14605    +7     
==========================================
  Files           1391     1391           
  Lines         103467   103471    +4     
  Branches       13559    13557    -2     
==========================================
+ Hits           51981    51993   +12     
+ Misses         45237    45235    -2     
+ Partials        6249     6243    -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Drifter-yh

Copy link
Copy Markdown
Contributor Author

CI update:

  • Maven matrix (Windows, macOS, Ubuntu; JDK 8), Coverage, Integration Tests, CodeQL, license and misspell checks all pass.
  • The only red check is Bazel: PopPriorityIT failed, which is a known flaky test tracked in [Enhancement] Fix or ignore several flaky tests of PopPriorityIT #10389. In that same run the new RocksDBConsumeQueueTableRangeQueryTest passed, along with the other 622 tests.
  • I do not have permission to re-run workflows on apache/rocketmq; a maintainer re-run of the Bazel job should clear that check.

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.

[Enhancement] Optimize RocksDB ConsumeQueue iterator for sequential reads

2 participants