-
Notifications
You must be signed in to change notification settings - Fork 10
Default the RocksDB WriteBufferManager on at 1/3 of block cache #1251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('node:assert/strict'); | ||
| const { resolveRocksMemoryConfig } = require('#src/utility/rocksMemoryConfig'); | ||
|
|
||
| const GB = 1024 * 1024 * 1024; | ||
|
|
||
| function resolve(overrides) { | ||
| return resolveRocksMemoryConfig({ | ||
| configuredBlockCacheSize: undefined, | ||
| configuredWriteBufferManagerSize: undefined, | ||
| configuredCostToCache: undefined, | ||
| configuredAllowStall: undefined, | ||
| availableMemory: 8 * GB, | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| describe('resolveRocksMemoryConfig', function () { | ||
| describe('block cache', function () { | ||
| it('defaults to 25% of available memory when unset', function () { | ||
| assert.strictEqual(resolve({}).blockCacheSize, 2 * GB); | ||
| }); | ||
|
|
||
| it('honors an explicit positive size', function () { | ||
| assert.strictEqual(resolve({ configuredBlockCacheSize: 512 * 1024 * 1024 }).blockCacheSize, 512 * 1024 * 1024); | ||
| }); | ||
|
|
||
| it('falls back to the default for zero, negative, or non-number values', function () { | ||
| assert.strictEqual(resolve({ configuredBlockCacheSize: 0 }).blockCacheSize, 2 * GB); | ||
| assert.strictEqual(resolve({ configuredBlockCacheSize: -1 }).blockCacheSize, 2 * GB); | ||
| assert.strictEqual(resolve({ configuredBlockCacheSize: 'big' }).blockCacheSize, 2 * GB); | ||
| }); | ||
| }); | ||
|
|
||
| describe('WriteBufferManager size', function () { | ||
| it('defaults to 1/3 of the resolved block cache when unset', function () { | ||
| const config = resolve({}); | ||
| assert.strictEqual(config.writeBufferManagerSize, Math.floor((2 * GB) / 3)); | ||
| }); | ||
|
|
||
| it('defaults relative to an explicit block cache size', function () { | ||
| const config = resolve({ configuredBlockCacheSize: 900 }); | ||
| assert.strictEqual(config.writeBufferManagerSize, 300); | ||
| }); | ||
|
|
||
| it('honors an explicit positive size', function () { | ||
| assert.strictEqual( | ||
| resolve({ configuredWriteBufferManagerSize: 256 * 1024 * 1024 }).writeBufferManagerSize, | ||
| 256 * 1024 * 1024 | ||
| ); | ||
| }); | ||
|
|
||
| it('disables the WBM entirely when explicitly set to 0', function () { | ||
| const config = resolve({ configuredWriteBufferManagerSize: 0 }); | ||
| assert.ok(!('writeBufferManagerSize' in config)); | ||
| assert.ok(!('writeBufferManagerCostToCache' in config)); | ||
| assert.ok(!('writeBufferManagerAllowStall' in config)); | ||
| }); | ||
|
|
||
| it('falls back to the default for non-number values', function () { | ||
| assert.strictEqual( | ||
| resolve({ configuredWriteBufferManagerSize: 'lots' }).writeBufferManagerSize, | ||
| Math.floor((2 * GB) / 3) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('integer flooring', function () { | ||
| it('floors a fractional block cache default', function () { | ||
| // 10 * 0.25 = 2.5 -> 2 | ||
| assert.strictEqual(resolve({ availableMemory: 10 }).blockCacheSize, 2); | ||
| }); | ||
|
|
||
| it('floors a fractional WriteBufferManager default', function () { | ||
| // floor(10 / 3) = 3 | ||
| const config = resolve({ configuredBlockCacheSize: 10 }); | ||
| assert.strictEqual(config.writeBufferManagerSize, 3); | ||
| }); | ||
|
|
||
| it('produces integer sizes for a realistic constrained memory limit', function () { | ||
| // 6 GB cgroup limit: block cache 1.5 GB, WBM 1.5 GB / 3 | ||
| const config = resolve({ availableMemory: 6 * GB }); | ||
| assert.ok(Number.isInteger(config.blockCacheSize)); | ||
| assert.ok(Number.isInteger(config.writeBufferManagerSize)); | ||
| }); | ||
| }); | ||
|
|
||
| describe('costToCache and allowStall', function () { | ||
| it('both default to true when the WBM is enabled', function () { | ||
| const config = resolve({}); | ||
| assert.strictEqual(config.writeBufferManagerCostToCache, true); | ||
| assert.strictEqual(config.writeBufferManagerAllowStall, true); | ||
| }); | ||
|
|
||
| it('honor explicit false values', function () { | ||
| const config = resolve({ configuredCostToCache: false, configuredAllowStall: false }); | ||
| assert.strictEqual(config.writeBufferManagerCostToCache, false); | ||
| assert.strictEqual(config.writeBufferManagerAllowStall, false); | ||
| }); | ||
|
|
||
| it('fall back to true for non-boolean values', function () { | ||
| const config = resolve({ configuredCostToCache: 'yes', configuredAllowStall: 1 }); | ||
| assert.strictEqual(config.writeBufferManagerCostToCache, true); | ||
| assert.strictEqual(config.writeBufferManagerAllowStall, true); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Resolves the RocksDB memory configuration (block cache + WriteBufferManager) from raw | ||
| // config values. Kept as a pure function so the defaulting logic can be unit tested without | ||
| // opening a database or touching the process-global RocksDatabase.config side effect. | ||
| // | ||
| // Values flow in from configUtils.castConfigValue (via envGet), which produces proper | ||
| // numbers/booleans/null — so we enforce types rather than coerce, and anything that isn't the | ||
| // expected type falls through to the default. | ||
|
|
||
| export interface RocksMemoryConfigInput { | ||
| configuredBlockCacheSize: unknown; | ||
| configuredWriteBufferManagerSize: unknown; | ||
| configuredCostToCache: unknown; | ||
| configuredAllowStall: unknown; | ||
| // min(process.constrainedMemory() ?? Infinity, totalmem()) — the cgroup-aware memory base. | ||
| availableMemory: number; | ||
| } | ||
|
|
||
| export interface RocksMemoryConfig { | ||
| blockCacheSize: number; | ||
| writeBufferManagerSize?: number; | ||
| writeBufferManagerCostToCache?: boolean; | ||
| writeBufferManagerAllowStall?: boolean; | ||
| } | ||
|
|
||
| export function resolveRocksMemoryConfig(input: RocksMemoryConfigInput): RocksMemoryConfig { | ||
| const { | ||
| configuredBlockCacheSize, | ||
| configuredWriteBufferManagerSize, | ||
| configuredCostToCache, | ||
| configuredAllowStall, | ||
| availableMemory, | ||
| } = input; | ||
| // Block cache: an explicit positive number wins, otherwise 25% of available memory. Floored | ||
| // because RocksDB expects integer byte counts and the percentage math produces fractions. | ||
| const blockCacheSize = Math.floor( | ||
| typeof configuredBlockCacheSize === 'number' && configuredBlockCacheSize > 0 | ||
| ? configuredBlockCacheSize | ||
| : availableMemory * 0.25 | ||
| ); | ||
| // WriteBufferManager size: an explicit number is honored (0 disables); any other value | ||
| // (unset/misconfigured) defaults to 1/3 of the block cache. Floored for the same reason. | ||
| const writeBufferManagerSize = Math.floor( | ||
| typeof configuredWriteBufferManagerSize === 'number' ? configuredWriteBufferManagerSize : blockCacheSize / 3 | ||
| ); | ||
|
Comment on lines
+40
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding inline defensive checks for References
|
||
| const config: RocksMemoryConfig = { blockCacheSize }; | ||
| // costToCache and allowStall only matter when the WBM is enabled. allowStall defaults to true | ||
| // so the buffer applies write backpressure rather than letting memtables grow unbounded, which | ||
| // also keeps bulk ingest from outrunning the memtable flush/conflict-check window. | ||
| if (writeBufferManagerSize > 0) { | ||
| config.writeBufferManagerSize = writeBufferManagerSize; | ||
| config.writeBufferManagerCostToCache = typeof configuredCostToCache === 'boolean' ? configuredCostToCache : true; | ||
| config.writeBufferManagerAllowStall = typeof configuredAllowStall === 'boolean' ? configuredAllowStall : true; | ||
| } | ||
| return config; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding defensive runtime checks for
configuredBlockCacheSizein the resolver, ensure this configuration parameter is explicitly defined and validated in the Joi validation schema. This prevents invalid values likeInfinityor non-numbers from bypassing validation while keeping the implementation free of redundant guards.References
allowUnknown: trueis enabled, potentially leading to runtime errors.