mirror of
https://github.com/facebook/rocksdb.git
synced 2026-11-06 19:12:36 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
937bcacfe9 | ||
|
|
f051e35ce8 | ||
|
|
9ed9d29162 | ||
|
|
336e4f9db1 | ||
|
|
b8672a8101 | ||
|
|
53f215f6d4 | ||
|
|
75da341340 | ||
|
|
e603f55e5f | ||
|
|
1c634ade4a | ||
|
|
81ee59210c | ||
|
|
08d8122809 | ||
|
|
7be9ffc81a | ||
|
|
1d37a88852 | ||
|
|
7b0019a1b8 | ||
|
|
d3a28293a5 | ||
|
|
2b3cdc5764 | ||
|
|
6c65d8046d | ||
|
|
2126b6e3ba | ||
|
|
6ff96384dd | ||
|
|
a4139ef9e9 | ||
|
|
3ef8b1b743 | ||
|
|
4bc0bff0e4 | ||
|
|
49ec219955 | ||
|
|
790294f3ad | ||
|
|
9886d52fc4 | ||
|
|
36e50a61fc | ||
|
|
7b06a9ae7e | ||
|
|
df5695cf24 | ||
|
|
820c4a77c1 | ||
|
|
94b6566505 | ||
|
|
1cc216a45b | ||
|
|
978bf67426 | ||
|
|
6b8ca89f45 | ||
|
|
50852b5c8d | ||
|
|
9f474a1034 | ||
|
|
f3a20854e0 | ||
|
|
c47e35395b | ||
|
|
a9ac4df630 | ||
|
|
4254c705a2 | ||
|
|
bad2d5b0af | ||
|
|
1fe1b1bd46 | ||
|
|
f3f7feba92 | ||
|
|
b7395b3fa4 | ||
|
|
3b38fde293 | ||
|
|
3070f73e97 | ||
|
|
9d609dd6fe | ||
|
|
4d8a0acd76 | ||
|
|
3efe9460ba | ||
|
|
4dd05023c0 | ||
|
|
6a618d0686 | ||
|
|
ec832ff42c | ||
|
|
91fa765f3a | ||
|
|
92cedd58af | ||
|
|
3dd6d060e5 | ||
|
|
1b817cff45 | ||
|
|
fc85a700cf | ||
|
|
06699cd0fb | ||
|
|
0186937710 | ||
|
|
8ec2177bd1 | ||
|
|
156328107d | ||
|
|
3f51c0a185 | ||
|
|
65e77282ce | ||
|
|
a0ff5b143f | ||
|
|
64f8d5765b | ||
|
|
4cf28450a5 | ||
|
|
ae0c9faaef | ||
|
|
51561fc2cd | ||
|
|
ebd118711c | ||
|
|
ffe58fa976 | ||
|
|
d6b03d9c96 | ||
|
|
abc1f7ced5 | ||
|
|
01ec15f328 | ||
|
|
af4e32945b | ||
|
|
b611aa7309 | ||
|
|
830347c765 | ||
|
|
de6cdbb62d | ||
|
|
b8bbee0f3e | ||
|
|
24fac74206 | ||
|
|
f12465cb93 | ||
|
|
7832ee886f | ||
|
|
ae322e0a73 | ||
|
|
1e23ee053f | ||
|
|
effac3a918 | ||
|
|
c7f0e1fa93 | ||
|
|
141a1f0abf | ||
|
|
41e5eb1a4d | ||
|
|
875b8544df | ||
|
|
e96295a288 | ||
|
|
f897c1b2ef | ||
|
|
dae450b78e | ||
|
|
2af38206ad | ||
|
|
5db0603613 | ||
|
|
a965706e73 | ||
|
|
1a4b1e42cc | ||
|
|
47de3a3a2c | ||
|
|
926e2b589f | ||
|
|
453ebd6f37 | ||
|
|
24dc8306f8 | ||
|
|
0921ec1200 | ||
|
|
f620a6d039 | ||
|
|
c78144e452 | ||
|
|
056a0f9664 | ||
|
|
f8ff77db6a | ||
|
|
dcf3db95a6 | ||
|
|
ec31d0074f | ||
|
|
304ae7190a | ||
|
|
9de0ea32e1 |
No files matched your search
@@ -9,7 +9,16 @@ runs:
|
||||
steps:
|
||||
- name: Build folly and dependencies
|
||||
if: ${{ inputs.cache-hit != 'true' }}
|
||||
run: make build_folly
|
||||
run: |
|
||||
clean_path=()
|
||||
IFS=: read -ra path_entries <<< "$PATH"
|
||||
for entry in "${path_entries[@]}"; do
|
||||
if [[ "$entry" != "/usr/lib/ccache" ]]; then
|
||||
clean_path+=("$entry")
|
||||
fi
|
||||
done
|
||||
export PATH="$(IFS=:; echo "${clean_path[*]}")"
|
||||
make build_folly
|
||||
shell: bash
|
||||
- name: Skip folly build (using cached version)
|
||||
if: ${{ inputs.cache-hit == 'true' }}
|
||||
|
||||
@@ -2,6 +2,9 @@ name: pre-steps
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- name: Install lld linker for faster builds
|
||||
run: apt-get update -y && apt-get install -y lld 2>/dev/null || true
|
||||
shell: bash
|
||||
- name: Setup Environment Variables
|
||||
run: |-
|
||||
echo "GTEST_THROW_ON_FAILURE=0" >> "$GITHUB_ENV"
|
||||
|
||||
@@ -16,6 +16,9 @@ runs:
|
||||
echo "CCACHE_DIR=${{ github.workspace }}/.ccache" >> $GITHUB_ENV
|
||||
echo "CCACHE_BASEDIR=${{ github.workspace }}" >> $GITHUB_ENV
|
||||
echo "CCACHE_NOHASHDIR=true" >> $GITHUB_ENV
|
||||
echo "CCACHE_COMPILERCHECK=content" >> $GITHUB_ENV
|
||||
echo "CCACHE_SLOPPINESS=clang_index_store,file_stat_matches,include_file_ctime,include_file_mtime,ivfsoverlay,pch_defines,modules,system_headers,time_macros" >> $GITHUB_ENV
|
||||
echo "CCACHE_MAXSIZE=4G" >> $GITHUB_ENV
|
||||
if [ "${{ inputs.portable }}" = "true" ]; then
|
||||
echo "PORTABLE=1" >> $GITHUB_ENV
|
||||
fi
|
||||
@@ -44,6 +47,8 @@ runs:
|
||||
echo "/usr/lib/ccache" >> $GITHUB_PATH
|
||||
fi
|
||||
shell: bash
|
||||
- name: Zero ccache stats
|
||||
run: ccache -z
|
||||
- name: Zero ccache stats and set build marker
|
||||
run: |
|
||||
ccache -z
|
||||
touch "$CCACHE_DIR/.build_marker"
|
||||
shell: bash
|
||||
@@ -0,0 +1,15 @@
|
||||
name: teardown-ccache
|
||||
description: Trim stale ccache entries and print stats
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- name: Trim and print ccache stats
|
||||
run: |
|
||||
if [ -z "$CCACHE_DIR" ]; then
|
||||
echo "teardown-ccache: CCACHE_DIR not set, skipping (setup-ccache may not have run)"
|
||||
exit 0
|
||||
fi
|
||||
.github/scripts/ccache-trim.sh || true
|
||||
ccache -s || echo "teardown-ccache: ccache not found, skipping stats"
|
||||
if: always()
|
||||
shell: bash
|
||||
@@ -1,4 +1,13 @@
|
||||
name: windows-build-steps
|
||||
inputs:
|
||||
suite-run:
|
||||
description: Comma-separated list of test suites to run (empty to skip C++ tests)
|
||||
required: false
|
||||
default: arena_test,db_basic_test,db_test,db_test2,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test
|
||||
run-java:
|
||||
description: Whether to run Java tests
|
||||
required: false
|
||||
default: "true"
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
@@ -8,11 +17,15 @@ runs:
|
||||
uses: hendrikmuhs/ccache-action@v1.2
|
||||
with:
|
||||
max-size: "10GB"
|
||||
key: ccache-windows-${{ github.workflow }}
|
||||
restore-keys: |
|
||||
ccache-windows-
|
||||
- name: Configure ccache
|
||||
shell: pwsh
|
||||
run: |
|
||||
ccache --set-config=base_dir=C:\a\rocksdb\rocksdb
|
||||
ccache --set-config=hash_dir=false
|
||||
ccache --set-config=compiler_check=content
|
||||
- name: Custom steps
|
||||
env:
|
||||
THIRDPARTY_HOME: ${{ github.workspace }}/thirdparty
|
||||
@@ -55,12 +68,22 @@ runs:
|
||||
msbuild build/rocksdb.sln /m:32 /p:LinkIncremental=false -property:Configuration=Debug -property:Platform=x64
|
||||
if(!$?) { Exit $LASTEXITCODE }
|
||||
echo ========================= Test RocksDB =========================
|
||||
build_tools\run_ci_db_test.ps1 -SuiteRun arena_test,db_basic_test,db_test,db_test2,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test -Concurrency 16
|
||||
if(!$?) { Exit $LASTEXITCODE }
|
||||
echo ======================== Test RocksJava ========================
|
||||
cd build\java
|
||||
& ctest -C Debug -j 16
|
||||
if(!$?) { Exit $LASTEXITCODE }
|
||||
$suiteRun = "${{ inputs.suite-run }}"
|
||||
if ($suiteRun -ne "") {
|
||||
$suiteArray = $suiteRun -split ','
|
||||
build_tools\run_ci_db_test.ps1 -SuiteRun $suiteArray -Concurrency 16
|
||||
if(!$?) { Exit $LASTEXITCODE }
|
||||
} else {
|
||||
echo "Skipping C++ tests (suite-run is empty)"
|
||||
}
|
||||
if ("${{ inputs.run-java }}" -eq "true") {
|
||||
echo ======================== Test RocksJava ========================
|
||||
cd build\java
|
||||
& ctest -C Debug -j 16
|
||||
if(!$?) { Exit $LASTEXITCODE }
|
||||
} else {
|
||||
echo "Skipping Java tests"
|
||||
}
|
||||
shell: pwsh
|
||||
- name: Show ccache stats
|
||||
shell: pwsh
|
||||
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
# Trim ccache to keep only entries accessed during the current build.
|
||||
#
|
||||
# Usage:
|
||||
# 1. Before build: touch "$CCACHE_DIR/.build_marker"
|
||||
# 2. Run build (ccache updates mtime on hits, creates new files for misses)
|
||||
# 3. After build: .github/scripts/ccache-trim.sh
|
||||
#
|
||||
# This removes cache files not accessed during the build (stale entries from
|
||||
# previous commits). Only intended for CI where each run builds one commit.
|
||||
# Do NOT use on local builds where multiple worktrees may share the cache.
|
||||
|
||||
set -e
|
||||
|
||||
CCACHE_DIR="${CCACHE_DIR:?CCACHE_DIR must be set}"
|
||||
MARKER="$CCACHE_DIR/.build_marker"
|
||||
|
||||
if [ ! -f "$MARKER" ]; then
|
||||
echo "ccache-trim: No .build_marker found, skipping (was the marker created before build?)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Count files before cleanup
|
||||
before=$(find "$CCACHE_DIR" -type f \( -name '*R' -o -name '*M' \) | wc -l)
|
||||
|
||||
# Delete cache files (results and manifests) older than the marker
|
||||
find "$CCACHE_DIR" -type f \( -name '*R' -o -name '*M' \) ! -newer "$MARKER" -delete
|
||||
|
||||
# Clean up empty directories
|
||||
find "$CCACHE_DIR" -mindepth 2 -type d -empty -delete 2>/dev/null || true
|
||||
|
||||
# Recalculate size counters
|
||||
ccache -c 2>/dev/null || true
|
||||
|
||||
after=$(find "$CCACHE_DIR" -type f \( -name '*R' -o -name '*M' \) | wc -l)
|
||||
echo "ccache-trim: $before -> $after cache files (removed $((before - after)) stale entries)"
|
||||
|
||||
# Clean up marker
|
||||
rm -f "$MARKER"
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
# Compute test shard for parallel CI execution.
|
||||
# Distributes tests round-robin across N shards for balanced load.
|
||||
# Outputs ROCKSDBTESTS_SUBSET — the list of test binaries for this shard.
|
||||
# The Makefile uses this to build and run only the assigned tests.
|
||||
#
|
||||
# Usage: compute-test-shard.sh <shard_index> <num_shards>
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
shard=${1:?Usage: compute-test-shard.sh <shard_index> <num_shards>}
|
||||
nshards=${2:?Usage: compute-test-shard.sh <shard_index> <num_shards>}
|
||||
|
||||
# Get sorted test list (db_test first since it's the heaviest, then alpha)
|
||||
make -s list_all_tests 2>/dev/null | tr ' ' '\n' | grep '_test$' | sort -u > /tmp/sorted.txt
|
||||
(echo db_test; grep -v '^db_test$' /tmp/sorted.txt) > /tmp/all_tests.txt
|
||||
|
||||
total=$(wc -l < /tmp/all_tests.txt)
|
||||
|
||||
# Round-robin: assign test i to shard (i % nshards).
|
||||
# This spreads heavy tests (which are scattered alphabetically) evenly.
|
||||
awk -v s="$shard" -v n="$nshards" 'NR > 0 && (NR - 1) % n == s' /tmp/all_tests.txt > /tmp/include.txt
|
||||
|
||||
included=$(wc -l < /tmp/include.txt)
|
||||
first=$(head -1 /tmp/include.txt)
|
||||
last=$(tail -1 /tmp/include.txt)
|
||||
|
||||
# Output space-separated list for ROCKSDBTESTS_SUBSET
|
||||
subset=$(tr '\n' ' ' < /tmp/include.txt)
|
||||
echo "subset=${subset}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
echo "Shard $shard/$nshards: $included tests (round-robin), first=$first last=$last (total $total)"
|
||||
@@ -7,7 +7,7 @@
|
||||
// Parameters:
|
||||
// executionFile - path to the JSON execution log from claude-code-base-action
|
||||
// conclusion - 'success' or 'failure' from the action output
|
||||
// meta - { trigger, headSha, reviewer, isQuery }
|
||||
// meta - { trigger, headSha, reviewer, isQuery, isPartial }
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
@@ -23,6 +23,29 @@ module.exports = function parseClaude({executionFile, conclusion, meta}) {
|
||||
|
||||
const resultMessage = executionLog.find(m => m.type === 'result');
|
||||
|
||||
// Helper: extract the last substantial assistant text from the log.
|
||||
// Used as a fallback when Claude ran out of turns and the recovery
|
||||
// session also failed to produce a result.
|
||||
function getLastAssistantText(log, minLength = 200) {
|
||||
for (let i = log.length - 1; i >= 0; i--) {
|
||||
const m = log[i];
|
||||
if (m.type !== 'assistant') continue;
|
||||
const content = m.message && m.message.content;
|
||||
if (!Array.isArray(content)) continue;
|
||||
for (let j = content.length - 1; j >= 0; j--) {
|
||||
if (content[j].type === 'text' && content[j].text &&
|
||||
content[j].text.trim().length >= minLength) {
|
||||
const text = content[j].text.trim();
|
||||
// Truncate to avoid enormous PR comments
|
||||
return text.length > 50000 ? text.substring(0, 50000) +
|
||||
'\n\n*[Truncated — full output in execution log artifact]*' :
|
||||
text;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!resultMessage) {
|
||||
responseBody = '⚠️ No result message found in execution log.';
|
||||
} else if (resultMessage.subtype === 'success' && resultMessage.result) {
|
||||
@@ -31,6 +54,19 @@ module.exports = function parseClaude({executionFile, conclusion, meta}) {
|
||||
const errorInfo =
|
||||
resultMessage.result || resultMessage.error || 'Unknown error';
|
||||
responseBody = `❌ **Claude encountered an error:**\n\n${errorInfo}`;
|
||||
} else if (resultMessage.subtype === 'error_max_turns') {
|
||||
// The workflow runs a recovery session when this happens, so this
|
||||
// branch is typically only hit if recovery wasn't attempted (e.g.,
|
||||
// no findings file was written). Extract what we can.
|
||||
const partial = getLastAssistantText(executionLog);
|
||||
if (partial) {
|
||||
responseBody =
|
||||
`⚠️ **Review incomplete — Claude hit the turn limit.**\n\nBelow is the last partial output. You can request a fresh review with \`/claude-review\`.\n\n---\n\n${
|
||||
partial}`;
|
||||
} else {
|
||||
responseBody =
|
||||
'⚠️ **Review incomplete — Claude hit the turn limit before producing output.** You can request a fresh review with `/claude-review`.';
|
||||
}
|
||||
} else if (resultMessage.result) {
|
||||
responseBody = `⚠️ **Completed with status: ${
|
||||
resultMessage.subtype}**\n\n${resultMessage.result}`;
|
||||
@@ -41,7 +77,8 @@ module.exports = function parseClaude({executionFile, conclusion, meta}) {
|
||||
responseBody = `❌ Error parsing Claude response: ${error.message}`;
|
||||
}
|
||||
|
||||
const icon = conclusion === 'success' ? '✅' : '⚠️';
|
||||
const isPartial = !!meta.isPartial;
|
||||
const icon = isPartial ? '🟡' : (conclusion === 'success' ? '✅' : '⚠️');
|
||||
const headerTitle = meta.isQuery ? 'Claude Response' : 'Claude Code Review';
|
||||
const triggerLine = meta.trigger === 'auto' ?
|
||||
`*Auto-triggered after CI passed — reviewing commit ${
|
||||
|
||||
@@ -20,7 +20,7 @@ jobs:
|
||||
|
||||
- name: Download clang-tidy results
|
||||
id: download
|
||||
uses: actions/download-artifact@v4.0.0
|
||||
uses: actions/download-artifact@v4.1.3
|
||||
with:
|
||||
name: clang-tidy-result
|
||||
run-id: ${{ github.event.workflow_run.id }}
|
||||
|
||||
@@ -11,7 +11,7 @@ jobs:
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
with:
|
||||
@@ -36,14 +36,14 @@ jobs:
|
||||
echo "skip=false" >> "$GITHUB_OUTPUT"
|
||||
- name: Install clang-tidy
|
||||
if: steps.diff-base.outputs.skip != 'true'
|
||||
run: apt-get update && apt-get install -y clang-tidy
|
||||
run: apt-get update && apt-get install -y clang-tidy-21 && ln -sf /usr/bin/clang-tidy-21 /usr/local/bin/clang-tidy
|
||||
- name: Generate compile_commands.json
|
||||
if: steps.diff-base.outputs.skip != 'true'
|
||||
run: |
|
||||
mkdir build && cd build
|
||||
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
|
||||
-DCMAKE_C_COMPILER=clang-18 \
|
||||
-DCMAKE_CXX_COMPILER=clang++-18 ..
|
||||
-DCMAKE_C_COMPILER=clang-21 \
|
||||
-DCMAKE_CXX_COMPILER=clang++-21 ..
|
||||
cd ..
|
||||
ln -sf build/compile_commands.json compile_commands.json
|
||||
- name: Run clang-tidy on changed files
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
# Security:
|
||||
# - This job has contents:read ONLY (no PR write, no issue write)
|
||||
# - ANTHROPIC_API_KEY is used here but never coexists with write tokens
|
||||
# - Claude uses read-only tools (View, GlobTool, GrepTool)
|
||||
# - Claude uses read-only tools (View, GlobTool, GrepTool) plus Write
|
||||
# (only for incremental findings file, not repo modifications)
|
||||
# - Review methodology: claude_md/code_review.md
|
||||
|
||||
name: Claude Code Review
|
||||
@@ -70,20 +71,37 @@ jobs:
|
||||
if (prs && prs.length > 0) {
|
||||
prNumber = prs[0].number;
|
||||
} else {
|
||||
const { data: pulls } = await github.rest.pulls.list({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
state: 'open',
|
||||
sort: 'updated',
|
||||
direction: 'desc',
|
||||
per_page: 30
|
||||
});
|
||||
const match = pulls.find(p => p.head.sha === headSha);
|
||||
if (match) prNumber = match.number;
|
||||
// PR may not be registered yet if push and PR creation raced.
|
||||
// Retry up to 5 times with 10s delay.
|
||||
for (let attempt = 0; attempt < 5; attempt++) {
|
||||
if (attempt > 0) {
|
||||
core.info(`PR not found for SHA ${headSha}, retrying in 10s (attempt ${attempt + 1}/5)...`);
|
||||
await new Promise(r => setTimeout(r, 10000));
|
||||
}
|
||||
const { data: pulls } = await github.rest.pulls.list({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
state: 'open',
|
||||
sort: 'updated',
|
||||
direction: 'desc',
|
||||
per_page: 100
|
||||
});
|
||||
const match = pulls.find(p => p.head.sha === headSha);
|
||||
if (match) {
|
||||
prNumber = match.number;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!prNumber) {
|
||||
core.setFailed(`Could not find PR for SHA ${headSha}`);
|
||||
core.warning(
|
||||
`Could not find PR for SHA ${headSha} after retries. ` +
|
||||
`This can happen when the PR head advanced before this ` +
|
||||
`workflow_run-triggered job started. Skipping stale review run.`
|
||||
);
|
||||
core.setOutput('skip', 'true');
|
||||
core.setOutput('skip_reason', `stale_sha:${headSha}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,6 +111,7 @@ jobs:
|
||||
pull_number: prNumber
|
||||
});
|
||||
|
||||
core.setOutput('skip', 'false');
|
||||
core.setOutput('pr_number', prNumber.toString());
|
||||
core.setOutput('head_repo', pr.data.head.repo.clone_url);
|
||||
core.setOutput('head_ref', pr.data.head.ref);
|
||||
@@ -103,7 +122,13 @@ jobs:
|
||||
core.setOutput('pr_body', pr.data.body || '');
|
||||
core.setOutput('pr_author', pr.data.user.login);
|
||||
|
||||
- name: Skip stale run
|
||||
if: steps.pr_info.outputs.skip == 'true'
|
||||
run: |-
|
||||
echo "Skipping stale auto-review run: ${{ steps.pr_info.outputs.skip_reason }}"
|
||||
|
||||
- name: Check for existing review
|
||||
if: steps.pr_info.outputs.skip != 'true'
|
||||
id: check_existing
|
||||
uses: actions/github-script@v7
|
||||
env:
|
||||
@@ -125,7 +150,7 @@ jobs:
|
||||
core.setOutput('skip', existing ? 'true' : 'false');
|
||||
|
||||
- name: Checkout base repository
|
||||
if: steps.check_existing.outputs.skip != 'true'
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ steps.pr_info.outputs.base_ref }}
|
||||
@@ -133,7 +158,7 @@ jobs:
|
||||
persist-credentials: false
|
||||
|
||||
- name: Generate diff and prompt
|
||||
if: steps.check_existing.outputs.skip != 'true'
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
env:
|
||||
HEAD_REPO: ${{ steps.pr_info.outputs.head_repo }}
|
||||
HEAD_REF: ${{ steps.pr_info.outputs.head_ref }}
|
||||
@@ -157,11 +182,7 @@ jobs:
|
||||
IS_TRUNCATED=true
|
||||
fi
|
||||
|
||||
cat > /tmp/prompt.txt << 'PROMPT_EOF'
|
||||
Read claude_md/code_review.md for the full review methodology, then
|
||||
review the pull request described below. Follow all perspectives and
|
||||
the output format defined in that document.
|
||||
PROMPT_EOF
|
||||
cat claude_md/ci_review_prompt.md > /tmp/prompt.txt
|
||||
|
||||
cat >> /tmp/prompt.txt << PROMPT_EOF
|
||||
|
||||
@@ -181,48 +202,91 @@ jobs:
|
||||
cat /tmp/pr.diff >> /tmp/prompt.txt
|
||||
|
||||
- name: Run Claude
|
||||
if: steps.check_existing.outputs.skip != 'true'
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
id: claude
|
||||
uses: anthropics/claude-code-base-action@beta
|
||||
with:
|
||||
prompt_file: /tmp/prompt.txt
|
||||
model: claude-opus-4-6
|
||||
max_turns: "30"
|
||||
max_turns: "300"
|
||||
timeout_minutes: "60"
|
||||
allowed_tools: "View,GlobTool,GrepTool,Write,Task"
|
||||
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
|
||||
- name: Recover partial review on turn limit
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
id: recover
|
||||
uses: actions/github-script@v7
|
||||
env:
|
||||
EXECUTION_FILE: ${{ steps.claude.outputs.execution_file }}
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const log = JSON.parse(fs.readFileSync(process.env.EXECUTION_FILE, 'utf8'));
|
||||
const result = log.find(m => m.type === 'result');
|
||||
const hitLimit = result && result.subtype === 'error_max_turns';
|
||||
const hasFindings = fs.existsSync('review-findings.md');
|
||||
core.setOutput('hit_limit', hitLimit ? 'true' : 'false');
|
||||
core.setOutput('has_findings', hasFindings ? 'true' : 'false');
|
||||
core.setOutput('needs_recovery', (hitLimit && hasFindings) ? 'true' : 'false');
|
||||
|
||||
- name: Format partial findings
|
||||
if: >-
|
||||
steps.pr_info.outputs.skip != 'true' &&
|
||||
steps.check_existing.outputs.skip != 'true' &&
|
||||
steps.recover.outputs.needs_recovery == 'true'
|
||||
id: format_recovery
|
||||
uses: anthropics/claude-code-base-action@beta
|
||||
with:
|
||||
prompt_file: claude_md/ci_recovery_prompt.md
|
||||
model: claude-sonnet-4-20250514
|
||||
max_turns: "10"
|
||||
allowed_tools: "View,GlobTool,GrepTool"
|
||||
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
|
||||
- name: Build review comment
|
||||
if: steps.check_existing.outputs.skip != 'true'
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
uses: actions/github-script@v7
|
||||
env:
|
||||
EXECUTION_FILE: ${{ steps.claude.outputs.execution_file }}
|
||||
RECOVERY_FILE: ${{ steps.format_recovery.outputs.execution_file }}
|
||||
CONCLUSION: ${{ steps.claude.outputs.conclusion }}
|
||||
HEAD_SHA: ${{ steps.pr_info.outputs.head_sha }}
|
||||
NEEDS_RECOVERY: ${{ steps.recover.outputs.needs_recovery }}
|
||||
with:
|
||||
script: |
|
||||
const parse = require('./.github/scripts/parse-claude-review.js');
|
||||
const fs = require('fs');
|
||||
let execFile = process.env.EXECUTION_FILE;
|
||||
if (process.env.NEEDS_RECOVERY === 'true') {
|
||||
const rf = process.env.RECOVERY_FILE;
|
||||
if (rf && fs.existsSync(rf)) {
|
||||
execFile = rf;
|
||||
}
|
||||
}
|
||||
const body = parse({
|
||||
executionFile: process.env.EXECUTION_FILE,
|
||||
executionFile: execFile,
|
||||
conclusion: process.env.CONCLUSION,
|
||||
meta: {
|
||||
trigger: 'auto',
|
||||
headSha: process.env.HEAD_SHA,
|
||||
reviewer: '',
|
||||
isQuery: false,
|
||||
isPartial: process.env.NEEDS_RECOVERY === 'true',
|
||||
}
|
||||
});
|
||||
const fs = require('fs');
|
||||
fs.writeFileSync('claude-review-comment.md', body);
|
||||
|
||||
- name: Save PR number
|
||||
if: steps.check_existing.outputs.skip != 'true'
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
run: echo "${{ steps.pr_info.outputs.pr_number }}" > pr_number.txt
|
||||
|
||||
- name: Save trigger metadata
|
||||
if: steps.check_existing.outputs.skip != 'true'
|
||||
if: steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
run: echo "auto" > trigger_type.txt
|
||||
|
||||
- name: Upload review artifact
|
||||
if: always() && steps.check_existing.outputs.skip != 'true'
|
||||
if: always() && steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: claude-review-result
|
||||
@@ -234,7 +298,7 @@ jobs:
|
||||
retention-days: 1
|
||||
|
||||
- name: Upload execution log
|
||||
if: always() && steps.check_existing.outputs.skip != 'true'
|
||||
if: always() && steps.pr_info.outputs.skip != 'true' && steps.check_existing.outputs.skip != 'true'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: claude-review-execution-log
|
||||
@@ -242,6 +306,15 @@ jobs:
|
||||
retention-days: 7
|
||||
if-no-files-found: warn
|
||||
|
||||
- name: Upload recovery execution log
|
||||
if: always() && steps.pr_info.outputs.skip != 'true' && steps.recover.outputs.needs_recovery == 'true'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: claude-review-recovery-log
|
||||
path: ${{ steps.format_recovery.outputs.execution_file }}
|
||||
retention-days: 7
|
||||
if-no-files-found: ignore
|
||||
|
||||
# ======================== Manual Review ======================== #
|
||||
manual-review:
|
||||
name: Manual Claude Review
|
||||
@@ -351,14 +424,8 @@ jobs:
|
||||
|
||||
if [ "${COMMAND_TYPE}" = "query" ]; then
|
||||
# Query prompt
|
||||
cat > /tmp/prompt.txt << 'PROMPT_EOF'
|
||||
You are an expert C++ engineer for RocksDB.
|
||||
Read CLAUDE.md in the repo root and claude_md/ files for project context.
|
||||
Answer thoroughly with exact file paths and line numbers.
|
||||
|
||||
---
|
||||
|
||||
PROMPT_EOF
|
||||
cat claude_md/ci_query_prompt.md > /tmp/prompt.txt
|
||||
printf '\n---\n\n' >> /tmp/prompt.txt
|
||||
|
||||
cat >> /tmp/prompt.txt << PROMPT_EOF
|
||||
## Pull Request Context
|
||||
@@ -375,11 +442,7 @@ jobs:
|
||||
cat /tmp/pr.diff >> /tmp/prompt.txt
|
||||
else
|
||||
# Review prompt
|
||||
cat > /tmp/prompt.txt << 'PROMPT_EOF'
|
||||
Read claude_md/code_review.md for the full review methodology, then
|
||||
review the pull request described below. Follow all perspectives and
|
||||
the output format defined in that document.
|
||||
PROMPT_EOF
|
||||
cat claude_md/ci_review_prompt.md > /tmp/prompt.txt
|
||||
|
||||
cat >> /tmp/prompt.txt << PROMPT_EOF
|
||||
|
||||
@@ -411,7 +474,41 @@ jobs:
|
||||
with:
|
||||
prompt_file: /tmp/prompt.txt
|
||||
model: ${{ github.event_name == 'workflow_dispatch' && inputs.model || 'claude-opus-4-6' }}
|
||||
max_turns: "30"
|
||||
max_turns: "300"
|
||||
timeout_minutes: "60"
|
||||
allowed_tools: "View,GlobTool,GrepTool,Write,Task"
|
||||
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
|
||||
- name: Recover partial review on turn limit
|
||||
id: recover
|
||||
uses: actions/github-script@v7
|
||||
env:
|
||||
EXECUTION_FILE: ${{ steps.claude.outputs.execution_file }}
|
||||
COMMAND_TYPE: ${{ steps.command.outputs.type }}
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
// Skip recovery for queries — only reviews use incremental findings
|
||||
if (process.env.COMMAND_TYPE === 'query') {
|
||||
core.setOutput('needs_recovery', 'false');
|
||||
return;
|
||||
}
|
||||
const log = JSON.parse(fs.readFileSync(process.env.EXECUTION_FILE, 'utf8'));
|
||||
const result = log.find(m => m.type === 'result');
|
||||
const hitLimit = result && result.subtype === 'error_max_turns';
|
||||
const hasFindings = fs.existsSync('review-findings.md');
|
||||
core.setOutput('hit_limit', hitLimit ? 'true' : 'false');
|
||||
core.setOutput('has_findings', hasFindings ? 'true' : 'false');
|
||||
core.setOutput('needs_recovery', (hitLimit && hasFindings) ? 'true' : 'false');
|
||||
|
||||
- name: Format partial findings
|
||||
if: steps.recover.outputs.needs_recovery == 'true'
|
||||
id: format_recovery
|
||||
uses: anthropics/claude-code-base-action@beta
|
||||
with:
|
||||
prompt_file: claude_md/ci_recovery_prompt.md
|
||||
model: claude-sonnet-4-20250514
|
||||
max_turns: "10"
|
||||
allowed_tools: "View,GlobTool,GrepTool"
|
||||
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
|
||||
|
||||
@@ -419,25 +516,35 @@ jobs:
|
||||
uses: actions/github-script@v7
|
||||
env:
|
||||
EXECUTION_FILE: ${{ steps.claude.outputs.execution_file }}
|
||||
RECOVERY_FILE: ${{ steps.format_recovery.outputs.execution_file }}
|
||||
CONCLUSION: ${{ steps.claude.outputs.conclusion }}
|
||||
HEAD_SHA: ${{ steps.pr_info.outputs.head_sha }}
|
||||
REVIEWER: ${{ github.event_name == 'workflow_dispatch' && github.actor || github.event.comment.user.login }}
|
||||
COMMAND_TYPE: ${{ steps.command.outputs.type }}
|
||||
NEEDS_RECOVERY: ${{ steps.recover.outputs.needs_recovery }}
|
||||
with:
|
||||
script: |
|
||||
const parse = require('./.github/scripts/parse-claude-review.js');
|
||||
const fs = require('fs');
|
||||
const isReview = process.env.COMMAND_TYPE === 'review';
|
||||
let execFile = process.env.EXECUTION_FILE;
|
||||
if (process.env.NEEDS_RECOVERY === 'true') {
|
||||
const rf = process.env.RECOVERY_FILE;
|
||||
if (rf && fs.existsSync(rf)) {
|
||||
execFile = rf;
|
||||
}
|
||||
}
|
||||
const body = parse({
|
||||
executionFile: process.env.EXECUTION_FILE,
|
||||
executionFile: execFile,
|
||||
conclusion: process.env.CONCLUSION,
|
||||
meta: {
|
||||
trigger: 'manual',
|
||||
headSha: process.env.HEAD_SHA,
|
||||
reviewer: process.env.REVIEWER,
|
||||
isQuery: !isReview,
|
||||
isPartial: process.env.NEEDS_RECOVERY === 'true',
|
||||
}
|
||||
});
|
||||
const fs = require('fs');
|
||||
fs.writeFileSync('claude-review-comment.md', body);
|
||||
|
||||
- name: Save PR number
|
||||
@@ -474,3 +581,12 @@ jobs:
|
||||
path: ${{ steps.claude.outputs.execution_file }}
|
||||
retention-days: 7
|
||||
if-no-files-found: warn
|
||||
|
||||
- name: Upload recovery execution log
|
||||
if: always() && steps.recover.outputs.needs_recovery == 'true'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: claude-review-recovery-log
|
||||
path: ${{ steps.format_recovery.outputs.execution_file }}
|
||||
retention-days: 7
|
||||
if-no-files-found: ignore
|
||||
@@ -41,22 +41,41 @@ jobs:
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- run: make V=1 -j32 check
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang-18-asan-ubsan-with-folly:
|
||||
build-linux-clang-21-asan-ubsan-with-folly:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
env:
|
||||
CC: clang-18
|
||||
CXX: clang++-18
|
||||
CC: clang-21
|
||||
CXX: clang++-21
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/cache-getdeps-downloads"
|
||||
- uses: "./.github/actions/setup-folly"
|
||||
- uses: "./.github/actions/build-folly"
|
||||
- name: Build folly and dependencies
|
||||
run: |
|
||||
clean_path=()
|
||||
IFS=: read -ra path_entries <<< "$PATH"
|
||||
for entry in "${path_entries[@]}"; do
|
||||
if [[ "$entry" != "/usr/lib/ccache" ]]; then
|
||||
clean_path+=("$entry")
|
||||
fi
|
||||
done
|
||||
export PATH="$(IFS=:; echo "${clean_path[*]}")"
|
||||
ccache_bin="$(command -v ccache || true)"
|
||||
if [[ -n "$ccache_bin" && -x "$ccache_bin" ]]; then
|
||||
mv "$ccache_bin" "${ccache_bin}.disabled"
|
||||
fi
|
||||
export CC=gcc
|
||||
export CXX=g++
|
||||
export USE_CCACHE=0
|
||||
export CCACHE_DISABLE=1
|
||||
make build_folly
|
||||
shell: bash
|
||||
- run: LIB_MODE=static USE_CLANG=1 USE_FOLLY=1 COMPILE_WITH_UBSAN=1 COMPILE_WITH_ASAN=1 make -j32 check
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-with-folly:
|
||||
@@ -145,13 +164,13 @@ jobs:
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- name: Build rocksdb lib
|
||||
run: CC=clang-18 CXX=clang++-18 USE_CLANG=1 make -j4 static_lib
|
||||
run: CC=clang-21 CXX=clang++-21 USE_CLANG=1 make -j4 static_lib
|
||||
- name: Build fuzzers
|
||||
run: cd fuzz && make sst_file_writer_fuzzer db_fuzzer db_map_fuzzer
|
||||
- uses: "./.github/actions/post-steps"
|
||||
|
||||
@@ -72,6 +72,8 @@ jobs:
|
||||
run: make check-buck-targets
|
||||
- name: Simple source code checks
|
||||
run: make check-sources
|
||||
- name: Validate GitHub Actions YAML
|
||||
run: make check-workflow-yaml
|
||||
- name: Sanity check check_format_compatible.sh
|
||||
run: |-
|
||||
export TEST_TMPDIR=/dev/shm/rocksdb
|
||||
@@ -96,10 +98,8 @@ jobs:
|
||||
with:
|
||||
cache-key-prefix: build-linux
|
||||
- run: make V=1 J=32 -j32 check
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-mingw:
|
||||
if: needs.config.outputs.only_job == 'build-linux-cmake-mingw' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -107,7 +107,7 @@ jobs:
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
@@ -123,10 +123,8 @@ jobs:
|
||||
which java && java -version
|
||||
which javac && javac -version
|
||||
mkdir build && cd build && cmake -DJNI=1 -DWITH_GFLAGS=OFF .. -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_SYSTEM_NAME=Windows && make -j4 rocksdb rocksdbjni
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-make-with-folly:
|
||||
if: needs.config.outputs.only_job == 'build-linux-make-with-folly' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -150,10 +148,8 @@ jobs:
|
||||
with:
|
||||
cache-hit: ${{ steps.cache-folly.outputs.cache-hit }}
|
||||
- run: USE_FOLLY=1 LIB_MODE=static V=1 make -j64 check
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-make-with-folly-lite-no-test:
|
||||
if: needs.config.outputs.only_job == 'build-linux-make-with-folly-lite-no-test' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -172,10 +168,8 @@ jobs:
|
||||
with:
|
||||
cache-key-prefix: make-folly-lite
|
||||
- run: USE_FOLLY_LITE=1 EXTRA_CXXFLAGS=-DGLOG_USE_GLOG_EXPORT V=1 make -j32 all
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-with-folly-coroutines:
|
||||
if: needs.config.outputs.only_job == 'build-linux-cmake-with-folly-coroutines' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -199,16 +193,18 @@ jobs:
|
||||
with:
|
||||
cache-hit: ${{ steps.cache-folly.outputs.cache-hit }}
|
||||
- run: "(mkdir build && cd build && cmake -DUSE_COROUTINES=1 -DWITH_GFLAGS=1 -DROCKSDB_BUILD_SHARED=0 -DPORTABLE=ON .. && make VERBOSE=1 -j64 && ctest -j64)"
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-with-benchmark-no-thread-status:
|
||||
if: needs.config.outputs.only_job == 'build-linux-cmake-with-benchmark-no-thread-status' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
shard: [0, 1, 2, 3]
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:22.1
|
||||
options: --shm-size=16gb
|
||||
@@ -218,12 +214,15 @@ jobs:
|
||||
- uses: "./.github/actions/setup-ccache"
|
||||
with:
|
||||
cache-key-prefix: cmake-benchmark
|
||||
- run: mkdir build && cd build && cmake -DWITH_GFLAGS=1 -DWITH_BENCHMARK=1 -DPORTABLE=ON -DCMAKE_CXX_FLAGS=-DNROCKSDB_THREAD_STATUS .. && make VERBOSE=1 -j20 && ctest -j20
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- name: Build
|
||||
run: mkdir build && cd build && cmake -DWITH_GFLAGS=1 -DWITH_BENCHMARK=1 -DPORTABLE=ON -DCMAKE_CXX_FLAGS=-DNROCKSDB_THREAD_STATUS .. && make VERBOSE=1 -j20
|
||||
- name: Test shard ${{ matrix.shard }} of 4
|
||||
run: cd build && ctest -j20 -I ${{ matrix.shard }},,4
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
with:
|
||||
artifact-prefix: "${{ github.job }}-shard${{ matrix.shard }}"
|
||||
build-linux-encrypted_env-no_compression:
|
||||
if: needs.config.outputs.only_job == 'build-linux-encrypted_env-no_compression' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
@@ -240,10 +239,8 @@ jobs:
|
||||
cache-key-prefix: encrypted-env-no-compression
|
||||
- run: ENCRYPTED_ENV=1 ROCKSDB_DISABLE_SNAPPY=1 ROCKSDB_DISABLE_ZLIB=1 ROCKSDB_DISABLE_BZIP=1 ROCKSDB_DISABLE_LZ4=1 ROCKSDB_DISABLE_ZSTD=1 make V=1 J=32 -j32 check
|
||||
- run: "./sst_dump --help | grep -E -q 'Supported built-in compression types: kNoCompression$' # Verify no compiled in compression\n"
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ======================== Linux No Test Runs ======================= #
|
||||
build-linux-release:
|
||||
@@ -275,10 +272,8 @@ jobs:
|
||||
- run: USE_RTTI=1 make V=1 -j32 release
|
||||
- run: ls librocksdb.a
|
||||
- run: if ./trace_analyzer --version; then false; else true; fi
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang-13-no_test_run:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang-13-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -299,32 +294,28 @@ jobs:
|
||||
- run: make clean
|
||||
# FIXME: get back to "release" target
|
||||
- run: CC=clang-13 CXX=clang++-13 USE_CLANG=1 EXTRA_CXXFLAGS=-stdlib=libc++ EXTRA_LDFLAGS=-stdlib=libc++ DEBUG_LEVEL=0 make -j32 shared_lib
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang-18-no_test_run:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang-18-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
build-linux-clang-21-no_test_run:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang-21-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/setup-ccache"
|
||||
with:
|
||||
cache-key-prefix: clang-18
|
||||
- run: CC=clang-18 CXX=clang++-18 USE_CLANG=1 make -j32 all microbench
|
||||
cache-key-prefix: clang-21
|
||||
- run: CC=clang-21 CXX=clang++-21 USE_CLANG=1 make -j32 all microbench
|
||||
- run: make clean
|
||||
- run: CC=clang-18 CXX=clang++-18 USE_CLANG=1 DEBUG_LEVEL=0 make -j32 release
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- run: CC=clang-21 CXX=clang++-21 USE_CLANG=1 DEBUG_LEVEL=0 make -j32 release
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-gcc-14-no_test_run:
|
||||
if: needs.config.outputs.only_job == 'build-linux-gcc-14-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -332,7 +323,7 @@ jobs:
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
@@ -341,10 +332,8 @@ jobs:
|
||||
with:
|
||||
cache-key-prefix: gcc-14
|
||||
- run: CC=gcc-14 CXX=g++-14 V=1 make -j32 all microbench
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
|
||||
# ======================== Linux Other Checks ======================= #
|
||||
@@ -366,16 +355,22 @@ jobs:
|
||||
- name: Unity build
|
||||
run: make V=1 -j8 unity_test
|
||||
- run: make V=1 -j8 -k check-headers
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-mini-crashtest:
|
||||
if: needs.config.outputs.only_job == 'build-linux-mini-crashtest' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- crash_test_target: blackbox_crash_test_with_atomic_flush
|
||||
crash_duration: 480
|
||||
- crash_test_target: blackbox_crash_test
|
||||
crash_duration: 240
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:22.1
|
||||
options: --shm-size=16gb
|
||||
@@ -385,53 +380,67 @@ jobs:
|
||||
- uses: "./.github/actions/setup-ccache"
|
||||
with:
|
||||
cache-key-prefix: mini-crashtest
|
||||
- run: ulimit -S -n `ulimit -H -n` && make V=1 -j8 CRASH_TEST_EXT_ARGS='--duration=960 --max_key=2500000' blackbox_crash_test_with_atomic_flush
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- run: ulimit -S -n `ulimit -H -n` && make V=1 -j8 CRASH_TEST_EXT_ARGS='--duration=${{ matrix.crash_duration }} --max_key=2500000' ${{ matrix.crash_test_target }}
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
with:
|
||||
artifact-prefix: "${{ github.job }}-${{ matrix.crash_test_target }}"
|
||||
# ======================= Linux with Sanitizers ===================== #
|
||||
build-linux-clang18-asan-ubsan:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang18-asan-ubsan' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
build-linux-clang21-asan-ubsan:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang21-asan-ubsan' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 32-core-ubuntu
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
shard: [0, 1, 2]
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/setup-ccache"
|
||||
with:
|
||||
cache-key-prefix: clang18-asan-ubsan
|
||||
- run: COMPILE_WITH_ASAN=1 COMPILE_WITH_UBSAN=1 CC=clang-18 CXX=clang++-18 ROCKSDB_DISABLE_ALIGNED_NEW=1 USE_CLANG=1 make V=1 -j40 check
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
cache-key-prefix: clang21-asan-ubsan
|
||||
- run: COMPILE_WITH_ASAN=1 COMPILE_WITH_UBSAN=1 CC=clang-21 CXX=clang++-21 ROCKSDB_DISABLE_ALIGNED_NEW=1 USE_CLANG=1 make V=1 -j40 check
|
||||
env:
|
||||
CI_SHARD_INDEX: ${{ matrix.shard }}
|
||||
CI_TOTAL_SHARDS: 3
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang18-mini-tsan:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang18-mini-tsan' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
with:
|
||||
artifact-prefix: "${{ github.job }}-shard${{ matrix.shard }}"
|
||||
build-linux-clang21-mini-tsan:
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang21-mini-tsan' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 32-core-ubuntu
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
shard: [0, 1, 2]
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
options: --shm-size=16gb
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/setup-ccache"
|
||||
with:
|
||||
cache-key-prefix: clang18-tsan
|
||||
- run: COMPILE_WITH_TSAN=1 CC=clang-18 CXX=clang++-18 ROCKSDB_DISABLE_ALIGNED_NEW=1 USE_CLANG=1 make V=1 -j32 check
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
cache-key-prefix: clang21-tsan
|
||||
- run: COMPILE_WITH_TSAN=1 CC=clang-21 CXX=clang++-21 ROCKSDB_DISABLE_ALIGNED_NEW=1 USE_CLANG=1 make V=1 -j32 check
|
||||
env:
|
||||
CI_SHARD_INDEX: ${{ matrix.shard }}
|
||||
CI_TOTAL_SHARDS: 3
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
with:
|
||||
artifact-prefix: "${{ github.job }}-shard${{ matrix.shard }}"
|
||||
build-linux-static_lib-alt_namespace-status_checked:
|
||||
if: needs.config.outputs.only_job == 'build-linux-static_lib-alt_namespace-status_checked' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
@@ -447,10 +456,8 @@ jobs:
|
||||
with:
|
||||
cache-key-prefix: static-alt-namespace
|
||||
- run: ASSERT_STATUS_CHECKED=1 TEST_UINT128_COMPAT=1 ROCKSDB_MODIFY_NPHASH=1 LIB_MODE=static OPT="-DROCKSDB_USE_STD_SEMAPHORES -DROCKSDB_NAMESPACE=alternative_rocksdb_ns" make V=1 -j24 check
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ========================= MacOS build only ======================== #
|
||||
build-macos:
|
||||
@@ -472,10 +479,8 @@ jobs:
|
||||
- uses: "./.github/actions/pre-steps-macos"
|
||||
- name: Build
|
||||
run: ulimit -S -n `ulimit -H -n` && make V=1 J=16 -j8 all
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ========================= MacOS with Tests ======================== #
|
||||
build-macos-cmake:
|
||||
@@ -512,10 +517,8 @@ jobs:
|
||||
- name: Run shard 3 out of 4 test shards
|
||||
run: ulimit -S -n `ulimit -H -n` && cd build && ctest -j8 -I 3,,4
|
||||
if: ${{ matrix.run_sharded_tests == 3 }}
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ======================== Windows with Tests ======================= #
|
||||
# NOTE: some windows jobs are in "nightly" to save resources
|
||||
@@ -523,12 +526,28 @@ jobs:
|
||||
if: needs.config.outputs.only_job == 'build-windows-vs2022' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: windows-8-core
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- test_shard: db_test
|
||||
suite_run: db_test
|
||||
run_java: "false"
|
||||
- test_shard: other
|
||||
suite_run: arena_test,db_basic_test,db_test2,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test
|
||||
run_java: "false"
|
||||
- test_shard: java
|
||||
suite_run: ""
|
||||
run_java: "true"
|
||||
env:
|
||||
CMAKE_GENERATOR: Visual Studio 17 2022
|
||||
CMAKE_PORTABLE: 1
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/windows-build-steps"
|
||||
with:
|
||||
suite-run: ${{ matrix.suite_run }}
|
||||
run-java: ${{ matrix.run_java }}
|
||||
# ============================ Java Jobs ============================ #
|
||||
build-linux-java:
|
||||
if: needs.config.outputs.only_job == 'build-linux-java' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -551,10 +570,8 @@ jobs:
|
||||
which javac && javac -version
|
||||
- name: Test RocksDBJava
|
||||
run: make V=1 J=8 -j8 jtest
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
build-linux-java-static:
|
||||
if: needs.config.outputs.only_job == 'build-linux-java-static' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
@@ -576,10 +593,8 @@ jobs:
|
||||
which javac && javac -version
|
||||
- name: Build RocksDBJava Static Library
|
||||
run: make V=1 J=8 -j8 rocksdbjavastatic
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
build-macos-java:
|
||||
if: needs.config.outputs.only_job == 'build-macos-java' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
@@ -606,10 +621,8 @@ jobs:
|
||||
which javac && javac -version
|
||||
- name: Test RocksDBJava
|
||||
run: make V=1 J=16 -j16 jtest
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-macos-java-static:
|
||||
if: needs.config.outputs.only_job == 'build-macos-java-static' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -636,10 +649,8 @@ jobs:
|
||||
which javac && javac -version
|
||||
- name: Build RocksDBJava x86 and ARM Static Libraries
|
||||
run: make V=1 J=16 -j16 rocksdbjavastaticosx
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-macos-java-static-universal:
|
||||
if: needs.config.outputs.only_job == 'build-macos-java-static-universal' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
@@ -666,10 +677,8 @@ jobs:
|
||||
which javac && javac -version
|
||||
- name: Build RocksDBJava Universal Binary Static Library
|
||||
run: make V=1 J=16 -j16 rocksdbjavastaticosx_ub
|
||||
- name: Print ccache stats
|
||||
run: ccache -s
|
||||
- uses: "./.github/actions/teardown-ccache"
|
||||
if: always()
|
||||
shell: bash
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-java-pmd:
|
||||
if: needs.config.outputs.only_job == 'build-linux-java-pmd' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
|
||||
@@ -102,3 +102,11 @@ cmake-build-*
|
||||
third-party/folly/
|
||||
.cache
|
||||
*.sublime-*
|
||||
|
||||
# Claude Code local settings
|
||||
.claude/settings.local.json
|
||||
|
||||
tools/__pycache__/
|
||||
# Keep documentation trackable even if broader ignores match names like "*_test".
|
||||
!docs/
|
||||
!docs/**
|
||||
@@ -0,0 +1,9 @@
|
||||
# Agent Instructions
|
||||
|
||||
This repository's authoritative agent instructions live in `CLAUDE.md`.
|
||||
|
||||
Read and follow [`CLAUDE.md`](./CLAUDE.md) in full before making changes or
|
||||
reviewing code in this checkout.
|
||||
|
||||
If there is any ambiguity between this file and `CLAUDE.md`, `CLAUDE.md` takes
|
||||
precedence.
|
||||
@@ -32,12 +32,14 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
|
||||
"db/blob/blob_file_cache.cc",
|
||||
"db/blob/blob_file_garbage.cc",
|
||||
"db/blob/blob_file_meta.cc",
|
||||
"db/blob/blob_file_partition_manager.cc",
|
||||
"db/blob/blob_file_reader.cc",
|
||||
"db/blob/blob_garbage_meter.cc",
|
||||
"db/blob/blob_log_format.cc",
|
||||
"db/blob/blob_log_sequential_reader.cc",
|
||||
"db/blob/blob_log_writer.cc",
|
||||
"db/blob/blob_source.cc",
|
||||
"db/blob/blob_write_batch_transformer.cc",
|
||||
"db/blob/prefetch_buffer_collection.cc",
|
||||
"db/builder.cc",
|
||||
"db/c.cc",
|
||||
@@ -109,6 +111,7 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
|
||||
"db/version_util.cc",
|
||||
"db/wal_edit.cc",
|
||||
"db/wal_manager.cc",
|
||||
"db/wide/read_path_blob_resolver.cc",
|
||||
"db/wide/wide_column_serialization.cc",
|
||||
"db/wide/wide_columns.cc",
|
||||
"db/wide/wide_columns_helper.cc",
|
||||
@@ -331,6 +334,7 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
|
||||
"utilities/secondary_index/simple_secondary_index.cc",
|
||||
"utilities/simulator_cache/cache_simulator.cc",
|
||||
"utilities/simulator_cache/sim_cache.cc",
|
||||
"utilities/sorted_run_builder/sorted_run_builder.cc",
|
||||
"utilities/table_properties_collectors/compact_for_tiering_collector.cc",
|
||||
"utilities/table_properties_collectors/compact_on_deletion_collector.cc",
|
||||
"utilities/trace/file_trace_reader_writer.cc",
|
||||
@@ -374,10 +378,10 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
|
||||
"utilities/write_batch_with_index/write_batch_with_index_internal.cc",
|
||||
], deps=[
|
||||
"//folly/container:f14_hash",
|
||||
"//folly/experimental/coro:blocking_wait",
|
||||
"//folly/experimental/coro:collect",
|
||||
"//folly/experimental/coro:coroutine",
|
||||
"//folly/experimental/coro:task",
|
||||
"//folly/coro:blocking_wait",
|
||||
"//folly/coro:collect",
|
||||
"//folly/coro:coroutine",
|
||||
"//folly/coro:task",
|
||||
"//folly/synchronization:distributed_mutex",
|
||||
], headers=glob(["**/*.h"]), link_whole=False, extra_test_libs=False)
|
||||
|
||||
@@ -4804,6 +4808,12 @@ cpp_unittest_wrapper(name="db_blob_corruption_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_blob_direct_write_test",
|
||||
srcs=["db/blob/db_blob_direct_write_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_blob_index_test",
|
||||
srcs=["db/blob/db_blob_index_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
@@ -4942,6 +4952,12 @@ cpp_unittest_wrapper(name="db_merge_operator_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_open_with_config_test",
|
||||
srcs=["db/db_open_with_config_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_options_test",
|
||||
srcs=["db/db_options_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
@@ -5032,6 +5048,12 @@ cpp_unittest_wrapper(name="db_wide_basic_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_wide_blob_direct_write_test",
|
||||
srcs=["db/wide/db_wide_blob_direct_write_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_with_timestamp_basic_test",
|
||||
srcs=["db/db_with_timestamp_basic_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
@@ -5532,6 +5554,12 @@ cpp_unittest_wrapper(name="slice_transform_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="sorted_run_builder_test",
|
||||
srcs=["utilities/sorted_run_builder/sorted_run_builder_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="sst_dump_test",
|
||||
srcs=["tools/sst_dump_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
|
||||
@@ -227,6 +227,11 @@ The following patterns emerged as frequent sources of review feedback:
|
||||
* When there are multiple unit tests need to be executed, try to use
|
||||
gtest_parallel.py if available. E.g.
|
||||
python3 ${GTEST_PARALLEL}/gtest_parallel.py ./table_test
|
||||
* After writing a test, stress-test for flakiness:
|
||||
```bash
|
||||
COERCE_CONTEXT_SWITCH=1 make {test_binary}
|
||||
./{test_binary} --gtest_filter="*YourTestName*" --gtest_repeat=5
|
||||
```
|
||||
|
||||
### Unit test dedup guidelines
|
||||
* Extract helper functions for repeated patterns such as object
|
||||
@@ -261,6 +266,14 @@ The following patterns emerged as frequent sources of review feedback:
|
||||
### Stress test
|
||||
* When adding a new feature, make sure stress test covers the new option.
|
||||
|
||||
### Component docs
|
||||
* For component-level design notes and implementation walkthroughs, start with
|
||||
`docs/components/index.md`.
|
||||
* Documentation under `docs/components/` is organized by subsystem in
|
||||
`docs/components/<area>/`.
|
||||
* Each subsystem directory should have an `index.md` entry point plus focused
|
||||
chapter files for deeper topics.
|
||||
|
||||
### DB bench update
|
||||
* When adding a performance related feature, support it in db_bench
|
||||
|
||||
@@ -274,6 +287,9 @@ The following patterns emerged as frequent sources of review feedback:
|
||||
* Execute make clean to clean all of the changes.
|
||||
* Execute make check to build all of the changes and execute all of the tests.
|
||||
Note that executing all of the tests could take multiple minutes.
|
||||
* Run `ASSERT_STATUS_CHECKED=1 make check` to verify all Status objects are
|
||||
properly checked. This catches missing error handling that can lead to
|
||||
silent data corruption.
|
||||
|
||||
### Monitoring make check progress
|
||||
* Use `make check-progress` to get machine-parseable JSON progress while
|
||||
|
||||
+69
-4
@@ -82,10 +82,15 @@ if(NOT CMAKE_BUILD_TYPE)
|
||||
endif()
|
||||
message(STATUS "CMAKE_BUILD_TYPE is set to ${CMAKE_BUILD_TYPE}")
|
||||
|
||||
# Use ccache for compilation if available. Use CMAKE_C/CXX_COMPILER_LAUNCHER
|
||||
# instead of RULE_LAUNCH_COMPILE to avoid double-wrapping when ccache is also
|
||||
# injected via PATH (e.g., /usr/lib/ccache or brew --prefix ccache/libexec).
|
||||
# Note: we intentionally do NOT set RULE_LAUNCH_LINK because ccache cannot
|
||||
# cache link operations -- it only adds overhead.
|
||||
find_program(CCACHE_FOUND ccache)
|
||||
if(CCACHE_FOUND)
|
||||
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
|
||||
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
|
||||
set(CMAKE_C_COMPILER_LAUNCHER ccache)
|
||||
set(CMAKE_CXX_COMPILER_LAUNCHER ccache)
|
||||
endif(CCACHE_FOUND)
|
||||
|
||||
option(WITH_JEMALLOC "build with JeMalloc" OFF)
|
||||
@@ -515,6 +520,15 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "iOS")
|
||||
add_definitions(-DOS_MACOSX -DIOS_CROSS_COMPILE)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||
add_definitions(-DOS_LINUX)
|
||||
# Use lld linker if available for faster linking (12x faster than ld.bfd)
|
||||
if(NOT ROCKSDB_NO_FAST_LINKER)
|
||||
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -fuse-ld=lld -Wl,--version
|
||||
OUTPUT_VARIABLE LLD_VERSION_OUTPUT ERROR_QUIET RESULT_VARIABLE LLD_RESULT)
|
||||
if(LLD_RESULT EQUAL 0 AND LLD_VERSION_OUTPUT MATCHES "LLD")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
|
||||
endif()
|
||||
endif()
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "SunOS")
|
||||
add_definitions(-DOS_SOLARIS)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "kFreeBSD")
|
||||
@@ -660,10 +674,38 @@ if(USE_FOLLY)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Folly itself uses gflags transitively, but RocksDB tools and benchmarks
|
||||
# also call gflags APIs directly, so they need an explicit link dependency.
|
||||
if(WITH_GFLAGS)
|
||||
if(NOT TARGET gflags::gflags AND NOT TARGET gflags_shared AND
|
||||
NOT gflags_LIBRARIES)
|
||||
find_package(gflags CONFIG QUIET)
|
||||
if(NOT gflags_FOUND)
|
||||
find_package(gflags REQUIRED)
|
||||
endif()
|
||||
endif()
|
||||
if(TARGET gflags::gflags)
|
||||
set(GFLAGS_LIB gflags::gflags)
|
||||
elseif(TARGET gflags_shared)
|
||||
set(GFLAGS_LIB gflags_shared)
|
||||
elseif(DEFINED GFLAGS_TARGET AND TARGET ${GFLAGS_TARGET})
|
||||
set(GFLAGS_LIB ${GFLAGS_TARGET})
|
||||
elseif(gflags_LIBRARIES)
|
||||
set(GFLAGS_LIB ${gflags_LIBRARIES})
|
||||
else()
|
||||
message(FATAL_ERROR
|
||||
"WITH_GFLAGS is enabled, but no gflags library could be resolved")
|
||||
endif()
|
||||
list(APPEND THIRDPARTY_LIBS ${GFLAGS_LIB})
|
||||
endif()
|
||||
|
||||
add_compile_definitions(USE_FOLLY FOLLY_NO_CONFIG HAVE_CXX11_ATOMIC)
|
||||
list(APPEND THIRDPARTY_LIBS Folly::folly)
|
||||
set(FOLLY_LIBS Folly::folly)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--copy-dt-needed-entries")
|
||||
# --copy-dt-needed-entries is ld.bfd-specific; lld handles DT_NEEDED transitively
|
||||
if(NOT CMAKE_EXE_LINKER_FLAGS MATCHES "fuse-ld=lld")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--copy-dt-needed-entries")
|
||||
endif()
|
||||
endif()
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
@@ -690,6 +732,7 @@ set(SOURCES
|
||||
db/blob/blob_file_addition.cc
|
||||
db/blob/blob_file_builder.cc
|
||||
db/blob/blob_file_cache.cc
|
||||
db/blob/blob_file_partition_manager.cc
|
||||
db/blob/blob_file_garbage.cc
|
||||
db/blob/blob_file_meta.cc
|
||||
db/blob/blob_file_reader.cc
|
||||
@@ -698,6 +741,7 @@ set(SOURCES
|
||||
db/blob/blob_log_sequential_reader.cc
|
||||
db/blob/blob_log_writer.cc
|
||||
db/blob/blob_source.cc
|
||||
db/blob/blob_write_batch_transformer.cc
|
||||
db/blob/prefetch_buffer_collection.cc
|
||||
db/builder.cc
|
||||
db/c.cc
|
||||
@@ -769,6 +813,7 @@ set(SOURCES
|
||||
db/version_util.cc
|
||||
db/wal_edit.cc
|
||||
db/wal_manager.cc
|
||||
db/wide/read_path_blob_resolver.cc
|
||||
db/wide/wide_column_serialization.cc
|
||||
db/wide/wide_columns.cc
|
||||
db/wide/wide_columns_helper.cc
|
||||
@@ -950,6 +995,7 @@ set(SOURCES
|
||||
utilities/cassandra/merge_operator.cc
|
||||
utilities/checkpoint/checkpoint_impl.cc
|
||||
utilities/compaction_filters.cc
|
||||
utilities/sorted_run_builder/sorted_run_builder.cc
|
||||
utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc
|
||||
utilities/counted_fs.cc
|
||||
utilities/debug.cc
|
||||
@@ -1119,8 +1165,23 @@ if(USE_FOLLY_LITE)
|
||||
FMT_INCLUDE_DIR)
|
||||
include_directories(${FMT_INCLUDE_DIR})
|
||||
|
||||
exec_program(python3 ${PROJECT_SOURCE_DIR}/third-party/folly ARGS
|
||||
build/fbcode_builder/getdeps.py show-inst-dir glog OUTPUT_VARIABLE
|
||||
GLOG_INST_PATH)
|
||||
if(EXISTS ${GLOG_INST_PATH}/lib64)
|
||||
set(GLOG_LIB_DIR ${GLOG_INST_PATH}/lib64)
|
||||
else()
|
||||
set(GLOG_LIB_DIR ${GLOG_INST_PATH}/lib)
|
||||
endif()
|
||||
|
||||
add_definitions(-DUSE_FOLLY -DFOLLY_NO_CONFIG)
|
||||
list(APPEND THIRDPARTY_LIBS glog)
|
||||
find_library(GLOG_LIBRARY NAMES glog PATHS ${GLOG_LIB_DIR} NO_DEFAULT_PATH)
|
||||
if(NOT GLOG_LIBRARY)
|
||||
find_library(GLOG_LIBRARY NAMES glog)
|
||||
endif()
|
||||
if(GLOG_LIBRARY)
|
||||
list(APPEND THIRDPARTY_LIBS ${GLOG_LIBRARY})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(ROCKSDB_STATIC_LIB rocksdb${ARTIFACT_SUFFIX})
|
||||
@@ -1366,6 +1427,7 @@ if(WITH_TESTS)
|
||||
db/blob/blob_garbage_meter_test.cc
|
||||
db/blob/blob_source_test.cc
|
||||
db/blob/db_blob_basic_test.cc
|
||||
db/blob/db_blob_direct_write_test.cc
|
||||
db/blob/db_blob_compaction_test.cc
|
||||
db/blob/db_blob_corruption_test.cc
|
||||
db/blob/db_blob_index_test.cc
|
||||
@@ -1403,6 +1465,7 @@ if(WITH_TESTS)
|
||||
db/db_memtable_test.cc
|
||||
db/db_merge_operator_test.cc
|
||||
db/db_merge_operand_test.cc
|
||||
db/db_open_with_config_test.cc
|
||||
db/db_options_test.cc
|
||||
db/db_properties_test.cc
|
||||
db/db_range_del_test.cc
|
||||
@@ -1455,6 +1518,7 @@ if(WITH_TESTS)
|
||||
db/wal_manager_test.cc
|
||||
db/wal_edit_test.cc
|
||||
db/wide/db_wide_basic_test.cc
|
||||
db/wide/db_wide_blob_direct_write_test.cc
|
||||
db/wide/wide_column_serialization_test.cc
|
||||
db/wide/wide_columns_helper_test.cc
|
||||
db/write_batch_test.cc
|
||||
@@ -1535,6 +1599,7 @@ if(WITH_TESTS)
|
||||
utilities/cassandra/cassandra_row_merge_test.cc
|
||||
utilities/cassandra/cassandra_serialize_test.cc
|
||||
utilities/checkpoint/checkpoint_test.cc
|
||||
utilities/sorted_run_builder/sorted_run_builder_test.cc
|
||||
utilities/env_timed_test.cc
|
||||
utilities/fault_injection_fs_test.cc
|
||||
utilities/memory/memory_test.cc
|
||||
|
||||
+50
@@ -1,6 +1,56 @@
|
||||
# Rocksdb Change Log
|
||||
> NOTE: Entries for next release do not go here. Follow instructions in `unreleased_history/README.txt`
|
||||
|
||||
## 11.2.4 (06/24/2026)
|
||||
### Bug Fixes
|
||||
* Fixed a bug where closing a read-only DB instance could delete live SST files created by a concurrent read-write DB sharing the same directory.
|
||||
|
||||
## 11.2.3 (05/07/2026)
|
||||
### New Features
|
||||
* Add experimental mutable DBOption `optimize_manifest_for_recovery` (default false). When enabled, RocksDB can reduce recovery work after a clean shutdown, which may lower DB::Open latency on warm reopens.
|
||||
* Add experimental `reuse_manifest_on_open` DBOption (default false). When enabled, DB::Open reuses the existing MANIFEST file for append instead of creating a fresh one, avoiding the cost of serializing the entire database state into a new MANIFEST on the first post-open write.
|
||||
|
||||
## 11.2.2 (05/07/2026)
|
||||
## 11.2.1 (04/29/2026)
|
||||
### Bug Fixes
|
||||
* Fix bug in range tombstone synthesis that covers live keys added during an IngestExternalFile
|
||||
|
||||
## 11.2.0 (04/18/2026)
|
||||
### New Features
|
||||
* Added experimental `DBOptions::fast_sst_open` option. When enabled, RocksDB retrieves opaque file system metadata for SST files after flush, compaction, and external file ingestion, persists it in the MANIFEST, and passes it back to the file system on subsequent file opens to accelerate DB open time.
|
||||
* Added new option `min_tombstones_for_range_conversion` in `AdvancedColumnFamilyOptions`. When set to a non-zero value N, forward or reverse iteration will convert N or more contiguous point tombstones into a range tombstone in the mutable memtable. Future read operations will then be able to benefit from range tombstone optimizations. There are some limitations when it comes to table_filters, prefix_filters, and UDTs. See header comments for more details.
|
||||
* Added read-triggered compaction: a new column family option `read_triggered_compaction_threshold` (default 0, disabled) that marks SST files for compaction when their read frequency (`num_collapsible_entry_reads_sampled / file_size`) exceeds the threshold. This helps reduce read amplification for frequently-read ("hot") keys. A new DB option `max_compaction_trigger_wakeup_seconds` (default 43200s / 12 hours) controls the maximum interval for periodic compaction score re-evaluation, which is necessary for this feature to work on quiet (no-write) databases.
|
||||
|
||||
### Public API Changes
|
||||
* Added new `WideColumnBlobResolver` interface and `CompactionFilter::FilterV4()` method, including `ResolveColumn()` / `ResolveColumns()` helpers for lazy loading blob column values in wide-column entities during compaction. This allows compaction filters to resolve blob values on-demand, avoiding unnecessary I/O for blob columns they don't need to access.
|
||||
* Changed experimental feature `ExternalTableReader::Get` and `ExternalTableReader::MultiGet` to use `PinnableSlice` instead of `std::string` for output values, enabling zero-copy pinning. This will break existing implementations.
|
||||
* Added `SstFileReader::Get` and `SstFileReader::MultiGet` overloads that accept `PinnableSlice`/`std::vector<PinnableSlice>*`, enabling zero-copy reads when the underlying `TableReader` supports pinning.
|
||||
|
||||
### Behavior Changes
|
||||
* Prefix filter changes - when seeking to a key that is out of domain, and total_order_seek is false, total_order_seek is treated as if it were true. When prefix_same_as_start = true, now iterating past a key that is out of domain invalidates the iterator. The existing behavior does not check for InDomain in the DBIter, so Transform() can produce undefined behavior (e.g. key of size 3 on FixedPrefixTransform(4)).
|
||||
* Wide-column entities with blob-backed columns now use a new V2 on-disk encoding; older RocksDB versions that do not support wide-column blob separation will reject DBs or SSTs containing those entities.
|
||||
|
||||
### Bug Fixes
|
||||
* Fix blob garbage accounting for blob direct-write flushes so flush-time filtering and overwrite elision correctly register obsolete blob bytes in blob metadata.
|
||||
* Fix a memory accounting leak in IODispatcher where ReadIndex() moved block values out of ReadSet without releasing the associated prefetch memory, causing subsequent prefetches to be blocked when max_prefetch_memory_bytes was set.
|
||||
* Fix MultiScan to fall back to synchronous coalesced reads when async I/O is unsupported at runtime.
|
||||
|
||||
## 11.1.0 (03/23/2026)
|
||||
### New Features
|
||||
* Add a new option `open_files_async`. The existing behavior is on DB open, we open all sst files and do basic validations. For very large DBs on remote filesystems with many ssts, this may take very long. This option performs these validations instead in the background. Open errors found by this async background task are surfaced as a new background error kAsyncFileOpen.
|
||||
* Added `BlockBasedTableOptions::kAuto` index block search type that automatically selects between binary and interpolation search on a per-index-block basis. During SST construction, each index block's key distribution uniformity is analyzed using the coefficient of variation of key gaps, and index blocks with uniform keys use interpolation search while others fall back to binary search. The uniformity threshold is configurable via `BlockBasedTableOptions::uniform_cv_threshold` (default: 0.2).
|
||||
* Introduced enforce_write_buffer_manager_during_recovery option to allow WriteBufferManager to be enforced during WAL recovery. (Default: true)
|
||||
* Add `memtable_batch_lookup_optimization` option to use batch lookup optimization for memtable MultiGet. For skip list memtables, after each key lookup, the search path is cached and reused for the next key, reducing per-key cost from O(log N) to O(log d) where d is the distance between consecutive keys. Benchmarks show ~7% improvement in memtable-resident MultiGet throughput.
|
||||
* Added `BlockBasedTableOptions::PrepopulateBlockCache::kFlushAndCompaction` to prepopulate the block cache during both flush and compaction. Compaction-warmed blocks are inserted at `BOTTOM` priority (vs `LOW` for flush) so they are evicted first under cache pressure. Recommended only for use cases where most or all of the database is expected to reside in cache (e.g., tiered or remote storage where the working set fits in cache).
|
||||
* Added new mutable DB option `verify_manifest_content_on_close` (default: false). When enabled, on DB close the MANIFEST file is read back and all records are validated (CRC checksums and logical content). If corruption is detected, a fresh MANIFEST is written from in-memory state.
|
||||
|
||||
### Behavior Changes
|
||||
* num_reads_sampled now factors in re-seeks and next/prev() on file iterators for files in L1+. next/prev() is discounted by 64x compared to seek due to being a much cheaper call.
|
||||
* Remote compaction workers now skip WAL recovery when opening the secondary DB instance, since only the LSM state from MANIFEST is needed for compaction. This reduces I/O and speeds up remote compaction startup.
|
||||
|
||||
### Bug Fixes
|
||||
* Fix a bug in round-robin compaction that missed selecting input files that are needed to guarantee data correctness and cause crashing in debug builds or silent data corruption in release builds for Get().
|
||||
|
||||
## 11.0.0 (02/23/2026)
|
||||
### New Features
|
||||
* Added support for storing wide-column entity column values in blob files. When `min_blob_size` is configured, large column values in wide-column entities will be stored in blob files, reducing SST file size and improving read performance.
|
||||
|
||||
@@ -450,7 +450,7 @@ ifndef USE_FOLLY
|
||||
endif
|
||||
|
||||
ifndef GTEST_THROW_ON_FAILURE
|
||||
export GTEST_THROW_ON_FAILURE=1
|
||||
export GTEST_THROW_ON_FAILURE=0
|
||||
endif
|
||||
ifndef GTEST_HAS_EXCEPTIONS
|
||||
export GTEST_HAS_EXCEPTIONS=1
|
||||
@@ -621,36 +621,24 @@ endif
|
||||
|
||||
ROCKSDBTESTS_SUBSET ?= $(TESTS)
|
||||
|
||||
# c_test - doesn't use gtest
|
||||
# env_test - suspicious use of test::TmpDir
|
||||
# deletefile_test - serial because it generates giant temporary files in
|
||||
# its various tests. Parallel can fill up your /dev/shm
|
||||
# db_bloom_filter_test - serial because excessive space usage by instances
|
||||
# of DBFilterConstructionReserveMemoryTestWithParam can fill up /dev/shm
|
||||
# perf_context_test - 1GB write_buffer_size default, parallel can fill /dev/shm
|
||||
# obsolete_files_test - ~1GB write_buffer_size/target_file_size_base
|
||||
# backup_engine_test - 1GB write_buffer_size in FailOverwritingBackups
|
||||
# prefetch_test - 1GB write_buffer_size in PrefetchWhenReseek parameterized test
|
||||
# db_io_failure_test - 256MB write_buffer_size in FlushSstRangeSyncError and
|
||||
# CompactionSstRangeSyncError
|
||||
# c_test - doesn't use gtest, can't be sharded
|
||||
# Other tests previously listed here (backup_engine_test, db_bloom_filter_test,
|
||||
# perf_context_test, etc.) were NON_PARALLEL due to /dev/shm memory concerns
|
||||
# when the old per-test-case sharding spawned thousands of processes. With the
|
||||
# new gtest-based sharding (GTEST_SHARD_SIZE=10, max NCORES*8 shards), at most
|
||||
# ~4 shards run concurrently on CI (4 cores), so peak memory is manageable
|
||||
# (4 * 1GB = 4GB << 16GB shm).
|
||||
NON_PARALLEL_TEST = \
|
||||
c_test \
|
||||
env_test \
|
||||
deletefile_test \
|
||||
db_bloom_filter_test \
|
||||
perf_context_test \
|
||||
obsolete_files_test \
|
||||
backup_engine_test \
|
||||
prefetch_test \
|
||||
db_io_failure_test \
|
||||
$(PLUGIN_TESTS) \
|
||||
|
||||
PARALLEL_TEST = $(filter-out $(NON_PARALLEL_TEST), $(TESTS))
|
||||
PARALLEL_TEST = $(filter-out $(NON_PARALLEL_TEST), $(ROCKSDBTESTS_SUBSET))
|
||||
|
||||
# Not necessarily well thought out or up-to-date, but matches old list
|
||||
TESTS_PLATFORM_DEPENDENT := \
|
||||
db_basic_test \
|
||||
db_blob_basic_test \
|
||||
db_blob_direct_write_test \
|
||||
db_encryption_test \
|
||||
external_sst_file_basic_test \
|
||||
auto_roll_logger_test \
|
||||
@@ -658,6 +646,7 @@ TESTS_PLATFORM_DEPENDENT := \
|
||||
dynamic_bloom_test \
|
||||
c_test \
|
||||
checkpoint_test \
|
||||
sorted_run_builder_test \
|
||||
crc32c_test \
|
||||
coding_test \
|
||||
inlineskiplist_test \
|
||||
@@ -818,7 +807,18 @@ endif # PLATFORM_SHARED_EXT
|
||||
rocksdbjavastatic rocksdbjava install install-static install-shared \
|
||||
uninstall analyze tools tools_lib check-headers checkout_folly clang-tidy
|
||||
|
||||
all: $(LIBRARY) $(BENCHMARKS) tools tools_lib test_libs $(TESTS)
|
||||
# Auto-configure git hooks on first build so developers do not need to run
|
||||
# "make install-hooks" manually. This is a no-op if already set.
|
||||
setup-hooks:
|
||||
@if [ -d .git ] && [ -d githooks ]; then \
|
||||
cur=$$(git config core.hooksPath 2>/dev/null); \
|
||||
if [ "$$cur" != "githooks" ]; then \
|
||||
git config core.hooksPath githooks; \
|
||||
echo "git hooks: configured core.hooksPath = githooks"; \
|
||||
fi; \
|
||||
fi
|
||||
|
||||
all: setup-hooks $(LIBRARY) $(BENCHMARKS) tools tools_lib test_libs $(TESTS)
|
||||
|
||||
all_but_some_tests: $(LIBRARY) $(BENCHMARKS) tools tools_lib test_libs $(ROCKSDBTESTS_SUBSET)
|
||||
|
||||
@@ -882,24 +882,42 @@ coverage: clean
|
||||
|
||||
parallel_tests = $(patsubst %,parallel_%,$(PARALLEL_TEST))
|
||||
.PHONY: gen_parallel_tests $(parallel_tests)
|
||||
# Shard size controls how many test cases run per process. The actual number
|
||||
# of shards per binary is: min(ceil(test_count / GTEST_SHARD_SIZE), NCORES * 8)
|
||||
# This adapts to machine size: many small shards on beefy machines, fewer
|
||||
# larger shards on CI (typically 2-4 cores).
|
||||
GTEST_SHARD_SIZE ?= 10
|
||||
NCORES ?= $(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
|
||||
|
||||
$(parallel_tests):
|
||||
$(AM_V_at)TEST_BINARY=$(patsubst parallel_%,%,$@); \
|
||||
TEST_NAMES=` \
|
||||
(./$$TEST_BINARY --gtest_list_tests || echo " $${TEST_BINARY}__list_tests_failure") \
|
||||
| awk '/^[^ ]/ { prefix = $$1 } /^[ ]/ { print prefix $$1 }'`; \
|
||||
echo " Generating parallel test scripts for $$TEST_BINARY"; \
|
||||
for TEST_NAME in $$TEST_NAMES; do \
|
||||
TEST_SCRIPT=t/run-$$TEST_BINARY-$${TEST_NAME//\//-}; \
|
||||
TEST_COUNT=` \
|
||||
(./$$TEST_BINARY --gtest_list_tests 2>/dev/null || echo " list_failure") \
|
||||
| grep -c '^ '`; \
|
||||
if [ "$$TEST_COUNT" -le 0 ]; then TEST_COUNT=1; fi; \
|
||||
MAX_SHARDS=$$(( $(NCORES) * 8 )); \
|
||||
NUM_SHARDS=$$(( (TEST_COUNT + $(GTEST_SHARD_SIZE) - 1) / $(GTEST_SHARD_SIZE) )); \
|
||||
if [ "$$NUM_SHARDS" -gt "$$MAX_SHARDS" ]; then NUM_SHARDS=$$MAX_SHARDS; fi; \
|
||||
if [ "$$NUM_SHARDS" -le 0 ]; then NUM_SHARDS=1; fi; \
|
||||
echo " Generating $$NUM_SHARDS shards for $$TEST_BINARY ($$TEST_COUNT tests)"; \
|
||||
SHARD_IDX=0; \
|
||||
while [ "$$SHARD_IDX" -lt "$$NUM_SHARDS" ]; do \
|
||||
if [ -n "$(CI_TOTAL_SHARDS)" ] && [ $$(( $$SHARD_IDX % $(CI_TOTAL_SHARDS) )) -ne $(CI_SHARD_INDEX) ]; then \
|
||||
SHARD_IDX=$$((SHARD_IDX + 1)); \
|
||||
continue; \
|
||||
fi; \
|
||||
TEST_SCRIPT=t/run-$$TEST_BINARY-shard-$$SHARD_IDX; \
|
||||
printf '%s\n' \
|
||||
'#!/bin/sh' \
|
||||
"d=\$(TEST_TMPDIR)$$TEST_SCRIPT" \
|
||||
'mkdir -p $$d' \
|
||||
"TEST_TMPDIR=\$$d $(DRIVER) ./$$TEST_BINARY --gtest_filter=$$TEST_NAME" \
|
||||
"TEST_TMPDIR=\$$d GTEST_TOTAL_SHARDS=$$NUM_SHARDS GTEST_SHARD_INDEX=$$SHARD_IDX $(DRIVER) ./$$TEST_BINARY" \
|
||||
'test_retcode=$$?' \
|
||||
'[ $$test_retcode -eq 0 ] && rm -rf $$d' \
|
||||
'exit $$test_retcode' \
|
||||
> $$TEST_SCRIPT; \
|
||||
chmod a=rx $$TEST_SCRIPT; \
|
||||
SHARD_IDX=$$((SHARD_IDX + 1)); \
|
||||
done
|
||||
|
||||
gen_parallel_tests:
|
||||
@@ -923,8 +941,10 @@ gen_parallel_tests:
|
||||
# 152.120 PASS t/DBTest.FileCreationRandomFailure
|
||||
# 107.816 PASS t/DBTest.EncodeDecompressedBlockSizeTest
|
||||
#
|
||||
# With sharded test execution, prioritize binaries known to be slow.
|
||||
# These generate many shards and should start early for good load balancing.
|
||||
slow_test_regexp = \
|
||||
^.*MySQLStyleTransactionTest.*$$|^.*SnapshotConcurrentAccessTest.*$$|^.*SeqAdvanceConcurrentTest.*$$|^t/run-table_test-HarnessTest.Randomized$$|^t/run-db_test-.*(?:FileCreationRandomFailure|EncodeDecompressedBlockSizeTest)$$|^.*RecoverFromCorruptedWALWithoutFlush$$
|
||||
^.*block_based_table_reader_test.*$$|^.*table_test.*$$|^.*block_test.*$$|^.*write_prepared_transaction_test.*$$|^.*transaction_test.*$$|^.*external_sst_file_test.*$$|^.*db_wal_test.*$$|^.*db_with_timestamp_basic_test.*$$|^.*db_test-.*$$
|
||||
prioritize_long_running_tests = \
|
||||
perl -pe 's,($(slow_test_regexp)),100 $$1,' \
|
||||
| sort -k1,1gr \
|
||||
@@ -959,7 +979,13 @@ check_0:
|
||||
'To monitor subtest <duration,pass/fail,name>,' \
|
||||
' run "make watch-log" in a separate window' ''; \
|
||||
{ \
|
||||
printf './%s\n' $(filter-out $(PARALLEL_TEST),$(TESTS)); \
|
||||
NON_PARALLEL_LIST="$(filter-out $(PARALLEL_TEST),$(ROCKSDBTESTS_SUBSET))"; \
|
||||
if [ -n "$$NON_PARALLEL_LIST" ]; then \
|
||||
printf './%s\n' $$NON_PARALLEL_LIST \
|
||||
| if [ -n "$(CI_TOTAL_SHARDS)" ]; then \
|
||||
awk -v s=$(CI_SHARD_INDEX) -v n=$(CI_TOTAL_SHARDS) '(NR-1)%n==s'; \
|
||||
else cat; fi; \
|
||||
fi; \
|
||||
find t -name 'run-*' -print; \
|
||||
} \
|
||||
| $(prioritize_long_running_tests) \
|
||||
@@ -1035,6 +1061,7 @@ ifneq ($(PLATFORM), OS_AIX)
|
||||
$(PYTHON) tools/check_all_python.py
|
||||
ifndef ASSERT_STATUS_CHECKED # not yet working with these tests
|
||||
$(PYTHON) tools/ldb_test.py
|
||||
$(PYTHON) tools/db_crashtest_test.py
|
||||
sh tools/rocksdb_dump_test.sh
|
||||
endif
|
||||
endif
|
||||
@@ -1042,6 +1069,7 @@ ifndef SKIP_FORMAT_BUCK_CHECKS
|
||||
$(MAKE) check-format
|
||||
$(MAKE) check-buck-targets
|
||||
$(MAKE) check-sources
|
||||
$(MAKE) check-workflow-yaml
|
||||
endif
|
||||
|
||||
# TODO add ldb_tests
|
||||
@@ -1052,6 +1080,10 @@ check_some: $(ROCKSDBTESTS_SUBSET)
|
||||
ldb_tests: ldb
|
||||
$(PYTHON) tools/ldb_test.py
|
||||
|
||||
.PHONY: db_crashtest_tests
|
||||
db_crashtest_tests:
|
||||
$(PYTHON) tools/db_crashtest_test.py
|
||||
|
||||
include crash_test.mk
|
||||
|
||||
asan_check: clean
|
||||
@@ -1235,12 +1267,40 @@ format-auto:
|
||||
check-format:
|
||||
build_tools/format-diff.sh -c
|
||||
|
||||
|
||||
install-hooks:
|
||||
@echo "Installing git hooks from githooks/..."
|
||||
@if [ -d githooks ]; then \
|
||||
for hook in githooks/*; do \
|
||||
hook_name=$$(basename "$$hook"); \
|
||||
cp "$$hook" .git/hooks/"$$hook_name"; \
|
||||
chmod +x .git/hooks/"$$hook_name"; \
|
||||
echo " Installed $$hook_name"; \
|
||||
done; \
|
||||
echo "Done. Hooks installed to .git/hooks/"; \
|
||||
else \
|
||||
echo "Error: githooks/ directory not found"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
uninstall-hooks:
|
||||
@echo "Removing installed git hooks..."
|
||||
@for hook in githooks/*; do \
|
||||
hook_name=$$(basename "$$hook"); \
|
||||
rm -f .git/hooks/"$$hook_name"; \
|
||||
echo " Removed $$hook_name"; \
|
||||
done
|
||||
@echo "Done."
|
||||
|
||||
check-buck-targets:
|
||||
buckifier/check_buck_targets.sh
|
||||
|
||||
check-sources:
|
||||
build_tools/check-sources.sh
|
||||
|
||||
check-workflow-yaml:
|
||||
build_tools/check-workflow-yaml.sh
|
||||
|
||||
# Run clang-tidy on locally changed files, filtered to changed lines only.
|
||||
# Requires compile_commands.json (generate with cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON).
|
||||
# Override CLANG_TIDY_BINARY and CLANG_TIDY_JOBS as needed:
|
||||
@@ -1428,6 +1488,9 @@ db_basic_test: $(OBJ_DIR)/db/db_basic_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
db_blob_basic_test: $(OBJ_DIR)/db/blob/db_blob_basic_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_blob_direct_write_test: $(OBJ_DIR)/db/blob/db_blob_direct_write_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_blob_compaction_test: $(OBJ_DIR)/db/blob/db_blob_compaction_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -1437,6 +1500,9 @@ db_readonly_with_timestamp_test: $(OBJ_DIR)/db/db_readonly_with_timestamp_test.o
|
||||
db_wide_basic_test: $(OBJ_DIR)/db/wide/db_wide_basic_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_wide_blob_direct_write_test: $(OBJ_DIR)/db/wide/db_wide_blob_direct_write_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_with_timestamp_basic_test: $(OBJ_DIR)/db/db_with_timestamp_basic_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -1446,6 +1512,9 @@ db_with_timestamp_compaction_test: db/db_with_timestamp_compaction_test.o $(TEST
|
||||
db_encryption_test: $(OBJ_DIR)/db/db_encryption_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_open_with_config_test: $(OBJ_DIR)/db/db_open_with_config_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_test: $(OBJ_DIR)/db/db_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -1590,6 +1659,9 @@ backup_engine_test: $(OBJ_DIR)/utilities/backup/backup_engine_test.o $(TEST_LIBR
|
||||
checkpoint_test: $(OBJ_DIR)/utilities/checkpoint/checkpoint_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
sorted_run_builder_test: $(OBJ_DIR)/utilities/sorted_run_builder/sorted_run_builder_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
cache_simulator_test: $(OBJ_DIR)/utilities/simulator_cache/cache_simulator_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -2589,7 +2661,7 @@ list_all_tests:
|
||||
|
||||
# Remove the rules for which dependencies should not be generated and see if any are left.
|
||||
#If so, include the dependencies; if not, do not include the dependency files
|
||||
ROCKS_DEP_RULES=$(filter-out clean format check-format check-buck-targets check-headers check-sources clang-tidy jclean jtest package analyze tags rocksdbjavastatic% unity.% unity_test checkout_folly, $(MAKECMDGOALS))
|
||||
ROCKS_DEP_RULES=$(filter-out clean format check-format check-buck-targets check-headers check-sources check-workflow-yaml clang-tidy jclean jtest package analyze tags rocksdbjavastatic% unity.% unity_test checkout_folly, $(MAKECMDGOALS))
|
||||
ifneq ("$(ROCKS_DEP_RULES)", "")
|
||||
-include $(DEPFILES)
|
||||
endif
|
||||
@@ -146,10 +146,10 @@ def generate_buck(repo_path, deps_map):
|
||||
src_mk["RANGE_TREE_SOURCES"] + src_mk["TOOL_LIB_SOURCES"],
|
||||
deps=[
|
||||
"//folly/container:f14_hash",
|
||||
"//folly/experimental/coro:blocking_wait",
|
||||
"//folly/experimental/coro:collect",
|
||||
"//folly/experimental/coro:coroutine",
|
||||
"//folly/experimental/coro:task",
|
||||
"//folly/coro:blocking_wait",
|
||||
"//folly/coro:collect",
|
||||
"//folly/coro:coroutine",
|
||||
"//folly/coro:task",
|
||||
"//folly/synchronization:distributed_mutex",
|
||||
],
|
||||
headers=LiteralValue("glob([\"**/*.h\"])")
|
||||
|
||||
@@ -148,6 +148,24 @@ PLATFORM_SHARED_LDFLAGS="-Wl,--no-as-needed -shared -Wl,-soname -Wl,"
|
||||
PLATFORM_SHARED_CFLAGS="-fPIC"
|
||||
PLATFORM_SHARED_VERSIONED=true
|
||||
|
||||
# Prefer lld linker when available on Linux. lld is typically 5-10x faster
|
||||
# than the default ld.bfd for large C++ projects. macOS uses ld64 (or
|
||||
# ld-prime) which is already fast, so we skip lld detection there.
|
||||
# Set ROCKSDB_NO_FAST_LINKER=1 to disable this auto-detection.
|
||||
if [ -z "$ROCKSDB_NO_FAST_LINKER" ] && [ "$TARGET_OS" = "Linux" ]; then
|
||||
if $CXX -fuse-ld=lld -L/usr/local/lib -x c++ - -o /dev/null 2>/dev/null <<EOF
|
||||
int main() { return 0; }
|
||||
EOF
|
||||
then
|
||||
PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -fuse-ld=lld"
|
||||
# Ensure lld can find libraries in /usr/local/lib (lld does not
|
||||
# search there by default, unlike ld.bfd)
|
||||
if [ -d /usr/local/lib ]; then
|
||||
PLATFORM_LDFLAGS="$PLATFORM_LDFLAGS -L/usr/local/lib"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# generic port files (working on all platform by #ifdef) go directly in /port
|
||||
GENERIC_PORT_FILES=`cd "$ROCKSDB_ROOT"; find port -name '*.cc' | tr "\n" " "`
|
||||
|
||||
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env bash
|
||||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
#
|
||||
# Validate GitHub Actions workflow YAML before it reaches CI runtime.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
if ! command -v ruby >/dev/null 2>&1; then
|
||||
echo "ruby is required to validate GitHub Actions workflow YAML"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ruby <<'RUBY'
|
||||
require "psych"
|
||||
|
||||
bad = false
|
||||
workflow_files = Dir[".github/workflows/*.{yml,yaml}"].sort
|
||||
|
||||
if workflow_files.empty?
|
||||
warn "No workflow YAML files found under .github/workflows"
|
||||
exit 1
|
||||
end
|
||||
|
||||
workflow_files.each do |path|
|
||||
begin
|
||||
Psych.parse_file(path)
|
||||
puts "OK #{path}"
|
||||
rescue Psych::Exception => e
|
||||
warn "Invalid YAML in #{path}: #{e.message}"
|
||||
bad = true
|
||||
end
|
||||
end
|
||||
|
||||
exit(bad ? 1 : 0)
|
||||
RUBY
|
||||
@@ -1913,7 +1913,7 @@ sub drain_job_queue {
|
||||
}
|
||||
$last_progress_time = time();
|
||||
$ps_reported = 0;
|
||||
} elsif (not $ps_reported and (time() - $last_progress_time) >= 60) {
|
||||
} elsif (not $ps_reported and (time() - $last_progress_time) >= 300) {
|
||||
# No progress in at least 60 seconds: run ps
|
||||
print $Global::original_stderr "\n";
|
||||
my $script_dir = ::dirname($0);
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
# the docker and docker-registry groups, and logging out and back in to pick
|
||||
# those up.)
|
||||
#
|
||||
# Meta employees: see https://fburl.com/rocksdb-docker to build this on a devvm.
|
||||
#
|
||||
# Follow https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-with-a-personal-access-token-classic
|
||||
# to login with your GitHub credentials, as in
|
||||
#
|
||||
@@ -15,8 +17,8 @@
|
||||
# Then in the build_tools/ubuntu22_image directory, (bump minor version for
|
||||
# random docker file updates, major version tracks Ubuntu release)
|
||||
#
|
||||
# $ docker build -t ghcr.io/facebook/rocksdb_ubuntu:22.0
|
||||
# $ docker push ghcr.io/facebook/rocksdb_ubuntu:22.0
|
||||
# $ docker build -t ghcr.io/facebook/rocksdb_ubuntu:22.2
|
||||
# $ docker push ghcr.io/facebook/rocksdb_ubuntu:22.2
|
||||
#
|
||||
# Might need to change visibility to public through
|
||||
# https://github.com/orgs/facebook/packages/container/rocksdb_ubuntu/settings
|
||||
@@ -28,7 +30,7 @@ FROM ubuntu:22.04
|
||||
RUN apt-get update
|
||||
RUN apt-get upgrade -y
|
||||
# install basic tools
|
||||
RUN apt-get install -y vim wget curl
|
||||
RUN apt-get install -y vim wget curl ccache
|
||||
# install tzdata noninteractive
|
||||
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
|
||||
# install git and default compilers
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
# the docker and docker-registry groups, and logging out and back in to pick
|
||||
# those up.)
|
||||
#
|
||||
# Meta employees: see https://fburl.com/rocksdb-docker to build this on a devvm.
|
||||
#
|
||||
# Follow https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-with-a-personal-access-token-classic
|
||||
# to login with your GitHub credentials, as in
|
||||
#
|
||||
@@ -15,8 +17,8 @@
|
||||
# Then in the build_tools/ubuntu24_image directory, (bump minor version for
|
||||
# random docker file updates, major version tracks Ubuntu release)
|
||||
#
|
||||
# $ docker build -t ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
# $ docker push ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
# $ docker build -t ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
# $ docker push ghcr.io/facebook/rocksdb_ubuntu:24.1
|
||||
#
|
||||
# Might need to change visibility to public through
|
||||
# https://github.com/orgs/facebook/packages/container/rocksdb_ubuntu/settings
|
||||
@@ -28,11 +30,15 @@ FROM ubuntu:24.04
|
||||
RUN apt-get update
|
||||
RUN apt-get upgrade -y
|
||||
# install basic tools
|
||||
RUN apt-get install -y vim wget curl
|
||||
RUN apt-get install -y vim wget curl ccache
|
||||
# install tzdata noninteractive
|
||||
RUN DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC apt-get -y install tzdata
|
||||
# install git and default compilers
|
||||
RUN apt-get install -y git gcc g++ clang clang-tools
|
||||
# install clang-21 from LLVM snapshot repo
|
||||
RUN curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor -o /etc/apt/trusted.gpg.d/llvm.gpg && \
|
||||
echo "deb https://apt.llvm.org/noble/ llvm-toolchain-noble-21 main" > /etc/apt/sources.list.d/llvm-21.list && \
|
||||
apt-get update && apt-get install -y clang-21
|
||||
# install basic package
|
||||
RUN apt-get install -y lsb-release software-properties-common gnupg
|
||||
# install gflags, tbb
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
You are an expert C++ engineer for RocksDB.
|
||||
Read CLAUDE.md in the repo root and claude_md/ files for project context.
|
||||
Answer thoroughly with exact file paths and line numbers.
|
||||
@@ -0,0 +1,14 @@
|
||||
Read review-findings.md. This file contains partial code
|
||||
review findings from a review session that ran out of turns before
|
||||
finishing. Format them into a clean final review comment using the
|
||||
output format defined in claude_md/code_review.md (Summary, Issues
|
||||
Found, Cross-Component Analysis, Positive Observations).
|
||||
|
||||
At the top of the review, add this notice:
|
||||
> **Partial review** -- the analysis was interrupted before all
|
||||
> perspectives were completed. The findings below cover the
|
||||
> perspectives that were finished. You can request a full re-review
|
||||
> with `/claude-review`.
|
||||
|
||||
Write ONLY the formatted review as your final response -- no
|
||||
commentary or explanation outside the review itself.
|
||||
@@ -0,0 +1,606 @@
|
||||
# Code Review Workflow — CI Prompt
|
||||
|
||||
## Purpose
|
||||
Thorough multi-agent code review for RocksDB commits following CLAUDE.md guidelines,
|
||||
with structured codebase exploration, inter-agent debate, and verification.
|
||||
|
||||
IMPORTANT — Incremental output: After completing EACH major phase (Setup,
|
||||
Codebase Context, Initial Review, Debate, Synthesis), append your findings
|
||||
to review-findings.md using the Write tool. This ensures partial results are
|
||||
saved even if the review is interrupted by the turn limit. After the final
|
||||
synthesis, write the complete review to review-findings.md, replacing
|
||||
previous content. Also output the final review as your response text.
|
||||
|
||||
## Workflow Steps
|
||||
|
||||
### 1. Setup Phase
|
||||
- Read CLAUDE.md to understand review guidelines
|
||||
- Identify the commit/diff/PR to review
|
||||
- The PR diff is provided below in this prompt. The repository is already
|
||||
checked out — use local file search tools (Grep, Glob, Read) to explore.
|
||||
- Parse the changed files and categorize them (API, core logic, tests, etc.)
|
||||
- Read all changed files to build full context
|
||||
|
||||
### 2. Codebase Context Phase (CRITICAL — Do Not Skip)
|
||||
|
||||
**Why this matters**: Review agents that only see the diff miss systemic bugs.
|
||||
For example, a change to an iterator's return value might look correct in
|
||||
isolation but break multi-SST iteration 5 layers up the call stack. A change
|
||||
to error handling might silently drop errors that a caller 3 levels up relies
|
||||
on for recovery. This phase builds the context that prevents those blind spots.
|
||||
|
||||
Go deep. Surface-level reading leads to reviews that miss caching layers,
|
||||
existing helper functions, or concurrency requirements. The most expensive
|
||||
failure mode is findings that look correct in isolation but miss how the
|
||||
change breaks the surrounding system.
|
||||
|
||||
The team lead spawns one or more research agents to perform the following
|
||||
analyses using local file search tools (Grep, Glob, Read). The research
|
||||
must be written to `context.md` BEFORE any review agent is spawned.
|
||||
|
||||
#### 2a. Subsystem Deep-Read
|
||||
Read the changed files AND their surrounding subsystem **in depth** — not just
|
||||
signatures, but actual logic, edge cases, data flows, locking protocols,
|
||||
concurrency patterns, and interactions between components.
|
||||
|
||||
For each changed file, also read:
|
||||
- The header that declares its interface (if the change is in a .cc file)
|
||||
- The sibling implementation that does the same thing the "standard" way
|
||||
(e.g., for a UDI iterator change, read how `IndexBlockIter` handles the
|
||||
same scenario — it's the reference implementation)
|
||||
- The wrapper/adapter layer that sits between this code and its callers
|
||||
- Any existing tests to understand what behaviors are currently guaranteed
|
||||
|
||||
#### 2b. Caller-Chain Analysis (upstream impact)
|
||||
For each changed function/method, trace the call chain UPWARD 3-5 levels to
|
||||
understand who consumes the changed behavior and what invariants they rely on.
|
||||
**Focus on control flow decisions** — if/else branches, while loop conditions,
|
||||
early returns — that depend on the changed return values or state.
|
||||
|
||||
**How to do this**:
|
||||
- Search for callers of each changed function (search for the function name, the
|
||||
class method, virtual overrides)
|
||||
- For each caller, search for ITS callers, repeating 3-5 levels up
|
||||
- At each level, read the actual code to understand the control flow decision
|
||||
- Stop when you reach a "terminal" caller (e.g., user-facing API, top-level
|
||||
iterator) or when the propagation is clearly safe
|
||||
|
||||
#### 2c. Callee-Chain Analysis (downstream dependencies AND side effects)
|
||||
For each changed function, trace what it calls and whether those calls have
|
||||
new preconditions or changed semantics. **Critically, trace SIDE EFFECTS —
|
||||
not just return values.** Many bugs hide in side effects on shared state
|
||||
(counters, sequence numbers, flags) that are invisible if you only check
|
||||
return values.
|
||||
|
||||
**For each callee, ask:**
|
||||
1. What does it RETURN? (the obvious check)
|
||||
2. What shared state does it MUTATE? (the non-obvious check)
|
||||
3. Under what PRECONDITIONS does it operate correctly?
|
||||
4. Are those preconditions met in the new calling context?
|
||||
|
||||
#### 2d. Sibling Implementation Comparison
|
||||
Identify the "reference" or "standard" implementation that does the same thing
|
||||
the changed code does, and compare their behavior. This is critical for plugin/
|
||||
extension APIs where the implementation must match an implicit contract.
|
||||
|
||||
#### 2e. Invariant Documentation
|
||||
Document the key invariants that the changed code must maintain. Read existing
|
||||
comments, assertions, and test expectations to discover these invariants.
|
||||
|
||||
#### 2f. Related Functionality and Existing Conventions
|
||||
Identify related functionality that already exists in the codebase:
|
||||
- Are there existing helper functions the change should use (or is duplicating)?
|
||||
- Are there existing patterns for the same kind of change (e.g., how other
|
||||
Customizable subclasses handle deprecation)?
|
||||
- Are there existing tests that test similar scenarios (e.g., multi-SST
|
||||
iteration tests that could be extended)?
|
||||
|
||||
#### 2g. Cross-Component Data Consumer Analysis (CRITICAL — common blind spot)
|
||||
|
||||
**Why this is separate from caller-chain analysis:** Sections 2b-2c trace
|
||||
who CALLS the changed functions. This section traces who CONSUMES the data
|
||||
that the changed code PRODUCES. These are often completely different
|
||||
subsystems.
|
||||
|
||||
When the change writes data to a shared structure (memtable, SST block, cache
|
||||
entry, statistics counter), ask: **"Who reads this data, under what rules,
|
||||
and do those rules match the writer's assumptions?"**
|
||||
|
||||
**How to do this:**
|
||||
1. Identify every piece of data the change WRITES (e.g., a range tombstone
|
||||
entry, a counter update, a metadata field)
|
||||
2. For each, search the codebase for ALL readers of that data structure
|
||||
3. For each reader, check:
|
||||
- Does the reader apply visibility rules (seqno, read_callback, snapshot)?
|
||||
- Are those rules compatible with the writer's assumptions?
|
||||
- Does the reader assume invariants about the data (e.g., seqno ordering,
|
||||
monotonicity) that the writer might violate?
|
||||
|
||||
#### 2h. Alternative Execution Contexts
|
||||
|
||||
The same code path may run in different contexts with different assumptions.
|
||||
**Enumerate all contexts and check each one.** This is where "works in the
|
||||
common case but breaks in edge configurations" bugs hide.
|
||||
|
||||
For RocksDB, always check whether the changed code interacts differently with:
|
||||
|
||||
| Context | Key difference | Common failure mode |
|
||||
|---------|---------------|-------------------|
|
||||
| WritePreparedTxnDB / WriteUnpreparedTxnDB | `read_callback_` controls visibility, not just seqno | Visibility bypass |
|
||||
| ReadOnly DB / SecondaryInstance | No mutable memtable, writes not allowed | Null pointer or no-op needed |
|
||||
| CompactionService / Remote compaction | Different process, serialized state | Serialization mismatch |
|
||||
| User-defined timestamps | Extra dimension in key comparison and visibility | Wrong ordering |
|
||||
| MemPurge | Memtable-to-memtable, not memtable-to-SST | Missing data |
|
||||
| Column family with BlobDB | Values may be in blob files, not inline | Missing dereference |
|
||||
| Snapshots (old, held long-term) | Snapshot seqno may be far behind current state | Metadata corruption |
|
||||
| Concurrent writers (allow_concurrent_memtable_write) | Lock-free paths vs locked paths | Lost updates |
|
||||
| FIFO / Universal compaction | Different compaction invariants than Level | Wrong assumptions |
|
||||
| Prefix seek / total order seek | Different iterator behavior | Wrong iteration results |
|
||||
|
||||
**For each context, ask:**
|
||||
1. Does the code execute in this context? (Check constructor, feature flags)
|
||||
2. If yes, do the assumptions still hold?
|
||||
3. If not, should the feature be disabled in this context?
|
||||
|
||||
#### 2i. Assumption Stress-Testing (the "logically X" audit)
|
||||
|
||||
When the change claims a property (e.g., "logically redundant," "no-op in
|
||||
this case," "safe because X"), **systematically break it:**
|
||||
|
||||
1. **State the claim precisely.** E.g., "The inserted range tombstone is
|
||||
logically redundant — it covers only keys already deleted by point
|
||||
tombstones."
|
||||
2. **List the preconditions** that must be true for the claim to hold.
|
||||
3. **For each precondition, construct a counterexample** — a concrete scenario
|
||||
where the precondition is violated.
|
||||
4. **Verify each counterexample against the code.** Does the code guard
|
||||
against it? If not, it's a bug.
|
||||
|
||||
**Anti-pattern: treating asserts as proofs.** When you see an assert, do NOT
|
||||
conclude "the invariant holds." Instead ask: "Can I construct an input where
|
||||
this assert fires?" If yes, it's a bug (the assert catches it in debug but
|
||||
release builds silently corrupt). If you cannot construct a counterexample
|
||||
after trying, document WHY it's impossible.
|
||||
|
||||
#### 2j. Write Context Document
|
||||
Write the complete analysis to `context.md` in the review folder. This document
|
||||
is provided to ALL review agents as part of their prompt.
|
||||
|
||||
**Context document template**:
|
||||
```markdown
|
||||
# Codebase Context for Review
|
||||
|
||||
## How the Relevant Subsystem Works Today
|
||||
[Detailed description of the subsystem architecture, data flows,
|
||||
and component interactions. Not just "what" but "how" and "why."]
|
||||
|
||||
## Changed Functions and Their Call Chains
|
||||
|
||||
### function_name() (file:line)
|
||||
**What changed**: [brief description of the behavioral change]
|
||||
|
||||
**Upstream callers** (who depends on this):
|
||||
1. CallerA::method() (file:line) — uses return value to decide X
|
||||
2. CallerB::method() (file:line) — passes result to CallerC
|
||||
3. CallerC::method() (file:line) — THE CRITICAL DECISION POINT: ...
|
||||
|
||||
**Downstream callees** (what this depends on):
|
||||
1. calleeA() — behavior unchanged
|
||||
2. calleeB() — NEW dependency, requires X
|
||||
|
||||
**Sibling implementation** (how the "standard" version handles this):
|
||||
- StandardImpl::method() does Y at file:line, which ensures invariant Z
|
||||
- The changed code must match or document any deviation
|
||||
|
||||
**Key invariants**:
|
||||
- Must never return X when Y is true because CallerC will...
|
||||
- Must always set Z before returning because CallerB assumes...
|
||||
|
||||
## System Architecture Context
|
||||
[How the changed components fit into the overall system]
|
||||
|
||||
## Known Invariants That Must Be Preserved
|
||||
1. ...
|
||||
2. ...
|
||||
|
||||
## Existing Conventions and Related Code
|
||||
- [helper functions, patterns, existing tests that are relevant]
|
||||
|
||||
## Cross-Component Data Consumers
|
||||
For each piece of data the change WRITES to a shared structure:
|
||||
### [data item] written to [structure]
|
||||
**Writer assumptions**: [what the writer assumes about how this data is consumed]
|
||||
**All readers**:
|
||||
1. ReaderA::method() (file:line) — reads under [visibility rules]
|
||||
- Compatible with writer? YES/NO. If NO, explain the mismatch.
|
||||
2. ReaderB::method() (file:line) — reads under [different rules]
|
||||
- Compatible with writer? YES/NO.
|
||||
|
||||
## Alternative Execution Contexts
|
||||
| Context | Does code execute? | Assumptions hold? | Action needed? |
|
||||
|---------|-------------------|-------------------|----------------|
|
||||
| WritePreparedTxnDB | YES/NO | YES/NO | [disable/guard/safe] |
|
||||
| Old snapshots | YES/NO | YES/NO | ... |
|
||||
| User-defined timestamps | YES/NO | YES/NO | ... |
|
||||
| ReadOnly DB | YES/NO | YES/NO | ... |
|
||||
| ... | ... | ... | ... |
|
||||
|
||||
## Assumption Stress Test
|
||||
### Claim: "[the design claim, e.g., logically redundant]"
|
||||
**Preconditions for claim to hold:**
|
||||
1. [precondition] — counterexample: [scenario]. Guarded? YES/NO.
|
||||
2. [precondition] — counterexample: [scenario]. Guarded? YES/NO.
|
||||
|
||||
## Potential Pitfalls
|
||||
- [specific scenarios where the change could interact badly with
|
||||
the surrounding system, identified from the caller-chain analysis]
|
||||
```
|
||||
|
||||
### 3. Initial Review Phase (Parallel)
|
||||
Create a team and spawn 5 review agents in parallel. **Include the context
|
||||
document from Phase 2 in each agent's prompt.** Each agent writes findings
|
||||
to its own file and sends a summary message to the team lead.
|
||||
|
||||
Each agent's prompt should include:
|
||||
```
|
||||
## Codebase Context (READ THIS FIRST)
|
||||
[paste or reference the context.md file]
|
||||
|
||||
You MUST consider how your findings interact with the upstream callers
|
||||
and system invariants documented above. A finding that looks correct
|
||||
in isolation may be a critical bug when you consider the full call chain.
|
||||
```
|
||||
|
||||
#### Agent: Design & Approach Reviewer
|
||||
- Read the commit message deeply to understand the problem being solved
|
||||
- Analyze whether this is the right approach to the problem
|
||||
- Consider alternative designs and trade-offs:
|
||||
- Are there simpler solutions that achieve the same goal?
|
||||
- Does the approach introduce unnecessary complexity?
|
||||
- Would a different data structure or algorithm be more appropriate?
|
||||
- Evaluate whether the change follows existing architectural patterns or
|
||||
introduces new patterns that may be inconsistent
|
||||
- Assess the scope: is the change too large? Should it be split into
|
||||
smaller, independently reviewable pieces?
|
||||
- Check if the approach has precedent in the codebase or in the academic
|
||||
literature it references (e.g., SuRF paper)
|
||||
|
||||
#### Agent: Correctness Reviewer
|
||||
- Thread safety and concurrency analysis
|
||||
- Error handling and propagation via Status type
|
||||
- Edge cases (empty inputs, overflow, boundary conditions)
|
||||
- Data corruption scenarios
|
||||
- Logic correctness of new algorithms
|
||||
- **Behavioral contract changes**: Do return value semantics match what
|
||||
upstream callers expect? (Use the caller-chain analysis from context.md)
|
||||
- **Callee side effects**: Does the change call functions that mutate shared
|
||||
state (counters, seqnos, metadata)? Are the mutations correct for the new
|
||||
calling context? (Use the callee-chain analysis from context.md)
|
||||
|
||||
#### Agent: Cross-Component & Adversarial Reviewer
|
||||
|
||||
This agent exists because the most critical bugs hide at component boundaries,
|
||||
not within a single component. It uses a fundamentally different methodology
|
||||
than the correctness reviewer: instead of verifying "does the code do what it
|
||||
intends?", it asks "what breaks when we change the assumptions?"
|
||||
|
||||
**Data-flow analysis:** Perform the cross-component data consumer analysis
|
||||
described in section 2g. For every piece of data the change WRITES to a shared
|
||||
structure, trace ALL READERS and verify their visibility rules are compatible
|
||||
with the writer's assumptions. This is a DATA-FLOW question, not a
|
||||
CONTROL-FLOW question — readers may be in completely different subsystems.
|
||||
|
||||
**Alternative execution contexts:** Use the canonical table from section 2h.
|
||||
Enumerate all contexts where the changed code executes and verify assumptions
|
||||
hold in each.
|
||||
|
||||
**Assumption stress-testing and assert-breaking:** Follow the methodology from
|
||||
section 2i. Identify every design claim, enumerate preconditions, construct
|
||||
counterexamples. Treat asserts as hypotheses to break, not proofs.
|
||||
**Red flag words**: "logically redundant", "safe because", "no-op",
|
||||
"always true", "cannot happen", "invariant holds"
|
||||
|
||||
**Callee side-effect audit:** Perform the callee side-effect audit described
|
||||
in section 2c. For every callee, ask what it RETURNS and what it MUTATES.
|
||||
|
||||
Write findings to `findings-cross-component.md`.
|
||||
|
||||
#### Agent: Invariant Adversary
|
||||
|
||||
This agent does NOT read the diff in detail. It takes the feature summary and
|
||||
systematically tries to break it. While other agents verify "does the code do
|
||||
what it says?", this agent asks "what existing system invariants does this
|
||||
feature violate?" and "under what inputs do the design claims fail?"
|
||||
|
||||
**Steps 1-4: Assumption stress-testing.** Follow the methodology from section
|
||||
2i: extract design claims, enumerate preconditions, construct counterexamples,
|
||||
and attempt to break every assert. Be exhaustive about input parameter ranges,
|
||||
shared state configurations, and concurrent interleaving.
|
||||
|
||||
**Step 5: Callee side-effect audit.** Perform the callee side-effect audit
|
||||
described in section 2c. For every function the changed code calls, list ALL
|
||||
mutations to shared state (atomic CAS loops, counter increments, flag/metadata
|
||||
updates, cache invalidation). A function returning `Status::OK()` does NOT
|
||||
mean its side effects are correct.
|
||||
|
||||
Write findings to `findings-invariant-adversary.md`.
|
||||
|
||||
#### Agent: Caller-Context Auditor
|
||||
|
||||
This agent traces BACKWARD from every entry point the changed code hooks into.
|
||||
While other agents read the changed code forward ("what does it do?"), this
|
||||
agent enumerates WHO invokes it and WITH WHAT parameter ranges.
|
||||
|
||||
The key insight: a function that is correct for all *typical* inputs may be
|
||||
catastrophically wrong for *atypical-but-reachable* inputs. This agent's job
|
||||
is to find the atypical inputs.
|
||||
|
||||
**Step 1: Identify entry points.** List every function/constructor/method in
|
||||
the changed code that receives external input (parameters, config values,
|
||||
pointers to shared state). Include:
|
||||
- Constructor parameters (e.g., `active_mem`, `read_callback`, `sequence`)
|
||||
- Virtual method overrides that callers invoke polymorphically
|
||||
- Functions called from multiple subsystems
|
||||
|
||||
**Step 2: Enumerate all callers (3-5 levels up).** For each entry point,
|
||||
search the codebase for ALL callers. Do NOT stop at the first caller — trace
|
||||
the full call chain. Use `Grep` and `Glob` to find:
|
||||
- Direct callers of the function
|
||||
- Callers of wrapper/adapter functions that forward to it
|
||||
- Factory functions that construct the object
|
||||
- Virtual dispatch sites (search for the base class method name)
|
||||
|
||||
**Step 3: Parameter range analysis.** For each caller, determine:
|
||||
- What values does it pass for each parameter?
|
||||
- Under what conditions does it call this function?
|
||||
- Are there callers that pass unusual values? (e.g., `kMaxSequenceNumber`,
|
||||
`nullptr`, old snapshot seqnos, non-null `read_callback_`)
|
||||
|
||||
Build a parameter range table for each entry point, listing all callers
|
||||
and the values they pass for each parameter.
|
||||
|
||||
**Step 4: Configuration matrix.** Enumerate option/config combinations that
|
||||
affect the changed code path:
|
||||
- Which options enable/disable the feature?
|
||||
- Which options change the execution context? (e.g.,
|
||||
`allow_concurrent_memtable_write`, `use_trie_index`)
|
||||
- Are there option combinations that create contradictions?
|
||||
|
||||
**Step 5: Cross-reference with context.md.** Compare your caller analysis
|
||||
with the invariants and execution contexts documented in context.md. Flag
|
||||
any caller that violates an assumed precondition.
|
||||
|
||||
Write findings to `findings-caller-audit.md`.
|
||||
|
||||
#### Agent: Performance Reviewer
|
||||
- Memory allocation on hot paths
|
||||
- Unnecessary copies (string, buffer)
|
||||
- Cache efficiency and data locality
|
||||
- Loop optimization opportunities
|
||||
- Branch prediction (LIKELY/UNLIKELY)
|
||||
- Zero-overhead design verification
|
||||
|
||||
#### Agent: API & Compatibility Reviewer
|
||||
- Public API backwards compatibility
|
||||
- API consistency with existing patterns
|
||||
- Documentation completeness
|
||||
- Deprecation policy compliance
|
||||
- Forward compatibility (struct extensibility)
|
||||
- **Behavioral compatibility**: Do changed semantics (e.g., kOutOfBound vs
|
||||
kUnknown) break implicit contracts with callers?
|
||||
|
||||
#### Agent: Serialization & Deserialization Reviewer
|
||||
- Format correctness and versioning
|
||||
- Alignment requirements
|
||||
- Integer overflow in size computations
|
||||
- Bounds checking on untrusted data
|
||||
- Crafted input attack vectors
|
||||
|
||||
#### Agent: Test Coverage Reviewer
|
||||
- Edge case coverage
|
||||
- Failure mode testing (corruption, truncation)
|
||||
- Round-trip testing (build → serialize → deserialize → verify)
|
||||
- Integration test coverage
|
||||
- **System-level test coverage**: Are the caller-chain interactions tested?
|
||||
(e.g., multi-SST iteration with UDI, level compaction with new behavior)
|
||||
- Test quality (no flaky patterns, good assertions)
|
||||
|
||||
### 4. Round-Robin Debate Phase
|
||||
After all 5 agents complete their initial review, the team lead orchestrates
|
||||
a structured debate:
|
||||
|
||||
#### Step 4a: Share findings
|
||||
- Team lead sends each agent a summary of ALL other agents' findings
|
||||
(or instructs each agent to read the other agents' findings files)
|
||||
|
||||
#### Step 4b: Critique round (2 rounds)
|
||||
Each agent reviews the findings from the other agents and sends messages to
|
||||
challenge, support, or refine them:
|
||||
|
||||
- **Challenge**: "I disagree with [agent] [finding] because [evidence]."
|
||||
- **Support**: "I independently found the same issue — this confirms it."
|
||||
- **Refine**: "[Finding A] is actually a consequence of [Finding B].
|
||||
The root cause is the same."
|
||||
|
||||
The debate assignment follows a round-robin pattern:
|
||||
```
|
||||
correctness-reviewer → critiques → invariant-adversary, serialization
|
||||
cross-component-reviewer → critiques → correctness, caller-audit
|
||||
invariant-adversary → critiques → correctness, cross-component
|
||||
caller-audit → critiques → invariant-adversary, cross-component
|
||||
performance-reviewer → critiques → api, cross-component
|
||||
api-reviewer → critiques → correctness, performance
|
||||
serialization-reviewer → critiques → correctness, invariant-adversary
|
||||
test-reviewer → critiques → serialization, caller-audit
|
||||
design-reviewer → critiques → cross-component, invariant-adversary
|
||||
```
|
||||
|
||||
**Note:** The invariant-adversary and caller-audit agents are deliberately
|
||||
cross-linked with correctness and cross-component because their findings
|
||||
often reveal the same underlying bug from different angles (invariant
|
||||
violation vs reachable bad input vs data-flow mismatch).
|
||||
|
||||
Each agent should:
|
||||
1. Read the assigned agents' findings files
|
||||
2. Send a critique message to each assigned agent
|
||||
3. Respond to critiques received from other agents
|
||||
4. Update their own findings file with any revisions (upgraded/downgraded severity,
|
||||
withdrawn findings, new findings inspired by others)
|
||||
|
||||
#### Step 4c: Team lead collects debate results
|
||||
- Read all updated findings files
|
||||
- Review the debate messages
|
||||
- Build consensus: findings supported by 2+ agents → HIGH confidence
|
||||
- Write consensus document with final severity classifications
|
||||
|
||||
### 5. Consensus Phase
|
||||
Team lead synthesizes the debate into a consensus document:
|
||||
- Cross-reference overlapping findings across agents
|
||||
- Count agreement (majority = 2+ agents independently flagging or supporting)
|
||||
- Note disagreements and how they were resolved
|
||||
- Classify findings as HIGH/MEDIUM/LOW severity
|
||||
- Write consensus document
|
||||
|
||||
### 6. Summary Phase
|
||||
- Compile all validated findings ordered by severity
|
||||
- Include detailed bug analysis (root cause, vulnerable code paths)
|
||||
- Include suggested fixes
|
||||
- Note which findings were debated and the outcome
|
||||
- Write the complete review to review-findings.md and output as response
|
||||
|
||||
**Final report quality rules:**
|
||||
- The final report must be CLEAN and POLISHED. No stream-of-consciousness,
|
||||
no "Wait, actually...", no "on closer re-examination". Those belong in
|
||||
the working notes, not the output.
|
||||
- If a finding was raised during review but later disproven during debate
|
||||
or deeper analysis, REMOVE IT entirely from the final report. Do not
|
||||
include it with a retraction — just drop it.
|
||||
- If a finding was downgraded (e.g., Critical → Suggestion), present it
|
||||
at the final severity only. Do not narrate the severity change.
|
||||
- Each finding in the final report should read as a confident, verified
|
||||
conclusion — not a record of the analysis process.
|
||||
- The reader should never see the reviewer arguing with itself.
|
||||
|
||||
## Review Checklist
|
||||
|
||||
### Context Phase (must be completed before agents spawn)
|
||||
- [ ] Subsystem deep-read — read changed files AND surrounding subsystem
|
||||
in depth (logic, edge cases, data flows, locking, concurrency)
|
||||
- [ ] Caller chain for every changed public/virtual method (3-5 levels up)
|
||||
- [ ] Critical decision points where callers branch on changed behavior
|
||||
- [ ] Callee chain with SIDE EFFECTS — downstream dependencies, new
|
||||
preconditions, AND mutations to shared state (section 2c)
|
||||
- [ ] Sibling implementations — how does the standard version handle same scenarios?
|
||||
- [ ] Invariants that callers rely on
|
||||
- [ ] Related functionality — existing helpers, patterns, conventions
|
||||
- [ ] Cross-component data consumers — for every data WRITTEN, find ALL readers
|
||||
and verify visibility rules match (section 2g)
|
||||
- [ ] Alternative execution contexts verified (section 2h table)
|
||||
- [ ] Assumption stress-test — for every design claim, list preconditions
|
||||
and construct counterexamples (section 2i)
|
||||
- [ ] Multi-component interactions (compaction, recovery, snapshots, iterators)
|
||||
- [ ] Configuration dependencies and unexpected option combinations
|
||||
- [ ] Potential pitfalls from caller-chain analysis
|
||||
|
||||
### Review Phase (verified by agents)
|
||||
- [ ] Database semantics preserved (snapshot isolation, key ordering)
|
||||
- [ ] All error cases handled
|
||||
- [ ] Thread-safe with correct synchronization
|
||||
- [ ] No data races or deadlocks
|
||||
- [ ] Appropriate test coverage (edge cases, failure modes, system-level)
|
||||
- [ ] No unnecessary allocations or copies in hot paths
|
||||
- [ ] Backwards compatible
|
||||
- [ ] New APIs consistent with existing patterns and documented
|
||||
- [ ] Code follows RocksDB style conventions
|
||||
- [ ] Behavioral contracts with upstream callers preserved
|
||||
- [ ] All callers enumerated with parameter range table
|
||||
|
||||
## Output Structure
|
||||
Write all review artifacts to the working directory root:
|
||||
- `review-findings.md` — Incremental findings (appended after each phase),
|
||||
then replaced with the final synthesized review at the end
|
||||
- `context.md` — Codebase context (call chains, invariants)
|
||||
- `findings-*.md` — Per-agent findings (design, correctness, cross-component,
|
||||
invariant-adversary, caller-audit, performance, api, serialization, tests)
|
||||
- `consensus.md` — Cross-review consensus (post-debate)
|
||||
|
||||
|
||||
|
||||
## Team Structure
|
||||
```
|
||||
Team Lead (you)
|
||||
│
|
||||
├── Phase 2: Codebase Context (team lead or dedicated research agent)
|
||||
│ └── context-researcher (general-purpose agent)
|
||||
│ ├── Trace caller chains (3-5 levels up)
|
||||
│ ├── Trace callee chains (dependencies AND side effects)
|
||||
│ ├── Trace data consumers (who reads what the change writes?)
|
||||
│ └── Document invariants
|
||||
│
|
||||
├── Phase 3: Initial Review (parallel, run_in_background)
|
||||
│ │ (all agents receive context.md in their prompt)
|
||||
│ ├── design-reviewer (general-purpose agent)
|
||||
│ ├── correctness-reviewer (general-purpose agent)
|
||||
│ ├── cross-component-reviewer (general-purpose agent)
|
||||
│ ├── invariant-adversary (general-purpose agent) ← NEW
|
||||
│ ├── caller-audit (general-purpose agent) ← NEW
|
||||
│ ├── performance-reviewer (general-purpose agent)
|
||||
│ ├── api-reviewer (general-purpose agent)
|
||||
│ ├── serialization-reviewer (general-purpose agent)
|
||||
│ └── test-reviewer (general-purpose agent)
|
||||
│
|
||||
├── Phase 4: Debate (agents message each other)
|
||||
│ ├── correctness ↔ invariant-adversary, serialization
|
||||
│ ├── cross-component ↔ correctness, caller-audit
|
||||
│ ├── invariant-adversary ↔ correctness, cross-component
|
||||
│ ├── caller-audit ↔ invariant-adversary, cross-component
|
||||
│ ├── performance ↔ api, cross-component
|
||||
│ ├── api ↔ correctness, performance
|
||||
│ ├── serialization ↔ correctness, invariant-adversary
|
||||
│ ├── test-coverage ↔ serialization, caller-audit
|
||||
│ └── design ↔ cross-component, invariant-adversary
|
||||
│
|
||||
```
|
||||
|
||||
|
||||
## Agent Communication Protocol
|
||||
|
||||
### During Initial Review (Phase 3)
|
||||
- Each agent writes findings to its own file
|
||||
- Each agent sends a summary message to team lead when done
|
||||
- Agents do NOT communicate with each other yet
|
||||
|
||||
### During Debate (Phase 4)
|
||||
- Team lead sends each agent a message with instructions to:
|
||||
1. Read the assigned agents' findings files
|
||||
2. Send critique messages to those agents
|
||||
3. Respond to incoming critiques
|
||||
4. Update their own findings file with revisions
|
||||
- Each critique message should include:
|
||||
- Which finding they're addressing (e.g., "Correctness F1")
|
||||
- Whether they AGREE, DISAGREE, or want to REFINE
|
||||
- Their reasoning with code evidence
|
||||
- Suggested severity adjustment (if any)
|
||||
|
||||
### Message format example
|
||||
```
|
||||
To: correctness-reviewer
|
||||
Re: Your Finding F1
|
||||
|
||||
AGREE/DISAGREE/REFINE - [reasoning with code evidence].
|
||||
[Suggested severity adjustment if any.]
|
||||
```
|
||||
|
||||
## Review Anti-Patterns
|
||||
|
||||
These recurring failure modes lead to missed bugs. Each is detailed in the
|
||||
referenced section; this table is a quick-reference checklist.
|
||||
|
||||
| Anti-Pattern | Fix | Reference |
|
||||
|---|---|---|
|
||||
| Return-Value Tunnel Vision | Trace callee MUTATIONS, not just returns | Section 2c |
|
||||
| Default-Configuration Bias | Enumerate all execution contexts | Section 2h |
|
||||
| Assert-as-Proof | Try to BREAK every assert | Section 2i |
|
||||
| Write-Path-Only Analysis | Trace data readers, not just writers | Section 2g |
|
||||
| Confirmation-Seeking Research | Use adversarial prompts ("find where X fails") | Invariant Adversary agent |
|
||||
| Data-Flow vs Control-Flow Confusion | Separate who CALLS from who READS the data | Section 2g |
|
||||
+18
-7
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "db/blob/blob_fetcher.h"
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/version_set.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
@@ -14,10 +16,13 @@ Status BlobFetcher::FetchBlob(const Slice& user_key,
|
||||
FilePrefetchBuffer* prefetch_buffer,
|
||||
PinnableSlice* blob_value,
|
||||
uint64_t* bytes_read) const {
|
||||
assert(version_);
|
||||
|
||||
return version_->GetBlob(read_options_, user_key, blob_index_slice,
|
||||
prefetch_buffer, blob_value, bytes_read);
|
||||
BlobIndex blob_index;
|
||||
Status status = blob_index.DecodeFrom(blob_index_slice);
|
||||
if (status.ok()) {
|
||||
status = FetchBlob(user_key, blob_index, prefetch_buffer, blob_value,
|
||||
bytes_read);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
Status BlobFetcher::FetchBlob(const Slice& user_key,
|
||||
@@ -25,10 +30,16 @@ Status BlobFetcher::FetchBlob(const Slice& user_key,
|
||||
FilePrefetchBuffer* prefetch_buffer,
|
||||
PinnableSlice* blob_value,
|
||||
uint64_t* bytes_read) const {
|
||||
assert(version_);
|
||||
if (!allow_write_path_fallback_) {
|
||||
assert(version_);
|
||||
|
||||
return version_->GetBlob(read_options_, user_key, blob_index, prefetch_buffer,
|
||||
blob_value, bytes_read);
|
||||
return version_->GetBlob(read_options_, user_key, blob_index,
|
||||
prefetch_buffer, blob_value, bytes_read);
|
||||
}
|
||||
|
||||
return BlobFilePartitionManager::ResolveBlobDirectWriteIndex(
|
||||
read_options_, user_key, blob_index, version_, blob_file_cache_,
|
||||
prefetch_buffer, blob_value, bytes_read);
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
+13
-3
@@ -10,17 +10,25 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
class BlobFileCache;
|
||||
class Version;
|
||||
class Slice;
|
||||
class FilePrefetchBuffer;
|
||||
class PinnableSlice;
|
||||
class BlobIndex;
|
||||
|
||||
// A thin wrapper around the blob retrieval functionality of Version.
|
||||
// A thin wrapper around blob retrieval. By default it reads through Version,
|
||||
// and it can optionally fall back to direct-write blob files that are not yet
|
||||
// manifest-visible.
|
||||
class BlobFetcher {
|
||||
public:
|
||||
BlobFetcher(const Version* version, const ReadOptions& read_options)
|
||||
: version_(version), read_options_(read_options) {}
|
||||
BlobFetcher(const Version* version, const ReadOptions& read_options,
|
||||
BlobFileCache* blob_file_cache = nullptr,
|
||||
bool allow_write_path_fallback = false)
|
||||
: version_(version),
|
||||
read_options_(read_options),
|
||||
blob_file_cache_(blob_file_cache),
|
||||
allow_write_path_fallback_(allow_write_path_fallback) {}
|
||||
|
||||
Status FetchBlob(const Slice& user_key, const Slice& blob_index_slice,
|
||||
FilePrefetchBuffer* prefetch_buffer,
|
||||
@@ -33,5 +41,7 @@ class BlobFetcher {
|
||||
private:
|
||||
const Version* version_;
|
||||
ReadOptions read_options_;
|
||||
BlobFileCache* blob_file_cache_;
|
||||
bool allow_write_path_fallback_;
|
||||
};
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -67,13 +67,11 @@ BlobFileBuilder::BlobFileBuilder(
|
||||
min_blob_size_(mutable_cf_options->min_blob_size),
|
||||
blob_file_size_(mutable_cf_options->blob_file_size),
|
||||
blob_compression_type_(mutable_cf_options->blob_compression_type),
|
||||
// TODO: support most CompressionOptions with a new CF option
|
||||
// blob_compression_opts
|
||||
// TODO with schema change: support custom compression manager and options
|
||||
// such as max_compressed_bytes_per_kb
|
||||
// NOTE: returns nullptr for kNoCompression
|
||||
blob_compressor_(GetBuiltinV2CompressionManager()->GetCompressor(
|
||||
CompressionOptions{}, blob_compression_type_)),
|
||||
mutable_cf_options->blob_compression_opts, blob_compression_type_)),
|
||||
blob_compressor_wa_(blob_compressor_
|
||||
? blob_compressor_->ObtainWorkingArea()
|
||||
: Compressor::ManagedWorkingArea{}),
|
||||
@@ -111,7 +109,7 @@ Status BlobFileBuilder::Add(const Slice& key, const Slice& value,
|
||||
assert(blob_index);
|
||||
assert(blob_index->empty());
|
||||
|
||||
if (value.size() < min_blob_size_) {
|
||||
if (value.empty() || value.size() < min_blob_size_) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
|
||||
+125
-3
@@ -9,6 +9,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "db/blob/blob_file_reader.h"
|
||||
#include "logging/logging.h"
|
||||
#include "options/cf_options.h"
|
||||
#include "rocksdb/cache.h"
|
||||
#include "rocksdb/slice.h"
|
||||
@@ -38,7 +39,8 @@ BlobFileCache::BlobFileCache(Cache* cache,
|
||||
|
||||
Status BlobFileCache::GetBlobFileReader(
|
||||
const ReadOptions& read_options, uint64_t blob_file_number,
|
||||
CacheHandleGuard<BlobFileReader>* blob_file_reader) {
|
||||
CacheHandleGuard<BlobFileReader>* blob_file_reader,
|
||||
bool allow_footer_skip_retry) {
|
||||
assert(blob_file_reader);
|
||||
assert(blob_file_reader->IsEmpty());
|
||||
|
||||
@@ -73,10 +75,22 @@ Status BlobFileCache::GetBlobFileReader(
|
||||
|
||||
{
|
||||
assert(file_options_);
|
||||
const Status s = BlobFileReader::Create(
|
||||
Status s = BlobFileReader::Create(
|
||||
*immutable_options_, read_options, *file_options_, column_family_id_,
|
||||
blob_file_read_hist_, blob_file_number, io_tracer_, &reader);
|
||||
blob_file_read_hist_, blob_file_number, io_tracer_,
|
||||
/*skip_footer_validation=*/false, &reader);
|
||||
if (!s.ok() && s.IsCorruption() && allow_footer_skip_retry) {
|
||||
reader.reset();
|
||||
s = BlobFileReader::Create(
|
||||
*immutable_options_, read_options, *file_options_, column_family_id_,
|
||||
blob_file_read_hist_, blob_file_number, io_tracer_,
|
||||
/*skip_footer_validation=*/true, &reader);
|
||||
}
|
||||
if (!s.ok()) {
|
||||
ROCKS_LOG_WARN(immutable_options_->logger,
|
||||
"BlobFileCache open failed for blob file %" PRIu64
|
||||
" in CF %u: %s",
|
||||
blob_file_number, column_family_id_, s.ToString().c_str());
|
||||
RecordTick(statistics, NO_FILE_ERRORS);
|
||||
return s;
|
||||
}
|
||||
@@ -99,12 +113,120 @@ Status BlobFileCache::GetBlobFileReader(
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobFileCache::OpenBlobFileReaderUncached(
|
||||
const ReadOptions& read_options, uint64_t blob_file_number,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader,
|
||||
bool allow_footer_skip_retry) {
|
||||
assert(blob_file_reader);
|
||||
assert(!*blob_file_reader);
|
||||
|
||||
Statistics* const statistics = immutable_options_->stats;
|
||||
RecordTick(statistics, NO_FILE_OPENS);
|
||||
|
||||
Status s = BlobFileReader::Create(
|
||||
*immutable_options_, read_options, *file_options_, column_family_id_,
|
||||
blob_file_read_hist_, blob_file_number, io_tracer_,
|
||||
/*skip_footer_validation=*/false, blob_file_reader);
|
||||
if (!s.ok() && s.IsCorruption() && allow_footer_skip_retry) {
|
||||
blob_file_reader->reset();
|
||||
s = BlobFileReader::Create(
|
||||
*immutable_options_, read_options, *file_options_, column_family_id_,
|
||||
blob_file_read_hist_, blob_file_number, io_tracer_,
|
||||
/*skip_footer_validation=*/true, blob_file_reader);
|
||||
}
|
||||
if (!s.ok()) {
|
||||
RecordTick(statistics, NO_FILE_ERRORS);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Status BlobFileCache::InsertBlobFileReader(
|
||||
uint64_t blob_file_number,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader,
|
||||
CacheHandleGuard<BlobFileReader>* cached_blob_file_reader) {
|
||||
assert(blob_file_reader);
|
||||
assert(*blob_file_reader);
|
||||
assert(cached_blob_file_reader);
|
||||
assert(cached_blob_file_reader->IsEmpty());
|
||||
|
||||
const Slice key = GetSliceForKey(&blob_file_number);
|
||||
// Serialize refreshes for the same blob file. The cache API does not expose
|
||||
// a conditional replace, so refresh is intentionally modeled as
|
||||
// Lookup/optional Erase/Insert under this per-key mutex.
|
||||
MutexLock lock(&mutex_.Get(key));
|
||||
|
||||
TypedHandle* handle = cache_.Lookup(key);
|
||||
if (handle) {
|
||||
*cached_blob_file_reader = cache_.Guard(handle);
|
||||
blob_file_reader->reset();
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
constexpr size_t charge = 1;
|
||||
Status s = cache_.Insert(key, blob_file_reader->get(), charge, &handle);
|
||||
if (!s.ok()) {
|
||||
RecordTick(immutable_options_->stats, NO_FILE_ERRORS);
|
||||
return s;
|
||||
}
|
||||
|
||||
// Ownership transferred to the cache.
|
||||
[[maybe_unused]] BlobFileReader* released_reader =
|
||||
blob_file_reader->release();
|
||||
*cached_blob_file_reader = cache_.Guard(handle);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobFileCache::RefreshBlobFileReader(
|
||||
uint64_t blob_file_number,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader,
|
||||
CacheHandleGuard<BlobFileReader>* cached_blob_file_reader) {
|
||||
assert(blob_file_reader);
|
||||
assert(*blob_file_reader);
|
||||
assert(cached_blob_file_reader);
|
||||
assert(cached_blob_file_reader->IsEmpty());
|
||||
|
||||
const Slice key = GetSliceForKey(&blob_file_number);
|
||||
MutexLock lock(&mutex_.Get(key));
|
||||
|
||||
TypedHandle* handle = cache_.Lookup(key);
|
||||
if (handle) {
|
||||
BlobFileReader* const cached_reader = cache_.Value(handle);
|
||||
assert(cached_reader != nullptr);
|
||||
|
||||
// Active direct-write blob files can grow between refresh attempts. Keep
|
||||
// whichever reader observed the larger on-disk size so an older refresh
|
||||
// cannot overwrite a newer one that another thread already installed.
|
||||
if (cached_reader->GetFileSize() >= (*blob_file_reader)->GetFileSize()) {
|
||||
*cached_blob_file_reader = cache_.Guard(handle);
|
||||
blob_file_reader->reset();
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
cache_.Release(handle);
|
||||
cache_.get()->Erase(key);
|
||||
}
|
||||
|
||||
constexpr size_t charge = 1;
|
||||
Status s = cache_.Insert(key, blob_file_reader->get(), charge, &handle);
|
||||
if (!s.ok()) {
|
||||
RecordTick(immutable_options_->stats, NO_FILE_ERRORS);
|
||||
return s;
|
||||
}
|
||||
|
||||
// Ownership transferred to the cache.
|
||||
[[maybe_unused]] BlobFileReader* released_reader =
|
||||
blob_file_reader->release();
|
||||
*cached_blob_file_reader = cache_.Guard(handle);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
void BlobFileCache::Evict(uint64_t blob_file_number) {
|
||||
// NOTE: sharing same Cache with table_cache
|
||||
const Slice key = GetSliceForKey(&blob_file_number);
|
||||
|
||||
assert(cache_);
|
||||
|
||||
MutexLock lock(&mutex_.Get(key));
|
||||
cache_.get()->Erase(key);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,9 +32,35 @@ class BlobFileCache {
|
||||
BlobFileCache(const BlobFileCache&) = delete;
|
||||
BlobFileCache& operator=(const BlobFileCache&) = delete;
|
||||
|
||||
// Returns a cached reader for `blob_file_number`, opening and caching it on
|
||||
// miss. If `allow_footer_skip_retry` is true, a footer-validation corruption
|
||||
// retries once without requiring a footer.
|
||||
Status GetBlobFileReader(const ReadOptions& read_options,
|
||||
uint64_t blob_file_number,
|
||||
CacheHandleGuard<BlobFileReader>* blob_file_reader);
|
||||
CacheHandleGuard<BlobFileReader>* blob_file_reader,
|
||||
bool allow_footer_skip_retry = false);
|
||||
|
||||
// Opens a blob file reader without inserting it into the cache.
|
||||
Status OpenBlobFileReaderUncached(
|
||||
const ReadOptions& read_options, uint64_t blob_file_number,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader,
|
||||
bool allow_footer_skip_retry = false);
|
||||
|
||||
// Inserts a freshly opened uncached reader unless another thread already
|
||||
// cached the same blob file.
|
||||
Status InsertBlobFileReader(
|
||||
uint64_t blob_file_number,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader,
|
||||
CacheHandleGuard<BlobFileReader>* cached_blob_file_reader);
|
||||
|
||||
// Installs a refresh-opened reader into the cache. If another thread has
|
||||
// already cached a reader opened on at least as large a file size, keep that
|
||||
// reader instead so a racing refresh cannot reintroduce an older active-file
|
||||
// view after the blob file grows.
|
||||
Status RefreshBlobFileReader(
|
||||
uint64_t blob_file_number,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader,
|
||||
CacheHandleGuard<BlobFileReader>* cached_blob_file_reader);
|
||||
|
||||
// Called when a blob file is obsolete to ensure it is removed from the cache
|
||||
// to avoid effectively leaking the open file and assicated memory
|
||||
|
||||
@@ -192,6 +192,102 @@ TEST_F(BlobFileCacheTest, GetBlobFileReader_Race) {
|
||||
SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
}
|
||||
|
||||
TEST_F(BlobFileCacheTest, RefreshBlobFileReaderPrefersLargestObservedFileSize) {
|
||||
Options options;
|
||||
options.env = mock_env_.get();
|
||||
options.statistics = CreateDBStatistics();
|
||||
options.cf_paths.emplace_back(
|
||||
test::PerThreadDBPath(
|
||||
mock_env_.get(),
|
||||
"BlobFileCacheTest_"
|
||||
"RefreshBlobFileReaderPrefersLargestObservedFileSize"),
|
||||
0);
|
||||
options.enable_blob_files = true;
|
||||
|
||||
constexpr uint32_t column_family_id = 1;
|
||||
ImmutableOptions immutable_options(options);
|
||||
constexpr uint64_t blob_file_number = 123;
|
||||
|
||||
const std::string blob_file_path =
|
||||
BlobFileName(immutable_options.cf_paths.front().path, blob_file_number);
|
||||
|
||||
std::unique_ptr<FSWritableFile> file;
|
||||
ASSERT_OK(NewWritableFile(immutable_options.fs.get(), blob_file_path, &file,
|
||||
FileOptions()));
|
||||
|
||||
std::unique_ptr<WritableFileWriter> file_writer(new WritableFileWriter(
|
||||
std::move(file), blob_file_path, FileOptions(), immutable_options.clock));
|
||||
|
||||
constexpr Statistics* statistics = nullptr;
|
||||
constexpr bool use_fsync = false;
|
||||
constexpr bool do_flush = false;
|
||||
BlobLogWriter blob_log_writer(std::move(file_writer), immutable_options.clock,
|
||||
statistics, blob_file_number, use_fsync,
|
||||
do_flush);
|
||||
|
||||
constexpr bool has_ttl = false;
|
||||
constexpr ExpirationRange expiration_range;
|
||||
BlobLogHeader header(column_family_id, kNoCompression, has_ttl,
|
||||
expiration_range);
|
||||
ASSERT_OK(blob_log_writer.WriteHeader(WriteOptions(), header));
|
||||
|
||||
uint64_t key_offset = 0;
|
||||
uint64_t blob_offset = 0;
|
||||
ASSERT_OK(blob_log_writer.AddRecord(WriteOptions(), "key0", "blob0",
|
||||
&key_offset, &blob_offset));
|
||||
ASSERT_OK(blob_log_writer.Sync(WriteOptions()));
|
||||
|
||||
constexpr size_t capacity = 10;
|
||||
std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity);
|
||||
|
||||
FileOptions file_options;
|
||||
constexpr HistogramImpl* blob_file_read_hist = nullptr;
|
||||
BlobFileCache blob_file_cache(backing_cache.get(), &immutable_options,
|
||||
&file_options, column_family_id,
|
||||
blob_file_read_hist, nullptr /*IOTracer*/);
|
||||
|
||||
const ReadOptions read_options;
|
||||
std::unique_ptr<BlobFileReader> stale_reader;
|
||||
ASSERT_OK(blob_file_cache.OpenBlobFileReaderUncached(
|
||||
read_options, blob_file_number, &stale_reader,
|
||||
/*allow_footer_skip_retry=*/true));
|
||||
|
||||
std::unique_ptr<BlobFileReader> initial_cached_reader;
|
||||
ASSERT_OK(blob_file_cache.OpenBlobFileReaderUncached(
|
||||
read_options, blob_file_number, &initial_cached_reader,
|
||||
/*allow_footer_skip_retry=*/true));
|
||||
|
||||
CacheHandleGuard<BlobFileReader> cached_reader;
|
||||
ASSERT_OK(blob_file_cache.RefreshBlobFileReader(
|
||||
blob_file_number, &initial_cached_reader, &cached_reader));
|
||||
ASSERT_NE(cached_reader.GetValue(), nullptr);
|
||||
const uint64_t initial_file_size = cached_reader.GetValue()->GetFileSize();
|
||||
|
||||
ASSERT_OK(blob_log_writer.AddRecord(WriteOptions(), "key1", "blob1",
|
||||
&key_offset, &blob_offset));
|
||||
ASSERT_OK(blob_log_writer.Sync(WriteOptions()));
|
||||
|
||||
std::unique_ptr<BlobFileReader> fresh_reader;
|
||||
ASSERT_OK(blob_file_cache.OpenBlobFileReaderUncached(
|
||||
read_options, blob_file_number, &fresh_reader,
|
||||
/*allow_footer_skip_retry=*/true));
|
||||
ASSERT_GT(fresh_reader->GetFileSize(), initial_file_size);
|
||||
|
||||
CacheHandleGuard<BlobFileReader> refreshed_reader;
|
||||
ASSERT_OK(blob_file_cache.RefreshBlobFileReader(
|
||||
blob_file_number, &fresh_reader, &refreshed_reader));
|
||||
ASSERT_NE(refreshed_reader.GetValue(), nullptr);
|
||||
ASSERT_GT(refreshed_reader.GetValue()->GetFileSize(), initial_file_size);
|
||||
BlobFileReader* const largest_reader = refreshed_reader.GetValue();
|
||||
|
||||
CacheHandleGuard<BlobFileReader> preserved_reader;
|
||||
ASSERT_OK(blob_file_cache.RefreshBlobFileReader(
|
||||
blob_file_number, &stale_reader, &preserved_reader));
|
||||
ASSERT_NE(preserved_reader.GetValue(), nullptr);
|
||||
ASSERT_EQ(preserved_reader.GetValue(), largest_reader);
|
||||
ASSERT_EQ(stale_reader.get(), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(BlobFileCacheTest, GetBlobFileReader_IOError) {
|
||||
Options options;
|
||||
options.env = mock_env_.get();
|
||||
|
||||
@@ -0,0 +1,840 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cinttypes>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "cache/cache_key.h"
|
||||
#include "cache/typed_cache.h"
|
||||
#include "db/blob/blob_contents.h"
|
||||
#include "db/blob/blob_file_cache.h"
|
||||
#include "db/blob/blob_file_completion_callback.h"
|
||||
#include "db/blob/blob_file_reader.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/blob/blob_log_writer.h"
|
||||
#include "db/version_set.h"
|
||||
#include "file/filename.h"
|
||||
#include "file/read_write_util.h"
|
||||
#include "file/writable_file_writer.h"
|
||||
#include "logging/logging.h"
|
||||
#include "util/aligned_buffer.h"
|
||||
#include "util/compression.h"
|
||||
#include "util/mutexlock.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
namespace {
|
||||
|
||||
class RoundRobinBlobFilePartitionStrategy : public BlobFilePartitionStrategy {
|
||||
public:
|
||||
using BlobFilePartitionStrategy::SelectPartition;
|
||||
|
||||
const char* Name() const override {
|
||||
return "RoundRobinBlobFilePartitionStrategy";
|
||||
}
|
||||
|
||||
uint32_t SelectPartition(uint32_t num_partitions,
|
||||
uint32_t /*column_family_id*/, const Slice& /*key*/,
|
||||
const Slice& /*value*/) override {
|
||||
assert(num_partitions > 0);
|
||||
return static_cast<uint32_t>(
|
||||
next_partition_.fetch_add(1, std::memory_order_relaxed) %
|
||||
static_cast<uint64_t>(num_partitions));
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic<uint64_t> next_partition_{0};
|
||||
};
|
||||
|
||||
struct DirectWriteCompressionState {
|
||||
// `working_area` must be released before its owning compressor.
|
||||
std::unique_ptr<Compressor> compressor;
|
||||
Compressor::ManagedWorkingArea working_area;
|
||||
};
|
||||
|
||||
DirectWriteCompressionState& GetDirectWriteCompressionState(
|
||||
CompressionType compression) {
|
||||
assert(compression <= kLastBuiltinCompression);
|
||||
|
||||
static thread_local std::array<DirectWriteCompressionState,
|
||||
static_cast<size_t>(kLastBuiltinCompression) +
|
||||
1>
|
||||
compression_states;
|
||||
|
||||
auto& compression_state =
|
||||
compression_states[static_cast<size_t>(compression)];
|
||||
if (compression != kNoCompression &&
|
||||
compression_state.compressor == nullptr) {
|
||||
// BDW v1 mirrors BlobFileBuilder by using the built-in compressor with the
|
||||
// default CompressionOptions only. Cache it per thread so repeated writes
|
||||
// do not allocate a new compressor and working area every time.
|
||||
compression_state.compressor =
|
||||
GetBuiltinV2CompressionManager()->GetCompressor(CompressionOptions{},
|
||||
compression);
|
||||
if (compression_state.compressor != nullptr) {
|
||||
compression_state.working_area =
|
||||
compression_state.compressor->ObtainWorkingArea();
|
||||
}
|
||||
}
|
||||
|
||||
return compression_state;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BlobFilePartitionManager::BlobFilePartitionManager(
|
||||
uint32_t num_partitions,
|
||||
std::shared_ptr<BlobFilePartitionStrategy> strategy,
|
||||
FileNumberAllocator file_number_allocator, FileSystem* fs,
|
||||
SystemClock* clock, Statistics* statistics, const FileOptions& file_options,
|
||||
std::string db_path, std::string column_family_name,
|
||||
uint64_t blob_file_size, bool use_fsync, BlobFileCache* blob_file_cache,
|
||||
BlobFileCompletionCallback* blob_callback,
|
||||
const std::vector<std::shared_ptr<EventListener>>& listeners,
|
||||
FileChecksumGenFactory* file_checksum_gen_factory,
|
||||
const FileTypeSet& checksum_handoff_file_types,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, std::string db_id,
|
||||
std::string db_session_id, Logger* info_log)
|
||||
: num_partitions_(num_partitions == 0 ? 1 : num_partitions),
|
||||
strategy_(strategy != nullptr
|
||||
? std::move(strategy)
|
||||
: std::make_shared<RoundRobinBlobFilePartitionStrategy>()),
|
||||
file_number_allocator_(std::move(file_number_allocator)),
|
||||
fs_(fs),
|
||||
clock_(clock),
|
||||
statistics_(statistics),
|
||||
file_options_(file_options),
|
||||
db_path_(std::move(db_path)),
|
||||
column_family_name_(std::move(column_family_name)),
|
||||
blob_file_size_(blob_file_size),
|
||||
use_fsync_(use_fsync),
|
||||
blob_file_cache_(blob_file_cache),
|
||||
blob_callback_(blob_callback),
|
||||
listeners_(listeners),
|
||||
file_checksum_gen_factory_(file_checksum_gen_factory),
|
||||
checksum_handoff_file_types_(checksum_handoff_file_types),
|
||||
io_tracer_(io_tracer),
|
||||
db_id_(std::move(db_id)),
|
||||
db_session_id_(std::move(db_session_id)),
|
||||
info_log_(info_log) {
|
||||
partitions_.reserve(num_partitions_);
|
||||
for (uint32_t i = 0; i < num_partitions_; ++i) {
|
||||
partitions_.emplace_back(new Partition());
|
||||
}
|
||||
}
|
||||
|
||||
BlobFilePartitionManager::~BlobFilePartitionManager() {
|
||||
if (blob_file_cache_ != nullptr) {
|
||||
std::vector<uint64_t> tracked_file_numbers;
|
||||
{
|
||||
ReadLock lock(&file_partition_mutex_);
|
||||
tracked_file_numbers.reserve(file_to_partition_.size());
|
||||
for (const auto& entry : file_to_partition_) {
|
||||
tracked_file_numbers.push_back(entry.first);
|
||||
}
|
||||
}
|
||||
|
||||
for (uint64_t file_number : tracked_file_numbers) {
|
||||
// Dropping the CF or closing the DB can destroy the manager while active
|
||||
// direct-write readers are still cached. Evict them before the CF-owned
|
||||
// blob cache goes away so Close() does not retain obsolete footer-less
|
||||
// readers for files that are no longer live.
|
||||
blob_file_cache_->Evict(file_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::ResetPartitionState(Partition* partition) {
|
||||
partition->writer.reset();
|
||||
partition->file_number = 0;
|
||||
partition->file_size = 0;
|
||||
partition->blob_count = 0;
|
||||
partition->total_blob_bytes = 0;
|
||||
partition->garbage_blob_count = 0;
|
||||
partition->garbage_blob_bytes = 0;
|
||||
partition->column_family_id = 0;
|
||||
partition->compression = kNoCompression;
|
||||
partition->sync_required = false;
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::AddFilePartitionMapping(uint64_t file_number,
|
||||
uint32_t partition_idx) {
|
||||
WriteLock lock(&file_partition_mutex_);
|
||||
file_to_partition_[file_number] = partition_idx;
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::RemoveFilePartitionMapping(
|
||||
uint64_t file_number) {
|
||||
WriteLock lock(&file_partition_mutex_);
|
||||
file_to_partition_.erase(file_number);
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::OpenNewBlobFile(Partition* partition,
|
||||
uint32_t column_family_id,
|
||||
CompressionType compression,
|
||||
uint32_t partition_idx) {
|
||||
assert(partition != nullptr);
|
||||
assert(!partition->writer);
|
||||
|
||||
const uint64_t blob_file_number = file_number_allocator_();
|
||||
const std::string blob_file_path = BlobFileName(db_path_, blob_file_number);
|
||||
|
||||
AddFilePartitionMapping(blob_file_number, partition_idx);
|
||||
|
||||
if (blob_callback_ != nullptr) {
|
||||
blob_callback_->OnBlobFileCreationStarted(
|
||||
blob_file_path, column_family_name_,
|
||||
/*job_id=*/0, BlobFileCreationReason::kFlush);
|
||||
}
|
||||
|
||||
std::unique_ptr<FSWritableFile> file;
|
||||
Status s = NewWritableFile(fs_, blob_file_path, &file, file_options_);
|
||||
if (!s.ok()) {
|
||||
RemoveFilePartitionMapping(blob_file_number);
|
||||
return s;
|
||||
}
|
||||
|
||||
const bool perform_data_verification =
|
||||
checksum_handoff_file_types_.Contains(FileType::kBlobFile);
|
||||
auto file_writer = std::make_unique<WritableFileWriter>(
|
||||
std::move(file), blob_file_path, file_options_, clock_, io_tracer_,
|
||||
statistics_, Histograms::BLOB_DB_BLOB_FILE_WRITE_MICROS, listeners_,
|
||||
file_checksum_gen_factory_, perform_data_verification);
|
||||
|
||||
// This only drains WritableFileWriter's buffered bytes so readers can see
|
||||
// each appended record promptly. Durability still comes from SyncAllOpenFiles
|
||||
// or AppendFooter(), both of which call Sync().
|
||||
constexpr bool kDoFlushEachRecord = true;
|
||||
auto blob_log_writer = std::make_unique<BlobLogWriter>(
|
||||
std::move(file_writer), clock_, statistics_, blob_file_number, use_fsync_,
|
||||
kDoFlushEachRecord);
|
||||
|
||||
constexpr bool has_ttl = false;
|
||||
constexpr ExpirationRange expiration_range{};
|
||||
BlobLogHeader header(column_family_id, compression, has_ttl,
|
||||
expiration_range);
|
||||
s = blob_log_writer->WriteHeader(WriteOptions(), header);
|
||||
if (!s.ok()) {
|
||||
RemoveFilePartitionMapping(blob_file_number);
|
||||
return s;
|
||||
}
|
||||
|
||||
partition->writer = std::move(blob_log_writer);
|
||||
partition->file_number = blob_file_number;
|
||||
partition->file_size = BlobLogHeader::kSize;
|
||||
partition->blob_count = 0;
|
||||
partition->total_blob_bytes = 0;
|
||||
partition->garbage_blob_count = 0;
|
||||
partition->garbage_blob_bytes = 0;
|
||||
partition->column_family_id = column_family_id;
|
||||
partition->compression = compression;
|
||||
partition->sync_required = false;
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::SealActiveBlobFile(
|
||||
const WriteOptions& write_options, Partition* partition,
|
||||
SealedFile* sealed_file) {
|
||||
assert(partition != nullptr);
|
||||
assert(partition->writer);
|
||||
assert(sealed_file != nullptr);
|
||||
|
||||
const uint64_t file_number = partition->file_number;
|
||||
Status s =
|
||||
FinalizeBlobFile(write_options, partition->writer.get(), file_number,
|
||||
partition->blob_count, partition->total_blob_bytes,
|
||||
&sealed_file->addition);
|
||||
if (!s.ok()) {
|
||||
RemoveFilePartitionMapping(file_number);
|
||||
ResetPartitionState(partition);
|
||||
return s;
|
||||
}
|
||||
|
||||
sealed_file->garbage_blob_count = partition->garbage_blob_count;
|
||||
sealed_file->garbage_blob_bytes = partition->garbage_blob_bytes;
|
||||
ResetPartitionState(partition);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::SealDeferredFile(
|
||||
const WriteOptions& write_options, DeferredFile* deferred,
|
||||
SealedFile* sealed_file) {
|
||||
assert(deferred != nullptr);
|
||||
assert(deferred->writer);
|
||||
assert(sealed_file != nullptr);
|
||||
|
||||
Status s = FinalizeBlobFile(
|
||||
write_options, deferred->writer.get(), deferred->file_number,
|
||||
deferred->blob_count, deferred->total_blob_bytes, &sealed_file->addition);
|
||||
if (!s.ok()) {
|
||||
RemoveFilePartitionMapping(deferred->file_number);
|
||||
return s;
|
||||
}
|
||||
|
||||
sealed_file->garbage_blob_count = deferred->garbage_blob_count;
|
||||
sealed_file->garbage_blob_bytes = deferred->garbage_blob_bytes;
|
||||
deferred->writer.reset();
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::FinalizeBlobFile(
|
||||
const WriteOptions& write_options, BlobLogWriter* writer,
|
||||
uint64_t file_number, uint64_t blob_count, uint64_t total_blob_bytes,
|
||||
BlobFileAddition* addition) {
|
||||
assert(writer != nullptr);
|
||||
assert(addition != nullptr);
|
||||
|
||||
BlobLogFooter footer;
|
||||
footer.blob_count = blob_count;
|
||||
|
||||
std::string checksum_method;
|
||||
std::string checksum_value;
|
||||
Status s = writer->AppendFooter(write_options, footer, &checksum_method,
|
||||
&checksum_value);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (blob_callback_ != nullptr) {
|
||||
const std::string blob_file_path = BlobFileName(db_path_, file_number);
|
||||
s = blob_callback_->OnBlobFileCompleted(
|
||||
blob_file_path, column_family_name_, /*job_id=*/0, file_number,
|
||||
BlobFileCreationReason::kFlush, s, checksum_value, checksum_method,
|
||||
blob_count, total_blob_bytes);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
*addition =
|
||||
BlobFileAddition(file_number, blob_count, total_blob_bytes,
|
||||
std::move(checksum_method), std::move(checksum_value));
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::AddSealedFileGarbage(
|
||||
const SealedFile& sealed_file, std::vector<BlobFileGarbage>* garbages) {
|
||||
assert(garbages != nullptr);
|
||||
|
||||
if (sealed_file.garbage_blob_count == 0) {
|
||||
assert(sealed_file.garbage_blob_bytes == 0);
|
||||
return;
|
||||
}
|
||||
|
||||
garbages->emplace_back(sealed_file.addition.GetBlobFileNumber(),
|
||||
sealed_file.garbage_blob_count,
|
||||
sealed_file.garbage_blob_bytes);
|
||||
}
|
||||
|
||||
bool BlobFilePartitionManager::MarkPartitionGarbage(Partition* partition,
|
||||
uint64_t file_number,
|
||||
uint64_t blob_count,
|
||||
uint64_t blob_bytes) {
|
||||
assert(partition != nullptr);
|
||||
|
||||
if (!partition->writer || partition->file_number != file_number) {
|
||||
return false;
|
||||
}
|
||||
|
||||
partition->garbage_blob_count += blob_count;
|
||||
partition->garbage_blob_bytes += blob_bytes;
|
||||
assert(partition->garbage_blob_count <= partition->blob_count);
|
||||
assert(partition->garbage_blob_bytes <= partition->total_blob_bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlobFilePartitionManager::MarkDeferredFileGarbage(DeferredFile* deferred,
|
||||
uint64_t file_number,
|
||||
uint64_t blob_count,
|
||||
uint64_t blob_bytes) {
|
||||
assert(deferred != nullptr);
|
||||
|
||||
if (deferred->file_number != file_number) {
|
||||
return false;
|
||||
}
|
||||
|
||||
deferred->garbage_blob_count += blob_count;
|
||||
deferred->garbage_blob_bytes += blob_bytes;
|
||||
assert(deferred->garbage_blob_count <= deferred->blob_count);
|
||||
assert(deferred->garbage_blob_bytes <= deferred->total_blob_bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlobFilePartitionManager::MarkSealedFileGarbage(
|
||||
std::vector<SealedFile>* sealed_files, uint64_t file_number,
|
||||
uint64_t blob_count, uint64_t blob_bytes) {
|
||||
assert(sealed_files != nullptr);
|
||||
|
||||
for (auto& sealed_file : *sealed_files) {
|
||||
if (sealed_file.addition.GetBlobFileNumber() != file_number) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sealed_file.garbage_blob_count += blob_count;
|
||||
sealed_file.garbage_blob_bytes += blob_bytes;
|
||||
assert(sealed_file.garbage_blob_count <=
|
||||
sealed_file.addition.GetTotalBlobCount());
|
||||
assert(sealed_file.garbage_blob_bytes <=
|
||||
sealed_file.addition.GetTotalBlobBytes());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::MaybePrepopulateBlobCache(
|
||||
const BlobDirectWriteSettings* settings, const Slice& original_value,
|
||||
uint64_t blob_file_number, uint64_t blob_offset) {
|
||||
if (settings == nullptr || settings->blob_cache == nullptr ||
|
||||
settings->prepopulate_blob_cache != PrepopulateBlobCache::kFlushOnly) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
FullTypedCacheInterface<BlobContents, BlobContentsCreator> blob_cache{
|
||||
settings->blob_cache};
|
||||
const OffsetableCacheKey base_cache_key(db_id_, db_session_id_,
|
||||
blob_file_number);
|
||||
const CacheKey cache_key = base_cache_key.WithOffset(blob_offset);
|
||||
return blob_cache.InsertSaved(cache_key.AsSlice(), original_value, nullptr,
|
||||
Cache::Priority::BOTTOM,
|
||||
CacheTier::kVolatileTier);
|
||||
}
|
||||
|
||||
uint32_t BlobFilePartitionManager::SelectWideColumnPartition(
|
||||
uint32_t column_family_id, const Slice& key,
|
||||
const WideColumns& columns) const {
|
||||
return strategy_->SelectPartition(num_partitions_, column_family_id, key,
|
||||
columns) %
|
||||
num_partitions_;
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::WriteBlob(
|
||||
const WriteOptions& write_options, uint32_t column_family_id,
|
||||
CompressionType compression, const Slice& key, const Slice& value,
|
||||
uint64_t* blob_file_number, uint64_t* blob_offset, uint64_t* blob_size,
|
||||
const BlobDirectWriteSettings* settings, const uint32_t* partition_idx) {
|
||||
assert(blob_file_number != nullptr);
|
||||
assert(blob_offset != nullptr);
|
||||
assert(blob_size != nullptr);
|
||||
|
||||
// Do compression before taking the partition mutex so large-value CPU work
|
||||
// does not serialize writers. A concurrent file rollover can still cause
|
||||
// this compressed buffer to be discarded below, which is acceptable on this
|
||||
// non-hot failure/configuration-change path.
|
||||
GrowableBuffer compressed_value;
|
||||
Slice write_value = value;
|
||||
if (compression != kNoCompression) {
|
||||
auto& compression_state = GetDirectWriteCompressionState(compression);
|
||||
if (compression_state.compressor == nullptr) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write compression type not supported");
|
||||
}
|
||||
Status s = LegacyForceBuiltinCompression(*compression_state.compressor,
|
||||
&compression_state.working_area,
|
||||
value, &compressed_value);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
write_value = Slice(compressed_value);
|
||||
}
|
||||
|
||||
// Partition selection is based on the logical write inputs. In particular,
|
||||
// strategies that inspect value contents or size see the original
|
||||
// uncompressed value rather than `write_value`. The modulo here is
|
||||
// intentional so custom strategies can return arbitrary hashed or sentinel
|
||||
// values without violating the partition bounds.
|
||||
const uint32_t selected_partition_idx =
|
||||
partition_idx != nullptr
|
||||
? (*partition_idx % num_partitions_)
|
||||
: (strategy_->SelectPartition(num_partitions_, column_family_id, key,
|
||||
value) %
|
||||
num_partitions_);
|
||||
|
||||
{
|
||||
MutexLock lock(&mutex_);
|
||||
Partition* partition = partitions_[selected_partition_idx].get();
|
||||
|
||||
auto seal_current_file = [&]() -> Status {
|
||||
if (!partition->writer) {
|
||||
return Status::OK();
|
||||
}
|
||||
SealedFile sealed_file;
|
||||
Status s = SealActiveBlobFile(write_options, partition, &sealed_file);
|
||||
if (s.ok()) {
|
||||
current_generation_sealed_files_.push_back(std::move(sealed_file));
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
const uint64_t record_size =
|
||||
BlobLogRecord::kHeaderSize + key.size() + write_value.size();
|
||||
const uint64_t future_file_size =
|
||||
partition->file_size + record_size + BlobLogFooter::kSize;
|
||||
|
||||
if (partition->writer &&
|
||||
(partition->column_family_id != column_family_id ||
|
||||
partition->compression != compression ||
|
||||
(partition->blob_count > 0 && future_file_size > blob_file_size_))) {
|
||||
Status s = seal_current_file();
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
if (!partition->writer) {
|
||||
Status s = OpenNewBlobFile(partition, column_family_id, compression,
|
||||
selected_partition_idx);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t key_offset = 0;
|
||||
Status s = partition->writer->AddRecord(write_options, key, write_value,
|
||||
&key_offset, blob_offset);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
partition->sync_required = true;
|
||||
partition->blob_count += 1;
|
||||
partition->total_blob_bytes += record_size;
|
||||
partition->file_size = BlobLogHeader::kSize + partition->total_blob_bytes;
|
||||
|
||||
*blob_file_number = partition->file_number;
|
||||
*blob_size = write_value.size();
|
||||
}
|
||||
|
||||
Status prepopulate_s = MaybePrepopulateBlobCache(
|
||||
settings, value, *blob_file_number, *blob_offset);
|
||||
if (!prepopulate_s.ok() && info_log_ != nullptr) {
|
||||
ROCKS_LOG_WARN(info_log_,
|
||||
"Failed to pre-populate direct-write blob cache entry: %s",
|
||||
prepopulate_s.ToString().c_str());
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::RotateCurrentGeneration() {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
GenerationBatch batch;
|
||||
batch.sealed_files = std::move(current_generation_sealed_files_);
|
||||
current_generation_sealed_files_.clear();
|
||||
|
||||
for (auto& partition : partitions_) {
|
||||
if (!partition->writer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DeferredFile deferred;
|
||||
deferred.writer = std::move(partition->writer);
|
||||
deferred.file_number = partition->file_number;
|
||||
deferred.blob_count = partition->blob_count;
|
||||
deferred.total_blob_bytes = partition->total_blob_bytes;
|
||||
deferred.garbage_blob_count = partition->garbage_blob_count;
|
||||
deferred.garbage_blob_bytes = partition->garbage_blob_bytes;
|
||||
ResetPartitionState(partition.get());
|
||||
batch.deferred_files.emplace_back(std::move(deferred));
|
||||
}
|
||||
|
||||
pending_generations_.emplace_back(std::move(batch));
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::PrepareFlushAdditions(
|
||||
const WriteOptions& write_options, size_t num_generations,
|
||||
std::vector<BlobFileAddition>* additions,
|
||||
std::vector<BlobFileGarbage>* garbages,
|
||||
std::vector<std::vector<uint64_t>>* generation_blob_file_numbers) {
|
||||
assert(additions != nullptr);
|
||||
assert(garbages != nullptr);
|
||||
additions->clear();
|
||||
garbages->clear();
|
||||
if (generation_blob_file_numbers != nullptr) {
|
||||
generation_blob_file_numbers->clear();
|
||||
}
|
||||
|
||||
MutexLock lock(&mutex_);
|
||||
if (num_generations > pending_generations_.size()) {
|
||||
return Status::Corruption(
|
||||
"Missing blob direct write generation metadata for flush");
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < num_generations; ++i) {
|
||||
GenerationBatch& batch = pending_generations_[i];
|
||||
while (!batch.deferred_files.empty()) {
|
||||
DeferredFile deferred = std::move(batch.deferred_files.front());
|
||||
batch.deferred_files.pop_front();
|
||||
|
||||
SealedFile sealed_file;
|
||||
Status s = SealDeferredFile(write_options, &deferred, &sealed_file);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
// Keep each successfully sealed file attached to the generation
|
||||
// immediately. If a later seal or the flush job itself fails, retry must
|
||||
// reuse these exact on-disk files instead of finalizing replacements.
|
||||
batch.sealed_files.push_back(std::move(sealed_file));
|
||||
}
|
||||
|
||||
std::vector<uint64_t>* generation_file_numbers = nullptr;
|
||||
if (generation_blob_file_numbers != nullptr) {
|
||||
generation_blob_file_numbers->emplace_back();
|
||||
generation_file_numbers = &generation_blob_file_numbers->back();
|
||||
generation_file_numbers->reserve(batch.sealed_files.size());
|
||||
}
|
||||
|
||||
for (const auto& sealed_file : batch.sealed_files) {
|
||||
additions->push_back(sealed_file.addition);
|
||||
AddSealedFileGarbage(sealed_file, garbages);
|
||||
if (generation_file_numbers != nullptr) {
|
||||
generation_file_numbers->push_back(
|
||||
sealed_file.addition.GetBlobFileNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::MarkBlobWriteAsGarbage(uint64_t file_number,
|
||||
uint64_t blob_count,
|
||||
uint64_t blob_bytes) {
|
||||
if (blob_count == 0) {
|
||||
assert(blob_bytes == 0);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
MutexLock lock(&mutex_);
|
||||
for (auto& partition : partitions_) {
|
||||
if (MarkPartitionGarbage(partition.get(), file_number, blob_count,
|
||||
blob_bytes)) {
|
||||
return Status::OK();
|
||||
}
|
||||
}
|
||||
|
||||
if (MarkSealedFileGarbage(¤t_generation_sealed_files_, file_number,
|
||||
blob_count, blob_bytes)) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
for (auto& batch : pending_generations_) {
|
||||
for (auto& deferred : batch.deferred_files) {
|
||||
if (MarkDeferredFileGarbage(&deferred, file_number, blob_count,
|
||||
blob_bytes)) {
|
||||
return Status::OK();
|
||||
}
|
||||
}
|
||||
if (MarkSealedFileGarbage(&batch.sealed_files, file_number, blob_count,
|
||||
blob_bytes)) {
|
||||
return Status::OK();
|
||||
}
|
||||
}
|
||||
|
||||
const std::string message =
|
||||
"Could not match failed blob direct-write rollback for file #" +
|
||||
std::to_string(file_number);
|
||||
if (info_log_ != nullptr) {
|
||||
ROCKS_LOG_ERROR(info_log_, "%s", message.c_str());
|
||||
}
|
||||
return Status::Corruption(message);
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::CommitPreparedGenerations(
|
||||
size_t num_generations) {
|
||||
MutexLock lock(&mutex_);
|
||||
while (num_generations-- > 0 && !pending_generations_.empty()) {
|
||||
pending_generations_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::SyncAllOpenFiles(
|
||||
const WriteOptions& write_options) {
|
||||
MutexLock lock(&mutex_);
|
||||
for (const auto& partition : partitions_) {
|
||||
if (!partition->writer || !partition->sync_required) {
|
||||
continue;
|
||||
}
|
||||
Status s = partition->writer->Sync(write_options);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
partition->sync_required = false;
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::GetActiveBlobFileNumbers(
|
||||
UnorderedSet<uint64_t>* file_numbers) const {
|
||||
assert(file_numbers != nullptr);
|
||||
ReadLock lock(&file_partition_mutex_);
|
||||
for (const auto& entry : file_to_partition_) {
|
||||
file_numbers->insert(entry.first);
|
||||
}
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::GetProtectedBlobFileNumbers(
|
||||
UnorderedSet<uint64_t>* file_numbers) const {
|
||||
assert(file_numbers != nullptr);
|
||||
ReadLock lock(&file_partition_mutex_);
|
||||
for (const auto& entry : protected_blob_file_refs_) {
|
||||
file_numbers->insert(entry.first);
|
||||
}
|
||||
}
|
||||
|
||||
bool BlobFilePartitionManager::IsTrackedBlobFileNumber(
|
||||
uint64_t file_number) const {
|
||||
ReadLock lock(&file_partition_mutex_);
|
||||
return file_to_partition_.find(file_number) != file_to_partition_.end() ||
|
||||
protected_blob_file_refs_.find(file_number) !=
|
||||
protected_blob_file_refs_.end();
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::ProtectSealedBlobFileNumbers(
|
||||
const std::vector<uint64_t>& file_numbers) {
|
||||
if (file_numbers.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
WriteLock lock(&file_partition_mutex_);
|
||||
for (uint64_t file_number : file_numbers) {
|
||||
++protected_blob_file_refs_[file_number];
|
||||
}
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::UnprotectSealedBlobFileNumbers(
|
||||
const std::vector<uint64_t>& file_numbers) {
|
||||
if (file_numbers.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
WriteLock lock(&file_partition_mutex_);
|
||||
for (uint64_t file_number : file_numbers) {
|
||||
auto it = protected_blob_file_refs_.find(file_number);
|
||||
if (it == protected_blob_file_refs_.end()) {
|
||||
if (info_log_ != nullptr) {
|
||||
ROCKS_LOG_ERROR(info_log_,
|
||||
"Memtable blob protection underflow for file #%" PRIu64,
|
||||
file_number);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
assert(it->second > 0);
|
||||
--it->second;
|
||||
if (it->second == 0) {
|
||||
protected_blob_file_refs_.erase(it);
|
||||
if (blob_file_cache_ != nullptr) {
|
||||
// Once the last memtable-backed reference goes away, any cached reader
|
||||
// for this file is either obsolete or can be reopened through the
|
||||
// manifest-visible path. Evict it here so delayed protection does not
|
||||
// leave an obsolete blob reader behind until DB close.
|
||||
blob_file_cache_->Evict(file_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BlobFilePartitionManager::RemoveFilePartitionMappings(
|
||||
const std::vector<uint64_t>& file_numbers) {
|
||||
if (file_numbers.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
WriteLock lock(&file_partition_mutex_);
|
||||
for (uint64_t file_number : file_numbers) {
|
||||
file_to_partition_.erase(file_number);
|
||||
if (blob_file_cache_ != nullptr) {
|
||||
// In-flight direct-write reads may have cached a footer-less reader for
|
||||
// this file before it was sealed. Drop it now so future manifest-visible
|
||||
// reads reopen against the finalized on-disk size and footer state.
|
||||
blob_file_cache_->Evict(file_number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Status BlobFilePartitionManager::ResolveBlobDirectWriteIndex(
|
||||
const ReadOptions& read_options, const Slice& user_key,
|
||||
const BlobIndex& blob_idx, const Version* version,
|
||||
BlobFileCache* blob_file_cache, FilePrefetchBuffer* prefetch_buffer,
|
||||
PinnableSlice* blob_value, uint64_t* bytes_read) {
|
||||
assert(blob_value != nullptr);
|
||||
|
||||
if (version != nullptr) {
|
||||
// Only fall back when the blob file is still owned exclusively by the
|
||||
// write path and therefore absent from Version metadata. Once Version
|
||||
// knows the file number, every result from Version::GetBlob(), including
|
||||
// corruption, must propagate directly.
|
||||
if (blob_idx.HasTTL() || blob_idx.IsInlined() ||
|
||||
version->storage_info()->GetBlobFileMetaData(blob_idx.file_number()) !=
|
||||
nullptr) {
|
||||
return version->GetBlob(read_options, user_key, blob_idx, prefetch_buffer,
|
||||
blob_value, bytes_read);
|
||||
}
|
||||
}
|
||||
|
||||
Status s = version != nullptr ? Status::Corruption("Invalid blob file number")
|
||||
: Status::NotFound();
|
||||
|
||||
if (blob_file_cache == nullptr) {
|
||||
return s;
|
||||
}
|
||||
|
||||
CacheHandleGuard<BlobFileReader> reader;
|
||||
s = blob_file_cache->GetBlobFileReader(read_options, blob_idx.file_number(),
|
||||
&reader,
|
||||
/*allow_footer_skip_retry=*/true);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
std::unique_ptr<BlobContents> blob_contents;
|
||||
s = reader.GetValue()->GetBlob(read_options, user_key, blob_idx.offset(),
|
||||
blob_idx.size(), blob_idx.compression(),
|
||||
prefetch_buffer, nullptr, &blob_contents,
|
||||
bytes_read);
|
||||
if (s.ok()) {
|
||||
blob_value->PinSelf(blob_contents->data());
|
||||
return s;
|
||||
}
|
||||
|
||||
if (!s.IsCorruption()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
reader.Reset();
|
||||
blob_file_cache->Evict(blob_idx.file_number());
|
||||
|
||||
std::unique_ptr<BlobFileReader> fresh_reader;
|
||||
s = blob_file_cache->OpenBlobFileReaderUncached(
|
||||
read_options, blob_idx.file_number(), &fresh_reader,
|
||||
/*allow_footer_skip_retry=*/true);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
std::unique_ptr<BlobContents> fresh_contents;
|
||||
s = fresh_reader->GetBlob(read_options, user_key, blob_idx.offset(),
|
||||
blob_idx.size(), blob_idx.compression(),
|
||||
prefetch_buffer, nullptr, &fresh_contents,
|
||||
bytes_read);
|
||||
if (s.ok()) {
|
||||
blob_value->PinSelf(fresh_contents->data());
|
||||
CacheHandleGuard<BlobFileReader> ignored;
|
||||
blob_file_cache
|
||||
->RefreshBlobFileReader(blob_idx.file_number(), &fresh_reader, &ignored)
|
||||
.PermitUncheckedError();
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -0,0 +1,294 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "db/blob/blob_file_addition.h"
|
||||
#include "db/blob/blob_file_garbage.h"
|
||||
#include "db/blob/blob_log_format.h"
|
||||
#include "db/blob/blob_write_batch_transformer.h"
|
||||
#include "port/port.h"
|
||||
#include "rocksdb/compression_type.h"
|
||||
#include "rocksdb/env.h"
|
||||
#include "rocksdb/file_system.h"
|
||||
#include "rocksdb/options.h"
|
||||
#include "rocksdb/rocksdb_namespace.h"
|
||||
#include "rocksdb/slice.h"
|
||||
#include "rocksdb/status.h"
|
||||
#include "util/hash_containers.h"
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
class BlobFileCache;
|
||||
class BlobFileCompletionCallback;
|
||||
class BlobIndex;
|
||||
class BlobLogWriter;
|
||||
class FilePrefetchBuffer;
|
||||
class IOTracer;
|
||||
class Logger;
|
||||
class PinnableSlice;
|
||||
class Version;
|
||||
class WritableFileWriter;
|
||||
struct FileOptions;
|
||||
struct ReadOptions;
|
||||
|
||||
// Manages per-partition blob files for write-path blob direct write.
|
||||
//
|
||||
// The v1 design keeps each memtable switch as one FIFO generation batch.
|
||||
// Direct-write files for that memtable are sealed and registered when the
|
||||
// matching flush commits. The current implementation still serializes blob
|
||||
// appends through one manager mutex; follow-up PRs can add independent
|
||||
// per-partition locks to allow concurrent blob writes without changing the
|
||||
// generation/flush contract. Follow-up PRs can also broaden compatibility as
|
||||
// the feature matures.
|
||||
class BlobFilePartitionManager {
|
||||
public:
|
||||
using FileNumberAllocator = std::function<uint64_t()>;
|
||||
|
||||
// Creates the per-column-family manager for write-path blob files.
|
||||
BlobFilePartitionManager(
|
||||
uint32_t num_partitions,
|
||||
std::shared_ptr<BlobFilePartitionStrategy> strategy,
|
||||
FileNumberAllocator file_number_allocator, FileSystem* fs,
|
||||
SystemClock* clock, Statistics* statistics,
|
||||
const FileOptions& file_options, std::string db_path,
|
||||
std::string column_family_name, uint64_t blob_file_size, bool use_fsync,
|
||||
BlobFileCache* blob_file_cache, BlobFileCompletionCallback* blob_callback,
|
||||
const std::vector<std::shared_ptr<EventListener>>& listeners,
|
||||
FileChecksumGenFactory* file_checksum_gen_factory,
|
||||
const FileTypeSet& checksum_handoff_file_types,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, std::string db_id,
|
||||
std::string db_session_id, Logger* info_log);
|
||||
|
||||
// Evicts cached readers for manager-owned files during shutdown.
|
||||
~BlobFilePartitionManager();
|
||||
|
||||
// Appends one blob record to a partition file and returns the resulting
|
||||
// BlobIndex location metadata.
|
||||
Status WriteBlob(const WriteOptions& write_options, uint32_t column_family_id,
|
||||
CompressionType compression, const Slice& key,
|
||||
const Slice& value, uint64_t* blob_file_number,
|
||||
uint64_t* blob_offset, uint64_t* blob_size,
|
||||
const BlobDirectWriteSettings* settings = nullptr,
|
||||
const uint32_t* partition_idx = nullptr);
|
||||
|
||||
// Selects the partition to use for all blob-backed columns of one PutEntity
|
||||
// operation. The return value is already normalized to [0, num_partitions_).
|
||||
uint32_t SelectWideColumnPartition(uint32_t column_family_id,
|
||||
const Slice& key,
|
||||
const WideColumns& columns) const;
|
||||
|
||||
// Move the current active partition files into the next immutable
|
||||
// memtable-generation batch. Called from SwitchMemtable() while DB mutex is
|
||||
// held. No I/O is performed here.
|
||||
void RotateCurrentGeneration();
|
||||
|
||||
// Seal the first `num_generations` queued immutable generations and return
|
||||
// all blob additions and initial-garbage updates that must be registered
|
||||
// with the matching flush. Generations stay queued until
|
||||
// CommitPreparedGenerations() is called. If `generation_blob_file_numbers`
|
||||
// is non-null, it receives the sealed blob file numbers for each prepared
|
||||
// generation in the same FIFO order.
|
||||
Status PrepareFlushAdditions(const WriteOptions& write_options,
|
||||
size_t num_generations,
|
||||
std::vector<BlobFileAddition>* additions,
|
||||
std::vector<BlobFileGarbage>* garbages,
|
||||
std::vector<std::vector<uint64_t>>*
|
||||
generation_blob_file_numbers = nullptr);
|
||||
|
||||
// Remove the first `num_generations` prepared immutable generations after
|
||||
// their blob-file additions and garbage edits were committed to MANIFEST.
|
||||
void CommitPreparedGenerations(size_t num_generations);
|
||||
|
||||
// In v1 flush-on-write mode each AddRecord flushes to the OS immediately, so
|
||||
// there is nothing extra to do for the non-sync write path.
|
||||
Status FlushAllOpenFiles(const WriteOptions& /*write_options*/) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Sync the active open blob files before a sync WAL write.
|
||||
Status SyncAllOpenFiles(const WriteOptions& write_options);
|
||||
|
||||
// Returns blob files still owned by the manager and therefore not yet safe
|
||||
// to consider obsolete.
|
||||
void GetActiveBlobFileNumbers(UnorderedSet<uint64_t>* file_numbers) const;
|
||||
|
||||
// Returns sealed blob files that are still reachable through live memtables
|
||||
// or old SuperVersions and therefore must not be purged yet.
|
||||
void GetProtectedBlobFileNumbers(UnorderedSet<uint64_t>* file_numbers) const;
|
||||
|
||||
// Returns true when the blob file is still owned by the write path or
|
||||
// protected by a live memtable / old SuperVersion.
|
||||
bool IsTrackedBlobFileNumber(uint64_t file_number) const;
|
||||
|
||||
// Increments / decrements memtable-held protection on sealed blob files.
|
||||
void ProtectSealedBlobFileNumbers(const std::vector<uint64_t>& file_numbers);
|
||||
void UnprotectSealedBlobFileNumbers(
|
||||
const std::vector<uint64_t>& file_numbers);
|
||||
|
||||
// Stops protecting file numbers that are now registered in MANIFEST.
|
||||
void RemoveFilePartitionMappings(const std::vector<uint64_t>& file_numbers);
|
||||
|
||||
// Marks blob records from a failed transformed write as initial garbage for
|
||||
// the target blob file while keeping the physical bytes in place.
|
||||
// Returns Corruption if the manager can no longer match the blob file.
|
||||
Status MarkBlobWriteAsGarbage(uint64_t file_number, uint64_t blob_count,
|
||||
uint64_t blob_bytes);
|
||||
|
||||
// Resolves a direct-write BlobIndex by consulting manifest-visible state
|
||||
// first, then falling back to direct blob-file reads only when the target
|
||||
// blob file is still write-path-owned and therefore not yet tracked by
|
||||
// Version. Existing manifest-visible read results, including I/O failures,
|
||||
// are returned directly rather than masked by fallback logic.
|
||||
static Status ResolveBlobDirectWriteIndex(
|
||||
const ReadOptions& read_options, const Slice& user_key,
|
||||
const BlobIndex& blob_idx, const Version* version,
|
||||
BlobFileCache* blob_file_cache, FilePrefetchBuffer* prefetch_buffer,
|
||||
PinnableSlice* blob_value, uint64_t* bytes_read);
|
||||
|
||||
private:
|
||||
struct Partition {
|
||||
// Active writer for this partition, or null when the partition is idle.
|
||||
std::unique_ptr<BlobLogWriter> writer;
|
||||
// Metadata tracked while the active file remains open.
|
||||
uint64_t file_number = 0;
|
||||
uint64_t file_size = 0;
|
||||
uint64_t blob_count = 0;
|
||||
uint64_t total_blob_bytes = 0;
|
||||
// Failed transformed writes already appended to this physical file.
|
||||
uint64_t garbage_blob_count = 0;
|
||||
uint64_t garbage_blob_bytes = 0;
|
||||
uint32_t column_family_id = 0;
|
||||
CompressionType compression = kNoCompression;
|
||||
bool sync_required = false;
|
||||
};
|
||||
|
||||
struct DeferredFile {
|
||||
// Blob writer moved out of an active partition at memtable rotation.
|
||||
std::unique_ptr<BlobLogWriter> writer;
|
||||
// Metadata preserved until flush preparation seals the file.
|
||||
uint64_t file_number = 0;
|
||||
uint64_t blob_count = 0;
|
||||
uint64_t total_blob_bytes = 0;
|
||||
// Failed transformed writes already appended before the file is sealed.
|
||||
uint64_t garbage_blob_count = 0;
|
||||
uint64_t garbage_blob_bytes = 0;
|
||||
};
|
||||
|
||||
struct SealedFile {
|
||||
// Blob-file metadata ready to be added to MANIFEST.
|
||||
BlobFileAddition addition;
|
||||
// Initial unreachable bytes that should be registered as garbage
|
||||
// alongside the file addition.
|
||||
uint64_t garbage_blob_count = 0;
|
||||
uint64_t garbage_blob_bytes = 0;
|
||||
};
|
||||
|
||||
struct GenerationBatch {
|
||||
// Files that were still open when the memtable became immutable.
|
||||
std::deque<DeferredFile> deferred_files;
|
||||
// Files already sealed for this memtable generation.
|
||||
std::vector<SealedFile> sealed_files;
|
||||
};
|
||||
|
||||
// Clears all active-file bookkeeping from a partition after sealing.
|
||||
void ResetPartitionState(Partition* partition);
|
||||
// Marks a blob file as manager-owned until it is committed or abandoned.
|
||||
void AddFilePartitionMapping(uint64_t file_number, uint32_t partition_idx);
|
||||
// Stops tracking a blob file after open/seal failure.
|
||||
void RemoveFilePartitionMapping(uint64_t file_number);
|
||||
|
||||
// Opens a new active blob file for one partition.
|
||||
Status OpenNewBlobFile(Partition* partition, uint32_t column_family_id,
|
||||
CompressionType compression, uint32_t partition_idx);
|
||||
// Finalizes one active partition file and returns the MANIFEST addition.
|
||||
Status SealActiveBlobFile(const WriteOptions& write_options,
|
||||
Partition* partition, SealedFile* sealed_file);
|
||||
// Finalizes one deferred file that was carried over to flush preparation.
|
||||
Status SealDeferredFile(const WriteOptions& write_options,
|
||||
DeferredFile* deferred, SealedFile* sealed_file);
|
||||
// Appends the footer, reports file completion, and builds the resulting
|
||||
// BlobFileAddition for one sealed blob file.
|
||||
Status FinalizeBlobFile(const WriteOptions& write_options,
|
||||
BlobLogWriter* writer, uint64_t file_number,
|
||||
uint64_t blob_count, uint64_t total_blob_bytes,
|
||||
BlobFileAddition* addition);
|
||||
// Appends one initial-garbage record to the flush output if any failed
|
||||
// transformed writes targeted the sealed blob file.
|
||||
static void AddSealedFileGarbage(const SealedFile& sealed_file,
|
||||
std::vector<BlobFileGarbage>* garbages);
|
||||
// Returns true if the active open file matches `file_number` and consumes
|
||||
// the failed-write garbage accounting.
|
||||
static bool MarkPartitionGarbage(Partition* partition, uint64_t file_number,
|
||||
uint64_t blob_count, uint64_t blob_bytes);
|
||||
// Returns true if the deferred flush-preparation file matches `file_number`
|
||||
// and consumes the failed-write garbage accounting.
|
||||
static bool MarkDeferredFileGarbage(DeferredFile* deferred,
|
||||
uint64_t file_number, uint64_t blob_count,
|
||||
uint64_t blob_bytes);
|
||||
// Returns true if one already-sealed file matches `file_number` and
|
||||
// consumes the failed-write garbage accounting.
|
||||
static bool MarkSealedFileGarbage(std::vector<SealedFile>* sealed_files,
|
||||
uint64_t file_number, uint64_t blob_count,
|
||||
uint64_t blob_bytes);
|
||||
// Optionally seeds the blob cache with the original uncompressed value.
|
||||
Status MaybePrepopulateBlobCache(const BlobDirectWriteSettings* settings,
|
||||
const Slice& original_value,
|
||||
uint64_t blob_file_number,
|
||||
uint64_t blob_offset);
|
||||
|
||||
// Configured partition fanout and write-selection strategy.
|
||||
const uint32_t num_partitions_;
|
||||
std::shared_ptr<BlobFilePartitionStrategy> strategy_;
|
||||
// Shared services needed to create, track, and cache blob files.
|
||||
FileNumberAllocator file_number_allocator_;
|
||||
FileSystem* fs_;
|
||||
SystemClock* clock_;
|
||||
Statistics* statistics_;
|
||||
FileOptions file_options_;
|
||||
// Column-family context used for file naming and callbacks.
|
||||
std::string db_path_;
|
||||
std::string column_family_name_;
|
||||
// File creation policy and shared blob-file infrastructure.
|
||||
uint64_t blob_file_size_;
|
||||
bool use_fsync_;
|
||||
BlobFileCache* blob_file_cache_;
|
||||
BlobFileCompletionCallback* blob_callback_;
|
||||
std::vector<std::shared_ptr<EventListener>> listeners_;
|
||||
FileChecksumGenFactory* file_checksum_gen_factory_;
|
||||
FileTypeSet checksum_handoff_file_types_;
|
||||
std::shared_ptr<IOTracer> io_tracer_;
|
||||
// DB identity used when constructing cache keys.
|
||||
std::string db_id_;
|
||||
std::string db_session_id_;
|
||||
Logger* info_log_;
|
||||
|
||||
// One active writer slot per configured partition.
|
||||
std::vector<std::unique_ptr<Partition>> partitions_;
|
||||
|
||||
// Protects partition state and generation queues.
|
||||
mutable port::Mutex mutex_;
|
||||
// Files already sealed while the current mutable memtable was active.
|
||||
std::vector<SealedFile> current_generation_sealed_files_;
|
||||
// FIFO immutable memtable generations waiting to be flushed.
|
||||
std::deque<GenerationBatch> pending_generations_;
|
||||
|
||||
// Tracks blob files still owned by active or deferred write-path state until
|
||||
// MANIFEST commit publishes them.
|
||||
std::unordered_map<uint64_t, uint32_t> file_to_partition_;
|
||||
// Sealed direct-write files that remain reachable from live memtables, such
|
||||
// as old SuperVersions serving lazy iterator reads after flush commit.
|
||||
std::unordered_map<uint64_t, uint32_t> protected_blob_file_refs_;
|
||||
// Protects file_to_partition_ and protected_blob_file_refs_.
|
||||
mutable port::RWMutex file_partition_mutex_;
|
||||
};
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
+35
-13
@@ -29,7 +29,7 @@ Status BlobFileReader::Create(
|
||||
const ImmutableOptions& immutable_options, const ReadOptions& read_options,
|
||||
const FileOptions& file_options, uint32_t column_family_id,
|
||||
HistogramImpl* blob_file_read_hist, uint64_t blob_file_number,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, bool skip_footer_validation,
|
||||
std::unique_ptr<BlobFileReader>* blob_file_reader) {
|
||||
assert(blob_file_reader);
|
||||
assert(!*blob_file_reader);
|
||||
@@ -40,7 +40,8 @@ Status BlobFileReader::Create(
|
||||
{
|
||||
const Status s =
|
||||
OpenFile(immutable_options, file_options, blob_file_read_hist,
|
||||
blob_file_number, io_tracer, &file_size, &file_reader);
|
||||
blob_file_number, io_tracer, &file_size, &file_reader,
|
||||
/*skip_footer_size_check=*/skip_footer_validation);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
@@ -61,7 +62,7 @@ Status BlobFileReader::Create(
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
if (!skip_footer_validation) {
|
||||
const Status s =
|
||||
ReadFooter(file_reader.get(), read_options, file_size, statistics);
|
||||
if (!s.ok()) {
|
||||
@@ -76,9 +77,10 @@ Status BlobFileReader::Create(
|
||||
compression_type);
|
||||
}
|
||||
|
||||
blob_file_reader->reset(new BlobFileReader(
|
||||
std::move(file_reader), file_size, compression_type,
|
||||
std::move(decompressor), immutable_options.clock, statistics));
|
||||
blob_file_reader->reset(
|
||||
new BlobFileReader(std::move(file_reader), file_size, compression_type,
|
||||
std::move(decompressor), immutable_options.clock,
|
||||
statistics, !skip_footer_validation));
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
@@ -87,7 +89,8 @@ Status BlobFileReader::OpenFile(
|
||||
const ImmutableOptions& immutable_options, const FileOptions& file_opts,
|
||||
HistogramImpl* blob_file_read_hist, uint64_t blob_file_number,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, uint64_t* file_size,
|
||||
std::unique_ptr<RandomAccessFileReader>* file_reader) {
|
||||
std::unique_ptr<RandomAccessFileReader>* file_reader,
|
||||
bool skip_footer_size_check) {
|
||||
assert(file_size);
|
||||
assert(file_reader);
|
||||
|
||||
@@ -112,17 +115,26 @@ Status BlobFileReader::OpenFile(
|
||||
}
|
||||
}
|
||||
|
||||
if (*file_size < BlobLogHeader::kSize + BlobLogFooter::kSize) {
|
||||
if (!skip_footer_size_check &&
|
||||
*file_size < BlobLogHeader::kSize + BlobLogFooter::kSize) {
|
||||
return Status::Corruption("Malformed blob file");
|
||||
}
|
||||
if (skip_footer_size_check && *file_size < BlobLogHeader::kSize) {
|
||||
return Status::Corruption("Malformed blob file");
|
||||
}
|
||||
|
||||
std::unique_ptr<FSRandomAccessFile> file;
|
||||
FileOptions reader_file_opts = file_opts;
|
||||
|
||||
if (skip_footer_size_check && reader_file_opts.use_direct_reads) {
|
||||
reader_file_opts.use_direct_reads = false;
|
||||
}
|
||||
|
||||
{
|
||||
TEST_SYNC_POINT("BlobFileReader::OpenFile:NewRandomAccessFile");
|
||||
|
||||
const Status s =
|
||||
fs->NewRandomAccessFile(blob_file_path, file_opts, &file, dbg);
|
||||
fs->NewRandomAccessFile(blob_file_path, reader_file_opts, &file, dbg);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
@@ -291,13 +303,14 @@ BlobFileReader::BlobFileReader(
|
||||
std::unique_ptr<RandomAccessFileReader>&& file_reader, uint64_t file_size,
|
||||
CompressionType compression_type,
|
||||
std::shared_ptr<Decompressor> decompressor, SystemClock* clock,
|
||||
Statistics* statistics)
|
||||
Statistics* statistics, bool has_footer)
|
||||
: file_reader_(std::move(file_reader)),
|
||||
file_size_(file_size),
|
||||
compression_type_(compression_type),
|
||||
decompressor_(std::move(decompressor)),
|
||||
clock_(clock),
|
||||
statistics_(statistics) {
|
||||
statistics_(statistics),
|
||||
has_footer_(has_footer) {
|
||||
assert(file_reader_);
|
||||
}
|
||||
|
||||
@@ -312,7 +325,8 @@ Status BlobFileReader::GetBlob(
|
||||
|
||||
const uint64_t key_size = user_key.size();
|
||||
|
||||
if (!IsValidBlobOffset(offset, key_size, value_size, file_size_)) {
|
||||
if (!IsValidBlobOffset(offset, key_size, value_size, file_size_,
|
||||
has_footer_)) {
|
||||
return Status::Corruption("Invalid blob offset");
|
||||
}
|
||||
|
||||
@@ -428,7 +442,8 @@ void BlobFileReader::MultiGetBlob(
|
||||
const uint64_t offset = req->offset;
|
||||
const uint64_t value_size = req->len;
|
||||
|
||||
if (!IsValidBlobOffset(offset, key_size, value_size, file_size_)) {
|
||||
if (!IsValidBlobOffset(offset, key_size, value_size, file_size_,
|
||||
has_footer_)) {
|
||||
*req->status = Status::Corruption("Invalid blob offset");
|
||||
continue;
|
||||
}
|
||||
@@ -454,6 +469,13 @@ void BlobFileReader::MultiGetBlob(
|
||||
|
||||
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_READ, total_len);
|
||||
|
||||
if (read_reqs.empty()) {
|
||||
if (bytes_read) {
|
||||
*bytes_read = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Buffer buf;
|
||||
AlignedBuf aligned_buf;
|
||||
|
||||
|
||||
@@ -36,7 +36,20 @@ class BlobFileReader {
|
||||
HistogramImpl* blob_file_read_hist,
|
||||
uint64_t blob_file_number,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
std::unique_ptr<BlobFileReader>* reader);
|
||||
std::unique_ptr<BlobFileReader>* reader) {
|
||||
return Create(immutable_options, read_options, file_options,
|
||||
column_family_id, blob_file_read_hist, blob_file_number,
|
||||
io_tracer, /*skip_footer_validation=*/false, reader);
|
||||
}
|
||||
|
||||
// Allows opening in-flight direct-write blob files by optionally skipping
|
||||
// footer validation.
|
||||
static Status Create(
|
||||
const ImmutableOptions& immutable_options,
|
||||
const ReadOptions& read_options, const FileOptions& file_options,
|
||||
uint32_t column_family_id, HistogramImpl* blob_file_read_hist,
|
||||
uint64_t blob_file_number, const std::shared_ptr<IOTracer>& io_tracer,
|
||||
bool skip_footer_validation, std::unique_ptr<BlobFileReader>* reader);
|
||||
|
||||
BlobFileReader(const BlobFileReader&) = delete;
|
||||
BlobFileReader& operator=(const BlobFileReader&) = delete;
|
||||
@@ -63,18 +76,22 @@ class BlobFileReader {
|
||||
uint64_t GetFileSize() const { return file_size_; }
|
||||
|
||||
private:
|
||||
// `has_footer` tracks whether offset validation should reserve footer space.
|
||||
BlobFileReader(std::unique_ptr<RandomAccessFileReader>&& file_reader,
|
||||
uint64_t file_size, CompressionType compression_type,
|
||||
std::shared_ptr<Decompressor> decompressor, SystemClock* clock,
|
||||
Statistics* statistics);
|
||||
Statistics* statistics, bool has_footer);
|
||||
|
||||
// `skip_footer_size_check` is used for direct-write files that are still
|
||||
// missing their footer at open time.
|
||||
static Status OpenFile(const ImmutableOptions& immutable_options,
|
||||
const FileOptions& file_opts,
|
||||
HistogramImpl* blob_file_read_hist,
|
||||
uint64_t blob_file_number,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
uint64_t* file_size,
|
||||
std::unique_ptr<RandomAccessFileReader>* file_reader);
|
||||
std::unique_ptr<RandomAccessFileReader>* file_reader,
|
||||
bool skip_footer_size_check);
|
||||
|
||||
static Status ReadHeader(const RandomAccessFileReader* file_reader,
|
||||
const ReadOptions& read_options,
|
||||
@@ -110,6 +127,8 @@ class BlobFileReader {
|
||||
std::shared_ptr<Decompressor> decompressor_;
|
||||
SystemClock* clock_;
|
||||
Statistics* statistics_;
|
||||
// False when the reader was opened before the blob file footer was written.
|
||||
bool has_footer_;
|
||||
};
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -8,70 +8,48 @@
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/blob/blob_log_format.h"
|
||||
#include "db/dbformat.h"
|
||||
#include "db/wide/wide_column_serialization.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
Status BlobGarbageMeter::ProcessInFlow(const Slice& key, const Slice& value) {
|
||||
uint64_t blob_file_number = kInvalidBlobFileNumber;
|
||||
uint64_t bytes = 0;
|
||||
|
||||
const Status s = Parse(key, value, &blob_file_number, &bytes);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (blob_file_number == kInvalidBlobFileNumber) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
flows_[blob_file_number].AddInFlow(bytes);
|
||||
|
||||
return Status::OK();
|
||||
return ProcessFlow(key, value, /*is_inflow=*/true);
|
||||
}
|
||||
|
||||
Status BlobGarbageMeter::ProcessOutFlow(const Slice& key, const Slice& value) {
|
||||
uint64_t blob_file_number = kInvalidBlobFileNumber;
|
||||
uint64_t bytes = 0;
|
||||
|
||||
const Status s = Parse(key, value, &blob_file_number, &bytes);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (blob_file_number == kInvalidBlobFileNumber) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Note: in order to measure the amount of additional garbage, we only need to
|
||||
// track the outflow for preexisting files, i.e. those that also had inflow.
|
||||
// (Newly written files would only have outflow.)
|
||||
auto it = flows_.find(blob_file_number);
|
||||
if (it == flows_.end()) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
it->second.AddOutFlow(bytes);
|
||||
|
||||
return Status::OK();
|
||||
return ProcessFlow(key, value, /*is_inflow=*/false);
|
||||
}
|
||||
|
||||
Status BlobGarbageMeter::Parse(const Slice& key, const Slice& value,
|
||||
uint64_t* blob_file_number, uint64_t* bytes) {
|
||||
Status BlobGarbageMeter::GetBlobReferenceDetails(const ParsedInternalKey& ikey,
|
||||
const BlobIndex& blob_index,
|
||||
uint64_t* blob_file_number,
|
||||
uint64_t* bytes) {
|
||||
assert(blob_file_number);
|
||||
assert(*blob_file_number == kInvalidBlobFileNumber);
|
||||
assert(bytes);
|
||||
assert(*bytes == 0);
|
||||
|
||||
ParsedInternalKey ikey;
|
||||
|
||||
{
|
||||
constexpr bool log_err_key = false;
|
||||
const Status s = ParseInternalKey(key, &ikey, log_err_key);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
if (blob_index.IsInlined() || blob_index.HasTTL()) {
|
||||
return Status::Corruption("Unexpected TTL/inlined blob index");
|
||||
}
|
||||
|
||||
*blob_file_number = blob_index.file_number();
|
||||
*bytes =
|
||||
blob_index.size() +
|
||||
BlobLogRecord::CalculateAdjustmentForRecordHeader(ikey.user_key.size());
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status BlobGarbageMeter::ParseBlobIndexReference(const ParsedInternalKey& ikey,
|
||||
const Slice& value,
|
||||
uint64_t* blob_file_number,
|
||||
uint64_t* bytes) {
|
||||
assert(blob_file_number);
|
||||
assert(*blob_file_number == kInvalidBlobFileNumber);
|
||||
assert(bytes);
|
||||
assert(*bytes == 0);
|
||||
|
||||
if (ikey.type != kTypeBlobIndex) {
|
||||
return Status::OK();
|
||||
}
|
||||
@@ -85,16 +63,71 @@ Status BlobGarbageMeter::Parse(const Slice& key, const Slice& value,
|
||||
}
|
||||
}
|
||||
|
||||
if (blob_index.IsInlined() || blob_index.HasTTL()) {
|
||||
return Status::Corruption("Unexpected TTL/inlined blob index");
|
||||
return GetBlobReferenceDetails(ikey, blob_index, blob_file_number, bytes);
|
||||
}
|
||||
|
||||
void BlobGarbageMeter::AddFlow(uint64_t blob_file_number, uint64_t bytes,
|
||||
bool is_inflow) {
|
||||
if (is_inflow) {
|
||||
flows_[blob_file_number].AddInFlow(bytes);
|
||||
return;
|
||||
}
|
||||
|
||||
*blob_file_number = blob_index.file_number();
|
||||
*bytes =
|
||||
blob_index.size() +
|
||||
BlobLogRecord::CalculateAdjustmentForRecordHeader(ikey.user_key.size());
|
||||
// Note: in order to measure the amount of additional garbage, we only need to
|
||||
// track the outflow for preexisting files, i.e. those that also had inflow.
|
||||
// (Newly written files would only have outflow.)
|
||||
auto it = flows_.find(blob_file_number);
|
||||
if (it != flows_.end()) {
|
||||
it->second.AddOutFlow(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
Status BlobGarbageMeter::ProcessFlow(const Slice& key, const Slice& value,
|
||||
bool is_inflow) {
|
||||
ParsedInternalKey ikey;
|
||||
|
||||
{
|
||||
constexpr bool log_err_key = false;
|
||||
const Status s = ParseInternalKey(key, &ikey, log_err_key);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t blob_file_number = kInvalidBlobFileNumber;
|
||||
uint64_t bytes = 0;
|
||||
if (Status s =
|
||||
ParseBlobIndexReference(ikey, value, &blob_file_number, &bytes);
|
||||
!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (blob_file_number != kInvalidBlobFileNumber) {
|
||||
AddFlow(blob_file_number, bytes, is_inflow);
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
return ProcessEntityBlobReferences(ikey, value, is_inflow);
|
||||
}
|
||||
|
||||
Status BlobGarbageMeter::ProcessEntityBlobReferences(
|
||||
const ParsedInternalKey& ikey, const Slice& value, bool is_inflow) {
|
||||
if (ikey.type != kTypeWideColumnEntity) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
return WideColumnSerialization::ForEachBlobFileNumber(
|
||||
value, [&](const BlobIndex& blob_index) -> Status {
|
||||
uint64_t file_number = kInvalidBlobFileNumber;
|
||||
uint64_t blob_bytes = 0;
|
||||
if (const Status s = GetBlobReferenceDetails(ikey, blob_index,
|
||||
&file_number, &blob_bytes);
|
||||
!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
AddFlow(file_number, blob_bytes, is_inflow);
|
||||
return Status::OK();
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
class BlobIndex;
|
||||
struct ParsedInternalKey;
|
||||
class Slice;
|
||||
|
||||
// A class that can be used to compute the amount of additional garbage
|
||||
@@ -93,8 +95,19 @@ class BlobGarbageMeter {
|
||||
}
|
||||
|
||||
private:
|
||||
static Status Parse(const Slice& key, const Slice& value,
|
||||
uint64_t* blob_file_number, uint64_t* bytes);
|
||||
static Status GetBlobReferenceDetails(const ParsedInternalKey& ikey,
|
||||
const BlobIndex& blob_index,
|
||||
uint64_t* blob_file_number,
|
||||
uint64_t* bytes);
|
||||
static Status ParseBlobIndexReference(const ParsedInternalKey& ikey,
|
||||
const Slice& value,
|
||||
uint64_t* blob_file_number,
|
||||
uint64_t* bytes);
|
||||
|
||||
void AddFlow(uint64_t blob_file_number, uint64_t bytes, bool is_inflow);
|
||||
Status ProcessFlow(const Slice& key, const Slice& value, bool is_inflow);
|
||||
Status ProcessEntityBlobReferences(const ParsedInternalKey& ikey,
|
||||
const Slice& value, bool is_inflow);
|
||||
|
||||
std::unordered_map<uint64_t, BlobInOutFlow> flows_;
|
||||
};
|
||||
|
||||
@@ -11,10 +11,23 @@
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/blob/blob_log_format.h"
|
||||
#include "db/dbformat.h"
|
||||
#include "db/wide/wide_column_serialization.h"
|
||||
#include "test_util/testharness.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
namespace {
|
||||
|
||||
BlobIndex MakeBlobIndex(uint64_t file_number, uint64_t offset, uint64_t size) {
|
||||
std::string encoded;
|
||||
BlobIndex::EncodeBlob(&encoded, file_number, offset, size, kNoCompression);
|
||||
BlobIndex blob_index;
|
||||
EXPECT_OK(blob_index.DecodeFrom(encoded));
|
||||
return blob_index;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(BlobGarbageMeterTest, MeasureGarbage) {
|
||||
BlobGarbageMeter blob_garbage_meter;
|
||||
|
||||
@@ -188,6 +201,67 @@ TEST(BlobGarbageMeterTest, InlinedTTLBlobIndex) {
|
||||
ASSERT_NOK(blob_garbage_meter.ProcessOutFlow(key_slice, value_slice));
|
||||
}
|
||||
|
||||
TEST(BlobGarbageMeterTest, WideColumnEntity) {
|
||||
constexpr char user_key[] = "entity_key";
|
||||
constexpr SequenceNumber seq = 123;
|
||||
const InternalKey key(user_key, seq, kTypeWideColumnEntity);
|
||||
const Slice key_slice = key.Encode();
|
||||
|
||||
const std::vector<std::pair<std::string, std::string>> columns = {
|
||||
{"", "default_inline"},
|
||||
{"blob_attr", "blob_inline"},
|
||||
{"ttl", "00000001"},
|
||||
};
|
||||
|
||||
const BlobIndex default_blob = MakeBlobIndex(4, 123, 555);
|
||||
const BlobIndex attr_blob = MakeBlobIndex(5, 456, 777);
|
||||
|
||||
std::string inflow_value;
|
||||
ASSERT_OK(WideColumnSerialization::SerializeV2(
|
||||
columns, {{0, default_blob}, {1, attr_blob}}, inflow_value));
|
||||
|
||||
std::string outflow_value;
|
||||
ASSERT_OK(WideColumnSerialization::SerializeV2(columns, {{0, default_blob}},
|
||||
outflow_value));
|
||||
|
||||
BlobGarbageMeter blob_garbage_meter;
|
||||
ASSERT_OK(blob_garbage_meter.ProcessInFlow(key_slice, inflow_value));
|
||||
ASSERT_OK(blob_garbage_meter.ProcessOutFlow(key_slice, outflow_value));
|
||||
|
||||
const auto& flows = blob_garbage_meter.flows();
|
||||
ASSERT_EQ(flows.size(), 2);
|
||||
|
||||
constexpr size_t kUserKeySize = sizeof(user_key) - 1;
|
||||
const uint64_t default_bytes =
|
||||
default_blob.size() +
|
||||
BlobLogRecord::CalculateAdjustmentForRecordHeader(kUserKeySize);
|
||||
const uint64_t attr_bytes =
|
||||
attr_blob.size() +
|
||||
BlobLogRecord::CalculateAdjustmentForRecordHeader(kUserKeySize);
|
||||
|
||||
{
|
||||
const auto it = flows.find(default_blob.file_number());
|
||||
ASSERT_NE(it, flows.end());
|
||||
ASSERT_EQ(it->second.GetInFlow().GetCount(), 1);
|
||||
ASSERT_EQ(it->second.GetInFlow().GetBytes(), default_bytes);
|
||||
ASSERT_EQ(it->second.GetOutFlow().GetCount(), 1);
|
||||
ASSERT_EQ(it->second.GetOutFlow().GetBytes(), default_bytes);
|
||||
ASSERT_FALSE(it->second.HasGarbage());
|
||||
}
|
||||
|
||||
{
|
||||
const auto it = flows.find(attr_blob.file_number());
|
||||
ASSERT_NE(it, flows.end());
|
||||
ASSERT_EQ(it->second.GetInFlow().GetCount(), 1);
|
||||
ASSERT_EQ(it->second.GetInFlow().GetBytes(), attr_bytes);
|
||||
ASSERT_EQ(it->second.GetOutFlow().GetCount(), 0);
|
||||
ASSERT_EQ(it->second.GetOutFlow().GetBytes(), 0);
|
||||
ASSERT_TRUE(it->second.HasGarbage());
|
||||
ASSERT_EQ(it->second.GetGarbageCount(), 1);
|
||||
ASSERT_EQ(it->second.GetGarbageBytes(), attr_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -148,13 +148,26 @@ struct BlobLogRecord {
|
||||
|
||||
// Checks whether a blob offset is potentially valid or not.
|
||||
inline bool IsValidBlobOffset(uint64_t value_offset, uint64_t key_size,
|
||||
uint64_t value_size, uint64_t file_size) {
|
||||
if (value_offset <
|
||||
BlobLogHeader::kSize + BlobLogRecord::kHeaderSize + key_size) {
|
||||
uint64_t value_size, uint64_t file_size,
|
||||
bool has_footer = true) {
|
||||
constexpr uint64_t kMinPrefix =
|
||||
BlobLogHeader::kSize + BlobLogRecord::kHeaderSize;
|
||||
if (value_offset < kMinPrefix) {
|
||||
return false;
|
||||
}
|
||||
if (value_offset - kMinPrefix < key_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value_offset + value_size + BlobLogFooter::kSize > file_size) {
|
||||
const uint64_t footer_size = has_footer ? BlobLogFooter::kSize : 0;
|
||||
if (file_size < footer_size) {
|
||||
return false;
|
||||
}
|
||||
const uint64_t max_value_end = file_size - footer_size;
|
||||
if (value_size > max_value_end) {
|
||||
return false;
|
||||
}
|
||||
if (value_offset > max_value_end - value_size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,21 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
namespace {
|
||||
|
||||
Status AppendBlobRefreshRetryFailure(const Status& stale_status,
|
||||
const Status& retry_status) {
|
||||
assert(stale_status.IsCorruption());
|
||||
assert(!retry_status.ok());
|
||||
if (retry_status.IsCorruption()) {
|
||||
return retry_status;
|
||||
}
|
||||
return Status::CopyAppendMessage(
|
||||
stale_status, "; refresh retry failed: ", retry_status.ToString());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
BlobSource::BlobSource(const ImmutableOptions& immutable_options,
|
||||
const MutableCFOptions& mutable_cf_options,
|
||||
const std::string& db_id,
|
||||
@@ -231,6 +246,38 @@ Status BlobSource::GetBlob(const ReadOptions& read_options,
|
||||
s = blob_file_reader.GetValue()->GetBlob(
|
||||
read_options, user_key, offset, value_size, compression_type,
|
||||
prefetch_buffer, allocator, &blob_contents, &read_size);
|
||||
if (s.IsCorruption()) {
|
||||
const Status stale_status = s;
|
||||
blob_file_reader.Reset();
|
||||
blob_file_cache_->Evict(file_number);
|
||||
|
||||
std::unique_ptr<BlobFileReader> fresh_reader;
|
||||
s = blob_file_cache_->OpenBlobFileReaderUncached(
|
||||
read_options, file_number, &fresh_reader,
|
||||
/*allow_footer_skip_retry=*/false);
|
||||
if (!s.ok()) {
|
||||
return AppendBlobRefreshRetryFailure(stale_status, s);
|
||||
}
|
||||
|
||||
if (compression_type != fresh_reader->GetCompressionType()) {
|
||||
return Status::Corruption(
|
||||
"Compression type mismatch when reading blob");
|
||||
}
|
||||
|
||||
blob_contents.reset();
|
||||
read_size = 0;
|
||||
s = fresh_reader->GetBlob(read_options, user_key, offset, value_size,
|
||||
compression_type, prefetch_buffer, allocator,
|
||||
&blob_contents, &read_size);
|
||||
if (!s.ok()) {
|
||||
s = AppendBlobRefreshRetryFailure(stale_status, s);
|
||||
} else {
|
||||
CacheHandleGuard<BlobFileReader> ignored_reader;
|
||||
blob_file_cache_
|
||||
->RefreshBlobFileReader(file_number, &fresh_reader, &ignored_reader)
|
||||
.PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
@@ -397,6 +444,89 @@ void BlobSource::MultiGetBlobFromOneFile(const ReadOptions& read_options,
|
||||
blob_file_reader.GetValue()->MultiGetBlob(read_options, allocator,
|
||||
_blob_reqs, &_bytes_read);
|
||||
|
||||
bool needs_reader_refresh = false;
|
||||
for (const auto& blob_req : _blob_reqs) {
|
||||
BlobReadRequest* const req = blob_req.first;
|
||||
assert(req != nullptr);
|
||||
assert(req->status != nullptr);
|
||||
if (req->status->IsCorruption()) {
|
||||
needs_reader_refresh = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (needs_reader_refresh) {
|
||||
blob_file_reader.Reset();
|
||||
blob_file_cache_->Evict(file_number);
|
||||
|
||||
std::unique_ptr<BlobFileReader> fresh_reader;
|
||||
s = blob_file_cache_->OpenBlobFileReaderUncached(
|
||||
read_options, file_number, &fresh_reader,
|
||||
/*allow_footer_skip_retry=*/false);
|
||||
if (!s.ok()) {
|
||||
for (const auto& blob_req : _blob_reqs) {
|
||||
BlobReadRequest* const req = blob_req.first;
|
||||
assert(req != nullptr);
|
||||
assert(req->status != nullptr);
|
||||
if (req->status->IsCorruption()) {
|
||||
*req->status = AppendBlobRefreshRetryFailure(*req->status, s);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
autovector<std::pair<BlobReadRequest*, std::unique_ptr<BlobContents>>>
|
||||
retry_blob_reqs;
|
||||
autovector<Status> stale_statuses;
|
||||
for (auto& blob_req : _blob_reqs) {
|
||||
BlobReadRequest* const req = blob_req.first;
|
||||
assert(req != nullptr);
|
||||
assert(req->status != nullptr);
|
||||
if (!req->status->IsCorruption()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
stale_statuses.emplace_back(*req->status);
|
||||
*req->status = Status::OK();
|
||||
blob_req.second.reset();
|
||||
retry_blob_reqs.emplace_back(req, std::unique_ptr<BlobContents>());
|
||||
}
|
||||
|
||||
uint64_t refreshed_bytes_read = 0;
|
||||
fresh_reader->MultiGetBlob(read_options, allocator, retry_blob_reqs,
|
||||
&refreshed_bytes_read);
|
||||
_bytes_read += refreshed_bytes_read;
|
||||
|
||||
bool install_fresh_reader = false;
|
||||
for (size_t i = 0; i < retry_blob_reqs.size(); ++i) {
|
||||
auto& retried_blob_req = retry_blob_reqs[i];
|
||||
BlobReadRequest* const retried_req = retried_blob_req.first;
|
||||
assert(retried_req != nullptr);
|
||||
if (retried_req->status->ok()) {
|
||||
install_fresh_reader = true;
|
||||
} else {
|
||||
*retried_req->status = AppendBlobRefreshRetryFailure(
|
||||
stale_statuses[i], *retried_req->status);
|
||||
}
|
||||
|
||||
for (auto& blob_req : _blob_reqs) {
|
||||
if (blob_req.first != retried_req) {
|
||||
continue;
|
||||
}
|
||||
|
||||
blob_req.second = std::move(retried_blob_req.second);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (install_fresh_reader) {
|
||||
CacheHandleGuard<BlobFileReader> ignored_reader;
|
||||
blob_file_cache_
|
||||
->RefreshBlobFileReader(file_number, &fresh_reader, &ignored_reader)
|
||||
.PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
|
||||
if (blob_cache_ && read_options.fill_cache) {
|
||||
// If filling cache is allowed and a cache is configured, try to put
|
||||
// the blob(s) to the cache.
|
||||
|
||||
@@ -1044,6 +1044,156 @@ TEST_F(BlobSourceTest, MultiGetBlobsFromCache) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(BlobSourceTest, GetBlobPreservesCorruptionDetailsWhenRefreshOpenFails) {
|
||||
options_.cf_paths.emplace_back(
|
||||
test::PerThreadDBPath(env_,
|
||||
"BlobSourceTest_GetBlobPreservesCorruptionDetails"),
|
||||
0);
|
||||
|
||||
DestroyAndReopen(options_);
|
||||
|
||||
ImmutableOptions immutable_options(options_);
|
||||
MutableCFOptions mutable_cf_options(options_);
|
||||
|
||||
constexpr uint32_t column_family_id = 1;
|
||||
constexpr bool has_ttl = false;
|
||||
constexpr ExpirationRange expiration_range;
|
||||
constexpr uint64_t blob_file_number = 1;
|
||||
|
||||
const std::string key_str = "key0";
|
||||
const std::string blob_str = "blob0";
|
||||
std::vector<Slice> keys{Slice(key_str)};
|
||||
std::vector<Slice> blobs{Slice(blob_str)};
|
||||
std::vector<uint64_t> blob_offsets(1);
|
||||
std::vector<uint64_t> blob_sizes(1);
|
||||
|
||||
const uint64_t file_size = BlobLogHeader::kSize + BlobLogRecord::kHeaderSize +
|
||||
key_str.size() + blob_str.size() +
|
||||
BlobLogFooter::kSize;
|
||||
|
||||
WriteBlobFile(immutable_options, column_family_id, has_ttl, expiration_range,
|
||||
expiration_range, blob_file_number, keys, blobs, kNoCompression,
|
||||
blob_offsets, blob_sizes);
|
||||
|
||||
constexpr size_t capacity = 1024;
|
||||
std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity);
|
||||
|
||||
FileOptions file_options;
|
||||
std::unique_ptr<BlobFileCache> blob_file_cache =
|
||||
std::make_unique<BlobFileCache>(
|
||||
backing_cache.get(), &immutable_options, &file_options,
|
||||
column_family_id, nullptr /*HistogramImpl*/, nullptr /*IOTracer*/);
|
||||
|
||||
BlobSource blob_source(immutable_options, mutable_cf_options, db_id_,
|
||||
db_session_id_, blob_file_cache.get());
|
||||
|
||||
ReadOptions read_options;
|
||||
read_options.verify_checksums = true;
|
||||
constexpr FilePrefetchBuffer* prefetch_buffer = nullptr;
|
||||
|
||||
PinnableSlice value;
|
||||
uint64_t bytes_read = 0;
|
||||
ASSERT_OK(blob_source.GetBlob(
|
||||
read_options, keys[0], blob_file_number, blob_offsets[0], file_size,
|
||||
blob_sizes[0], kNoCompression, prefetch_buffer, &value, &bytes_read));
|
||||
ASSERT_EQ(value, blobs[0]);
|
||||
|
||||
const std::string blob_file_path =
|
||||
BlobFileName(immutable_options.cf_paths.front().path, blob_file_number);
|
||||
ASSERT_OK(env_->DeleteFile(blob_file_path));
|
||||
|
||||
PinnableSlice invalid_value;
|
||||
bytes_read = 0;
|
||||
Status s =
|
||||
blob_source.GetBlob(read_options, keys[0], blob_file_number, file_size,
|
||||
file_size, blob_sizes[0], kNoCompression,
|
||||
prefetch_buffer, &invalid_value, &bytes_read);
|
||||
ASSERT_TRUE(s.IsCorruption());
|
||||
ASSERT_EQ(bytes_read, 0);
|
||||
|
||||
const std::string status_str = s.ToString();
|
||||
ASSERT_NE(status_str.find("Invalid blob offset"), std::string::npos);
|
||||
ASSERT_NE(status_str.find("refresh retry failed"), std::string::npos);
|
||||
ASSERT_NE(status_str.find("IO error"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST_F(BlobSourceTest,
|
||||
MultiGetBlobPreservesCorruptionDetailsWhenRefreshOpenFails) {
|
||||
options_.cf_paths.emplace_back(
|
||||
test::PerThreadDBPath(
|
||||
env_, "BlobSourceTest_MultiGetBlobPreservesCorruptionDetails"),
|
||||
0);
|
||||
|
||||
DestroyAndReopen(options_);
|
||||
|
||||
ImmutableOptions immutable_options(options_);
|
||||
MutableCFOptions mutable_cf_options(options_);
|
||||
|
||||
constexpr uint32_t column_family_id = 1;
|
||||
constexpr bool has_ttl = false;
|
||||
constexpr ExpirationRange expiration_range;
|
||||
constexpr uint64_t blob_file_number = 1;
|
||||
|
||||
const std::string key_str = "key0";
|
||||
const std::string blob_str = "blob0";
|
||||
std::vector<Slice> keys{Slice(key_str)};
|
||||
std::vector<Slice> blobs{Slice(blob_str)};
|
||||
std::vector<uint64_t> blob_offsets(1);
|
||||
std::vector<uint64_t> blob_sizes(1);
|
||||
|
||||
const uint64_t file_size = BlobLogHeader::kSize + BlobLogRecord::kHeaderSize +
|
||||
key_str.size() + blob_str.size() +
|
||||
BlobLogFooter::kSize;
|
||||
|
||||
WriteBlobFile(immutable_options, column_family_id, has_ttl, expiration_range,
|
||||
expiration_range, blob_file_number, keys, blobs, kNoCompression,
|
||||
blob_offsets, blob_sizes);
|
||||
|
||||
constexpr size_t capacity = 1024;
|
||||
std::shared_ptr<Cache> backing_cache = NewLRUCache(capacity);
|
||||
|
||||
FileOptions file_options;
|
||||
std::unique_ptr<BlobFileCache> blob_file_cache =
|
||||
std::make_unique<BlobFileCache>(
|
||||
backing_cache.get(), &immutable_options, &file_options,
|
||||
column_family_id, nullptr /*HistogramImpl*/, nullptr /*IOTracer*/);
|
||||
|
||||
BlobSource blob_source(immutable_options, mutable_cf_options, db_id_,
|
||||
db_session_id_, blob_file_cache.get());
|
||||
|
||||
ReadOptions read_options;
|
||||
read_options.verify_checksums = true;
|
||||
constexpr FilePrefetchBuffer* prefetch_buffer = nullptr;
|
||||
|
||||
PinnableSlice value;
|
||||
uint64_t bytes_read = 0;
|
||||
ASSERT_OK(blob_source.GetBlob(
|
||||
read_options, keys[0], blob_file_number, blob_offsets[0], file_size,
|
||||
blob_sizes[0], kNoCompression, prefetch_buffer, &value, &bytes_read));
|
||||
ASSERT_EQ(value, blobs[0]);
|
||||
|
||||
const std::string blob_file_path =
|
||||
BlobFileName(immutable_options.cf_paths.front().path, blob_file_number);
|
||||
ASSERT_OK(env_->DeleteFile(blob_file_path));
|
||||
|
||||
std::array<Status, 1> statuses;
|
||||
std::array<PinnableSlice, 1> values;
|
||||
autovector<BlobReadRequest> blob_reqs;
|
||||
blob_reqs.emplace_back(keys[0], file_size, blob_sizes[0], kNoCompression,
|
||||
&values[0], &statuses[0]);
|
||||
|
||||
bytes_read = 0;
|
||||
blob_source.MultiGetBlobFromOneFile(read_options, blob_file_number, file_size,
|
||||
blob_reqs, &bytes_read);
|
||||
ASSERT_TRUE(statuses[0].IsCorruption());
|
||||
ASSERT_EQ(bytes_read, 0);
|
||||
|
||||
const std::string status_str = statuses[0].ToString();
|
||||
ASSERT_NE(status_str.find("Invalid blob offset"), std::string::npos);
|
||||
ASSERT_NE(status_str.find("refresh retry failed"), std::string::npos);
|
||||
ASSERT_NE(status_str.find("IO error"), std::string::npos);
|
||||
}
|
||||
|
||||
class BlobSecondaryCacheTest : public DBTestBase {
|
||||
protected:
|
||||
public:
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
|
||||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
|
||||
#include "db/blob/blob_write_batch_transformer.h"
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/blob/blob_log_format.h"
|
||||
#include "db/wide/wide_column_serialization.h"
|
||||
#include "db/write_batch_internal.h"
|
||||
#include "port/likely.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
BlobWriteBatchTransformer::BlobWriteBatchTransformer(
|
||||
const BlobPartitionManagerProvider& partition_mgr_provider,
|
||||
WriteBatch* output_batch,
|
||||
const BlobDirectWriteSettingsProvider& settings_provider,
|
||||
const WriteOptions& write_options)
|
||||
: partition_mgr_provider_(partition_mgr_provider),
|
||||
output_batch_(output_batch),
|
||||
settings_provider_(settings_provider),
|
||||
write_options_(write_options) {
|
||||
assert(partition_mgr_provider_);
|
||||
assert(output_batch_);
|
||||
assert(settings_provider_);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::TransformBatch(
|
||||
const WriteOptions& write_options, WriteBatch* input_batch,
|
||||
WriteBatch* output_batch,
|
||||
const BlobPartitionManagerProvider& partition_mgr_provider,
|
||||
const BlobDirectWriteSettingsProvider& settings_provider, bool* transformed,
|
||||
std::vector<BlobFilePartitionManager*>* used_managers,
|
||||
std::vector<RollbackInfo>* rollback_infos) {
|
||||
assert(input_batch);
|
||||
assert(output_batch);
|
||||
assert(transformed);
|
||||
|
||||
output_batch->Clear();
|
||||
*transformed = false;
|
||||
|
||||
BlobWriteBatchTransformer transformer(partition_mgr_provider, output_batch,
|
||||
settings_provider, write_options);
|
||||
|
||||
Status s = input_batch->Iterate(&transformer);
|
||||
*transformed = transformer.HasTransformed();
|
||||
|
||||
if (used_managers) {
|
||||
used_managers->assign(transformer.used_managers_.begin(),
|
||||
transformer.used_managers_.end());
|
||||
}
|
||||
|
||||
if (rollback_infos) {
|
||||
*rollback_infos = std::move(transformer.rollback_infos_);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MaybePreprocessWideColumns(
|
||||
const WriteOptions& write_options, uint32_t column_family_id,
|
||||
const Slice& key, const WideColumns& columns,
|
||||
BlobFilePartitionManager* partition_mgr,
|
||||
const BlobDirectWriteSettings& settings, bool serialize_inline_entity,
|
||||
std::string* entity, bool* transformed,
|
||||
std::vector<RollbackInfo>* rollback_infos) {
|
||||
assert(entity != nullptr);
|
||||
assert(transformed != nullptr);
|
||||
|
||||
*transformed = false;
|
||||
|
||||
if (partition_mgr == nullptr || !settings.enable_blob_direct_write) {
|
||||
if (!serialize_inline_entity) {
|
||||
return Status::OK();
|
||||
}
|
||||
entity->clear();
|
||||
return WideColumnSerialization::Serialize(columns, *entity);
|
||||
}
|
||||
|
||||
std::vector<std::pair<size_t, BlobIndex>> new_blob_columns;
|
||||
new_blob_columns.reserve(columns.size());
|
||||
std::string blob_index_buf;
|
||||
bool has_entity_partition = false;
|
||||
uint32_t entity_partition = 0;
|
||||
|
||||
for (size_t column_idx = 0; column_idx < columns.size(); ++column_idx) {
|
||||
const Slice& column_value = columns[column_idx].value();
|
||||
if (column_value.size() < settings.min_blob_size) {
|
||||
continue;
|
||||
}
|
||||
if (!has_entity_partition) {
|
||||
entity_partition = partition_mgr->SelectWideColumnPartition(
|
||||
column_family_id, key, columns);
|
||||
has_entity_partition = true;
|
||||
}
|
||||
|
||||
uint64_t blob_file_number = 0;
|
||||
uint64_t blob_offset = 0;
|
||||
uint64_t blob_size = 0;
|
||||
Status s = partition_mgr->WriteBlob(
|
||||
write_options, column_family_id, settings.compression_type, key,
|
||||
column_value, &blob_file_number, &blob_offset, &blob_size, &settings,
|
||||
&entity_partition);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (rollback_infos != nullptr) {
|
||||
const uint64_t record_bytes =
|
||||
BlobLogRecord::kHeaderSize + key.size() + blob_size;
|
||||
rollback_infos->push_back(
|
||||
{partition_mgr, blob_file_number, /*count=*/1, record_bytes});
|
||||
}
|
||||
|
||||
BlobIndex blob_index;
|
||||
BlobIndex::EncodeBlob(&blob_index_buf, blob_file_number, blob_offset,
|
||||
blob_size, settings.compression_type);
|
||||
s = blob_index.DecodeFrom(blob_index_buf);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
new_blob_columns.emplace_back(column_idx, blob_index);
|
||||
}
|
||||
|
||||
if (new_blob_columns.empty()) {
|
||||
if (!serialize_inline_entity) {
|
||||
return Status::OK();
|
||||
}
|
||||
entity->clear();
|
||||
return WideColumnSerialization::Serialize(columns, *entity);
|
||||
}
|
||||
|
||||
entity->clear();
|
||||
Status s =
|
||||
WideColumnSerialization::SerializeV2(columns, new_blob_columns, *entity);
|
||||
if (s.ok()) {
|
||||
*transformed = true;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::PutCF(uint32_t column_family_id,
|
||||
const Slice& key, const Slice& value) {
|
||||
// Use cached settings/manager for the same CF to avoid per-entry lookup.
|
||||
if (column_family_id != cached_cf_id_) {
|
||||
cached_settings_ = settings_provider_(column_family_id);
|
||||
cached_partition_mgr_ = partition_mgr_provider_(column_family_id);
|
||||
cached_cf_id_ = column_family_id;
|
||||
}
|
||||
const auto& settings = cached_settings_;
|
||||
|
||||
if (!cached_partition_mgr_ || !settings.enable_blob_direct_write ||
|
||||
value.size() < settings.min_blob_size) {
|
||||
return WriteBatchInternal::Put(output_batch_, column_family_id, key, value);
|
||||
}
|
||||
|
||||
uint64_t blob_file_number = 0;
|
||||
uint64_t blob_offset = 0;
|
||||
uint64_t blob_size = 0;
|
||||
|
||||
Status s = cached_partition_mgr_->WriteBlob(
|
||||
write_options_, column_family_id, settings.compression_type, key, value,
|
||||
&blob_file_number, &blob_offset, &blob_size, &settings);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
used_managers_.insert(cached_partition_mgr_);
|
||||
|
||||
// Track the exact file so stale transformed attempts can rollback
|
||||
// per-file rather than smearing bytes across all partitions at seal time.
|
||||
const uint64_t record_bytes =
|
||||
BlobLogRecord::kHeaderSize + key.size() + blob_size;
|
||||
rollback_infos_.push_back(
|
||||
{cached_partition_mgr_, blob_file_number, /*count=*/1, record_bytes});
|
||||
|
||||
BlobIndex::EncodeBlob(&blob_index_buf_, blob_file_number, blob_offset,
|
||||
blob_size, settings.compression_type);
|
||||
|
||||
has_transformed_ = true;
|
||||
return WriteBatchInternal::PutBlobIndex(output_batch_, column_family_id, key,
|
||||
blob_index_buf_);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::TimedPutCF(uint32_t column_family_id,
|
||||
const Slice& key,
|
||||
const Slice& value,
|
||||
uint64_t write_time) {
|
||||
// TimedPut: pass through without blob separation for now.
|
||||
return WriteBatchInternal::TimedPut(output_batch_, column_family_id, key,
|
||||
value, write_time);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::PutEntityCF(uint32_t column_family_id,
|
||||
const Slice& key,
|
||||
const Slice& entity) {
|
||||
if (column_family_id != cached_cf_id_) {
|
||||
cached_settings_ = settings_provider_(column_family_id);
|
||||
cached_partition_mgr_ = partition_mgr_provider_(column_family_id);
|
||||
cached_cf_id_ = column_family_id;
|
||||
}
|
||||
const auto& settings = cached_settings_;
|
||||
|
||||
if (!cached_partition_mgr_ || !settings.enable_blob_direct_write) {
|
||||
return WriteBatchInternal::PutEntitySerialized(
|
||||
output_batch_, column_family_id, key, entity);
|
||||
}
|
||||
|
||||
Slice entity_ref = entity;
|
||||
std::vector<WideColumn> columns;
|
||||
std::vector<std::pair<size_t, BlobIndex>> existing_blob_columns;
|
||||
Status status = WideColumnSerialization::DeserializeV2(entity_ref, columns,
|
||||
existing_blob_columns);
|
||||
if (status.ok()) {
|
||||
// Reject pre-serialized entities that already contain blob references.
|
||||
// Passing them through would preserve references to blob files outside this
|
||||
// batch's direct-write lifetime tracking, which can later turn into stale
|
||||
// reads after blob GC.
|
||||
if (UNLIKELY(!existing_blob_columns.empty())) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write does not support pre-serialized wide entities "
|
||||
"with blob references");
|
||||
}
|
||||
|
||||
std::string rewritten_entity;
|
||||
bool transformed = false;
|
||||
status = MaybePreprocessWideColumns(
|
||||
write_options_, column_family_id, key, columns, cached_partition_mgr_,
|
||||
settings, /*serialize_inline_entity=*/false, &rewritten_entity,
|
||||
&transformed, &rollback_infos_);
|
||||
if (status.ok()) {
|
||||
if (!transformed) {
|
||||
return WriteBatchInternal::PutEntitySerialized(
|
||||
output_batch_, column_family_id, key, entity);
|
||||
}
|
||||
|
||||
has_transformed_ = true;
|
||||
used_managers_.insert(cached_partition_mgr_);
|
||||
return WriteBatchInternal::PutEntitySerialized(
|
||||
output_batch_, column_family_id, key, rewritten_entity);
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::DeleteCF(uint32_t column_family_id,
|
||||
const Slice& key) {
|
||||
return WriteBatchInternal::Delete(output_batch_, column_family_id, key);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::SingleDeleteCF(uint32_t column_family_id,
|
||||
const Slice& key) {
|
||||
return WriteBatchInternal::SingleDelete(output_batch_, column_family_id, key);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::DeleteRangeCF(uint32_t column_family_id,
|
||||
const Slice& begin_key,
|
||||
const Slice& end_key) {
|
||||
return WriteBatchInternal::DeleteRange(output_batch_, column_family_id,
|
||||
begin_key, end_key);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MergeCF(uint32_t column_family_id,
|
||||
const Slice& key,
|
||||
const Slice& value) {
|
||||
return WriteBatchInternal::Merge(output_batch_, column_family_id, key, value);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::PutBlobIndexCF(uint32_t column_family_id,
|
||||
const Slice& key,
|
||||
const Slice& value) {
|
||||
// Already a blob index — pass through unchanged.
|
||||
return WriteBatchInternal::PutBlobIndex(output_batch_, column_family_id, key,
|
||||
value);
|
||||
}
|
||||
|
||||
void BlobWriteBatchTransformer::LogData(const Slice& blob) {
|
||||
output_batch_->PutLogData(blob).PermitUncheckedError();
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MarkBeginPrepare(bool unprepared) {
|
||||
return WriteBatchInternal::InsertBeginPrepare(
|
||||
output_batch_, !unprepared /* write_after_commit */, unprepared);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MarkEndPrepare(const Slice& xid) {
|
||||
return WriteBatchInternal::InsertEndPrepare(output_batch_, xid);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MarkCommit(const Slice& xid) {
|
||||
return WriteBatchInternal::MarkCommit(output_batch_, xid);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MarkCommitWithTimestamp(const Slice& xid,
|
||||
const Slice& ts) {
|
||||
return WriteBatchInternal::MarkCommitWithTimestamp(output_batch_, xid, ts);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MarkRollback(const Slice& xid) {
|
||||
return WriteBatchInternal::MarkRollback(output_batch_, xid);
|
||||
}
|
||||
|
||||
Status BlobWriteBatchTransformer::MarkNoop(bool /*empty_batch*/) {
|
||||
return WriteBatchInternal::InsertNoop(output_batch_);
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -0,0 +1,174 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "rocksdb/advanced_options.h"
|
||||
#include "rocksdb/compression_type.h"
|
||||
#include "rocksdb/options.h"
|
||||
#include "rocksdb/rocksdb_namespace.h"
|
||||
#include "rocksdb/slice.h"
|
||||
#include "rocksdb/status.h"
|
||||
#include "rocksdb/write_batch.h"
|
||||
#include "util/hash_containers.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
class BlobFilePartitionManager;
|
||||
class Cache;
|
||||
|
||||
// Callback to look up per-CF blob settings.
|
||||
struct BlobDirectWriteSettings {
|
||||
// Whether this column family should attempt direct-write blob separation.
|
||||
bool enable_blob_direct_write = false;
|
||||
// Minimum value size eligible for direct-write blob separation.
|
||||
uint64_t min_blob_size = 0;
|
||||
// Compression to use for newly written blob records.
|
||||
CompressionType compression_type = kNoCompression;
|
||||
// Raw pointer — the Cache is owned by ColumnFamilyOptions and outlives all
|
||||
// settings snapshots. Using raw avoids 2 atomic ref-count ops per Put().
|
||||
Cache* blob_cache = nullptr;
|
||||
// Blob-cache prepopulation policy for direct-write records.
|
||||
PrepopulateBlobCache prepopulate_blob_cache = PrepopulateBlobCache::kDisable;
|
||||
};
|
||||
|
||||
using BlobDirectWriteSettingsProvider =
|
||||
std::function<BlobDirectWriteSettings(uint32_t column_family_id)>;
|
||||
|
||||
// Callback to look up per-CF partition manager.
|
||||
using BlobPartitionManagerProvider =
|
||||
std::function<BlobFilePartitionManager*(uint32_t column_family_id)>;
|
||||
|
||||
// Transforms a WriteBatch by writing large values directly to blob files
|
||||
// and replacing them with BlobIndex entries. Non-qualifying entries
|
||||
// (small values, deletes, merges, etc.) are passed through unchanged.
|
||||
class BlobWriteBatchTransformer : public WriteBatch::Handler {
|
||||
public:
|
||||
struct RollbackInfo {
|
||||
// Manager that owns the file updated during transform.
|
||||
BlobFilePartitionManager* partition_mgr = nullptr;
|
||||
// Blob file number written during transform.
|
||||
uint64_t file_number = 0;
|
||||
// Number of blob records appended to that file.
|
||||
uint64_t count = 0;
|
||||
// Number of bytes appended to that file.
|
||||
uint64_t bytes = 0;
|
||||
};
|
||||
|
||||
// Builds the final serialized entity for one PutEntity write.
|
||||
//
|
||||
// `columns` must already be sorted by column name. When direct-write blob
|
||||
// separation is enabled and one or more columns meet `min_blob_size`, this
|
||||
// writes those columns to blob files and returns a V2 entity containing
|
||||
// BlobIndex references. Otherwise, if `serialize_inline_entity` is true, it
|
||||
// returns a normal V1 entity with all columns inline.
|
||||
//
|
||||
// `rollback_infos`, when provided, is appended with the exact blob writes
|
||||
// performed before the method returns, even if it later fails, so callers
|
||||
// can account those bytes as initial garbage.
|
||||
static Status MaybePreprocessWideColumns(
|
||||
const WriteOptions& write_options, uint32_t column_family_id,
|
||||
const Slice& key, const WideColumns& columns,
|
||||
BlobFilePartitionManager* partition_mgr,
|
||||
const BlobDirectWriteSettings& settings, bool serialize_inline_entity,
|
||||
std::string* entity, bool* transformed,
|
||||
std::vector<RollbackInfo>* rollback_infos = nullptr);
|
||||
|
||||
// Constructs a per-batch transformer. Most callers should use
|
||||
// TransformBatch() instead of driving the handler directly.
|
||||
BlobWriteBatchTransformer(
|
||||
const BlobPartitionManagerProvider& partition_mgr_provider,
|
||||
WriteBatch* output_batch,
|
||||
const BlobDirectWriteSettingsProvider& settings_provider,
|
||||
const WriteOptions& write_options);
|
||||
|
||||
// Transform a WriteBatch. If no values qualify for blob separation,
|
||||
// output_batch will be empty and the caller should use the original batch.
|
||||
// If any values are separated, output_batch contains the full transformed
|
||||
// batch. used_managers (if non-null) receives the set of partition managers
|
||||
// that had data written to them, so the caller can flush/sync them.
|
||||
// rollback_infos (if non-null) receives the exact file/count/byte writes so
|
||||
// a failed transformed attempt can be accounted as initial garbage. Partial
|
||||
// results are returned even if this method fails after some blob writes.
|
||||
static Status TransformBatch(
|
||||
const WriteOptions& write_options, WriteBatch* input_batch,
|
||||
WriteBatch* output_batch,
|
||||
const BlobPartitionManagerProvider& partition_mgr_provider,
|
||||
const BlobDirectWriteSettingsProvider& settings_provider,
|
||||
bool* transformed,
|
||||
std::vector<BlobFilePartitionManager*>* used_managers = nullptr,
|
||||
std::vector<RollbackInfo>* rollback_infos = nullptr);
|
||||
|
||||
// WriteBatch::Handler overrides
|
||||
Status PutCF(uint32_t column_family_id, const Slice& key,
|
||||
const Slice& value) override;
|
||||
|
||||
Status TimedPutCF(uint32_t column_family_id, const Slice& key,
|
||||
const Slice& value, uint64_t write_time) override;
|
||||
|
||||
Status PutEntityCF(uint32_t column_family_id, const Slice& key,
|
||||
const Slice& entity) override;
|
||||
|
||||
Status DeleteCF(uint32_t column_family_id, const Slice& key) override;
|
||||
|
||||
Status SingleDeleteCF(uint32_t column_family_id, const Slice& key) override;
|
||||
|
||||
Status DeleteRangeCF(uint32_t column_family_id, const Slice& begin_key,
|
||||
const Slice& end_key) override;
|
||||
|
||||
Status MergeCF(uint32_t column_family_id, const Slice& key,
|
||||
const Slice& value) override;
|
||||
|
||||
Status PutBlobIndexCF(uint32_t column_family_id, const Slice& key,
|
||||
const Slice& value) override;
|
||||
|
||||
void LogData(const Slice& blob) override;
|
||||
|
||||
Status MarkBeginPrepare(bool unprepared = false) override;
|
||||
Status MarkEndPrepare(const Slice& xid) override;
|
||||
Status MarkCommit(const Slice& xid) override;
|
||||
Status MarkCommitWithTimestamp(const Slice& xid, const Slice& ts) override;
|
||||
Status MarkRollback(const Slice& xid) override;
|
||||
Status MarkNoop(bool empty_batch) override;
|
||||
|
||||
// Returns true once at least one value has been rewritten to a BlobIndex.
|
||||
bool HasTransformed() const { return has_transformed_; }
|
||||
|
||||
private:
|
||||
// Callback to look up the partition manager for a given column family ID.
|
||||
BlobPartitionManagerProvider partition_mgr_provider_;
|
||||
// Output batch that receives transformed entries (BlobIndex for qualifying
|
||||
// values, passthrough for everything else).
|
||||
WriteBatch* output_batch_;
|
||||
// Callback to look up blob direct write settings for a given CF ID.
|
||||
BlobDirectWriteSettingsProvider settings_provider_;
|
||||
// Write options from the caller, forwarded to WriteBlob calls.
|
||||
const WriteOptions& write_options_;
|
||||
// True once at least one value has been separated into a blob file.
|
||||
bool has_transformed_ = false;
|
||||
// Reusable buffer for encoding BlobIndex entries (avoids per-Put alloc).
|
||||
std::string blob_index_buf_;
|
||||
// Per-batch cache of the last CF's settings and manager, avoiding
|
||||
// redundant provider lookups when consecutive entries share the same CF.
|
||||
uint32_t cached_cf_id_ = UINT32_MAX;
|
||||
// Cached settings for `cached_cf_id_`.
|
||||
BlobDirectWriteSettings cached_settings_;
|
||||
// Cached partition manager for `cached_cf_id_`.
|
||||
BlobFilePartitionManager* cached_partition_mgr_ = nullptr;
|
||||
// Set of partition managers that received data during this batch,
|
||||
// returned to the caller so it can flush/sync them.
|
||||
UnorderedSet<BlobFilePartitionManager*> used_managers_;
|
||||
// Exact blob writes performed during this batch. We only aggregate these
|
||||
// entries if rollback is needed so the normal path keeps minimal overhead.
|
||||
std::vector<RollbackInfo> rollback_infos_;
|
||||
};
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -3,17 +3,29 @@
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "cache/compressed_secondary_cache.h"
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/blob/blob_log_format.h"
|
||||
#include "db/blob/blob_log_sequential_reader.h"
|
||||
#include "db/column_family.h"
|
||||
#include "db/db_test_util.h"
|
||||
#include "db/db_with_timestamp_test_util.h"
|
||||
#include "file/filename.h"
|
||||
#include "file/random_access_file_reader.h"
|
||||
#include "port/stack_trace.h"
|
||||
#include "rocksdb/convenience.h"
|
||||
#include "rocksdb/trace_reader_writer.h"
|
||||
#include "rocksdb/trace_record.h"
|
||||
#include "rocksdb/utilities/replayer.h"
|
||||
#include "test_util/sync_point.h"
|
||||
#include "util/compression.h"
|
||||
#include "utilities/fault_injection_env.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
@@ -51,6 +63,43 @@ TEST_F(DBBlobBasicTest, GetBlob) {
|
||||
.IsIncomplete());
|
||||
}
|
||||
|
||||
TEST_F(DBBlobBasicTest, EmptyValueNotStoredAsBlob) {
|
||||
// Regression test for crash when empty blob value is evicted to
|
||||
// CompressedSecondaryCache (T261142690). Empty values should always be
|
||||
// stored inline in the SST, never as blobs, even with min_blob_size=0.
|
||||
// A BlobIndex for an empty value is strictly larger than the value itself,
|
||||
// so storing it as a blob is pure overhead.
|
||||
Options options = GetDefaultOptions();
|
||||
options.enable_blob_files = true;
|
||||
options.min_blob_size = 0;
|
||||
options.disable_auto_compactions = true;
|
||||
|
||||
Reopen(options);
|
||||
|
||||
// Write an empty value and a non-empty value.
|
||||
ASSERT_OK(Put("empty_key", ""));
|
||||
constexpr char blob_value[] = "blob_value";
|
||||
ASSERT_OK(Put("nonempty_key", blob_value));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Both values should be readable.
|
||||
ASSERT_EQ(Get("empty_key"), "");
|
||||
ASSERT_EQ(Get("nonempty_key"), blob_value);
|
||||
|
||||
// The empty value should be stored inline (readable from block cache
|
||||
// without blob file I/O), while the non-empty value requires blob I/O.
|
||||
ReadOptions ro;
|
||||
ro.read_tier = kBlockCacheTier;
|
||||
|
||||
PinnableSlice result;
|
||||
ASSERT_OK(db_->Get(ro, db_->DefaultColumnFamily(), "empty_key", &result));
|
||||
ASSERT_EQ(result, "");
|
||||
|
||||
result.Reset();
|
||||
ASSERT_TRUE(db_->Get(ro, db_->DefaultColumnFamily(), "nonempty_key", &result)
|
||||
.IsIncomplete());
|
||||
}
|
||||
|
||||
TEST_F(DBBlobBasicTest, GetBlobFromCache) {
|
||||
Options options = GetDefaultOptions();
|
||||
|
||||
@@ -2459,6 +2508,138 @@ TEST_F(DBBlobWithTimestampTest, IterateBlobs) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DBBlobBasicTest, GetApproximateSizesIncludingBlobFiles) {
|
||||
Options options = GetDefaultOptions();
|
||||
options.enable_blob_files = true;
|
||||
options.min_blob_size = 0;
|
||||
|
||||
Reopen(options);
|
||||
|
||||
// Write some key-value pairs with blob values and flush to create blob files.
|
||||
constexpr int kNumKeys = 1000;
|
||||
constexpr int kValueSize = 1024;
|
||||
|
||||
Random rnd(301);
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
ASSERT_OK(Put(Key(i), rnd.RandomString(kValueSize)));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Verify blob files exist.
|
||||
std::vector<std::string> files;
|
||||
ASSERT_OK(env_->GetChildren(dbname_, &files));
|
||||
bool has_blob_files = false;
|
||||
for (const auto& f : files) {
|
||||
if (f.size() > 5 && f.substr(f.size() - 5) == ".blob") {
|
||||
has_blob_files = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_TRUE(has_blob_files);
|
||||
|
||||
// Query the full range - all keys are covered.
|
||||
std::string start = Key(0);
|
||||
std::string end = Key(kNumKeys);
|
||||
Range r(start, end);
|
||||
|
||||
// Without include_blob_files (default behavior): should not include blob
|
||||
// file sizes.
|
||||
uint64_t size_without_blobs = 0;
|
||||
{
|
||||
SizeApproximationOptions size_approx_options;
|
||||
size_approx_options.include_files = true;
|
||||
size_approx_options.include_blob_files = false;
|
||||
ASSERT_OK(db_->GetApproximateSizes(size_approx_options,
|
||||
db_->DefaultColumnFamily(), &r, 1,
|
||||
&size_without_blobs));
|
||||
ASSERT_GT(size_without_blobs, 0);
|
||||
}
|
||||
|
||||
// With include_blob_files: should be strictly larger.
|
||||
{
|
||||
SizeApproximationOptions size_approx_options;
|
||||
size_approx_options.include_files = true;
|
||||
size_approx_options.include_blob_files = true;
|
||||
uint64_t size_with_blobs = 0;
|
||||
ASSERT_OK(db_->GetApproximateSizes(size_approx_options,
|
||||
db_->DefaultColumnFamily(), &r, 1,
|
||||
&size_with_blobs));
|
||||
ASSERT_GT(size_with_blobs, size_without_blobs);
|
||||
}
|
||||
|
||||
// Range that doesn't overlap any data should return 0.
|
||||
{
|
||||
std::string no_start = Key(kNumKeys + 100);
|
||||
std::string no_end = Key(kNumKeys + 200);
|
||||
Range no_r(no_start, no_end);
|
||||
SizeApproximationOptions size_approx_options;
|
||||
size_approx_options.include_files = true;
|
||||
size_approx_options.include_blob_files = true;
|
||||
uint64_t no_size = 0;
|
||||
ASSERT_OK(db_->GetApproximateSizes(
|
||||
size_approx_options, db_->DefaultColumnFamily(), &no_r, 1, &no_size));
|
||||
ASSERT_EQ(no_size, 0);
|
||||
}
|
||||
|
||||
// Partial range should return proportionally less blob size than full range.
|
||||
{
|
||||
SizeApproximationOptions size_approx_options;
|
||||
size_approx_options.include_files = true;
|
||||
size_approx_options.include_blob_files = true;
|
||||
|
||||
uint64_t full_size = 0;
|
||||
ASSERT_OK(db_->GetApproximateSizes(
|
||||
size_approx_options, db_->DefaultColumnFamily(), &r, 1, &full_size));
|
||||
|
||||
// Query roughly the first half of keys.
|
||||
std::string half_end = Key(kNumKeys / 2);
|
||||
Range half_r(start, half_end);
|
||||
uint64_t half_size = 0;
|
||||
ASSERT_OK(db_->GetApproximateSizes(size_approx_options,
|
||||
db_->DefaultColumnFamily(), &half_r, 1,
|
||||
&half_size));
|
||||
ASSERT_GT(half_size, 0);
|
||||
ASSERT_LT(half_size, full_size);
|
||||
}
|
||||
|
||||
// Via SizeApproximationFlags API.
|
||||
{
|
||||
uint64_t size_flags = 0;
|
||||
ASSERT_OK(db_->GetApproximateSizes(
|
||||
db_->DefaultColumnFamily(), &r, 1, &size_flags,
|
||||
DB::SizeApproximationFlags::INCLUDE_FILES |
|
||||
DB::SizeApproximationFlags::INCLUDE_BLOB_FILES));
|
||||
ASSERT_GT(size_flags, size_without_blobs);
|
||||
}
|
||||
|
||||
// Multi-range query: two non-overlapping sub-ranges should sum to
|
||||
// approximately the full-range result.
|
||||
{
|
||||
SizeApproximationOptions size_approx_options;
|
||||
size_approx_options.include_files = true;
|
||||
size_approx_options.include_blob_files = true;
|
||||
|
||||
std::string mid = Key(kNumKeys / 2);
|
||||
std::string r1_start = Key(0);
|
||||
std::string r1_end = mid;
|
||||
std::string r2_start = mid;
|
||||
std::string r2_end = Key(kNumKeys);
|
||||
Range ranges[2] = {Range(r1_start, r1_end), Range(r2_start, r2_end)};
|
||||
uint64_t sizes[2] = {0, 0};
|
||||
ASSERT_OK(db_->GetApproximateSizes(
|
||||
size_approx_options, db_->DefaultColumnFamily(), ranges, 2, sizes));
|
||||
// Each sub-range should return a positive size.
|
||||
ASSERT_GT(sizes[0], 0);
|
||||
ASSERT_GT(sizes[1], 0);
|
||||
// Sum of sub-ranges should be close to the full-range result.
|
||||
uint64_t full_size = 0;
|
||||
ASSERT_OK(db_->GetApproximateSizes(
|
||||
size_approx_options, db_->DefaultColumnFamily(), &r, 1, &full_size));
|
||||
ASSERT_NEAR(static_cast<double>(sizes[0] + sizes[1]),
|
||||
static_cast<double>(full_size), full_size * 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+64
-8
@@ -13,7 +13,10 @@
|
||||
#include <deque>
|
||||
#include <vector>
|
||||
|
||||
#include "db/blob/blob_counting_iterator.h"
|
||||
#include "db/blob/blob_file_builder.h"
|
||||
#include "db/blob/blob_garbage_meter.h"
|
||||
#include "db/column_family.h"
|
||||
#include "db/compaction/compaction_iterator.h"
|
||||
#include "db/dbformat.h"
|
||||
#include "db/event_helpers.h"
|
||||
@@ -87,7 +90,8 @@ Status BuildTable(
|
||||
Env::WriteLifeTimeHint write_hint, const std::string* full_history_ts_low,
|
||||
BlobFileCompletionCallback* blob_callback, Version* version,
|
||||
uint64_t* memtable_payload_bytes, uint64_t* memtable_garbage_bytes,
|
||||
InternalStats::CompactionStats* flush_stats) {
|
||||
InternalStats::CompactionStats* flush_stats,
|
||||
std::vector<BlobFileGarbage>* blob_file_garbages, bool fast_sst_open) {
|
||||
assert((tboptions.column_family_id ==
|
||||
TablePropertiesCollectorFactory::Context::kUnknownColumnFamily) ==
|
||||
tboptions.column_family_name.empty());
|
||||
@@ -99,7 +103,24 @@ Status BuildTable(
|
||||
/*enable_hash=*/paranoid_file_checks);
|
||||
Status s;
|
||||
meta->fd.file_size = 0;
|
||||
iter->SeekToFirst();
|
||||
if (blob_file_garbages != nullptr) {
|
||||
blob_file_garbages->clear();
|
||||
}
|
||||
|
||||
InternalIterator* input = iter;
|
||||
std::unique_ptr<BlobGarbageMeter> blob_garbage_meter;
|
||||
std::unique_ptr<BlobCountingIterator> blob_counting_iter;
|
||||
if (blob_file_garbages != nullptr) {
|
||||
// Flush can drop blob-backed records via overwrite elision and flush-time
|
||||
// compaction filters. Track input and surviving output refs just like the
|
||||
// compaction path so the manifest learns about the new garbage.
|
||||
blob_garbage_meter.reset(new BlobGarbageMeter());
|
||||
blob_counting_iter.reset(
|
||||
new BlobCountingIterator(iter, blob_garbage_meter.get()));
|
||||
input = blob_counting_iter.get();
|
||||
}
|
||||
|
||||
input->SeekToFirst();
|
||||
std::unique_ptr<CompactionRangeDelAggregator> range_del_agg(
|
||||
new CompactionRangeDelAggregator(&tboptions.internal_comparator,
|
||||
snapshots, full_history_ts_low));
|
||||
@@ -128,7 +149,7 @@ Status BuildTable(
|
||||
|
||||
TableProperties tp;
|
||||
bool table_file_created = false;
|
||||
if (iter->Valid() || !range_del_agg->IsEmpty()) {
|
||||
if (input->Valid() || !range_del_agg->IsEmpty()) {
|
||||
std::unique_ptr<CompactionFilter> compaction_filter;
|
||||
if (ioptions.compaction_filter_factory != nullptr &&
|
||||
ioptions.compaction_filter_factory->ShouldFilterTableFileCreation(
|
||||
@@ -211,7 +232,7 @@ Status BuildTable(
|
||||
|
||||
const std::atomic<bool> kManualCompactionCanceledFalse{false};
|
||||
CompactionIterator c_iter(
|
||||
iter, ucmp, &merge, kMaxSequenceNumber, &snapshots, earliest_snapshot,
|
||||
input, ucmp, &merge, kMaxSequenceNumber, &snapshots, earliest_snapshot,
|
||||
earliest_write_conflict_snapshot, job_snapshot, snapshot_checker, env,
|
||||
ShouldReportDetailedTime(env, ioptions.stats), range_del_agg.get(),
|
||||
blob_file_builder.get(), ioptions.allow_data_in_errors,
|
||||
@@ -219,7 +240,17 @@ Status BuildTable(
|
||||
/*manual_compaction_canceled=*/kManualCompactionCanceledFalse,
|
||||
true /* must_count_input_entries */,
|
||||
/*compaction=*/nullptr, compaction_filter.get(),
|
||||
/*shutting_down=*/nullptr, db_options.info_log, full_history_ts_low);
|
||||
/*shutting_down=*/nullptr, db_options.info_log, full_history_ts_low,
|
||||
std::nullopt, version, tboptions.read_options.io_activity);
|
||||
|
||||
if (version != nullptr) {
|
||||
ColumnFamilyData* const cfd = version->cfd();
|
||||
c_iter.SetBlobFetcher(
|
||||
version, cfd != nullptr ? cfd->blob_file_cache() : nullptr,
|
||||
tboptions.read_options.io_activity,
|
||||
tboptions.reason == TableFileCreationReason::kFlush ||
|
||||
tboptions.reason == TableFileCreationReason::kRecovery);
|
||||
}
|
||||
|
||||
SequenceNumber smallest_preferred_seqno = kMaxSequenceNumber;
|
||||
std::string key_after_flush_buf;
|
||||
@@ -268,6 +299,14 @@ Status BuildTable(
|
||||
flush_stats->num_output_records++;
|
||||
}
|
||||
|
||||
if (blob_garbage_meter != nullptr) {
|
||||
s = blob_garbage_meter->ProcessOutFlow(key_after_flush,
|
||||
value_after_flush);
|
||||
if (!s.ok()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
s = meta->UpdateBoundaries(key_after_flush, value_after_flush,
|
||||
ikey.sequence, ikey.type);
|
||||
if (!s.ok()) {
|
||||
@@ -457,6 +496,8 @@ Status BuildTable(
|
||||
// here because this is a special case after we finish the table building.
|
||||
// No matter whether use_direct_io_for_flush_and_compaction is true,
|
||||
// the goal is to cache it here for further user reads.
|
||||
std::string* file_open_metadata_ptr =
|
||||
fast_sst_open ? &meta->file_open_metadata : nullptr;
|
||||
std::unique_ptr<InternalIterator> it(table_cache->NewIterator(
|
||||
tboptions.read_options, file_options, tboptions.internal_comparator,
|
||||
*meta, nullptr /* range_del_agg */, mutable_cf_options, nullptr,
|
||||
@@ -467,7 +508,10 @@ Status BuildTable(
|
||||
MaxFileSizeForL0MetaPin(mutable_cf_options),
|
||||
/*smallest_compaction_key=*/nullptr,
|
||||
/*largest_compaction_key*/ nullptr,
|
||||
/*allow_unprepared_value*/ false));
|
||||
/*allow_unprepared_value*/ false,
|
||||
/*range_del_read_seqno=*/nullptr,
|
||||
/*range_del_iter=*/nullptr,
|
||||
/*maybe_pin_table_handle=*/false, file_open_metadata_ptr));
|
||||
s = it->status();
|
||||
if (s.ok() && paranoid_file_checks) {
|
||||
OutputValidator file_validator(tboptions.internal_comparator,
|
||||
@@ -485,8 +529,20 @@ Status BuildTable(
|
||||
}
|
||||
|
||||
// Check for input iterator errors
|
||||
if (!iter->status().ok()) {
|
||||
s = iter->status();
|
||||
if (!input->status().ok()) {
|
||||
s = input->status();
|
||||
}
|
||||
|
||||
if (s.ok() && blob_file_garbages != nullptr &&
|
||||
blob_garbage_meter != nullptr) {
|
||||
for (const auto& pair : blob_garbage_meter->flows()) {
|
||||
const uint64_t blob_file_number = pair.first;
|
||||
const BlobGarbageMeter::BlobInOutFlow& flow = pair.second;
|
||||
if (flow.HasGarbage()) {
|
||||
blob_file_garbages->emplace_back(
|
||||
blob_file_number, flow.GetGarbageCount(), flow.GetGarbageBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!s.ok() || meta->fd.GetFileSize() == 0) {
|
||||
|
||||
+4
-1
@@ -31,6 +31,7 @@ struct FileMetaData;
|
||||
|
||||
class VersionSet;
|
||||
class BlobFileAddition;
|
||||
class BlobFileGarbage;
|
||||
class SnapshotChecker;
|
||||
class TableCache;
|
||||
class TableBuilder;
|
||||
@@ -79,6 +80,8 @@ Status BuildTable(
|
||||
BlobFileCompletionCallback* blob_callback = nullptr,
|
||||
Version* version = nullptr, uint64_t* memtable_payload_bytes = nullptr,
|
||||
uint64_t* memtable_garbage_bytes = nullptr,
|
||||
InternalStats::CompactionStats* flush_stats = nullptr);
|
||||
InternalStats::CompactionStats* flush_stats = nullptr,
|
||||
std::vector<BlobFileGarbage>* blob_file_garbages = nullptr,
|
||||
bool fast_sst_open = false);
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -3056,6 +3056,7 @@ class H : public WriteBatch::Handler {
|
||||
(*log_data_)(state_, blob.data(), blob.size());
|
||||
}
|
||||
}
|
||||
Status MarkNoop(bool /* empty_batch */) override { return Status::OK(); }
|
||||
};
|
||||
|
||||
class HCF : public WriteBatch::Handler {
|
||||
@@ -3089,6 +3090,7 @@ class HCF : public WriteBatch::Handler {
|
||||
(*log_data_)(state_, blob.data(), blob.size());
|
||||
}
|
||||
}
|
||||
Status MarkNoop(bool /* empty_batch */) override { return Status::OK(); }
|
||||
};
|
||||
|
||||
void rocksdb_writebatch_iterate(rocksdb_writebatch_t* b, void* state,
|
||||
@@ -4648,6 +4650,16 @@ uint32_t rocksdb_options_get_memtable_avg_op_scan_flush_trigger(
|
||||
return opt->rep.memtable_avg_op_scan_flush_trigger;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_min_tombstones_for_range_conversion(
|
||||
rocksdb_options_t* opt, uint32_t n) {
|
||||
opt->rep.min_tombstones_for_range_conversion = n;
|
||||
}
|
||||
|
||||
uint32_t rocksdb_options_get_min_tombstones_for_range_conversion(
|
||||
rocksdb_options_t* opt) {
|
||||
return opt->rep.min_tombstones_for_range_conversion;
|
||||
}
|
||||
|
||||
void rocksdb_options_enable_statistics(rocksdb_options_t* opt) {
|
||||
opt->rep.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
||||
}
|
||||
@@ -4747,6 +4759,26 @@ double rocksdb_options_get_blob_gc_force_threshold(rocksdb_options_t* opt) {
|
||||
return opt->rep.blob_garbage_collection_force_threshold;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_read_triggered_compaction_threshold(
|
||||
rocksdb_options_t* opt, double val) {
|
||||
opt->rep.read_triggered_compaction_threshold = val;
|
||||
}
|
||||
|
||||
double rocksdb_options_get_read_triggered_compaction_threshold(
|
||||
rocksdb_options_t* opt) {
|
||||
return opt->rep.read_triggered_compaction_threshold;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_max_compaction_trigger_wakeup_seconds(
|
||||
rocksdb_options_t* opt, uint64_t val) {
|
||||
opt->rep.max_compaction_trigger_wakeup_seconds = val;
|
||||
}
|
||||
|
||||
uint64_t rocksdb_options_get_max_compaction_trigger_wakeup_seconds(
|
||||
rocksdb_options_t* opt) {
|
||||
return opt->rep.max_compaction_trigger_wakeup_seconds;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_blob_compaction_readahead_size(rocksdb_options_t* opt,
|
||||
uint64_t val) {
|
||||
opt->rep.blob_compaction_readahead_size = val;
|
||||
@@ -8284,6 +8316,11 @@ void rocksdb_transaction_delete_cf(
|
||||
SaveError(errptr, txn->rep->Delete(column_family->rep, Slice(key, klen)));
|
||||
}
|
||||
|
||||
void rocksdb_transaction_put_log_data(rocksdb_transaction_t* txn,
|
||||
const char* blob, size_t len) {
|
||||
txn->rep->PutLogData(Slice(blob, len));
|
||||
}
|
||||
|
||||
// Delete a key outside a transaction
|
||||
void rocksdb_transactiondb_delete(rocksdb_transactiondb_t* txn_db,
|
||||
const rocksdb_writeoptions_t* options,
|
||||
|
||||
+64
@@ -205,6 +205,27 @@ static void CheckDel(void* ptr, const char* k, size_t klen) {
|
||||
(*state)++;
|
||||
}
|
||||
|
||||
static void NopPut(void* ptr, const char* k, size_t klen, const char* v,
|
||||
size_t vlen) {
|
||||
(void)ptr;
|
||||
(void)k;
|
||||
(void)klen;
|
||||
(void)v;
|
||||
(void)vlen;
|
||||
}
|
||||
|
||||
static void NopDel(void* ptr, const char* k, size_t klen) {
|
||||
(void)ptr;
|
||||
(void)k;
|
||||
(void)klen;
|
||||
}
|
||||
|
||||
static void CheckLogData(void* ptr, const char* blob, size_t blen) {
|
||||
CheckEqual("log_blob", blob, blen);
|
||||
int* found = (int*)ptr;
|
||||
*found = 1;
|
||||
}
|
||||
|
||||
// Callback from rocksdb_writebatch_iterate_cf()
|
||||
static void CheckPutCF(void* ptr, uint32_t cfid, const char* k, size_t klen,
|
||||
const char* v, size_t vlen) {
|
||||
@@ -2643,6 +2664,10 @@ int main(int argc, char** argv) {
|
||||
CheckCondition(150 ==
|
||||
rocksdb_options_get_memtable_avg_op_scan_flush_trigger(o));
|
||||
|
||||
rocksdb_options_set_min_tombstones_for_range_conversion(o, 200);
|
||||
CheckCondition(200 ==
|
||||
rocksdb_options_get_min_tombstones_for_range_conversion(o));
|
||||
|
||||
rocksdb_options_set_ttl(o, 5000);
|
||||
CheckCondition(5000 == rocksdb_options_get_ttl(o));
|
||||
|
||||
@@ -2867,6 +2892,14 @@ int main(int argc, char** argv) {
|
||||
rocksdb_options_set_blob_gc_force_threshold(o, 0.75);
|
||||
CheckCondition(0.75 == rocksdb_options_get_blob_gc_force_threshold(o));
|
||||
|
||||
rocksdb_options_set_read_triggered_compaction_threshold(o, 0.001);
|
||||
CheckCondition(0.001 ==
|
||||
rocksdb_options_get_read_triggered_compaction_threshold(o));
|
||||
|
||||
rocksdb_options_set_max_compaction_trigger_wakeup_seconds(o, 3600);
|
||||
CheckCondition(
|
||||
3600 == rocksdb_options_get_max_compaction_trigger_wakeup_seconds(o));
|
||||
|
||||
rocksdb_options_set_blob_compaction_readahead_size(o, 262144);
|
||||
CheckCondition(262144 ==
|
||||
rocksdb_options_get_blob_compaction_readahead_size(o));
|
||||
@@ -3086,6 +3119,12 @@ int main(int argc, char** argv) {
|
||||
CheckCondition(150 ==
|
||||
rocksdb_options_get_memtable_avg_op_scan_flush_trigger(o));
|
||||
|
||||
rocksdb_options_set_min_tombstones_for_range_conversion(copy, 1000);
|
||||
CheckCondition(
|
||||
1000 == rocksdb_options_get_min_tombstones_for_range_conversion(copy));
|
||||
CheckCondition(200 ==
|
||||
rocksdb_options_get_min_tombstones_for_range_conversion(o));
|
||||
|
||||
rocksdb_options_set_ttl(copy, 8000);
|
||||
CheckCondition(8000 == rocksdb_options_get_ttl(copy));
|
||||
CheckCondition(5000 == rocksdb_options_get_ttl(o));
|
||||
@@ -3850,10 +3889,35 @@ int main(int argc, char** argv) {
|
||||
CheckMultiGetValues(3, vals, vals_sizes, errs, expected);
|
||||
}
|
||||
|
||||
rocksdb_transaction_put_log_data(txn, "log_blob", 8);
|
||||
// record sequence number before commit so we can scan the WAL after
|
||||
rocksdb_t* base_db_ld = rocksdb_transactiondb_get_base_db(txn_db);
|
||||
uint64_t seq_before = rocksdb_get_latest_sequence_number(base_db_ld);
|
||||
|
||||
// commit
|
||||
rocksdb_transaction_commit(txn, &err);
|
||||
CheckNoError(err);
|
||||
|
||||
// verify log data was written to WAL by scanning batches since seq_before
|
||||
{
|
||||
rocksdb_wal_iterator_t* wal_iter =
|
||||
rocksdb_get_updates_since(base_db_ld, seq_before, NULL, &err);
|
||||
CheckNoError(err);
|
||||
int log_found = 0;
|
||||
for (; rocksdb_wal_iter_valid(wal_iter) && !log_found;
|
||||
rocksdb_wal_iter_next(wal_iter)) {
|
||||
uint64_t seq;
|
||||
rocksdb_writebatch_t* wal_wb =
|
||||
rocksdb_wal_iter_get_batch(wal_iter, &seq);
|
||||
rocksdb_writebatch_iterate_ld(wal_wb, &log_found, NopPut, NopDel,
|
||||
CheckLogData);
|
||||
rocksdb_writebatch_destroy(wal_wb);
|
||||
}
|
||||
CheckCondition(log_found == 1);
|
||||
rocksdb_wal_iter_destroy(wal_iter);
|
||||
}
|
||||
rocksdb_transactiondb_close_base_db(base_db_ld);
|
||||
|
||||
// read from outside transaction, after commit
|
||||
CheckTxnDBGet(txn_db, roptions, "foo", "hello");
|
||||
CheckTxnDBPinGet(txn_db, roptions, "foo", "hello");
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace ROCKSDB_NAMESPACE {
|
||||
void CoalescingIterator::Coalesce(
|
||||
const autovector<MultiCfIteratorInfo>& items) {
|
||||
assert(wide_columns_.empty());
|
||||
assert(owned_columns_.empty());
|
||||
MinHeap heap;
|
||||
for (const auto& item : items) {
|
||||
assert(item.iterator);
|
||||
@@ -22,13 +23,22 @@ void CoalescingIterator::Coalesce(
|
||||
if (heap.empty()) {
|
||||
return;
|
||||
}
|
||||
// Own the coalesced bytes so the result stays valid even if a child iterator
|
||||
// refreshes or reuses its internal buffers.
|
||||
owned_columns_.reserve(heap.size());
|
||||
wide_columns_.reserve(heap.size());
|
||||
auto add_column = [this](const WideColumn& column) {
|
||||
owned_columns_.emplace_back(column.name().ToString(),
|
||||
column.value().ToString());
|
||||
const auto& owned = owned_columns_.back();
|
||||
wide_columns_.emplace_back(owned.first, owned.second);
|
||||
};
|
||||
auto current = heap.top();
|
||||
heap.pop();
|
||||
while (!heap.empty()) {
|
||||
int comparison = current.column->name().compare(heap.top().column->name());
|
||||
if (comparison < 0) {
|
||||
wide_columns_.push_back(*current.column);
|
||||
add_column(*current.column);
|
||||
} else if (comparison > 0) {
|
||||
// Shouldn't reach here.
|
||||
// Current item in the heap is greater than the top item in the min heap
|
||||
@@ -37,7 +47,7 @@ void CoalescingIterator::Coalesce(
|
||||
current = heap.top();
|
||||
heap.pop();
|
||||
}
|
||||
wide_columns_.push_back(*current.column);
|
||||
add_column(*current.column);
|
||||
|
||||
if (WideColumnsHelper::HasDefaultColumn(wide_columns_)) {
|
||||
value_ = WideColumnsHelper::GetDefaultColumn(wide_columns_);
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "db/multi_cf_iterator_impl.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
@@ -45,6 +49,7 @@ class CoalescingIterator : public Iterator {
|
||||
void Reset() {
|
||||
value_.clear();
|
||||
wide_columns_.clear();
|
||||
owned_columns_.clear();
|
||||
}
|
||||
|
||||
bool PrepareValue() override { return impl_.PrepareValue(); }
|
||||
@@ -79,6 +84,7 @@ class CoalescingIterator : public Iterator {
|
||||
MultiCfIteratorImpl<ResetFunc, PopulateFunc> impl_;
|
||||
Slice value_;
|
||||
WideColumns wide_columns_;
|
||||
std::vector<std::pair<std::string, std::string>> owned_columns_;
|
||||
|
||||
struct WideColumnWithOrder {
|
||||
const WideColumn* column;
|
||||
|
||||
+63
-14
@@ -11,12 +11,14 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "db/blob/blob_file_cache.h"
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_source.h"
|
||||
#include "db/compaction/compaction_picker.h"
|
||||
#include "db/compaction/compaction_picker_fifo.h"
|
||||
@@ -495,6 +497,15 @@ ColumnFamilyOptions SanitizeCfOptions(const ImmutableDBOptions& db_options,
|
||||
result.memtable_avg_op_scan_flush_trigger = 0;
|
||||
}
|
||||
}
|
||||
if (result.enable_blob_direct_write && !result.enable_blob_files) {
|
||||
ROCKS_LOG_WARN(db_options.info_log.get(),
|
||||
"enable_blob_direct_write requires enable_blob_files=true. "
|
||||
"Disabling blob direct write.");
|
||||
result.enable_blob_direct_write = false;
|
||||
}
|
||||
if (result.blob_direct_write_partitions == 0) {
|
||||
result.blob_direct_write_partitions = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -601,7 +612,7 @@ ColumnFamilyData::ColumnFamilyData(
|
||||
const FileOptions* file_options, ColumnFamilySet* column_family_set,
|
||||
BlockCacheTracer* const block_cache_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, const std::string& db_id,
|
||||
const std::string& db_session_id, bool read_only)
|
||||
const std::string& db_session_id, bool read_only, bool fast_sst_open)
|
||||
: id_(id),
|
||||
name_(name),
|
||||
dummy_versions_(_dummy_versions),
|
||||
@@ -661,7 +672,7 @@ ColumnFamilyData::ColumnFamilyData(
|
||||
new InternalStats(ioptions_.num_levels, ioptions_.clock, this));
|
||||
table_cache_.reset(new TableCache(ioptions_, file_options, _table_cache,
|
||||
block_cache_tracer, io_tracer,
|
||||
db_session_id));
|
||||
db_session_id, fast_sst_open));
|
||||
blob_file_cache_.reset(
|
||||
new BlobFileCache(_table_cache, &ioptions(), soptions(), id_,
|
||||
internal_stats_->GetBlobFileReadHist(), io_tracer));
|
||||
@@ -782,6 +793,11 @@ ColumnFamilyData::~ColumnFamilyData() {
|
||||
}
|
||||
}
|
||||
|
||||
void ColumnFamilyData::SetBlobPartitionManager(
|
||||
std::shared_ptr<BlobFilePartitionManager> mgr) {
|
||||
blob_partition_manager_ = std::move(mgr);
|
||||
}
|
||||
|
||||
bool ColumnFamilyData::UnrefAndTryDelete() {
|
||||
int old_refs = refs_.fetch_sub(1);
|
||||
assert(old_refs > 0);
|
||||
@@ -1523,6 +1539,32 @@ Status ColumnFamilyData::ValidateOptions(
|
||||
|
||||
const auto* ucmp = cf_options.comparator;
|
||||
assert(ucmp);
|
||||
if (cf_options.enable_blob_direct_write) {
|
||||
if (db_options.enable_pipelined_write) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write v1 does not support pipelined writes.");
|
||||
}
|
||||
if (db_options.allow_concurrent_memtable_write) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write v1 does not support concurrent memtable writes.");
|
||||
}
|
||||
if (db_options.unordered_write) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write v1 does not support unordered writes.");
|
||||
}
|
||||
if (db_options.two_write_queues) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write v1 does not support two write queues.");
|
||||
}
|
||||
if (cf_options.experimental_mempurge_threshold > 0.0) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write does not support MemPurge.");
|
||||
}
|
||||
if (ucmp->timestamp_size() > 0) {
|
||||
return Status::NotSupported(
|
||||
"Blob direct write does not support user-defined timestamps.");
|
||||
}
|
||||
}
|
||||
if (ucmp->timestamp_size() > 0 &&
|
||||
!cf_options.persist_user_defined_timestamps) {
|
||||
if (db_options.atomic_flush) {
|
||||
@@ -1563,6 +1605,14 @@ Status ColumnFamilyData::ValidateOptions(
|
||||
}
|
||||
}
|
||||
|
||||
if (cf_options.read_triggered_compaction_threshold < 0.0 ||
|
||||
std::isnan(cf_options.read_triggered_compaction_threshold) ||
|
||||
std::isinf(cf_options.read_triggered_compaction_threshold)) {
|
||||
return Status::InvalidArgument(
|
||||
"read_triggered_compaction_threshold must be >= 0.0 and finite. "
|
||||
"Use 0.0 to disable.");
|
||||
}
|
||||
|
||||
if (cf_options.compaction_style == kCompactionStyleFIFO &&
|
||||
db_options.max_open_files != -1 && cf_options.ttl > 0) {
|
||||
return Status::NotSupported(
|
||||
@@ -1759,16 +1809,14 @@ void ColumnFamilyData::RecoverEpochNumbers() {
|
||||
vstorage->RecoverEpochNumbers(this);
|
||||
}
|
||||
|
||||
ColumnFamilySet::ColumnFamilySet(const std::string& dbname,
|
||||
const ImmutableDBOptions* db_options,
|
||||
const FileOptions& file_options,
|
||||
Cache* table_cache,
|
||||
WriteBufferManager* _write_buffer_manager,
|
||||
WriteController* _write_controller,
|
||||
BlockCacheTracer* const block_cache_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
const std::string& db_id,
|
||||
const std::string& db_session_id)
|
||||
ColumnFamilySet::ColumnFamilySet(
|
||||
const std::string& dbname, const ImmutableDBOptions* db_options,
|
||||
const FileOptions& file_options, Cache* table_cache,
|
||||
WriteBufferManager* _write_buffer_manager,
|
||||
WriteController* _write_controller,
|
||||
BlockCacheTracer* const block_cache_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, const std::string& db_id,
|
||||
const std::string& db_session_id, bool fast_sst_open)
|
||||
: max_column_family_(0),
|
||||
file_options_(file_options),
|
||||
dummy_cfd_(new ColumnFamilyData(
|
||||
@@ -1785,7 +1833,8 @@ ColumnFamilySet::ColumnFamilySet(const std::string& dbname,
|
||||
block_cache_tracer_(block_cache_tracer),
|
||||
io_tracer_(io_tracer),
|
||||
db_id_(db_id),
|
||||
db_session_id_(db_session_id) {
|
||||
db_session_id_(db_session_id),
|
||||
fast_sst_open_(fast_sst_open) {
|
||||
// initialize linked list
|
||||
dummy_cfd_->prev_ = dummy_cfd_;
|
||||
dummy_cfd_->next_ = dummy_cfd_;
|
||||
@@ -1852,7 +1901,7 @@ ColumnFamilyData* ColumnFamilySet::CreateColumnFamily(
|
||||
ColumnFamilyData* new_cfd = new ColumnFamilyData(
|
||||
id, name, dummy_versions, table_cache_, write_buffer_manager_, options,
|
||||
*db_options_, &file_options_, this, block_cache_tracer_, io_tracer_,
|
||||
db_id_, db_session_id_, read_only);
|
||||
db_id_, db_session_id_, read_only, fast_sst_open_);
|
||||
column_families_.insert({name, id});
|
||||
column_family_data_.insert({id, new_cfd});
|
||||
auto ucmp = new_cfd->user_comparator();
|
||||
|
||||
+41
-9
@@ -49,6 +49,7 @@ class InstrumentedMutex;
|
||||
class InstrumentedMutexLock;
|
||||
struct SuperVersionContext;
|
||||
class BlobFileCache;
|
||||
class BlobFilePartitionManager;
|
||||
class BlobSource;
|
||||
|
||||
extern const double kIncSlowdownRatio;
|
||||
@@ -415,6 +416,19 @@ class ColumnFamilyData {
|
||||
TableCache* table_cache() const { return table_cache_.get(); }
|
||||
BlobFileCache* blob_file_cache() const { return blob_file_cache_.get(); }
|
||||
BlobSource* blob_source() const { return blob_source_.get(); }
|
||||
// Returns the write-path blob partition manager for this CF, or null if BDW
|
||||
// is disabled.
|
||||
BlobFilePartitionManager* blob_partition_manager() const {
|
||||
return blob_partition_manager_.get();
|
||||
}
|
||||
// Returns a shared ownership handle for cold paths that must keep the
|
||||
// manager alive beyond the lifetime of this ColumnFamilyData.
|
||||
std::shared_ptr<BlobFilePartitionManager> blob_partition_manager_handle()
|
||||
const {
|
||||
return blob_partition_manager_;
|
||||
}
|
||||
// Installs the write-path blob partition manager for this CF.
|
||||
void SetBlobPartitionManager(std::shared_ptr<BlobFilePartitionManager> mgr);
|
||||
|
||||
// See documentation in compaction_picker.h
|
||||
// REQUIRES: DB mutex held
|
||||
@@ -605,16 +619,23 @@ class ColumnFamilyData {
|
||||
return ioptions_.cf_allow_ingest_behind || ioptions_.allow_ingest_behind;
|
||||
}
|
||||
|
||||
// Per-CF reader-writer lock that serializes IngestExternalFiles with range
|
||||
// tombstone conversion.
|
||||
port::RWMutex& GetIngestSstLock() { return ingest_sst_lock_; }
|
||||
|
||||
private:
|
||||
friend class ColumnFamilySet;
|
||||
ColumnFamilyData(
|
||||
uint32_t id, const std::string& name, Version* dummy_versions,
|
||||
Cache* table_cache, WriteBufferManager* write_buffer_manager,
|
||||
const ColumnFamilyOptions& options, const ImmutableDBOptions& db_options,
|
||||
const FileOptions* file_options, ColumnFamilySet* column_family_set,
|
||||
BlockCacheTracer* const block_cache_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer, const std::string& db_id,
|
||||
const std::string& db_session_id, bool read_only);
|
||||
ColumnFamilyData(uint32_t id, const std::string& name,
|
||||
Version* dummy_versions, Cache* table_cache,
|
||||
WriteBufferManager* write_buffer_manager,
|
||||
const ColumnFamilyOptions& options,
|
||||
const ImmutableDBOptions& db_options,
|
||||
const FileOptions* file_options,
|
||||
ColumnFamilySet* column_family_set,
|
||||
BlockCacheTracer* const block_cache_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
const std::string& db_id, const std::string& db_session_id,
|
||||
bool read_only, bool fast_sst_open = false);
|
||||
|
||||
std::vector<std::string> GetDbPaths() const;
|
||||
|
||||
@@ -648,6 +669,8 @@ class ColumnFamilyData {
|
||||
std::unique_ptr<TableCache> table_cache_;
|
||||
std::unique_ptr<BlobFileCache> blob_file_cache_;
|
||||
std::unique_ptr<BlobSource> blob_source_;
|
||||
// Per-CF manager for write-path blob direct-write files.
|
||||
std::shared_ptr<BlobFilePartitionManager> blob_partition_manager_;
|
||||
|
||||
std::unique_ptr<InternalStats> internal_stats_;
|
||||
|
||||
@@ -713,6 +736,10 @@ class ColumnFamilyData {
|
||||
bool mempurge_used_;
|
||||
|
||||
std::atomic<uint64_t> next_epoch_number_;
|
||||
|
||||
// Used to synchronize IngestExternalFile with range tombstone conversion. See
|
||||
// also Memtable::ingest_seqno_barrier_.
|
||||
port::RWMutex ingest_sst_lock_;
|
||||
};
|
||||
|
||||
// ColumnFamilySet has interesting thread-safety requirements
|
||||
@@ -758,7 +785,8 @@ class ColumnFamilySet {
|
||||
WriteController* _write_controller,
|
||||
BlockCacheTracer* const block_cache_tracer,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
const std::string& db_id, const std::string& db_session_id);
|
||||
const std::string& db_id, const std::string& db_session_id,
|
||||
bool fast_sst_open = false);
|
||||
~ColumnFamilySet();
|
||||
|
||||
ColumnFamilyData* GetDefault() const;
|
||||
@@ -794,6 +822,9 @@ class ColumnFamilySet {
|
||||
|
||||
Cache* get_table_cache() { return table_cache_; }
|
||||
|
||||
bool GetFastSstOpen() const { return fast_sst_open_; }
|
||||
void SetFastSstOpen(bool v) { fast_sst_open_ = v; }
|
||||
|
||||
WriteBufferManager* write_buffer_manager() { return write_buffer_manager_; }
|
||||
|
||||
WriteController* write_controller() { return write_controller_; }
|
||||
@@ -842,6 +873,7 @@ class ColumnFamilySet {
|
||||
std::shared_ptr<IOTracer> io_tracer_;
|
||||
const std::string& db_id_;
|
||||
std::string db_session_id_;
|
||||
bool fast_sst_open_;
|
||||
};
|
||||
|
||||
// A wrapper for ColumnFamilySet that supports releasing DB mutex during each
|
||||
|
||||
@@ -7,12 +7,14 @@
|
||||
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
|
||||
#include "db/blob/blob_fetcher.h"
|
||||
#include "db/blob/blob_file_builder.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/blob/prefetch_buffer_collection.h"
|
||||
#include "db/snapshot_checker.h"
|
||||
#include "db/wide/blob_column_resolver_util.h"
|
||||
#include "db/wide/wide_column_serialization.h"
|
||||
#include "db/wide/wide_columns_helper.h"
|
||||
#include "logging/logging.h"
|
||||
@@ -20,8 +22,106 @@
|
||||
#include "rocksdb/listener.h"
|
||||
#include "table/internal_iterator.h"
|
||||
#include "test_util/sync_point.h"
|
||||
#include "util/autovector.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
// CompactionBlobResolver implementation
|
||||
void CompactionBlobResolver::Init(BlobFetcher* blob_fetcher,
|
||||
PrefetchBufferCollection* prefetch_buffers,
|
||||
CompactionIterationStats* iter_stats) {
|
||||
blob_fetcher_ = blob_fetcher;
|
||||
prefetch_buffers_ = prefetch_buffers;
|
||||
iter_stats_ = iter_stats;
|
||||
}
|
||||
|
||||
void CompactionBlobResolver::Reset(
|
||||
const Slice& user_key, const std::vector<WideColumn>* columns,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>* blob_columns,
|
||||
bool track_resolve_error) {
|
||||
user_key_ = user_key;
|
||||
columns_ = columns;
|
||||
blob_columns_ = blob_columns;
|
||||
track_resolve_error_ = track_resolve_error;
|
||||
resolved_cache_.clear();
|
||||
resolve_error_.reset();
|
||||
}
|
||||
|
||||
Status CompactionBlobResolver::ResolveColumn(size_t column_index,
|
||||
Slice* resolved_value) {
|
||||
Status status = Status::OK();
|
||||
if (columns_ == nullptr || column_index >= columns_->size()) {
|
||||
status = Status::InvalidArgument("Column index out of bounds");
|
||||
} else {
|
||||
const BlobIndex* blob_index_ptr =
|
||||
blob_resolver_util::FindBlobColumn(blob_columns_, column_index);
|
||||
|
||||
if (blob_index_ptr == nullptr) {
|
||||
// Not a blob column - return the inline value directly
|
||||
*resolved_value = (*columns_)[column_index].value();
|
||||
} else {
|
||||
// Check if we've already resolved this column
|
||||
PinnableSlice* cached =
|
||||
blob_resolver_util::FindInCache(resolved_cache_, column_index);
|
||||
if (cached != nullptr) {
|
||||
*resolved_value = Slice(*cached);
|
||||
} else if (blob_fetcher_ == nullptr) {
|
||||
status = Status::NotSupported("Blob fetcher not available");
|
||||
} else {
|
||||
const BlobIndex& blob_index = *blob_index_ptr;
|
||||
|
||||
// Handle inlined blobs (e.g., from legacy stacked BlobDB with TTL)
|
||||
if (blob_index.IsInlined()) {
|
||||
*resolved_value = blob_resolver_util::CacheInlinedBlob(
|
||||
resolved_cache_, column_index, blob_index);
|
||||
} else {
|
||||
FilePrefetchBuffer* prefetch_buffer =
|
||||
prefetch_buffers_ ? prefetch_buffers_->GetOrCreatePrefetchBuffer(
|
||||
blob_index.file_number())
|
||||
: nullptr;
|
||||
|
||||
uint64_t bytes_read = 0;
|
||||
resolved_cache_.emplace_back(column_index,
|
||||
std::make_unique<PinnableSlice>());
|
||||
auto& new_entry = resolved_cache_.back();
|
||||
|
||||
status =
|
||||
blob_fetcher_->FetchBlob(user_key_, blob_index, prefetch_buffer,
|
||||
new_entry.second.get(), &bytes_read);
|
||||
if (!status.ok()) {
|
||||
resolved_cache_.pop_back();
|
||||
} else {
|
||||
if (iter_stats_ != nullptr) {
|
||||
++iter_stats_->num_blobs_read;
|
||||
iter_stats_->total_blob_bytes_read += bytes_read;
|
||||
}
|
||||
|
||||
*resolved_value = Slice(*new_entry.second);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!status.ok() && track_resolve_error_ && !resolve_error_.has_value()) {
|
||||
resolve_error_.emplace(status);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
bool CompactionBlobResolver::IsBlobColumn(size_t column_index) const {
|
||||
if (columns_ == nullptr || column_index >= columns_->size()) {
|
||||
return false;
|
||||
}
|
||||
return blob_resolver_util::IsBlobColumnIndex(blob_columns_, column_index);
|
||||
}
|
||||
|
||||
size_t CompactionBlobResolver::NumColumns() const {
|
||||
if (columns_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return columns_->size();
|
||||
}
|
||||
|
||||
CompactionIterator::CompactionIterator(
|
||||
InternalIterator* input, const Comparator* cmp, MergeHelper* merge_helper,
|
||||
SequenceNumber last_sequence, std::vector<SequenceNumber>* snapshots,
|
||||
@@ -38,7 +138,8 @@ CompactionIterator::CompactionIterator(
|
||||
const std::atomic<bool>* shutting_down,
|
||||
const std::shared_ptr<Logger> info_log,
|
||||
const std::string* full_history_ts_low,
|
||||
std::optional<SequenceNumber> preserve_seqno_min)
|
||||
std::optional<SequenceNumber> preserve_seqno_min,
|
||||
const Version* input_version, Env::IOActivity blob_read_io_activity)
|
||||
: CompactionIterator(
|
||||
input, cmp, merge_helper, last_sequence, snapshots, earliest_snapshot,
|
||||
earliest_write_conflict_snapshot, job_snapshot, snapshot_checker, env,
|
||||
@@ -47,7 +148,8 @@ CompactionIterator::CompactionIterator(
|
||||
manual_compaction_canceled,
|
||||
compaction ? std::make_unique<RealCompaction>(compaction) : nullptr,
|
||||
must_count_input_entries, compaction_filter, shutting_down, info_log,
|
||||
full_history_ts_low, preserve_seqno_min) {}
|
||||
full_history_ts_low, preserve_seqno_min, input_version,
|
||||
blob_read_io_activity) {}
|
||||
|
||||
CompactionIterator::CompactionIterator(
|
||||
InternalIterator* input, const Comparator* cmp, MergeHelper* merge_helper,
|
||||
@@ -65,7 +167,8 @@ CompactionIterator::CompactionIterator(
|
||||
const std::atomic<bool>* shutting_down,
|
||||
const std::shared_ptr<Logger> info_log,
|
||||
const std::string* full_history_ts_low,
|
||||
std::optional<SequenceNumber> preserve_seqno_min)
|
||||
std::optional<SequenceNumber> preserve_seqno_min,
|
||||
const Version* input_version, Env::IOActivity blob_read_io_activity)
|
||||
: input_(input, cmp, must_count_input_entries),
|
||||
cmp_(cmp),
|
||||
merge_helper_(merge_helper),
|
||||
@@ -80,6 +183,8 @@ CompactionIterator::CompactionIterator(
|
||||
blob_file_builder_(blob_file_builder),
|
||||
compaction_(std::move(compaction)),
|
||||
compaction_filter_(compaction_filter),
|
||||
filter_supports_v4_(
|
||||
compaction_filter_ ? compaction_filter_->SupportsFilterV4() : false),
|
||||
shutting_down_(shutting_down),
|
||||
manual_compaction_canceled_(manual_compaction_canceled),
|
||||
bottommost_level_(compaction_ && compaction_->bottommost_level() &&
|
||||
@@ -98,7 +203,8 @@ CompactionIterator::CompactionIterator(
|
||||
merge_out_iter_(merge_helper_),
|
||||
blob_garbage_collection_cutoff_file_number_(
|
||||
ComputeBlobGarbageCollectionCutoffFileNumber(compaction_.get())),
|
||||
blob_fetcher_(CreateBlobFetcherIfNeeded(compaction_.get())),
|
||||
blob_fetcher_(CreateBlobFetcherIfNeeded(compaction_.get(), input_version,
|
||||
blob_read_io_activity)),
|
||||
prefetch_buffers_(
|
||||
CreatePrefetchBufferCollectionIfNeeded(compaction_.get())),
|
||||
current_key_committed_(false),
|
||||
@@ -120,6 +226,8 @@ CompactionIterator::CompactionIterator(
|
||||
timestamp_size_ == full_history_ts_low_->size());
|
||||
#endif
|
||||
input_.SetPinnedItersMgr(&pinned_iters_mgr_);
|
||||
blob_resolver_.Init(blob_fetcher_.get(), prefetch_buffers_.get(),
|
||||
&iter_stats_);
|
||||
// The default `merge_until_status_` does not need to be checked since it is
|
||||
// overwritten as soon as `MergeUntil()` is called
|
||||
merge_until_status_.PermitUncheckedError();
|
||||
@@ -131,6 +239,24 @@ CompactionIterator::~CompactionIterator() {
|
||||
input_.SetPinnedItersMgr(nullptr);
|
||||
}
|
||||
|
||||
void CompactionIterator::SetBlobFetcher(const Version* version,
|
||||
BlobFileCache* blob_file_cache,
|
||||
Env::IOActivity io_activity,
|
||||
bool allow_write_path_fallback) {
|
||||
if (blob_fetcher_ != nullptr || version == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
ReadOptions read_options;
|
||||
read_options.io_activity = io_activity;
|
||||
read_options.fill_cache = false;
|
||||
|
||||
blob_fetcher_ = std::make_unique<BlobFetcher>(
|
||||
version, read_options, blob_file_cache, allow_write_path_fallback);
|
||||
blob_resolver_.Init(blob_fetcher_.get(), prefetch_buffers_.get(),
|
||||
&iter_stats_);
|
||||
}
|
||||
|
||||
void CompactionIterator::ResetRecordCounts() {
|
||||
iter_stats_.num_record_drop_user = 0;
|
||||
iter_stats_.num_record_drop_hidden = 0;
|
||||
@@ -258,9 +384,10 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
compaction_filter_skip_until_.rep());
|
||||
if (decision == CompactionFilter::Decision::kUndetermined &&
|
||||
!compaction_filter_->IsStackedBlobDbInternalCompactionFilter()) {
|
||||
if (!compaction_) {
|
||||
status_ =
|
||||
Status::Corruption("Unexpected blob index outside of compaction");
|
||||
if (!blob_fetcher_) {
|
||||
status_ = Status::NotSupported(
|
||||
"Blob-backed value filtering requires blob read support in the "
|
||||
"current table-file creation context");
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
@@ -287,8 +414,6 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
|
||||
uint64_t bytes_read = 0;
|
||||
|
||||
assert(blob_fetcher_);
|
||||
|
||||
s = blob_fetcher_->FetchBlob(ikey_.user_key, blob_index,
|
||||
prefetch_buffer, &blob_value_,
|
||||
&bytes_read);
|
||||
@@ -309,7 +434,19 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
const Slice* existing_val = nullptr;
|
||||
const WideColumns* existing_col = nullptr;
|
||||
|
||||
WideColumns existing_columns;
|
||||
// Reuse member variable to avoid per-key heap allocation.
|
||||
filter_existing_columns_.clear();
|
||||
WideColumnBlobResolver* blob_resolver_ptr = nullptr;
|
||||
|
||||
// Use member variables for entity column storage to avoid per-key
|
||||
// heap allocations. Cleared at start of each use.
|
||||
entity_columns_.clear();
|
||||
entity_blob_columns_.clear();
|
||||
// Storage for eagerly resolved blob values used by the FilterV3
|
||||
// compatibility path. Keep this in the same scope as the FilterV4()
|
||||
// invocation below because filter_existing_columns_ stores Slices into
|
||||
// these buffers.
|
||||
autovector<PinnableSlice, 4> resolved_blobs_storage;
|
||||
|
||||
if (ikey_.type != kTypeWideColumnEntity) {
|
||||
if (!blob_value_.empty()) {
|
||||
@@ -318,23 +455,125 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
existing_val = &value_;
|
||||
}
|
||||
} else {
|
||||
Slice value_copy = value_;
|
||||
const Status s =
|
||||
WideColumnSerialization::Deserialize(value_copy, existing_columns);
|
||||
// Check if entity has blob columns that need resolution
|
||||
bool has_blob_columns = false;
|
||||
{
|
||||
Status s_hbc =
|
||||
WideColumnSerialization::HasBlobColumns(value_, has_blob_columns);
|
||||
if (!s_hbc.ok()) {
|
||||
status_ = s_hbc;
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (UNLIKELY(has_blob_columns)) {
|
||||
// Entity has blob columns. DeserializeV2() populates
|
||||
// entity_columns_ with the full column set; blob-backed entries still
|
||||
// carry the serialized BlobIndex bytes in value(). The companion
|
||||
// entity_blob_columns_ side list identifies which column indexes are
|
||||
// blob references and provides the decoded BlobIndex objects.
|
||||
Slice input_copy = value_;
|
||||
Status s = WideColumnSerialization::DeserializeV2(
|
||||
input_copy, entity_columns_, entity_blob_columns_);
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
// Mark as deserialized so PrepareOutput can skip re-deserialization
|
||||
// if the filter returns kKeep.
|
||||
entity_deserialized_ = true;
|
||||
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
// Build filter_existing_columns_ with raw values (blob columns
|
||||
// have blob index as value, not the actual blob data)
|
||||
for (size_t i = 0; i < entity_columns_.size(); ++i) {
|
||||
filter_existing_columns_.emplace_back(entity_columns_[i].name(),
|
||||
entity_columns_[i].value());
|
||||
}
|
||||
|
||||
if (filter_supports_v4_) {
|
||||
// Reset member blob resolver for this entity - filter can call
|
||||
// resolver->ResolveColumn() to fetch blob values on-demand
|
||||
blob_resolver_.Reset(ikey_.user_key, &entity_columns_,
|
||||
&entity_blob_columns_,
|
||||
/*track_resolve_error=*/true);
|
||||
blob_resolver_ptr = &blob_resolver_;
|
||||
} else {
|
||||
// FilterV3 compatibility: eagerly resolve all blob columns so
|
||||
// the filter sees actual values (not raw BlobIndex bytes).
|
||||
// resolved_blobs_storage is in the outer scope so the data
|
||||
// outlives filter_existing_columns_ (which holds Slices into
|
||||
// it).
|
||||
resolved_blobs_storage.resize(entity_blob_columns_.size());
|
||||
for (size_t bi = 0; bi < entity_blob_columns_.size(); ++bi) {
|
||||
const size_t col_idx = entity_blob_columns_[bi].first;
|
||||
const BlobIndex& blob_idx = entity_blob_columns_[bi].second;
|
||||
|
||||
if (blob_idx.IsInlined()) {
|
||||
resolved_blobs_storage[bi].PinSelf(blob_idx.value());
|
||||
} else {
|
||||
FilePrefetchBuffer* prefetch_buffer =
|
||||
prefetch_buffers_
|
||||
? prefetch_buffers_->GetOrCreatePrefetchBuffer(
|
||||
blob_idx.file_number())
|
||||
: nullptr;
|
||||
uint64_t bytes_read = 0;
|
||||
assert(blob_fetcher_);
|
||||
Status s_fetch = blob_fetcher_->FetchBlob(
|
||||
ikey_.user_key, blob_idx, prefetch_buffer,
|
||||
&resolved_blobs_storage[bi], &bytes_read);
|
||||
if (!s_fetch.ok()) {
|
||||
status_ = s_fetch;
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
++iter_stats_.num_blobs_read;
|
||||
iter_stats_.total_blob_bytes_read += bytes_read;
|
||||
}
|
||||
|
||||
// Replace the blob index value in filter_existing_columns_
|
||||
// with the resolved blob value
|
||||
filter_existing_columns_[col_idx] =
|
||||
WideColumn(entity_columns_[col_idx].name(),
|
||||
Slice(resolved_blobs_storage[bi]));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No blob columns, use fast path
|
||||
Slice value_copy = value_;
|
||||
const Status s = WideColumnSerialization::Deserialize(
|
||||
value_copy, filter_existing_columns_);
|
||||
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
existing_col = &filter_existing_columns_;
|
||||
}
|
||||
|
||||
// existing_val / existing_col point into value_, blob_value_,
|
||||
// entity_columns_, and resolved_blobs_storage, all of which stay alive
|
||||
// until this call returns.
|
||||
decision = compaction_filter_->FilterV4(
|
||||
level_, filter_key, value_type, existing_val, existing_col,
|
||||
&compaction_filter_value_, &new_columns,
|
||||
compaction_filter_skip_until_.rep(), blob_resolver_ptr);
|
||||
|
||||
if (blob_resolver_ptr != nullptr) {
|
||||
Status resolve_status = blob_resolver_.resolve_status();
|
||||
if (!resolve_status.ok()) {
|
||||
// Keep lazy FilterV4 failure semantics aligned with the eager
|
||||
// FilterV3 compatibility path: if blob resolution fails while the
|
||||
// filter is inspecting the entry, fail compaction even if the
|
||||
// filter returned kKeep after noticing the error.
|
||||
status_ = std::move(resolve_status);
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
|
||||
existing_col = &existing_columns;
|
||||
}
|
||||
|
||||
decision = compaction_filter_->FilterV3(
|
||||
level_, filter_key, value_type, existing_val, existing_col,
|
||||
&compaction_filter_value_, &new_columns,
|
||||
compaction_filter_skip_until_.rep());
|
||||
}
|
||||
|
||||
iter_stats_.total_filter_time +=
|
||||
@@ -342,10 +581,10 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
}
|
||||
|
||||
if (decision == CompactionFilter::Decision::kUndetermined) {
|
||||
// Should not reach here, since FilterV2/FilterV3 should never return
|
||||
// Should not reach here, since FilterV2/V3/V4 should never return
|
||||
// kUndetermined.
|
||||
status_ = Status::NotSupported(
|
||||
"FilterV2/FilterV3 should never return kUndetermined");
|
||||
"FilterV2/V3/V4 should never return kUndetermined");
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
@@ -354,7 +593,7 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
cmp_->Compare(*compaction_filter_skip_until_.rep(), ikey_.user_key) <=
|
||||
0) {
|
||||
// Can't skip to a key smaller than the current one.
|
||||
// Keep the key as per FilterV2/FilterV3 documentation.
|
||||
// Keep the key as per FilterV2/V3/V4 documentation.
|
||||
decision = CompactionFilter::Decision::kKeep;
|
||||
}
|
||||
|
||||
@@ -442,6 +681,16 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
}
|
||||
|
||||
value_ = compaction_filter_value_;
|
||||
// Value changed, force re-deserialization in PrepareOutput
|
||||
entity_deserialized_ = false;
|
||||
}
|
||||
|
||||
if (UNLIKELY(compaction_filter_value_.size() >
|
||||
std::numeric_limits<uint32_t>::max())) {
|
||||
status_ = Status::Corruption(
|
||||
"CompactionFilter result value size exceeds 4GB limit");
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -450,9 +699,14 @@ bool CompactionIterator::InvokeFilterIfNeeded(bool* need_skip,
|
||||
void CompactionIterator::NextFromInput() {
|
||||
at_next_ = false;
|
||||
validity_info_.Invalidate();
|
||||
entity_deserialized_ = false;
|
||||
|
||||
while (!Valid() && input_.Valid() && !IsPausingManualCompaction() &&
|
||||
!IsShuttingDown()) {
|
||||
// A filtered-out key can advance directly to the next input record without
|
||||
// returning to PrepareOutput(), so clear the per-record deserialization
|
||||
// cache at the start of each loop iteration.
|
||||
entity_deserialized_ = false;
|
||||
key_ = input_.key();
|
||||
value_ = input_.value();
|
||||
blob_value_.Reset();
|
||||
@@ -1266,6 +1520,340 @@ void CompactionIterator::GarbageCollectBlobIfNeeded() {
|
||||
}
|
||||
}
|
||||
|
||||
// Wide-column entity compaction flow after InvokeFilterIfNeeded():
|
||||
// 1. PrepareOutput() is the only caller of the helper block below.
|
||||
// 2. PrepareOutput() ensures entity_columns_/entity_blob_columns_ describe
|
||||
// the current entity, either by reusing the filter-time deserialization or
|
||||
// by deserializing once itself.
|
||||
// 3. PrepareOutput() then picks exactly one follow-up path:
|
||||
// - inline-only entity:
|
||||
// ExtractLargeColumnValuesIfNeeded()
|
||||
// This is the flush/compaction extraction path. It may rewrite large
|
||||
// inline columns into blob references.
|
||||
// - entity already containing blob references:
|
||||
// GarbageCollectEntityBlobsIfNeeded()
|
||||
// This is the blob-GC path. It only fetches / relocates references
|
||||
// that fall below the current GC cutoff.
|
||||
// Both helpers leave value_ unchanged when no rewrite is needed.
|
||||
void CompactionIterator::ExtractLargeColumnValuesIfNeeded() {
|
||||
assert(ikey_.type == kTypeWideColumnEntity);
|
||||
|
||||
// Check if blob extraction is enabled
|
||||
if (!blob_file_builder_) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Deserialize using member variables (already populated by PrepareOutput
|
||||
// for the combined deserialization path). If not yet populated, deserialize
|
||||
// here.
|
||||
if (entity_columns_.empty()) {
|
||||
Slice entity_slice = value_;
|
||||
entity_columns_.clear();
|
||||
entity_blob_columns_.clear();
|
||||
Status s = WideColumnSerialization::DeserializeV2(
|
||||
entity_slice, entity_columns_, entity_blob_columns_);
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// IMPORTANT: If there are already blob columns, we skip extraction entirely.
|
||||
// This means new large inline columns added to such entities (e.g., via
|
||||
// PutEntity after a previous PutEntity that already had blob columns) will
|
||||
// NOT be extracted to blob files until the entity undergoes a full rewrite
|
||||
// (e.g., via compaction that GCs all existing blob refs).
|
||||
//
|
||||
// This is a deliberate simplification: supporting partial extraction
|
||||
// (extracting only new large inline columns while preserving existing
|
||||
// blob refs) would require merging old and new blob column sets during
|
||||
// serialization, and the benefit is marginal since full rewrites happen
|
||||
// naturally during blob GC.
|
||||
if (UNLIKELY(!entity_blob_columns_.empty())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to extract large column values to blob files
|
||||
// Track which columns were extracted and their blob indices
|
||||
std::vector<std::pair<size_t, BlobIndex>> new_blob_columns;
|
||||
std::string temp_blob_index;
|
||||
|
||||
for (size_t i = 0; i < entity_columns_.size(); ++i) {
|
||||
const Slice& col_value = entity_columns_[i].value();
|
||||
|
||||
// Clear the temporary buffer for each column
|
||||
temp_blob_index.clear();
|
||||
|
||||
// Try to add the column value to blob file
|
||||
// blob_file_builder_->Add() will check min_blob_size internally
|
||||
// and only write to blob file if the value is large enough
|
||||
const Status add_status =
|
||||
blob_file_builder_->Add(user_key(), col_value, &temp_blob_index);
|
||||
if (!add_status.ok()) {
|
||||
status_ = add_status;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
|
||||
// If blob_index is not empty, the value was extracted to a blob file
|
||||
if (!temp_blob_index.empty()) {
|
||||
BlobIndex blob_idx;
|
||||
const Status decode_status = blob_idx.DecodeFrom(temp_blob_index);
|
||||
if (!decode_status.ok()) {
|
||||
status_ = decode_status;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
new_blob_columns.emplace_back(i, blob_idx);
|
||||
}
|
||||
}
|
||||
|
||||
// If no columns were extracted, nothing to do
|
||||
if (new_blob_columns.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Re-serialize the entity with blob indices using the Slice-based overload
|
||||
// to avoid copying column names and values to strings.
|
||||
entity_wide_columns_.clear();
|
||||
entity_wide_columns_.reserve(entity_columns_.size());
|
||||
for (const auto& col : entity_columns_) {
|
||||
entity_wide_columns_.emplace_back(col.name(), col.value());
|
||||
}
|
||||
|
||||
rewritten_entity_.clear();
|
||||
const Status serialize_status = WideColumnSerialization::SerializeV2(
|
||||
entity_wide_columns_, new_blob_columns, rewritten_entity_);
|
||||
if (!serialize_status.ok()) {
|
||||
status_ = serialize_status;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
|
||||
// Update value_ to point to the rewritten entity
|
||||
value_ = rewritten_entity_;
|
||||
}
|
||||
|
||||
bool CompactionIterator::FetchBlobsNeedingGC(
|
||||
const std::vector<WideColumn>& /*columns*/,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& blob_columns,
|
||||
std::vector<std::pair<size_t, PinnableSlice>>* fetched_blob_values) {
|
||||
assert(fetched_blob_values != nullptr);
|
||||
|
||||
for (const auto& blob_col : blob_columns) {
|
||||
const size_t col_idx = blob_col.first;
|
||||
const BlobIndex& blob_index = blob_col.second;
|
||||
|
||||
// Skip inlined blobs - they don't need GC
|
||||
if (blob_index.IsInlined()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if this blob file needs garbage collection
|
||||
if (blob_index.file_number() >=
|
||||
blob_garbage_collection_cutoff_file_number_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// This blob needs to be relocated - fetch its value
|
||||
FilePrefetchBuffer* prefetch_buffer =
|
||||
prefetch_buffers_ ? prefetch_buffers_->GetOrCreatePrefetchBuffer(
|
||||
blob_index.file_number())
|
||||
: nullptr;
|
||||
|
||||
uint64_t bytes_read = 0;
|
||||
assert(blob_fetcher_);
|
||||
|
||||
PinnableSlice blob_value;
|
||||
Status s = blob_fetcher_->FetchBlob(user_key(), blob_index, prefetch_buffer,
|
||||
&blob_value, &bytes_read);
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
validity_info_.Invalidate();
|
||||
return false;
|
||||
}
|
||||
|
||||
++iter_stats_.num_blobs_read;
|
||||
iter_stats_.total_blob_bytes_read += bytes_read;
|
||||
|
||||
fetched_blob_values->emplace_back(col_idx, std::move(blob_value));
|
||||
}
|
||||
|
||||
return !fetched_blob_values->empty();
|
||||
}
|
||||
|
||||
std::vector<std::pair<size_t, BlobIndex>>
|
||||
CompactionIterator::RelocateBlobValues(
|
||||
const std::vector<std::pair<size_t, PinnableSlice>>& fetched_blob_values) {
|
||||
std::vector<std::pair<size_t, BlobIndex>> new_blob_columns;
|
||||
|
||||
if (blob_file_builder_) {
|
||||
for (const auto& fv : fetched_blob_values) {
|
||||
const size_t col_idx = fv.first;
|
||||
const Slice col_value(fv.second);
|
||||
|
||||
std::string temp_blob_index;
|
||||
Status status =
|
||||
blob_file_builder_->Add(user_key(), col_value, &temp_blob_index);
|
||||
if (!status.ok()) {
|
||||
status_ = status;
|
||||
validity_info_.Invalidate();
|
||||
new_blob_columns.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
if (!temp_blob_index.empty()) {
|
||||
BlobIndex blob_idx;
|
||||
status = blob_idx.DecodeFrom(temp_blob_index);
|
||||
if (!status.ok()) {
|
||||
status_ = status;
|
||||
validity_info_.Invalidate();
|
||||
new_blob_columns.clear();
|
||||
break;
|
||||
}
|
||||
new_blob_columns.emplace_back(col_idx, blob_idx);
|
||||
|
||||
// Track relocation stats here (after successful relocation)
|
||||
++iter_stats_.num_blobs_relocated;
|
||||
iter_stats_.total_blob_bytes_relocated += col_value.size();
|
||||
}
|
||||
// If temp_blob_index is empty, the value will be inlined
|
||||
}
|
||||
}
|
||||
|
||||
return new_blob_columns;
|
||||
}
|
||||
|
||||
void CompactionIterator::SerializeEntityAfterGC(
|
||||
const std::vector<WideColumn>& columns,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& original_blob_columns,
|
||||
const std::vector<std::pair<size_t, PinnableSlice>>& fetched_blob_values,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& new_blob_columns) {
|
||||
// Collect all blob columns for final serialization: include non-GC'd
|
||||
// originals and newly relocated. Use linear scans since these
|
||||
// collections are typically small (<5 elements).
|
||||
std::vector<std::pair<size_t, BlobIndex>> final_blob_columns;
|
||||
for (const auto& blob_col : original_blob_columns) {
|
||||
// Check if this column was fetched (GC'd) via linear scan
|
||||
bool was_fetched = false;
|
||||
for (const auto& fv : fetched_blob_values) {
|
||||
if (fv.first == blob_col.first) {
|
||||
was_fetched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!was_fetched) {
|
||||
// This blob column wasn't GC'd - keep original
|
||||
final_blob_columns.push_back(blob_col);
|
||||
}
|
||||
}
|
||||
// Add newly relocated blobs
|
||||
for (const auto& nb : new_blob_columns) {
|
||||
final_blob_columns.push_back(nb);
|
||||
}
|
||||
|
||||
// Build WideColumns for serialization, replacing fetched blob values
|
||||
// with their resolved inline values.
|
||||
WideColumns wide_columns;
|
||||
wide_columns.reserve(columns.size());
|
||||
|
||||
for (size_t i = 0; i < columns.size(); ++i) {
|
||||
// Linear scan to find if this column was fetched
|
||||
const PinnableSlice* fetched_value = nullptr;
|
||||
for (const auto& fv : fetched_blob_values) {
|
||||
if (fv.first == i) {
|
||||
fetched_value = &fv.second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (fetched_value != nullptr) {
|
||||
// This blob column was fetched for GC; use the resolved inline value.
|
||||
wide_columns.emplace_back(columns[i].name(), Slice(*fetched_value));
|
||||
} else {
|
||||
// For inline columns, this is the actual value. For non-fetched blob
|
||||
// columns, this contains the raw serialized blob index, which is fine
|
||||
// because SerializeV2() re-encodes from the BlobIndex
|
||||
// object in final_blob_columns, ignoring this value for blob-type
|
||||
// columns.
|
||||
wide_columns.emplace_back(columns[i].name(), columns[i].value());
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize the entity
|
||||
rewritten_entity_.clear();
|
||||
Status s;
|
||||
if (final_blob_columns.empty()) {
|
||||
s = WideColumnSerialization::Serialize(wide_columns, rewritten_entity_);
|
||||
} else {
|
||||
s = WideColumnSerialization::SerializeV2(wide_columns, final_blob_columns,
|
||||
rewritten_entity_);
|
||||
}
|
||||
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
|
||||
value_ = rewritten_entity_;
|
||||
}
|
||||
|
||||
void CompactionIterator::GarbageCollectEntityBlobsIfNeeded() {
|
||||
assert(ikey_.type == kTypeWideColumnEntity);
|
||||
|
||||
if (!compaction_ || !compaction_->enable_blob_garbage_collection()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This is the second branch in the flow above: the entity already has V2
|
||||
// blob references, so compaction considers only relocating the subset of
|
||||
// referenced blob files that are old enough for blob GC.
|
||||
|
||||
// Use member variables already populated by PrepareOutput's combined
|
||||
// deserialization path. If not yet populated, deserialize here.
|
||||
if (entity_columns_.empty()) {
|
||||
Slice entity_slice = value_;
|
||||
entity_columns_.clear();
|
||||
entity_blob_columns_.clear();
|
||||
Status s = WideColumnSerialization::DeserializeV2(
|
||||
entity_slice, entity_columns_, entity_blob_columns_);
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// The caller (PrepareOutput) already verified blob columns exist.
|
||||
assert(!entity_blob_columns_.empty());
|
||||
if (entity_blob_columns_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch blobs that need GC
|
||||
std::vector<std::pair<size_t, PinnableSlice>> fetched_blob_values;
|
||||
if (!FetchBlobsNeedingGC(entity_columns_, entity_blob_columns_,
|
||||
&fetched_blob_values)) {
|
||||
// Either no blobs needed GC (status_ is OK) or an error occurred
|
||||
// (status_ is already set by FetchBlobsNeedingGC). In both cases,
|
||||
// we return without modifying the entity.
|
||||
return;
|
||||
}
|
||||
|
||||
// Relocate fetched values to new blob files
|
||||
std::vector<std::pair<size_t, BlobIndex>> new_blob_columns =
|
||||
RelocateBlobValues(fetched_blob_values);
|
||||
if (!status_.ok()) {
|
||||
return; // Error occurred during relocation
|
||||
}
|
||||
|
||||
// Serialize the final entity
|
||||
SerializeEntityAfterGC(entity_columns_, entity_blob_columns_,
|
||||
fetched_blob_values, new_blob_columns);
|
||||
}
|
||||
|
||||
void CompactionIterator::PrepareOutput() {
|
||||
if (Valid()) {
|
||||
if (LIKELY(!is_range_del_)) {
|
||||
@@ -1273,6 +1861,37 @@ void CompactionIterator::PrepareOutput() {
|
||||
ExtractLargeValueIfNeeded();
|
||||
} else if (ikey_.type == kTypeBlobIndex) {
|
||||
GarbageCollectBlobIfNeeded();
|
||||
} else if (ikey_.type == kTypeWideColumnEntity) {
|
||||
// If entity was already deserialized (e.g., by InvokeFilterIfNeeded)
|
||||
// and the filter returned kKeep, skip redundant re-deserialization.
|
||||
if (!entity_deserialized_) {
|
||||
TEST_SYNC_POINT(
|
||||
"CompactionIterator::PrepareOutput:DeserializeEntity");
|
||||
// Deserialize entity once into member variables, then decide between
|
||||
// blob GC and extraction based on whether blob columns exist.
|
||||
// This avoids the double parse of HasBlobColumns() + DeserializeV2().
|
||||
entity_columns_.clear();
|
||||
entity_blob_columns_.clear();
|
||||
Slice entity_slice = value_;
|
||||
{
|
||||
Status s_deser = WideColumnSerialization::DeserializeV2(
|
||||
entity_slice, entity_columns_, entity_blob_columns_);
|
||||
if (!s_deser.ok()) {
|
||||
status_ = s_deser;
|
||||
validity_info_.Invalidate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
TEST_SYNC_POINT(
|
||||
"CompactionIterator::PrepareOutput:SkipDeserializeEntity");
|
||||
}
|
||||
entity_deserialized_ = false; // Reset for next iteration
|
||||
if (UNLIKELY(!entity_blob_columns_.empty())) {
|
||||
GarbageCollectEntityBlobsIfNeeded();
|
||||
} else {
|
||||
ExtractLargeColumnValuesIfNeeded();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1421,21 +2040,32 @@ uint64_t CompactionIterator::ComputeBlobGarbageCollectionCutoffFileNumber(
|
||||
}
|
||||
|
||||
std::unique_ptr<BlobFetcher> CompactionIterator::CreateBlobFetcherIfNeeded(
|
||||
const CompactionProxy* compaction) {
|
||||
if (!compaction) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const Version* const version = compaction->input_version();
|
||||
if (!version) {
|
||||
const CompactionProxy* compaction, const Version* input_version,
|
||||
Env::IOActivity blob_read_io_activity) {
|
||||
const Version* const version =
|
||||
compaction != nullptr ? compaction->input_version() : input_version;
|
||||
if (version == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ReadOptions read_options;
|
||||
read_options.io_activity = Env::IOActivity::kCompaction;
|
||||
read_options.io_activity = compaction != nullptr
|
||||
? Env::IOActivity::kCompaction
|
||||
: blob_read_io_activity;
|
||||
read_options.fill_cache = false;
|
||||
|
||||
return std::unique_ptr<BlobFetcher>(new BlobFetcher(version, read_options));
|
||||
BlobFileCache* blob_file_cache = nullptr;
|
||||
bool allow_write_path_fallback = false;
|
||||
if (compaction == nullptr) {
|
||||
allow_write_path_fallback = true;
|
||||
auto* cfd = version->cfd();
|
||||
if (cfd != nullptr) {
|
||||
blob_file_cache = cfd->blob_file_cache();
|
||||
}
|
||||
}
|
||||
|
||||
return std::unique_ptr<BlobFetcher>(new BlobFetcher(
|
||||
version, read_options, blob_file_cache, allow_write_path_fallback));
|
||||
}
|
||||
|
||||
std::unique_ptr<PrefetchBufferCollection>
|
||||
|
||||
@@ -7,10 +7,12 @@
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/compaction/compaction.h"
|
||||
#include "db/compaction/compaction_iteration_stats.h"
|
||||
#include "db/merge_helper.h"
|
||||
@@ -19,12 +21,69 @@
|
||||
#include "db/snapshot_checker.h"
|
||||
#include "options/cf_options.h"
|
||||
#include "rocksdb/compaction_filter.h"
|
||||
#include "rocksdb/slice.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
class BlobFileBuilder;
|
||||
class BlobFileCache;
|
||||
class BlobFetcher;
|
||||
class Version;
|
||||
class PrefetchBufferCollection;
|
||||
class FilePrefetchBuffer;
|
||||
class Version;
|
||||
|
||||
// Internal implementation of WideColumnBlobResolver for compaction.
|
||||
// This class enables lazy loading of blob column values during compaction
|
||||
// filter processing.
|
||||
class CompactionBlobResolver : public WideColumnBlobResolver {
|
||||
public:
|
||||
CompactionBlobResolver()
|
||||
: columns_(nullptr),
|
||||
blob_columns_(nullptr),
|
||||
blob_fetcher_(nullptr),
|
||||
prefetch_buffers_(nullptr),
|
||||
iter_stats_(nullptr) {}
|
||||
|
||||
// Set the fixed context (blob fetcher, prefetch buffers, stats) once.
|
||||
void Init(BlobFetcher* blob_fetcher,
|
||||
PrefetchBufferCollection* prefetch_buffers,
|
||||
CompactionIterationStats* iter_stats);
|
||||
|
||||
Status ResolveColumn(size_t column_index, Slice* resolved_value) override;
|
||||
bool IsBlobColumn(size_t column_index) const override;
|
||||
size_t NumColumns() const override;
|
||||
Status resolve_status() const {
|
||||
if (!resolve_error_.has_value()) {
|
||||
return Status::OK();
|
||||
}
|
||||
return *resolve_error_;
|
||||
}
|
||||
|
||||
// Reset the resolver for a new entity. Clears resolved cache.
|
||||
void Reset(const Slice& user_key, const std::vector<WideColumn>* columns,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>* blob_columns,
|
||||
bool track_resolve_error = false);
|
||||
|
||||
private:
|
||||
Slice user_key_;
|
||||
const std::vector<WideColumn>* columns_;
|
||||
const std::vector<std::pair<size_t, BlobIndex>>* blob_columns_;
|
||||
BlobFetcher* blob_fetcher_;
|
||||
PrefetchBufferCollection* prefetch_buffers_;
|
||||
CompactionIterationStats* iter_stats_;
|
||||
bool track_resolve_error_ = false;
|
||||
|
||||
// Cache for resolved blob values to avoid re-fetching. Uses a vector of
|
||||
// (column_index, PinnableSlice) pairs — typical entities have few blob
|
||||
// columns (<5), making linear scan cheaper than hash map overhead.
|
||||
std::vector<std::pair<size_t, std::unique_ptr<PinnableSlice>>>
|
||||
resolved_cache_;
|
||||
// Sticky resolver error for the current entity. This is enabled only for the
|
||||
// lazy FilterV4 path so compaction can still fail even if the filter
|
||||
// notices the error and returns kKeep.
|
||||
std::optional<Status> resolve_error_;
|
||||
};
|
||||
|
||||
// A wrapper of internal iterator whose purpose is to count how
|
||||
// many entries there are in the iterator.
|
||||
@@ -213,34 +272,43 @@ class CompactionIterator {
|
||||
const std::atomic<bool>* shutting_down = nullptr,
|
||||
const std::shared_ptr<Logger> info_log = nullptr,
|
||||
const std::string* full_history_ts_low = nullptr,
|
||||
std::optional<SequenceNumber> preserve_seqno_min = {});
|
||||
std::optional<SequenceNumber> preserve_seqno_min = {},
|
||||
const Version* input_version = nullptr,
|
||||
Env::IOActivity blob_read_io_activity = Env::IOActivity::kCompaction);
|
||||
|
||||
// Constructor with custom CompactionProxy, used for tests.
|
||||
CompactionIterator(InternalIterator* input, const Comparator* cmp,
|
||||
MergeHelper* merge_helper, SequenceNumber last_sequence,
|
||||
std::vector<SequenceNumber>* snapshots,
|
||||
SequenceNumber earliest_snapshot,
|
||||
SequenceNumber earliest_write_conflict_snapshot,
|
||||
SequenceNumber job_snapshot,
|
||||
const SnapshotChecker* snapshot_checker, Env* env,
|
||||
bool report_detailed_time,
|
||||
CompactionRangeDelAggregator* range_del_agg,
|
||||
BlobFileBuilder* blob_file_builder,
|
||||
bool allow_data_in_errors,
|
||||
bool enforce_single_del_contracts,
|
||||
const std::atomic<bool>& manual_compaction_canceled,
|
||||
std::unique_ptr<CompactionProxy> compaction,
|
||||
bool must_count_input_entries,
|
||||
const CompactionFilter* compaction_filter = nullptr,
|
||||
const std::atomic<bool>* shutting_down = nullptr,
|
||||
const std::shared_ptr<Logger> info_log = nullptr,
|
||||
const std::string* full_history_ts_low = nullptr,
|
||||
std::optional<SequenceNumber> preserve_seqno_min = {});
|
||||
CompactionIterator(
|
||||
InternalIterator* input, const Comparator* cmp, MergeHelper* merge_helper,
|
||||
SequenceNumber last_sequence, std::vector<SequenceNumber>* snapshots,
|
||||
SequenceNumber earliest_snapshot,
|
||||
SequenceNumber earliest_write_conflict_snapshot,
|
||||
SequenceNumber job_snapshot, const SnapshotChecker* snapshot_checker,
|
||||
Env* env, bool report_detailed_time,
|
||||
CompactionRangeDelAggregator* range_del_agg,
|
||||
BlobFileBuilder* blob_file_builder, bool allow_data_in_errors,
|
||||
bool enforce_single_del_contracts,
|
||||
const std::atomic<bool>& manual_compaction_canceled,
|
||||
std::unique_ptr<CompactionProxy> compaction,
|
||||
bool must_count_input_entries,
|
||||
const CompactionFilter* compaction_filter = nullptr,
|
||||
const std::atomic<bool>* shutting_down = nullptr,
|
||||
const std::shared_ptr<Logger> info_log = nullptr,
|
||||
const std::string* full_history_ts_low = nullptr,
|
||||
std::optional<SequenceNumber> preserve_seqno_min = {},
|
||||
const Version* input_version = nullptr,
|
||||
Env::IOActivity blob_read_io_activity = Env::IOActivity::kCompaction);
|
||||
|
||||
~CompactionIterator();
|
||||
|
||||
void ResetRecordCounts();
|
||||
|
||||
// Non-compaction table-file creation (e.g., flush/recovery) can still need
|
||||
// blob resolution when direct-write wide entities already contain blob
|
||||
// references. Plumb a fetcher explicitly in those cases.
|
||||
void SetBlobFetcher(const Version* version, BlobFileCache* blob_file_cache,
|
||||
Env::IOActivity io_activity,
|
||||
bool allow_write_path_fallback);
|
||||
|
||||
// Seek to the beginning of the compaction iterator output.
|
||||
//
|
||||
// REQUIRED: Call only once.
|
||||
@@ -302,6 +370,12 @@ class CompactionIterator {
|
||||
// for regular values (kTypeValue).
|
||||
void ExtractLargeValueIfNeeded();
|
||||
|
||||
// Extracts large column values from wide column entities to blob files.
|
||||
// For each column whose value size >= min_blob_size, the value is written
|
||||
// to a blob file and replaced with a blob index reference. Should only be
|
||||
// called for wide column entities (kTypeWideColumnEntity).
|
||||
void ExtractLargeColumnValuesIfNeeded();
|
||||
|
||||
// Relocates valid blobs residing in the oldest blob files if garbage
|
||||
// collection is enabled. Relocated blobs are written to new blob files or
|
||||
// inlined in the LSM tree depending on the current settings (i.e.
|
||||
@@ -312,6 +386,39 @@ class CompactionIterator {
|
||||
// algorithm is also called from here.
|
||||
void GarbageCollectBlobIfNeeded();
|
||||
|
||||
// Garbage collects blob references within wide column entities. For entities
|
||||
// that contain blob column references, this method checks if any of the
|
||||
// referenced blob files need garbage collection. If so, the blob values are
|
||||
// fetched and either relocated to new blob files or inlined based on
|
||||
// current settings. Should only be called for wide column entities
|
||||
// (kTypeWideColumnEntity) that have blob columns.
|
||||
void GarbageCollectEntityBlobsIfNeeded();
|
||||
|
||||
// Helper for GarbageCollectEntityBlobsIfNeeded: Fetches blob values that need
|
||||
// garbage collection based on the cutoff file number.
|
||||
// Returns true if any blobs were fetched, false otherwise.
|
||||
// On error, sets status_ and returns false.
|
||||
bool FetchBlobsNeedingGC(
|
||||
const std::vector<WideColumn>& columns,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& blob_columns,
|
||||
std::vector<std::pair<size_t, PinnableSlice>>* fetched_blob_values);
|
||||
|
||||
// Helper for GarbageCollectEntityBlobsIfNeeded: Relocates fetched blob
|
||||
// values to new blob files if blob_file_builder_ is available.
|
||||
// Returns the new blob column indices. On error, sets status_ and returns
|
||||
// empty.
|
||||
std::vector<std::pair<size_t, BlobIndex>> RelocateBlobValues(
|
||||
const std::vector<std::pair<size_t, PinnableSlice>>& fetched_blob_values);
|
||||
|
||||
// Helper for GarbageCollectEntityBlobsIfNeeded: Serializes the entity after
|
||||
// garbage collection, combining original columns with fetched values and
|
||||
// new blob indices. On error, sets status_.
|
||||
void SerializeEntityAfterGC(
|
||||
const std::vector<WideColumn>& columns,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& original_blob_columns,
|
||||
const std::vector<std::pair<size_t, PinnableSlice>>& fetched_blob_values,
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& new_blob_columns);
|
||||
|
||||
// Invoke compaction filter if needed.
|
||||
// Return true on success, false on failures (e.g.: kIOError).
|
||||
bool InvokeFilterIfNeeded(bool* need_skip, Slice* skip_until);
|
||||
@@ -352,7 +459,8 @@ class CompactionIterator {
|
||||
static uint64_t ComputeBlobGarbageCollectionCutoffFileNumber(
|
||||
const CompactionProxy* compaction);
|
||||
static std::unique_ptr<BlobFetcher> CreateBlobFetcherIfNeeded(
|
||||
const CompactionProxy* compaction);
|
||||
const CompactionProxy* compaction, const Version* input_version,
|
||||
Env::IOActivity blob_read_io_activity);
|
||||
static std::unique_ptr<PrefetchBufferCollection>
|
||||
CreatePrefetchBufferCollectionIfNeeded(const CompactionProxy* compaction);
|
||||
|
||||
@@ -376,6 +484,7 @@ class CompactionIterator {
|
||||
BlobFileBuilder* blob_file_builder_;
|
||||
std::unique_ptr<CompactionProxy> compaction_;
|
||||
const CompactionFilter* compaction_filter_;
|
||||
const bool filter_supports_v4_;
|
||||
const std::atomic<bool>* shutting_down_;
|
||||
const std::atomic<bool>& manual_compaction_canceled_;
|
||||
const bool bottommost_level_;
|
||||
@@ -479,6 +588,32 @@ class CompactionIterator {
|
||||
PinnableSlice blob_value_;
|
||||
std::string compaction_filter_value_;
|
||||
InternalKey compaction_filter_skip_until_;
|
||||
// Buffer for storing rewritten entity with blob references after
|
||||
// extracting large column values to blob files.
|
||||
std::string rewritten_entity_;
|
||||
// Cached deserialized columns for the current wide-column entity, reused
|
||||
// across iterations to avoid per-key heap allocations. This is the full
|
||||
// sorted column set. For blob-backed columns, value() initially contains the
|
||||
// serialized BlobIndex bytes from the entity.
|
||||
std::vector<WideColumn> entity_columns_;
|
||||
// Side list for the blob-backed subset of entity_columns_. Each pair is
|
||||
// (column_index_in_entity_columns_, decoded_blob_index), so callers can tell
|
||||
// which entries in entity_columns_ are blob references without reparsing.
|
||||
std::vector<std::pair<size_t, BlobIndex>> entity_blob_columns_;
|
||||
// Temporary Slice-based view over entity_columns_ used when reserializing an
|
||||
// entity after blob extraction, to avoid copying names/values into strings.
|
||||
WideColumns entity_wide_columns_;
|
||||
// Reusable storage for existing_columns in InvokeFilterIfNeeded,
|
||||
// avoiding per-key heap allocations.
|
||||
WideColumns filter_existing_columns_;
|
||||
// Reusable blob resolver for compaction filter, avoiding per-key heap
|
||||
// allocation. Init() is called once; Reset() is called per entity.
|
||||
CompactionBlobResolver blob_resolver_;
|
||||
// True when entity_columns_/entity_blob_columns_ already describe the
|
||||
// current input record (e.g., because InvokeFilterIfNeeded() deserialized
|
||||
// it) and PrepareOutput() can skip redundant work. Reset for each candidate
|
||||
// input record and whenever the filter rewrites the entity value.
|
||||
bool entity_deserialized_{false};
|
||||
// "level_ptrs" holds indices that remember which file of an associated
|
||||
// level we were last checking during the last call to compaction->
|
||||
// KeyNotExistsBeyondOutputLevel(). This allows future calls to the function
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -58,6 +58,7 @@
|
||||
#include "table/table_builder.h"
|
||||
#include "table/unique_id_impl.h"
|
||||
#include "test_util/sync_point.h"
|
||||
#include "util/hash_containers.h"
|
||||
#include "util/stop_watch.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
@@ -104,6 +105,8 @@ const char* GetCompactionReasonString(CompactionReason compaction_reason) {
|
||||
return "RoundRobinTtl";
|
||||
case CompactionReason::kRefitLevel:
|
||||
return "RefitLevel";
|
||||
case CompactionReason::kReadTriggered:
|
||||
return "ReadTriggered";
|
||||
case CompactionReason::kNumOfReasons:
|
||||
// fall through
|
||||
default:
|
||||
@@ -896,6 +899,10 @@ Status CompactionJob::VerifyOutputFiles() {
|
||||
}
|
||||
|
||||
auto verify_table = [&](SubcompactionState& subcompaction_state) {
|
||||
// Collect file open metadata during verification when fast_sst_open
|
||||
// is enabled, keyed by file number.
|
||||
UnorderedMap<uint64_t, std::string> file_open_metadata_map;
|
||||
|
||||
for (const auto& output_file : subcompaction_state.GetOutputs()) {
|
||||
// Verify that the table is usable
|
||||
// We set for_compaction to false and don't
|
||||
@@ -913,6 +920,10 @@ Status CompactionJob::VerifyOutputFiles() {
|
||||
TableReader* table_reader_ptr = table_reader_guard.get();
|
||||
verification_read_options.rate_limiter_priority =
|
||||
GetRateLimiterPriority();
|
||||
std::string file_open_metadata;
|
||||
std::string* file_open_metadata_ptr =
|
||||
mutable_db_options_copy_.fast_sst_open ? &file_open_metadata
|
||||
: nullptr;
|
||||
InternalIterator* iter = cfd->table_cache()->NewIterator(
|
||||
verification_read_options, file_options_, cfd->internal_comparator(),
|
||||
output_file.meta,
|
||||
@@ -925,7 +936,10 @@ Status CompactionJob::VerifyOutputFiles() {
|
||||
MaxFileSizeForL0MetaPin(compact_->compaction->mutable_cf_options()),
|
||||
/*smallest_compaction_key=*/nullptr,
|
||||
/*largest_compaction_key=*/nullptr,
|
||||
/*allow_unprepared_value=*/false);
|
||||
/*allow_unprepared_value=*/false,
|
||||
/*range_del_read_seqno=*/nullptr,
|
||||
/*range_del_iter=*/nullptr,
|
||||
/*maybe_pin_table_handle=*/false, file_open_metadata_ptr);
|
||||
auto s = iter->status();
|
||||
if (s.ok()) {
|
||||
// Check for remote/local compaction and verify_output_flags flags
|
||||
@@ -1000,6 +1014,27 @@ Status CompactionJob::VerifyOutputFiles() {
|
||||
subcompaction_state.status = s;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!file_open_metadata.empty()) {
|
||||
file_open_metadata_map[output_file.meta.fd.GetNumber()] =
|
||||
std::move(file_open_metadata);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply collected file open metadata to mutable outputs
|
||||
if (!file_open_metadata_map.empty()) {
|
||||
auto apply_metadata =
|
||||
[&file_open_metadata_map](
|
||||
std::vector<CompactionOutputs::Output>& outputs) {
|
||||
for (auto& output : outputs) {
|
||||
auto it = file_open_metadata_map.find(output.meta.fd.GetNumber());
|
||||
if (it != file_open_metadata_map.end()) {
|
||||
output.meta.file_open_metadata = std::move(it->second);
|
||||
}
|
||||
}
|
||||
};
|
||||
apply_metadata(subcompaction_state.GetMutableCompactionOutputs());
|
||||
apply_metadata(subcompaction_state.GetMutableProximalOutputs());
|
||||
}
|
||||
};
|
||||
for (size_t i = 1; i < compact_->sub_compact_states.size(); i++) {
|
||||
@@ -1305,6 +1340,16 @@ Status CompactionJob::Install(bool* compaction_released) {
|
||||
<< pl_stats.bytes_written_blob;
|
||||
}
|
||||
|
||||
// Propagate Install failure to compact_->status so that
|
||||
// CleanupCompaction() -> SubcompactionState::Cleanup() sees the failure and
|
||||
// calls ReleaseObsolete on output files' table cache entries. Without this,
|
||||
// if Run() succeeds but InstallCompactionResults() fails, Cleanup would see
|
||||
// overall_status = OK and skip ReleaseObsolete, leaking entries for output
|
||||
// files that were never installed into any Version.
|
||||
if (!status.ok() && compact_->status.ok()) {
|
||||
compact_->status = status;
|
||||
}
|
||||
|
||||
CleanupCompaction();
|
||||
return status;
|
||||
}
|
||||
@@ -2353,11 +2398,16 @@ Status CompactionJob::InstallCompactionResults(bool* compaction_released) {
|
||||
*compaction_released = true;
|
||||
};
|
||||
|
||||
return versions_->LogAndApply(compaction->column_family_data(), read_options,
|
||||
write_options, edit, db_mutex_, db_directory_,
|
||||
/*new_descriptor_log=*/false,
|
||||
/*column_family_options=*/nullptr,
|
||||
manifest_wcb);
|
||||
Status s;
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
"CompactionJob::InstallCompactionResults:BeforeLogAndApply", &s);
|
||||
if (s.ok()) {
|
||||
s = versions_->LogAndApply(compaction->column_family_data(), read_options,
|
||||
write_options, edit, db_mutex_, db_directory_,
|
||||
/*new_descriptor_log=*/false,
|
||||
/*column_family_options=*/nullptr, manifest_wcb);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void CompactionJob::RecordCompactionIOStats() {
|
||||
|
||||
@@ -60,6 +60,7 @@ class CompactionOutputs {
|
||||
}
|
||||
|
||||
const std::vector<Output>& GetOutputs() const { return outputs_; }
|
||||
std::vector<Output>& GetMutableOutputs() { return outputs_; }
|
||||
|
||||
// Set new table builder for the current output
|
||||
void NewBuilder(const TableBuilderOptions& tboptions);
|
||||
|
||||
@@ -36,6 +36,9 @@ bool LevelCompactionPicker::NeedsCompaction(
|
||||
if (!vstorage->FilesMarkedForForcedBlobGC().empty()) {
|
||||
return true;
|
||||
}
|
||||
if (!vstorage->ReadTriggeredCompactionFiles().empty()) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i <= vstorage->MaxInputLevel(); i++) {
|
||||
if (vstorage->CompactionScore(i) >= 1) {
|
||||
return true;
|
||||
@@ -325,6 +328,14 @@ void LevelCompactionBuilder::SetupInitialFiles() {
|
||||
compaction_reason_ = CompactionReason::kForcedBlobGC;
|
||||
return;
|
||||
}
|
||||
|
||||
// Read-triggered compaction
|
||||
PickFileToCompact(vstorage_->ReadTriggeredCompactionFiles(),
|
||||
CompactToNextLevel::kYes);
|
||||
if (!start_level_inputs_.empty()) {
|
||||
compaction_reason_ = CompactionReason::kReadTriggered;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool LevelCompactionBuilder::SetupOtherL0FilesIfNeeded() {
|
||||
|
||||
@@ -984,6 +984,186 @@ TEST_F(CompactionPickerTest, UniversalIncrementalSpace1) {
|
||||
ASSERT_EQ(14U, compaction->input(1, 3)->fd.GetNumber());
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, ReadTriggeredCompactionDisabled) {
|
||||
NewVersionStorage(6, kCompactionStyleLevel);
|
||||
// threshold=0 means disabled, no files should be marked
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.0;
|
||||
Add(0, 1U, "100", "200", 1000U);
|
||||
file_map_[1U].first->stats.num_collapsible_entry_reads_sampled.store(999999);
|
||||
UpdateVersionStorageInfo();
|
||||
ASSERT_TRUE(vstorage_->ReadTriggeredCompactionFiles().empty());
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, ReadTriggeredCompactionBelowThreshold) {
|
||||
NewVersionStorage(6, kCompactionStyleLevel);
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 1.0;
|
||||
// file_size=1000, reads=500 => reads_per_byte=0.5 < 1.0
|
||||
Add(0, 1U, "100", "200", 1000U);
|
||||
file_map_[1U].first->stats.num_collapsible_entry_reads_sampled.store(500);
|
||||
UpdateVersionStorageInfo();
|
||||
ASSERT_TRUE(vstorage_->ReadTriggeredCompactionFiles().empty());
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, ReadTriggeredCompactionAboveThreshold) {
|
||||
NewVersionStorage(6, kCompactionStyleLevel);
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.5;
|
||||
// file_size=1000, reads=600 => reads_per_byte=0.6 > 0.5
|
||||
Add(0, 1U, "100", "200", 1000U);
|
||||
file_map_[1U].first->stats.num_collapsible_entry_reads_sampled.store(600);
|
||||
// file_size=1000, reads=300 => reads_per_byte=0.3 < 0.5 (not marked)
|
||||
Add(1, 2U, "300", "400", 1000U);
|
||||
file_map_[2U].first->stats.num_collapsible_entry_reads_sampled.store(300);
|
||||
// file_size=1000, reads=800 => reads_per_byte=0.8 > 0.5 (hottest)
|
||||
Add(2, 3U, "500", "600", 1000U);
|
||||
file_map_[3U].first->stats.num_collapsible_entry_reads_sampled.store(800);
|
||||
// Add a file at the bottom so L2 is not the last non-empty level
|
||||
Add(4, 4U, "700", "800", 1000U);
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
const auto& marked = vstorage_->ReadTriggeredCompactionFiles();
|
||||
ASSERT_EQ(marked.size(), 2);
|
||||
// Sorted by reads_per_byte descending: file 3 (0.8) then file 1 (0.6)
|
||||
ASSERT_EQ(marked[0].second->fd.GetNumber(), 3U);
|
||||
ASSERT_EQ(marked[1].second->fd.GetNumber(), 1U);
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, NeedsCompactionReadTriggered) {
|
||||
NewVersionStorage(6, kCompactionStyleLevel);
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.1;
|
||||
Add(1, 1U, "100", "200", 1000U);
|
||||
file_map_[1U].first->stats.num_collapsible_entry_reads_sampled.store(500);
|
||||
Add(3, 2U, "300", "400", 1000U);
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
ASSERT_FALSE(vstorage_->ReadTriggeredCompactionFiles().empty());
|
||||
ASSERT_TRUE(level_compaction_picker.NeedsCompaction(vstorage_.get()));
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, ReadTriggeredPicksFile) {
|
||||
NewVersionStorage(6, kCompactionStyleLevel);
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.1;
|
||||
Add(1, 1U, "100", "200", 1000U);
|
||||
file_map_[1U].first->stats.num_collapsible_entry_reads_sampled.store(500);
|
||||
Add(3, 2U, "300", "400", 1000U);
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
|
||||
cf_name_, mutable_cf_options_, mutable_db_options_,
|
||||
/*existing_snapshots=*/{}, /*snapshot_checker=*/nullptr, vstorage_.get(),
|
||||
&log_buffer_, /*full_history_ts_low=*/""));
|
||||
ASSERT_TRUE(compaction.get() != nullptr);
|
||||
ASSERT_EQ(compaction->compaction_reason(), CompactionReason::kReadTriggered);
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, UniversalReadTriggeredCompaction) {
|
||||
const uint64_t kFileSize = 100000;
|
||||
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.001;
|
||||
// Set trigger high so size amp / sorted run pickers don't fire
|
||||
mutable_cf_options_.level0_file_num_compaction_trigger = 10;
|
||||
UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
|
||||
|
||||
NewVersionStorage(5, kCompactionStyleUniversal);
|
||||
|
||||
// Hot file at L2 with data at L4 below it
|
||||
Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
|
||||
Add(2, 2U, "301", "350", kFileSize, 0, 201, 250);
|
||||
Add(4, 3U, "301", "400", kFileSize, 0, 101, 150);
|
||||
|
||||
// Mark file 2 (L2) as having high reads
|
||||
file_map_[2U].first->stats.num_collapsible_entry_reads_sampled.store(
|
||||
kFileSize);
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
ASSERT_TRUE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
|
||||
|
||||
std::unique_ptr<Compaction> compaction(
|
||||
universal_compaction_picker.PickCompaction(
|
||||
cf_name_, mutable_cf_options_, mutable_db_options_,
|
||||
/*existing_snapshots=*/{}, /*snapshot_checker=*/nullptr,
|
||||
vstorage_.get(), &log_buffer_, /*full_history_ts_low=*/""));
|
||||
|
||||
ASSERT_TRUE(compaction);
|
||||
ASSERT_EQ(compaction->compaction_reason(), CompactionReason::kReadTriggered);
|
||||
ASSERT_EQ(compaction->start_level(), 2);
|
||||
ASSERT_EQ(compaction->output_level(), 4);
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, ReadTriggeredSkipsLastLevel) {
|
||||
const uint64_t kFileSize = 100000;
|
||||
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.001;
|
||||
mutable_cf_options_.level0_file_num_compaction_trigger = 10;
|
||||
UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
|
||||
|
||||
NewVersionStorage(5, kCompactionStyleUniversal);
|
||||
|
||||
Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
|
||||
Add(4, 3U, "301", "350", kFileSize, 0, 101, 150);
|
||||
|
||||
// File 3 is at the last non-empty level — should NOT be marked for
|
||||
// read-triggered compaction. Bottommost file cleanup is handled
|
||||
// separately by ComputeBottommostFilesMarkedForCompaction().
|
||||
file_map_[3U].first->stats.num_collapsible_entry_reads_sampled.store(
|
||||
kFileSize);
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
ASSERT_TRUE(vstorage_->ReadTriggeredCompactionFiles().empty());
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, UniversalReadTriggeredNoPickWhenNotMarked) {
|
||||
const uint64_t kFileSize = 100000;
|
||||
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.001;
|
||||
UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
|
||||
|
||||
NewVersionStorage(5, kCompactionStyleUniversal);
|
||||
|
||||
Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
|
||||
Add(4, 3U, "301", "350", kFileSize, 0, 101, 150);
|
||||
|
||||
// No reads on any file
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
ASSERT_TRUE(vstorage_->ReadTriggeredCompactionFiles().empty());
|
||||
// Not enough sorted runs to trigger compaction either
|
||||
ASSERT_FALSE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, UniversalReadTriggeredIntraL0) {
|
||||
const uint64_t kFileSize = 100000;
|
||||
|
||||
mutable_cf_options_.read_triggered_compaction_threshold = 0.001;
|
||||
mutable_cf_options_.level0_file_num_compaction_trigger = 10;
|
||||
UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
|
||||
|
||||
// Single-level universal with overlapping L0 files
|
||||
NewVersionStorage(1, kCompactionStyleUniversal);
|
||||
|
||||
// Two L0 files with overlapping key ranges
|
||||
Add(0, 1U, "100", "300", kFileSize, 0, 500, 550);
|
||||
Add(0, 2U, "200", "400", kFileSize, 0, 400, 450);
|
||||
|
||||
// Mark file 1 as hot
|
||||
file_map_[1U].first->stats.num_collapsible_entry_reads_sampled.store(
|
||||
kFileSize);
|
||||
UpdateVersionStorageInfo();
|
||||
|
||||
ASSERT_FALSE(vstorage_->ReadTriggeredCompactionFiles().empty());
|
||||
ASSERT_TRUE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
|
||||
|
||||
std::unique_ptr<Compaction> compaction(
|
||||
universal_compaction_picker.PickCompaction(
|
||||
cf_name_, mutable_cf_options_, mutable_db_options_,
|
||||
/*existing_snapshots=*/{}, /*snapshot_checker=*/nullptr,
|
||||
vstorage_.get(), &log_buffer_, /*full_history_ts_low=*/""));
|
||||
|
||||
ASSERT_TRUE(compaction);
|
||||
ASSERT_EQ(compaction->compaction_reason(), CompactionReason::kReadTriggered);
|
||||
ASSERT_EQ(compaction->start_level(), 0);
|
||||
ASSERT_EQ(compaction->output_level(), 0);
|
||||
}
|
||||
|
||||
TEST_F(CompactionPickerTest, UniversalIncrementalSpace2) {
|
||||
const uint64_t kFileSize = 100000;
|
||||
|
||||
|
||||
@@ -275,6 +275,21 @@ class UniversalCompactionBuilder {
|
||||
return c;
|
||||
}
|
||||
|
||||
Compaction* MaybePickReadTriggeredCompaction(
|
||||
Compaction* const prev_picked_c) {
|
||||
if (prev_picked_c != nullptr ||
|
||||
vstorage_->ReadTriggeredCompactionFiles().empty()) {
|
||||
return prev_picked_c;
|
||||
}
|
||||
Compaction* c = PickReadTriggeredCompaction();
|
||||
if (c != nullptr) {
|
||||
ROCKS_LOG_BUFFER(log_buffer_,
|
||||
"[%s] Universal: picked for read triggered compaction\n",
|
||||
cf_name_.c_str());
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
// Pick Universal compaction to limit read amplification
|
||||
Compaction* PickCompactionToReduceSortedRuns(
|
||||
unsigned int ratio, unsigned int max_number_of_files_to_compact);
|
||||
@@ -292,6 +307,14 @@ class UniversalCompactionBuilder {
|
||||
|
||||
Compaction* PickDeleteTriggeredCompaction();
|
||||
|
||||
Compaction* PickReadTriggeredCompaction();
|
||||
|
||||
// Given already-selected start_level_inputs, find the first non-empty output
|
||||
// level, compute overlapping inputs, and create a Compaction. Can produce
|
||||
// intra-L0 compaction if there is only level 0.
|
||||
Compaction* BuildCompactionToNextLevel(
|
||||
CompactionInputFiles& start_level_inputs, CompactionReason reason);
|
||||
|
||||
// Returns true if this given file (that is marked be compaction) should be
|
||||
// skipped from being picked for now. We do this to best use standalone range
|
||||
// tombstone files.
|
||||
@@ -594,6 +617,9 @@ bool UniversalCompactionPicker::NeedsCompaction(
|
||||
if (!vstorage->FilesMarkedForCompaction().empty()) {
|
||||
return true;
|
||||
}
|
||||
if (!vstorage->ReadTriggeredCompactionFiles().empty()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -759,6 +785,7 @@ Compaction* UniversalCompactionBuilder::PickCompaction() {
|
||||
if (sorted_runs_.size() == 0 ||
|
||||
(vstorage_->FilesMarkedForPeriodicCompaction().empty() &&
|
||||
vstorage_->FilesMarkedForCompaction().empty() &&
|
||||
vstorage_->ReadTriggeredCompactionFiles().empty() &&
|
||||
sorted_runs_.size() < (unsigned int)file_num_compaction_trigger)) {
|
||||
ROCKS_LOG_BUFFER(log_buffer_, "[%s] Universal: nothing to do\n",
|
||||
cf_name_.c_str());
|
||||
@@ -782,6 +809,7 @@ Compaction* UniversalCompactionBuilder::PickCompaction() {
|
||||
c = MaybePickCompactionToReduceSortedRuns(c, file_num_compaction_trigger,
|
||||
ratio);
|
||||
c = MaybePickDeleteTriggeredCompaction(c);
|
||||
c = MaybePickReadTriggeredCompaction(c);
|
||||
|
||||
if (c == nullptr) {
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
@@ -1461,9 +1489,6 @@ Compaction* UniversalCompactionBuilder::PickIncrementalForReduceSizeAmp(
|
||||
// CompactOnDeleteCollector due to the presence of tombstones.
|
||||
Compaction* UniversalCompactionBuilder::PickDeleteTriggeredCompaction() {
|
||||
CompactionInputFiles start_level_inputs;
|
||||
int output_level;
|
||||
std::vector<CompactionInputFiles> inputs;
|
||||
std::vector<FileMetaData*> grandparents;
|
||||
|
||||
if (vstorage_->num_levels() == 1) {
|
||||
// This is single level universal. Since we're basically trying to reclaim
|
||||
@@ -1474,7 +1499,6 @@ Compaction* UniversalCompactionBuilder::PickDeleteTriggeredCompaction() {
|
||||
|
||||
start_level_inputs.level = 0;
|
||||
start_level_inputs.files.clear();
|
||||
output_level = 0;
|
||||
// Find the first file marked for compaction. Ignore the last file
|
||||
for (size_t loop = 0; loop + 1 < sorted_runs_.size(); loop++) {
|
||||
SortedRun* sr = &sorted_runs_[loop];
|
||||
@@ -1503,13 +1527,9 @@ Compaction* UniversalCompactionBuilder::PickDeleteTriggeredCompaction() {
|
||||
FileMetaData* f = vstorage_->LevelFiles(0)[loop];
|
||||
start_level_inputs.files.push_back(f);
|
||||
}
|
||||
if (start_level_inputs.size() <= 1) {
|
||||
// If only the last file in L0 is marked for compaction, ignore it
|
||||
return nullptr;
|
||||
}
|
||||
inputs.push_back(start_level_inputs);
|
||||
} else {
|
||||
int start_level;
|
||||
int output_level;
|
||||
|
||||
// For multi-level universal, the strategy is to make this look more like
|
||||
// leveled. We pick one of the files marked for compaction and compact with
|
||||
@@ -1522,100 +1542,10 @@ Compaction* UniversalCompactionBuilder::PickDeleteTriggeredCompaction() {
|
||||
if (start_level_inputs.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int max_output_level = vstorage_->MaxOutputLevel(allow_ingest_behind_);
|
||||
// Pick the first non-empty level after the start_level
|
||||
for (output_level = start_level + 1; output_level <= max_output_level;
|
||||
output_level++) {
|
||||
if (vstorage_->NumLevelFiles(output_level) != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If all higher levels are empty, pick the highest level as output level
|
||||
if (output_level > max_output_level) {
|
||||
if (start_level == 0) {
|
||||
output_level = max_output_level;
|
||||
} else {
|
||||
// If start level is non-zero and all higher levels are empty, this
|
||||
// compaction will translate into a trivial move. Since the idea is
|
||||
// to reclaim space and trivial move doesn't help with that, we
|
||||
// skip compaction in this case and return nullptr
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
assert(output_level <= max_output_level);
|
||||
|
||||
if (!MeetsOutputLevelRequirements(output_level)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (output_level != 0) {
|
||||
// For standalone range deletion, we don't want to compact it with newer
|
||||
// L0 files that it doesn't cover.
|
||||
const FileMetaData* starting_l0_file =
|
||||
(start_level == 0 && start_level_inputs.size() == 1 &&
|
||||
start_level_inputs.files[0]->FileIsStandAloneRangeTombstone())
|
||||
? start_level_inputs.files[0]
|
||||
: nullptr;
|
||||
|
||||
if (start_level == 0) {
|
||||
if (!picker_->GetOverlappingL0Files(vstorage_, &start_level_inputs,
|
||||
output_level, nullptr,
|
||||
starting_l0_file)) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
CompactionInputFiles output_level_inputs;
|
||||
int parent_index = -1;
|
||||
|
||||
output_level_inputs.level = output_level;
|
||||
if (!picker_->SetupOtherInputs(cf_name_, mutable_cf_options_, vstorage_,
|
||||
&start_level_inputs, &output_level_inputs,
|
||||
&parent_index, -1, false,
|
||||
starting_l0_file)) {
|
||||
return nullptr;
|
||||
}
|
||||
inputs.push_back(start_level_inputs);
|
||||
if (!output_level_inputs.empty()) {
|
||||
inputs.push_back(output_level_inputs);
|
||||
}
|
||||
if (picker_->FilesRangeOverlapWithCompaction(
|
||||
inputs, output_level,
|
||||
Compaction::EvaluateProximalLevel(vstorage_, mutable_cf_options_,
|
||||
ioptions_, start_level,
|
||||
output_level))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
picker_->GetGrandparents(vstorage_, start_level_inputs,
|
||||
output_level_inputs, &grandparents);
|
||||
} else {
|
||||
inputs.push_back(start_level_inputs);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t estimated_total_size = 0;
|
||||
// Use size of the output level as estimated file size
|
||||
for (FileMetaData* f : vstorage_->LevelFiles(output_level)) {
|
||||
estimated_total_size += f->fd.GetFileSize();
|
||||
}
|
||||
uint32_t path_id =
|
||||
GetPathId(ioptions_, mutable_cf_options_, estimated_total_size);
|
||||
return new Compaction(
|
||||
vstorage_, ioptions_, mutable_cf_options_, mutable_db_options_,
|
||||
std::move(inputs), output_level,
|
||||
MaxFileSizeForLevel(mutable_cf_options_, output_level,
|
||||
kCompactionStyleUniversal),
|
||||
/* max_grandparent_overlap_bytes */ GetMaxOverlappingBytes(), path_id,
|
||||
GetCompressionType(vstorage_, mutable_cf_options_, output_level, 1),
|
||||
GetCompressionOptions(mutable_cf_options_, vstorage_, output_level),
|
||||
Temperature::kUnknown,
|
||||
/* max_subcompactions */ 0, grandparents, earliest_snapshot_,
|
||||
snapshot_checker_, CompactionReason::kFilesMarkedForCompaction,
|
||||
/* trim_ts */ "", score_,
|
||||
/* l0_files_might_overlap */ true);
|
||||
return BuildCompactionToNextLevel(
|
||||
start_level_inputs, CompactionReason::kFilesMarkedForCompaction);
|
||||
}
|
||||
|
||||
Compaction* UniversalCompactionBuilder::PickCompactionToOldest(
|
||||
@@ -1782,6 +1712,153 @@ Compaction* UniversalCompactionBuilder::PickPeriodicCompaction() {
|
||||
return c;
|
||||
}
|
||||
|
||||
Compaction* UniversalCompactionBuilder::PickReadTriggeredCompaction() {
|
||||
ROCKS_LOG_BUFFER(log_buffer_, "[%s] Universal: Read Triggered Compaction",
|
||||
cf_name_.c_str());
|
||||
|
||||
if (vstorage_->ReadTriggeredCompactionFiles().empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CompactionInputFiles start_level_inputs;
|
||||
int start_level = -1;
|
||||
|
||||
for (const auto& level_file : vstorage_->ReadTriggeredCompactionFiles()) {
|
||||
assert(!level_file.second->being_compacted);
|
||||
start_level = level_file.first;
|
||||
|
||||
if (start_level == 0 &&
|
||||
!picker_->level0_compactions_in_progress()->empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
start_level_inputs.files = {level_file.second};
|
||||
start_level_inputs.level = start_level;
|
||||
if (picker_->ExpandInputsToCleanCut(cf_name_, vstorage_,
|
||||
&start_level_inputs)) {
|
||||
break;
|
||||
}
|
||||
start_level_inputs.files.clear();
|
||||
}
|
||||
|
||||
if (start_level_inputs.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// For intra L0 compactions, pick up all overlapping files
|
||||
if (vstorage_->num_levels() == 1 && start_level_inputs.level == 0) {
|
||||
if (!picker_->GetOverlappingL0Files(vstorage_, &start_level_inputs,
|
||||
/*output_level=*/0, nullptr, nullptr)) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return BuildCompactionToNextLevel(start_level_inputs,
|
||||
CompactionReason::kReadTriggered);
|
||||
}
|
||||
|
||||
Compaction* UniversalCompactionBuilder::BuildCompactionToNextLevel(
|
||||
CompactionInputFiles& start_level_inputs, CompactionReason reason) {
|
||||
int start_level = start_level_inputs.level;
|
||||
int max_output_level = vstorage_->MaxOutputLevel(allow_ingest_behind_);
|
||||
|
||||
// Pick the first non-empty level after start_level as output.
|
||||
int output_level;
|
||||
for (output_level = start_level + 1; output_level <= max_output_level;
|
||||
output_level++) {
|
||||
if (vstorage_->NumLevelFiles(output_level) != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If all higher levels are empty, pick the highest level as output level
|
||||
// for L0 start. For non-L0 start, a trivial move doesn't help reclaim
|
||||
// space, so skip.
|
||||
if (output_level > max_output_level) {
|
||||
if (start_level == 0) {
|
||||
output_level = max_output_level;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (start_level_inputs.size() <= 1 && output_level == 0) {
|
||||
// If only the last file in L0 is marked for compaction, ignore it
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<CompactionInputFiles> inputs;
|
||||
std::vector<FileMetaData*> grandparents;
|
||||
|
||||
if (output_level != 0) {
|
||||
if (!MeetsOutputLevelRequirements(output_level)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// For standalone range deletion, we don't want to compact it with newer
|
||||
// L0 files that it doesn't cover.
|
||||
const FileMetaData* starting_l0_file =
|
||||
(start_level == 0 && start_level_inputs.size() == 1 &&
|
||||
start_level_inputs.files[0]->FileIsStandAloneRangeTombstone())
|
||||
? start_level_inputs.files[0]
|
||||
: nullptr;
|
||||
|
||||
if (start_level == 0) {
|
||||
if (!picker_->GetOverlappingL0Files(vstorage_, &start_level_inputs,
|
||||
output_level, nullptr,
|
||||
starting_l0_file)) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
CompactionInputFiles output_level_inputs;
|
||||
int parent_index = -1;
|
||||
|
||||
output_level_inputs.level = output_level;
|
||||
if (!picker_->SetupOtherInputs(
|
||||
cf_name_, mutable_cf_options_, vstorage_, &start_level_inputs,
|
||||
&output_level_inputs, &parent_index, -1, false, starting_l0_file)) {
|
||||
return nullptr;
|
||||
}
|
||||
inputs.push_back(start_level_inputs);
|
||||
if (!output_level_inputs.empty()) {
|
||||
inputs.push_back(output_level_inputs);
|
||||
}
|
||||
if (picker_->FilesRangeOverlapWithCompaction(
|
||||
inputs, output_level,
|
||||
Compaction::EvaluateProximalLevel(vstorage_, mutable_cf_options_,
|
||||
ioptions_, start_level,
|
||||
output_level))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
picker_->GetGrandparents(vstorage_, start_level_inputs, output_level_inputs,
|
||||
&grandparents);
|
||||
} else {
|
||||
inputs.push_back(start_level_inputs);
|
||||
}
|
||||
|
||||
uint64_t estimated_total_size = 0;
|
||||
for (FileMetaData* f : vstorage_->LevelFiles(output_level)) {
|
||||
estimated_total_size += f->fd.GetFileSize();
|
||||
}
|
||||
uint32_t path_id =
|
||||
GetPathId(ioptions_, mutable_cf_options_, estimated_total_size);
|
||||
return new Compaction(
|
||||
vstorage_, ioptions_, mutable_cf_options_, mutable_db_options_,
|
||||
std::move(inputs), output_level,
|
||||
MaxFileSizeForLevel(mutable_cf_options_, output_level,
|
||||
kCompactionStyleUniversal),
|
||||
/* max_grandparent_overlap_bytes */ GetMaxOverlappingBytes(), path_id,
|
||||
GetCompressionType(vstorage_, mutable_cf_options_, output_level, 1),
|
||||
GetCompressionOptions(mutable_cf_options_, vstorage_, output_level),
|
||||
Temperature::kUnknown,
|
||||
/* max_subcompactions */ 0, grandparents, earliest_snapshot_,
|
||||
snapshot_checker_, reason,
|
||||
/* trim_ts */ "", score_,
|
||||
/* l0_files_might_overlap */ true);
|
||||
}
|
||||
|
||||
uint64_t UniversalCompactionBuilder::GetMaxOverlappingBytes() const {
|
||||
if (!mutable_cf_options_.compaction_options_universal.incremental) {
|
||||
return std::numeric_limits<uint64_t>::max();
|
||||
|
||||
@@ -846,11 +846,16 @@ static std::unordered_map<std::string, OptionTypeInfo>
|
||||
{offsetof(struct InternalStats::CompactionStats, count),
|
||||
OptionType::kUInt64T, OptionVerificationType::kNormal,
|
||||
OptionTypeFlags::kNone}},
|
||||
{"counts", OptionTypeInfo::Array<
|
||||
int, static_cast<int>(CompactionReason::kNumOfReasons)>(
|
||||
offsetof(struct InternalStats::CompactionStats, counts),
|
||||
OptionVerificationType::kNormal, OptionTypeFlags::kNone,
|
||||
{0, OptionType::kInt})},
|
||||
{"counts",
|
||||
OptionTypeInfo::Array<
|
||||
/* In release 11.2, a new compaction reason was added. This broken
|
||||
* the reader and writer. To unblock release 11.1, we temporarily
|
||||
* reduce the count array size to the old one. TODO add a proper
|
||||
* serialization and deserialization method. */
|
||||
int, static_cast<int>(CompactionReason::kNumOfReasons) - 1>(
|
||||
offsetof(struct InternalStats::CompactionStats, counts),
|
||||
OptionVerificationType::kNormal, OptionTypeFlags::kNone,
|
||||
{0, OptionType::kInt})},
|
||||
};
|
||||
|
||||
static std::unordered_map<std::string, OptionTypeInfo>
|
||||
|
||||
@@ -81,6 +81,15 @@ class SubcompactionState {
|
||||
// it returns both the last level outputs and proximal level outputs.
|
||||
OutputIterator GetOutputs() const;
|
||||
|
||||
// Get mutable access to outputs for post-processing (e.g., retrieving
|
||||
// file open metadata).
|
||||
std::vector<CompactionOutputs::Output>& GetMutableCompactionOutputs() {
|
||||
return compaction_outputs_.GetMutableOutputs();
|
||||
}
|
||||
std::vector<CompactionOutputs::Output>& GetMutableProximalOutputs() {
|
||||
return proximal_level_outputs_.GetMutableOutputs();
|
||||
}
|
||||
|
||||
// Assign range dels aggregator. The various tombstones will potentially
|
||||
// be filtered to different outputs.
|
||||
void AssignRangeDelAggregator(
|
||||
|
||||
@@ -102,6 +102,757 @@ TEST_F(DBBasicTest, OpenWhenOpen) {
|
||||
ASSERT_TRUE(strstr(s.getState(), "lock ") != nullptr);
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Helper that captures per-branch SkippedNoopEdit counts for tests.
|
||||
struct RecoveryOptimizationCounters {
|
||||
std::atomic<int> setup_dbid{0};
|
||||
std::atomic<int> per_cf{0};
|
||||
std::atomic<int> wal_deletion{0};
|
||||
std::atomic<int> next_file_number{0};
|
||||
|
||||
void Install() {
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
sp->SetCallBack("DBImpl::Recovery:SkippedNoopEdit:SetupDBId",
|
||||
[this](void*) { setup_dbid.fetch_add(1); });
|
||||
sp->SetCallBack("DBImpl::Recovery:SkippedNoopEdit:PerCF",
|
||||
[this](void*) { per_cf.fetch_add(1); });
|
||||
sp->SetCallBack("DBImpl::Recovery:SkippedNoopEdit:WalDeletion",
|
||||
[this](void*) { wal_deletion.fetch_add(1); });
|
||||
sp->SetCallBack("DBImpl::Recovery:SkippedNoopEdit:NextFileNumber",
|
||||
[this](void*) { next_file_number.fetch_add(1); });
|
||||
sp->EnableProcessing();
|
||||
}
|
||||
void Uninstall() {
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
sp->DisableProcessing();
|
||||
sp->ClearAllCallBacks();
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// optimize_manifest_for_recovery=true: a clean reopen of a flushed DB must
|
||||
// append fewer individual records to the MANIFEST than the default-off path.
|
||||
// Verified by counting AddRecord calls (one per VersionEdit written to the
|
||||
// MANIFEST log).
|
||||
TEST_F(DBBasicTest,
|
||||
OptimizeManifestForRecoveryReducesManifestWritesOnCleanReopen) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
|
||||
// Measure MANIFEST records with optimize_manifest_for_recovery OFF (default).
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k1", "v1"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
std::atomic<int> records_off{0};
|
||||
std::atomic<int> next_file_skips_off{0};
|
||||
sp->SetCallBack("VersionSet::ProcessManifestWrites:AddRecord",
|
||||
[&](void*) { records_off.fetch_add(1); });
|
||||
sp->SetCallBack("DBImpl::Recovery:SkippedNoopEdit:NextFileNumber",
|
||||
[&](void*) { next_file_skips_off.fetch_add(1); });
|
||||
sp->EnableProcessing();
|
||||
Reopen(options);
|
||||
sp->DisableProcessing();
|
||||
sp->ClearAllCallBacks();
|
||||
ASSERT_EQ("v1", Get("k1"));
|
||||
int off_count = records_off.load();
|
||||
ASSERT_GT(off_count, 0);
|
||||
ASSERT_EQ(0, next_file_skips_off.load());
|
||||
Close();
|
||||
|
||||
// Measure MANIFEST records with optimize_manifest_for_recovery ON.
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k2", "v2"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
std::atomic<int> records_on{0};
|
||||
std::atomic<int> next_file_skips_on{0};
|
||||
sp->SetCallBack("VersionSet::ProcessManifestWrites:AddRecord",
|
||||
[&](void*) { records_on.fetch_add(1); });
|
||||
sp->SetCallBack("DBImpl::Recovery:SkippedNoopEdit:NextFileNumber",
|
||||
[&](void*) { next_file_skips_on.fetch_add(1); });
|
||||
sp->EnableProcessing();
|
||||
Reopen(options);
|
||||
sp->DisableProcessing();
|
||||
sp->ClearAllCallBacks();
|
||||
ASSERT_EQ("v2", Get("k2"));
|
||||
int on_count = records_on.load();
|
||||
ASSERT_GT(next_file_skips_on.load(), 0);
|
||||
ASSERT_LT(on_count, off_count);
|
||||
}
|
||||
|
||||
// When the per-CF log_number actually advances, the per-CF skip must NOT
|
||||
// fire — the edit carries real information and must be emitted.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryEmitsPerCFWhenLogAdvances) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
// Write data and close WITHOUT flushing — the WAL has un-replayed
|
||||
// records, so on reopen recovery flushes the memtable and the per-CF
|
||||
// edit's log_number must advance past the prior WAL.
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_EQ(0, counters.per_cf.load());
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// With track_and_verify_wals_in_manifest=true, the wal_deletion edit
|
||||
// must STILL be emitted (the DeleteWalsBefore record is required).
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryPreservesWalTracking) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
options.track_and_verify_wals_in_manifest = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_EQ(0, counters.wal_deletion.load());
|
||||
}
|
||||
|
||||
// best_efforts_recovery requires a fresh MANIFEST + CURRENT to be
|
||||
// produced on every open (the salvage contract). Even with the option
|
||||
// on, none of the SkippedNoopEdit branches must fire under
|
||||
// best_efforts_recovery — otherwise CURRENT can be left missing or
|
||||
// stale (regression caught by DBBasicTest.RecoverWithNoCurrentFile and
|
||||
// DBTest2.BestEffortsRecoveryWithSstUniqueIdVerification under an
|
||||
// option-on default).
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryDisabledByBestEffortsRecovery) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
options.best_efforts_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_EQ(0, counters.setup_dbid.load());
|
||||
ASSERT_EQ(0, counters.per_cf.load());
|
||||
ASSERT_EQ(0, counters.wal_deletion.load());
|
||||
ASSERT_EQ(0, counters.next_file_number.load());
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// Multi-column-family coverage: with one CF flushed and another not,
|
||||
// the un-flushed CF's edit must still be emitted.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryMultiCF) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
CreateAndReopenWithCF({"pikachu", "raichu"}, options);
|
||||
ASSERT_OK(Put(0, "k", "v0"));
|
||||
ASSERT_OK(Put(1, "k", "v1"));
|
||||
ASSERT_OK(Put(2, "k", "v2"));
|
||||
ASSERT_OK(Flush(0));
|
||||
// CF 1 and 2 keep dirty memtables — recovery must emit their per-CF
|
||||
// edits (log_number advance from WAL replay).
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
ReopenWithColumnFamilies({"default", "pikachu", "raichu"}, options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_EQ("v0", Get(0, "k"));
|
||||
ASSERT_EQ("v1", Get(1, "k"));
|
||||
ASSERT_EQ("v2", Get(2, "k"));
|
||||
}
|
||||
|
||||
// MaybeUpdateNextFileNumber's seed value (next_file_number_) makes the
|
||||
// post-loop comparison trivially true on every clean recovery, causing a
|
||||
// no-op SetNextFile edit to be appended to the MANIFEST. With
|
||||
// optimize_manifest_for_recovery=true, that emission is gated and
|
||||
// the SkippedNoopEdit:NextFileNumber sync point fires in its place.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoverySkipsNextFileNumber) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_EQ(1, counters.next_file_number.load());
|
||||
}
|
||||
|
||||
// With option=true and a synthetic on-disk file whose number is at or
|
||||
// above next_file_number_, MaybeUpdateNextFileNumber MUST emit the
|
||||
// SetNextFile edit and advance the counter past the synthetic number.
|
||||
TEST_F(DBBasicTest,
|
||||
OptimizeManifestForRecoveryEmitsNextFileNumberWhenJustified) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
const uint64_t next_before_close =
|
||||
dbfull()->GetVersionSet()->current_next_file_number();
|
||||
Close();
|
||||
|
||||
// Drop an empty .sst with a number well above next_file_number into
|
||||
// the DB dir. MaybeUpdateNextFileNumber must observe it and advance.
|
||||
const uint64_t synthetic_number = next_before_close + 100;
|
||||
const std::string synthetic_sst =
|
||||
dbname_ + "/" + MakeTableFileName("", synthetic_number);
|
||||
ASSERT_OK(WriteStringToFile(env_, "" /*data*/, synthetic_sst,
|
||||
/*should_sync=*/true));
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_EQ(0, counters.next_file_number.load());
|
||||
ASSERT_GT(dbfull()->GetVersionSet()->current_next_file_number(),
|
||||
synthetic_number);
|
||||
}
|
||||
|
||||
// optimize_manifest_for_recovery=true: after a clean Put + Flush + Close, the
|
||||
// next Open's min_log_number_to_keep equals (max_wal_before_close + 1),
|
||||
// proving the close-time MANIFEST write took effect.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryAdvancesWalMarkersOnClose) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
uint64_t max_wal_before_close = 0;
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
sp->SetCallBack("DBImpl::CloseHelper:CapturedMaxWal", [&](void* arg) {
|
||||
max_wal_before_close = *static_cast<uint64_t*>(arg);
|
||||
});
|
||||
sp->EnableProcessing();
|
||||
Close();
|
||||
sp->DisableProcessing();
|
||||
sp->ClearAllCallBacks();
|
||||
ASSERT_GT(max_wal_before_close, 0u);
|
||||
|
||||
Reopen(options);
|
||||
ASSERT_EQ(max_wal_before_close + 1,
|
||||
dbfull()->GetVersionSet()->min_log_number_to_keep());
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// Shared-option matrix: default-off and disable-before-close should both fall
|
||||
// back to recovery-time MANIFEST work, while enable-through-close should avoid
|
||||
// MANIFEST appends on a clean reopen.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryCleanReopenMatrix) {
|
||||
struct TestCase {
|
||||
const char* name;
|
||||
bool enable_on_open;
|
||||
bool disable_before_close;
|
||||
bool expect_close_write;
|
||||
};
|
||||
const TestCase test_cases[] = {
|
||||
{"default_off", false, false, false},
|
||||
{"enabled", true, false, true},
|
||||
{"disabled_before_close", true, true, false},
|
||||
};
|
||||
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
for (const auto& test_case : test_cases) {
|
||||
SCOPED_TRACE(test_case.name);
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = test_case.enable_on_open;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", test_case.name));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
if (test_case.disable_before_close) {
|
||||
ASSERT_OK(dbfull()->SetDBOptions(
|
||||
{{"optimize_manifest_for_recovery", "false"}}));
|
||||
}
|
||||
|
||||
std::atomic<int> entered{0};
|
||||
sp->SetCallBack("DBImpl::CloseHelper:WriteWalMarkersOnCloseEntered",
|
||||
[&](void*) { entered.fetch_add(1); });
|
||||
sp->EnableProcessing();
|
||||
Close();
|
||||
sp->DisableProcessing();
|
||||
sp->ClearAllCallBacks();
|
||||
|
||||
std::atomic<int> records{0};
|
||||
RecoveryOptimizationCounters counters;
|
||||
counters.Install();
|
||||
sp->SetCallBack("VersionSet::ProcessManifestWrites:AddRecord",
|
||||
[&](void*) { records.fetch_add(1); });
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
if (test_case.expect_close_write) {
|
||||
ASSERT_EQ(1, entered.load());
|
||||
ASSERT_EQ(0, records.load());
|
||||
ASSERT_GT(counters.per_cf.load(), 0);
|
||||
} else {
|
||||
ASSERT_EQ(0, entered.load());
|
||||
ASSERT_GT(records.load(), 0);
|
||||
}
|
||||
ASSERT_EQ(test_case.name, Get("k"));
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
// allow_2pc=true: even with the option on, the close-time write must NOT
|
||||
// advance MinLogNumberToKeep / DeleteWalsBefore (2pc requires the WAL to
|
||||
// remain replayable for uncommitted prepared transactions).
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryRespectsTwoPC) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
options.allow_2pc = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
const uint64_t min_log_before_close =
|
||||
dbfull()->GetVersionSet()->min_log_number_to_keep();
|
||||
Close();
|
||||
Reopen(options);
|
||||
ASSERT_EQ(min_log_before_close,
|
||||
dbfull()->GetVersionSet()->min_log_number_to_keep());
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// Mixed-CF emptiness: on reopen, recovery should still have MANIFEST work to
|
||||
// do for the dirty CF while skipping per-CF recovery edits for the empty CFs
|
||||
// whose markers were persisted at close.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoverySkipsNonEmptyCF) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
CreateAndReopenWithCF({"empty_cf", "dirty_cf"}, options);
|
||||
ASSERT_OK(Put(1, "k", "v1"));
|
||||
ASSERT_OK(Flush(1));
|
||||
ASSERT_OK(Put(2, "k", "v2"));
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
std::atomic<int> records{0};
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
counters.Install();
|
||||
sp->SetCallBack("VersionSet::ProcessManifestWrites:AddRecord",
|
||||
[&](void*) { records.fetch_add(1); });
|
||||
ReopenWithColumnFamilies({"default", "empty_cf", "dirty_cf"}, options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_GT(counters.per_cf.load(), 0);
|
||||
ASSERT_GT(records.load(), 0);
|
||||
ASSERT_EQ("v1", Get(1, "k"));
|
||||
ASSERT_EQ("v2", Get(2, "k"));
|
||||
}
|
||||
|
||||
// track_and_verify_wals_in_manifest=true: the close-time write must leave
|
||||
// recovery with only the mandatory WAL-tracking MANIFEST work. The per-CF
|
||||
// recovery edits should still skip.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryEmitsWalDeletionWhenTracking) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
options.track_and_verify_wals_in_manifest = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
RecoveryOptimizationCounters counters;
|
||||
std::atomic<int> records{0};
|
||||
auto* sp = ROCKSDB_NAMESPACE::SyncPoint::GetInstance();
|
||||
counters.Install();
|
||||
sp->SetCallBack("VersionSet::ProcessManifestWrites:AddRecord",
|
||||
[&](void*) { records.fetch_add(1); });
|
||||
Reopen(options);
|
||||
counters.Uninstall();
|
||||
|
||||
ASSERT_GT(counters.per_cf.load(), 0);
|
||||
ASSERT_EQ(1, records.load());
|
||||
ASSERT_GT(dbfull()->GetVersionSet()->GetWalSet().GetMinWalNumberToKeep(), 0u);
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// Regression for the close-time marker path: if a new WAL is created while an
|
||||
// otherwise-empty user CF stays empty, Close() must reserve the next file
|
||||
// number before persisting SetLogNumber(cur_wal + 1) for that CF.
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoveryImmediateCloseAfterWarmReopen) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
CreateAndReopenWithCF({"empty_cf"}, options);
|
||||
|
||||
ASSERT_OK(Put(0, "k", "v"));
|
||||
const uint64_t wal_before_switch = dbfull()->TEST_LogfileNumber();
|
||||
ASSERT_OK(dbfull()->TEST_SwitchMemtable());
|
||||
ASSERT_GT(dbfull()->TEST_LogfileNumber(), wal_before_switch);
|
||||
|
||||
const uint64_t expected_new_log_num = dbfull()->TEST_LogfileNumber() + 1;
|
||||
ASSERT_EQ(expected_new_log_num,
|
||||
dbfull()->GetVersionSet()->current_next_file_number());
|
||||
|
||||
Close();
|
||||
ReopenWithColumnFamilies({"default", "empty_cf"}, options);
|
||||
|
||||
ASSERT_GT(dbfull()->GetVersionSet()->current_next_file_number(),
|
||||
expected_new_log_num);
|
||||
ASSERT_EQ("v", Get(0, "k"));
|
||||
}
|
||||
|
||||
// Dropped-CF safety: dropped column families must NOT have markers written for
|
||||
// them at close time (the !IsDropped() guard protects against attaching the
|
||||
// global edit to a dropped CF, which would later trip MANIFEST replay
|
||||
// assertions).
|
||||
TEST_F(DBBasicTest, OptimizeManifestForRecoverySkipsDroppedCF) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
CreateAndReopenWithCF({"to_drop", "keeper"}, options);
|
||||
ASSERT_OK(Put(1, "k", "v1"));
|
||||
ASSERT_OK(Put(2, "k", "v2"));
|
||||
ASSERT_OK(Flush(1));
|
||||
ASSERT_OK(Flush(2));
|
||||
ASSERT_OK(db_->DropColumnFamily(handles_[1]));
|
||||
Close();
|
||||
|
||||
ReopenWithColumnFamilies({"default", "keeper"}, options);
|
||||
ASSERT_EQ("v2", Get(1, "k"));
|
||||
}
|
||||
|
||||
// reuse_manifest_on_open=true: the next LogAndApply after Recover must
|
||||
// append to the existing MANIFEST file instead of allocating a fresh
|
||||
// one. Force a write after reopen and verify the MANIFEST file number
|
||||
// stays unchanged.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenAppendsToExistingFile) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
Reopen(options);
|
||||
const uint64_t manifest_after_reopen =
|
||||
dbfull()->TEST_Current_Manifest_FileNo();
|
||||
ASSERT_OK(Put("k2", "v2"));
|
||||
ASSERT_OK(Flush());
|
||||
const uint64_t manifest_after_flush =
|
||||
dbfull()->TEST_Current_Manifest_FileNo();
|
||||
|
||||
EXPECT_EQ(manifest_after_reopen, manifest_after_flush);
|
||||
EXPECT_EQ("v", Get("k"));
|
||||
EXPECT_EQ("v2", Get("k2"));
|
||||
}
|
||||
|
||||
// Default off: ReopenManifestForAppend must NOT be invoked.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenDefaultOffSkipsReopen) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
// reuse_manifest_on_open defaults to false
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
std::atomic<int> reopened{0};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"VersionSet::ReopenManifestForAppend:Reopened",
|
||||
[&](void* /*arg*/) { reopened.fetch_add(1); });
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
Reopen(options);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
ASSERT_EQ(0, reopened.load());
|
||||
}
|
||||
|
||||
// Regression for the WritableFileWriter::filesize_=0 bug fixed via
|
||||
// constructor-time initial_file_size: after ReopenManifestForAppend binds a
|
||||
// writer to the existing MANIFEST, GetFileSize() must return the on-disk
|
||||
// size, not 0.
|
||||
// (If it returned 0, the size-limit check in ProcessManifestWrites would
|
||||
// compare against 0 and Close-time Truncate could shrink the file.)
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenAdoptsOnDiskSize) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
// Capture the on-disk MANIFEST size before reopen.
|
||||
Reopen(options);
|
||||
const uint64_t manifest_no = dbfull()->TEST_Current_Manifest_FileNo();
|
||||
const std::string manifest_path = DescriptorFileName(dbname_, manifest_no);
|
||||
uint64_t on_disk_size = 0;
|
||||
ASSERT_OK(env_->GetFileSize(manifest_path, &on_disk_size));
|
||||
ASSERT_GT(on_disk_size, 0u);
|
||||
|
||||
// Force a write to flush the writer's buffer; verify Close doesn't
|
||||
// shrink the file (which would happen if Truncate(filesize_=0) ran).
|
||||
ASSERT_OK(Put("k2", "v2"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
uint64_t after_close_size = 0;
|
||||
ASSERT_OK(env_->GetFileSize(manifest_path, &after_close_size));
|
||||
EXPECT_GE(after_close_size, on_disk_size);
|
||||
}
|
||||
|
||||
// MANIFEST rotation continues to work under reuse: when the file grows
|
||||
// past tuned_max_manifest_file_size_, ProcessManifestWrites must rotate
|
||||
// to a fresh MANIFEST (same as legacy). Use the rotation SyncPoint to
|
||||
// observe rotation directly, since tuned_max_manifest_file_size_ is
|
||||
// auto-derived and not directly = max_manifest_file_size.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenStillRotatesOnSizeCap) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
options.max_manifest_file_size = 1;
|
||||
options.max_manifest_space_amp_pct = 0; // disable amp-based tuning
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
std::atomic<int> rotations{0};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"VersionSet::ProcessManifestWrites:BeforeNewManifest",
|
||||
[&](void* /*arg*/) { rotations.fetch_add(1); });
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
Reopen(options);
|
||||
ASSERT_OK(Put("k2", "v2"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
// The Flush after Reopen must have triggered a rotation given the
|
||||
// tiny size cap — proves the size-driven rotation path still runs
|
||||
// when descriptor_log_ is bound by reuse.
|
||||
EXPECT_GT(rotations.load(), 0);
|
||||
EXPECT_EQ("v", Get("k"));
|
||||
EXPECT_EQ("v2", Get("k2"));
|
||||
}
|
||||
|
||||
// Multi-CF reuse: append-mode MANIFEST must correctly handle edits
|
||||
// from multiple CFs after reopen.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenMultiCF) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
CreateAndReopenWithCF({"alpha", "beta"}, options);
|
||||
ASSERT_OK(Put(0, "k", "v0"));
|
||||
ASSERT_OK(Put(1, "k", "v1"));
|
||||
ASSERT_OK(Put(2, "k", "v2"));
|
||||
ASSERT_OK(Flush(0));
|
||||
ASSERT_OK(Flush(1));
|
||||
ASSERT_OK(Flush(2));
|
||||
Close();
|
||||
|
||||
ReopenWithColumnFamilies({"default", "alpha", "beta"}, options);
|
||||
// Trigger more writes post-reopen on each CF — these get appended to
|
||||
// the reused MANIFEST.
|
||||
ASSERT_OK(Put(0, "k2", "v0b"));
|
||||
ASSERT_OK(Put(1, "k2", "v1b"));
|
||||
ASSERT_OK(Put(2, "k2", "v2b"));
|
||||
ASSERT_OK(Flush(0));
|
||||
ASSERT_OK(Flush(1));
|
||||
ASSERT_OK(Flush(2));
|
||||
|
||||
EXPECT_EQ("v0", Get(0, "k"));
|
||||
EXPECT_EQ("v1", Get(1, "k"));
|
||||
EXPECT_EQ("v2", Get(2, "k"));
|
||||
EXPECT_EQ("v0b", Get(0, "k2"));
|
||||
EXPECT_EQ("v1b", Get(1, "k2"));
|
||||
EXPECT_EQ("v2b", Get(2, "k2"));
|
||||
}
|
||||
|
||||
// Disabled under best_efforts_recovery: that mode rebuilds CURRENT and
|
||||
// MANIFEST as the side-effect of LogAndApplyForRecovery emitting an
|
||||
// edit; reusing the prior MANIFEST contradicts the salvage contract.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenDisabledByBestEffortsRecovery) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
options.best_efforts_recovery = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
std::atomic<int> reopened{0};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"VersionSet::ReopenManifestForAppend:Reopened",
|
||||
[&](void* /*arg*/) { reopened.fetch_add(1); });
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
Reopen(options);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
ASSERT_EQ(0, reopened.load());
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// Full-stack composition: optimize_manifest_for_recovery plus
|
||||
// reuse_manifest_on_open. Verifies that Close writes recovery markers,
|
||||
// Reopen skips clean-recovery MANIFEST edits, and the MANIFEST is reused
|
||||
// instead of recreated for the next metadata update.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenFullStackComposition) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.optimize_manifest_for_recovery = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
// Capture the MANIFEST file number before reopen.
|
||||
std::atomic<int> reopened{0};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"VersionSet::ReopenManifestForAppend:Reopened",
|
||||
[&](void* /*arg*/) { reopened.fetch_add(1); });
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
Reopen(options);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
// All three composing: reuse fired, data is intact, no fresh MANIFEST.
|
||||
EXPECT_EQ(1, reopened.load());
|
||||
EXPECT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
// Direct unit test for WritableFileWriter's initial_file_size parameter:
|
||||
// verifies the visible size accessors report the existing on-disk size
|
||||
// immediately, rather than the constructor's zero default.
|
||||
TEST_F(DBBasicTest, WritableFileWriterInitialFileSizeAdoptsExistingSize) {
|
||||
Env* env = Env::Default();
|
||||
std::string fname = test::PerThreadDBPath("set_file_size_test");
|
||||
ASSERT_OK(env->CreateDirIfMissing(test::TmpDir(env)));
|
||||
|
||||
// Create a file with some bytes, close it.
|
||||
{
|
||||
std::unique_ptr<WritableFile> raw;
|
||||
ASSERT_OK(env->NewWritableFile(fname, &raw, EnvOptions()));
|
||||
ASSERT_OK(raw->Append("hello world"));
|
||||
ASSERT_OK(raw->Close());
|
||||
}
|
||||
|
||||
// Reopen and wrap in a WritableFileWriter without initial_file_size —
|
||||
// GetFileSize() should be 0 (the constructor's default).
|
||||
std::unique_ptr<FSWritableFile> fs_file;
|
||||
ASSERT_OK(env->GetFileSystem()->ReopenWritableFile(
|
||||
fname, FileOptions(), &fs_file, /*dbg=*/nullptr));
|
||||
std::unique_ptr<WritableFileWriter> writer(
|
||||
new WritableFileWriter(std::move(fs_file), fname, FileOptions()));
|
||||
EXPECT_EQ(0u, writer->GetFileSize());
|
||||
|
||||
// Reopen again and seed the writer's size accounting from the existing
|
||||
// bytes. GetFileSize and GetFlushedSize must reflect it immediately.
|
||||
ASSERT_OK(env->GetFileSystem()->ReopenWritableFile(
|
||||
fname, FileOptions(), &fs_file, /*dbg=*/nullptr));
|
||||
writer.reset(new WritableFileWriter(
|
||||
std::move(fs_file), fname, FileOptions(),
|
||||
/*clock=*/nullptr, /*io_tracer=*/nullptr, /*stats=*/nullptr,
|
||||
Histograms::HISTOGRAM_ENUM_MAX, /*listeners=*/{},
|
||||
/*file_checksum_gen_factory=*/nullptr,
|
||||
/*perform_data_verification=*/false,
|
||||
/*buffered_data_with_checksum=*/false,
|
||||
/*initial_file_size=*/11));
|
||||
EXPECT_EQ(11u, writer->GetFileSize());
|
||||
EXPECT_EQ(11u, writer->GetFlushedSize());
|
||||
|
||||
ASSERT_OK(env->DeleteFile(fname));
|
||||
}
|
||||
|
||||
// Tail corruption: appending garbage bytes to the MANIFEST after a
|
||||
// clean close must prevent reuse — the physical size exceeds the
|
||||
// last valid record end.
|
||||
TEST_F(DBBasicTest, ReuseManifestOnOpenSkipsOnTailCorruption) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.reuse_manifest_on_open = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("k", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
Close();
|
||||
|
||||
// Find the MANIFEST file and append garbage to it.
|
||||
std::string manifest_path;
|
||||
{
|
||||
std::vector<std::string> files;
|
||||
ASSERT_OK(env_->GetChildren(dbname_, &files));
|
||||
for (const auto& f : files) {
|
||||
uint64_t number;
|
||||
FileType type;
|
||||
if (ParseFileName(f, &number, &type) && type == kDescriptorFile) {
|
||||
manifest_path = dbname_ + "/" + f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ASSERT_FALSE(manifest_path.empty());
|
||||
{
|
||||
std::string contents;
|
||||
ASSERT_OK(ReadFileToString(env_, manifest_path, &contents));
|
||||
contents.append("garbage!");
|
||||
ASSERT_OK(WriteStringToFile(env_, contents, manifest_path));
|
||||
}
|
||||
|
||||
std::atomic<int> reopened{0};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"VersionSet::ReopenManifestForAppend:Reopened",
|
||||
[&](void* /*arg*/) { reopened.fetch_add(1); });
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
Reopen(options);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
ASSERT_EQ(0, reopened.load());
|
||||
ASSERT_EQ("v", Get("k"));
|
||||
}
|
||||
|
||||
TEST_F(DBBasicTest, EnableDirectIOWithZeroBuf) {
|
||||
if (!IsDirectIOSupported()) {
|
||||
ROCKSDB_GTEST_BYPASS("Direct IO not supported");
|
||||
|
||||
@@ -119,6 +119,17 @@ class DBCompactionTest : public DBTestBase {
|
||||
DBCompactionTest()
|
||||
: DBTestBase("db_compaction_test", /*env_do_fsync=*/false) {}
|
||||
|
||||
void TearDown() override {
|
||||
// Reset Env::Default() thread pool state that tests may have modified.
|
||||
// Under sharded execution multiple tests share a process, so leaked
|
||||
// thread-pool sizes (especially BOTTOM) cause later tests to see
|
||||
// unexpected compaction scheduling (e.g. ForwardToBottomPriPool).
|
||||
Env::Default()->SetBackgroundThreads(0, Env::Priority::BOTTOM);
|
||||
Env::Default()->SetBackgroundThreads(1, Env::Priority::LOW);
|
||||
Env::Default()->SetBackgroundThreads(1, Env::Priority::HIGH);
|
||||
DBTestBase::TearDown();
|
||||
}
|
||||
|
||||
protected:
|
||||
/*
|
||||
* Verifies compaction stats of cfd are valid.
|
||||
@@ -11682,6 +11693,76 @@ TEST_F(DBCompactionTest, PeriodicTask) {
|
||||
Close();
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionTest, ReadTriggeredCompaction) {
|
||||
Options options = CurrentOptions();
|
||||
options.num_levels = 3;
|
||||
options.level0_file_num_compaction_trigger = 10;
|
||||
options.read_triggered_compaction_threshold = 0.001;
|
||||
options.disable_auto_compactions = true;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Write data at L2 first so L1 is not the bottommost level
|
||||
Random rnd(301);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
ASSERT_OK(Put(Key(i), rnd.RandomString(1024)));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(2);
|
||||
ASSERT_EQ("0,0,1", FilesPerLevel());
|
||||
|
||||
// Write more data and move to L1 (the hot level)
|
||||
for (int i = 5; i < 10; ++i) {
|
||||
ASSERT_OK(Put(Key(i), rnd.RandomString(1024)));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(1);
|
||||
ASSERT_EQ("0,1,1", FilesPerLevel());
|
||||
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
dbfull()->GetColumnFamilyMetaData(dbfull()->DefaultColumnFamily(), &cf_meta);
|
||||
ASSERT_EQ(cf_meta.levels[1].files.size(), 1);
|
||||
uint64_t file_size = cf_meta.levels[1].files[0].size;
|
||||
// Set reads high enough to exceed threshold (0.001 * file_size)
|
||||
uint64_t reads_needed =
|
||||
static_cast<uint64_t>(0.002 * static_cast<double>(file_size));
|
||||
|
||||
{
|
||||
auto* cfd = static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
dbfull()->DefaultColumnFamily())
|
||||
->cfd();
|
||||
auto* vstorage = cfd->current()->storage_info();
|
||||
for (auto* f : vstorage->LevelFiles(1)) {
|
||||
f->stats.num_collapsible_entry_reads_sampled.store(reads_needed);
|
||||
}
|
||||
}
|
||||
|
||||
std::atomic<int> read_triggered_compactions{0};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"LevelCompactionPicker::PickCompaction:Return", [&](void* arg) {
|
||||
Compaction* compaction = static_cast<Compaction*>(arg);
|
||||
if (compaction->compaction_reason() ==
|
||||
CompactionReason::kReadTriggered) {
|
||||
read_triggered_compactions.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
});
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
// Enable auto compactions to trigger
|
||||
ASSERT_OK(dbfull()->SetOptions({{"disable_auto_compactions", "false"}}));
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
|
||||
ASSERT_GE(read_triggered_compactions, 1);
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
|
||||
// Verify the L1 file was compacted down (L1 should be empty now)
|
||||
ASSERT_EQ(0, NumTableFilesAtLevel(1));
|
||||
|
||||
// Verify data integrity: all keys are still readable
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
ASSERT_NE(Get(Key(i)), "NOT_FOUND");
|
||||
}
|
||||
}
|
||||
|
||||
// Regression test for a bug in SetupOtherFilesWithRoundRobinExpansion where
|
||||
// duplicate files are added to the compaction input, corrupting
|
||||
// ExpandInputsToCleanCut and violating the clean-cut invariant. The bug
|
||||
@@ -11830,6 +11911,88 @@ TEST_F(DBCompactionTest, LeakedTableCacheEntryOnCompactionFailure) {
|
||||
db_ = nullptr;
|
||||
}
|
||||
|
||||
// Regression test for table cache leak when InstallCompactionResults fails.
|
||||
// Similar to LeakedTableCacheEntryOnCompactionFailure but the failure occurs
|
||||
// during Install() (MANIFEST write) rather than during Run().
|
||||
// 1. Compaction Run() succeeds: all subcompactions OK, VerifyOutputFiles
|
||||
// inserts output files into the table cache, compact_->status = OK.
|
||||
// 2. Install() calls InstallCompactionResults() -> LogAndApply(), which
|
||||
// fails (injected MANIFEST write error).
|
||||
// 3. Install()'s local status captures the error, but compact_->status
|
||||
// was never updated (THE BUG). CleanupCompaction passes compact_->status
|
||||
// (OK) to Cleanup(), which skips ReleaseObsolete.
|
||||
// 4. FaultInjection prevents FindObsoleteFiles backstop from working.
|
||||
// 5. Close()'s TEST_VerifyNoObsoleteFilesCached finds the leaked entry.
|
||||
TEST_F(DBCompactionTest, LeakedTableCacheEntryOnInstallFailure) {
|
||||
auto fault_fs = std::make_shared<FaultInjectionTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> fault_env(NewCompositeEnv(fault_fs));
|
||||
|
||||
Options options = CurrentOptions();
|
||||
options.env = fault_env.get();
|
||||
options.paranoid_file_checks = true;
|
||||
options.level0_file_num_compaction_trigger = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
options.num_levels = 3;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Write overlapping data to force a real (non-trivial) compaction.
|
||||
ASSERT_OK(Put("a", std::string(1024, 'x')));
|
||||
ASSERT_OK(Put("z", std::string(1024, 'x')));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(Put("a", std::string(1024, 'y')));
|
||||
ASSERT_OK(Put("z", std::string(1024, 'y')));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_EQ(NumTableFilesAtLevel(0), 2);
|
||||
|
||||
// After VerifyOutputFiles succeeds (cache entries created) and Run()
|
||||
// completes successfully, inject an error into InstallCompactionResults()
|
||||
// so that Install() fails while compact_->status remains OK.
|
||||
// Also enable metadata read faults to prevent the FindObsoleteFiles
|
||||
// backstop from evicting the leaked cache entries.
|
||||
std::atomic<bool> inject_error{true};
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"CompactionJob::InstallCompactionResults:BeforeLogAndApply",
|
||||
[&](void* arg) {
|
||||
if (inject_error.exchange(false)) {
|
||||
*static_cast<Status*>(arg) = Status::IOError("injected");
|
||||
}
|
||||
});
|
||||
|
||||
// After the compaction job finishes (including Install), enable metadata
|
||||
// read faults so FindObsoleteFiles backstop cannot scan the directory.
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"BackgroundCallCompaction:1", [&](void*) {
|
||||
fault_fs->SetThreadLocalErrorContext(
|
||||
FaultInjectionIOType::kMetadataRead, /*seed=*/0, /*one_in=*/1,
|
||||
/*retryable=*/false, /*has_data_loss=*/false);
|
||||
fault_fs->EnableThreadLocalErrorInjection(
|
||||
FaultInjectionIOType::kMetadataRead);
|
||||
});
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
// Trigger compaction — Run() succeeds but Install() fails.
|
||||
Status s = dbfull()->TEST_CompactRange(0, nullptr, nullptr);
|
||||
ASSERT_NOK(s);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
// Enable metadata read fault injection on the main thread too, so
|
||||
// Close()'s FindObsoleteFiles also fails to find the orphan file.
|
||||
fault_fs->SetThreadLocalErrorContext(
|
||||
FaultInjectionIOType::kMetadataRead, /*seed=*/0, /*one_in=*/1,
|
||||
/*retryable=*/false, /*has_data_loss=*/false);
|
||||
fault_fs->EnableThreadLocalErrorInjection(
|
||||
FaultInjectionIOType::kMetadataRead);
|
||||
|
||||
// TEST_VerifyNoObsoleteFilesCached asserted within Close on ASAN builds
|
||||
s = db_->Close();
|
||||
ASSERT_OK(s);
|
||||
// Release DB before fault_env goes out of scope to avoid use-after-free.
|
||||
db_ = nullptr;
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionTest, VerifyFileChecksumOnCompactionOutput) {
|
||||
Options options = CurrentOptions();
|
||||
options.disable_auto_compactions = true;
|
||||
@@ -11958,6 +12121,56 @@ TEST_F(DBCompactionTest, VerifyAllOutputFlagsWithoutParanoidFileChecks) {
|
||||
}
|
||||
}
|
||||
|
||||
// Requires ~8GB+ RAM because the compaction filter internally creates a ~4GB
|
||||
// value via std::string::assign().
|
||||
TEST_F(DBCompactionTest, CompactionFilterLargeValueRejected) {
|
||||
if (!test::HasBigMem()) {
|
||||
ROCKSDB_GTEST_BYPASS("insufficient memory for reliable continuous testing");
|
||||
return;
|
||||
}
|
||||
|
||||
// A compaction filter that inflates every value to a configurable size
|
||||
class InflatingFilter : public CompactionFilter {
|
||||
public:
|
||||
std::atomic<size_t> target_size{0};
|
||||
Decision FilterV2(int /*level*/, const Slice& /*key*/,
|
||||
ValueType /*value_type*/, const Slice& /*existing_value*/,
|
||||
std::string* new_value,
|
||||
std::string* /*skip_until*/) const override {
|
||||
new_value->assign(target_size.load(), 'X');
|
||||
return Decision::kChangeValue;
|
||||
}
|
||||
const char* Name() const override { return "InflatingFilter"; }
|
||||
};
|
||||
|
||||
InflatingFilter inflating_filter;
|
||||
Options options = CurrentOptions();
|
||||
options.compaction_filter = &inflating_filter;
|
||||
options.disable_auto_compactions = true;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Control: value at exactly 4GB - 1 should be accepted
|
||||
inflating_filter.target_size = size_t{std::numeric_limits<uint32_t>::max()};
|
||||
ASSERT_OK(Put("key", "small_value"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(Put("key", "small_value2"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(db_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
|
||||
// Now test rejection: value at 4GB should be rejected
|
||||
inflating_filter.target_size =
|
||||
size_t{std::numeric_limits<uint32_t>::max()} + 1;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("key", "small_value"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(Put("key", "small_value2"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
Status s = db_->CompactRange(CompactRangeOptions(), nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsCorruption()) << s.ToString();
|
||||
ASSERT_TRUE(s.ToString().find("4GB") != std::string::npos) << s.ToString();
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
+35
-22
@@ -28,9 +28,10 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
Status DBImpl::FlushForGetLiveFiles() {
|
||||
return DBImpl::FlushAllColumnFamilies(FlushOptions(),
|
||||
FlushReason::kGetLiveFiles);
|
||||
Status DBImpl::FlushForGetLiveFiles(bool force_atomic_flush) {
|
||||
FlushOptions flush_opts;
|
||||
flush_opts.force_atomic_flush = force_atomic_flush;
|
||||
return DBImpl::FlushAllColumnFamilies(flush_opts, FlushReason::kGetLiveFiles);
|
||||
}
|
||||
|
||||
Status DBImpl::GetLiveFiles(std::vector<std::string>& ret,
|
||||
@@ -204,7 +205,6 @@ Status DBImpl::GetLiveFilesStorageInfo(
|
||||
|
||||
// NOTE: This implementation was largely migrated from Checkpoint.
|
||||
|
||||
Status s;
|
||||
VectorWalPtr live_wal_files;
|
||||
bool flush_memtable = true;
|
||||
if (!immutable_db_options_.allow_2pc) {
|
||||
@@ -216,12 +216,12 @@ Status DBImpl::GetLiveFilesStorageInfo(
|
||||
// Don't take archived log size into account when calculating wal
|
||||
// size for flush, and don't need to verify consistency with manifest
|
||||
// here & now.
|
||||
s = wal_manager_.GetSortedWalFiles(live_wal_files,
|
||||
/* need_seqnos */ false,
|
||||
/*include_archived*/ false);
|
||||
Status wal_s = wal_manager_.GetSortedWalFiles(live_wal_files,
|
||||
/* need_seqnos */ false,
|
||||
/*include_archived*/ false);
|
||||
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
if (!wal_s.ok()) {
|
||||
return wal_s;
|
||||
}
|
||||
|
||||
// Don't flush column families if total log size is smaller than
|
||||
@@ -242,13 +242,28 @@ Status DBImpl::GetLiveFilesStorageInfo(
|
||||
// This is a modified version of GetLiveFiles, to get access to more
|
||||
// metadata.
|
||||
mutex_.Lock();
|
||||
bool wal_locked = false;
|
||||
const bool needs_blob_direct_write_flush =
|
||||
HasInFlightBlobDirectWriteFilesWithLockHeld();
|
||||
if (needs_blob_direct_write_flush && !flush_memtable) {
|
||||
mutex_.Unlock();
|
||||
return Status::NotSupported(
|
||||
"Blob direct write requires flushing active blob files before "
|
||||
"capturing live files. Retry with flush enabled.");
|
||||
}
|
||||
if (flush_memtable) {
|
||||
bool wal_locked = lock_wal_count_ > 0;
|
||||
wal_locked = lock_wal_count_ > 0;
|
||||
if (wal_locked) {
|
||||
if (needs_blob_direct_write_flush) {
|
||||
mutex_.Unlock();
|
||||
return Status::NotSupported(
|
||||
"Blob direct write requires flushing active blob files before "
|
||||
"capturing live files. Retry with WAL unlocked.");
|
||||
}
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
"Can't FlushForGetLiveFiles while WAL is locked");
|
||||
} else {
|
||||
Status status = FlushForGetLiveFiles();
|
||||
Status status = FlushForGetLiveFiles(opts.atomic_flush);
|
||||
if (!status.ok()) {
|
||||
mutex_.Unlock();
|
||||
ROCKS_LOG_ERROR(immutable_db_options_.info_log,
|
||||
@@ -392,17 +407,15 @@ Status DBImpl::GetLiveFilesStorageInfo(
|
||||
TEST_SYNC_POINT("CheckpointImpl::CreateCheckpoint:SavedLiveFiles1");
|
||||
TEST_SYNC_POINT("CheckpointImpl::CreateCheckpoint:SavedLiveFiles2");
|
||||
|
||||
if (s.ok()) {
|
||||
// FlushWAL is required to ensure we can physically copy everything
|
||||
// logically written to the WAL. (Sync not strictly required for
|
||||
// active WAL to be copied rather than hard linked, even when
|
||||
// Checkpoint guarantees that the copied-to file is sync-ed. Plus we can't
|
||||
// help track_and_verify_wals_in_manifest after manifest_size is
|
||||
// already determined.)
|
||||
s = FlushWAL(/*sync=*/false);
|
||||
if (s.IsNotSupported()) { // read-only DB or similar
|
||||
s = Status::OK();
|
||||
}
|
||||
// FlushWAL is required to ensure we can physically copy everything
|
||||
// logically written to the WAL. (Sync not strictly required for
|
||||
// active WAL to be copied rather than hard linked, even when
|
||||
// Checkpoint guarantees that the copied-to file is sync-ed. Plus we can't
|
||||
// help track_and_verify_wals_in_manifest after manifest_size is
|
||||
// already determined.)
|
||||
Status s = FlushWAL(/*sync=*/false);
|
||||
if (s.IsNotSupported()) { // read-only DB or similar
|
||||
s = Status::OK();
|
||||
}
|
||||
|
||||
TEST_SYNC_POINT("CheckpointImpl::CreateCustomCheckpoint:AfterGetLive1");
|
||||
|
||||
@@ -3849,6 +3849,125 @@ TEST_F(DBFlushTest, BuilderWriteFaultPropagationDuringFlush) {
|
||||
|
||||
Close();
|
||||
}
|
||||
// Regression test for table cache leak when flush install fails.
|
||||
// BuildTable caches the flush output file (for user reads) but if the
|
||||
// subsequent TryInstallMemtableFlushResults (LogAndApply) fails, the
|
||||
// cache entry was never evicted. The FindObsoleteFiles backstop normally
|
||||
// catches this, but fails under metadata read fault injection
|
||||
// (open_metadata_read_fault_one_in), causing
|
||||
// TEST_VerifyNoObsoleteFilesCached to fire during Close().
|
||||
TEST_F(DBFlushTest, LeakedTableCacheEntryOnFlushInstallFailure) {
|
||||
auto fault_fs = std::make_shared<FaultInjectionTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> fault_env(NewCompositeEnv(fault_fs));
|
||||
|
||||
Options options = CurrentOptions();
|
||||
options.env = fault_env.get();
|
||||
options.paranoid_file_checks = true;
|
||||
// Disable auto-recovery so it doesn't clean up the orphan on a separate
|
||||
// thread where metadata faults aren't enabled.
|
||||
options.max_bgerror_resume_count = 0;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Write data so flush produces a non-empty file.
|
||||
ASSERT_OK(Put("a", std::string(1024, 'x')));
|
||||
|
||||
// After BuildTable succeeds (caching the output file), inject a failure
|
||||
// before the install-or-rollback decision. This simulates scenarios like
|
||||
// CF dropped or shutdown after BuildTable. Also deactivate the filesystem
|
||||
// so the post-flush FindObsoleteFiles backstop cannot scan the directory.
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"FlushJob::Run:PostBuildTable", [&](void* arg) {
|
||||
*static_cast<Status*>(arg) = Status::IOError("injected");
|
||||
fault_fs->SetFilesystemActive(false);
|
||||
});
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
// Trigger flush — BuildTable succeeds but LogAndApply fails.
|
||||
Status s = Flush();
|
||||
ASSERT_NOK(s);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
// With the fix, the leaked cache entry was already evicted by
|
||||
// ReleaseObsolete in FlushJob::Run(). Without the fix, it persists.
|
||||
// We can verify by counting non-live cache entries. The quarantine
|
||||
// includes the file (BG error is active), but that's only a workaround --
|
||||
// the cache entry should not exist at all with the fix.
|
||||
//
|
||||
// Check: with the fix, the table cache should be empty (the flush output
|
||||
// was never installed and the fix evicted it). Without the fix, there's
|
||||
// a leaked entry.
|
||||
int cache_entries = 0;
|
||||
auto count_fn = [&cache_entries](const Slice&, Cache::ObjectPtr, size_t,
|
||||
const Cache::CacheItemHelper*) {
|
||||
cache_entries++;
|
||||
};
|
||||
dbfull()->TEST_table_cache()->ApplyToAllEntries(count_fn, {});
|
||||
ASSERT_EQ(cache_entries, 0)
|
||||
<< "Leaked table cache entry for flush output that was never installed";
|
||||
|
||||
// Reactivate filesystem for clean shutdown.
|
||||
fault_fs->SetFilesystemActive(true);
|
||||
db_ = nullptr; // destructor handles Close
|
||||
}
|
||||
|
||||
// Verify that ConstructFragmentedRangeTombstones runs after MarkImmutable
|
||||
// in SwitchMemtable. A range tombstone inserted between construction and
|
||||
// immutability would cause a flush entry count mismatch.
|
||||
TEST_F(DBFlushTest, FlushAfterReadPathRangeTombstoneInsertion) {
|
||||
Options options = CurrentOptions();
|
||||
options.min_tombstones_for_range_conversion = 2;
|
||||
options.flush_verify_memtable_count = true;
|
||||
options.statistics = CreateDBStatistics();
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Write base data and flush to L0.
|
||||
for (char c = 'a'; c <= 'h'; c++) {
|
||||
ASSERT_OK(Put(std::string(1, c), std::string("v") + c));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Delete contiguous keys in the memtable.
|
||||
for (const auto& key : {"b", "c", "d", "e"}) {
|
||||
ASSERT_OK(Delete(key));
|
||||
}
|
||||
|
||||
// When SwitchMemtable hits the sync point after
|
||||
// ConstructFragmentedRangeTombstones, directly call
|
||||
// AddLogicallyRedundantRangeTombstone on the memtable being switched.
|
||||
// We can't create an iterator here (would deadlock on the DB mutex).
|
||||
auto* cfh = static_cast<ColumnFamilyHandleImpl*>(db_->DefaultColumnFamily());
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"DBImpl::SwitchMemtable:AfterConstructFragmentedRangeTombstones",
|
||||
[&](void*) {
|
||||
// Try to insert a range tombstone. With the fix (Construct after
|
||||
// MarkImmutable), this returns false because the memtable is
|
||||
// already immutable. Without the fix, this succeeds and creates
|
||||
// an entry count mismatch.
|
||||
// Try to insert a range tombstone. With the fix (Construct after
|
||||
// MarkImmutable), this returns false because the memtable is
|
||||
// already immutable. Without the fix, this succeeds and creates
|
||||
// an entry count mismatch.
|
||||
cfh->cfd()->mem()->AddLogicallyRedundantRangeTombstone(
|
||||
1 /* seq */, "b", "f", cfh->cfd()->GetIngestSstLock());
|
||||
});
|
||||
SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
Status s = Flush();
|
||||
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
ASSERT_OK(s);
|
||||
|
||||
ASSERT_EQ(Get("a"), "va");
|
||||
ASSERT_EQ(Get("b"), "NOT_FOUND");
|
||||
ASSERT_EQ(Get("d"), "NOT_FOUND");
|
||||
ASSERT_EQ(Get("f"), "vf");
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "db/db_impl/compacted_db_impl.h"
|
||||
|
||||
#include "db/blob/blob_fetcher.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/version_set.h"
|
||||
#include "logging/logging.h"
|
||||
@@ -75,13 +76,16 @@ Status CompactedDBImpl::Get(const ReadOptions& _read_options,
|
||||
}
|
||||
|
||||
GetWithTimestampReadCallback read_cb(kMaxSequenceNumber);
|
||||
BlobFetcher blob_fetcher(version_, read_options);
|
||||
std::string* ts =
|
||||
user_comparator_->timestamp_size() > 0 ? timestamp : nullptr;
|
||||
LookupKey lkey(key, kMaxSequenceNumber, read_options.timestamp);
|
||||
GetContext get_context(user_comparator_, nullptr, nullptr, nullptr,
|
||||
GetContext::kNotFound, lkey.user_key(), value,
|
||||
/*columns=*/nullptr, ts, nullptr, nullptr, true,
|
||||
nullptr, nullptr, nullptr, nullptr, &read_cb);
|
||||
nullptr, nullptr, nullptr, nullptr, &read_cb,
|
||||
nullptr /* is_blob_index */, 0 /* tracing_get_id */,
|
||||
&blob_fetcher);
|
||||
|
||||
const FdWithKeyRange& f = files_.files[FindFile(lkey.user_key())];
|
||||
if (user_comparator_->CompareWithoutTimestamp(
|
||||
@@ -165,6 +169,7 @@ void CompactedDBImpl::MultiGet(const ReadOptions& _read_options,
|
||||
}
|
||||
|
||||
GetWithTimestampReadCallback read_cb(kMaxSequenceNumber);
|
||||
BlobFetcher blob_fetcher(version_, read_options);
|
||||
for (size_t i = 0; i < num_keys; ++i) {
|
||||
const Slice& key = keys[i];
|
||||
LookupKey lkey(key, kMaxSequenceNumber, read_options.timestamp);
|
||||
@@ -177,13 +182,15 @@ void CompactedDBImpl::MultiGet(const ReadOptions& _read_options,
|
||||
statuses[i] = Status::NotFound();
|
||||
continue;
|
||||
}
|
||||
|
||||
PinnableSlice& pinnable_val = values[i];
|
||||
std::string* timestamp = timestamps ? ×tamps[i] : nullptr;
|
||||
GetContext get_context(
|
||||
user_comparator_, nullptr, nullptr, nullptr, GetContext::kNotFound,
|
||||
lkey.user_key(), &pinnable_val, /*columns=*/nullptr,
|
||||
user_comparator_->timestamp_size() > 0 ? timestamp : nullptr, nullptr,
|
||||
nullptr, true, nullptr, nullptr, nullptr, nullptr, &read_cb);
|
||||
nullptr, true, nullptr, nullptr, nullptr, nullptr, &read_cb,
|
||||
nullptr /* is_blob_index */, 0 /* tracing_get_id */, &blob_fetcher);
|
||||
TableReader* t = nullptr;
|
||||
TableCache::TypedHandle* handle = nullptr;
|
||||
Status status = cfd_->table_cache()->FindTable(
|
||||
@@ -201,12 +208,10 @@ void CompactedDBImpl::MultiGet(const ReadOptions& _read_options,
|
||||
}
|
||||
if (!status.ok() && !status.IsNotFound()) {
|
||||
statuses[i] = status;
|
||||
} else if (get_context.State() == GetContext::kFound) {
|
||||
statuses[i] = Status::OK();
|
||||
} else {
|
||||
if (get_context.State() == GetContext::kFound) {
|
||||
statuses[i] = Status::OK();
|
||||
} else {
|
||||
statuses[i] = Status::NotFound();
|
||||
}
|
||||
statuses[i] = Status::NotFound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ class CompactedDBImpl : public DBImpl {
|
||||
// Share with DBImplReadOnly?
|
||||
|
||||
protected:
|
||||
Status FlushForGetLiveFiles() override {
|
||||
Status FlushForGetLiveFiles(bool /*force_atomic_flush*/) override {
|
||||
// No-op for read-only DB
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
+635
-45
@@ -15,6 +15,7 @@
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstdio>
|
||||
#include <deque>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -23,11 +24,14 @@
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "db/arena_wrapped_db_iter.h"
|
||||
#include "db/attribute_group_iterator_impl.h"
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/builder.h"
|
||||
#include "db/coalescing_iterator.h"
|
||||
#include "db/compaction/compaction_job.h"
|
||||
@@ -54,6 +58,7 @@
|
||||
#include "db/table_properties_collector.h"
|
||||
#include "db/transaction_log_impl.h"
|
||||
#include "db/version_set.h"
|
||||
#include "db/wide/wide_column_serialization.h"
|
||||
#include "db/write_batch_internal.h"
|
||||
#include "db/write_callback.h"
|
||||
#include "env/unique_id_gen.h"
|
||||
@@ -169,6 +174,7 @@ DBImpl::DBImpl(const DBOptions& options, const std::string& dbname,
|
||||
const bool seq_per_batch, const bool batch_per_txn,
|
||||
bool read_only)
|
||||
: dbname_(dbname),
|
||||
read_only_(read_only),
|
||||
own_info_log_(options.info_log == nullptr),
|
||||
initial_db_options_(SanitizeOptions(dbname, options, read_only,
|
||||
&init_logger_creation_s_)),
|
||||
@@ -485,11 +491,37 @@ void DBImpl::CancelAllBackgroundWork(bool wait) {
|
||||
s.PermitUncheckedError();
|
||||
|
||||
InstrumentedMutexLock l(&mutex_);
|
||||
if (!shutting_down_.load(std::memory_order_acquire) &&
|
||||
// Blob direct-write relies on clean shutdown flushes to register live blob
|
||||
// files in MANIFEST before close. WAL recovery of unregistered files is out
|
||||
// of scope in v1.
|
||||
const bool force_flush_for_blob_direct_write =
|
||||
HasAnyBlobDirectWriteColumnFamilyWithLockHeld();
|
||||
const bool flush_for_unpersisted_data =
|
||||
has_unpersisted_data_.load(std::memory_order_relaxed) &&
|
||||
!mutable_db_options_.avoid_flush_during_shutdown) {
|
||||
!mutable_db_options_.avoid_flush_during_shutdown;
|
||||
const bool already_shutting_down =
|
||||
shutting_down_.load(std::memory_order_acquire);
|
||||
if (already_shutting_down && force_flush_for_blob_direct_write &&
|
||||
HasInFlightBlobDirectWriteFilesWithLockHeld()) {
|
||||
// Flush jobs treat `shutting_down_` as a rollback condition, so once the
|
||||
// shutdown marker is already published we cannot safely promise a final
|
||||
// BDW registration flush anymore. Make that loss of crash-safety explicit.
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"Shutdown already in progress before blob direct-write "
|
||||
"close flush; active blob files may remain unregistered.");
|
||||
}
|
||||
if (!already_shutting_down &&
|
||||
(flush_for_unpersisted_data || force_flush_for_blob_direct_write)) {
|
||||
s = DBImpl::FlushAllColumnFamilies(FlushOptions(), FlushReason::kShutDown);
|
||||
s.PermitUncheckedError(); //**TODO: What to do on error?
|
||||
if (!s.ok()) {
|
||||
ROCKS_LOG_WARN(
|
||||
immutable_db_options_.info_log, "Shutdown flush failed%s: %s",
|
||||
force_flush_for_blob_direct_write
|
||||
? "; active blob direct-write files may remain unregistered"
|
||||
: "",
|
||||
s.ToString().c_str());
|
||||
s.PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
|
||||
// Cancel awaiting remote compactions
|
||||
@@ -523,6 +555,189 @@ void DBImpl::UntrackDataFiles() {
|
||||
/*track=*/false);
|
||||
}
|
||||
|
||||
bool DBImpl::HasAnyBlobDirectWriteColumnFamily() {
|
||||
return blob_direct_write_cf_count_.load(std::memory_order_relaxed) > 0;
|
||||
}
|
||||
|
||||
bool DBImpl::HasAnyBlobDirectWriteColumnFamilyWithLockHeld() {
|
||||
mutex_.AssertHeld();
|
||||
return blob_direct_write_cf_count_.load(std::memory_order_relaxed) > 0;
|
||||
}
|
||||
|
||||
bool DBImpl::HasInFlightBlobDirectWriteFilesWithLockHeld() {
|
||||
mutex_.AssertHeld();
|
||||
if (!HasAnyBlobDirectWriteColumnFamilyWithLockHeld()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
UnorderedSet<uint64_t> active_blob_direct_write_files;
|
||||
for (auto* cfd : *versions_->GetColumnFamilySet()) {
|
||||
if (cfd->IsDropped()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto* mgr = cfd->blob_partition_manager();
|
||||
if (mgr == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
mgr->GetActiveBlobFileNumbers(&active_blob_direct_write_files);
|
||||
if (!active_blob_direct_write_files.empty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void DBImpl::MaybeInitBlobDirectWriteColumnFamily(
|
||||
ColumnFamilyData* cfd, const ColumnFamilyOptions& cf_options,
|
||||
const std::string& column_family_name) {
|
||||
assert(cfd != nullptr);
|
||||
|
||||
if (!cf_options.enable_blob_files || !cf_options.enable_blob_direct_write) {
|
||||
return;
|
||||
}
|
||||
if (cfd->blob_partition_manager() != nullptr) {
|
||||
// DB::Open(..., create_missing_column_families=true, ...) can create the
|
||||
// missing CF during open and then revisit the same handle in the post-open
|
||||
// initialization loop below. Treat BDW initialization as idempotent for
|
||||
// that path so we do not double-register the same CF.
|
||||
return;
|
||||
}
|
||||
|
||||
auto mgr = std::make_shared<BlobFilePartitionManager>(
|
||||
cf_options.blob_direct_write_partitions,
|
||||
cf_options.blob_direct_write_partition_strategy,
|
||||
[vs = versions_.get()]() { return vs->NewFileNumber(); }, fs_.get(),
|
||||
immutable_db_options_.clock, stats_, file_options_, dbname_,
|
||||
column_family_name, cf_options.blob_file_size,
|
||||
immutable_db_options_.use_fsync, cfd->blob_file_cache(), &blob_callback_,
|
||||
immutable_db_options_.listeners,
|
||||
immutable_db_options_.file_checksum_gen_factory.get(),
|
||||
immutable_db_options_.checksum_handoff_file_types, io_tracer_, db_id_,
|
||||
db_session_id_, immutable_db_options_.info_log.get());
|
||||
|
||||
cfd->SetBlobPartitionManager(std::move(mgr));
|
||||
RegisterBlobDirectWriteColumnFamily();
|
||||
}
|
||||
|
||||
void DBImpl::RegisterBlobDirectWriteColumnFamily() {
|
||||
blob_direct_write_cf_count_.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void DBImpl::UnregisterBlobDirectWriteColumnFamily() {
|
||||
uint32_t current =
|
||||
blob_direct_write_cf_count_.load(std::memory_order_relaxed);
|
||||
while (current != 0) {
|
||||
if (blob_direct_write_cf_count_.compare_exchange_weak(
|
||||
current, current - 1, std::memory_order_relaxed)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
|
||||
Status DBImpl::MaybeWriteWalMarkersToManifestOnClose() {
|
||||
mutex_.AssertHeld();
|
||||
if (!mutable_db_options_.optimize_manifest_for_recovery ||
|
||||
!opened_successfully_ || read_only_ || versions_ == nullptr ||
|
||||
logs_.empty()) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
TEST_SYNC_POINT("DBImpl::CloseHelper:WriteWalMarkersOnCloseEntered");
|
||||
const ReadOptions read_options(Env::IOActivity::kUnknown);
|
||||
const WriteOptions write_options(Env::IOActivity::kUnknown);
|
||||
uint64_t max_wal_number = logs_.back().number;
|
||||
TEST_SYNC_POINT_CALLBACK("DBImpl::CloseHelper:CapturedMaxWal",
|
||||
&max_wal_number);
|
||||
const uint64_t new_log_num = max_wal_number + 1;
|
||||
|
||||
// std::deque keeps VersionEdit pointers stable across emplace_back.
|
||||
std::deque<VersionEdit> edits;
|
||||
autovector<ColumnFamilyData*> cfds;
|
||||
autovector<autovector<VersionEdit*>> edit_lists;
|
||||
VersionEdit global_edit;
|
||||
bool persist_global_edit = false;
|
||||
bool reserved_log_number = false;
|
||||
|
||||
bool all_cfs_empty = true;
|
||||
for (auto* cfd : *versions_->GetColumnFamilySet()) {
|
||||
if (cfd->IsDropped()) {
|
||||
continue;
|
||||
}
|
||||
// cfd->IsEmpty() checks both mem and imm. An immutable memtable queued for
|
||||
// flush still pins an older WAL; advancing past it would drop unflushed
|
||||
// records on the next open.
|
||||
if (!cfd->IsEmpty()) {
|
||||
all_cfs_empty = false;
|
||||
continue;
|
||||
}
|
||||
if (new_log_num > cfd->GetLogNumber()) {
|
||||
if (!reserved_log_number) {
|
||||
const uint64_t next_file_number = versions_->current_next_file_number();
|
||||
if (next_file_number <= new_log_num) {
|
||||
versions_->FetchAddFileNumber(new_log_num + 1 - next_file_number);
|
||||
global_edit.SetNextFile(versions_->current_next_file_number());
|
||||
persist_global_edit = true;
|
||||
}
|
||||
reserved_log_number = true;
|
||||
}
|
||||
edits.emplace_back();
|
||||
VersionEdit& edit = edits.back();
|
||||
edit.SetLogNumber(new_log_num);
|
||||
edit.SetColumnFamily(cfd->GetID());
|
||||
cfds.push_back(cfd);
|
||||
edit_lists.emplace_back();
|
||||
edit_lists.back().push_back(&edit);
|
||||
}
|
||||
}
|
||||
|
||||
if (all_cfs_empty && !allow_2pc()) {
|
||||
if (new_log_num > versions_->min_log_number_to_keep()) {
|
||||
global_edit.SetMinLogNumberToKeep(new_log_num);
|
||||
persist_global_edit = true;
|
||||
}
|
||||
// Mirror the monotonicity guard in db_impl_files.cc:
|
||||
// PrecomputeWalDeletionEdit emits DeleteWalsBefore only when the
|
||||
// marker actually advances the WalSet's existing min. Skipping
|
||||
// this guard would let a stale-replay edit regress WalSet state.
|
||||
if (immutable_db_options_.track_and_verify_wals_in_manifest &&
|
||||
new_log_num > versions_->GetWalSet().GetMinWalNumberToKeep()) {
|
||||
global_edit.DeleteWalsBefore(new_log_num);
|
||||
persist_global_edit = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (persist_global_edit) {
|
||||
ColumnFamilyData* default_cfd =
|
||||
versions_->GetColumnFamilySet()->GetDefault();
|
||||
if (default_cfd && !default_cfd->IsDropped()) {
|
||||
bool attached_to_existing = false;
|
||||
for (size_t i = 0; i < cfds.size(); ++i) {
|
||||
if (cfds[i] == default_cfd) {
|
||||
edit_lists[i].push_back(&global_edit);
|
||||
attached_to_existing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!attached_to_existing) {
|
||||
cfds.push_back(default_cfd);
|
||||
edit_lists.emplace_back();
|
||||
edit_lists.back().push_back(&global_edit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cfds.empty()) {
|
||||
return Status::OK();
|
||||
}
|
||||
return versions_->LogAndApply(cfds, read_options, write_options, edit_lists,
|
||||
&mutex_, directories_.GetDbDir(),
|
||||
/*new_descriptor_log=*/false);
|
||||
}
|
||||
|
||||
Status DBImpl::CloseHelper() {
|
||||
// Guarantee that there is no background error recovery in progress before
|
||||
// continuing with the shutdown
|
||||
@@ -614,7 +829,7 @@ Status DBImpl::CloseHelper() {
|
||||
// manifest file), it is not able to identify live files correctly. As a
|
||||
// result, all "live" files can get deleted by accident. However, corrupted
|
||||
// manifest is recoverable by RepairDB().
|
||||
if (opened_successfully_) {
|
||||
if (opened_successfully_ && !read_only_) {
|
||||
JobContext job_context(next_job_id_.fetch_add(1));
|
||||
FindObsoleteFiles(&job_context, true);
|
||||
|
||||
@@ -627,6 +842,17 @@ Status DBImpl::CloseHelper() {
|
||||
job_context.Clean();
|
||||
mutex_.Lock();
|
||||
}
|
||||
// Best-effort warm-reopen optimization: after obsolete-file cleanup and
|
||||
// before logs_.clear(), persist close-time WAL markers that can reduce
|
||||
// recovery work on the next open. This matters most when some CFs have no
|
||||
// unflushed writes and/or the newest WAL is empty, since recovery would
|
||||
// otherwise append marker-only MANIFEST edits on reopen.
|
||||
Status manifest_s = MaybeWriteWalMarkersToManifestOnClose();
|
||||
if (!manifest_s.ok()) {
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"optimize_manifest_for_recovery close-time write failed: %s",
|
||||
manifest_s.ToString().c_str());
|
||||
}
|
||||
{
|
||||
InstrumentedMutexLock lock(&wal_write_mutex_);
|
||||
for (auto l : wals_to_free_) {
|
||||
@@ -822,8 +1048,9 @@ static uint64_t GetMinTimeBasedCompactionInterval(
|
||||
}
|
||||
|
||||
uint64_t DBImpl::ComputeTriggerCompactionPeriod() {
|
||||
// Start with a maximum period of every 12 hours.
|
||||
uint64_t period_sec = 12 * 60 * 60;
|
||||
// Start with the configured maximum, then reduce based on other options.
|
||||
uint64_t period_sec =
|
||||
mutable_db_options_.max_compaction_trigger_wakeup_seconds;
|
||||
|
||||
// Consider DB-level options that have the DB waking up periodically anyway.
|
||||
// Waking up to check for compactions at the same interval should be no
|
||||
@@ -1359,6 +1586,7 @@ Status DBImpl::SetOptions(
|
||||
for (const auto& cfd_opts : column_family_datas) {
|
||||
InstallSuperVersionForConfigChange(cfd_opts.first, &sv_context);
|
||||
}
|
||||
|
||||
persist_options_status =
|
||||
WriteOptionsFile(write_options, true /*db_mutex_already_held*/);
|
||||
bg_cv_.SignalAll();
|
||||
@@ -1550,9 +1778,12 @@ Status DBImpl::SetDBOptions(
|
||||
: new_options.max_open_files - 10);
|
||||
// Potential table cache capacity change requires updating if table
|
||||
// handles should get pinned.
|
||||
versions_->GetColumnFamilySet()->SetFastSstOpen(
|
||||
new_options.fast_sst_open);
|
||||
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
||||
if (!cfd->IsDropped()) {
|
||||
cfd->table_cache()->UpdateShouldPinTableHandles();
|
||||
cfd->table_cache()->SetFastSstOpen(new_options.fast_sst_open);
|
||||
}
|
||||
}
|
||||
wal_other_option_changed = mutable_db_options_.wal_bytes_per_sync !=
|
||||
@@ -2155,6 +2386,10 @@ void DBImpl::BackgroundCallPurge() {
|
||||
purge_files_.erase(it);
|
||||
|
||||
mutex_.Unlock();
|
||||
if (type == kBlobFile && ShouldKeepBlobFileDuringPurge(number, fname)) {
|
||||
mutex_.Lock();
|
||||
continue;
|
||||
}
|
||||
DeleteObsoleteFileImpl(job_id, fname, dir_to_sync, type, number);
|
||||
mutex_.Lock();
|
||||
}
|
||||
@@ -2479,6 +2714,217 @@ bool DBImpl::ShouldReferenceSuperVersion(const MergeContext& merge_context) {
|
||||
merge_context.GetOperands().size();
|
||||
}
|
||||
|
||||
static Slice GetBlobLookupUserKey(const Slice& user_key,
|
||||
const std::string* timestamp,
|
||||
std::string* user_key_with_ts) {
|
||||
if (timestamp == nullptr || timestamp->empty()) {
|
||||
return user_key;
|
||||
}
|
||||
|
||||
assert(user_key_with_ts != nullptr);
|
||||
user_key_with_ts->assign(user_key.data(), user_key.size());
|
||||
user_key_with_ts->append(timestamp->data(), timestamp->size());
|
||||
return Slice(*user_key_with_ts);
|
||||
}
|
||||
|
||||
static Status DecodeDirectWriteBlobIndex(const Slice& blob_index_slice,
|
||||
BlobIndex* blob_idx) {
|
||||
assert(blob_idx != nullptr);
|
||||
|
||||
Status status = blob_idx->DecodeFrom(blob_index_slice);
|
||||
if (status.ok() && blob_idx->HasTTL()) {
|
||||
status =
|
||||
Status::Corruption("Unexpected TTL blob index for blob direct write");
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
Status DBImpl::ResolveDirectWritePlainValue(
|
||||
const ReadOptions& read_options, const Slice& key, const Version* current,
|
||||
ColumnFamilyData* cfd, PinnableSlice* value, PinnableWideColumns* columns) {
|
||||
Slice blob_index_slice;
|
||||
std::string blob_index_storage;
|
||||
if (value != nullptr) {
|
||||
if (value->size() > 0) {
|
||||
blob_index_slice = Slice(value->data(), value->size());
|
||||
} else {
|
||||
blob_index_slice = Slice(*(value->GetSelf()));
|
||||
}
|
||||
} else {
|
||||
assert(columns != nullptr);
|
||||
|
||||
const WideColumns& plain_value_columns = columns->columns();
|
||||
// A plain value exposed through `PinnableWideColumns` is represented as a
|
||||
// single default column at index 0. Resolve that default-column payload as
|
||||
// the user's value.
|
||||
assert(plain_value_columns.size() == 1);
|
||||
assert(plain_value_columns.front().name() == kDefaultWideColumnName);
|
||||
blob_index_slice = plain_value_columns.front().value();
|
||||
}
|
||||
|
||||
PinnableSlice resolved_value;
|
||||
PinnableSlice* target = value != nullptr ? value : &resolved_value;
|
||||
if (value != nullptr) {
|
||||
if (value->IsPinned()) {
|
||||
// We reuse `value` as the destination for the resolved blob payload.
|
||||
// If it is still pinned to a table block, Reset() will drop that cleanup
|
||||
// chain, so preserve the encoded blob-index bytes first.
|
||||
blob_index_storage.assign(blob_index_slice.data(),
|
||||
blob_index_slice.size());
|
||||
blob_index_slice = Slice(blob_index_storage);
|
||||
}
|
||||
value->Reset();
|
||||
}
|
||||
|
||||
BlobIndex blob_idx;
|
||||
Status status = DecodeDirectWriteBlobIndex(blob_index_slice, &blob_idx);
|
||||
if (status.ok()) {
|
||||
status = BlobFilePartitionManager::ResolveBlobDirectWriteIndex(
|
||||
read_options, key, blob_idx, current, cfd->blob_file_cache(),
|
||||
nullptr /* prefetch_buffer */, target, nullptr /* bytes_read */);
|
||||
if (status.ok() && columns != nullptr) {
|
||||
columns->SetPlainValue(std::move(*target));
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
Status DBImpl::ResolveDirectWriteWideColumns(const ReadOptions& read_options,
|
||||
const Slice& key,
|
||||
const Version* current,
|
||||
ColumnFamilyData* cfd,
|
||||
PinnableWideColumns* columns) {
|
||||
assert(columns != nullptr);
|
||||
|
||||
const WideColumns& unresolved_columns = columns->columns();
|
||||
WideColumns resolved_columns;
|
||||
resolved_columns.reserve(unresolved_columns.size());
|
||||
|
||||
// `unresolved_blob_column_indices_` stores sorted column positions whose
|
||||
// values still hold encoded blob indexes. Keep resolved blob payloads alive
|
||||
// in a side array until the rebuilt entity is serialized back into `columns`.
|
||||
std::vector<PinnableSlice> resolved_blob_values(
|
||||
columns->unresolved_blob_column_indices_.size());
|
||||
size_t unresolved_blob_idx = 0;
|
||||
|
||||
for (size_t column_idx = 0; column_idx < unresolved_columns.size();
|
||||
++column_idx) {
|
||||
const bool is_unresolved_blob =
|
||||
unresolved_blob_idx < columns->unresolved_blob_column_indices_.size() &&
|
||||
columns->unresolved_blob_column_indices_[unresolved_blob_idx] ==
|
||||
column_idx;
|
||||
if (!is_unresolved_blob) {
|
||||
resolved_columns.emplace_back(unresolved_columns[column_idx].name(),
|
||||
unresolved_columns[column_idx].value());
|
||||
continue;
|
||||
}
|
||||
|
||||
BlobIndex blob_idx;
|
||||
Status s = DecodeDirectWriteBlobIndex(
|
||||
unresolved_columns[column_idx].value(), &blob_idx);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
Slice resolved_value;
|
||||
if (blob_idx.IsInlined()) {
|
||||
// V2 entities can still encode inline blob indexes. In that case the
|
||||
// bytes are already present in the entity, so no blob file read is
|
||||
// needed.
|
||||
resolved_value = blob_idx.value();
|
||||
} else {
|
||||
s = BlobFilePartitionManager::ResolveBlobDirectWriteIndex(
|
||||
read_options, key, blob_idx, current, cfd->blob_file_cache(),
|
||||
nullptr /* prefetch_buffer */,
|
||||
&resolved_blob_values[unresolved_blob_idx], nullptr /* bytes_read */);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
resolved_value = Slice(resolved_blob_values[unresolved_blob_idx]);
|
||||
}
|
||||
|
||||
resolved_columns.emplace_back(unresolved_columns[column_idx].name(),
|
||||
resolved_value);
|
||||
++unresolved_blob_idx;
|
||||
}
|
||||
|
||||
if (unresolved_blob_idx != columns->unresolved_blob_column_indices_.size()) {
|
||||
return Status::Corruption("Wide column blob metadata out of sync");
|
||||
}
|
||||
|
||||
std::string resolved_entity;
|
||||
Status status =
|
||||
WideColumnSerialization::Serialize(resolved_columns, resolved_entity);
|
||||
if (status.ok()) {
|
||||
status = columns->SetWideColumnValue(std::move(resolved_entity));
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
bool DBImpl::MaybeResolveDirectWriteValue(
|
||||
const ReadOptions& read_options, const Slice& key,
|
||||
bool resolve_direct_write_value, const Version* current,
|
||||
ColumnFamilyData* cfd, PinnableSlice* value, PinnableWideColumns* columns,
|
||||
Status* s, bool* is_blob_index, bool* value_found) {
|
||||
if (!s->ok() || !resolve_direct_write_value || (!value && !columns)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const bool needs_plain_value_resolution =
|
||||
is_blob_index != nullptr && *is_blob_index;
|
||||
const bool needs_wide_column_resolution =
|
||||
columns != nullptr && !columns->unresolved_blob_column_indices_.empty();
|
||||
if (!needs_plain_value_resolution && !needs_wide_column_resolution) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// This helper only handles the temporary read-time gap for blob direct write
|
||||
// references that can appear before flush makes the blob file visible through
|
||||
// normal Version metadata. There are two shapes to patch up here:
|
||||
// 1. a plain value encoded as a blob index
|
||||
// 2. a wide-column entity with some unresolved blob-valued columns
|
||||
// Once a blob file is registered in Version metadata, the normal integrated
|
||||
// blob read path is already agnostic to whether the file came from direct
|
||||
// write or flush/compaction. This helper only covers the pre-flush gap where
|
||||
// a memtable can already reference a blob file that is not yet
|
||||
// manifest-visible.
|
||||
if (read_options.read_tier == kBlockCacheTier) {
|
||||
// Resolving a pre-flush direct-write reference can require blob file I/O.
|
||||
// `kBlockCacheTier` forbids that, so surface the result as incomplete.
|
||||
if (value != nullptr) {
|
||||
value->Reset();
|
||||
} else {
|
||||
assert(columns != nullptr);
|
||||
columns->Reset();
|
||||
}
|
||||
if (value_found != nullptr) {
|
||||
*value_found = false;
|
||||
}
|
||||
*s = Status::Incomplete();
|
||||
if (is_blob_index != nullptr) {
|
||||
*is_blob_index = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (needs_plain_value_resolution) {
|
||||
*s = ResolveDirectWritePlainValue(read_options, key, current, cfd, value,
|
||||
columns);
|
||||
assert(is_blob_index != nullptr);
|
||||
*is_blob_index = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
assert(columns != nullptr);
|
||||
*s = ResolveDirectWriteWideColumns(read_options, key, current, cfd, columns);
|
||||
|
||||
if (is_blob_index != nullptr) {
|
||||
*is_blob_index = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Status DBImpl::GetImpl(const ReadOptions& read_options, const Slice& key,
|
||||
GetImplOptions& get_impl_options) {
|
||||
assert(get_impl_options.value != nullptr ||
|
||||
@@ -2615,40 +3061,63 @@ Status DBImpl::GetImpl(const ReadOptions& read_options, const Slice& key,
|
||||
bool skip_memtable = (read_options.read_tier == kPersistedTier &&
|
||||
has_unpersisted_data_.load(std::memory_order_relaxed));
|
||||
bool done = false;
|
||||
std::string* timestamp =
|
||||
ucmp->timestamp_size() > 0 ? get_impl_options.timestamp : nullptr;
|
||||
bool is_blob_index = false;
|
||||
bool* is_blob_ptr = get_impl_options.is_blob_index;
|
||||
auto* partition_mgr = cfd->blob_partition_manager();
|
||||
std::string timestamp_storage;
|
||||
std::string* timestamp = nullptr;
|
||||
if (ucmp->timestamp_size() > 0) {
|
||||
timestamp = get_impl_options.timestamp != nullptr
|
||||
? get_impl_options.timestamp
|
||||
: (partition_mgr != nullptr ? ×tamp_storage : nullptr);
|
||||
}
|
||||
if (partition_mgr != nullptr && !is_blob_ptr && get_impl_options.get_value) {
|
||||
is_blob_ptr = &is_blob_index;
|
||||
}
|
||||
const bool resolve_direct_write_value =
|
||||
partition_mgr != nullptr && (is_blob_ptr == &is_blob_index);
|
||||
std::string blob_lookup_key_storage;
|
||||
auto get_blob_lookup_key = [&]() -> Slice {
|
||||
return GetBlobLookupUserKey(key, timestamp, &blob_lookup_key_storage);
|
||||
};
|
||||
auto maybe_resolve_memtable_value = [&]() {
|
||||
if (resolve_direct_write_value) {
|
||||
const bool value_resolved = MaybeResolveDirectWriteValue(
|
||||
read_options, get_blob_lookup_key(), resolve_direct_write_value,
|
||||
sv->current, cfd, get_impl_options.value, get_impl_options.columns,
|
||||
&s, &is_blob_index, get_impl_options.value_found);
|
||||
if (!value_resolved && get_impl_options.value != nullptr) {
|
||||
get_impl_options.value->PinSelf();
|
||||
}
|
||||
} else if (get_impl_options.value != nullptr) {
|
||||
get_impl_options.value->PinSelf();
|
||||
}
|
||||
};
|
||||
if (!skip_memtable) {
|
||||
// Get value associated with key
|
||||
if (get_impl_options.get_value) {
|
||||
if (sv->mem->Get(
|
||||
lkey,
|
||||
get_impl_options.value ? get_impl_options.value->GetSelf()
|
||||
: nullptr,
|
||||
get_impl_options.columns, timestamp, &s, &merge_context,
|
||||
&max_covering_tombstone_seq, read_options,
|
||||
false /* immutable_memtable */, get_impl_options.callback,
|
||||
get_impl_options.is_blob_index)) {
|
||||
if (sv->mem->Get(lkey,
|
||||
get_impl_options.value
|
||||
? get_impl_options.value->GetSelf()
|
||||
: nullptr,
|
||||
get_impl_options.columns, timestamp, &s, &merge_context,
|
||||
&max_covering_tombstone_seq, read_options,
|
||||
false /* immutable_memtable */,
|
||||
get_impl_options.callback, is_blob_ptr)) {
|
||||
done = true;
|
||||
|
||||
if (get_impl_options.value) {
|
||||
get_impl_options.value->PinSelf();
|
||||
}
|
||||
maybe_resolve_memtable_value();
|
||||
|
||||
RecordTick(stats_, MEMTABLE_HIT);
|
||||
} else if ((s.ok() || s.IsMergeInProgress()) &&
|
||||
sv->imm->Get(lkey,
|
||||
get_impl_options.value
|
||||
? get_impl_options.value->GetSelf()
|
||||
: nullptr,
|
||||
get_impl_options.columns, timestamp, &s,
|
||||
&merge_context, &max_covering_tombstone_seq,
|
||||
read_options, get_impl_options.callback,
|
||||
get_impl_options.is_blob_index)) {
|
||||
sv->imm->Get(
|
||||
lkey,
|
||||
get_impl_options.value ? get_impl_options.value->GetSelf()
|
||||
: nullptr,
|
||||
get_impl_options.columns, timestamp, &s, &merge_context,
|
||||
&max_covering_tombstone_seq, read_options,
|
||||
get_impl_options.callback, is_blob_ptr)) {
|
||||
done = true;
|
||||
|
||||
if (get_impl_options.value) {
|
||||
get_impl_options.value->PinSelf();
|
||||
}
|
||||
maybe_resolve_memtable_value();
|
||||
|
||||
RecordTick(stats_, MEMTABLE_HIT);
|
||||
}
|
||||
@@ -2688,8 +3157,14 @@ Status DBImpl::GetImpl(const ReadOptions& read_options, const Slice& key,
|
||||
get_impl_options.get_value ? get_impl_options.value_found : nullptr,
|
||||
nullptr, nullptr,
|
||||
get_impl_options.get_value ? get_impl_options.callback : nullptr,
|
||||
get_impl_options.get_value ? get_impl_options.is_blob_index : nullptr,
|
||||
get_impl_options.get_value ? is_blob_ptr : nullptr,
|
||||
get_impl_options.get_value);
|
||||
if (get_impl_options.get_value && resolve_direct_write_value) {
|
||||
MaybeResolveDirectWriteValue(
|
||||
read_options, get_blob_lookup_key(), resolve_direct_write_value,
|
||||
sv->current, cfd, get_impl_options.value, get_impl_options.columns,
|
||||
&s, &is_blob_index, get_impl_options.value_found);
|
||||
}
|
||||
RecordTick(stats_, MEMTABLE_MISS);
|
||||
}
|
||||
|
||||
@@ -3268,9 +3743,13 @@ void DBImpl::MultiGetWithCallbackImpl(
|
||||
const ReadOptions& read_options, ColumnFamilyHandle* column_family,
|
||||
ReadCallback* callback,
|
||||
autovector<KeyContext*, MultiGetContext::MAX_BATCH_SIZE>* sorted_keys) {
|
||||
assert(sorted_keys != nullptr);
|
||||
std::array<ColumnFamilySuperVersionPair, 1> cf_sv_pairs;
|
||||
cf_sv_pairs[0] = ColumnFamilySuperVersionPair(column_family, nullptr);
|
||||
size_t num_keys = sorted_keys->size();
|
||||
if (num_keys == 0) {
|
||||
return;
|
||||
}
|
||||
SequenceNumber consistent_seqnum = kMaxSequenceNumber;
|
||||
bool sv_from_thread_local = false;
|
||||
Status s = MultiCFSnapshot<std::array<ColumnFamilySuperVersionPair, 1>>(
|
||||
@@ -3344,6 +3823,13 @@ Status DBImpl::MultiGetImpl(
|
||||
|
||||
assert(sorted_keys);
|
||||
assert(start_key + num_keys <= sorted_keys->size());
|
||||
if (num_keys == 0) {
|
||||
return Status::OK();
|
||||
}
|
||||
auto* cfh = static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
(*sorted_keys)[start_key]->column_family);
|
||||
ColumnFamilyData* cfd = cfh->cfd();
|
||||
auto* partition_mgr = cfd->blob_partition_manager();
|
||||
// Clear the timestamps for returning results so that we can distinguish
|
||||
// between tombstone or key that has never been written
|
||||
for (size_t i = start_key; i < start_key + num_keys; ++i) {
|
||||
@@ -3428,6 +3914,16 @@ Status DBImpl::MultiGetImpl(
|
||||
assert(key);
|
||||
assert(key->s);
|
||||
|
||||
if (partition_mgr != nullptr && key->s->ok()) {
|
||||
std::string blob_lookup_key_storage;
|
||||
MaybeResolveDirectWriteValue(
|
||||
read_options,
|
||||
GetBlobLookupUserKey(*key->key, key->timestamp,
|
||||
&blob_lookup_key_storage),
|
||||
/*resolve_direct_write_value=*/true, super_version->current, cfd,
|
||||
key->value, key->columns, key->s, &key->is_blob_index);
|
||||
}
|
||||
|
||||
if (key->s->ok()) {
|
||||
const auto& merge_threshold = read_options.merge_operand_count_threshold;
|
||||
if (merge_threshold.has_value() &&
|
||||
@@ -3817,6 +4313,7 @@ Status DBImpl::CreateColumnFamilyImpl(const ReadOptions& read_options,
|
||||
auto* cfd =
|
||||
versions_->GetColumnFamilySet()->GetColumnFamily(column_family_name);
|
||||
assert(cfd != nullptr);
|
||||
MaybeInitBlobDirectWriteColumnFamily(cfd, cf_options, column_family_name);
|
||||
InstallSuperVersionForConfigChange(cfd, &sv_context);
|
||||
|
||||
if (!cfd->mem()->IsSnapshotSupported()) {
|
||||
@@ -3914,6 +4411,9 @@ Status DBImpl::DropColumnFamilyImpl(ColumnFamilyHandle* column_family) {
|
||||
s = versions_->LogAndApply(cfd, read_options, write_options, &edit,
|
||||
&mutex_, directories_.GetDbDir());
|
||||
write_thread_.ExitUnbatched(&w);
|
||||
if (s.ok() && cfd->blob_partition_manager() != nullptr) {
|
||||
UnregisterBlobDirectWriteColumnFamily();
|
||||
}
|
||||
}
|
||||
if (s.ok()) {
|
||||
auto& moptions = cfd->GetLatestMutableCFOptions();
|
||||
@@ -4034,6 +4534,14 @@ Iterator* DBImpl::NewIterator(const ReadOptions& _read_options,
|
||||
ColumnFamilyData* cfd = cfh->cfd();
|
||||
assert(cfd != nullptr);
|
||||
SuperVersion* sv = cfd->GetReferencedSuperVersion(this);
|
||||
{
|
||||
const Status s = FailIfTableFilterWithRangeConversion(
|
||||
read_options, sv->mutable_cf_options);
|
||||
if (!s.ok()) {
|
||||
CleanupSuperVersion(sv);
|
||||
return NewErrorIterator(s);
|
||||
}
|
||||
}
|
||||
if (read_options.timestamp && read_options.timestamp->size() > 0) {
|
||||
const Status s =
|
||||
FailIfReadCollapsedHistory(cfd, sv, *(read_options.timestamp));
|
||||
@@ -4133,14 +4641,21 @@ ArenaWrappedDBIter* DBImpl::NewIteratorImpl(
|
||||
// Laying out the iterators in the order of being accessed makes it more
|
||||
// likely that any iterator pointer is close to the iterator it points to so
|
||||
// that they are likely to be in the same cache line and/or page.
|
||||
return NewArenaWrappedDbIterator(
|
||||
env_, read_options, cfh, sv, snapshot, read_callback, this,
|
||||
expose_blob_index, allow_refresh, /*allow_mark_memtable_for_flush=*/true);
|
||||
return NewArenaWrappedDbIterator(env_, read_options, cfh, sv, snapshot,
|
||||
read_callback, this, expose_blob_index,
|
||||
allow_refresh,
|
||||
/*allow_mark_memtable_for_flush=*/true);
|
||||
}
|
||||
|
||||
std::unique_ptr<Iterator> DBImpl::NewCoalescingIterator(
|
||||
const ReadOptions& _read_options,
|
||||
const std::vector<ColumnFamilyHandle*>& column_families) {
|
||||
if (column_families.size() == 1) {
|
||||
// Preserve direct iterator semantics, including lazy value preparation,
|
||||
// when callers route a single CF through the multi-CF API.
|
||||
return std::unique_ptr<Iterator>(
|
||||
NewIterator(_read_options, column_families.front()));
|
||||
}
|
||||
return NewMultiCfIterator<Iterator, CoalescingIterator>(
|
||||
_read_options, column_families, [](const Status& s) {
|
||||
return std::unique_ptr<Iterator>(NewErrorIterator(s));
|
||||
@@ -4248,6 +4763,16 @@ Status DBImpl::NewIterators(
|
||||
}
|
||||
|
||||
assert(cf_sv_pairs.size() == column_families.size());
|
||||
for (const auto& cf_sv_pair : cf_sv_pairs) {
|
||||
s = FailIfTableFilterWithRangeConversion(
|
||||
read_options, cf_sv_pair.super_version->mutable_cf_options);
|
||||
if (!s.ok()) {
|
||||
for (const auto& cleanup_pair : cf_sv_pairs) {
|
||||
CleanupSuperVersion(cleanup_pair.super_version);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
if (read_options.tailing) {
|
||||
read_options.total_order_seek |=
|
||||
immutable_db_options_.prefix_seek_opt_in_only;
|
||||
@@ -4261,7 +4786,8 @@ Status DBImpl::NewIterators(
|
||||
cf_sv_pair.super_version->mutable_cf_options,
|
||||
cf_sv_pair.cfd->user_comparator(), iter,
|
||||
cf_sv_pair.super_version->current, kMaxSequenceNumber,
|
||||
nullptr /*read_callback*/, /*active_mem=*/nullptr, cf_sv_pair.cfh));
|
||||
nullptr /*read_callback*/, /*active_mem=*/nullptr, cf_sv_pair.cfh,
|
||||
/*expose_blob_index=*/false, /*arena=*/nullptr));
|
||||
}
|
||||
} else {
|
||||
for (const auto& cf_sv_pair : cf_sv_pairs) {
|
||||
@@ -4952,7 +5478,8 @@ void DBImpl::GetApproximateMemTableStats(ColumnFamilyHandle* column_family,
|
||||
Status DBImpl::GetApproximateSizes(const SizeApproximationOptions& options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
const Range* range, int n, uint64_t* sizes) {
|
||||
if (!options.include_memtables && !options.include_files) {
|
||||
if (!options.include_memtables && !options.include_files &&
|
||||
!options.include_blob_files) {
|
||||
return Status::InvalidArgument("Invalid options");
|
||||
}
|
||||
|
||||
@@ -4968,6 +5495,28 @@ Status DBImpl::GetApproximateSizes(const SizeApproximationOptions& options,
|
||||
|
||||
// TODO: plumb Env::IOActivity, Env::IOPriority
|
||||
const ReadOptions read_options;
|
||||
|
||||
// Pre-compute blob-to-SST ratio once (invariant across ranges for the same
|
||||
// Version). This avoids iterating all levels and blob files per range.
|
||||
double blob_to_sst_ratio = 0.0;
|
||||
if (options.include_blob_files) {
|
||||
const auto* vstorage = v->storage_info();
|
||||
uint64_t total_sst_size = 0;
|
||||
for (int level = 0; level < vstorage->num_non_empty_levels(); ++level) {
|
||||
total_sst_size += vstorage->NumLevelBytes(level);
|
||||
}
|
||||
if (total_sst_size > 0) {
|
||||
uint64_t total_blob_size = 0;
|
||||
const auto& blob_files = vstorage->GetBlobFiles();
|
||||
for (const auto& blob_file_meta : blob_files) {
|
||||
assert(blob_file_meta);
|
||||
total_blob_size += blob_file_meta->GetBlobFileSize();
|
||||
}
|
||||
blob_to_sst_ratio = static_cast<double>(total_blob_size) /
|
||||
static_cast<double>(total_sst_size);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
// Add timestamp if needed
|
||||
std::string start_with_ts, limit_with_ts;
|
||||
@@ -4979,16 +5528,26 @@ Status DBImpl::GetApproximateSizes(const SizeApproximationOptions& options,
|
||||
InternalKey k1(start.value(), kMaxSequenceNumber, kValueTypeForSeek);
|
||||
InternalKey k2(limit.value(), kMaxSequenceNumber, kValueTypeForSeek);
|
||||
sizes[i] = 0;
|
||||
if (options.include_files) {
|
||||
sizes[i] += versions_->ApproximateSize(
|
||||
// Compute SST size in range (needed for both include_files and
|
||||
// include_blob_files, since blob size is prorated by SST ratio).
|
||||
uint64_t sst_size_in_range = 0;
|
||||
if (options.include_files || options.include_blob_files) {
|
||||
sst_size_in_range = versions_->ApproximateSize(
|
||||
options, read_options, v, k1.Encode(), k2.Encode(),
|
||||
/*start_level=*/0,
|
||||
/*end_level=*/-1, TableReaderCaller::kUserApproximateSize);
|
||||
}
|
||||
if (options.include_files) {
|
||||
sizes[i] += sst_size_in_range;
|
||||
}
|
||||
if (options.include_memtables) {
|
||||
sizes[i] += sv->mem->ApproximateStats(k1.Encode(), k2.Encode()).size;
|
||||
sizes[i] += sv->imm->ApproximateStats(k1.Encode(), k2.Encode()).size;
|
||||
}
|
||||
if (options.include_blob_files) {
|
||||
sizes[i] += static_cast<uint64_t>(static_cast<double>(sst_size_in_range) *
|
||||
blob_to_sst_ratio);
|
||||
}
|
||||
}
|
||||
|
||||
ReturnAndCleanupSuperVersion(cfd, sv);
|
||||
@@ -6047,6 +6606,19 @@ Status DBImpl::IngestExternalFiles(
|
||||
TEST_SYNC_POINT("DBImpl::IngestExternalFiles:BeforeJobsRun:0");
|
||||
TEST_SYNC_POINT("DBImpl::IngestExternalFiles:BeforeJobsRun:1");
|
||||
TEST_SYNC_POINT("DBImpl::AddFile:Start");
|
||||
|
||||
// Acquire per-CF ingest_sst_lock as ReadLocks (shared) BEFORE the DB
|
||||
// mutex so the lock-acquisition order is ingest_sst_lock -> DB mutex
|
||||
// throughout. Use readlock so we still allow concurrent ingestions.
|
||||
std::vector<std::unique_ptr<ReadLock>> ingest_read_locks;
|
||||
ingest_read_locks.reserve(num_cfs);
|
||||
for (size_t i = 0; i != num_cfs; ++i) {
|
||||
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
|
||||
if (!cfd->IsDropped()) {
|
||||
ingest_read_locks.emplace_back(
|
||||
std::make_unique<ReadLock>(&cfd->GetIngestSstLock()));
|
||||
}
|
||||
}
|
||||
{
|
||||
InstrumentedMutexLock l(&mutex_);
|
||||
TEST_SYNC_POINT("DBImpl::AddFile:MutexLock");
|
||||
@@ -6136,6 +6708,23 @@ Status DBImpl::IngestExternalFiles(
|
||||
ingestion_jobs[i].RegisterRange();
|
||||
}
|
||||
}
|
||||
// Now that Run() has assigned the actual seqno for each ingested file,
|
||||
// bump each affected memtable's ingest_seqno_barrier_ to that exact
|
||||
// value. We still hold the per-CF ingest_sst_lock as a ReadLock; any
|
||||
// concurrent conversion's TryWriteLock on the same lock fails, so no
|
||||
// converter can be reading or about to read the barrier while we
|
||||
// update it. After we release the ReadLocks at function exit, the
|
||||
// next conversion observes the new barrier and refuses any insert
|
||||
// with insert_seq < assigned.
|
||||
if (status.ok()) {
|
||||
for (size_t i = 0; i != num_cfs; ++i) {
|
||||
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
|
||||
SequenceNumber assigned = ingestion_jobs[i].MaxAssignedSequenceNumber();
|
||||
if (assigned > 0) {
|
||||
cfd->mem()->BumpIngestSeqnoBarrier(assigned);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status.ok()) {
|
||||
ReadOptions read_options;
|
||||
read_options.fill_cache = args[0].options.fill_cache;
|
||||
@@ -7003,11 +7592,12 @@ void DBImpl::TriggerPeriodicCompaction() {
|
||||
if (cfd->queued_for_compaction()) {
|
||||
continue;
|
||||
}
|
||||
// Check if this CF has any time-based compaction trigger configured.
|
||||
// This includes periodic_compaction_seconds, ttl, or FIFO temperature
|
||||
// thresholds. Note: periodic_compaction_seconds may be 0 even when
|
||||
// ttl or temperature thresholds are set, due to option sanitization.
|
||||
if (GetMinTimeBasedCompactionInterval(cfd->GetLatestCFOptions()) > 0) {
|
||||
// Check if this CF has any time-based or read-triggered compaction
|
||||
// configured. This includes periodic_compaction_seconds, ttl, FIFO
|
||||
// temperature thresholds, or read_triggered_compaction_threshold.
|
||||
if (GetMinTimeBasedCompactionInterval(cfd->GetLatestCFOptions()) > 0 ||
|
||||
cfd->GetLatestMutableCFOptions().read_triggered_compaction_threshold >
|
||||
0.0) {
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
"DBImpl::TriggerPeriodicCompaction:BeforeComputeCompactionScore",
|
||||
cfd);
|
||||
|
||||
+206
-14
@@ -260,6 +260,10 @@ class DBImpl : public DB {
|
||||
const WriteOptions& options,
|
||||
std::shared_ptr<WriteBatchWithIndex> wbwi) override;
|
||||
|
||||
// Returns true if any live column family currently has blob direct write
|
||||
// enabled.
|
||||
bool HasAnyBlobDirectWriteColumnFamily();
|
||||
|
||||
using DB::Get;
|
||||
Status Get(const ReadOptions& _read_options,
|
||||
ColumnFamilyHandle* column_family, const Slice& key,
|
||||
@@ -1367,6 +1371,7 @@ class DBImpl : public DB {
|
||||
|
||||
protected:
|
||||
const std::string dbname_;
|
||||
const bool read_only_;
|
||||
// TODO(peterd): unify with VersionSet::db_id_
|
||||
std::string db_id_;
|
||||
// db_session_id_ is an identifier that gets reset
|
||||
@@ -1531,7 +1536,7 @@ class DBImpl : public DB {
|
||||
Status FlushAllColumnFamilies(const FlushOptions& flush_options,
|
||||
FlushReason flush_reason);
|
||||
|
||||
virtual Status FlushForGetLiveFiles();
|
||||
virtual Status FlushForGetLiveFiles(bool force_atomic_flush = false);
|
||||
|
||||
void NewThreadStatusCfInfo(ColumnFamilyData* cfd) const;
|
||||
|
||||
@@ -1551,10 +1556,17 @@ class DBImpl : public DB {
|
||||
// @param memtable_updated Whether the same write that ingests wbwi has
|
||||
// updated memtable. This is useful for determining whether to set bg
|
||||
// error when IngestWBWIAsMemtable fails.
|
||||
// @param ingest_wbwi_for_commit Whether wbwi ingestion is publishing the
|
||||
// committed data of a prepared transaction. This means a failure can leave
|
||||
// committed data durable in WAL but not published in memtables.
|
||||
// @param ignore_missing_cf If true, skip column families not found in the DB
|
||||
// instead of returning an error.
|
||||
Status IngestWBWIAsMemtable(std::shared_ptr<WriteBatchWithIndex> wbwi,
|
||||
const WBWIMemTable::SeqnoRange& assigned_seqno,
|
||||
uint64_t min_prep_log, SequenceNumber last_seqno,
|
||||
bool memtable_updated, bool ignore_missing_cf);
|
||||
bool memtable_updated,
|
||||
bool ingest_wbwi_for_commit,
|
||||
bool ignore_missing_cf);
|
||||
|
||||
// If disable_memtable is set the application logic must guarantee that the
|
||||
// batch will still be skipped from memtable during the recovery. An excption
|
||||
@@ -1577,21 +1589,131 @@ class DBImpl : public DB {
|
||||
// See more in comment above PreReleaseCallback::Callback().
|
||||
// post_memtable_callback is called after memtable write but before publishing
|
||||
// the sequence number to readers.
|
||||
// `trace_batch_override`, when non-null, supplies the logical batch to trace
|
||||
// instead of `updates`. Fast paths may rewrite or rebuild `updates` before
|
||||
// apply, but tracing should still record the original user-visible batch.
|
||||
// `skip_blob_direct_write_transform` indicates the caller has already
|
||||
// performed any needed batch-level blob direct-write preprocessing.
|
||||
// `deferred_put_entities`, when non-null, carries structured `PutEntity()`
|
||||
// ops that should be materialized into `updates` after `PreprocessWrite()`
|
||||
// has selected the target memtable/blob generation.
|
||||
//
|
||||
// The main write queue. This is the only write queue that updates
|
||||
// LastSequence. When using one write queue, the same sequence also indicates
|
||||
// the last published sequence.
|
||||
Status WriteImpl(const WriteOptions& options, WriteBatch* updates,
|
||||
WriteCallback* callback = nullptr,
|
||||
UserWriteCallback* user_write_cb = nullptr,
|
||||
uint64_t* wal_used = nullptr, uint64_t log_ref = 0,
|
||||
bool disable_memtable = false, uint64_t* seq_used = nullptr,
|
||||
size_t batch_cnt = 0,
|
||||
PreReleaseCallback* pre_release_callback = nullptr,
|
||||
PostMemTableCallback* post_memtable_callback = nullptr,
|
||||
std::shared_ptr<WriteBatchWithIndex> wbwi = nullptr);
|
||||
struct DeferredPutEntityBatch;
|
||||
Status WriteImpl(
|
||||
const WriteOptions& options, WriteBatch* updates,
|
||||
WriteCallback* callback = nullptr,
|
||||
UserWriteCallback* user_write_cb = nullptr, uint64_t* wal_used = nullptr,
|
||||
uint64_t log_ref = 0, bool disable_memtable = false,
|
||||
uint64_t* seq_used = nullptr, size_t batch_cnt = 0,
|
||||
PreReleaseCallback* pre_release_callback = nullptr,
|
||||
PostMemTableCallback* post_memtable_callback = nullptr,
|
||||
std::shared_ptr<WriteBatchWithIndex> wbwi = nullptr,
|
||||
WriteBatch* trace_batch_override = nullptr,
|
||||
bool skip_blob_direct_write_transform = false,
|
||||
const DeferredPutEntityBatch* deferred_put_entities = nullptr);
|
||||
|
||||
// Per-WriteImpl state that keeps BDW column families pinned through
|
||||
// referenced SuperVersions until the transformed write either commits or
|
||||
// rolls back.
|
||||
struct BlobDirectWriteContext;
|
||||
// High-level `PutEntity()` fast-path flow:
|
||||
//
|
||||
// non-BDW target CF:
|
||||
// PutEntityFastPath()
|
||||
// -> AppendPreprocessedPutEntityToBatch()
|
||||
// -> WritePreprocessedPutEntityBatch()
|
||||
// -> WriteImpl(skip_blob_direct_write_transform = true)
|
||||
//
|
||||
// BDW-enabled target CF:
|
||||
// PutEntityFastPath()
|
||||
// -> sort columns and stash DeferredPutEntityBatch::Op
|
||||
// -> WritePreprocessedPutEntityBatch()
|
||||
// -> WriteImpl(..., deferred_put_entities)
|
||||
// -> after PreprocessWrite() picks the memtable/blob generation:
|
||||
// AppendSortedPutEntityToBatch()
|
||||
//
|
||||
// The BDW branch defers final serialization until `WriteImpl()` knows the
|
||||
// target memtable/blob generation, avoiding the old
|
||||
// serialize -> deserialize -> serialize cycle on the write hot path.
|
||||
// Staging buffer for the `PutEntity()` fast path when blob direct write may
|
||||
// need to rewrite wide-column values. `PutEntityFastPath()` records the
|
||||
// logical operations here instead of serializing them into a `WriteBatch`
|
||||
// immediately, then passes this struct to `WriteImpl()`. Once
|
||||
// `PreprocessWrite()` has selected the memtable/blob-file generation,
|
||||
// `WriteImpl()` materializes each op directly into the final batch with blob
|
||||
// direct-write preprocessing applied. This avoids the old
|
||||
// serialize-deserialize-serialize cycle on the write hot path.
|
||||
struct DeferredPutEntityBatch {
|
||||
// The fast path sorts columns up front so `WriteImpl()` can append the
|
||||
// final serialized entity without redoing that work.
|
||||
struct Op {
|
||||
// Target column family for the logical `PutEntity()` op.
|
||||
uint32_t column_family_id = 0;
|
||||
// Owned key bytes kept alive until `WriteImpl()` rebuilds the batch.
|
||||
std::string key;
|
||||
// Pre-sorted columns ready for final serialization and blob
|
||||
// direct-write preprocessing.
|
||||
WideColumns sorted_columns;
|
||||
};
|
||||
|
||||
// Logical `PutEntity()` ops collected by the fast path and replayed into
|
||||
// the final `WriteBatch` inside `WriteImpl()`.
|
||||
std::vector<Op> ops;
|
||||
};
|
||||
// Rewrites a write batch for blob direct write when the current DB and batch
|
||||
// shape are compatible, recording touched managers and rollback metadata in
|
||||
// `blob_direct_write_ctx`.
|
||||
Status MaybeTransformBatchForBlobDirectWrite(
|
||||
const WriteOptions& write_options, WriteBatch** batch,
|
||||
bool should_write_to_memtable, bool lookup_from_write_thread,
|
||||
WriteBatch* transformed_storage,
|
||||
BlobDirectWriteContext* blob_direct_write_ctx);
|
||||
// Sorts and preprocesses one `PutEntity()` op, then appends the final
|
||||
// serialized form to `batch`.
|
||||
Status AppendPreprocessedPutEntityToBatch(
|
||||
const WriteOptions& write_options, WriteBatch* batch,
|
||||
ColumnFamilyHandle* column_family, const Slice& key,
|
||||
const WideColumns& columns,
|
||||
BlobDirectWriteContext* blob_direct_write_ctx);
|
||||
// Appends a `PutEntity()` op whose columns are already sorted, allowing the
|
||||
// fast path to avoid re-sorting before final serialization.
|
||||
Status AppendSortedPutEntityToBatch(
|
||||
const WriteOptions& write_options, WriteBatch* batch,
|
||||
uint32_t column_family_id, const Slice& key,
|
||||
const WideColumns& sorted_columns,
|
||||
BlobDirectWriteContext* blob_direct_write_ctx);
|
||||
// Specialized `PutEntity()` write path for a single column family. This
|
||||
// avoids building an intermediate serialized entity when blob direct write
|
||||
// needs to rewrite the final batch inside `WriteImpl()`.
|
||||
Status PutEntityFastPath(const WriteOptions& write_options,
|
||||
ColumnFamilyHandle* column_family, const Slice& key,
|
||||
const WideColumns& columns);
|
||||
// Specialized `PutEntity(AttributeGroups)` path that defers final batch
|
||||
// materialization until `WriteImpl()` when any target CF may use blob direct
|
||||
// write.
|
||||
Status PutEntityFastPath(const WriteOptions& write_options, const Slice& key,
|
||||
const AttributeGroups& attribute_groups);
|
||||
// Writes a batch prepared by the `PutEntity()` fast path. When
|
||||
// `deferred_put_entities` is present, `WriteImpl()` rebuilds `batch`
|
||||
// internally after `PreprocessWrite()` and disables normal write batching to
|
||||
// keep the staging state local to this write. `trace_batch` stays separate
|
||||
// so tracing can still emit the logical `PutEntity()` batch even when the
|
||||
// applied batch is materialized later inside `WriteImpl()`.
|
||||
Status WritePreprocessedPutEntityBatch(
|
||||
const WriteOptions& write_options, WriteBatch* batch,
|
||||
WriteBatch* trace_batch,
|
||||
const DeferredPutEntityBatch* deferred_put_entities = nullptr);
|
||||
// Flushes or syncs all blob direct-write managers touched by the current
|
||||
// transformed write before the write can proceed.
|
||||
Status SyncBlobDirectWriteManagers(
|
||||
const WriteOptions& write_options,
|
||||
const BlobDirectWriteContext& blob_direct_write_ctx);
|
||||
|
||||
Status PipelinedWriteImpl(const WriteOptions& options, WriteBatch* updates,
|
||||
WriteBatch* trace_batch,
|
||||
WriteCallback* callback = nullptr,
|
||||
UserWriteCallback* user_write_cb = nullptr,
|
||||
uint64_t* wal_used = nullptr, uint64_t log_ref = 0,
|
||||
@@ -1620,7 +1742,7 @@ class DBImpl : public DB {
|
||||
// marks start of a new sub-batch.
|
||||
Status WriteImplWALOnly(
|
||||
WriteThread* write_thread, const WriteOptions& options,
|
||||
WriteBatch* updates, WriteCallback* callback,
|
||||
WriteBatch* updates, WriteBatch* trace_batch, WriteCallback* callback,
|
||||
UserWriteCallback* user_write_cb, uint64_t* wal_used,
|
||||
const uint64_t log_ref, uint64_t* seq_used, const size_t sub_batch_cnt,
|
||||
PreReleaseCallback* pre_release_callback, const AssignOrder assign_order,
|
||||
@@ -1700,6 +1822,9 @@ class DBImpl : public DB {
|
||||
Status FailIfCfHasTs(const ColumnFamilyHandle* column_family) const;
|
||||
Status FailIfTsMismatchCf(ColumnFamilyHandle* column_family,
|
||||
const Slice& ts) const;
|
||||
Status FailIfTableFilterWithRangeConversion(
|
||||
const ReadOptions& read_options,
|
||||
const MutableCFOptions& mutable_cf_options) const;
|
||||
|
||||
// Check that the read timestamp `ts` is at or above the `full_history_ts_low`
|
||||
// timestamp in a `SuperVersion`. It's necessary to do this check after
|
||||
@@ -1778,6 +1903,21 @@ class DBImpl : public DB {
|
||||
friend class CompactionServiceTest_PreservedOptionsRemoteCompaction_Test;
|
||||
#endif
|
||||
|
||||
// Same as HasAnyBlobDirectWriteColumnFamily(), but requires `mutex_` held.
|
||||
bool HasAnyBlobDirectWriteColumnFamilyWithLockHeld();
|
||||
// Returns true if any BDW column family still owns blob files that have not
|
||||
// yet been made visible through MANIFEST. Requires `mutex_` held.
|
||||
bool HasInFlightBlobDirectWriteFilesWithLockHeld();
|
||||
// Creates and attaches the per-CF blob direct-write partition manager when
|
||||
// the column family is opened with the feature enabled.
|
||||
void MaybeInitBlobDirectWriteColumnFamily(
|
||||
ColumnFamilyData* cfd, const ColumnFamilyOptions& cf_options,
|
||||
const std::string& column_family_name);
|
||||
// Increments the DB-level count of live blob direct-write column families.
|
||||
void RegisterBlobDirectWriteColumnFamily();
|
||||
// Decrements the DB-level count of live blob direct-write column families.
|
||||
void UnregisterBlobDirectWriteColumnFamily();
|
||||
|
||||
struct CompactionState;
|
||||
struct PrepickedCompaction;
|
||||
struct PurgeFileInfo;
|
||||
@@ -1915,11 +2055,12 @@ class DBImpl : public DB {
|
||||
flush_reason_(FlushReason::kOthers) {}
|
||||
BGFlushArg(ColumnFamilyData* cfd, uint64_t max_memtable_id,
|
||||
SuperVersionContext* superversion_context,
|
||||
FlushReason flush_reason)
|
||||
FlushReason flush_reason, bool atomic_flush)
|
||||
: cfd_(cfd),
|
||||
max_memtable_id_(max_memtable_id),
|
||||
superversion_context_(superversion_context),
|
||||
flush_reason_(flush_reason) {}
|
||||
flush_reason_(flush_reason),
|
||||
atomic_flush_(atomic_flush) {}
|
||||
|
||||
// Column family to flush.
|
||||
ColumnFamilyData* cfd_;
|
||||
@@ -1931,6 +2072,8 @@ class DBImpl : public DB {
|
||||
// requires a SuperVersionContext object (currently embedded in JobContext).
|
||||
SuperVersionContext* superversion_context_;
|
||||
FlushReason flush_reason_;
|
||||
// Whether this flush should use atomic flush code path.
|
||||
bool atomic_flush_ = false;
|
||||
};
|
||||
|
||||
// Argument passed to flush thread.
|
||||
@@ -2050,6 +2193,10 @@ class DBImpl : public DB {
|
||||
void DeleteObsoleteFileImpl(int job_id, const std::string& fname,
|
||||
const std::string& path_to_sync, FileType type,
|
||||
uint64_t number);
|
||||
// Returns true when a blob file must be preserved because it is still
|
||||
// tracked by blob direct write or is still footer-less on disk.
|
||||
bool ShouldKeepBlobFileDuringPurge(uint64_t number,
|
||||
const std::string& blob_file_path);
|
||||
|
||||
// Background process needs to call
|
||||
// auto x = CaptureCurrentFileNumberInPendingOutputs()
|
||||
@@ -2437,6 +2584,8 @@ class DBImpl : public DB {
|
||||
|
||||
struct FlushRequest {
|
||||
FlushReason flush_reason;
|
||||
// Whether this flush request should use the atomic flush code path.
|
||||
bool atomic_flush = false;
|
||||
// A map from column family to flush to largest memtable id to persist for
|
||||
// each column family. Once all the memtables whose IDs are smaller than or
|
||||
// equal to this per-column-family specified value, this flush request is
|
||||
@@ -2595,6 +2744,8 @@ class DBImpl : public DB {
|
||||
|
||||
Status MaybeReleaseTimestampedSnapshotsAndCheck();
|
||||
|
||||
Status MaybeWriteWalMarkersToManifestOnClose();
|
||||
|
||||
Status CloseHelper();
|
||||
|
||||
void WaitForBackgroundWork();
|
||||
@@ -2772,6 +2923,30 @@ class DBImpl : public DB {
|
||||
std::string ts_low);
|
||||
|
||||
bool ShouldReferenceSuperVersion(const MergeContext& merge_context);
|
||||
// Resolves a plain value whose current payload is an encoded direct-write
|
||||
// blob index, either through `value` directly or through the default-column
|
||||
// view layered on `columns`.
|
||||
static Status ResolveDirectWritePlainValue(const ReadOptions& read_options,
|
||||
const Slice& key,
|
||||
const Version* current,
|
||||
ColumnFamilyData* cfd,
|
||||
PinnableSlice* value,
|
||||
PinnableWideColumns* columns);
|
||||
// Resolves each unresolved direct-write blob-valued column in `columns` and
|
||||
// rebuilds the serialized wide-column entity in place.
|
||||
static Status ResolveDirectWriteWideColumns(const ReadOptions& read_options,
|
||||
const Slice& key,
|
||||
const Version* current,
|
||||
ColumnFamilyData* cfd,
|
||||
PinnableWideColumns* columns);
|
||||
// Dispatches between plain-value and wide-column direct-write resolution for
|
||||
// read results observed before flush makes the corresponding blob file
|
||||
// visible through normal Version metadata.
|
||||
static bool MaybeResolveDirectWriteValue(
|
||||
const ReadOptions& read_options, const Slice& key,
|
||||
bool resolve_direct_write_value, const Version* current,
|
||||
ColumnFamilyData* cfd, PinnableSlice* value, PinnableWideColumns* columns,
|
||||
Status* s, bool* is_blob_index, bool* value_found = nullptr);
|
||||
|
||||
template <typename IterType, typename ImplType,
|
||||
typename ErrorIteratorFuncType>
|
||||
@@ -3133,6 +3308,11 @@ class DBImpl : public DB {
|
||||
// Used when disableWAL is true.
|
||||
std::atomic<bool> has_unpersisted_data_{false};
|
||||
|
||||
// Number of live column families with an active blob direct write partition
|
||||
// manager. This keeps the read/write fast paths at O(1) when the feature is
|
||||
// completely disabled for the DB.
|
||||
std::atomic<uint32_t> blob_direct_write_cf_count_{0};
|
||||
|
||||
// if an attempt was made to flush all column families that
|
||||
// the oldest log depends on but uncommitted data in the oldest
|
||||
// log prevents the log from being released.
|
||||
@@ -3361,6 +3541,18 @@ inline Status DBImpl::FailIfTsMismatchCf(ColumnFamilyHandle* column_family,
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
inline Status DBImpl::FailIfTableFilterWithRangeConversion(
|
||||
const ReadOptions& read_options,
|
||||
const MutableCFOptions& mutable_cf_options) const {
|
||||
if (read_options.table_filter &&
|
||||
mutable_cf_options.min_tombstones_for_range_conversion > 0) {
|
||||
return Status::InvalidArgument(
|
||||
"ReadOptions::table_filter is not supported when "
|
||||
"min_tombstones_for_range_conversion > 0");
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
inline Status DBImpl::FailIfReadCollapsedHistory(const ColumnFamilyData* cfd,
|
||||
const SuperVersion* sv,
|
||||
const Slice& ts) const {
|
||||
|
||||
@@ -8,7 +8,9 @@
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
#include <cinttypes>
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/builder.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/error_handler.h"
|
||||
@@ -215,7 +217,8 @@ Status DBImpl::FlushMemTableToOutputFile(
|
||||
&event_logger_, mutable_cf_options.report_bg_io_stats,
|
||||
true /* sync_output_directory */, true /* write_manifest */, thread_pri,
|
||||
io_tracer_, cfd->GetSuperVersion()->ShareSeqnoToTimeMapping(), db_id_,
|
||||
db_session_id_, cfd->GetFullHistoryTsLow(), &blob_callback_);
|
||||
db_session_id_, cfd->GetFullHistoryTsLow(), &blob_callback_,
|
||||
mutable_db_options_.fast_sst_open);
|
||||
FileMetaData file_meta;
|
||||
|
||||
Status s;
|
||||
@@ -279,12 +282,49 @@ Status DBImpl::FlushMemTableToOutputFile(
|
||||
flush_reason);
|
||||
|
||||
bool switched_to_mempurge = false;
|
||||
size_t prepared_blob_generations = 0;
|
||||
std::vector<uint64_t> sealed_blob_numbers;
|
||||
// Within flush_job.Run, rocksdb may call event listener to notify
|
||||
// file creation and deletion.
|
||||
//
|
||||
// Note that flush_job.Run will unlock and lock the db_mutex,
|
||||
// and EventListener callback will be called when the db_mutex
|
||||
// is unlocked by the current thread.
|
||||
if (s.ok()) {
|
||||
auto mgr_handle = cfd->blob_partition_manager_handle();
|
||||
auto* mgr = mgr_handle.get();
|
||||
if (mgr != nullptr) {
|
||||
prepared_blob_generations = flush_job.GetMemTables().size();
|
||||
std::vector<BlobFileAddition> write_path_additions;
|
||||
std::vector<BlobFileGarbage> write_path_garbages;
|
||||
std::vector<std::vector<uint64_t>> generation_blob_file_numbers;
|
||||
mutex_.Unlock();
|
||||
s = mgr->PrepareFlushAdditions(
|
||||
write_options, prepared_blob_generations, &write_path_additions,
|
||||
&write_path_garbages, &generation_blob_file_numbers);
|
||||
mutex_.Lock();
|
||||
if (!s.ok()) {
|
||||
prepared_blob_generations = 0;
|
||||
} else if (!write_path_additions.empty()) {
|
||||
const auto& memtables = flush_job.GetMemTables();
|
||||
assert(generation_blob_file_numbers.size() == memtables.size());
|
||||
for (size_t i = 0; i < generation_blob_file_numbers.size(); ++i) {
|
||||
// Old SuperVersions can keep these memtables alive after the flush
|
||||
// commits, so keep their sealed direct-write blob files protected
|
||||
// from obsolete-file purge until the memtable is finally released.
|
||||
memtables[i]->ProtectSealedBlobFiles(mgr_handle,
|
||||
generation_blob_file_numbers[i]);
|
||||
}
|
||||
for (const auto& addition : write_path_additions) {
|
||||
sealed_blob_numbers.push_back(addition.GetBlobFileNumber());
|
||||
}
|
||||
flush_job.AddExternalBlobFileAdditions(std::move(write_path_additions));
|
||||
flush_job.AddExternalBlobFileGarbages(std::move(write_path_garbages));
|
||||
} else if (!write_path_garbages.empty()) {
|
||||
flush_job.AddExternalBlobFileGarbages(std::move(write_path_garbages));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s.ok()) {
|
||||
s = flush_job.Run(&logs_with_prep_tracker_, &file_meta,
|
||||
&switched_to_mempurge, &skip_set_bg_error,
|
||||
@@ -292,10 +332,33 @@ Status DBImpl::FlushMemTableToOutputFile(
|
||||
need_cancel = false;
|
||||
}
|
||||
|
||||
if (cfd->blob_partition_manager() != nullptr &&
|
||||
prepared_blob_generations > 0) {
|
||||
auto unconsumed_additions = flush_job.TakeExternalBlobFileAdditions();
|
||||
auto unconsumed_garbages = flush_job.TakeExternalBlobFileGarbages();
|
||||
if (!s.ok() || !unconsumed_additions.empty() ||
|
||||
!unconsumed_garbages.empty()) {
|
||||
// Skip committing this attempt, but keep the prepared generations queued
|
||||
// in pending_generations_. The same immutable memtables still reference
|
||||
// these sealed blob files, so a later flush retry must publish the exact
|
||||
// same additions rather than abandoning them here.
|
||||
prepared_blob_generations = 0;
|
||||
sealed_blob_numbers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (!s.ok() && need_cancel) {
|
||||
flush_job.Cancel();
|
||||
}
|
||||
|
||||
if (s.ok() && prepared_blob_generations > 0) {
|
||||
auto* mgr = cfd->blob_partition_manager();
|
||||
mgr->CommitPreparedGenerations(prepared_blob_generations);
|
||||
if (!sealed_blob_numbers.empty()) {
|
||||
mgr->RemoveFilePartitionMappings(sealed_blob_numbers);
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
InstallSuperVersionAndScheduleWork(cfd, superversion_context);
|
||||
if (made_progress) {
|
||||
@@ -388,7 +451,12 @@ Status DBImpl::FlushMemTableToOutputFile(
|
||||
Status DBImpl::FlushMemTablesToOutputFiles(
|
||||
const autovector<BGFlushArg>& bg_flush_args, bool* made_progress,
|
||||
JobContext* job_context, LogBuffer* log_buffer, Env::Priority thread_pri) {
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
#ifndef NDEBUG
|
||||
for (const auto& bg_flush_arg : bg_flush_args) {
|
||||
assert(bg_flush_arg.atomic_flush_ == bg_flush_args[0].atomic_flush_);
|
||||
}
|
||||
#endif /* !NDEBUG */
|
||||
if (bg_flush_args[0].atomic_flush_) {
|
||||
return AtomicFlushMemTablesToOutputFiles(
|
||||
bg_flush_args, made_progress, job_context, log_buffer, thread_pri);
|
||||
}
|
||||
@@ -481,7 +549,8 @@ Status DBImpl::AtomicFlushMemTablesToOutputFiles(
|
||||
false /* sync_output_directory */, false /* write_manifest */,
|
||||
thread_pri, io_tracer_,
|
||||
cfd->GetSuperVersion()->ShareSeqnoToTimeMapping(), db_id_,
|
||||
db_session_id_, cfd->GetFullHistoryTsLow(), &blob_callback_));
|
||||
db_session_id_, cfd->GetFullHistoryTsLow(), &blob_callback_,
|
||||
mutable_db_options_.fast_sst_open));
|
||||
}
|
||||
|
||||
std::vector<FileMetaData> file_meta(num_cfs);
|
||||
@@ -530,11 +599,13 @@ Status DBImpl::AtomicFlushMemTablesToOutputFiles(
|
||||
// <bool /* executed */, Status /* status code */>
|
||||
autovector<std::pair<bool, Status>> exec_status;
|
||||
std::vector<bool> pick_status;
|
||||
std::vector<size_t> prepared_blob_generations(num_cfs, 0);
|
||||
for (int i = 0; i != num_cfs; ++i) {
|
||||
// Initially all jobs are not executed, with status OK.
|
||||
exec_status.emplace_back(false, Status::OK());
|
||||
pick_status.push_back(false);
|
||||
}
|
||||
std::unordered_map<int, std::vector<uint64_t>> sealed_blob_numbers_by_cf;
|
||||
|
||||
bool flush_for_recovery =
|
||||
bg_flush_args[0].flush_reason_ == FlushReason::kErrorRecovery ||
|
||||
@@ -558,6 +629,46 @@ Status DBImpl::AtomicFlushMemTablesToOutputFiles(
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
for (int i = 0; i != num_cfs; ++i) {
|
||||
auto mgr_handle = cfds[i]->blob_partition_manager_handle();
|
||||
auto* mgr = mgr_handle.get();
|
||||
if (mgr == nullptr) {
|
||||
continue;
|
||||
}
|
||||
|
||||
prepared_blob_generations[i] = jobs[i]->GetMemTables().size();
|
||||
std::vector<BlobFileAddition> write_path_additions;
|
||||
std::vector<BlobFileGarbage> write_path_garbages;
|
||||
std::vector<std::vector<uint64_t>> generation_blob_file_numbers;
|
||||
mutex_.Unlock();
|
||||
s = mgr->PrepareFlushAdditions(
|
||||
write_options, prepared_blob_generations[i], &write_path_additions,
|
||||
&write_path_garbages, &generation_blob_file_numbers);
|
||||
mutex_.Lock();
|
||||
if (!s.ok()) {
|
||||
prepared_blob_generations[i] = 0;
|
||||
break;
|
||||
}
|
||||
if (!write_path_additions.empty()) {
|
||||
const auto& memtables = jobs[i]->GetMemTables();
|
||||
assert(generation_blob_file_numbers.size() == memtables.size());
|
||||
for (size_t j = 0; j < generation_blob_file_numbers.size(); ++j) {
|
||||
memtables[j]->ProtectSealedBlobFiles(mgr_handle,
|
||||
generation_blob_file_numbers[j]);
|
||||
}
|
||||
auto& sealed_blob_numbers = sealed_blob_numbers_by_cf[i];
|
||||
for (const auto& addition : write_path_additions) {
|
||||
sealed_blob_numbers.push_back(addition.GetBlobFileNumber());
|
||||
}
|
||||
jobs[i]->AddExternalBlobFileAdditions(std::move(write_path_additions));
|
||||
}
|
||||
if (!write_path_garbages.empty()) {
|
||||
jobs[i]->AddExternalBlobFileGarbages(std::move(write_path_garbages));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
assert(switched_to_mempurge.size() ==
|
||||
static_cast<long unsigned int>(num_cfs));
|
||||
@@ -763,6 +874,30 @@ Status DBImpl::AtomicFlushMemTablesToOutputFiles(
|
||||
directories_.GetDbDir(), log_buffer);
|
||||
}
|
||||
|
||||
for (int i = 0; i != num_cfs; ++i) {
|
||||
auto* mgr = cfds[i]->blob_partition_manager();
|
||||
if (mgr == nullptr || prepared_blob_generations[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto unconsumed_additions = jobs[i]->TakeExternalBlobFileAdditions();
|
||||
auto unconsumed_garbages = jobs[i]->TakeExternalBlobFileGarbages();
|
||||
if (!s.ok() || !unconsumed_additions.empty() ||
|
||||
!unconsumed_garbages.empty()) {
|
||||
// Same retry semantics as the single-CF flush path above: do not
|
||||
// consume these prepared generations yet, because the rolled-back
|
||||
// memtables still need to reuse the same sealed blob files on retry.
|
||||
prepared_blob_generations[i] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
mgr->CommitPreparedGenerations(prepared_blob_generations[i]);
|
||||
auto it = sealed_blob_numbers_by_cf.find(i);
|
||||
if (it != sealed_blob_numbers_by_cf.end() && !it->second.empty()) {
|
||||
mgr->RemoveFilePartitionMappings(it->second);
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
assert(num_cfs ==
|
||||
static_cast<int>(job_context->superversion_contexts.size()));
|
||||
@@ -2037,7 +2172,7 @@ Status DBImpl::FlushAllColumnFamilies(const FlushOptions& flush_options,
|
||||
FlushReason flush_reason) {
|
||||
mutex_.AssertHeld();
|
||||
Status status;
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
if (immutable_db_options_.atomic_flush || flush_options.force_atomic_flush) {
|
||||
mutex_.Unlock();
|
||||
status = AtomicFlushMemTables(flush_options, flush_reason);
|
||||
if (status.IsColumnFamilyDropped()) {
|
||||
@@ -2070,7 +2205,7 @@ Status DBImpl::Flush(const FlushOptions& flush_options,
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log, "[%s] Manual flush start.",
|
||||
cfh->GetName().c_str());
|
||||
Status s;
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
if (immutable_db_options_.atomic_flush || flush_options.force_atomic_flush) {
|
||||
s = AtomicFlushMemTables(flush_options, FlushReason::kManualFlush,
|
||||
{cfh->cfd()});
|
||||
} else {
|
||||
@@ -2086,7 +2221,8 @@ Status DBImpl::Flush(const FlushOptions& flush_options,
|
||||
Status DBImpl::Flush(const FlushOptions& flush_options,
|
||||
const std::vector<ColumnFamilyHandle*>& column_families) {
|
||||
Status s;
|
||||
if (!immutable_db_options_.atomic_flush) {
|
||||
if (!immutable_db_options_.atomic_flush &&
|
||||
!flush_options.force_atomic_flush) {
|
||||
for (auto cfh : column_families) {
|
||||
s = Flush(flush_options, cfh);
|
||||
if (!s.ok()) {
|
||||
@@ -2381,7 +2517,7 @@ void DBImpl::GenerateFlushRequest(const autovector<ColumnFamilyData*>& cfds,
|
||||
continue;
|
||||
}
|
||||
uint64_t max_memtable_id = cfd->imm()->GetLatestMemTableID(
|
||||
immutable_db_options_.atomic_flush /* for_atomic_flush */);
|
||||
req->atomic_flush /* for_atomic_flush */);
|
||||
req->cfd_to_max_mem_id_to_persist.emplace(cfd, max_memtable_id);
|
||||
}
|
||||
}
|
||||
@@ -2451,7 +2587,8 @@ Status DBImpl::FlushMemTable(ColumnFamilyData* cfd,
|
||||
if (cfd->imm()->NumNotFlushed() != 0 || !cfd->mem()->IsEmpty() ||
|
||||
!cached_recoverable_state_empty_.load() ||
|
||||
IsRecoveryFlush(flush_reason)) {
|
||||
FlushRequest req{flush_reason, {{cfd, flush_memtable_id}}};
|
||||
FlushRequest req{
|
||||
flush_reason, false /* atomic_flush */, {{cfd, flush_memtable_id}}};
|
||||
flush_reqs.emplace_back(std::move(req));
|
||||
memtable_ids_to_wait.emplace_back(
|
||||
cfd->imm()->GetLatestMemTableID(false /* for_atomic_flush */));
|
||||
@@ -2479,7 +2616,9 @@ Status DBImpl::FlushMemTable(ColumnFamilyData* cfd,
|
||||
"to avoid holding old logs",
|
||||
cfd->GetName().c_str());
|
||||
s = SwitchMemtable(cfd_stats, &context);
|
||||
FlushRequest req{flush_reason, {{cfd_stats, flush_memtable_id}}};
|
||||
FlushRequest req{flush_reason,
|
||||
false /* atomic_flush */,
|
||||
{{cfd_stats, flush_memtable_id}}};
|
||||
flush_reqs.emplace_back(std::move(req));
|
||||
memtable_ids_to_wait.emplace_back(
|
||||
cfd_stats->imm()->GetLatestMemTableID(
|
||||
@@ -2558,7 +2697,6 @@ Status DBImpl::AtomicFlushMemTables(
|
||||
const FlushOptions& flush_options, FlushReason flush_reason,
|
||||
const autovector<ColumnFamilyData*>& provided_candidate_cfds,
|
||||
bool entered_write_thread) {
|
||||
assert(immutable_db_options_.atomic_flush);
|
||||
if (!flush_options.wait && write_controller_.IsStopped()) {
|
||||
std::ostringstream oss;
|
||||
oss << "Writes have been stopped, thus unable to perform manual flush. "
|
||||
@@ -2613,6 +2751,7 @@ Status DBImpl::AtomicFlushMemTables(
|
||||
}
|
||||
const bool needs_to_join_write_thread = !entered_write_thread;
|
||||
FlushRequest flush_req;
|
||||
flush_req.atomic_flush = true;
|
||||
autovector<ColumnFamilyData*> cfds;
|
||||
{
|
||||
WriteContext context;
|
||||
@@ -2720,6 +2859,7 @@ Status DBImpl::RetryFlushesForErrorRecovery(FlushReason flush_reason,
|
||||
autovector<uint64_t> flush_memtable_ids;
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
FlushRequest flush_req;
|
||||
flush_req.atomic_flush = true;
|
||||
GenerateFlushRequest(cfds, flush_reason, &flush_req);
|
||||
EnqueuePendingFlush(flush_req);
|
||||
for (auto& iter : flush_req.cfd_to_max_mem_id_to_persist) {
|
||||
@@ -2733,6 +2873,7 @@ Status DBImpl::RetryFlushesForErrorRecovery(FlushReason flush_reason,
|
||||
// reason to do so outside of atomic flush.
|
||||
FlushRequest flush_req{
|
||||
flush_reason,
|
||||
false /* atomic_flush */,
|
||||
{{cfd,
|
||||
std::numeric_limits<uint64_t>::max() /* max_mem_id_to_persist */}}};
|
||||
if (EnqueuePendingFlush(flush_req)) {
|
||||
@@ -3021,7 +3162,7 @@ void DBImpl::ResumeAllCompactions() {
|
||||
void DBImpl::MaybeScheduleFlushOrCompaction() {
|
||||
mutex_.AssertHeld();
|
||||
TEST_SYNC_POINT("DBImpl::MaybeScheduleFlushOrCompaction:Start");
|
||||
if (!opened_successfully_) {
|
||||
if (!opened_successfully_ || read_only_) {
|
||||
// Compaction may introduce data race to DB open
|
||||
return;
|
||||
}
|
||||
@@ -3253,11 +3394,11 @@ DBImpl::FlushRequest DBImpl::PopFirstFromFlushQueue() {
|
||||
assert(!flush_queue_.empty());
|
||||
FlushRequest flush_req = std::move(flush_queue_.front());
|
||||
flush_queue_.pop_front();
|
||||
if (!immutable_db_options_.atomic_flush) {
|
||||
if (!flush_req.atomic_flush) {
|
||||
assert(flush_req.cfd_to_max_mem_id_to_persist.size() == 1);
|
||||
}
|
||||
for (const auto& elem : flush_req.cfd_to_max_mem_id_to_persist) {
|
||||
if (!immutable_db_options_.atomic_flush) {
|
||||
if (!flush_req.atomic_flush) {
|
||||
ColumnFamilyData* cfd = elem.first;
|
||||
assert(cfd);
|
||||
assert(cfd->queued_for_flush());
|
||||
@@ -3302,7 +3443,7 @@ bool DBImpl::EnqueuePendingFlush(const FlushRequest& flush_req) {
|
||||
if (flush_req.cfd_to_max_mem_id_to_persist.empty()) {
|
||||
return enqueued;
|
||||
}
|
||||
if (!immutable_db_options_.atomic_flush) {
|
||||
if (!flush_req.atomic_flush) {
|
||||
// For the non-atomic flush case, we never schedule multiple column
|
||||
// families in the same flush request.
|
||||
assert(flush_req.cfd_to_max_mem_id_to_persist.size() == 1);
|
||||
@@ -3318,6 +3459,11 @@ bool DBImpl::EnqueuePendingFlush(const FlushRequest& flush_req) {
|
||||
enqueued = true;
|
||||
}
|
||||
} else {
|
||||
// Atomic flush requests bypass the queued_for_flush() deduplication guard.
|
||||
// This means an atomic request and a non-atomic request for the same CF
|
||||
// can coexist in the flush queue. This is safe because PickMemtablesToFlush
|
||||
// uses flush_in_progress_ to prevent double-picking of memtables, and
|
||||
// BackgroundFlush filters out CFs where !IsFlushPending().
|
||||
for (auto& iter : flush_req.cfd_to_max_mem_id_to_persist) {
|
||||
ColumnFamilyData* cfd = iter.first;
|
||||
cfd->Ref();
|
||||
@@ -3481,7 +3627,7 @@ Status DBImpl::BackgroundFlush(bool* made_progress, JobContext* job_context,
|
||||
}
|
||||
return status;
|
||||
}
|
||||
if (!immutable_db_options_.atomic_flush &&
|
||||
if (!flush_req.atomic_flush &&
|
||||
ShouldRescheduleFlushRequestToRetainUDT(flush_req)) {
|
||||
assert(flush_req.cfd_to_max_mem_id_to_persist.size() == 1);
|
||||
ColumnFamilyData* cfd =
|
||||
@@ -3530,7 +3676,8 @@ Status DBImpl::BackgroundFlush(bool* made_progress, JobContext* job_context,
|
||||
}
|
||||
superversion_contexts.emplace_back(true);
|
||||
bg_flush_args.emplace_back(cfd, max_memtable_id,
|
||||
&(superversion_contexts.back()), flush_reason);
|
||||
&(superversion_contexts.back()), flush_reason,
|
||||
flush_req.atomic_flush);
|
||||
}
|
||||
// `MaybeScheduleFlushOrCompaction` schedules as many `BackgroundCallFlush`
|
||||
// jobs as the number of `FlushRequest` in the `flush_queue_`, a.k.a
|
||||
@@ -5008,6 +5155,10 @@ void DBImpl::InitSnapshotContext(JobContext* job_context) {
|
||||
Status DBImpl::WaitForCompact(
|
||||
const WaitForCompactOptions& wait_for_compact_options) {
|
||||
InstrumentedMutexLock l(&mutex_);
|
||||
// `close_db=true` needs the same direct-write cleanup guarantee as DB close:
|
||||
// register live blob files before relying on the DB being reopenable.
|
||||
const bool flush_for_blob_direct_write =
|
||||
HasAnyBlobDirectWriteColumnFamilyWithLockHeld();
|
||||
if (wait_for_compact_options.flush) {
|
||||
Status s = DBImpl::FlushAllColumnFamilies(FlushOptions(),
|
||||
FlushReason::kManualFlush);
|
||||
@@ -5015,8 +5166,9 @@ Status DBImpl::WaitForCompact(
|
||||
return s;
|
||||
}
|
||||
} else if (wait_for_compact_options.close_db &&
|
||||
has_unpersisted_data_.load(std::memory_order_relaxed) &&
|
||||
!mutable_db_options_.avoid_flush_during_shutdown) {
|
||||
(flush_for_blob_direct_write ||
|
||||
(has_unpersisted_data_.load(std::memory_order_relaxed) &&
|
||||
!mutable_db_options_.avoid_flush_during_shutdown))) {
|
||||
Status s =
|
||||
DBImpl::FlushAllColumnFamilies(FlushOptions(), FlushReason::kShutDown);
|
||||
if (!s.ok()) {
|
||||
|
||||
@@ -8,14 +8,17 @@
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
#ifndef NDEBUG
|
||||
#include <iostream>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "db/blob/blob_file_cache.h"
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/column_family.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/error_handler.h"
|
||||
#include "db/periodic_task_scheduler.h"
|
||||
#include "monitoring/thread_status_updater.h"
|
||||
#include "util/cast_util.h"
|
||||
#include "util/hash_containers.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
uint64_t DBImpl::TEST_GetLevel0TotalSize() {
|
||||
@@ -357,11 +360,15 @@ void DBImpl::TEST_VerifyNoObsoleteFilesCached(
|
||||
}
|
||||
|
||||
// Live and "quarantined" files are allowed to be open in table cache
|
||||
std::set<uint64_t> live_and_quar_files;
|
||||
UnorderedSet<uint64_t> live_and_quar_files;
|
||||
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
||||
if (cfd->IsDropped()) {
|
||||
continue;
|
||||
}
|
||||
if (auto* mgr = cfd->blob_partition_manager(); mgr != nullptr) {
|
||||
mgr->GetActiveBlobFileNumbers(&live_and_quar_files);
|
||||
mgr->GetProtectedBlobFileNumbers(&live_and_quar_files);
|
||||
}
|
||||
// Iterate over live versions
|
||||
Version* current = cfd->current();
|
||||
Version* ver = current;
|
||||
|
||||
+164
-14
@@ -6,10 +6,13 @@
|
||||
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
#include <array>
|
||||
#include <cinttypes>
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_log_format.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/event_helpers.h"
|
||||
#include "db/memtable_list.h"
|
||||
@@ -17,6 +20,7 @@
|
||||
#include "file/filename.h"
|
||||
#include "file/sst_file_manager_impl.h"
|
||||
#include "logging/logging.h"
|
||||
#include "monitoring/thread_status_util.h"
|
||||
#include "port/port.h"
|
||||
#include "rocksdb/options.h"
|
||||
#include "util/autovector.h"
|
||||
@@ -24,6 +28,91 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
namespace {
|
||||
|
||||
Env::IOActivity GetCurrentThreadIOActivityForMetadataRead() {
|
||||
switch (ThreadStatusUtil::GetThreadOperation()) {
|
||||
case ThreadStatus::OperationType::OP_FLUSH:
|
||||
return Env::IOActivity::kFlush;
|
||||
case ThreadStatus::OperationType::OP_COMPACTION:
|
||||
return Env::IOActivity::kCompaction;
|
||||
case ThreadStatus::OperationType::OP_DBOPEN:
|
||||
return Env::IOActivity::kDBOpen;
|
||||
case ThreadStatus::OperationType::OP_GET:
|
||||
return Env::IOActivity::kGet;
|
||||
case ThreadStatus::OperationType::OP_MULTIGET:
|
||||
return Env::IOActivity::kMultiGet;
|
||||
case ThreadStatus::OperationType::OP_DBITERATOR:
|
||||
return Env::IOActivity::kDBIterator;
|
||||
case ThreadStatus::OperationType::OP_VERIFY_DB_CHECKSUM:
|
||||
return Env::IOActivity::kVerifyDBChecksum;
|
||||
case ThreadStatus::OperationType::OP_VERIFY_FILE_CHECKSUMS:
|
||||
return Env::IOActivity::kVerifyFileChecksums;
|
||||
case ThreadStatus::OperationType::OP_GETENTITY:
|
||||
return Env::IOActivity::kGetEntity;
|
||||
case ThreadStatus::OperationType::OP_MULTIGETENTITY:
|
||||
return Env::IOActivity::kMultiGetEntity;
|
||||
case ThreadStatus::OperationType::
|
||||
OP_GET_FILE_CHECKSUMS_FROM_CURRENT_MANIFEST:
|
||||
return Env::IOActivity::kGetFileChecksumsFromCurrentManifest;
|
||||
default:
|
||||
return Env::IOActivity::kUnknown;
|
||||
}
|
||||
}
|
||||
|
||||
// A full-scan obsolete-file purge can observe a newly created direct-write
|
||||
// blob file before it is added to the manager's active-file set. Those
|
||||
// in-flight files are still missing their footer, so conservatively keep any
|
||||
// footer-less blob file here rather than risk deleting a live write-path file.
|
||||
bool ShouldKeepFooterlessBlobFile(FileSystem* fs,
|
||||
const FileOptions& file_options,
|
||||
const std::string& blob_file_path) {
|
||||
assert(fs != nullptr);
|
||||
|
||||
constexpr IODebugContext* dbg = nullptr;
|
||||
// This purge path can run from DB open, flush/compaction cleanup, or
|
||||
// iterator-triggered obsolete-file cleanup. Tag the probe with the current
|
||||
// thread activity so db_stress keeps validating the read against the active
|
||||
// operation instead of seeing an unexpected kUnknown metadata read.
|
||||
IOOptions io_options;
|
||||
io_options.io_activity = GetCurrentThreadIOActivityForMetadataRead();
|
||||
uint64_t file_size = 0;
|
||||
IOStatus io_s = fs->GetFileSize(blob_file_path, io_options, &file_size, dbg);
|
||||
if (!io_s.ok()) {
|
||||
return !io_s.IsPathNotFound();
|
||||
}
|
||||
|
||||
if (file_size < BlobLogHeader::kSize + BlobLogFooter::kSize) {
|
||||
return true;
|
||||
}
|
||||
|
||||
FileOptions read_file_options = file_options;
|
||||
read_file_options.use_direct_reads = false;
|
||||
|
||||
std::unique_ptr<FSRandomAccessFile> file;
|
||||
io_s = fs->NewRandomAccessFile(blob_file_path, read_file_options, &file, dbg);
|
||||
if (!io_s.ok()) {
|
||||
return !io_s.IsPathNotFound();
|
||||
}
|
||||
|
||||
std::array<char, BlobLogFooter::kSize> scratch{};
|
||||
Slice footer_slice;
|
||||
io_s = file->Read(file_size - BlobLogFooter::kSize, BlobLogFooter::kSize,
|
||||
io_options, &footer_slice, scratch.data(), dbg);
|
||||
if (!io_s.ok()) {
|
||||
return !io_s.IsPathNotFound();
|
||||
}
|
||||
|
||||
if (footer_slice.size() != BlobLogFooter::kSize) {
|
||||
return true;
|
||||
}
|
||||
|
||||
BlobLogFooter footer;
|
||||
return !footer.DecodeFrom(footer_slice).ok();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
uint64_t DBImpl::MinLogNumberToKeep() {
|
||||
return versions_->min_log_number_to_keep();
|
||||
}
|
||||
@@ -156,6 +245,17 @@ void DBImpl::FindObsoleteFiles(JobContext* job_context, bool force,
|
||||
job_context->min_pending_output = MinObsoleteSstNumberToKeep();
|
||||
job_context->files_to_quarantine = error_handler_.GetFilesToQuarantine();
|
||||
job_context->min_options_file_number = MinOptionsFileNumberToKeep();
|
||||
job_context->min_blob_file_number_to_keep =
|
||||
versions_->current_next_file_number();
|
||||
for (auto* cfd : *versions_->GetColumnFamilySet()) {
|
||||
auto* mgr = cfd->blob_partition_manager();
|
||||
if (mgr != nullptr) {
|
||||
mgr->GetActiveBlobFileNumbers(
|
||||
&job_context->active_blob_direct_write_files);
|
||||
mgr->GetProtectedBlobFileNumbers(
|
||||
&job_context->active_blob_direct_write_files);
|
||||
}
|
||||
}
|
||||
|
||||
// Get obsolete files. This function will also update the list of
|
||||
// pending files in VersionSet().
|
||||
@@ -587,13 +687,24 @@ void DBImpl::PurgeObsoleteFiles(JobContext& state, bool schedule_only) {
|
||||
files_to_del.insert(number);
|
||||
}
|
||||
break;
|
||||
case kBlobFile:
|
||||
case kBlobFile: {
|
||||
keep = number >= state.min_pending_output ||
|
||||
(blob_live_set.find(number) != blob_live_set.end());
|
||||
number >= state.min_blob_file_number_to_keep ||
|
||||
(blob_live_set.find(number) != blob_live_set.end()) ||
|
||||
(state.active_blob_direct_write_files.find(number) !=
|
||||
state.active_blob_direct_write_files.end());
|
||||
if (!keep) {
|
||||
const std::string blob_file_path =
|
||||
BlobFileName(candidate_file.file_path, number);
|
||||
if (ShouldKeepBlobFileDuringPurge(number, blob_file_path)) {
|
||||
keep = true;
|
||||
break;
|
||||
}
|
||||
TableCache::Evict(table_cache_.get(), number);
|
||||
files_to_del.insert(number);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case kTempFile:
|
||||
// Any temp files that are currently being written to must
|
||||
// be recorded in pending_outputs_, which is inserted into "live".
|
||||
@@ -750,6 +861,21 @@ void DBImpl::DeleteObsoleteFiles() {
|
||||
mutex_.Lock();
|
||||
}
|
||||
|
||||
bool DBImpl::ShouldKeepBlobFileDuringPurge(uint64_t number,
|
||||
const std::string& blob_file_path) {
|
||||
{
|
||||
InstrumentedMutexLock lock(&mutex_);
|
||||
for (auto* cfd : *versions_->GetColumnFamilySet()) {
|
||||
auto* mgr = cfd->blob_partition_manager();
|
||||
if (mgr != nullptr && mgr->IsTrackedBlobFileNumber(number)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ShouldKeepFooterlessBlobFile(fs_.get(), file_options_, blob_file_path);
|
||||
}
|
||||
|
||||
VersionEdit GetDBRecoveryEditForObsoletingMemTables(
|
||||
VersionSet* vset, const ColumnFamilyData& cfd,
|
||||
const autovector<VersionEdit*>& edit_list,
|
||||
@@ -1060,8 +1186,9 @@ std::set<std::string> DBImpl::CollectAllDBPaths() {
|
||||
|
||||
Status DBImpl::MaybeUpdateNextFileNumber(RecoveryContext* recovery_ctx) {
|
||||
mutex_.AssertHeld();
|
||||
uint64_t next_file_number = versions_->current_next_file_number();
|
||||
uint64_t largest_file_number = next_file_number;
|
||||
const uint64_t prev_next_file_number = versions_->current_next_file_number();
|
||||
uint64_t largest_file_number = prev_next_file_number;
|
||||
bool on_disk_file_advanced_counter = false;
|
||||
Status s;
|
||||
for (const auto& path : CollectAllDBPaths()) {
|
||||
std::vector<std::string> files;
|
||||
@@ -1075,7 +1202,15 @@ Status DBImpl::MaybeUpdateNextFileNumber(RecoveryContext* recovery_ctx) {
|
||||
if (!ParseFileName(fname, &number, &type)) {
|
||||
continue;
|
||||
}
|
||||
const std::string normalized_fpath = path + kFilePathSeparator + fname;
|
||||
std::string normalized_fpath = path;
|
||||
normalized_fpath += kFilePathSeparator;
|
||||
normalized_fpath.append(fname);
|
||||
// Use >= so a crashed-mid-allocation file at exactly
|
||||
// prev_next_file_number triggers the advance — otherwise the next
|
||||
// NewFileNumber() would collide with it.
|
||||
if (number >= prev_next_file_number) {
|
||||
on_disk_file_advanced_counter = true;
|
||||
}
|
||||
largest_file_number = std::max(largest_file_number, number);
|
||||
if ((type == kTableFile || type == kBlobFile)) {
|
||||
recovery_ctx->existing_data_files_.push_back(normalized_fpath);
|
||||
@@ -1086,16 +1221,31 @@ Status DBImpl::MaybeUpdateNextFileNumber(RecoveryContext* recovery_ctx) {
|
||||
return s;
|
||||
}
|
||||
|
||||
if (largest_file_number >= next_file_number) {
|
||||
versions_->next_file_number_.store(largest_file_number + 1);
|
||||
}
|
||||
// Legacy bug: initializing largest_file_number from prev_next_file_number
|
||||
// made the post-loop check trivially true and emitted a noop SetNextFile
|
||||
// on every recovery.
|
||||
// Preserved verbatim by default (some tests pin file numbers); skipped
|
||||
// only when the option is on AND we're not in salvage mode (which
|
||||
// requires the MANIFEST rewrite this emit triggers).
|
||||
const bool optimize_manifest_for_recovery =
|
||||
mutable_db_options_.optimize_manifest_for_recovery &&
|
||||
!immutable_db_options_.best_efforts_recovery;
|
||||
|
||||
VersionEdit edit;
|
||||
edit.SetNextFile(versions_->next_file_number_.load());
|
||||
assert(versions_->GetColumnFamilySet());
|
||||
ColumnFamilyData* default_cfd = versions_->GetColumnFamilySet()->GetDefault();
|
||||
assert(default_cfd);
|
||||
recovery_ctx->UpdateVersionEdits(default_cfd, edit);
|
||||
if (!optimize_manifest_for_recovery || on_disk_file_advanced_counter) {
|
||||
if (largest_file_number >= prev_next_file_number) {
|
||||
versions_->next_file_number_.store(largest_file_number + 1);
|
||||
}
|
||||
TEST_SYNC_POINT("DBImpl::MaybeUpdateNextFileNumber:EmitEdit");
|
||||
VersionEdit edit;
|
||||
edit.SetNextFile(versions_->next_file_number_.load());
|
||||
assert(versions_->GetColumnFamilySet());
|
||||
ColumnFamilyData* default_cfd =
|
||||
versions_->GetColumnFamilySet()->GetDefault();
|
||||
assert(default_cfd);
|
||||
recovery_ctx->UpdateVersionEdits(default_cfd, edit);
|
||||
} else {
|
||||
TEST_SYNC_POINT("DBImpl::Recovery:SkippedNoopEdit:NextFileNumber");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
+60
-11
@@ -8,6 +8,7 @@
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
#include <cinttypes>
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/builder.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/error_handler.h"
|
||||
@@ -679,8 +680,18 @@ Status DBImpl::Recover(
|
||||
} else if (immutable_db_options_.write_dbid_to_manifest && recovery_ctx) {
|
||||
VersionEdit edit;
|
||||
s = SetupDBId(write_options, read_only, is_new_db, is_retry, &edit);
|
||||
recovery_ctx->UpdateVersionEdits(
|
||||
versions_->GetColumnFamilySet()->GetDefault(), edit);
|
||||
// best_efforts_recovery rebuilds CURRENT/MANIFEST as the side-effect
|
||||
// of LogAndApplyForRecovery emitting an edit, so the optimization is
|
||||
// disabled there (see optimize_manifest_for_recovery doc).
|
||||
const bool optimize_manifest_for_recovery =
|
||||
mutable_db_options_.optimize_manifest_for_recovery &&
|
||||
!immutable_db_options_.best_efforts_recovery;
|
||||
if (!optimize_manifest_for_recovery || edit.HasDbId()) {
|
||||
recovery_ctx->UpdateVersionEdits(
|
||||
versions_->GetColumnFamilySet()->GetDefault(), edit);
|
||||
} else {
|
||||
TEST_SYNC_POINT("DBImpl::Recovery:SkippedNoopEdit:SetupDBId");
|
||||
}
|
||||
} else {
|
||||
s = SetupDBId(write_options, read_only, is_new_db, is_retry, nullptr);
|
||||
}
|
||||
@@ -769,6 +780,10 @@ Status DBImpl::Recover(
|
||||
// otherwise, in the future, if WAL tracking is enabled again,
|
||||
// since the WALs deleted when WAL tracking is disabled are not persisted
|
||||
// into MANIFEST, WAL check may fail.
|
||||
//
|
||||
// Intentionally NOT gated by optimize_manifest_for_recovery:
|
||||
// this edit is safety-critical for a future re-enable of WAL tracking,
|
||||
// even though it can look noop-ish under the current session.
|
||||
VersionEdit edit;
|
||||
WalNumber max_wal_number =
|
||||
versions_->GetWalSet().GetWals().rbegin()->first;
|
||||
@@ -982,7 +997,11 @@ Status DBImpl::InitPersistStatsColumnFamily() {
|
||||
|
||||
Status DBImpl::LogAndApplyForRecovery(const RecoveryContext& recovery_ctx) {
|
||||
mutex_.AssertHeld();
|
||||
assert(versions_->descriptor_log_ == nullptr);
|
||||
// descriptor_log_ is normally null after Recover, but when
|
||||
// reuse_manifest_on_open is set VersionSet::Recover may have already
|
||||
// bound a log::Writer to the existing MANIFEST for append.
|
||||
assert(versions_->descriptor_log_ == nullptr ||
|
||||
immutable_db_options_.reuse_manifest_on_open);
|
||||
const ReadOptions read_options(Env::IOActivity::kDBOpen);
|
||||
const WriteOptions write_options(Env::IOActivity::kDBOpen);
|
||||
|
||||
@@ -1869,25 +1888,47 @@ Status DBImpl::MaybeFlushFinalMemtableOrRestoreActiveLogFiles(
|
||||
versions_->MarkFileNumberUsed(max_wal_number + 1);
|
||||
assert(recovery_ctx != nullptr);
|
||||
|
||||
const bool optimize_manifest_for_recovery =
|
||||
mutable_db_options_.optimize_manifest_for_recovery &&
|
||||
!immutable_db_options_.best_efforts_recovery;
|
||||
for (auto* cfd : *versions_->GetColumnFamilySet()) {
|
||||
auto iter = version_edits->find(cfd->GetID());
|
||||
assert(iter != version_edits->end());
|
||||
recovery_ctx->UpdateVersionEdits(cfd, iter->second);
|
||||
const VersionEdit& cf_edit = iter->second;
|
||||
if (!optimize_manifest_for_recovery ||
|
||||
cf_edit.ShouldEmitPerColumnFamilyRecoveryEdit(
|
||||
cfd->GetLogNumber())) {
|
||||
recovery_ctx->UpdateVersionEdits(cfd, cf_edit);
|
||||
} else {
|
||||
TEST_SYNC_POINT("DBImpl::Recovery:SkippedNoopEdit:PerCF");
|
||||
}
|
||||
}
|
||||
|
||||
if (flushed || !data_seen) {
|
||||
const uint64_t new_min_log = max_wal_number + 1;
|
||||
VersionEdit wal_deletion;
|
||||
bool emit_wal_deletion = false;
|
||||
if (immutable_db_options_.track_and_verify_wals_in_manifest) {
|
||||
wal_deletion.DeleteWalsBefore(max_wal_number + 1);
|
||||
// Determining whether DeleteWalsBefore actually shrinks WalSet
|
||||
// membership requires WalSet state outside this site, so emit
|
||||
// unconditionally (pre-existing behavior).
|
||||
wal_deletion.DeleteWalsBefore(new_min_log);
|
||||
emit_wal_deletion = true;
|
||||
}
|
||||
if (!allow_2pc()) {
|
||||
// In non-2pc mode, flushing the memtables of the column families
|
||||
// means we can advance min_log_number_to_keep.
|
||||
wal_deletion.SetMinLogNumberToKeep(max_wal_number + 1);
|
||||
const bool min_log_advances =
|
||||
new_min_log > versions_->min_log_number_to_keep();
|
||||
if (!allow_2pc() &&
|
||||
(!optimize_manifest_for_recovery || min_log_advances)) {
|
||||
wal_deletion.SetMinLogNumberToKeep(new_min_log);
|
||||
emit_wal_deletion = true;
|
||||
}
|
||||
assert(versions_->GetColumnFamilySet() != nullptr);
|
||||
recovery_ctx->UpdateVersionEdits(
|
||||
versions_->GetColumnFamilySet()->GetDefault(), wal_deletion);
|
||||
if (!optimize_manifest_for_recovery || emit_wal_deletion) {
|
||||
recovery_ctx->UpdateVersionEdits(
|
||||
versions_->GetColumnFamilySet()->GetDefault(), wal_deletion);
|
||||
} else {
|
||||
TEST_SYNC_POINT("DBImpl::Recovery:SkippedNoopEdit:WalDeletion");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2606,6 +2647,14 @@ Status DBImpl::Open(const DBOptions& db_options, const std::string& dbname,
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
for (size_t i = 0; i < column_families.size(); ++i) {
|
||||
const auto& cf = column_families[i];
|
||||
auto* cfd = static_cast<ColumnFamilyHandleImpl*>((*handles)[i])->cfd();
|
||||
impl->MaybeInitBlobDirectWriteColumnFamily(cfd, cf.options, cf.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok() && impl->immutable_db_options_.persist_stats_to_disk) {
|
||||
// Install SuperVersion for hidden column family
|
||||
assert(impl->persist_stats_cf_handle_);
|
||||
|
||||
@@ -186,7 +186,7 @@ class DBImplReadOnly : public DBImpl {
|
||||
// FIXME: some missing overrides for more "write" functions
|
||||
|
||||
protected:
|
||||
Status FlushForGetLiveFiles() override {
|
||||
Status FlushForGetLiveFiles(bool /*force_atomic_flush*/) override {
|
||||
// No-op for read-only DB
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ class DBImplSecondary : public DBImpl {
|
||||
#endif // NDEBUG
|
||||
|
||||
protected:
|
||||
Status FlushForGetLiveFiles() override {
|
||||
Status FlushForGetLiveFiles(bool /*force_atomic_flush*/) override {
|
||||
// No-op for read-only DB
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
+664
-47
@@ -7,18 +7,81 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
||||
#include <cinttypes>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_write_batch_transformer.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/error_handler.h"
|
||||
#include "db/event_helpers.h"
|
||||
#include "db/wide/wide_columns_helper.h"
|
||||
#include "logging/logging.h"
|
||||
#include "memtable/wbwi_memtable.h"
|
||||
#include "monitoring/perf_context_imp.h"
|
||||
#include "options/options_helper.h"
|
||||
#include "test_util/sync_point.h"
|
||||
#include "util/cast_util.h"
|
||||
#include "util/hash_containers.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
namespace {
|
||||
|
||||
class BlobWriteRollbackGuard {
|
||||
public:
|
||||
explicit BlobWriteRollbackGuard(
|
||||
const std::vector<BlobWriteBatchTransformer::RollbackInfo>*
|
||||
rollback_infos,
|
||||
Logger* info_log)
|
||||
: rollback_infos_(rollback_infos), info_log_(info_log) {}
|
||||
BlobWriteRollbackGuard(const BlobWriteRollbackGuard&) = delete;
|
||||
BlobWriteRollbackGuard& operator=(const BlobWriteRollbackGuard&) = delete;
|
||||
BlobWriteRollbackGuard(BlobWriteRollbackGuard&&) = delete;
|
||||
BlobWriteRollbackGuard& operator=(BlobWriteRollbackGuard&&) = delete;
|
||||
|
||||
~BlobWriteRollbackGuard() {
|
||||
if (!active_ || rollback_infos_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& rollback_info : *rollback_infos_) {
|
||||
if (rollback_info.partition_mgr == nullptr) {
|
||||
continue;
|
||||
}
|
||||
Status rollback_s = rollback_info.partition_mgr->MarkBlobWriteAsGarbage(
|
||||
rollback_info.file_number, rollback_info.count, rollback_info.bytes);
|
||||
if (!rollback_s.ok()) {
|
||||
if (info_log_ != nullptr) {
|
||||
ROCKS_LOG_ERROR(
|
||||
info_log_,
|
||||
"Failed to rollback blob direct-write garbage accounting for "
|
||||
"file #%" PRIu64 " (%" PRIu64 " blobs, %" PRIu64 " bytes): %s",
|
||||
rollback_info.file_number, rollback_info.count,
|
||||
rollback_info.bytes, rollback_s.ToString().c_str());
|
||||
}
|
||||
rollback_s.PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Dismiss() { active_ = false; }
|
||||
|
||||
private:
|
||||
const std::vector<BlobWriteBatchTransformer::RollbackInfo>* rollback_infos_;
|
||||
Logger* info_log_;
|
||||
bool active_ = true;
|
||||
};
|
||||
|
||||
class PutEntityFastPathWriteCallback final : public WriteCallback {
|
||||
public:
|
||||
Status Callback(DB* /*db*/) override { return Status::OK(); }
|
||||
|
||||
bool AllowWriteBatching() override { return false; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
// Convenience methods
|
||||
Status DBImpl::Put(const WriteOptions& o, ColumnFamilyHandle* column_family,
|
||||
const Slice& key, const Slice& val) {
|
||||
@@ -46,18 +109,23 @@ Status DBImpl::PutEntity(const WriteOptions& options,
|
||||
return s;
|
||||
}
|
||||
|
||||
return DB::PutEntity(options, column_family, key, columns);
|
||||
return PutEntityFastPath(options, column_family, key, columns);
|
||||
}
|
||||
|
||||
Status DBImpl::PutEntity(const WriteOptions& options, const Slice& key,
|
||||
const AttributeGroups& attribute_groups) {
|
||||
if (attribute_groups.empty()) {
|
||||
return Status::InvalidArgument(
|
||||
"Cannot call this method with empty attribute groups");
|
||||
}
|
||||
|
||||
for (const AttributeGroup& ag : attribute_groups) {
|
||||
const Status s = FailIfCfHasTs(ag.column_family());
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return DB::PutEntity(options, key, attribute_groups);
|
||||
return PutEntityFastPath(options, key, attribute_groups);
|
||||
}
|
||||
|
||||
Status DBImpl::Merge(const WriteOptions& o, ColumnFamilyHandle* column_family,
|
||||
@@ -200,6 +268,11 @@ Status DBImpl::IngestWriteBatchWithIndex(
|
||||
return Status::NotSupported(
|
||||
"IngestWriteBatchWithIndex does not support disableWAL=true");
|
||||
}
|
||||
if (HasAnyBlobDirectWriteColumnFamily()) {
|
||||
return Status::NotSupported(
|
||||
"IngestWriteBatchWithIndex is not supported with "
|
||||
"enable_blob_direct_write");
|
||||
}
|
||||
Status s;
|
||||
if (write_options.protection_bytes_per_key > 0) {
|
||||
s = WriteBatchInternal::UpdateProtectionInfo(
|
||||
@@ -221,7 +294,7 @@ Status DBImpl::IngestWBWIAsMemtable(
|
||||
std::shared_ptr<WriteBatchWithIndex> wbwi,
|
||||
const WBWIMemTable::SeqnoRange& assigned_seqno, uint64_t min_prep_log,
|
||||
SequenceNumber last_seqno_after_ingest, bool memtable_updated,
|
||||
bool ignore_missing_cf) {
|
||||
bool ingest_wbwi_for_commit, bool ignore_missing_cf) {
|
||||
// Keys in new memtable have seqno > last_seqno_after_ingest >= keys in wbwi.
|
||||
assert(assigned_seqno.upper_bound <= last_seqno_after_ingest);
|
||||
// Keys in the current memtable have seqno <= LastSequence() < keys in wbwi.
|
||||
@@ -250,8 +323,16 @@ Status DBImpl::IngestWBWIAsMemtable(
|
||||
std::to_string(cf_id));
|
||||
if (memtable_updated) {
|
||||
s = Status::Corruption(
|
||||
"Part of the write batch is applied. Memtable is in a inconsistent "
|
||||
"state. " +
|
||||
"Part of the write batch is applied. Memtable is in an "
|
||||
"inconsistent state due to invalid column family. " +
|
||||
s.ToString());
|
||||
error_handler_.SetBGError(s, BackgroundErrorReason::kMemTable);
|
||||
} else if (ingest_wbwi_for_commit) {
|
||||
s = Status::Corruption(
|
||||
"Commit marker is durable in WAL, but publishing committed WBWI "
|
||||
"data to memtable failed due to invalid column family. This DB "
|
||||
"instance cannot safely resume; close and reopen the DB for WAL "
|
||||
"recovery. " +
|
||||
s.ToString());
|
||||
error_handler_.SetBGError(s, BackgroundErrorReason::kMemTable);
|
||||
}
|
||||
@@ -316,8 +397,16 @@ Status DBImpl::IngestWBWIAsMemtable(
|
||||
if (i != 0 || memtable_updated) {
|
||||
// escalate error to non-recoverable
|
||||
s = Status::Corruption(
|
||||
"Part of the write batch is applied. Memtable is in a inconsistent "
|
||||
"state. " +
|
||||
"Part of the write batch is applied. Memtable is in an "
|
||||
"inconsistent state due to SwitchMemtable failure. " +
|
||||
s.ToString());
|
||||
error_handler_.SetBGError(s, BackgroundErrorReason::kMemTable);
|
||||
} else if (ingest_wbwi_for_commit) {
|
||||
s = Status::Corruption(
|
||||
"Commit marker is durable in WAL, but publishing committed WBWI "
|
||||
"data to memtable failed due to SwitchMemtable failure. This DB "
|
||||
"instance cannot safely resume; close and reopen the DB for WAL "
|
||||
"recovery. " +
|
||||
s.ToString());
|
||||
error_handler_.SetBGError(s, BackgroundErrorReason::kMemTable);
|
||||
} else {
|
||||
@@ -358,6 +447,7 @@ Status DBImpl::IngestWBWIAsMemtable(
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
AssignAtomicFlushSeq(cfds);
|
||||
FlushRequest flush_req;
|
||||
flush_req.atomic_flush = true;
|
||||
GenerateFlushRequest(cfds, FlushReason::kExternalFileIngestion,
|
||||
&flush_req);
|
||||
EnqueuePendingFlush(flush_req);
|
||||
@@ -367,14 +457,421 @@ Status DBImpl::IngestWBWIAsMemtable(
|
||||
return s;
|
||||
}
|
||||
|
||||
Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
WriteBatch* my_batch, WriteCallback* callback,
|
||||
UserWriteCallback* user_write_cb, uint64_t* wal_used,
|
||||
uint64_t log_ref, bool disable_memtable,
|
||||
uint64_t* seq_used, size_t batch_cnt,
|
||||
PreReleaseCallback* pre_release_callback,
|
||||
PostMemTableCallback* post_memtable_callback,
|
||||
std::shared_ptr<WriteBatchWithIndex> wbwi) {
|
||||
struct DBImpl::BlobDirectWriteContext {
|
||||
struct Entry {
|
||||
// Hold a referenced SuperVersion so BDW uses the same published mutable
|
||||
// option snapshot as the rest of the DB while keeping the CFD alive.
|
||||
SuperVersion* super_version = nullptr;
|
||||
BlobFilePartitionManager* partition_mgr = nullptr;
|
||||
BlobDirectWriteSettings settings;
|
||||
};
|
||||
|
||||
explicit BlobDirectWriteContext(DBImpl* db_impl) : db_impl_(db_impl) {}
|
||||
BlobDirectWriteContext(const BlobDirectWriteContext&) = delete;
|
||||
BlobDirectWriteContext& operator=(const BlobDirectWriteContext&) = delete;
|
||||
BlobDirectWriteContext(BlobDirectWriteContext&&) = delete;
|
||||
BlobDirectWriteContext& operator=(BlobDirectWriteContext&&) = delete;
|
||||
|
||||
~BlobDirectWriteContext() { Release(); }
|
||||
|
||||
BlobDirectWriteSettings GetSettings(uint32_t cf_id,
|
||||
bool lookup_from_write_thread) {
|
||||
return GetOrCreate(cf_id, lookup_from_write_thread).settings;
|
||||
}
|
||||
|
||||
BlobFilePartitionManager* GetPartitionManager(uint32_t cf_id,
|
||||
bool lookup_from_write_thread) {
|
||||
return GetOrCreate(cf_id, lookup_from_write_thread).partition_mgr;
|
||||
}
|
||||
|
||||
void Release() {
|
||||
if (entries_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& entry : entries_) {
|
||||
if (entry.second.super_version != nullptr) {
|
||||
db_impl_->CleanupSuperVersion(entry.second.super_version);
|
||||
}
|
||||
}
|
||||
entries_.clear();
|
||||
}
|
||||
|
||||
UnorderedSet<BlobFilePartitionManager*> touched_managers;
|
||||
std::vector<BlobWriteBatchTransformer::RollbackInfo> rollback_infos;
|
||||
|
||||
private:
|
||||
static BlobDirectWriteSettings BuildSettings(const SuperVersion* sv) {
|
||||
assert(sv != nullptr);
|
||||
|
||||
BlobDirectWriteSettings settings;
|
||||
settings.enable_blob_direct_write =
|
||||
sv->cfd->ioptions().enable_blob_direct_write;
|
||||
settings.min_blob_size = sv->mutable_cf_options.min_blob_size;
|
||||
settings.compression_type = sv->mutable_cf_options.blob_compression_type;
|
||||
settings.blob_cache = sv->cfd->ioptions().blob_cache.get();
|
||||
settings.prepopulate_blob_cache =
|
||||
sv->mutable_cf_options.prepopulate_blob_cache;
|
||||
return settings;
|
||||
}
|
||||
|
||||
SuperVersion* AcquireReferencedSuperVersion(uint32_t cf_id,
|
||||
bool lookup_from_write_thread) {
|
||||
if (lookup_from_write_thread) {
|
||||
auto* cfd =
|
||||
db_impl_->versions_->GetColumnFamilySet()->GetColumnFamily(cf_id);
|
||||
if (cfd != nullptr) {
|
||||
return cfd->GetReferencedSuperVersion(db_impl_);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ColumnFamilyData* cfd = nullptr;
|
||||
{
|
||||
InstrumentedMutexLock lock(&db_impl_->mutex_);
|
||||
cfd = db_impl_->versions_->GetColumnFamilySet()->GetColumnFamily(cf_id);
|
||||
if (cfd != nullptr) {
|
||||
// Hold the CFD long enough to acquire a referenced SuperVersion after
|
||||
// dropping DB mutex. This reuses the existing SuperVersion publication
|
||||
// path for mutable options rather than maintaining a separate BDW
|
||||
// settings snapshot. A future attached-WriteBatch path could pin the
|
||||
// needed CFDs up front and avoid this DB mutex lookup entirely.
|
||||
cfd->Ref();
|
||||
}
|
||||
}
|
||||
if (cfd == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SuperVersion* sv = cfd->GetReferencedSuperVersion(db_impl_);
|
||||
{
|
||||
InstrumentedMutexLock lock(&db_impl_->mutex_);
|
||||
cfd->UnrefAndTryDelete();
|
||||
}
|
||||
return sv;
|
||||
}
|
||||
|
||||
Entry& GetOrCreate(uint32_t cf_id, bool lookup_from_write_thread) {
|
||||
auto entry_it = entries_.find(cf_id);
|
||||
if (entry_it != entries_.end()) {
|
||||
return entry_it->second;
|
||||
}
|
||||
|
||||
Entry entry;
|
||||
if (SuperVersion* sv =
|
||||
AcquireReferencedSuperVersion(cf_id, lookup_from_write_thread)) {
|
||||
entry.super_version = sv;
|
||||
entry.partition_mgr = sv->cfd->blob_partition_manager();
|
||||
if (entry.partition_mgr != nullptr) {
|
||||
entry.settings = BuildSettings(sv);
|
||||
}
|
||||
}
|
||||
|
||||
return entries_.emplace(cf_id, std::move(entry)).first->second;
|
||||
}
|
||||
|
||||
DBImpl* db_impl_;
|
||||
std::unordered_map<uint32_t, Entry> entries_;
|
||||
};
|
||||
|
||||
Status DBImpl::MaybeTransformBatchForBlobDirectWrite(
|
||||
const WriteOptions& write_options, WriteBatch** batch,
|
||||
bool should_write_to_memtable, bool lookup_from_write_thread,
|
||||
WriteBatch* transformed_storage,
|
||||
BlobDirectWriteContext* blob_direct_write_ctx) {
|
||||
assert(batch != nullptr);
|
||||
assert(transformed_storage != nullptr);
|
||||
assert(blob_direct_write_ctx != nullptr);
|
||||
|
||||
if (!should_write_to_memtable || *batch == nullptr ||
|
||||
(!(*batch)->HasPut() && !(*batch)->HasPutEntity()) ||
|
||||
(*batch)->HasMerge()) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
// Quick check: skip the (potentially expensive) batch iteration when no
|
||||
// column family has blob direct write enabled. This also avoids Iterate()
|
||||
// returning an error on a corrupted batch before the WAL verification path
|
||||
// has a chance to detect it.
|
||||
if (!HasAnyBlobDirectWriteColumnFamily()) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
std::vector<BlobFilePartitionManager*> batch_managers;
|
||||
std::vector<BlobWriteBatchTransformer::RollbackInfo> batch_rollbacks;
|
||||
bool transformed = false;
|
||||
transformed_storage->Clear();
|
||||
|
||||
auto settings_provider = [&](uint32_t cf_id) -> BlobDirectWriteSettings {
|
||||
return blob_direct_write_ctx->GetSettings(cf_id, lookup_from_write_thread);
|
||||
};
|
||||
auto partition_mgr_provider =
|
||||
[&](uint32_t cf_id) -> BlobFilePartitionManager* {
|
||||
return blob_direct_write_ctx->GetPartitionManager(cf_id,
|
||||
lookup_from_write_thread);
|
||||
};
|
||||
Status s = BlobWriteBatchTransformer::TransformBatch(
|
||||
write_options, *batch, transformed_storage, partition_mgr_provider,
|
||||
settings_provider, &transformed, &batch_managers, &batch_rollbacks);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
if (transformed) {
|
||||
*batch = transformed_storage;
|
||||
blob_direct_write_ctx->touched_managers.insert(batch_managers.begin(),
|
||||
batch_managers.end());
|
||||
blob_direct_write_ctx->rollback_infos.insert(
|
||||
blob_direct_write_ctx->rollback_infos.end(), batch_rollbacks.begin(),
|
||||
batch_rollbacks.end());
|
||||
}
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DBImpl::AppendPreprocessedPutEntityToBatch(
|
||||
const WriteOptions& write_options, WriteBatch* batch,
|
||||
ColumnFamilyHandle* column_family, const Slice& key,
|
||||
const WideColumns& columns, BlobDirectWriteContext* blob_direct_write_ctx) {
|
||||
assert(batch != nullptr);
|
||||
|
||||
Status s;
|
||||
uint32_t cf_id = 0;
|
||||
size_t ts_sz = 0;
|
||||
std::tie(s, cf_id, ts_sz) =
|
||||
WriteBatchInternal::GetColumnFamilyIdAndTimestampSize(batch,
|
||||
column_family);
|
||||
if (s.ok()) {
|
||||
assert(ts_sz == 0);
|
||||
|
||||
WideColumns sorted_columns(columns);
|
||||
WideColumnsHelper::SortColumns(sorted_columns);
|
||||
|
||||
return AppendSortedPutEntityToBatch(write_options, batch, cf_id, key,
|
||||
sorted_columns, blob_direct_write_ctx);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Status DBImpl::AppendSortedPutEntityToBatch(
|
||||
const WriteOptions& write_options, WriteBatch* batch, uint32_t cf_id,
|
||||
const Slice& key, const WideColumns& sorted_columns,
|
||||
BlobDirectWriteContext* blob_direct_write_ctx) {
|
||||
assert(batch != nullptr);
|
||||
|
||||
Status s;
|
||||
BlobDirectWriteSettings settings;
|
||||
BlobFilePartitionManager* partition_mgr = nullptr;
|
||||
std::vector<BlobWriteBatchTransformer::RollbackInfo>* rollback_infos =
|
||||
nullptr;
|
||||
if (blob_direct_write_ctx != nullptr) {
|
||||
settings = blob_direct_write_ctx->GetSettings(
|
||||
cf_id, /*lookup_from_write_thread=*/false);
|
||||
partition_mgr = blob_direct_write_ctx->GetPartitionManager(
|
||||
cf_id, /*lookup_from_write_thread=*/false);
|
||||
rollback_infos = &blob_direct_write_ctx->rollback_infos;
|
||||
}
|
||||
|
||||
std::string entity;
|
||||
bool transformed = false;
|
||||
s = BlobWriteBatchTransformer::MaybePreprocessWideColumns(
|
||||
write_options, cf_id, key, sorted_columns, partition_mgr, settings,
|
||||
/*serialize_inline_entity=*/true, &entity, &transformed, rollback_infos);
|
||||
if (s.ok()) {
|
||||
if (transformed && blob_direct_write_ctx != nullptr) {
|
||||
blob_direct_write_ctx->touched_managers.insert(partition_mgr);
|
||||
}
|
||||
|
||||
return WriteBatchInternal::PutEntitySerialized(batch, cf_id, key, entity);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Status DBImpl::PutEntityFastPath(const WriteOptions& write_options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
const Slice& key, const WideColumns& columns) {
|
||||
const ColumnFamilyHandle* const default_cf = DefaultColumnFamily();
|
||||
assert(default_cf);
|
||||
|
||||
const Comparator* const default_cf_ucmp = default_cf->GetComparator();
|
||||
assert(default_cf_ucmp);
|
||||
|
||||
WriteBatch batch(/*reserved_bytes=*/0, /*max_bytes=*/0,
|
||||
write_options.protection_bytes_per_key,
|
||||
default_cf_ucmp->timestamp_size());
|
||||
std::unique_ptr<WriteBatch> trace_batch;
|
||||
bool needs_trace_batch = false;
|
||||
{
|
||||
InstrumentedMutexLock lock(&trace_mutex_);
|
||||
needs_trace_batch = tracer_ != nullptr;
|
||||
}
|
||||
if (needs_trace_batch) {
|
||||
// Keep tracing on the original logical `PutEntity()` op. The applied batch
|
||||
// can stay empty here and be materialized later inside `WriteImpl()`.
|
||||
trace_batch = std::make_unique<WriteBatch>(
|
||||
/*reserved_bytes=*/0, /*max_bytes=*/0,
|
||||
/*protection_bytes_per_key=*/0, default_cf_ucmp->timestamp_size());
|
||||
Status trace_s = trace_batch->PutEntity(column_family, key, columns);
|
||||
if (!trace_s.ok()) {
|
||||
return trace_s;
|
||||
}
|
||||
}
|
||||
|
||||
auto* cfh = static_cast_with_check<ColumnFamilyHandleImpl>(column_family);
|
||||
const bool needs_deferred_transform =
|
||||
cfh->cfd()->blob_partition_manager() != nullptr;
|
||||
std::optional<DeferredPutEntityBatch> deferred_put_entities;
|
||||
if (needs_deferred_transform) {
|
||||
WideColumns sorted_columns(columns);
|
||||
WideColumnsHelper::SortColumns(sorted_columns);
|
||||
deferred_put_entities.emplace();
|
||||
deferred_put_entities->ops.push_back(
|
||||
{cfh->cfd()->GetID(), key.ToString(), std::move(sorted_columns)});
|
||||
} else {
|
||||
Status append_s = AppendPreprocessedPutEntityToBatch(
|
||||
write_options, &batch, column_family, key, columns,
|
||||
/*blob_direct_write_ctx=*/nullptr);
|
||||
if (!append_s.ok()) {
|
||||
return append_s;
|
||||
}
|
||||
}
|
||||
|
||||
Status write_s = WritePreprocessedPutEntityBatch(
|
||||
write_options, &batch, trace_batch ? trace_batch.get() : nullptr,
|
||||
deferred_put_entities ? &deferred_put_entities.value() : nullptr);
|
||||
return write_s;
|
||||
}
|
||||
|
||||
Status DBImpl::PutEntityFastPath(const WriteOptions& write_options,
|
||||
const Slice& key,
|
||||
const AttributeGroups& attribute_groups) {
|
||||
ColumnFamilyHandle* default_cf = DefaultColumnFamily();
|
||||
assert(default_cf);
|
||||
const Comparator* const default_cf_ucmp = default_cf->GetComparator();
|
||||
assert(default_cf_ucmp);
|
||||
|
||||
WriteBatch batch(/*reserved_bytes=*/0, /*max_bytes=*/0,
|
||||
write_options.protection_bytes_per_key,
|
||||
default_cf_ucmp->timestamp_size());
|
||||
std::unique_ptr<WriteBatch> trace_batch;
|
||||
bool needs_trace_batch = false;
|
||||
{
|
||||
InstrumentedMutexLock lock(&trace_mutex_);
|
||||
needs_trace_batch = tracer_ != nullptr;
|
||||
}
|
||||
if (needs_trace_batch) {
|
||||
// Keep tracing on the original logical `PutEntity()` op. The applied batch
|
||||
// can stay empty here and be materialized later inside `WriteImpl()`.
|
||||
trace_batch = std::make_unique<WriteBatch>(
|
||||
/*reserved_bytes=*/0, /*max_bytes=*/0,
|
||||
/*protection_bytes_per_key=*/0, default_cf_ucmp->timestamp_size());
|
||||
Status trace_s = trace_batch->PutEntity(key, attribute_groups);
|
||||
if (!trace_s.ok()) {
|
||||
return trace_s;
|
||||
}
|
||||
}
|
||||
|
||||
bool needs_deferred_transform = false;
|
||||
for (const AttributeGroup& ag : attribute_groups) {
|
||||
auto* cfh =
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(ag.column_family());
|
||||
if (cfh->cfd()->blob_partition_manager() != nullptr) {
|
||||
needs_deferred_transform = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<DeferredPutEntityBatch> deferred_put_entities;
|
||||
if (needs_deferred_transform) {
|
||||
deferred_put_entities.emplace();
|
||||
}
|
||||
|
||||
for (const AttributeGroup& ag : attribute_groups) {
|
||||
auto* cfh =
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(ag.column_family());
|
||||
if (deferred_put_entities.has_value()) {
|
||||
WideColumns sorted_columns(ag.columns());
|
||||
WideColumnsHelper::SortColumns(sorted_columns);
|
||||
deferred_put_entities->ops.push_back(
|
||||
{cfh->cfd()->GetID(), key.ToString(), std::move(sorted_columns)});
|
||||
continue;
|
||||
}
|
||||
|
||||
Status append_s = AppendPreprocessedPutEntityToBatch(
|
||||
write_options, &batch, ag.column_family(), key, ag.columns(),
|
||||
/*blob_direct_write_ctx=*/nullptr);
|
||||
if (!append_s.ok()) {
|
||||
return append_s;
|
||||
}
|
||||
}
|
||||
|
||||
Status write_s = WritePreprocessedPutEntityBatch(
|
||||
write_options, &batch, trace_batch ? trace_batch.get() : nullptr,
|
||||
deferred_put_entities ? &deferred_put_entities.value() : nullptr);
|
||||
return write_s;
|
||||
}
|
||||
|
||||
Status DBImpl::WritePreprocessedPutEntityBatch(
|
||||
const WriteOptions& write_options, WriteBatch* batch,
|
||||
WriteBatch* trace_batch,
|
||||
const DeferredPutEntityBatch* deferred_put_entities) {
|
||||
Status s;
|
||||
if (deferred_put_entities == nullptr &&
|
||||
write_options.protection_bytes_per_key > 0) {
|
||||
s = WriteBatchInternal::UpdateProtectionInfo(
|
||||
batch, write_options.protection_bytes_per_key);
|
||||
}
|
||||
if (s.ok()) {
|
||||
PutEntityFastPathWriteCallback no_batching_callback;
|
||||
WriteCallback* callback =
|
||||
deferred_put_entities != nullptr ? &no_batching_callback : nullptr;
|
||||
// Pass the logical trace batch separately since `batch` may be rebuilt in
|
||||
// `WriteImpl()` after `PreprocessWrite()` selects the target generation.
|
||||
s = WriteImpl(write_options, batch, callback,
|
||||
/*user_write_cb=*/nullptr,
|
||||
/*wal_used=*/nullptr, /*log_ref=*/0,
|
||||
/*disable_memtable=*/false, /*seq_used=*/nullptr,
|
||||
/*batch_cnt=*/0, /*pre_release_callback=*/nullptr,
|
||||
/*post_memtable_callback=*/nullptr,
|
||||
/*wbwi=*/nullptr, trace_batch,
|
||||
/*skip_blob_direct_write_transform=*/true,
|
||||
deferred_put_entities);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
Status DBImpl::SyncBlobDirectWriteManagers(
|
||||
const WriteOptions& write_options,
|
||||
const BlobDirectWriteContext& blob_direct_write_ctx) {
|
||||
for (auto* mgr : blob_direct_write_ctx.touched_managers) {
|
||||
Status blob_s = write_options.sync ? mgr->SyncAllOpenFiles(write_options)
|
||||
: mgr->FlushAllOpenFiles(write_options);
|
||||
if (!blob_s.ok()) {
|
||||
return blob_s;
|
||||
}
|
||||
}
|
||||
|
||||
if (!blob_direct_write_ctx.touched_managers.empty()) {
|
||||
// Tests can inject a post-transform failure after blob bytes are durable
|
||||
// enough to require manifest-time garbage accounting.
|
||||
Status blob_s;
|
||||
TEST_SYNC_POINT_CALLBACK("DBImpl::WriteImpl:AfterBlobDirectWrite", &blob_s);
|
||||
if (!blob_s.ok()) {
|
||||
return blob_s;
|
||||
}
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
Status DBImpl::WriteImpl(
|
||||
const WriteOptions& write_options, WriteBatch* my_batch,
|
||||
WriteCallback* callback, UserWriteCallback* user_write_cb,
|
||||
uint64_t* wal_used, uint64_t log_ref, bool disable_memtable,
|
||||
uint64_t* seq_used, size_t batch_cnt,
|
||||
PreReleaseCallback* pre_release_callback,
|
||||
PostMemTableCallback* post_memtable_callback,
|
||||
std::shared_ptr<WriteBatchWithIndex> wbwi, WriteBatch* trace_batch_override,
|
||||
bool skip_blob_direct_write_transform,
|
||||
const DBImpl::DeferredPutEntityBatch* deferred_put_entities) {
|
||||
assert(!seq_per_batch_ || batch_cnt != 0);
|
||||
assert(my_batch == nullptr || my_batch->Count() == 0 ||
|
||||
write_options.protection_bytes_per_key == 0 ||
|
||||
@@ -421,6 +918,12 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
"WriteOptions::disableWAL option is not supported if "
|
||||
"DBOptions::recycle_log_file_num > 0");
|
||||
}
|
||||
WriteBatch* trace_batch =
|
||||
trace_batch_override != nullptr ? trace_batch_override : my_batch;
|
||||
// Non-preserved-order tracing runs before the write joins a group, so choose
|
||||
// the logical batch here. Preserved-order tracing later uses
|
||||
// `Writer::trace_batch` on each grouped writer.
|
||||
|
||||
// TODO: this use of operator bool on `tracer_` can avoid unnecessary lock
|
||||
// grabs but does not seem thread-safe.
|
||||
if (tracer_) {
|
||||
@@ -430,7 +933,7 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
// efficient to trace here than to add latency to a phase of the log/apply
|
||||
// pipeline.
|
||||
// TODO: maybe handle the tracing status?
|
||||
tracer_->Write(my_batch).PermitUncheckedError();
|
||||
tracer_->Write(trace_batch).PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
if (write_options.sync && write_options.disableWAL) {
|
||||
@@ -504,10 +1007,50 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
seq_per_batch_ ? kDoAssignOrder : kDontAssignOrder;
|
||||
// Otherwise it is WAL-only Prepare batches in WriteCommitted policy and
|
||||
// they don't consume sequence.
|
||||
return WriteImplWALOnly(
|
||||
&nonmem_write_thread_, write_options, my_batch, callback, user_write_cb,
|
||||
wal_used, log_ref, seq_used, batch_cnt, pre_release_callback,
|
||||
assign_order, kDontPublishLastSeq, disable_memtable);
|
||||
return WriteImplWALOnly(&nonmem_write_thread_, write_options, my_batch,
|
||||
my_batch, callback, user_write_cb, wal_used,
|
||||
log_ref, seq_used, batch_cnt, pre_release_callback,
|
||||
assign_order, kDontPublishLastSeq,
|
||||
disable_memtable);
|
||||
}
|
||||
|
||||
const bool maybe_use_blob_direct_write = wbwi == nullptr &&
|
||||
!skip_blob_direct_write_transform &&
|
||||
HasAnyBlobDirectWriteColumnFamily();
|
||||
const bool maybe_use_deferred_put_entity =
|
||||
deferred_put_entities != nullptr && wbwi == nullptr &&
|
||||
HasAnyBlobDirectWriteColumnFamily();
|
||||
std::optional<WriteBatch> transformed_batch_storage;
|
||||
std::optional<std::vector<WriteBatch>> transformed_write_group_batches;
|
||||
std::optional<BlobDirectWriteContext> blob_direct_write_ctx;
|
||||
std::optional<BlobWriteRollbackGuard> blob_write_rollback_guard;
|
||||
if (maybe_use_blob_direct_write || maybe_use_deferred_put_entity) {
|
||||
blob_direct_write_ctx.emplace(this);
|
||||
blob_write_rollback_guard.emplace(&blob_direct_write_ctx->rollback_infos,
|
||||
immutable_db_options_.info_log.get());
|
||||
}
|
||||
auto finish_write = [&](Status s) {
|
||||
if (s.ok() && blob_write_rollback_guard.has_value()) {
|
||||
blob_write_rollback_guard->Dismiss();
|
||||
}
|
||||
return s;
|
||||
};
|
||||
if (UNLIKELY(maybe_use_blob_direct_write) &&
|
||||
(immutable_db_options_.unordered_write ||
|
||||
immutable_db_options_.enable_pipelined_write)) {
|
||||
transformed_batch_storage.emplace();
|
||||
Status blob_s = MaybeTransformBatchForBlobDirectWrite(
|
||||
write_options, &my_batch, !disable_memtable,
|
||||
/*lookup_from_write_thread=*/false, &transformed_batch_storage.value(),
|
||||
&blob_direct_write_ctx.value());
|
||||
if (!blob_s.ok()) {
|
||||
return blob_s;
|
||||
}
|
||||
blob_s = SyncBlobDirectWriteManagers(write_options,
|
||||
blob_direct_write_ctx.value());
|
||||
if (!blob_s.ok()) {
|
||||
return blob_s;
|
||||
}
|
||||
}
|
||||
|
||||
if (immutable_db_options_.unordered_write) {
|
||||
@@ -518,10 +1061,11 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
uint64_t seq = 0;
|
||||
// Use a write thread to i) optimize for WAL write, ii) publish last
|
||||
// sequence in in increasing order, iii) call pre_release_callback serially
|
||||
Status status = WriteImplWALOnly(
|
||||
&write_thread_, write_options, my_batch, callback, user_write_cb,
|
||||
wal_used, log_ref, &seq, sub_batch_cnt, pre_release_callback,
|
||||
kDoAssignOrder, kDoPublishLastSeq, disable_memtable);
|
||||
Status status =
|
||||
WriteImplWALOnly(&write_thread_, write_options, my_batch, trace_batch,
|
||||
callback, user_write_cb, wal_used, log_ref, &seq,
|
||||
sub_batch_cnt, pre_release_callback, kDoAssignOrder,
|
||||
kDoPublishLastSeq, disable_memtable);
|
||||
TEST_SYNC_POINT("DBImpl::WriteImpl:UnorderedWriteAfterWriteWAL");
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
@@ -534,19 +1078,20 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
status = UnorderedWriteMemtable(write_options, my_batch, callback,
|
||||
log_ref, seq, sub_batch_cnt);
|
||||
}
|
||||
return status;
|
||||
return finish_write(status);
|
||||
}
|
||||
|
||||
if (immutable_db_options_.enable_pipelined_write) {
|
||||
return PipelinedWriteImpl(write_options, my_batch, callback, user_write_cb,
|
||||
wal_used, log_ref, disable_memtable, seq_used);
|
||||
return finish_write(PipelinedWriteImpl(
|
||||
write_options, my_batch, trace_batch, callback, user_write_cb, wal_used,
|
||||
log_ref, disable_memtable, seq_used));
|
||||
}
|
||||
|
||||
PERF_TIMER_GUARD(write_pre_and_post_process_time);
|
||||
WriteThread::Writer w(write_options, my_batch, callback, user_write_cb,
|
||||
log_ref, disable_memtable, batch_cnt,
|
||||
pre_release_callback, post_memtable_callback,
|
||||
/*_ingest_wbwi=*/wbwi != nullptr);
|
||||
/*_ingest_wbwi=*/wbwi != nullptr, trace_batch);
|
||||
StopWatch write_sw(immutable_db_options_.clock, stats_, DB_WRITE);
|
||||
|
||||
write_thread_.JoinBatchGroup(&w);
|
||||
@@ -604,7 +1149,7 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
*seq_used = w.sequence;
|
||||
}
|
||||
// write is complete and leader has updated sequence
|
||||
return w.FinalStatus();
|
||||
return finish_write(w.FinalStatus());
|
||||
}
|
||||
// else we are the leader of the write batch group
|
||||
assert(w.state == WriteThread::STATE_GROUP_LEADER);
|
||||
@@ -654,6 +1199,59 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
IOStatus io_s;
|
||||
Status pre_release_cb_status;
|
||||
size_t seq_inc = 0;
|
||||
if (status.ok()) {
|
||||
if (deferred_put_entities != nullptr) {
|
||||
my_batch->Clear();
|
||||
for (const auto& op : deferred_put_entities->ops) {
|
||||
status = AppendSortedPutEntityToBatch(
|
||||
write_options, my_batch, op.column_family_id, op.key,
|
||||
op.sorted_columns,
|
||||
blob_direct_write_ctx ? &blob_direct_write_ctx.value() : nullptr);
|
||||
if (!status.ok()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (status.ok() && write_options.protection_bytes_per_key > 0) {
|
||||
status = WriteBatchInternal::UpdateProtectionInfo(
|
||||
my_batch, write_options.protection_bytes_per_key);
|
||||
}
|
||||
if (status.ok() && blob_direct_write_ctx.has_value()) {
|
||||
status = SyncBlobDirectWriteManagers(write_options,
|
||||
blob_direct_write_ctx.value());
|
||||
}
|
||||
}
|
||||
|
||||
if (UNLIKELY(maybe_use_blob_direct_write) &&
|
||||
!immutable_db_options_.unordered_write &&
|
||||
!immutable_db_options_.enable_pipelined_write) {
|
||||
// PreprocessWrite() may switch memtables in order to schedule pending
|
||||
// flushes. Defer the blob direct-write transformation until after that
|
||||
// point so the blob file generation matches the memtable that will store
|
||||
// the transformed blob index.
|
||||
transformed_write_group_batches.emplace();
|
||||
transformed_write_group_batches->reserve(write_group.size);
|
||||
for (auto* writer : write_group) {
|
||||
assert(writer != nullptr);
|
||||
if (!writer->ShouldWriteToMemtable()) {
|
||||
continue;
|
||||
}
|
||||
transformed_write_group_batches->emplace_back();
|
||||
status = MaybeTransformBatchForBlobDirectWrite(
|
||||
write_options, &writer->batch, /*should_write_to_memtable=*/true,
|
||||
/*lookup_from_write_thread=*/true,
|
||||
&transformed_write_group_batches->back(),
|
||||
&blob_direct_write_ctx.value());
|
||||
if (!status.ok()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (status.ok()) {
|
||||
status = SyncBlobDirectWriteManagers(write_options,
|
||||
blob_direct_write_ctx.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (status.ok()) {
|
||||
// Rules for when we can update the memtable concurrently
|
||||
// 1. supported by memtable
|
||||
@@ -702,7 +1300,7 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
// is in writer->batch
|
||||
tracer_->Write(wbwi->GetWriteBatch()).PermitUncheckedError();
|
||||
} else {
|
||||
tracer_->Write(writer->batch).PermitUncheckedError();
|
||||
tracer_->Write(writer->trace_batch).PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -923,11 +1521,11 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
if (two_write_queues_) {
|
||||
assert(ub <= versions_->LastAllocatedSequence());
|
||||
}
|
||||
status =
|
||||
IngestWBWIAsMemtable(wbwi, {/*lower_bound=*/lb, /*upper_bound=*/ub},
|
||||
/*min_prep_log=*/log_ref, last_sequence,
|
||||
/*memtable_updated=*/memtable_update_count > 0,
|
||||
write_options.ignore_missing_column_families);
|
||||
status = IngestWBWIAsMemtable(
|
||||
wbwi, {/*lower_bound=*/lb, /*upper_bound=*/ub},
|
||||
/*min_prep_log=*/log_ref, last_sequence,
|
||||
/*memtable_updated=*/memtable_update_count > 0,
|
||||
ingest_wbwi_for_commit, write_options.ignore_missing_column_families);
|
||||
RecordTick(stats_, NUMBER_WBWI_INGEST);
|
||||
}
|
||||
}
|
||||
@@ -965,11 +1563,12 @@ Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
||||
if (status.ok()) {
|
||||
status = w.FinalStatus();
|
||||
}
|
||||
return status;
|
||||
return finish_write(status);
|
||||
}
|
||||
|
||||
Status DBImpl::PipelinedWriteImpl(const WriteOptions& write_options,
|
||||
WriteBatch* my_batch, WriteCallback* callback,
|
||||
WriteBatch* my_batch, WriteBatch* trace_batch,
|
||||
WriteCallback* callback,
|
||||
UserWriteCallback* user_write_cb,
|
||||
uint64_t* wal_used, uint64_t log_ref,
|
||||
bool disable_memtable, uint64_t* seq_used) {
|
||||
@@ -980,7 +1579,9 @@ Status DBImpl::PipelinedWriteImpl(const WriteOptions& write_options,
|
||||
|
||||
WriteThread::Writer w(write_options, my_batch, callback, user_write_cb,
|
||||
log_ref, disable_memtable, /*_batch_cnt=*/0,
|
||||
/*_pre_release_callback=*/nullptr);
|
||||
/*_pre_release_callback=*/nullptr,
|
||||
/*_post_memtable_callback=*/nullptr,
|
||||
/*_ingest_wbwi=*/false, trace_batch);
|
||||
write_thread_.JoinBatchGroup(&w);
|
||||
TEST_SYNC_POINT("DBImplWrite::PipelinedWriteImpl:AfterJoinBatchGroup");
|
||||
if (w.state == WriteThread::STATE_GROUP_LEADER) {
|
||||
@@ -1029,7 +1630,7 @@ Status DBImpl::PipelinedWriteImpl(const WriteOptions& write_options,
|
||||
continue;
|
||||
}
|
||||
// TODO: maybe handle the tracing status?
|
||||
tracer_->Write(writer->batch).PermitUncheckedError();
|
||||
tracer_->Write(writer->trace_batch).PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1224,7 +1825,7 @@ Status DBImpl::UnorderedWriteMemtable(const WriteOptions& write_options,
|
||||
// applicable in a two-queue setting.
|
||||
Status DBImpl::WriteImplWALOnly(
|
||||
WriteThread* write_thread, const WriteOptions& write_options,
|
||||
WriteBatch* my_batch, WriteCallback* callback,
|
||||
WriteBatch* my_batch, WriteBatch* trace_batch, WriteCallback* callback,
|
||||
UserWriteCallback* user_write_cb, uint64_t* wal_used,
|
||||
const uint64_t log_ref, uint64_t* seq_used, const size_t sub_batch_cnt,
|
||||
PreReleaseCallback* pre_release_callback, const AssignOrder assign_order,
|
||||
@@ -1232,7 +1833,9 @@ Status DBImpl::WriteImplWALOnly(
|
||||
PERF_TIMER_GUARD(write_pre_and_post_process_time);
|
||||
WriteThread::Writer w(write_options, my_batch, callback, user_write_cb,
|
||||
log_ref, disable_memtable, sub_batch_cnt,
|
||||
pre_release_callback);
|
||||
pre_release_callback,
|
||||
/*_post_memtable_callback=*/nullptr,
|
||||
/*_ingest_wbwi=*/false, trace_batch);
|
||||
StopWatch write_sw(immutable_db_options_.clock, stats_, DB_WRITE);
|
||||
|
||||
write_thread->JoinBatchGroup(&w);
|
||||
@@ -1312,7 +1915,7 @@ Status DBImpl::WriteImplWALOnly(
|
||||
continue;
|
||||
}
|
||||
// TODO: maybe handle the tracing status?
|
||||
tracer_->Write(writer->batch).PermitUncheckedError();
|
||||
tracer_->Write(writer->trace_batch).PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1985,7 +2588,6 @@ void DBImpl::SelectColumnFamiliesForAtomicFlush(
|
||||
|
||||
// Assign sequence number for atomic flush.
|
||||
void DBImpl::AssignAtomicFlushSeq(const autovector<ColumnFamilyData*>& cfds) {
|
||||
assert(immutable_db_options_.atomic_flush);
|
||||
auto seq = versions_->LastSequence();
|
||||
for (auto cfd : cfds) {
|
||||
// cfd can be nullptr, see ScheduleFlushes()
|
||||
@@ -2089,6 +2691,7 @@ Status DBImpl::SwitchWAL(WriteContext* write_context) {
|
||||
}
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
FlushRequest flush_req;
|
||||
flush_req.atomic_flush = true;
|
||||
GenerateFlushRequest(cfds, FlushReason::kWalFull, &flush_req);
|
||||
EnqueuePendingFlush(flush_req);
|
||||
}
|
||||
@@ -2181,6 +2784,7 @@ Status DBImpl::HandleWriteBufferManagerFlush(WriteContext* write_context) {
|
||||
}
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
FlushRequest flush_req;
|
||||
flush_req.atomic_flush = true;
|
||||
GenerateFlushRequest(cfds, FlushReason::kWriteBufferManager, &flush_req);
|
||||
EnqueuePendingFlush(flush_req);
|
||||
}
|
||||
@@ -2457,6 +3061,7 @@ Status DBImpl::ScheduleFlushes(WriteContext* context) {
|
||||
if (immutable_db_options_.atomic_flush) {
|
||||
AssignAtomicFlushSeq(cfds);
|
||||
FlushRequest flush_req;
|
||||
flush_req.atomic_flush = true;
|
||||
GenerateFlushRequest(cfds, FlushReason::kWriteBufferFull, &flush_req);
|
||||
EnqueuePendingFlush(flush_req);
|
||||
} else {
|
||||
@@ -2540,7 +3145,7 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
|
||||
memtable_info.num_deletes = cfd->mem()->NumDeletion();
|
||||
if (!cfd->ioptions().persist_user_defined_timestamps &&
|
||||
cfd->user_comparator()->timestamp_size() > 0) {
|
||||
const Slice& newest_udt = cfd->mem()->GetNewestUDT();
|
||||
Slice newest_udt = cfd->mem()->GetNewestUDT();
|
||||
memtable_info.newest_udt.assign(newest_udt.data(), newest_udt.size());
|
||||
}
|
||||
// Log this later after lock release. It may be outdated, e.g., if background
|
||||
@@ -2563,6 +3168,7 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
|
||||
// of mutable_cf_options.write_buffer_size.
|
||||
io_s = CreateWAL(write_options, new_log_number, recycle_log_number,
|
||||
preallocate_block_size, info, &new_log);
|
||||
TEST_SYNC_POINT_CALLBACK("DBImpl::SwitchMemtable:AfterCreateWAL", &io_s);
|
||||
if (s.ok()) {
|
||||
s = io_s;
|
||||
}
|
||||
@@ -2588,11 +3194,16 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
|
||||
"[%s] New memtable created with log file: #%" PRIu64
|
||||
". Immutable memtables: %d.\n",
|
||||
cfd->GetName().c_str(), new_log_number, num_imm_unflushed);
|
||||
// There should be no concurrent write as the thread is at the front of
|
||||
// writer queue
|
||||
cfd->mem()->ConstructFragmentedRangeTombstones();
|
||||
}
|
||||
|
||||
// MarkImmutable() blocks concurrent AddLogicallyRedundantRangeTombstone()
|
||||
// from reader threads. Once blocked, safe to construct the fragmented range
|
||||
// tombstone list. Both calls happen outside the DB mutex to avoid increasing
|
||||
// write stall. MarkImmutable() is idempotent, so Add() calling it again
|
||||
// inside the mutex is harmless.
|
||||
cfd->mem()->MarkImmutable();
|
||||
cfd->mem()->ConstructFragmentedRangeTombstones();
|
||||
|
||||
mutex_.Lock();
|
||||
if (recycle_log_number != 0) {
|
||||
// Since renaming the file is done outside DB mutex, we need to ensure
|
||||
@@ -2719,9 +3330,15 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
|
||||
}
|
||||
}
|
||||
|
||||
if (cfd->blob_partition_manager() != nullptr) {
|
||||
cfd->blob_partition_manager()->RotateCurrentGeneration();
|
||||
}
|
||||
|
||||
cfd->mem()->SetNextLogNumber(cur_wal_number_);
|
||||
assert(new_mem != nullptr);
|
||||
cfd->imm()->Add(cfd->mem(), &context->memtables_to_free_);
|
||||
TEST_SYNC_POINT(
|
||||
"DBImpl::SwitchMemtable:AfterConstructFragmentedRangeTombstones");
|
||||
if (new_imm) {
|
||||
// Need to assign memtable id here before SetMemtable() below assigns id to
|
||||
// the new live memtable
|
||||
|
||||
+478
-129
@@ -11,7 +11,11 @@
|
||||
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "db/blob/blob_fetcher.h"
|
||||
#include "db/blob/blob_file_partition_manager.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/dbformat.h"
|
||||
#include "db/merge_context.h"
|
||||
#include "db/merge_helper.h"
|
||||
@@ -22,6 +26,7 @@
|
||||
#include "logging/logging.h"
|
||||
#include "memory/arena.h"
|
||||
#include "monitoring/perf_context_imp.h"
|
||||
#include "port/likely.h"
|
||||
#include "rocksdb/env.h"
|
||||
#include "rocksdb/io_dispatcher.h"
|
||||
#include "rocksdb/iterator.h"
|
||||
@@ -37,6 +42,26 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
namespace {
|
||||
|
||||
bool HasFullTimestampVisibility(const ReadOptions& read_options) {
|
||||
if (read_options.iter_start_ts != nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (read_options.timestamp == nullptr) {
|
||||
return true;
|
||||
}
|
||||
const Slice ts = *read_options.timestamp;
|
||||
for (size_t i = 0; i < ts.size(); ++i) {
|
||||
if (static_cast<unsigned char>(ts[i]) != 0xff) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DBIter::DBIter(Env* _env, const ReadOptions& read_options,
|
||||
const ImmutableOptions& ioptions,
|
||||
const MutableCFOptions& mutable_cf_options,
|
||||
@@ -51,11 +76,14 @@ DBIter::DBIter(Env* _env, const ReadOptions& read_options,
|
||||
user_comparator_(cmp),
|
||||
merge_operator_(ioptions.merge_operator.get()),
|
||||
iter_(iter),
|
||||
blob_reader_(version, read_options.read_tier,
|
||||
read_options.verify_checksums, read_options.fill_cache,
|
||||
read_options.io_activity),
|
||||
blob_reader_(
|
||||
version, read_options.read_tier, read_options.verify_checksums,
|
||||
read_options.fill_cache, read_options.io_activity,
|
||||
cfh ? cfh->cfd()->blob_file_cache() : nullptr,
|
||||
cfh != nullptr && cfh->cfd()->blob_partition_manager() != nullptr),
|
||||
read_callback_(read_callback),
|
||||
sequence_(s),
|
||||
value_columns_state_(version, read_options, cfh),
|
||||
statistics_(ioptions.stats),
|
||||
max_skip_(mutable_cf_options.max_sequential_skip_in_iterations),
|
||||
max_skippable_internal_keys_(read_options.max_skippable_internal_keys),
|
||||
@@ -72,6 +100,7 @@ DBIter::DBIter(Env* _env, const ReadOptions& read_options,
|
||||
avg_op_scan_flush_trigger_(0),
|
||||
iter_step_since_seek_(1),
|
||||
mem_hidden_op_scanned_since_seek_(0),
|
||||
contiguous_tombstone_count_(0),
|
||||
direction_(kForward),
|
||||
valid_(false),
|
||||
current_entry_is_merged_(false),
|
||||
@@ -82,6 +111,18 @@ DBIter::DBIter(Env* _env, const ReadOptions& read_options,
|
||||
expect_total_order_inner_iter_(prefix_extractor_ == nullptr ||
|
||||
read_options.total_order_seek ||
|
||||
read_options.auto_prefix_mode),
|
||||
// Read-path range conversion assumes the scan can observe all interior
|
||||
// live keys. table_filter can hide whole SSTs, and timestamp filtering
|
||||
// can hide newer UDT versions unless the read is at max timestamp with no
|
||||
// lower timestamp bound. Legacy prefix iterators without
|
||||
// prefix_same_as_start do not guarantee complete scans, so conversion
|
||||
// must stay disabled for the iterator lifetime.
|
||||
min_tombstones_for_range_conversion_(
|
||||
active_mem != nullptr && !read_options.table_filter &&
|
||||
(expect_total_order_inner_iter_ || prefix_same_as_start_) &&
|
||||
HasFullTimestampVisibility(read_options)
|
||||
? mutable_cf_options.min_tombstones_for_range_conversion
|
||||
: 0),
|
||||
expose_blob_index_(expose_blob_index),
|
||||
allow_unprepared_value_(read_options.allow_unprepared_value),
|
||||
is_blob_(false),
|
||||
@@ -137,7 +178,7 @@ Status DBIter::GetProperty(std::string prop_name, std::string* prop) {
|
||||
} else if (prop_name == "rocksdb.iterator.is-value-pinned") {
|
||||
if (valid_) {
|
||||
*prop = (pin_thru_lifetime_ && iter_.Valid() &&
|
||||
iter_.value().data() == value_.data())
|
||||
iter_.value().data() == value_columns_state_.value().data())
|
||||
? "1"
|
||||
: "0";
|
||||
} else {
|
||||
@@ -183,6 +224,7 @@ void DBIter::Next() {
|
||||
bool ok = true;
|
||||
if (direction_ == kReverse) {
|
||||
is_key_seqnum_zero_ = false;
|
||||
ResetContiguousTombstoneTracking();
|
||||
if (!ReverseToForward()) {
|
||||
ok = false;
|
||||
}
|
||||
@@ -201,13 +243,7 @@ void DBIter::Next() {
|
||||
if (ok && iter_.Valid()) {
|
||||
ClearSavedValue();
|
||||
|
||||
if (prefix_same_as_start_) {
|
||||
assert(prefix_extractor_ != nullptr);
|
||||
const Slice prefix = prefix_.GetUserKey();
|
||||
FindNextUserEntry(true /* skipping the current user key */, &prefix);
|
||||
} else {
|
||||
FindNextUserEntry(true /* skipping the current user key */, nullptr);
|
||||
}
|
||||
FindNextUserEntry(true /* skipping the current user key */);
|
||||
} else {
|
||||
is_key_seqnum_zero_ = false;
|
||||
valid_ = false;
|
||||
@@ -218,11 +254,12 @@ void DBIter::Next() {
|
||||
}
|
||||
}
|
||||
|
||||
Status DBIter::BlobReader::RetrieveAndSetBlobValue(const Slice& user_key,
|
||||
const Slice& blob_index) {
|
||||
Status DBIter::BlobReader::RetrieveAndSetBlobValue(
|
||||
const Slice& user_key, const Slice& blob_index,
|
||||
bool allow_write_path_fallback) {
|
||||
assert(blob_value_.empty());
|
||||
|
||||
if (!version_) {
|
||||
if (!version_ && (!allow_write_path_fallback || !blob_file_cache_)) {
|
||||
return Status::Corruption("Encountered unexpected blob index.");
|
||||
}
|
||||
|
||||
@@ -237,19 +274,42 @@ Status DBIter::BlobReader::RetrieveAndSetBlobValue(const Slice& user_key,
|
||||
constexpr FilePrefetchBuffer* prefetch_buffer = nullptr;
|
||||
constexpr uint64_t* bytes_read = nullptr;
|
||||
|
||||
const Status s = version_->GetBlob(read_options, user_key, blob_index,
|
||||
prefetch_buffer, &blob_value_, bytes_read);
|
||||
if (!allow_write_path_fallback) {
|
||||
assert(version_ != nullptr);
|
||||
return version_->GetBlob(read_options, user_key, blob_index,
|
||||
prefetch_buffer, &blob_value_, bytes_read);
|
||||
}
|
||||
|
||||
BlobIndex blob_idx;
|
||||
Status s = blob_idx.DecodeFrom(blob_index);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
return Status::OK();
|
||||
return BlobFilePartitionManager::ResolveBlobDirectWriteIndex(
|
||||
read_options, user_key, blob_idx, version_, blob_file_cache_,
|
||||
prefetch_buffer, &blob_value_, bytes_read);
|
||||
}
|
||||
|
||||
BlobFetcher DBIter::BlobReader::CreateBlobFetcher() const {
|
||||
ReadOptions read_options;
|
||||
read_options.read_tier = read_tier_;
|
||||
read_options.verify_checksums = verify_checksums_;
|
||||
read_options.fill_cache = fill_cache_;
|
||||
read_options.io_activity = io_activity_;
|
||||
return BlobFetcher(version_, read_options, blob_file_cache_,
|
||||
allow_write_path_fallback_);
|
||||
}
|
||||
|
||||
bool DBIter::SetValueAndColumnsFromBlobImpl(const Slice& user_key,
|
||||
const Slice& blob_index) {
|
||||
const Status s = blob_reader_.RetrieveAndSetBlobValue(user_key, blob_index);
|
||||
// Keep the non-BDW iterator path on the pre-existing Version::GetBlob()
|
||||
// fast path. Only enable the direct-write fallback when this CF actually
|
||||
// has a write-path partition manager.
|
||||
const bool allow_write_path_fallback =
|
||||
cfh_ != nullptr && cfh_->cfd()->blob_partition_manager() != nullptr;
|
||||
const Status s = blob_reader_.RetrieveAndSetBlobValue(
|
||||
user_key, blob_index, allow_write_path_fallback);
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
valid_ = false;
|
||||
@@ -273,8 +333,8 @@ bool DBIter::SetValueAndColumnsFromBlob(const Slice& user_key,
|
||||
}
|
||||
|
||||
if (allow_unprepared_value_) {
|
||||
assert(value_.empty());
|
||||
assert(wide_columns_.empty());
|
||||
assert(value_columns_state_.value().empty());
|
||||
assert(value_columns_state_.wide_columns().empty());
|
||||
|
||||
assert(lazy_blob_index_.empty());
|
||||
lazy_blob_index_ = blob_index;
|
||||
@@ -286,22 +346,102 @@ bool DBIter::SetValueAndColumnsFromBlob(const Slice& user_key,
|
||||
}
|
||||
|
||||
bool DBIter::SetValueAndColumnsFromEntity(Slice slice) {
|
||||
assert(value_.empty());
|
||||
assert(wide_columns_.empty());
|
||||
auto& state = value_columns_state_;
|
||||
state.AssertReadyForEntity();
|
||||
|
||||
const Status s = WideColumnSerialization::Deserialize(slice, wide_columns_);
|
||||
// Fast path: if no blob columns, use the simpler Deserialize
|
||||
bool has_blob_columns = false;
|
||||
{
|
||||
const Status s_hbc =
|
||||
WideColumnSerialization::HasBlobColumns(slice, has_blob_columns);
|
||||
if (!s_hbc.ok()) {
|
||||
status_ = s_hbc;
|
||||
valid_ = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (LIKELY(!has_blob_columns)) {
|
||||
WideColumns& wide_columns = state.wide_columns();
|
||||
const Status s = WideColumnSerialization::Deserialize(slice, wide_columns);
|
||||
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
valid_ = false;
|
||||
wide_columns_.clear();
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
valid_ = false;
|
||||
state.ClearWideColumns();
|
||||
return false;
|
||||
}
|
||||
|
||||
state.MaybeSetValueFromMaterializedDefaultColumn();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Entity has blob columns.
|
||||
// First, copy the serialized data to the saved entity buffer so that column
|
||||
// name/value Slices remain valid after the internal iterator moves.
|
||||
// Guard: if slice already aliases that saved buffer (e.g., when called from
|
||||
// SetValueAndColumnsFromMergeResult), skip the redundant copy to
|
||||
// avoid self-aliased std::string::assign (undefined behavior).
|
||||
state.SaveEntitySliceIfNeeded(slice);
|
||||
|
||||
{
|
||||
Slice input_copy = state.PrepareForLazyEntityDeserialize();
|
||||
const Status s = WideColumnSerialization::DeserializeV2(
|
||||
input_copy, state.lazy_entity_columns(), state.lazy_blob_columns());
|
||||
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
valid_ = false;
|
||||
state.ClearLazyEntity();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator positions must expose fully prepared values and columns once
|
||||
// Valid() becomes true, so resolve and materialize all blob columns here.
|
||||
state.BindLazyEntity(saved_key_.GetUserKey());
|
||||
if (!MaterializeLazyEntityColumns()) {
|
||||
state.ClearLazyEntity();
|
||||
return false;
|
||||
}
|
||||
state.MaybeSetValueFromMaterializedDefaultColumn();
|
||||
|
||||
if (WideColumnsHelper::HasDefaultColumn(wide_columns_)) {
|
||||
value_ = WideColumnsHelper::GetDefaultColumn(wide_columns_);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DBIter::MaterializeLazyEntityColumns() const {
|
||||
const auto& state = value_columns_state_;
|
||||
if (state.lazy_entity_columns().empty() || !state.wide_columns().empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(state.lazy_entity_columns_mutex());
|
||||
if (state.lazy_entity_columns().empty() || !state.wide_columns().empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
DBIter* const mutable_this = const_cast<DBIter*>(this);
|
||||
auto& mutable_state = mutable_this->value_columns_state_;
|
||||
WideColumns materialized_columns;
|
||||
materialized_columns.reserve(state.lazy_entity_columns().size());
|
||||
for (const auto& col : state.lazy_entity_columns()) {
|
||||
materialized_columns.emplace_back(col.name(), col.value());
|
||||
}
|
||||
|
||||
for (const auto& blob_col : state.lazy_blob_columns()) {
|
||||
Slice resolved_value;
|
||||
const Status s = mutable_state.entity_blob_resolver().ResolveColumn(
|
||||
blob_col.first, &resolved_value);
|
||||
if (!s.ok()) {
|
||||
mutable_this->status_ = s;
|
||||
mutable_this->valid_ = false;
|
||||
mutable_state.wide_columns().clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
materialized_columns[blob_col.first].value() = resolved_value;
|
||||
}
|
||||
|
||||
mutable_state.wide_columns() = std::move(materialized_columns);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -314,7 +454,7 @@ bool DBIter::SetValueAndColumnsFromMergeResult(const Status& merge_status,
|
||||
}
|
||||
|
||||
if (result_type == kTypeWideColumnEntity) {
|
||||
if (!SetValueAndColumnsFromEntity(saved_value_)) {
|
||||
if (!SetValueAndColumnsFromEntity(value_columns_state_.saved_value())) {
|
||||
assert(!valid_);
|
||||
return false;
|
||||
}
|
||||
@@ -324,8 +464,9 @@ bool DBIter::SetValueAndColumnsFromMergeResult(const Status& merge_status,
|
||||
}
|
||||
|
||||
assert(result_type == kTypeValue);
|
||||
SetValueAndColumnsFromPlain(pinned_value_.data() ? pinned_value_
|
||||
: saved_value_);
|
||||
SetValueAndColumnsFromPlain(pinned_value_.data()
|
||||
? pinned_value_
|
||||
: value_columns_state_.saved_value());
|
||||
valid_ = true;
|
||||
return true;
|
||||
}
|
||||
@@ -352,7 +493,7 @@ bool DBIter::PrepareValue() {
|
||||
// POST: saved_key_ should have the next user key if valid_,
|
||||
// if the current entry is a result of merge
|
||||
// current_entry_is_merged_ => true
|
||||
// saved_value_ => the merged value
|
||||
// the saved merge buffer => the merged value
|
||||
//
|
||||
// NOTE: In between, saved_key_ can point to a user key that has
|
||||
// a delete marker or a sequence number higher than sequence_
|
||||
@@ -361,14 +502,13 @@ bool DBIter::PrepareValue() {
|
||||
// The prefix parameter, if not null, indicates that we need to iterate
|
||||
// within the prefix, and the iterator needs to be made invalid, if no
|
||||
// more entry for the prefix can be found.
|
||||
bool DBIter::FindNextUserEntry(bool skipping_saved_key, const Slice* prefix) {
|
||||
bool DBIter::FindNextUserEntry(bool skipping_saved_key) {
|
||||
PERF_TIMER_GUARD(find_next_user_entry_time);
|
||||
return FindNextUserEntryInternal(skipping_saved_key, prefix);
|
||||
return FindNextUserEntryInternal(skipping_saved_key);
|
||||
}
|
||||
|
||||
// Actual implementation of DBIter::FindNextUserEntry()
|
||||
bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
const Slice* prefix) {
|
||||
bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key) {
|
||||
// Loop until we hit an acceptable entry to yield
|
||||
assert(iter_.Valid());
|
||||
assert(status_.ok());
|
||||
@@ -421,12 +561,18 @@ bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
break;
|
||||
}
|
||||
|
||||
assert(prefix == nullptr || prefix_extractor_ != nullptr);
|
||||
if (prefix != nullptr &&
|
||||
prefix_extractor_->Transform(user_key_without_ts).compare(*prefix) !=
|
||||
0) {
|
||||
assert(prefix_same_as_start_);
|
||||
break;
|
||||
assert(!prefix_.has_value() || prefix_extractor_ != nullptr);
|
||||
if (!PrefixCheck(user_key_without_ts)) {
|
||||
// Insert any pending tombstone run using the last tracked delete
|
||||
// (saved_key_) as the end key. We cannot use the current key's
|
||||
// prefix as the boundary because bloom filters may have hidden
|
||||
// entire prefixes between the seek prefix and the current key.
|
||||
// The tombstone covers n-1 of n deletes; the last remains as a
|
||||
// point delete.
|
||||
FlushPendingTombstoneRun(saved_key_.GetUserKey());
|
||||
if (prefix_same_as_start_) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (TooManyInternalKeysSkipped()) {
|
||||
@@ -474,6 +620,15 @@ bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
skipping_saved_key = true;
|
||||
PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
|
||||
MarkMemtableForFlushForPerOpTrigger(mem_hidden_op_scanned);
|
||||
// Track contiguous tombstones for range conversion.
|
||||
// Skip if outside seek prefix — the top-of-loop check
|
||||
// flushed any pending run, but we must also avoid starting
|
||||
// a new run outside the prefix.
|
||||
if (min_tombstones_for_range_conversion_ > 0 &&
|
||||
PrefixCheck(user_key_without_ts)) {
|
||||
TrackContiguousTombstone(ikey_.user_key,
|
||||
/*always_update_first_key=*/false);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kTypeValue:
|
||||
@@ -483,6 +638,7 @@ bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
if (!PrepareValueInternal()) {
|
||||
return false;
|
||||
}
|
||||
FlushPendingTombstoneRun(ikey_.user_key);
|
||||
if (timestamp_lb_) {
|
||||
saved_key_.SetInternalKey(ikey_);
|
||||
} else {
|
||||
@@ -516,6 +672,7 @@ bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
if (!PrepareValueInternal()) {
|
||||
return false;
|
||||
}
|
||||
FlushPendingTombstoneRun(ikey_.user_key);
|
||||
saved_key_.SetUserKey(
|
||||
ikey_.user_key,
|
||||
!pin_thru_lifetime_ || !iter_.iter()->IsKeyPinned() /* copy */);
|
||||
@@ -617,6 +774,28 @@ bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
}
|
||||
} while (iter_.Valid());
|
||||
|
||||
// If we accumulated tombstones, insert the range tombstone. Use
|
||||
// iterate_upper_bound_ if within the seek prefix, otherwise fall back to
|
||||
// saved_key_ (the last tracked delete, covering n-1 deletes).
|
||||
if (contiguous_tombstone_count_ > 0 && iter_.status().ok()) {
|
||||
if (iterate_upper_bound_ != nullptr && PrefixCheck(*iterate_upper_bound_)) {
|
||||
if (timestamp_size_ == 0) {
|
||||
MaybeInsertRangeTombstone(*iterate_upper_bound_);
|
||||
} else {
|
||||
// iterate_upper_bound_ is a plain user key without a timestamp
|
||||
// suffix. Pad with min timestamp so it sorts after all entries with
|
||||
// this user key, preserving the exclusive bound semantics.
|
||||
std::string end_key_with_ts;
|
||||
AppendKeyWithMinTimestamp(&end_key_with_ts, *iterate_upper_bound_,
|
||||
timestamp_size_);
|
||||
MaybeInsertRangeTombstone(end_key_with_ts);
|
||||
}
|
||||
} else if (prefix_.has_value()) {
|
||||
MaybeInsertRangeTombstone(saved_key_.GetUserKey());
|
||||
}
|
||||
}
|
||||
ResetContiguousTombstoneTracking();
|
||||
|
||||
valid_ = false;
|
||||
return iter_.status().ok();
|
||||
}
|
||||
@@ -626,7 +805,7 @@ bool DBIter::FindNextUserEntryInternal(bool skipping_saved_key,
|
||||
// PRE: iter_.key() points to the first merge type entry
|
||||
// saved_key_ stores the user key
|
||||
// iter_.PrepareValue() has been called
|
||||
// POST: saved_value_ has the merged value for the user key
|
||||
// POST: the saved merge buffer has the merged value for the user key
|
||||
// iter_ points to the next entry (or invalid)
|
||||
bool DBIter::MergeValuesNewToOld() {
|
||||
if (!merge_operator_) {
|
||||
@@ -676,7 +855,7 @@ bool DBIter::MergeValuesNewToOld() {
|
||||
value = ParsePackedValueForValue(value);
|
||||
}
|
||||
// hit a put or put equivalent, merge the put value with operands and
|
||||
// store the final result in saved_value_. We are done!
|
||||
// store the final result in the saved merge buffer. We are done!
|
||||
if (!MergeWithPlainBaseValue(value, ikey.user_key)) {
|
||||
return false;
|
||||
}
|
||||
@@ -756,19 +935,20 @@ void DBIter::Prev() {
|
||||
ResetInternalKeysSkippedCounter();
|
||||
bool ok = true;
|
||||
if (direction_ == kForward) {
|
||||
ResetContiguousTombstoneTracking();
|
||||
if (!ReverseToBackward()) {
|
||||
ok = false;
|
||||
}
|
||||
// Transitioning to reverse: current key is the end bound
|
||||
if (ok && min_tombstones_for_range_conversion_ > 0) {
|
||||
range_tomb_end_key_.SetUserKey(saved_key_.GetUserKey(),
|
||||
!saved_key_.IsKeyPinned());
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
ClearSavedValue();
|
||||
|
||||
Slice prefix;
|
||||
if (prefix_same_as_start_) {
|
||||
assert(prefix_extractor_ != nullptr);
|
||||
prefix = prefix_.GetUserKey();
|
||||
}
|
||||
PrevInternal(prefix_same_as_start_ ? &prefix : nullptr);
|
||||
PrevInternal();
|
||||
}
|
||||
|
||||
if (statistics_ != nullptr) {
|
||||
@@ -860,22 +1040,34 @@ bool DBIter::ReverseToBackward() {
|
||||
return FindUserKeyBeforeSavedKey();
|
||||
}
|
||||
|
||||
void DBIter::PrevInternal(const Slice* prefix) {
|
||||
void DBIter::PrevInternal() {
|
||||
// Capture saved_key_ (previous live key) into range_tomb_end_key_ before
|
||||
// saved_key_ is overwritten below.
|
||||
if (min_tombstones_for_range_conversion_ > 0) {
|
||||
range_tomb_end_key_.Swap(saved_key_);
|
||||
}
|
||||
|
||||
while (iter_.Valid()) {
|
||||
saved_key_.SetUserKey(
|
||||
ExtractUserKey(iter_.key()),
|
||||
!iter_.iter()->IsKeyPinned() || !pin_thru_lifetime_ /* copy */);
|
||||
|
||||
assert(prefix == nullptr || prefix_extractor_ != nullptr);
|
||||
if (prefix != nullptr &&
|
||||
prefix_extractor_
|
||||
->Transform(StripTimestampFromUserKey(saved_key_.GetUserKey(),
|
||||
timestamp_size_))
|
||||
.compare(*prefix) != 0) {
|
||||
assert(prefix_same_as_start_);
|
||||
// Current key does not have the same prefix as start
|
||||
valid_ = false;
|
||||
return;
|
||||
assert(!prefix_.has_value() || prefix_extractor_ != nullptr);
|
||||
Slice saved_key_without_ts =
|
||||
StripTimestampFromUserKey(saved_key_.GetUserKey(), timestamp_size_);
|
||||
// When prefix filtering is active, insert any pending tombstone run
|
||||
// before we leave the seek prefix.
|
||||
if (!PrefixCheck(saved_key_without_ts)) {
|
||||
// Insert any pending tombstone run before leaving the seek prefix.
|
||||
// Only insert if end_key (previous live key) is within the seek prefix.
|
||||
if (range_tomb_end_key_.Size() > 0) {
|
||||
FlushPendingTombstoneRun(range_tomb_end_key_.GetUserKey(),
|
||||
/*check_prefix_match=*/true);
|
||||
}
|
||||
if (prefix_same_as_start_) {
|
||||
valid_ = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
assert(iterate_lower_bound_ == nullptr || iter_.MayBeOutOfLowerBound() ||
|
||||
@@ -887,14 +1079,39 @@ void DBIter::PrevInternal(const Slice* prefix) {
|
||||
saved_key_.GetUserKey(), /*a_has_ts=*/true, *iterate_lower_bound_,
|
||||
/*b_has_ts=*/false) < 0) {
|
||||
// We've iterated earlier than the user-specified lower bound.
|
||||
if (range_tomb_end_key_.Size() > 0) {
|
||||
FlushPendingTombstoneRun(range_tomb_end_key_.GetUserKey(),
|
||||
/*check_prefix_match=*/true);
|
||||
}
|
||||
valid_ = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FindValueForCurrentKey()) { // assigns valid_
|
||||
bool found_visible = false;
|
||||
if (!FindValueForCurrentKey(found_visible)) { // assigns valid_
|
||||
return;
|
||||
}
|
||||
|
||||
// Track contiguous tombstones for reverse range tombstone conversion.
|
||||
// Only track when FindValueForCurrentKey found a visible entry
|
||||
// (found_visible == true). When no visible entry exists (all seqno >
|
||||
// snapshot), the key doesn't exist at this snapshot and must not be
|
||||
// treated as a tombstone. Additionally, ikey_ is only updated when a
|
||||
// visible entry is found, so reading ikey_.sequence without this guard
|
||||
// would use a stale value.
|
||||
if (min_tombstones_for_range_conversion_ > 0 &&
|
||||
range_tomb_end_key_.Size() > 0 && timestamp_lb_ == nullptr) {
|
||||
if (!valid_ && found_visible && PrefixCheck(saved_key_without_ts)) {
|
||||
// Key was deleted and is within the seek prefix — track it.
|
||||
TrackContiguousTombstone(saved_key_.GetUserKey(),
|
||||
/*always_update_first_key=*/true);
|
||||
} else if (valid_) {
|
||||
// Live key breaks the run.
|
||||
FlushPendingTombstoneRun(range_tomb_end_key_.GetUserKey(),
|
||||
/*check_prefix_match=*/true);
|
||||
}
|
||||
}
|
||||
|
||||
// Whether or not we found a value for current key, we need iter_ to end up
|
||||
// on a smaller key.
|
||||
if (!FindUserKeyBeforeSavedKey()) {
|
||||
@@ -911,6 +1128,11 @@ void DBIter::PrevInternal(const Slice* prefix) {
|
||||
}
|
||||
}
|
||||
|
||||
if (range_tomb_end_key_.Size() > 0) {
|
||||
FlushPendingTombstoneRun(range_tomb_end_key_.GetUserKey(),
|
||||
/*check_prefix_match=*/true);
|
||||
}
|
||||
|
||||
// We haven't found any key - iterator is not valid
|
||||
valid_ = false;
|
||||
}
|
||||
@@ -920,13 +1142,19 @@ void DBIter::PrevInternal(const Slice* prefix) {
|
||||
// value for it, or executes a merge, or determines that the value was deleted.
|
||||
// Sets valid_ to true if the value is found and is ready to be presented to
|
||||
// the user through value().
|
||||
// Sets valid_ to false if the value was deleted, and we should try another key.
|
||||
// Sets valid_ to false if the value was deleted or no visible entry exists.
|
||||
// Sets ikey_ to the last visible entry's internal key. When found_visible
|
||||
// is false, ikey_ is not updated and may contain stale data.
|
||||
// Sets found_visible to true if at least one entry passed the IsVisible()
|
||||
// check (seqno <= snapshot). When false, no entry was visible — the key
|
||||
// does not exist at this snapshot and should not be treated as a tombstone.
|
||||
// Returns false if an error occurred, and !status().ok() and !valid_.
|
||||
//
|
||||
// PRE: iter_ is positioned on the last entry with user key equal to saved_key_.
|
||||
// POST: iter_ is positioned on one of the entries equal to saved_key_, or on
|
||||
// the entry just before them, or on the entry just after them.
|
||||
bool DBIter::FindValueForCurrentKey() {
|
||||
bool DBIter::FindValueForCurrentKey(bool& found_visible) {
|
||||
found_visible = false;
|
||||
assert(iter_.Valid());
|
||||
merge_context_.Clear();
|
||||
current_entry_is_merged_ = false;
|
||||
@@ -973,6 +1201,10 @@ bool DBIter::FindValueForCurrentKey() {
|
||||
break;
|
||||
}
|
||||
|
||||
// Entry survived the visibility check — at least one visible version
|
||||
// exists for this user key.
|
||||
found_visible = true;
|
||||
|
||||
if (!ts.empty()) {
|
||||
saved_timestamp_.assign(ts.data(), ts.size());
|
||||
}
|
||||
@@ -993,10 +1225,6 @@ bool DBIter::FindValueForCurrentKey() {
|
||||
}
|
||||
|
||||
if (timestamp_lb_ != nullptr) {
|
||||
// Only needed when timestamp_lb_ is not null
|
||||
[[maybe_unused]] const bool ret = ParseKey(&ikey_);
|
||||
// Since the preceding ParseKey(&ikey) succeeds, so must this.
|
||||
assert(ret);
|
||||
saved_key_.SetInternalKey(ikey);
|
||||
} else if (user_comparator_.Compare(ikey.user_key,
|
||||
saved_key_.GetUserKey()) < 0) {
|
||||
@@ -1005,6 +1233,8 @@ bool DBIter::FindValueForCurrentKey() {
|
||||
!pin_thru_lifetime_ || !iter_.iter()->IsKeyPinned() /* copy */);
|
||||
}
|
||||
|
||||
// Ensure ikey_ is only set to VISIBLE keys.
|
||||
ikey_ = ikey;
|
||||
valid_entry_seen = true;
|
||||
last_key_entry_type = ikey.type;
|
||||
switch (last_key_entry_type) {
|
||||
@@ -1208,6 +1438,11 @@ bool DBIter::FindValueForCurrentKeyUsingSeek() {
|
||||
iter_.Next();
|
||||
}
|
||||
|
||||
// Keep ikey_ in sync with the entry found by the seek.
|
||||
ikey_ = ikey;
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
"DBIter::FindValueForCurrentKeyUsingSeek:ikey_updated", &ikey_);
|
||||
|
||||
if (ikey.type == kTypeDeletion || ikey.type == kTypeSingleDeletion ||
|
||||
kTypeDeletionWithTimestamp == ikey.type) {
|
||||
if (timestamp_lb_ == nullptr) {
|
||||
@@ -1357,7 +1592,7 @@ bool DBIter::MergeWithNoBaseValue(const Slice& user_key) {
|
||||
merge_operator_, user_key, MergeHelper::kNoBaseValue,
|
||||
merge_context_.GetOperands(), logger_, statistics_, clock_,
|
||||
/* update_num_ops_stats */ true, /* op_failure_scope */ nullptr,
|
||||
&saved_value_, &pinned_value_, &result_type);
|
||||
&value_columns_state_.saved_value(), &pinned_value_, &result_type);
|
||||
return SetValueAndColumnsFromMergeResult(s, result_type);
|
||||
}
|
||||
|
||||
@@ -1370,7 +1605,7 @@ bool DBIter::MergeWithPlainBaseValue(const Slice& value,
|
||||
merge_operator_, user_key, MergeHelper::kPlainBaseValue, value,
|
||||
merge_context_.GetOperands(), logger_, statistics_, clock_,
|
||||
/* update_num_ops_stats */ true, /* op_failure_scope */ nullptr,
|
||||
&saved_value_, &pinned_value_, &result_type);
|
||||
&value_columns_state_.saved_value(), &pinned_value_, &result_type);
|
||||
return SetValueAndColumnsFromMergeResult(s, result_type);
|
||||
}
|
||||
|
||||
@@ -1385,7 +1620,10 @@ bool DBIter::MergeWithBlobBaseValue(const Slice& blob_index,
|
||||
return false;
|
||||
}
|
||||
|
||||
const Status s = blob_reader_.RetrieveAndSetBlobValue(user_key, blob_index);
|
||||
const bool allow_write_path_fallback =
|
||||
cfh_ != nullptr && cfh_->cfd()->blob_partition_manager() != nullptr;
|
||||
const Status s = blob_reader_.RetrieveAndSetBlobValue(
|
||||
user_key, blob_index, allow_write_path_fallback);
|
||||
if (!s.ok()) {
|
||||
status_ = s;
|
||||
valid_ = false;
|
||||
@@ -1405,14 +1643,28 @@ bool DBIter::MergeWithBlobBaseValue(const Slice& blob_index,
|
||||
|
||||
bool DBIter::MergeWithWideColumnBaseValue(const Slice& entity,
|
||||
const Slice& user_key) {
|
||||
// Resolve V2 entity blob columns if present, since TimedFullMerge only
|
||||
// supports V1 format.
|
||||
BlobFetcher blob_fetcher = blob_reader_.CreateBlobFetcher();
|
||||
std::string resolved_entity;
|
||||
Slice effective_entity;
|
||||
Status s_resolve = WideColumnSerialization::ResolveEntityForMerge(
|
||||
entity, user_key, &blob_fetcher, nullptr /* prefetch_buffers */,
|
||||
resolved_entity, effective_entity);
|
||||
if (!s_resolve.ok()) {
|
||||
status_ = std::move(s_resolve);
|
||||
valid_ = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// `op_failure_scope` (an output parameter) is not provided (set to nullptr)
|
||||
// since a failure must be propagated regardless of its value.
|
||||
ValueType result_type;
|
||||
const Status s = MergeHelper::TimedFullMerge(
|
||||
merge_operator_, user_key, MergeHelper::kWideBaseValue, entity,
|
||||
Status s = MergeHelper::TimedFullMerge(
|
||||
merge_operator_, user_key, MergeHelper::kWideBaseValue, effective_entity,
|
||||
merge_context_.GetOperands(), logger_, statistics_, clock_,
|
||||
/* update_num_ops_stats */ true, /* op_failure_scope */ nullptr,
|
||||
&saved_value_, &pinned_value_, &result_type);
|
||||
&value_columns_state_.saved_value(), &pinned_value_, &result_type);
|
||||
return SetValueAndColumnsFromMergeResult(s, result_type);
|
||||
}
|
||||
|
||||
@@ -1486,6 +1738,103 @@ bool DBIter::FindUserKeyBeforeSavedKey() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void DBIter::TrackContiguousTombstone(const Slice& user_key,
|
||||
bool always_update_first_key) {
|
||||
if (always_update_first_key || contiguous_tombstone_count_ == 0) {
|
||||
range_tomb_first_key_.SetUserKey(user_key, true /* copy */);
|
||||
}
|
||||
contiguous_tombstone_count_++;
|
||||
}
|
||||
|
||||
void DBIter::FlushPendingTombstoneRun(const Slice& end_key,
|
||||
bool check_prefix_match) {
|
||||
if (contiguous_tombstone_count_ == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (check_prefix_match) {
|
||||
Slice end_key_without_ts =
|
||||
StripTimestampFromUserKey(end_key, timestamp_size_);
|
||||
if (PrefixCheck(end_key_without_ts)) {
|
||||
MaybeInsertRangeTombstone(end_key);
|
||||
}
|
||||
} else {
|
||||
MaybeInsertRangeTombstone(end_key);
|
||||
}
|
||||
|
||||
ResetContiguousTombstoneTracking();
|
||||
}
|
||||
|
||||
void DBIter::MaybeInsertRangeTombstone(const Slice& end_key) {
|
||||
if (contiguous_tombstone_count_ < min_tombstones_for_range_conversion_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (active_mem_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert(PrefixCheck(range_tomb_first_key_.GetUserKey()));
|
||||
assert(PrefixCheck(end_key));
|
||||
assert(user_comparator_.Compare(range_tomb_first_key_.GetUserKey(),
|
||||
end_key) <= 0);
|
||||
|
||||
auto earliest_seq = active_mem_->GetEarliestSequenceNumber();
|
||||
// Skip if the iterator's snapshot predates the memtable. Otherwise entries
|
||||
// added with seqno between sequence_ and earliest_seq will be unintentionally
|
||||
// covered.
|
||||
if (sequence_ < earliest_seq) {
|
||||
RecordTick(statistics_, READ_PATH_RANGE_TOMBSTONES_DISCARDED);
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert at the read sequence so the converted tombstone is visible only
|
||||
// to readers that could already observe the deletion run.
|
||||
SequenceNumber insert_seq = sequence_;
|
||||
|
||||
// Skip if the insertion seq could shadow prepared-but-uncommitted writes.
|
||||
if (read_callback_ != nullptr &&
|
||||
insert_seq >= read_callback_->min_uncommitted()) {
|
||||
RecordTick(statistics_, READ_PATH_RANGE_TOMBSTONES_DISCARDED);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the memtable already has a range tombstone covering [start, end).
|
||||
{
|
||||
ReadOptions ro;
|
||||
// Assumption is that this should be relatively cheap as other read requests
|
||||
// should be building the cached core fragmented list.
|
||||
std::unique_ptr<FragmentedRangeTombstoneIterator> range_iter(
|
||||
active_mem_->NewRangeTombstoneIterator(ro, sequence_,
|
||||
false /* immutable_memtable */));
|
||||
if (range_iter) {
|
||||
range_iter->Seek(range_tomb_first_key_.GetUserKey());
|
||||
if (range_iter->Valid() &&
|
||||
user_comparator_.Compare(range_iter->start_key(),
|
||||
range_tomb_first_key_.GetUserKey()) <= 0 &&
|
||||
user_comparator_.Compare(range_iter->end_key(), end_key) >= 0) {
|
||||
RecordTick(statistics_, READ_PATH_RANGE_TOMBSTONES_DISCARDED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(cfh_ != nullptr);
|
||||
if (active_mem_->AddLogicallyRedundantRangeTombstone(
|
||||
insert_seq, range_tomb_first_key_.GetUserKey(), end_key,
|
||||
cfh_->cfd()->GetIngestSstLock())) {
|
||||
RecordTick(statistics_, READ_PATH_RANGE_TOMBSTONES_INSERTED);
|
||||
ROCKS_LOG_DEBUG(logger_,
|
||||
"Inserted range tombstone [%s, %s) @ seq %" PRIu64
|
||||
" (count=%" PRIu32 ", snapshot=%" PRIu64 ")",
|
||||
range_tomb_first_key_.GetUserKey().ToString(true).c_str(),
|
||||
end_key.ToString(true).c_str(), insert_seq,
|
||||
contiguous_tombstone_count_, sequence_);
|
||||
} else {
|
||||
RecordTick(statistics_, READ_PATH_RANGE_TOMBSTONES_DISCARDED);
|
||||
}
|
||||
}
|
||||
|
||||
bool DBIter::TooManyInternalKeysSkipped(bool increment) {
|
||||
if ((max_skippable_internal_keys_ > 0) &&
|
||||
(num_internal_keys_skipped_ > max_skippable_internal_keys_)) {
|
||||
@@ -1703,10 +2052,8 @@ void DBIter::Seek(const Slice& target) {
|
||||
}
|
||||
|
||||
status_ = Status::OK();
|
||||
ReleaseTempPinnedData();
|
||||
ResetBlobData();
|
||||
ResetValueAndColumns();
|
||||
ResetInternalKeysSkippedCounter();
|
||||
ResetSeekState();
|
||||
|
||||
MarkMemtableForFlushForAvgTrigger();
|
||||
|
||||
// Seek the inner iterator based on the target key.
|
||||
@@ -1727,20 +2074,13 @@ void DBIter::Seek(const Slice& target) {
|
||||
// Now the inner iterator is placed to the target position. From there,
|
||||
// we need to find out the next key that is visible to the user.
|
||||
ClearSavedValue();
|
||||
if (prefix_same_as_start_) {
|
||||
// The case where the iterator needs to be invalidated if it has exhausted
|
||||
// keys within the same prefix of the seek key.
|
||||
assert(prefix_extractor_ != nullptr);
|
||||
Slice target_prefix = prefix_extractor_->Transform(target);
|
||||
FindNextUserEntry(false /* not skipping saved_key */,
|
||||
&target_prefix /* prefix */);
|
||||
if (valid_) {
|
||||
// Remember the prefix of the seek key for the future Next() call to
|
||||
// check.
|
||||
prefix_.SetUserKey(target_prefix);
|
||||
}
|
||||
} else {
|
||||
FindNextUserEntry(false /* not skipping saved_key */, nullptr);
|
||||
if (ShouldSetPrefix(target)) {
|
||||
prefix_.emplace();
|
||||
prefix_->SetUserKey(prefix_extractor_->Transform(target));
|
||||
}
|
||||
FindNextUserEntry(false /* not skipping saved_key */);
|
||||
if (!valid_) {
|
||||
prefix_.reset();
|
||||
}
|
||||
if (!valid_) {
|
||||
return;
|
||||
@@ -1780,10 +2120,8 @@ void DBIter::SeekForPrev(const Slice& target) {
|
||||
}
|
||||
|
||||
status_ = Status::OK();
|
||||
ReleaseTempPinnedData();
|
||||
ResetBlobData();
|
||||
ResetValueAndColumns();
|
||||
ResetInternalKeysSkippedCounter();
|
||||
ResetSeekState();
|
||||
|
||||
MarkMemtableForFlushForAvgTrigger();
|
||||
|
||||
// Seek the inner iterator based on the target key.
|
||||
@@ -1803,19 +2141,18 @@ void DBIter::SeekForPrev(const Slice& target) {
|
||||
// we need to find out the first key that is visible to the user in the
|
||||
// backward direction.
|
||||
ClearSavedValue();
|
||||
if (prefix_same_as_start_) {
|
||||
// The case where the iterator needs to be invalidated if it has exhausted
|
||||
// keys within the same prefix of the seek key.
|
||||
assert(prefix_extractor_ != nullptr);
|
||||
Slice target_prefix = prefix_extractor_->Transform(target);
|
||||
PrevInternal(&target_prefix);
|
||||
if (valid_) {
|
||||
// Remember the prefix of the seek key for the future Prev() call to
|
||||
// check.
|
||||
prefix_.SetUserKey(target_prefix);
|
||||
}
|
||||
} else {
|
||||
PrevInternal(nullptr);
|
||||
if (ShouldSetPrefix(target)) {
|
||||
prefix_.emplace();
|
||||
prefix_->SetUserKey(prefix_extractor_->Transform(target));
|
||||
}
|
||||
PrevInternal();
|
||||
if (!valid_) {
|
||||
prefix_.reset();
|
||||
}
|
||||
// Set end key for first Prev() call's tombstone tracking
|
||||
if (valid_ && min_tombstones_for_range_conversion_ > 0) {
|
||||
range_tomb_end_key_.SetUserKey(saved_key_.GetUserKey(),
|
||||
!saved_key_.IsKeyPinned());
|
||||
}
|
||||
|
||||
// Report stats and perf context.
|
||||
@@ -1842,10 +2179,8 @@ void DBIter::SeekToFirst() {
|
||||
// if iterator is empty, this status_ could be unchecked.
|
||||
status_.PermitUncheckedError();
|
||||
direction_ = kForward;
|
||||
ReleaseTempPinnedData();
|
||||
ResetBlobData();
|
||||
ResetValueAndColumns();
|
||||
ResetInternalKeysSkippedCounter();
|
||||
ResetSeekState();
|
||||
|
||||
MarkMemtableForFlushForAvgTrigger();
|
||||
ClearSavedValue();
|
||||
is_key_seqnum_zero_ = false;
|
||||
@@ -1860,8 +2195,8 @@ void DBIter::SeekToFirst() {
|
||||
saved_key_.SetUserKey(
|
||||
ExtractUserKey(iter_.key()),
|
||||
!iter_.iter()->IsKeyPinned() || !pin_thru_lifetime_ /* copy */);
|
||||
FindNextUserEntry(false /* not skipping saved_key */,
|
||||
nullptr /* no prefix check */);
|
||||
assert(!prefix_.has_value());
|
||||
FindNextUserEntry(false /* not skipping saved_key */);
|
||||
if (statistics_ != nullptr) {
|
||||
if (valid_) {
|
||||
RecordTick(statistics_, NUMBER_DB_SEEK_FOUND);
|
||||
@@ -1872,10 +2207,13 @@ void DBIter::SeekToFirst() {
|
||||
} else {
|
||||
valid_ = false;
|
||||
}
|
||||
if (valid_ && prefix_same_as_start_) {
|
||||
assert(prefix_extractor_ != nullptr);
|
||||
prefix_.SetUserKey(prefix_extractor_->Transform(
|
||||
StripTimestampFromUserKey(saved_key_.GetUserKey(), timestamp_size_)));
|
||||
if (valid_) {
|
||||
Slice user_key_without_ts =
|
||||
StripTimestampFromUserKey(saved_key_.GetUserKey(), timestamp_size_);
|
||||
if (ShouldSetPrefix(user_key_without_ts)) {
|
||||
prefix_.emplace();
|
||||
prefix_->SetUserKey(prefix_extractor_->Transform(user_key_without_ts));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1906,19 +2244,27 @@ void DBIter::SeekToLast() {
|
||||
// if iterator is empty, this status_ could be unchecked.
|
||||
status_.PermitUncheckedError();
|
||||
direction_ = kReverse;
|
||||
ReleaseTempPinnedData();
|
||||
ResetBlobData();
|
||||
ResetValueAndColumns();
|
||||
ResetInternalKeysSkippedCounter();
|
||||
ResetSeekState();
|
||||
|
||||
MarkMemtableForFlushForAvgTrigger();
|
||||
ClearSavedValue();
|
||||
is_key_seqnum_zero_ = false;
|
||||
|
||||
// Clear stale saved_key_ so PrevInternal()'s Swap does not pollute
|
||||
// range_tomb_end_key_ with a key from a previous seek operation.
|
||||
saved_key_.Clear();
|
||||
|
||||
{
|
||||
PERF_TIMER_GUARD(seek_internal_seek_time);
|
||||
iter_.SeekToLast();
|
||||
}
|
||||
PrevInternal(nullptr);
|
||||
assert(!prefix_.has_value());
|
||||
PrevInternal();
|
||||
// Set end key for first Prev() call's tombstone tracking
|
||||
if (valid_ && min_tombstones_for_range_conversion_ > 0) {
|
||||
range_tomb_end_key_.SetUserKey(saved_key_.GetUserKey(),
|
||||
!saved_key_.IsKeyPinned());
|
||||
}
|
||||
if (statistics_ != nullptr) {
|
||||
RecordTick(statistics_, NUMBER_DB_SEEK);
|
||||
if (valid_) {
|
||||
@@ -1927,10 +2273,13 @@ void DBIter::SeekToLast() {
|
||||
PERF_COUNTER_ADD(iter_read_bytes, key().size() + value().size());
|
||||
}
|
||||
}
|
||||
if (valid_ && prefix_same_as_start_) {
|
||||
assert(prefix_extractor_ != nullptr);
|
||||
prefix_.SetUserKey(prefix_extractor_->Transform(
|
||||
StripTimestampFromUserKey(saved_key_.GetUserKey(), timestamp_size_)));
|
||||
if (valid_) {
|
||||
Slice user_key_without_ts =
|
||||
StripTimestampFromUserKey(saved_key_.GetUserKey(), timestamp_size_);
|
||||
if (ShouldSetPrefix(user_key_without_ts)) {
|
||||
prefix_.emplace();
|
||||
prefix_->SetUserKey(prefix_extractor_->Transform(user_key_without_ts));
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
+243
-42
@@ -9,9 +9,17 @@
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "db/blob/blob_fetcher.h"
|
||||
#include "db/blob/blob_index.h"
|
||||
#include "db/db_impl/db_impl.h"
|
||||
#include "db/wide/read_path_blob_resolver.h"
|
||||
#include "db/wide/wide_columns_helper.h"
|
||||
#include "memory/arena.h"
|
||||
#include "options/cf_options.h"
|
||||
#include "rocksdb/db.h"
|
||||
@@ -21,6 +29,7 @@
|
||||
#include "util/autovector.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
class BlobFileCache;
|
||||
class Version;
|
||||
|
||||
// This file declares the factory functions of DBIter, in its original form
|
||||
@@ -186,13 +195,14 @@ class DBIter final : public Iterator {
|
||||
Slice value() const override {
|
||||
assert(valid_);
|
||||
|
||||
return value_;
|
||||
return value_columns_state_.value();
|
||||
}
|
||||
|
||||
const WideColumns& columns() const override {
|
||||
assert(valid_);
|
||||
|
||||
return wide_columns_;
|
||||
assert(!value_columns_state_.HasLazyEntityColumns() ||
|
||||
value_columns_state_.HasMaterializedColumns());
|
||||
return value_columns_state_.wide_columns();
|
||||
}
|
||||
|
||||
Status status() const override {
|
||||
@@ -256,17 +266,23 @@ class DBIter final : public Iterator {
|
||||
public:
|
||||
BlobReader(const Version* version, ReadTier read_tier,
|
||||
bool verify_checksums, bool fill_cache,
|
||||
Env::IOActivity io_activity)
|
||||
Env::IOActivity io_activity, BlobFileCache* blob_file_cache,
|
||||
bool allow_write_path_fallback)
|
||||
: version_(version),
|
||||
read_tier_(read_tier),
|
||||
verify_checksums_(verify_checksums),
|
||||
fill_cache_(fill_cache),
|
||||
io_activity_(io_activity) {}
|
||||
io_activity_(io_activity),
|
||||
blob_file_cache_(blob_file_cache),
|
||||
allow_write_path_fallback_(allow_write_path_fallback) {}
|
||||
|
||||
const Slice& GetBlobValue() const { return blob_value_; }
|
||||
Status RetrieveAndSetBlobValue(const Slice& user_key,
|
||||
const Slice& blob_index);
|
||||
const Slice& blob_index,
|
||||
bool allow_write_path_fallback);
|
||||
void ResetBlobValue() { blob_value_.Reset(); }
|
||||
// Create a BlobFetcher with the same read options as this BlobReader.
|
||||
BlobFetcher CreateBlobFetcher() const;
|
||||
|
||||
private:
|
||||
PinnableSlice blob_value_;
|
||||
@@ -275,6 +291,147 @@ class DBIter final : public Iterator {
|
||||
bool verify_checksums_;
|
||||
bool fill_cache_;
|
||||
Env::IOActivity io_activity_;
|
||||
// Cache used by the write-path fallback for in-flight direct-write blob
|
||||
// files that are not yet reachable through Version.
|
||||
BlobFileCache* blob_file_cache_;
|
||||
bool allow_write_path_fallback_;
|
||||
};
|
||||
|
||||
// Groups the current iterator result together with the backing storage and
|
||||
// lazy entity-resolution metadata it depends on. Resetting this object drops
|
||||
// all aliases into the saved entity buffer and resolver cache at once.
|
||||
class ValueColumnsState {
|
||||
public:
|
||||
ValueColumnsState(const Version* version, const ReadOptions& read_options,
|
||||
ColumnFamilyHandleImpl* cfh)
|
||||
: entity_blob_resolver_(
|
||||
version, read_options.read_tier, read_options.verify_checksums,
|
||||
read_options.fill_cache, read_options.io_activity,
|
||||
cfh ? cfh->cfd()->blob_file_cache() : nullptr,
|
||||
cfh != nullptr &&
|
||||
cfh->cfd()->blob_partition_manager() != nullptr) {}
|
||||
|
||||
Slice& value() { return value_; }
|
||||
const Slice& value() const { return value_; }
|
||||
WideColumns& wide_columns() { return wide_columns_; }
|
||||
const WideColumns& wide_columns() const { return wide_columns_; }
|
||||
bool HasMaterializedColumns() const { return !wide_columns_.empty(); }
|
||||
bool HasLazyEntityColumns() const { return !lazy_entity_columns_.empty(); }
|
||||
|
||||
std::string& saved_value() { return saved_value_; }
|
||||
const std::string& saved_value() const { return saved_value_; }
|
||||
|
||||
std::vector<WideColumn>& lazy_entity_columns() {
|
||||
return lazy_entity_columns_;
|
||||
}
|
||||
const std::vector<WideColumn>& lazy_entity_columns() const {
|
||||
return lazy_entity_columns_;
|
||||
}
|
||||
|
||||
std::vector<std::pair<size_t, BlobIndex>>& lazy_blob_columns() {
|
||||
return lazy_blob_columns_;
|
||||
}
|
||||
const std::vector<std::pair<size_t, BlobIndex>>& lazy_blob_columns() const {
|
||||
return lazy_blob_columns_;
|
||||
}
|
||||
|
||||
ReadPathBlobResolver& entity_blob_resolver() {
|
||||
return entity_blob_resolver_;
|
||||
}
|
||||
const ReadPathBlobResolver& entity_blob_resolver() const {
|
||||
return entity_blob_resolver_;
|
||||
}
|
||||
|
||||
std::mutex& lazy_entity_columns_mutex() const {
|
||||
return lazy_entity_columns_mutex_;
|
||||
}
|
||||
|
||||
// DBIter calls this after ResetValueAndColumns() before repopulating the
|
||||
// current entry from a serialized wide-column entity.
|
||||
void AssertReadyForEntity() const {
|
||||
assert(value_.empty());
|
||||
assert(wide_columns_.empty());
|
||||
assert(lazy_entity_columns_.empty());
|
||||
assert(lazy_blob_columns_.empty());
|
||||
}
|
||||
|
||||
inline void ClearSavedValue() {
|
||||
if (saved_value_.capacity() > 1048576) {
|
||||
std::string empty;
|
||||
swap(empty, saved_value_);
|
||||
} else {
|
||||
saved_value_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// Preserve the serialized entity bytes when DBIter needs stable backing
|
||||
// storage for lazy column slices across iterator movement.
|
||||
void SaveEntitySliceIfNeeded(const Slice& slice) {
|
||||
if (slice.data() != saved_value_.data() ||
|
||||
slice.size() != saved_value_.size()) {
|
||||
saved_value_.assign(slice.data(), slice.size());
|
||||
}
|
||||
}
|
||||
|
||||
// Clears the previous lazy entity metadata and returns the saved entity
|
||||
// buffer as input for DeserializeV2().
|
||||
Slice PrepareForLazyEntityDeserialize() {
|
||||
ClearLazyEntity();
|
||||
return Slice(saved_value_);
|
||||
}
|
||||
|
||||
// Publishes the deserialized lazy entity metadata to the blob resolver.
|
||||
void BindLazyEntity(const Slice& user_key) {
|
||||
entity_blob_resolver_.Reset(user_key, &lazy_entity_columns_,
|
||||
&lazy_blob_columns_);
|
||||
}
|
||||
|
||||
// Drops the lazy entity metadata and any resolver aliases into it.
|
||||
void ClearLazyEntity() {
|
||||
lazy_entity_columns_.clear();
|
||||
lazy_blob_columns_.clear();
|
||||
entity_blob_resolver_.Reset(Slice(), nullptr, nullptr);
|
||||
}
|
||||
|
||||
// Clears materialized wide columns on error before DBIter invalidates
|
||||
// itself.
|
||||
void ClearWideColumns() { wide_columns_.clear(); }
|
||||
|
||||
// Fast path for inline entities whose default column is already
|
||||
// materialized in wide_columns_.
|
||||
void MaybeSetValueFromMaterializedDefaultColumn() {
|
||||
if (WideColumnsHelper::HasDefaultColumn(wide_columns_)) {
|
||||
value_ = WideColumnsHelper::GetDefaultColumn(wide_columns_);
|
||||
}
|
||||
}
|
||||
|
||||
void SetFromPlain(const Slice& slice) {
|
||||
assert(value_.empty());
|
||||
assert(wide_columns_.empty());
|
||||
assert(lazy_entity_columns_.empty());
|
||||
assert(lazy_blob_columns_.empty());
|
||||
|
||||
value_ = slice;
|
||||
wide_columns_.emplace_back(kDefaultWideColumnName, slice);
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
value_.clear();
|
||||
wide_columns_.clear();
|
||||
ClearLazyEntity();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string saved_value_;
|
||||
// Value of the default column.
|
||||
Slice value_;
|
||||
// All columns (i.e. name-value pairs).
|
||||
WideColumns wide_columns_;
|
||||
// Lazy resolution state for V2 entities with blob columns.
|
||||
ReadPathBlobResolver entity_blob_resolver_;
|
||||
std::vector<WideColumn> lazy_entity_columns_;
|
||||
std::vector<std::pair<size_t, BlobIndex>> lazy_blob_columns_;
|
||||
mutable std::mutex lazy_entity_columns_mutex_;
|
||||
};
|
||||
|
||||
// For all methods in this block:
|
||||
@@ -291,22 +448,22 @@ class DBIter final : public Iterator {
|
||||
// It might get adjusted if the seek key is larger than iterator upper bound.
|
||||
// target does not have timestamp.
|
||||
void SetSavedKeyToSeekForPrevTarget(const Slice& target);
|
||||
bool FindValueForCurrentKey();
|
||||
bool FindValueForCurrentKey(bool& found_visible);
|
||||
bool FindValueForCurrentKeyUsingSeek();
|
||||
bool FindUserKeyBeforeSavedKey();
|
||||
// If `skipping_saved_key` is true, the function will keep iterating until it
|
||||
// finds a user key that is larger than `saved_key_`.
|
||||
// If `prefix` is not null, the iterator needs to stop when all keys for the
|
||||
// prefix are exhausted and the iterator is set to invalid.
|
||||
bool FindNextUserEntry(bool skipping_saved_key, const Slice* prefix);
|
||||
// When prefix_ is set, the iterator stops when all keys for the prefix are
|
||||
// exhausted and the iterator is set to invalid.
|
||||
bool FindNextUserEntry(bool skipping_saved_key);
|
||||
// Internal implementation of FindNextUserEntry().
|
||||
bool FindNextUserEntryInternal(bool skipping_saved_key, const Slice* prefix);
|
||||
bool FindNextUserEntryInternal(bool skipping_saved_key);
|
||||
bool ParseKey(ParsedInternalKey* key);
|
||||
bool MergeValuesNewToOld();
|
||||
|
||||
// If prefix is not null, we need to set the iterator to invalid if no more
|
||||
// When prefix_ is set, we need to set the iterator to invalid if no more
|
||||
// entry can be found within the prefix.
|
||||
void PrevInternal(const Slice* prefix);
|
||||
void PrevInternal();
|
||||
bool TooManyInternalKeysSkipped(bool increment = true);
|
||||
bool IsVisible(SequenceNumber sequence, const Slice& ts,
|
||||
bool* more_recent = nullptr);
|
||||
@@ -326,14 +483,7 @@ class DBIter final : public Iterator {
|
||||
}
|
||||
}
|
||||
|
||||
inline void ClearSavedValue() {
|
||||
if (saved_value_.capacity() > 1048576) {
|
||||
std::string empty;
|
||||
swap(empty, saved_value_);
|
||||
} else {
|
||||
saved_value_.clear();
|
||||
}
|
||||
}
|
||||
inline void ClearSavedValue() { value_columns_state_.ClearSavedValue(); }
|
||||
|
||||
inline void ResetInternalKeysSkippedCounter() {
|
||||
local_stats_.skip_count_ += num_internal_keys_skipped_;
|
||||
@@ -358,11 +508,7 @@ class DBIter final : public Iterator {
|
||||
}
|
||||
|
||||
void SetValueAndColumnsFromPlain(const Slice& slice) {
|
||||
assert(value_.empty());
|
||||
assert(wide_columns_.empty());
|
||||
|
||||
value_ = slice;
|
||||
wide_columns_.emplace_back(kDefaultWideColumnName, slice);
|
||||
value_columns_state_.SetFromPlain(slice);
|
||||
}
|
||||
|
||||
bool SetValueAndColumnsFromBlobImpl(const Slice& user_key,
|
||||
@@ -371,14 +517,12 @@ class DBIter final : public Iterator {
|
||||
const Slice& blob_index);
|
||||
|
||||
bool SetValueAndColumnsFromEntity(Slice slice);
|
||||
bool MaterializeLazyEntityColumns() const;
|
||||
|
||||
bool SetValueAndColumnsFromMergeResult(const Status& merge_status,
|
||||
ValueType result_type);
|
||||
|
||||
void ResetValueAndColumns() {
|
||||
value_.clear();
|
||||
wide_columns_.clear();
|
||||
}
|
||||
void ResetValueAndColumns() { value_columns_state_.Reset(); }
|
||||
|
||||
void ResetBlobData() {
|
||||
blob_reader_.ResetBlobValue();
|
||||
@@ -410,6 +554,59 @@ class DBIter final : public Iterator {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Record a deletion into the current contiguous tombstone run.
|
||||
// In forward iteration, first_key is set only for the first tombstone
|
||||
// (always_update_first_key=false). In reverse, keys arrive in decreasing
|
||||
// order so first_key is updated every time (always_update_first_key=true).
|
||||
void TrackContiguousTombstone(const Slice& user_key,
|
||||
bool always_update_first_key);
|
||||
|
||||
// If a contiguous tombstone run is pending, insert a range tombstone
|
||||
// (if threshold is met) and reset tracking state. When
|
||||
// check_prefix_match is true, the insertion is skipped (but tracking is
|
||||
// still reset) if end_key is outside the seek prefix.
|
||||
void FlushPendingTombstoneRun(const Slice& end_key,
|
||||
bool check_prefix_match = false);
|
||||
|
||||
// If enough contiguous tombstones have been tracked, insert a range
|
||||
// tombstone [first_key, end_key) into the mutable memtable.
|
||||
// end_key is the exclusive upper bound — typically the next live key.
|
||||
void MaybeInsertRangeTombstone(const Slice& end_key);
|
||||
void ResetContiguousTombstoneTracking() {
|
||||
contiguous_tombstone_count_ = 0;
|
||||
range_tomb_first_key_.Clear();
|
||||
range_tomb_end_key_.Clear();
|
||||
}
|
||||
|
||||
// Returns true if there is no prefix constraint (prefix_ not set) or
|
||||
// if `key` is in the prefix extractor's domain and its prefix matches.
|
||||
// Out-of-domain keys return false when a prefix is set.
|
||||
bool PrefixCheck(const Slice& key) const {
|
||||
return !prefix_.has_value() || (prefix_extractor_->InDomain(key) &&
|
||||
prefix_extractor_->Transform(key).compare(
|
||||
prefix_->GetUserKey()) == 0);
|
||||
}
|
||||
|
||||
// Returns true if a prefix should be extracted from the seek target and
|
||||
// used for prefix boundary tracking. True when prefix_same_as_start is
|
||||
// set, or when range tombstone conversion is enabled during a legacy
|
||||
// prefix seek. If target is out of domain then false is returned.
|
||||
bool ShouldSetPrefix(const Slice& target) const {
|
||||
return (prefix_same_as_start_ ||
|
||||
(min_tombstones_for_range_conversion_ > 0 &&
|
||||
!expect_total_order_inner_iter_)) &&
|
||||
prefix_extractor_->InDomain(target);
|
||||
}
|
||||
|
||||
void ResetSeekState() {
|
||||
ReleaseTempPinnedData();
|
||||
ResetBlobData();
|
||||
ResetValueAndColumns();
|
||||
ResetInternalKeysSkippedCounter();
|
||||
ResetContiguousTombstoneTracking();
|
||||
prefix_.reset();
|
||||
}
|
||||
|
||||
void MarkMemtableForFlushForAvgTrigger() {
|
||||
if (avg_op_scan_flush_trigger_ &&
|
||||
mem_hidden_op_scanned_since_seek_ >= memtable_op_scan_flush_trigger_ &&
|
||||
@@ -464,13 +661,8 @@ class DBIter final : public Iterator {
|
||||
// kTypeValuePreferredSeqno entry, this is the write time specified by the
|
||||
// user.
|
||||
uint64_t saved_write_unix_time_;
|
||||
std::string saved_value_;
|
||||
ValueColumnsState value_columns_state_;
|
||||
Slice pinned_value_;
|
||||
// for prefix seek mode to support prev()
|
||||
// Value of the default column
|
||||
Slice value_;
|
||||
// All columns (i.e. name-value pairs)
|
||||
WideColumns wide_columns_;
|
||||
Statistics* statistics_;
|
||||
uint64_t max_skip_;
|
||||
uint64_t max_skippable_internal_keys_;
|
||||
@@ -478,12 +670,16 @@ class DBIter final : public Iterator {
|
||||
const Slice* iterate_lower_bound_;
|
||||
const Slice* iterate_upper_bound_;
|
||||
|
||||
// The prefix of the seek key. It is only used when prefix_same_as_start_
|
||||
// is true and prefix extractor is not null. In Next() or Prev(), current keys
|
||||
// will be checked against this prefix, so that the iterator can be
|
||||
// invalidated if the keys in this prefix has been exhausted. Set it using
|
||||
// SetUserKey() and use it using GetUserKey().
|
||||
IterKey prefix_;
|
||||
// The prefix of the seek key. Set during Seek/SeekForPrev when either
|
||||
// prefix_same_as_start_ is true or the iterator uses prefix filtering
|
||||
// (!expect_total_order_inner_iter_ && InDomain(target)). Used in Next()
|
||||
// and Prev() to:
|
||||
// - invalidate the iterator when prefix_same_as_start_ is true and keys
|
||||
// in this prefix have been exhausted.
|
||||
// - bound range tombstone tracking to the seek prefix when
|
||||
// min_tombstones_for_range_conversion_ > 0.
|
||||
// Set via SetUserKey(), read via GetUserKey().
|
||||
std::optional<IterKey> prefix_;
|
||||
|
||||
Status status_;
|
||||
Slice lazy_blob_index_;
|
||||
@@ -505,6 +701,7 @@ class DBIter final : public Iterator {
|
||||
uint32_t avg_op_scan_flush_trigger_;
|
||||
uint32_t iter_step_since_seek_;
|
||||
uint32_t mem_hidden_op_scanned_since_seek_;
|
||||
uint32_t contiguous_tombstone_count_;
|
||||
Direction direction_;
|
||||
bool valid_;
|
||||
bool current_entry_is_merged_;
|
||||
@@ -519,11 +716,15 @@ class DBIter final : public Iterator {
|
||||
// Expect the inner iterator to maintain a total order.
|
||||
// prefix_extractor_ must be non-NULL if the value is false.
|
||||
const bool expect_total_order_inner_iter_;
|
||||
const uint32_t min_tombstones_for_range_conversion_;
|
||||
// Whether the iterator is allowed to expose blob references. Set to true when
|
||||
// the stacked BlobDB implementation is used, false otherwise.
|
||||
bool expose_blob_index_;
|
||||
bool allow_unprepared_value_;
|
||||
bool is_blob_;
|
||||
bool arena_mode_;
|
||||
|
||||
IterKey range_tomb_first_key_;
|
||||
IterKey range_tomb_end_key_;
|
||||
};
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
File diff suppressed because it is too large.
Load diff
@@ -163,6 +163,10 @@ TEST_F(DBMemTableTest, DuplicateSeq) {
|
||||
mem->Add(seq, kTypeSingleDeletion, "key", "", nullptr /* kv_prot_info */)
|
||||
.IsTryAgain());
|
||||
|
||||
ASSERT_EQ(mem->NumEntries(), 2u);
|
||||
ASSERT_EQ(mem->NumDeletion(), 0u);
|
||||
ASSERT_EQ(mem->NumRangeDeletion(), 0u);
|
||||
|
||||
// Test the duplicate keys under stress
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
bool insert_dup = i % 10 == 1;
|
||||
@@ -204,6 +208,11 @@ TEST_F(DBMemTableTest, DuplicateSeq) {
|
||||
ASSERT_TRUE(mem->Add(seq, kTypeValue, "key", "value",
|
||||
nullptr /* kv_prot_info */, true, &post_process_info)
|
||||
.IsTryAgain());
|
||||
|
||||
mem->BatchPostProcess(post_process_info);
|
||||
ASSERT_EQ(mem->NumEntries(), 1u);
|
||||
ASSERT_EQ(mem->NumDeletion(), 0u);
|
||||
ASSERT_EQ(mem->NumRangeDeletion(), 0u);
|
||||
delete mem;
|
||||
}
|
||||
|
||||
@@ -242,6 +251,7 @@ TEST_F(DBMemTableTest, ConcurrentMergeWrite) {
|
||||
true, &post_process_info1));
|
||||
v1.clear();
|
||||
}
|
||||
mem->BatchPostProcess(post_process_info1);
|
||||
});
|
||||
ROCKSDB_NAMESPACE::port::Thread write_thread2([&]() {
|
||||
MemTablePostProcessInfo post_process_info2;
|
||||
@@ -252,9 +262,14 @@ TEST_F(DBMemTableTest, ConcurrentMergeWrite) {
|
||||
true, &post_process_info2));
|
||||
v2.clear();
|
||||
}
|
||||
mem->BatchPostProcess(post_process_info2);
|
||||
});
|
||||
write_thread1.join();
|
||||
write_thread2.join();
|
||||
// 1 non-concurrent Put + (num_ops - 1) concurrent Merges
|
||||
ASSERT_EQ(mem->NumEntries(), static_cast<uint64_t>(num_ops));
|
||||
ASSERT_EQ(mem->NumDeletion(), 0u);
|
||||
ASSERT_EQ(mem->NumRangeDeletion(), 0u);
|
||||
|
||||
Status status;
|
||||
ReadOptions roptions;
|
||||
|
||||
@@ -0,0 +1,490 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
|
||||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||
// This source code is licensed under both the GPLv2 (found in the
|
||||
// COPYING file in the root directory) and Apache 2.0 License
|
||||
// (found in the LICENSE.Apache file in the root directory).
|
||||
|
||||
#include "db/db_test_util.h"
|
||||
#include "port/stack_trace.h"
|
||||
#include "rocksdb/convenience.h"
|
||||
#include "rocksdb/table.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
class DBOpenWithConfigTest : public DBTestBase {
|
||||
protected:
|
||||
DBOpenWithConfigTest()
|
||||
: DBTestBase("db_open_with_config_test", /* env_do_fsync */ false) {}
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
bool CompressionSupported(CompressionType compression) {
|
||||
for (CompressionType supported : GetSupportedCompressions()) {
|
||||
if (supported == compression) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
CompressionType GetPrimaryCompressionForTest() {
|
||||
if (CompressionSupported(kLZ4Compression)) {
|
||||
return kLZ4Compression;
|
||||
}
|
||||
if (CompressionSupported(kZSTD)) {
|
||||
return kZSTD;
|
||||
}
|
||||
return kNoCompression;
|
||||
}
|
||||
|
||||
CompressionType GetBottommostCompressionForTest(CompressionType compression) {
|
||||
if (CompressionSupported(kZSTD)) {
|
||||
return kZSTD;
|
||||
}
|
||||
return compression;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Test opening a RocksDB instance with a comprehensive configuration
|
||||
// including FIFO compaction, blob files, compression settings, and
|
||||
// various memory/protection options.
|
||||
TEST_F(DBOpenWithConfigTest, OpenWithComprehensiveConfig) {
|
||||
Options options = GetDefaultOptions();
|
||||
options.max_open_files = -1;
|
||||
const CompressionType primary_compression = GetPrimaryCompressionForTest();
|
||||
const CompressionType bottommost_compression =
|
||||
GetBottommostCompressionForTest(primary_compression);
|
||||
|
||||
// Basic DB options
|
||||
options.create_if_missing = true;
|
||||
|
||||
// Compression settings. Prefer LZ4 when linked, otherwise use another
|
||||
// supported compression so this test remains portable across no-compression
|
||||
// builds.
|
||||
options.compression = primary_compression;
|
||||
|
||||
// Prefer ZSTD for bottommost compression when linked.
|
||||
options.bottommost_compression = bottommost_compression;
|
||||
|
||||
// compression_opts={checksum=true;max_dict_buffer_bytes=0;enabled=true;
|
||||
// max_dict_bytes=0;max_compressed_bytes_per_kb=896;parallel_threads=1;
|
||||
// zstd_max_train_bytes=0;level=32767;use_zstd_dict_trainer=true;
|
||||
// strategy=0;window_bits=-14;}
|
||||
options.compression_opts.checksum = true;
|
||||
options.compression_opts.max_dict_buffer_bytes = 0;
|
||||
options.compression_opts.enabled = true;
|
||||
options.compression_opts.max_dict_bytes = 0;
|
||||
options.compression_opts.max_compressed_bytes_per_kb = 896;
|
||||
options.compression_opts.parallel_threads = 1;
|
||||
options.compression_opts.zstd_max_train_bytes = 0;
|
||||
options.compression_opts.level = CompressionOptions::kDefaultCompressionLevel;
|
||||
options.compression_opts.use_zstd_dict_trainer = true;
|
||||
options.compression_opts.strategy = 0;
|
||||
options.compression_opts.window_bits = -14;
|
||||
|
||||
// bottommost_compression_opts={checksum=false;max_dict_buffer_bytes=0;
|
||||
// enabled=true;max_dict_bytes=0;max_compressed_bytes_per_kb=896;
|
||||
// parallel_threads=1;zstd_max_train_bytes=0;level=3;
|
||||
// use_zstd_dict_trainer=true;strategy=0;window_bits=-14;}
|
||||
options.bottommost_compression_opts.checksum = false;
|
||||
options.bottommost_compression_opts.max_dict_buffer_bytes = 0;
|
||||
options.bottommost_compression_opts.enabled = true;
|
||||
options.bottommost_compression_opts.max_dict_bytes = 0;
|
||||
options.bottommost_compression_opts.max_compressed_bytes_per_kb = 896;
|
||||
options.bottommost_compression_opts.parallel_threads = 1;
|
||||
options.bottommost_compression_opts.zstd_max_train_bytes = 0;
|
||||
options.bottommost_compression_opts.level = 3;
|
||||
options.bottommost_compression_opts.use_zstd_dict_trainer = true;
|
||||
options.bottommost_compression_opts.strategy = 0;
|
||||
options.bottommost_compression_opts.window_bits = -14;
|
||||
|
||||
// compression_manager=nullptr (default)
|
||||
options.compression_manager = nullptr;
|
||||
|
||||
// Write buffer settings
|
||||
// write_buffer_size=134217728 (128MB)
|
||||
options.write_buffer_size = 134217728;
|
||||
|
||||
// max_write_buffer_number=64
|
||||
options.max_write_buffer_number = 64;
|
||||
|
||||
// min_write_buffer_number_to_merge=1
|
||||
options.min_write_buffer_number_to_merge = 1;
|
||||
|
||||
// max_write_buffer_size_to_maintain=0
|
||||
options.max_write_buffer_size_to_maintain = 0;
|
||||
|
||||
// arena_block_size=1048576 (1MB)
|
||||
options.arena_block_size = 1048576;
|
||||
|
||||
// Memtable settings
|
||||
// memtable_factory=SkipListFactory (default)
|
||||
options.memtable_factory = std::make_shared<SkipListFactory>();
|
||||
|
||||
// memtable_prefix_bloom_size_ratio=0.010000
|
||||
options.memtable_prefix_bloom_size_ratio = 0.01;
|
||||
|
||||
// memtable_whole_key_filtering=true
|
||||
options.memtable_whole_key_filtering = true;
|
||||
|
||||
// memtable_huge_page_size=0
|
||||
options.memtable_huge_page_size = 0;
|
||||
|
||||
// memtable_insert_with_hint_prefix_extractor=nullptr
|
||||
options.memtable_insert_with_hint_prefix_extractor = nullptr;
|
||||
|
||||
// memtable_protection_bytes_per_key=1
|
||||
options.memtable_protection_bytes_per_key = 1;
|
||||
|
||||
// memtable_max_range_deletions=0 (if available)
|
||||
// Note: This option may not be available in all versions
|
||||
|
||||
// memtable_avg_op_scan_flush_trigger=0
|
||||
options.memtable_avg_op_scan_flush_trigger = 0;
|
||||
|
||||
// memtable_op_scan_flush_trigger=0
|
||||
options.memtable_op_scan_flush_trigger = 0;
|
||||
|
||||
// memtable_veirfy_per_key_checksum_on_seek=false
|
||||
options.memtable_veirfy_per_key_checksum_on_seek = false;
|
||||
|
||||
// Level and compaction settings
|
||||
// num_levels=50
|
||||
options.num_levels = 50;
|
||||
|
||||
// level0_file_num_compaction_trigger=16
|
||||
options.level0_file_num_compaction_trigger = 16;
|
||||
|
||||
// level0_slowdown_writes_trigger=2147483647
|
||||
options.level0_slowdown_writes_trigger = 2147483647;
|
||||
|
||||
// level0_stop_writes_trigger=2147483647
|
||||
options.level0_stop_writes_trigger = 2147483647;
|
||||
|
||||
// target_file_size_base=536870912 (512MB)
|
||||
options.target_file_size_base = 536870912;
|
||||
|
||||
// target_file_size_multiplier=1
|
||||
options.target_file_size_multiplier = 1;
|
||||
|
||||
// target_file_size_is_upper_bound=false
|
||||
options.target_file_size_is_upper_bound = false;
|
||||
|
||||
// max_bytes_for_level_base=104857600 (100MB)
|
||||
options.max_bytes_for_level_base = 104857600;
|
||||
|
||||
// max_bytes_for_level_multiplier=10.000000
|
||||
options.max_bytes_for_level_multiplier = 10.0;
|
||||
|
||||
// max_bytes_for_level_multiplier_additional=1:1:1:1:1:1:1
|
||||
options.max_bytes_for_level_multiplier_additional = {1, 1, 1, 1, 1, 1, 1};
|
||||
|
||||
// max_compaction_bytes=13421772800
|
||||
options.max_compaction_bytes = 13421772800;
|
||||
|
||||
// soft_pending_compaction_bytes_limit=0
|
||||
options.soft_pending_compaction_bytes_limit = 0;
|
||||
|
||||
// hard_pending_compaction_bytes_limit=0
|
||||
options.hard_pending_compaction_bytes_limit = 0;
|
||||
|
||||
// level_compaction_dynamic_level_bytes=false
|
||||
options.level_compaction_dynamic_level_bytes = false;
|
||||
|
||||
// Compaction style and priority
|
||||
// compaction_style=kCompactionStyleFIFO
|
||||
options.compaction_style = kCompactionStyleFIFO;
|
||||
|
||||
// compaction_pri=kByCompensatedSize
|
||||
options.compaction_pri = kByCompensatedSize;
|
||||
|
||||
// disable_auto_compactions=true
|
||||
options.disable_auto_compactions = true;
|
||||
|
||||
// FIFO compaction options
|
||||
// compaction_options_fifo={trivial_copy_buffer_size=4096;
|
||||
// allow_trivial_copy_when_change_temperature=false;
|
||||
// file_temperature_age_thresholds=;allow_compaction=false;
|
||||
// age_for_warm=0;max_table_files_size=1125899906842624;}
|
||||
options.compaction_options_fifo.trivial_copy_buffer_size = 4096;
|
||||
options.compaction_options_fifo.allow_trivial_copy_when_change_temperature =
|
||||
false;
|
||||
options.compaction_options_fifo.file_temperature_age_thresholds.clear();
|
||||
options.compaction_options_fifo.allow_compaction = false;
|
||||
options.compaction_options_fifo.age_for_warm = 0;
|
||||
options.compaction_options_fifo.max_table_files_size = 1125899906842624ULL;
|
||||
|
||||
// Universal compaction options (for reference, even though using FIFO)
|
||||
// compaction_options_universal={reduce_file_locking=true;incremental=false;
|
||||
// compression_size_percent=-1;allow_trivial_move=false;
|
||||
// max_size_amplification_percent=50;max_merge_width=4294967295;
|
||||
// stop_style=kCompactionStopStyleTotalSize;min_merge_width=2;
|
||||
// max_read_amp=0;size_ratio=10;}
|
||||
options.compaction_options_universal.reduce_file_locking = true;
|
||||
options.compaction_options_universal.incremental = false;
|
||||
options.compaction_options_universal.compression_size_percent = -1;
|
||||
options.compaction_options_universal.allow_trivial_move = false;
|
||||
options.compaction_options_universal.max_size_amplification_percent = 50;
|
||||
options.compaction_options_universal.max_merge_width = 4294967295;
|
||||
options.compaction_options_universal.stop_style =
|
||||
kCompactionStopStyleTotalSize;
|
||||
options.compaction_options_universal.min_merge_width = 2;
|
||||
options.compaction_options_universal.max_read_amp = 0;
|
||||
options.compaction_options_universal.size_ratio = 10;
|
||||
|
||||
// Blob file settings
|
||||
// enable_blob_files=true
|
||||
options.enable_blob_files = true;
|
||||
|
||||
// min_blob_size=0
|
||||
options.min_blob_size = 0;
|
||||
|
||||
// blob_file_size=268435456 (256MB)
|
||||
options.blob_file_size = 268435456;
|
||||
|
||||
// blob_compression_type=kNoCompression
|
||||
options.blob_compression_type = kNoCompression;
|
||||
|
||||
// enable_blob_garbage_collection=false
|
||||
options.enable_blob_garbage_collection = false;
|
||||
|
||||
// blob_garbage_collection_age_cutoff=0.250000
|
||||
options.blob_garbage_collection_age_cutoff = 0.25;
|
||||
|
||||
// blob_garbage_collection_force_threshold=1.000000
|
||||
options.blob_garbage_collection_force_threshold = 1.0;
|
||||
|
||||
// blob_compaction_readahead_size=0
|
||||
options.blob_compaction_readahead_size = 0;
|
||||
|
||||
// blob_file_starting_level=0
|
||||
options.blob_file_starting_level = 0;
|
||||
|
||||
// prepopulate_blob_cache=kDisable
|
||||
options.prepopulate_blob_cache = PrepopulateBlobCache::kDisable;
|
||||
|
||||
// Iterator settings
|
||||
// max_sequential_skip_in_iterations=8
|
||||
options.max_sequential_skip_in_iterations = 8;
|
||||
|
||||
// Merge settings
|
||||
// max_successive_merges=1000
|
||||
options.max_successive_merges = 1000;
|
||||
|
||||
// strict_max_successive_merges=true
|
||||
options.strict_max_successive_merges = true;
|
||||
|
||||
// merge_operator=nullptr (default, ZippyDBDeltaUpdateOperator is internal)
|
||||
options.merge_operator = nullptr;
|
||||
|
||||
// TTL and periodic compaction
|
||||
// ttl=21600 (6 hours)
|
||||
options.ttl = 21600;
|
||||
|
||||
// periodic_compaction_seconds=0
|
||||
options.periodic_compaction_seconds = 0;
|
||||
|
||||
// Temperature settings
|
||||
// last_level_temperature=kUnknown
|
||||
options.last_level_temperature = Temperature::kUnknown;
|
||||
|
||||
// default_write_temperature=kUnknown
|
||||
options.default_write_temperature = Temperature::kUnknown;
|
||||
|
||||
// default_temperature=kUnknown
|
||||
options.default_temperature = Temperature::kUnknown;
|
||||
|
||||
// Tiered storage settings
|
||||
// preclude_last_level_data_seconds=0
|
||||
options.preclude_last_level_data_seconds = 0;
|
||||
|
||||
// preserve_internal_time_seconds=1209600 (14 days)
|
||||
options.preserve_internal_time_seconds = 1209600;
|
||||
|
||||
// Protection and verification settings
|
||||
// block_protection_bytes_per_key=1
|
||||
options.block_protection_bytes_per_key = 1;
|
||||
|
||||
// paranoid_memory_checks=true
|
||||
options.paranoid_memory_checks = true;
|
||||
|
||||
// paranoid_file_checks=false
|
||||
options.paranoid_file_checks = false;
|
||||
|
||||
// force_consistency_checks=true
|
||||
options.force_consistency_checks = true;
|
||||
|
||||
// verify_output_flags=2049 (kVerifyBlockChecksum | kEnableForLocalCompaction)
|
||||
options.verify_output_flags = VerifyOutputFlags::kVerifyBlockChecksum |
|
||||
VerifyOutputFlags::kEnableForLocalCompaction;
|
||||
|
||||
// IO and stats settings
|
||||
// report_bg_io_stats=true
|
||||
options.report_bg_io_stats = true;
|
||||
|
||||
// sample_for_compression=0
|
||||
options.sample_for_compression = 0;
|
||||
|
||||
// Inplace update settings
|
||||
// inplace_update_support=false
|
||||
options.inplace_update_support = false;
|
||||
|
||||
// inplace_update_num_locks=10000
|
||||
options.inplace_update_num_locks = 10000;
|
||||
|
||||
// Bloom and prefix settings
|
||||
// bloom_locality=0
|
||||
options.bloom_locality = 0;
|
||||
|
||||
// prefix_extractor=nullptr
|
||||
options.prefix_extractor = nullptr;
|
||||
|
||||
// optimize_filters_for_hits=false
|
||||
options.optimize_filters_for_hits = false;
|
||||
|
||||
// Other settings
|
||||
// experimental_mempurge_threshold=0.000000
|
||||
options.experimental_mempurge_threshold = 0.0;
|
||||
|
||||
// bottommost_file_compaction_delay=0
|
||||
options.bottommost_file_compaction_delay = 0;
|
||||
|
||||
// disallow_memtable_writes=false
|
||||
options.disallow_memtable_writes = false;
|
||||
|
||||
// cf_allow_ingest_behind=false
|
||||
options.cf_allow_ingest_behind = false;
|
||||
|
||||
// persist_user_defined_timestamps=true
|
||||
options.persist_user_defined_timestamps = true;
|
||||
|
||||
// comparator=leveldb.BytewiseComparator (default)
|
||||
options.comparator = BytewiseComparator();
|
||||
|
||||
// compaction_filter=nullptr
|
||||
options.compaction_filter = nullptr;
|
||||
|
||||
// compaction_filter_factory=nullptr (ZippyDBCompactionFilterFactory is
|
||||
// internal)
|
||||
options.compaction_filter_factory = nullptr;
|
||||
|
||||
// sst_partitioner_factory=nullptr (ZippyDBCustomSstPartitionerFactory is
|
||||
// internal)
|
||||
options.sst_partitioner_factory = nullptr;
|
||||
|
||||
// Table factory - BlockBasedTable (default)
|
||||
// table_factory=BlockBasedTable
|
||||
BlockBasedTableOptions table_options;
|
||||
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
||||
|
||||
// Open the database
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Verify the database opened successfully by writing and reading data
|
||||
// Using PutEntity API to write wide-column entities
|
||||
WideColumns first_columns{{kDefaultWideColumnName, "value1"},
|
||||
{"attr_name1", "attr_value1"}};
|
||||
ASSERT_OK(db_->PutEntity(WriteOptions(), db_->DefaultColumnFamily(), "key1",
|
||||
first_columns));
|
||||
|
||||
const std::string second_value(1000, 'x');
|
||||
WideColumns second_columns{
|
||||
{kDefaultWideColumnName, second_value},
|
||||
{"attr_large", "large_attr_value"}}; // Larger value for blob
|
||||
ASSERT_OK(db_->PutEntity(WriteOptions(), db_->DefaultColumnFamily(), "key2",
|
||||
second_columns));
|
||||
|
||||
ASSERT_OK(
|
||||
db_->Put(WriteOptions(), db_->DefaultColumnFamily(), "key3", "value1"));
|
||||
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::string value;
|
||||
ASSERT_OK(db_->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ(value, "value1");
|
||||
|
||||
ASSERT_OK(db_->Get(ReadOptions(), "key2", &value));
|
||||
ASSERT_EQ(value, second_value);
|
||||
|
||||
// Verify some options are correctly set
|
||||
Options retrieved_options = db_->GetOptions();
|
||||
ASSERT_EQ(retrieved_options.compression, primary_compression);
|
||||
ASSERT_EQ(retrieved_options.bottommost_compression, bottommost_compression);
|
||||
ASSERT_EQ(retrieved_options.compaction_style, kCompactionStyleFIFO);
|
||||
ASSERT_TRUE(retrieved_options.enable_blob_files);
|
||||
ASSERT_EQ(retrieved_options.min_blob_size, 0);
|
||||
ASSERT_EQ(retrieved_options.write_buffer_size, 134217728);
|
||||
ASSERT_EQ(retrieved_options.max_write_buffer_number, 64);
|
||||
ASSERT_EQ(retrieved_options.num_levels, 50);
|
||||
ASSERT_TRUE(retrieved_options.disable_auto_compactions);
|
||||
ASSERT_EQ(retrieved_options.ttl, 21600);
|
||||
ASSERT_EQ(retrieved_options.memtable_protection_bytes_per_key, 1);
|
||||
ASSERT_EQ(retrieved_options.block_protection_bytes_per_key, 1);
|
||||
ASSERT_TRUE(retrieved_options.paranoid_memory_checks);
|
||||
ASSERT_TRUE(retrieved_options.report_bg_io_stats);
|
||||
ASSERT_EQ(retrieved_options.max_successive_merges, 1000);
|
||||
ASSERT_TRUE(retrieved_options.strict_max_successive_merges);
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
// Test that verifies we can reopen the database with the same configuration
|
||||
TEST_F(DBOpenWithConfigTest, ReopenWithSameConfig) {
|
||||
Options options = GetDefaultOptions();
|
||||
options.max_open_files = -1;
|
||||
options.create_if_missing = true;
|
||||
const CompressionType primary_compression = GetPrimaryCompressionForTest();
|
||||
const CompressionType bottommost_compression =
|
||||
GetBottommostCompressionForTest(primary_compression);
|
||||
|
||||
// Set key configuration options
|
||||
options.compression = primary_compression;
|
||||
options.bottommost_compression = bottommost_compression;
|
||||
options.compaction_style = kCompactionStyleFIFO;
|
||||
options.enable_blob_files = true;
|
||||
options.min_blob_size = 0;
|
||||
options.write_buffer_size = 134217728;
|
||||
options.max_write_buffer_number = 64;
|
||||
options.num_levels = 50;
|
||||
options.disable_auto_compactions = true;
|
||||
options.compaction_options_fifo.max_table_files_size = 1125899906842624ULL;
|
||||
options.memtable_protection_bytes_per_key = 1;
|
||||
options.block_protection_bytes_per_key = 1;
|
||||
options.paranoid_memory_checks = true;
|
||||
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Write some data
|
||||
ASSERT_OK(Put("key1", "value1"));
|
||||
ASSERT_OK(Put("key2", std::string(1000, 'x')));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Close and reopen
|
||||
Close();
|
||||
Reopen(options);
|
||||
|
||||
// Verify data persisted
|
||||
std::string value;
|
||||
ASSERT_OK(db_->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ(value, "value1");
|
||||
|
||||
ASSERT_OK(db_->Get(ReadOptions(), "key2", &value));
|
||||
ASSERT_EQ(value, std::string(1000, 'x'));
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
RegisterCustomObjects(argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
@@ -772,6 +772,72 @@ TEST_F(DBPropertiesTest, NumImmutableMemTable) {
|
||||
} while (ChangeCompactOptions());
|
||||
}
|
||||
|
||||
TEST_F(DBPropertiesTest, ConcurrentWriteMemTableProperties) {
|
||||
Options options = CurrentOptions();
|
||||
options.allow_concurrent_memtable_write = true;
|
||||
options.memtable_factory = std::make_shared<SkipListFactory>();
|
||||
options.flush_verify_memtable_count = true;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Multi-threaded writes that go through the concurrent Add() +
|
||||
// BatchPostProcess path.
|
||||
const int kNumThreads = 4;
|
||||
const int kEntriesPerThread = 100;
|
||||
std::vector<port::Thread> threads;
|
||||
threads.reserve(kNumThreads);
|
||||
for (int t = 0; t < kNumThreads; t++) {
|
||||
threads.emplace_back([&, t]() {
|
||||
for (int i = 0; i < kEntriesPerThread; i++) {
|
||||
std::string key = "k_" + std::to_string(t) + "_" + std::to_string(i);
|
||||
ASSERT_OK(Put(key, "val"));
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
uint64_t num_entries;
|
||||
ASSERT_TRUE(dbfull()->GetIntProperty("rocksdb.num-entries-active-mem-table",
|
||||
&num_entries));
|
||||
ASSERT_EQ(num_entries,
|
||||
static_cast<uint64_t>(kNumThreads * kEntriesPerThread));
|
||||
|
||||
// Write deletes and single deletes via concurrent path
|
||||
threads.clear();
|
||||
for (int t = 0; t < kNumThreads; t++) {
|
||||
threads.emplace_back([&, t]() {
|
||||
for (int i = 0; i < kEntriesPerThread; i++) {
|
||||
std::string key = "d_" + std::to_string(t) + "_" + std::to_string(i);
|
||||
if (i % 2 == 0) {
|
||||
ASSERT_OK(Delete(key));
|
||||
} else {
|
||||
ASSERT_OK(SingleDelete(key));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
uint64_t total_entries;
|
||||
ASSERT_TRUE(dbfull()->GetIntProperty("rocksdb.num-entries-active-mem-table",
|
||||
&total_entries));
|
||||
ASSERT_EQ(total_entries,
|
||||
static_cast<uint64_t>(kNumThreads * kEntriesPerThread * 2));
|
||||
|
||||
uint64_t num_deletes;
|
||||
ASSERT_TRUE(dbfull()->GetIntProperty("rocksdb.num-deletes-active-mem-table",
|
||||
&num_deletes));
|
||||
ASSERT_EQ(num_deletes,
|
||||
static_cast<uint64_t>(kNumThreads * kEntriesPerThread));
|
||||
|
||||
// Flush exercises the integrity check (flush_verify_memtable_count = true).
|
||||
// If stat counters are wrong, this would return Corruption.
|
||||
ASSERT_OK(Flush());
|
||||
}
|
||||
|
||||
// TODO(techdept) : Disabled flaky test #12863555
|
||||
TEST_F(DBPropertiesTest, DISABLED_GetProperty) {
|
||||
// Set sizes to both background thread pool to be 1 and block them.
|
||||
|
||||
@@ -1122,6 +1122,8 @@ TEST_F(DBSSTTest, DeleteSchedulerMultipleDBPaths) {
|
||||
begin = "Key4";
|
||||
end = "Key7";
|
||||
ASSERT_OK(db_->CompactRange(compact_options, &begin, &end));
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForPurge());
|
||||
ASSERT_EQ("0,2", FilesPerLevel(0));
|
||||
|
||||
sfm->WaitForEmptyTrash();
|
||||
@@ -1132,6 +1134,8 @@ TEST_F(DBSSTTest, DeleteSchedulerMultipleDBPaths) {
|
||||
compact_options.bottommost_level_compaction =
|
||||
BottommostLevelCompaction::kForceOptimized;
|
||||
ASSERT_OK(db_->CompactRange(compact_options, nullptr, nullptr));
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForPurge());
|
||||
ASSERT_EQ("0,1", FilesPerLevel(0));
|
||||
|
||||
sfm->WaitForEmptyTrash();
|
||||
|
||||
@@ -8071,6 +8071,41 @@ INSTANTIATE_TEST_CASE_P(OpenFilesAsync, OpenFilesAsyncTest,
|
||||
::testing::Values(-1, 10),
|
||||
::testing::Bool()));
|
||||
|
||||
TEST_F(DBTest, ReadOnlyCloseDoesNotDeleteWriterFiles) {
|
||||
Options options = CurrentOptions();
|
||||
options.create_if_missing = true;
|
||||
options.disable_auto_compactions = true;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
ASSERT_OK(Put("before", "value"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::unique_ptr<DB> read_only_db;
|
||||
ASSERT_OK(DB::OpenForReadOnly(options, dbname_, &read_only_db));
|
||||
|
||||
ASSERT_OK(Put("after", "value"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::vector<LiveFileMetaData> live_files;
|
||||
db_->GetLiveFilesMetaData(&live_files);
|
||||
ASSERT_GE(live_files.size(), 2);
|
||||
|
||||
std::string newest_sst_path;
|
||||
uint64_t newest_file_number = 0;
|
||||
for (const auto& live_file : live_files) {
|
||||
if (live_file.file_number > newest_file_number) {
|
||||
newest_file_number = live_file.file_number;
|
||||
newest_sst_path = live_file.directory + "/" + live_file.relative_filename;
|
||||
}
|
||||
}
|
||||
ASSERT_FALSE(newest_sst_path.empty());
|
||||
ASSERT_OK(env_->FileExists(newest_sst_path));
|
||||
|
||||
read_only_db.reset();
|
||||
|
||||
ASSERT_OK(env_->FileExists(newest_sst_path));
|
||||
}
|
||||
|
||||
// Test mix of races with async file open, reads, compactions
|
||||
TEST_P(OpenFilesAsyncTest, ConcurrentFileAccess) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
+552
@@ -7638,6 +7638,558 @@ TEST_F(DBTest2, GetFileChecksumsFromCurrentManifest_CRC32) {
|
||||
}
|
||||
}
|
||||
|
||||
// Parameterized by (allow_concurrent_memtable_write,
|
||||
// min_tombstones_for_range_conversion).
|
||||
class DBTestConcurrentRangeTombstoneConversions
|
||||
: public DBTestBase,
|
||||
public testing::WithParamInterface<std::tuple<bool, uint32_t>> {
|
||||
public:
|
||||
DBTestConcurrentRangeTombstoneConversions()
|
||||
: DBTestBase("db_test_concurrent_memtable_ops", /*env_do_fsync=*/true) {}
|
||||
};
|
||||
|
||||
TEST_P(DBTestConcurrentRangeTombstoneConversions,
|
||||
MixedWritesWithConcurrentReaders) {
|
||||
auto [allow_concurrent, range_conversion_threshold] = GetParam();
|
||||
|
||||
Options options = CurrentOptions();
|
||||
options.allow_concurrent_memtable_write = allow_concurrent;
|
||||
options.min_tombstones_for_range_conversion = range_conversion_threshold;
|
||||
options.statistics = CreateDBStatistics();
|
||||
// Larger memtable to avoid flushes during the test.
|
||||
options.write_buffer_size = 64 << 20;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Seed 100 keys.
|
||||
for (int i = 0; i < 100; i++) {
|
||||
ASSERT_OK(Put(Key(i), "seed_" + std::to_string(i)));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Thread 1: Put 10 keys then SingleDelete them.
|
||||
port::Thread writer_thread([&] {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ASSERT_OK(Put(Key(i), "new_" + std::to_string(i)));
|
||||
ASSERT_OK(db_->SingleDelete(WriteOptions(), Key(i)));
|
||||
}
|
||||
});
|
||||
|
||||
// Thread 2: Delete keys 20-29 (10 contiguous point tombstones).
|
||||
port::Thread deleter_thread([&] {
|
||||
for (int i = 20; i < 30; i++) {
|
||||
ASSERT_OK(Delete(Key(i)));
|
||||
}
|
||||
});
|
||||
|
||||
// Thread 3: DeleteRange 3 ranges.
|
||||
port::Thread range_deleter_thread([&] {
|
||||
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
||||
Key(40), Key(50)));
|
||||
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
||||
Key(60), Key(70)));
|
||||
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
|
||||
Key(80), Key(90)));
|
||||
});
|
||||
|
||||
// Wait for deletions to land before reading.
|
||||
deleter_thread.join();
|
||||
|
||||
// 4 forward + 4 reverse read threads scan keys 20-30 to hit the contiguous
|
||||
// point tombstones.
|
||||
std::vector<port::Thread> reader_threads;
|
||||
reader_threads.reserve(8);
|
||||
for (int t = 0; t < 4; t++) {
|
||||
// Forward readers
|
||||
reader_threads.emplace_back([&] {
|
||||
ReadOptions ro;
|
||||
auto iter = std::unique_ptr<Iterator>(db_->NewIterator(ro));
|
||||
iter->Seek(Key(20));
|
||||
while (iter->Valid() && iter->key().compare(Key(30)) < 0) {
|
||||
iter->Next();
|
||||
}
|
||||
ASSERT_OK(iter->status());
|
||||
});
|
||||
// Reverse readers
|
||||
reader_threads.emplace_back([&] {
|
||||
ReadOptions ro;
|
||||
auto iter = std::unique_ptr<Iterator>(db_->NewIterator(ro));
|
||||
iter->SeekForPrev(Key(30));
|
||||
while (iter->Valid() && iter->key().compare(Key(20)) >= 0) {
|
||||
iter->Prev();
|
||||
}
|
||||
ASSERT_OK(iter->status());
|
||||
});
|
||||
}
|
||||
|
||||
for (auto& t : reader_threads) {
|
||||
t.join();
|
||||
}
|
||||
writer_thread.join();
|
||||
range_deleter_thread.join();
|
||||
|
||||
if (range_conversion_threshold > 0) {
|
||||
uint64_t inserted =
|
||||
options.statistics->getTickerCount(READ_PATH_RANGE_TOMBSTONES_INSERTED);
|
||||
uint64_t discarded = options.statistics->getTickerCount(
|
||||
READ_PATH_RANGE_TOMBSTONES_DISCARDED);
|
||||
ASSERT_GT(inserted + discarded, 0);
|
||||
}
|
||||
|
||||
// Build expected keys: 10-19, 30-39, 50-59, 70-79, 90-99 are alive.
|
||||
// Keys 0-9 removed by Put+SingleDelete which also covers the SST entry.
|
||||
// Keys 20-29 deleted, 40-49/60-69/80-89 range-deleted.
|
||||
std::vector<std::string> expected_keys;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if ((i >= 0 && i < 10) || (i >= 20 && i < 30) || (i >= 40 && i < 50) ||
|
||||
(i >= 60 && i < 70) || (i >= 80 && i < 90)) {
|
||||
continue;
|
||||
}
|
||||
expected_keys.push_back(Key(i));
|
||||
}
|
||||
|
||||
// Verify forward iteration matches expected keys and values.
|
||||
{
|
||||
ReadOptions ro;
|
||||
auto iter = std::unique_ptr<Iterator>(db_->NewIterator(ro));
|
||||
int idx = 0;
|
||||
for (iter->SeekToFirst(); iter->Valid(); iter->Next(), idx++) {
|
||||
ASSERT_LT(idx, static_cast<int>(expected_keys.size()));
|
||||
ASSERT_EQ(iter->key().ToString(), expected_keys[idx]);
|
||||
}
|
||||
ASSERT_OK(iter->status());
|
||||
ASSERT_EQ(idx, static_cast<int>(expected_keys.size()));
|
||||
}
|
||||
|
||||
// Verify reverse iteration matches expected keys in reverse order.
|
||||
{
|
||||
ReadOptions ro;
|
||||
auto iter = std::unique_ptr<Iterator>(db_->NewIterator(ro));
|
||||
int idx = static_cast<int>(expected_keys.size()) - 1;
|
||||
for (iter->SeekToLast(); iter->Valid(); iter->Prev(), idx--) {
|
||||
ASSERT_GE(idx, 0);
|
||||
ASSERT_EQ(iter->key().ToString(), expected_keys[idx]);
|
||||
}
|
||||
ASSERT_OK(iter->status());
|
||||
ASSERT_EQ(idx, -1);
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
DBTestConcurrentRangeTombstoneConversions,
|
||||
DBTestConcurrentRangeTombstoneConversions,
|
||||
::testing::Combine(
|
||||
/*allow_concurrent_memtable_write=*/::testing::Bool(),
|
||||
/*min_tombstones_for_range_conversion=*/::testing::Values(0, 4)));
|
||||
|
||||
// Test file system that supports GetFileOpenMetadata for fast_sst_open testing
|
||||
class FastOpenTestRandomAccessFile : public FSRandomAccessFileWrapper {
|
||||
public:
|
||||
explicit FastOpenTestRandomAccessFile(
|
||||
std::unique_ptr<FSRandomAccessFile>&& file, const std::string& fname,
|
||||
std::atomic<int>* metadata_retrieved_count)
|
||||
: FSRandomAccessFileWrapper(file.get()),
|
||||
file_(std::move(file)),
|
||||
fname_(fname),
|
||||
metadata_retrieved_count_(metadata_retrieved_count) {}
|
||||
|
||||
IOStatus GetFileOpenMetadata(std::string* metadata) override {
|
||||
// Return the file name as opaque metadata for testing
|
||||
*metadata = "fast_open_metadata:" + fname_;
|
||||
metadata_retrieved_count_->fetch_add(1, std::memory_order_relaxed);
|
||||
return IOStatus::OK();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<FSRandomAccessFile> file_;
|
||||
std::string fname_;
|
||||
std::atomic<int>* metadata_retrieved_count_;
|
||||
};
|
||||
|
||||
class FastOpenTestFS : public FileSystemWrapper {
|
||||
public:
|
||||
explicit FastOpenTestFS(const std::shared_ptr<FileSystem>& base)
|
||||
: FileSystemWrapper(base) {}
|
||||
|
||||
const char* Name() const override { return "FastOpenTestFS"; }
|
||||
|
||||
IOStatus NewRandomAccessFile(const std::string& fname,
|
||||
const FileOptions& file_opts,
|
||||
std::unique_ptr<FSRandomAccessFile>* result,
|
||||
IODebugContext* dbg) override {
|
||||
if (file_opts.file_metadata != nullptr) {
|
||||
metadata_passed_on_open_.fetch_add(1, std::memory_order_relaxed);
|
||||
last_metadata_received_ = *file_opts.file_metadata;
|
||||
}
|
||||
IOStatus s = target()->NewRandomAccessFile(fname, file_opts, result, dbg);
|
||||
if (s.ok()) {
|
||||
result->reset(new FastOpenTestRandomAccessFile(
|
||||
std::move(*result), fname, &metadata_retrieved_count_));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
int GetMetadataRetrievedCount() const {
|
||||
return metadata_retrieved_count_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
int GetMetadataPassedOnOpenCount() const {
|
||||
return metadata_passed_on_open_.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
std::string GetLastMetadataReceived() const {
|
||||
return last_metadata_received_;
|
||||
}
|
||||
|
||||
void ResetCounters() {
|
||||
metadata_retrieved_count_.store(0, std::memory_order_relaxed);
|
||||
metadata_passed_on_open_.store(0, std::memory_order_relaxed);
|
||||
last_metadata_received_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic<int> metadata_retrieved_count_{0};
|
||||
std::atomic<int> metadata_passed_on_open_{0};
|
||||
std::string last_metadata_received_;
|
||||
};
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenDefaultFSReturnsNotSupported) {
|
||||
// Verify that the default FS returns NotSupported for GetFileOpenMetadata.
|
||||
std::string fname = dbname_ + "/000001.sst";
|
||||
// Create a small file so we can open it
|
||||
{
|
||||
std::unique_ptr<FSWritableFile> wf;
|
||||
ASSERT_OK(env_->GetFileSystem()->NewWritableFile(fname, FileOptions(), &wf,
|
||||
nullptr));
|
||||
ASSERT_OK(wf->Append("test", IOOptions(), nullptr));
|
||||
ASSERT_OK(wf->Close(IOOptions(), nullptr));
|
||||
}
|
||||
std::unique_ptr<FSRandomAccessFile> file;
|
||||
ASSERT_OK(env_->GetFileSystem()->NewRandomAccessFile(fname, FileOptions(),
|
||||
&file, nullptr));
|
||||
std::string metadata;
|
||||
IOStatus s = file->GetFileOpenMetadata(&metadata);
|
||||
ASSERT_TRUE(s.IsNotSupported());
|
||||
ASSERT_TRUE(metadata.empty());
|
||||
// Clean up
|
||||
ASSERT_OK(env_->GetFileSystem()->DeleteFile(fname, IOOptions(), nullptr));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenFlushAndReopen) {
|
||||
// Test with default FS (GetFileOpenMetadata returns NotSupported).
|
||||
// Verifies the code path works without crashing.
|
||||
Options options = CurrentOptions();
|
||||
options.fast_sst_open = true;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
ASSERT_OK(Put("key1", "value1"));
|
||||
ASSERT_OK(Put("key2", "value2"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
Close();
|
||||
Reopen(options);
|
||||
|
||||
ASSERT_EQ("value1", Get("key1"));
|
||||
ASSERT_EQ("value2", Get("key2"));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenCompactionAndReopen) {
|
||||
Options options = CurrentOptions();
|
||||
options.fast_sst_open = true;
|
||||
options.level0_file_num_compaction_trigger = 2;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
ASSERT_OK(Put("key1", "value1"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(Put("key2", "value2"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
|
||||
Close();
|
||||
Reopen(options);
|
||||
|
||||
ASSERT_EQ("value1", Get("key1"));
|
||||
ASSERT_EQ("value2", Get("key2"));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenWithTestFS) {
|
||||
// Full fast_sst_open flow: flush produces 1 SST, verify exact counts.
|
||||
auto test_fs = std::make_shared<FastOpenTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> test_env(new CompositeEnvWrapper(env_, test_fs));
|
||||
|
||||
Options options;
|
||||
options.env = test_env.get();
|
||||
options.fast_sst_open = true;
|
||||
options.create_if_missing = true;
|
||||
|
||||
std::string test_dbname = test::PerThreadDBPath("fast_open_test");
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
test_fs->ResetCounters();
|
||||
|
||||
// One flush produces one SST file -> exactly 1 GetFileOpenMetadata call
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key1", "value1"));
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key2", "value2"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
|
||||
ASSERT_EQ(1, test_fs->GetMetadataRetrievedCount());
|
||||
ASSERT_EQ(0, test_fs->GetMetadataPassedOnOpenCount());
|
||||
|
||||
test_fs->ResetCounters();
|
||||
db.reset();
|
||||
|
||||
// Reopen: the SST file should be opened with metadata
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
std::string value;
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ("value1", value);
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key2", &value));
|
||||
ASSERT_EQ("value2", value);
|
||||
|
||||
// Exactly 1 SST file was opened with metadata passed
|
||||
ASSERT_EQ(1, test_fs->GetMetadataPassedOnOpenCount());
|
||||
ASSERT_TRUE(test_fs->GetLastMetadataReceived().find("fast_open_metadata:") ==
|
||||
0);
|
||||
|
||||
db.reset();
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenCompactionWithTestFS) {
|
||||
// Compaction: 2 flushes trigger compaction, producing 1 output SST.
|
||||
auto test_fs = std::make_shared<FastOpenTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> test_env(new CompositeEnvWrapper(env_, test_fs));
|
||||
|
||||
Options options;
|
||||
options.env = test_env.get();
|
||||
options.fast_sst_open = true;
|
||||
options.create_if_missing = true;
|
||||
options.level0_file_num_compaction_trigger = 2;
|
||||
|
||||
std::string test_dbname = test::PerThreadDBPath("fast_open_compact");
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
test_fs->ResetCounters();
|
||||
|
||||
// 2 flushes = 2 SST files -> 2 GetFileOpenMetadata calls from flushes
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key1", "value1"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key2", "value2"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
ASSERT_OK(static_cast<DBImpl*>(db.get())->TEST_WaitForCompact());
|
||||
|
||||
// 2 flush SSTs retrieved metadata. Compaction output may or may not
|
||||
// trigger an additional retrieval depending on table cache state.
|
||||
ASSERT_GE(test_fs->GetMetadataRetrievedCount(), 2);
|
||||
|
||||
test_fs->ResetCounters();
|
||||
db.reset();
|
||||
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
std::string value;
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ("value1", value);
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key2", &value));
|
||||
ASSERT_EQ("value2", value);
|
||||
|
||||
// At least 1 SST opened with metadata on reopen
|
||||
ASSERT_GE(test_fs->GetMetadataPassedOnOpenCount(), 1);
|
||||
|
||||
db.reset();
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenDisabledNoMetadata) {
|
||||
// When fast_sst_open is disabled, no metadata should be retrieved or passed.
|
||||
auto test_fs = std::make_shared<FastOpenTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> test_env(new CompositeEnvWrapper(env_, test_fs));
|
||||
|
||||
Options options;
|
||||
options.env = test_env.get();
|
||||
options.fast_sst_open = false;
|
||||
options.create_if_missing = true;
|
||||
|
||||
std::string test_dbname = test::PerThreadDBPath("fast_open_disabled");
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
test_fs->ResetCounters();
|
||||
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key1", "value1"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
|
||||
// No metadata retrieved when disabled
|
||||
ASSERT_EQ(0, test_fs->GetMetadataRetrievedCount());
|
||||
|
||||
test_fs->ResetCounters();
|
||||
db.reset();
|
||||
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
std::string value;
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ("value1", value);
|
||||
|
||||
// No metadata passed on open since none was saved
|
||||
ASSERT_EQ(0, test_fs->GetMetadataPassedOnOpenCount());
|
||||
|
||||
db.reset();
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenToggleOption) {
|
||||
// Toggle: disabled for first flush, enabled for second.
|
||||
// Only the second file should have metadata on reopen.
|
||||
auto test_fs = std::make_shared<FastOpenTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> test_env(new CompositeEnvWrapper(env_, test_fs));
|
||||
|
||||
Options options;
|
||||
options.env = test_env.get();
|
||||
options.fast_sst_open = false;
|
||||
options.create_if_missing = true;
|
||||
// Prevent compaction so we have 2 separate L0 files
|
||||
options.level0_file_num_compaction_trigger = 100;
|
||||
|
||||
std::string test_dbname = test::PerThreadDBPath("fast_open_toggle");
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key1", "value1"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
ASSERT_EQ(0, test_fs->GetMetadataRetrievedCount());
|
||||
db.reset();
|
||||
|
||||
// Enable fast_sst_open for second session
|
||||
options.fast_sst_open = true;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
test_fs->ResetCounters();
|
||||
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key2", "value2"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
// Exactly 1 metadata retrieved (for the new flush)
|
||||
ASSERT_EQ(1, test_fs->GetMetadataRetrievedCount());
|
||||
|
||||
test_fs->ResetCounters();
|
||||
db.reset();
|
||||
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
std::string value;
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ("value1", value);
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key2", &value));
|
||||
ASSERT_EQ("value2", value);
|
||||
|
||||
// 2 SST files total, but only 1 has metadata (the one from the enabled
|
||||
// session). The other file opens without metadata.
|
||||
ASSERT_EQ(1, test_fs->GetMetadataPassedOnOpenCount());
|
||||
|
||||
db.reset();
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenIngestion) {
|
||||
// Test that file ingestion retrieves and persists metadata.
|
||||
auto test_fs = std::make_shared<FastOpenTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> test_env(new CompositeEnvWrapper(env_, test_fs));
|
||||
|
||||
Options options;
|
||||
options.env = test_env.get();
|
||||
options.fast_sst_open = true;
|
||||
options.create_if_missing = true;
|
||||
|
||||
std::string test_dbname = test::PerThreadDBPath("fast_open_ingest");
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
// Create an SST file externally
|
||||
std::string sst_file = test_dbname + "/external.sst";
|
||||
SstFileWriter sst_writer(EnvOptions(), options);
|
||||
ASSERT_OK(sst_writer.Open(sst_file));
|
||||
ASSERT_OK(sst_writer.Put("ikey1", "ival1"));
|
||||
ASSERT_OK(sst_writer.Put("ikey2", "ival2"));
|
||||
ASSERT_OK(sst_writer.Finish());
|
||||
|
||||
test_fs->ResetCounters();
|
||||
|
||||
// Ingest the external SST file
|
||||
IngestExternalFileOptions ingest_opts;
|
||||
ingest_opts.move_files = false;
|
||||
ASSERT_OK(db->IngestExternalFile({sst_file}, ingest_opts));
|
||||
|
||||
// Ingestion should retrieve metadata for the ingested file (1 call from
|
||||
// the separate open in the ingestion path)
|
||||
ASSERT_GE(test_fs->GetMetadataRetrievedCount(), 1);
|
||||
|
||||
test_fs->ResetCounters();
|
||||
db.reset();
|
||||
|
||||
// Reopen and verify metadata is passed
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
std::string value;
|
||||
ASSERT_OK(db->Get(ReadOptions(), "ikey1", &value));
|
||||
ASSERT_EQ("ival1", value);
|
||||
ASSERT_OK(db->Get(ReadOptions(), "ikey2", &value));
|
||||
ASSERT_EQ("ival2", value);
|
||||
|
||||
// Metadata should be passed when opening the ingested SST
|
||||
ASSERT_GE(test_fs->GetMetadataPassedOnOpenCount(), 1);
|
||||
|
||||
db.reset();
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
}
|
||||
|
||||
TEST_F(DBTest2, FastSstOpenDisableAfterMetadataPersisted) {
|
||||
// Verify that disabling fast_sst_open prevents previously-persisted
|
||||
// metadata from being passed to NewRandomAccessFile. This is critical
|
||||
// for cases where stale metadata (e.g. expired credentials) would
|
||||
// cause file open failures.
|
||||
auto test_fs = std::make_shared<FastOpenTestFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> test_env(new CompositeEnvWrapper(env_, test_fs));
|
||||
|
||||
Options options;
|
||||
options.env = test_env.get();
|
||||
options.fast_sst_open = true;
|
||||
options.create_if_missing = true;
|
||||
|
||||
std::string test_dbname = test::PerThreadDBPath("fast_open_disable_after");
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
|
||||
// Open with fast_sst_open enabled, write and flush to persist metadata
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key1", "value1"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
ASSERT_EQ(1, test_fs->GetMetadataRetrievedCount());
|
||||
db.reset();
|
||||
|
||||
// Reopen with fast_sst_open DISABLED — metadata is in the MANIFEST
|
||||
// but should NOT be passed to the filesystem
|
||||
options.fast_sst_open = false;
|
||||
test_fs->ResetCounters();
|
||||
ASSERT_OK(DB::Open(options, test_dbname, &db));
|
||||
|
||||
std::string value;
|
||||
ASSERT_OK(db->Get(ReadOptions(), "key1", &value));
|
||||
ASSERT_EQ("value1", value);
|
||||
|
||||
// No metadata should have been passed despite being in the MANIFEST
|
||||
ASSERT_EQ(0, test_fs->GetMetadataPassedOnOpenCount());
|
||||
|
||||
db.reset();
|
||||
ASSERT_OK(DestroyDB(test_dbname, options));
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -119,6 +119,10 @@ DBTestBase::~DBTestBase() {
|
||||
} else {
|
||||
EXPECT_OK(DestroyDB(dbname_, options));
|
||||
}
|
||||
// Ensure SstFileManager (and its DeleteScheduler background thread) is
|
||||
// destroyed before env_. The bg thread calls env_ virtual methods
|
||||
// (e.g. NewDirectory), so env_ must outlive it.
|
||||
last_options_.sst_file_manager.reset();
|
||||
delete env_;
|
||||
}
|
||||
|
||||
|
||||
+13
-7
@@ -1433,6 +1433,8 @@ class DBTestBase : public testing::Test {
|
||||
tp->raw_key_size = 0;
|
||||
tp->raw_value_size = 0;
|
||||
tp->num_data_blocks = 0;
|
||||
tp->num_data_blocks_compression_rejected = 0;
|
||||
tp->num_data_blocks_compression_bypassed = 0;
|
||||
tp->num_entries = 0;
|
||||
tp->num_deletions = 0;
|
||||
tp->num_merge_operands = 0;
|
||||
@@ -1446,21 +1448,25 @@ class DBTestBase : public testing::Test {
|
||||
ResetTableProperties(tp);
|
||||
int count = sscanf(
|
||||
tp_string.c_str(),
|
||||
"# data blocks %" SCNu64 " # entries %" SCNu64 " # deletions %" SCNu64
|
||||
" # merge operands %" SCNu64 " # range deletions %" SCNu64
|
||||
" raw key size %" SCNu64
|
||||
"# data blocks %" SCNu64 " # data blocks compression rejected %" SCNu64
|
||||
" # data blocks compression bypassed %" SCNu64
|
||||
" # uniform blocks %" SCNu64 " # entries %" SCNu64
|
||||
" # deletions %" SCNu64 " # merge operands %" SCNu64
|
||||
" # range deletions %" SCNu64 " raw key size %" SCNu64
|
||||
" raw average key size %lf "
|
||||
" raw value size %" SCNu64
|
||||
" raw average value size %lf "
|
||||
" data block size %" SCNu64 " data uncompressed size %" SCNu64
|
||||
" index block size (user-key? %" SCNu64 ", delta-value? %" SCNu64
|
||||
") %" SCNu64 " filter block size %" SCNu64,
|
||||
&tp->num_data_blocks, &tp->num_entries, &tp->num_deletions,
|
||||
&tp->num_merge_operands, &tp->num_range_deletions, &tp->raw_key_size,
|
||||
&dummy_double, &tp->raw_value_size, &dummy_double, &tp->data_size,
|
||||
&tp->num_data_blocks, &tp->num_data_blocks_compression_rejected,
|
||||
&tp->num_data_blocks_compression_bypassed, &tp->num_uniform_blocks,
|
||||
&tp->num_entries, &tp->num_deletions, &tp->num_merge_operands,
|
||||
&tp->num_range_deletions, &tp->raw_key_size, &dummy_double,
|
||||
&tp->raw_value_size, &dummy_double, &tp->data_size,
|
||||
&tp->uncompressed_data_size, &tp->index_key_is_user_key,
|
||||
&tp->index_value_is_delta_encoded, &tp->index_size, &tp->filter_size);
|
||||
ASSERT_EQ(count, 15);
|
||||
ASSERT_EQ(count, 18);
|
||||
}
|
||||
|
||||
private: // Prone to error on direct use
|
||||
|
||||
@@ -830,6 +830,85 @@ class IterKey {
|
||||
|
||||
bool IsUserKey() const { return is_user_key_; }
|
||||
|
||||
void Swap(IterKey& other) {
|
||||
// Record pointer relationships before any mutation.
|
||||
bool this_key_in_buf = (key_ == buf_);
|
||||
bool this_key_in_sec = (key_ == secondary_buf_);
|
||||
bool other_key_in_buf = (other.key_ == other.buf_);
|
||||
bool other_key_in_sec = (other.key_ == other.secondary_buf_);
|
||||
const char* orig_this_key = key_;
|
||||
const char* orig_other_key = other.key_;
|
||||
|
||||
// --- Swap primary buffer ---
|
||||
bool this_buf_inline = (buf_ == space_);
|
||||
bool other_buf_inline = (other.buf_ == other.space_);
|
||||
if (this_buf_inline && other_buf_inline) {
|
||||
char temp[kInlineBufferSize];
|
||||
size_t max_sz =
|
||||
std::min(std::max(key_size_, other.key_size_), kInlineBufferSize);
|
||||
memcpy(temp, space_, max_sz);
|
||||
memcpy(space_, other.space_, max_sz);
|
||||
memcpy(other.space_, temp, max_sz);
|
||||
} else if (this_buf_inline) {
|
||||
memcpy(other.space_, space_, key_size_);
|
||||
buf_ = other.buf_;
|
||||
other.buf_ = other.space_;
|
||||
} else if (other_buf_inline) {
|
||||
memcpy(space_, other.space_, other.key_size_);
|
||||
other.buf_ = buf_;
|
||||
buf_ = space_;
|
||||
} else {
|
||||
std::swap(buf_, other.buf_);
|
||||
}
|
||||
|
||||
// --- Swap secondary buffer ---
|
||||
bool this_sec_inline = (secondary_buf_ == space_for_secondary_buf_);
|
||||
bool other_sec_inline =
|
||||
(other.secondary_buf_ == other.space_for_secondary_buf_);
|
||||
if (this_sec_inline && other_sec_inline) {
|
||||
char temp[kInlineBufferSize];
|
||||
memcpy(temp, space_for_secondary_buf_, kInlineBufferSize);
|
||||
memcpy(space_for_secondary_buf_, other.space_for_secondary_buf_,
|
||||
kInlineBufferSize);
|
||||
memcpy(other.space_for_secondary_buf_, temp, kInlineBufferSize);
|
||||
} else if (this_sec_inline) {
|
||||
memcpy(other.space_for_secondary_buf_, space_for_secondary_buf_,
|
||||
secondary_buf_size_);
|
||||
secondary_buf_ = other.secondary_buf_;
|
||||
other.secondary_buf_ = other.space_for_secondary_buf_;
|
||||
} else if (other_sec_inline) {
|
||||
memcpy(space_for_secondary_buf_, other.space_for_secondary_buf_,
|
||||
other.secondary_buf_size_);
|
||||
other.secondary_buf_ = secondary_buf_;
|
||||
secondary_buf_ = space_for_secondary_buf_;
|
||||
} else {
|
||||
std::swap(secondary_buf_, other.secondary_buf_);
|
||||
}
|
||||
|
||||
// --- Redirect key_ pointers ---
|
||||
if (other_key_in_buf) {
|
||||
key_ = buf_;
|
||||
} else if (other_key_in_sec) {
|
||||
key_ = secondary_buf_;
|
||||
} else {
|
||||
key_ = orig_other_key;
|
||||
}
|
||||
|
||||
if (this_key_in_buf) {
|
||||
other.key_ = other.buf_;
|
||||
} else if (this_key_in_sec) {
|
||||
other.key_ = other.secondary_buf_;
|
||||
} else {
|
||||
other.key_ = orig_this_key;
|
||||
}
|
||||
|
||||
std::swap(key_size_, other.key_size_);
|
||||
std::swap(buf_size_, other.buf_size_);
|
||||
std::swap(secondary_buf_size_, other.secondary_buf_size_);
|
||||
std::swap(is_user_key_, other.is_user_key_);
|
||||
std::swap(key_slices_, other.key_slices_);
|
||||
}
|
||||
|
||||
private:
|
||||
char* buf_;
|
||||
const char* key_;
|
||||
|
||||
@@ -178,6 +178,80 @@ TEST_F(FormatTest, IterKeyOperation) {
|
||||
"abcdefghijklmnopqrstuvwxyz"));
|
||||
}
|
||||
|
||||
class IterKeySwapTest
|
||||
: public testing::TestWithParam<
|
||||
std::tuple<size_t, // a_key_len: inline (<=39) vs heap (>39)
|
||||
bool, // a_copy: true = copied, false = pinned
|
||||
bool, // a_use_secondary: key_ in secondary_buf_
|
||||
size_t, // b_key_len
|
||||
bool, // b_copy
|
||||
bool // b_use_secondary
|
||||
>> {
|
||||
protected:
|
||||
// Populates an IterKey with a key of the given length. If !copy, the key is
|
||||
// pinned (key_ points to external data); caller must keep `storage` alive.
|
||||
// If use_secondary, moves key_ to the secondary buffer via
|
||||
// TrimAppendWithTimestamp (only meaningful when copy=true).
|
||||
static std::string PopulateKey(IterKey& k, size_t key_len, char fill,
|
||||
bool copy, bool use_secondary,
|
||||
std::string& storage) {
|
||||
std::string base(key_len, fill);
|
||||
if (!copy) {
|
||||
storage = base;
|
||||
k.SetUserKey(Slice(storage), false /* copy */);
|
||||
return base;
|
||||
}
|
||||
k.SetUserKey(base);
|
||||
if (use_secondary) {
|
||||
size_t ts_sz = sizeof(uint64_t);
|
||||
// Keep 1 byte from the existing key, append the rest + timestamp.
|
||||
std::string suffix = base.substr(1);
|
||||
k.TrimAppendWithTimestamp(1, suffix.data(), suffix.size(), ts_sz);
|
||||
return base + std::string(ts_sz, '\0');
|
||||
}
|
||||
return base;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(IterKeySwapTest, SwapAndDestroy) {
|
||||
auto [a_key_len, a_copy, a_use_secondary, b_key_len, b_copy,
|
||||
b_use_secondary] = GetParam();
|
||||
|
||||
std::string expected_a, expected_b;
|
||||
// Storage for pinned keys — must outlive the IterKeys.
|
||||
std::string a_storage, b_storage;
|
||||
IterKey a;
|
||||
expected_a =
|
||||
PopulateKey(a, a_key_len, 'a', a_copy, a_use_secondary, a_storage);
|
||||
ASSERT_EQ(a.GetUserKey().ToString(), expected_a);
|
||||
|
||||
{
|
||||
IterKey b;
|
||||
expected_b =
|
||||
PopulateKey(b, b_key_len, 'b', b_copy, b_use_secondary, b_storage);
|
||||
ASSERT_EQ(b.GetUserKey().ToString(), expected_b);
|
||||
|
||||
a.Swap(b);
|
||||
|
||||
// After swap: a has b's old data, b has a's old data.
|
||||
ASSERT_EQ(a.GetUserKey().ToString(), expected_b);
|
||||
ASSERT_EQ(b.GetUserKey().ToString(), expected_a);
|
||||
} // b destroyed here — must not corrupt a's data
|
||||
|
||||
// a must still hold valid data after b's destruction.
|
||||
ASSERT_EQ(a.GetUserKey().ToString(), expected_b);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
IterKeySwap, IterKeySwapTest,
|
||||
::testing::Combine(
|
||||
/*a_key_len=*/::testing::Values(10, 50), // inline vs heap
|
||||
/*a_copy=*/::testing::Bool(),
|
||||
/*a_use_secondary=*/::testing::Bool(),
|
||||
/*b_key_len=*/::testing::Values(10, 50), // inline vs heap
|
||||
/*b_copy=*/::testing::Bool(),
|
||||
/*b_use_secondary=*/::testing::Bool()));
|
||||
|
||||
TEST_F(FormatTest, IterKeyWithTimestampOperation) {
|
||||
IterKey k;
|
||||
k.SetUserKey("");
|
||||
|
||||
@@ -259,6 +259,7 @@ void EventHelpers::NotifyOnErrorRecoveryEnd(
|
||||
db_mutex->Lock();
|
||||
} else {
|
||||
old_bg_error.PermitUncheckedError();
|
||||
new_bg_error.PermitUncheckedError();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3272,6 +3272,56 @@ INSTANTIATE_TEST_CASE_P(ExternalSSTFileBasicTest, ExternalSSTFileBasicTest,
|
||||
std::make_tuple(false, true),
|
||||
std::make_tuple(false, false)));
|
||||
|
||||
// Uses anonymous mmap (lazy-zeroed) so the large data itself doesn't consume
|
||||
// physical memory -- only the SstFileWriter's internal copy does (~4GB peak
|
||||
// during the acceptance case).
|
||||
TEST_F(ExternalSSTFileBasicTest, LargeSizeSstFileWriter) {
|
||||
if (!test::HasBigMem()) {
|
||||
ROCKSDB_GTEST_BYPASS("insufficient memory for reliable continuous testing");
|
||||
return;
|
||||
}
|
||||
|
||||
constexpr size_t kMaxKeySize =
|
||||
size_t{std::numeric_limits<uint32_t>::max()} - 8;
|
||||
constexpr size_t kMaxValueSize = size_t{std::numeric_limits<uint32_t>::max()};
|
||||
|
||||
// --- Large key ---
|
||||
{
|
||||
Options options = CurrentOptions();
|
||||
SstFileWriter sst_file_writer(EnvOptions(), options);
|
||||
ASSERT_OK(sst_file_writer.Open(dbname_ + "/large_key.sst"));
|
||||
|
||||
MemMapping mm = MemMapping::AllocateLazyZeroed(kMaxKeySize + 1);
|
||||
ASSERT_NE(nullptr, mm.Get());
|
||||
|
||||
// A key one byte over the limit should be rejected
|
||||
ASSERT_TRUE(sst_file_writer.Put(mm.AsSlice(), "val").IsInvalidArgument());
|
||||
|
||||
// A key at the limit should be accepted
|
||||
ASSERT_OK(
|
||||
sst_file_writer.Put(Slice(mm.AsSlice().data(), kMaxKeySize), "val"));
|
||||
ASSERT_OK(sst_file_writer.Finish());
|
||||
}
|
||||
|
||||
// --- Large value ---
|
||||
{
|
||||
Options options = CurrentOptions();
|
||||
SstFileWriter sst_file_writer(EnvOptions(), options);
|
||||
ASSERT_OK(sst_file_writer.Open(dbname_ + "/large_value.sst"));
|
||||
|
||||
MemMapping mm = MemMapping::AllocateLazyZeroed(kMaxValueSize + 1);
|
||||
ASSERT_NE(nullptr, mm.Get());
|
||||
|
||||
// A value one byte over the limit should be rejected
|
||||
ASSERT_TRUE(sst_file_writer.Put("key", mm.AsSlice()).IsInvalidArgument());
|
||||
|
||||
// A value at the limit should be accepted
|
||||
ASSERT_OK(
|
||||
sst_file_writer.Put("key", Slice(mm.AsSlice().data(), kMaxValueSize)));
|
||||
ASSERT_OK(sst_file_writer.Finish());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "file/file_util.h"
|
||||
#include "file/random_access_file_reader.h"
|
||||
#include "logging/logging.h"
|
||||
#include "monitoring/statistics_impl.h"
|
||||
#include "table/merging_iterator.h"
|
||||
#include "table/sst_file_writer_collectors.h"
|
||||
#include "table/table_builder.h"
|
||||
@@ -719,6 +720,34 @@ Status ExternalSstFileIngestionJob::AssignLevelsForOneBatch(
|
||||
// This ensures ingested files have proper timestamp ranges in FileMetaData,
|
||||
// similar to files created by flush and compaction.
|
||||
ExtractTimestampFromTableProperties(file->table_properties, &f_metadata);
|
||||
// Retrieve file open metadata for fast SST open
|
||||
if (mutable_db_options_.fast_sst_open) {
|
||||
std::unique_ptr<FSRandomAccessFile> readable_file;
|
||||
FileOptions fopts{env_options_};
|
||||
fopts.file_checksum = f_metadata.file_checksum;
|
||||
fopts.file_checksum_func_name = f_metadata.file_checksum_func_name;
|
||||
IOStatus io_s = fs_->NewRandomAccessFile(file->internal_file_path, fopts,
|
||||
&readable_file, nullptr);
|
||||
if (io_s.ok()) {
|
||||
io_s =
|
||||
readable_file->GetFileOpenMetadata(&f_metadata.file_open_metadata);
|
||||
if (io_s.ok() && !f_metadata.file_open_metadata.empty() &&
|
||||
f_metadata.file_open_metadata.size() <=
|
||||
FSRandomAccessFile::kMaxFileOpenMetadataSize) {
|
||||
RecordTick(db_options_.stats, FILE_OPEN_METADATA_RETRIEVED);
|
||||
} else {
|
||||
if (io_s.ok() && f_metadata.file_open_metadata.size() >
|
||||
FSRandomAccessFile::kMaxFileOpenMetadataSize) {
|
||||
ROCKS_LOG_WARN(db_options_.info_log,
|
||||
"File open metadata for %s too large (%zu bytes), "
|
||||
"ignoring",
|
||||
file->internal_file_path.c_str(),
|
||||
f_metadata.file_open_metadata.size());
|
||||
}
|
||||
f_metadata.file_open_metadata.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
edit_.AddFile(file->picked_level, f_metadata);
|
||||
|
||||
*batch_uppermost_level =
|
||||
|
||||
+61
-26
@@ -23,6 +23,7 @@
|
||||
#include "db/memtable_list.h"
|
||||
#include "db/merge_context.h"
|
||||
#include "db/range_tombstone_fragmenter.h"
|
||||
#include "db/table_cache.h"
|
||||
#include "db/version_edit.h"
|
||||
#include "db/version_set.h"
|
||||
#include "file/file_util.h"
|
||||
@@ -100,7 +101,8 @@ FlushJob::FlushJob(
|
||||
Env::Priority thread_pri, const std::shared_ptr<IOTracer>& io_tracer,
|
||||
std::shared_ptr<const SeqnoToTimeMapping> seqno_to_time_mapping,
|
||||
const std::string& db_id, const std::string& db_session_id,
|
||||
std::string full_history_ts_low, BlobFileCompletionCallback* blob_callback)
|
||||
std::string full_history_ts_low, BlobFileCompletionCallback* blob_callback,
|
||||
bool fast_sst_open)
|
||||
: dbname_(dbname),
|
||||
db_id_(db_id),
|
||||
db_session_id_(db_session_id),
|
||||
@@ -132,6 +134,7 @@ FlushJob::FlushJob(
|
||||
clock_(db_options_.clock),
|
||||
full_history_ts_low_(std::move(full_history_ts_low)),
|
||||
blob_callback_(blob_callback),
|
||||
fast_sst_open_(fast_sst_open),
|
||||
seqno_to_time_mapping_(std::move(seqno_to_time_mapping)) {
|
||||
assert(job_context->snapshot_context_initialized);
|
||||
// Update the thread status to indicate flush.
|
||||
@@ -303,6 +306,8 @@ Status FlushJob::Run(LogsWithPrepTracker* prep_tracker, FileMetaData* file_meta,
|
||||
s = MaybeIncreaseFullHistoryTsLowToAboveCutoffUDT();
|
||||
}
|
||||
|
||||
TEST_SYNC_POINT_CALLBACK("FlushJob::Run:PostBuildTable", &s);
|
||||
|
||||
if (!s.ok()) {
|
||||
cfd_->imm()->RollbackMemtableFlush(
|
||||
mems_, /*rollback_succeeding_memtables=*/!db_options_.atomic_flush);
|
||||
@@ -332,6 +337,18 @@ Status FlushJob::Run(LogsWithPrepTracker* prep_tracker, FileMetaData* file_meta,
|
||||
}
|
||||
}
|
||||
|
||||
if (!s.ok() && meta_.fd.GetFileSize() > 0) {
|
||||
// If BuildTable succeeded (file was cached in table cache for user reads)
|
||||
// but the flush failed to install (e.g., LogAndApply MANIFEST I/O error,
|
||||
// CF dropped, shutdown), evict the cached entry. Without this, the file
|
||||
// is cached but not in any Version, and the FindObsoleteFiles backstop
|
||||
// can fail under metadata read fault injection, causing
|
||||
// TEST_VerifyNoObsoleteFilesCached to fire during Close().
|
||||
TableCache::ReleaseObsolete(cfd_->table_cache()->get_cache().get(),
|
||||
meta_.fd.GetNumber(), nullptr /*handle*/,
|
||||
mutable_cf_options_.uncache_aggressiveness);
|
||||
}
|
||||
|
||||
if (s.ok() && file_meta != nullptr) {
|
||||
*file_meta = meta_;
|
||||
}
|
||||
@@ -571,6 +588,7 @@ Status FlushJob::MemPurge() {
|
||||
auto tombstone = range_del_it->Tombstone();
|
||||
new_first_seqno =
|
||||
tombstone.seq_ < new_first_seqno ? tombstone.seq_ : new_first_seqno;
|
||||
MemTablePostProcessInfo post_process_info;
|
||||
s = new_mem->Add(
|
||||
tombstone.seq_, // Sequence number
|
||||
kTypeRangeDeletion, // KV type
|
||||
@@ -579,12 +597,15 @@ Status FlushJob::MemPurge() {
|
||||
nullptr, // KV protection info set as nullptr since it
|
||||
// should only be useful for the first add to
|
||||
// the original memtable.
|
||||
false, // : allow concurrent_memtable_writes_
|
||||
// Not seen as necessary for now.
|
||||
nullptr, // get_post_process_info(m) must be nullptr
|
||||
// when concurrent_memtable_writes is switched off.
|
||||
true, // allow_concurrent: required for range
|
||||
// deletions since range_del_table_ uses
|
||||
// concurrent-safe SkipList.
|
||||
&post_process_info,
|
||||
nullptr); // hint, only used when concurrent_memtable_writes_
|
||||
// is switched on.
|
||||
if (s.ok()) {
|
||||
new_mem->BatchPostProcess(post_process_info);
|
||||
}
|
||||
|
||||
if (!s.ok()) {
|
||||
break;
|
||||
@@ -868,6 +889,13 @@ Status FlushJob::WriteLevel0Table() {
|
||||
ts_sz > 0 && !cfd_->ioptions().persist_user_defined_timestamps;
|
||||
|
||||
std::vector<BlobFileAddition> blob_file_additions;
|
||||
std::vector<BlobFileGarbage> blob_file_garbages;
|
||||
// Only direct-write memtables can carry blob indexes that reference
|
||||
// pre-existing blob files. Plain flushes write new blob files from inline
|
||||
// values, so there is no pre-existing blob garbage to meter on the input
|
||||
// side.
|
||||
std::vector<BlobFileGarbage>* const blob_file_garbages_for_filtering =
|
||||
cfd_->blob_partition_manager() != nullptr ? &blob_file_garbages : nullptr;
|
||||
// Note that here we treat flush as level 0 compaction in internal stats
|
||||
InternalStats::CompactionStats flush_stats(CompactionReason::kFlush,
|
||||
1 /* count**/);
|
||||
@@ -1011,8 +1039,8 @@ Status FlushJob::WriteLevel0Table() {
|
||||
&io_s, io_tracer_, BlobFileCreationReason::kFlush,
|
||||
seqno_to_time_mapping_.get(), event_logger_, job_context_->job_id,
|
||||
&table_properties_, write_hint, full_history_ts_low, blob_callback_,
|
||||
base_, &memtable_payload_bytes, &memtable_garbage_bytes,
|
||||
&flush_stats);
|
||||
base_, &memtable_payload_bytes, &memtable_garbage_bytes, &flush_stats,
|
||||
blob_file_garbages_for_filtering, fast_sst_open_);
|
||||
TEST_SYNC_POINT_CALLBACK("FlushJob::WriteLevel0Table:s", &s);
|
||||
// TODO: Cleanup io_status in BuildTable and table builders
|
||||
assert(!s.ok() || io_s.ok());
|
||||
@@ -1083,28 +1111,35 @@ Status FlushJob::WriteLevel0Table() {
|
||||
}
|
||||
base_->Unref();
|
||||
|
||||
// Note that if file_size is zero, the file has been deleted and
|
||||
// should not be added to the manifest.
|
||||
// Note that if file_size is zero, the SST has been deleted and should not be
|
||||
// added to the manifest. Blob metadata updates may still need to be
|
||||
// committed for direct-write files or flush-time filtering.
|
||||
const bool has_output = meta_.fd.GetFileSize() > 0;
|
||||
|
||||
if (s.ok() && has_output) {
|
||||
TEST_SYNC_POINT("DBImpl::FlushJob:SSTFileCreated");
|
||||
// if we have more than 1 background thread, then we cannot
|
||||
// insert files directly into higher levels because some other
|
||||
// threads could be concurrently producing compacted files for
|
||||
// that key range.
|
||||
// Add file to L0
|
||||
edit_->AddFile(0 /* level */, meta_.fd.GetNumber(), meta_.fd.GetPathId(),
|
||||
meta_.fd.GetFileSize(), meta_.smallest, meta_.largest,
|
||||
meta_.fd.smallest_seqno, meta_.fd.largest_seqno,
|
||||
meta_.marked_for_compaction, meta_.temperature,
|
||||
meta_.oldest_blob_file_number, meta_.oldest_ancester_time,
|
||||
meta_.file_creation_time, meta_.epoch_number,
|
||||
meta_.file_checksum, meta_.file_checksum_func_name,
|
||||
meta_.unique_id, meta_.compensated_range_deletion_size,
|
||||
meta_.tail_size, meta_.user_defined_timestamps_persisted,
|
||||
meta_.min_timestamp, meta_.max_timestamp);
|
||||
if (s.ok()) {
|
||||
if (has_output) {
|
||||
TEST_SYNC_POINT("DBImpl::FlushJob:SSTFileCreated");
|
||||
// if we have more than 1 background thread, then we cannot
|
||||
// insert files directly into higher levels because some other
|
||||
// threads could be concurrently producing compacted files for
|
||||
// that key range.
|
||||
// Add file to L0
|
||||
TEST_SYNC_POINT_CALLBACK("FileMetaData::FileMetaData", &meta_);
|
||||
edit_->AddFile(0 /* level */, meta_);
|
||||
}
|
||||
|
||||
edit_->SetBlobFileAdditions(std::move(blob_file_additions));
|
||||
for (auto& garbage : blob_file_garbages) {
|
||||
edit_->AddBlobFileGarbage(std::move(garbage));
|
||||
}
|
||||
for (auto& addition : external_blob_file_additions_) {
|
||||
edit_->AddBlobFile(std::move(addition));
|
||||
}
|
||||
for (auto& garbage : external_blob_file_garbages_) {
|
||||
edit_->AddBlobFileGarbage(std::move(garbage));
|
||||
}
|
||||
external_blob_file_additions_.clear();
|
||||
external_blob_file_garbages_.clear();
|
||||
}
|
||||
// Piggyback FlushJobInfo on the first first flushed memtable.
|
||||
mems_[0]->SetFlushJobInfo(GetFlushJobInfo());
|
||||
|
||||
Loaded 100 of 318 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user