mirror of
https://github.com/facebook/rocksdb.git
synced 2026-11-07 11:32:34 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a908554251 | ||
|
|
c5af7e0de2 | ||
|
|
44f3e1d68d | ||
|
|
c31c3ef191 | ||
|
|
87832c60a2 | ||
|
|
2e5f177eeb | ||
|
|
5c9a51b2c2 | ||
|
|
6aa939b288 | ||
|
|
2e985ae69d | ||
|
|
d180f08a66 | ||
|
|
b7f2c73fc8 | ||
|
|
f96414d976 | ||
|
|
e704352e5d | ||
|
|
901c88e37b | ||
|
|
eb5d12e744 | ||
|
|
4e11dd79e1 | ||
|
|
aa7571c3ad | ||
|
|
29819f37e1 | ||
|
|
6d6f7d825b | ||
|
|
4c89ff1102 | ||
|
|
f1a6759b1f | ||
|
|
520c3ecbf1 | ||
|
|
407f02da19 | ||
|
|
98002215d0 | ||
|
|
cfc2a523a3 | ||
|
|
3556c22059 | ||
|
|
61b4edd15e | ||
|
|
d3817f058d | ||
|
|
1f9d8ee302 | ||
|
|
641f4703ac | ||
|
|
d693d5ae26 | ||
|
|
653fd9c65b | ||
|
|
f065e1c95d | ||
|
|
49695ef868 | ||
|
|
09bda51c50 | ||
|
|
5f692d747c | ||
|
|
ebd1000008 | ||
|
|
821bd37d09 | ||
|
|
88ff4f6b12 | ||
|
|
b040ab83e1 | ||
|
|
9f47518676 | ||
|
|
871f79d6ef | ||
|
|
672389fd8c | ||
|
|
c33a4989ad | ||
|
|
7ecc12110c | ||
|
|
d72a471749 | ||
|
|
c3184220b8 | ||
|
|
d8b1893c9d | ||
|
|
3148c6cad4 | ||
|
|
56cb88ec79 | ||
|
|
51feb25567 | ||
|
|
6284a79847 | ||
|
|
8f9cb1a708 | ||
|
|
3695cb6767 | ||
|
|
a668dcbe8c | ||
|
|
6ac0da313e | ||
|
|
47344a0feb | ||
|
|
48ec45d7bb | ||
|
|
c2fab4629b | ||
|
|
82ff0678d4 | ||
|
|
92378eb3b8 | ||
|
|
d1b63738e0 | ||
|
|
feffb67303 | ||
|
|
27d70ecd74 | ||
|
|
656b734a5f | ||
|
|
21a8b5f77f | ||
|
|
e94df3db52 | ||
|
|
83d24db3d5 | ||
|
|
374c8dd635 | ||
|
|
053b0d54dc | ||
|
|
56d40be243 | ||
|
|
148f6c9845 | ||
|
|
6b5ccbbec6 | ||
|
|
80f3d86f21 | ||
|
|
2366f63e4f | ||
|
|
acfea34c91 | ||
|
|
de06ce37db | ||
|
|
a3fe685cdc | ||
|
|
cc691126e5 | ||
|
|
ad218cacf6 | ||
|
|
6a79e02ebd | ||
|
|
a9906f0dd0 | ||
|
|
b89d290c20 | ||
|
|
f84351de98 | ||
|
|
f312633aff | ||
|
|
a6af317476 | ||
|
|
eb5e1a2d1f | ||
|
|
ea5e649225 | ||
|
|
2a7a6a6d72 | ||
|
|
88aff40c97 | ||
|
|
c6d08d3efe | ||
|
|
a1af6f9f64 | ||
|
|
57036b68d9 | ||
|
|
256838180e | ||
|
|
2893c25ca2 | ||
|
|
a6325e9564 | ||
|
|
4bcec5ae89 | ||
|
|
4bf2bcdbb3 | ||
|
|
2b28885c80 | ||
|
|
429b36c22d | ||
|
|
6f03c3dfea | ||
|
|
a7c1acbe9f | ||
|
|
4e10e0bcac | ||
|
|
387cb4aae7 | ||
|
|
8b6f98cdb4 | ||
|
|
1cc1df8dab | ||
|
|
3818cc1aca | ||
|
|
276721cd10 | ||
|
|
45690d0f6a | ||
|
|
e77ba4bc95 | ||
|
|
7e9f54d56b | ||
|
|
41beb1422f |
No files matched your search
+88
@@ -0,0 +1,88 @@
|
||||
# When making changes, verify the output of:
|
||||
# clang-tidy -list-checks
|
||||
---
|
||||
Checks: "-*,\
|
||||
bugprone-argument-comment,\
|
||||
bugprone-dangling-handle,\
|
||||
bugprone-fold-init-type,\
|
||||
bugprone-forward-declaration-namespace,\
|
||||
bugprone-forwarding-reference-overload,\
|
||||
bugprone-shadow,\
|
||||
bugprone-sizeof-*,\
|
||||
bugprone-string-constructor,\
|
||||
bugprone-undefined-memory-manipulation,\
|
||||
bugprone-unused-return-value,\
|
||||
bugprone-use-after-move,\
|
||||
cert-env33-c,\
|
||||
cert-err58-cpp,\
|
||||
cert-msc30-c,\
|
||||
cert-msc50-cpp,\
|
||||
clang-analyzer-core.NullDereference,\
|
||||
clang-analyzer-core.StackAddressEscape,\
|
||||
clang-analyzer-deadcode.DeadStores,\
|
||||
clang-diagnostic-*,\
|
||||
-clang-diagnostic-missing-designated-field-initializers,\
|
||||
concurrency-mt-unsafe,\
|
||||
cppcoreguidelines-avoid-non-const-global-variables,\
|
||||
cppcoreguidelines-missing-std-forward,\
|
||||
cppcoreguidelines-pro-type-member-init,\
|
||||
cppcoreguidelines-special-member-functions,\
|
||||
cppcoreguidelines-virtual-class-destructor,\
|
||||
google-build-using-namespace,\
|
||||
google-explicit-constructor,\
|
||||
google-readability-avoid-underscore-in-googletest-name,\
|
||||
misc-definitions-in-headers,\
|
||||
misc-redundant-expression,\
|
||||
modernize-make-shared,\
|
||||
modernize-use-emplace,\
|
||||
modernize-use-noexcept,\
|
||||
modernize-use-override,\
|
||||
modernize-use-using,\
|
||||
performance-faster-string-find,\
|
||||
performance-for-range-copy,\
|
||||
performance-implicit-conversion-in-loop,\
|
||||
performance-inefficient-algorithm,\
|
||||
performance-inefficient-string-concatenation,\
|
||||
performance-inefficient-vector-operation,\
|
||||
performance-move-const-arg,\
|
||||
performance-move-constructor-init,\
|
||||
performance-no-automatic-move,\
|
||||
performance-no-int-to-ptr,\
|
||||
performance-noexcept-move-constructor,\
|
||||
performance-noexcept-swap,\
|
||||
performance-trivially-destructible,\
|
||||
performance-type-promotion-in-math-fn,\
|
||||
performance-unnecessary-copy-initialization,\
|
||||
performance-unnecessary-value-param,\
|
||||
readability-braces-around-statements,\
|
||||
readability-duplicate-include,\
|
||||
readability-isolate-declaration,\
|
||||
readability-operators-representation,\
|
||||
readability-redundant-string-init"
|
||||
|
||||
WarningsAsErrors: "bugprone-use-after-move"
|
||||
|
||||
CheckOptions:
|
||||
- key: bugprone-easily-swappable-parameters.MinimumLength
|
||||
value: 4
|
||||
- key: cppcoreguidelines-avoid-non-const-global-variables.AllowThreadLocal
|
||||
value: true
|
||||
- key: cppcoreguidelines-special-member-functions.AllowSoleDefaultDtor
|
||||
value: true
|
||||
- key: cppcoreguidelines-special-member-functions.AllowImplicitlyDeletedCopyOrMove
|
||||
value: true
|
||||
- key: modernize-use-using.IgnoreExternC
|
||||
value: true
|
||||
- key: performance-move-const-arg.CheckTriviallyCopyableMove
|
||||
value: false
|
||||
- key: performance-unnecessary-value-param.AllowedTypes
|
||||
value: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$'
|
||||
- key: performance-unnecessary-copy-initialization.AllowedTypes
|
||||
value: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$'
|
||||
- key: readability-operators-representation.BinaryOperators
|
||||
value: '&&;&=;&;|;~;!;!=;||;|=;^;^='
|
||||
- key: readability-redundant-string-init.StringNames
|
||||
value: '::std::basic_string'
|
||||
- key: readability-named-parameter.InsertPlainNamesInForwardDecls
|
||||
value: true
|
||||
...
|
||||
@@ -0,0 +1,21 @@
|
||||
name: cache-getdeps-downloads
|
||||
description: Cache getdeps downloads to avoid unreliable mirrors and speed up builds
|
||||
outputs:
|
||||
cache-hit:
|
||||
description: Whether the cache was hit
|
||||
value: ${{ steps.cache-downloads.outputs.cache-hit }}
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- name: Cache getdeps downloads
|
||||
id: cache-downloads
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
# Use a fixed path that we control - folly.mk will sync with getdeps downloads dir
|
||||
path: /tmp/rocksdb-getdeps-cache
|
||||
# Use a rolling cache key - the cache accumulates downloads over time
|
||||
# The key includes a weekly timestamp to ensure periodic refresh
|
||||
key: getdeps-downloads-${{ runner.os }}-${{ runner.arch }}-week-${{ github.run_id }}
|
||||
restore-keys: |
|
||||
getdeps-downloads-${{ runner.os }}-${{ runner.arch }}-week-
|
||||
getdeps-downloads-${{ runner.os }}-${{ runner.arch }}-
|
||||
@@ -6,6 +6,6 @@ runs:
|
||||
run: |
|
||||
make checkout_folly
|
||||
shell: bash
|
||||
- name: Install patchelf
|
||||
run: apt-get update -y && apt-get install -y patchelf
|
||||
- name: Install patchelf and libaio
|
||||
run: apt-get update -y && apt-get install -y patchelf libaio-dev
|
||||
shell: bash
|
||||
@@ -0,0 +1,58 @@
|
||||
name: Post clang-tidy PR comment
|
||||
on:
|
||||
workflow_run:
|
||||
workflows: ["clang-tidy"]
|
||||
types: [completed]
|
||||
|
||||
permissions:
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
comment:
|
||||
if: github.event.workflow_run.event == 'pull_request'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Download clang-tidy results
|
||||
id: download
|
||||
uses: actions/download-artifact@v4.0.0
|
||||
with:
|
||||
name: clang-tidy-result
|
||||
run-id: ${{ github.event.workflow_run.id }}
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
continue-on-error: true
|
||||
- name: Post or update PR comment
|
||||
if: steps.download.outcome == 'success'
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
if (!fs.existsSync('clang-tidy-comment.md') || !fs.existsSync('pr_number.txt')) {
|
||||
core.info('No clang-tidy results found; skipping.');
|
||||
return;
|
||||
}
|
||||
const body = fs.readFileSync('clang-tidy-comment.md', 'utf8');
|
||||
const prNumber = parseInt(fs.readFileSync('pr_number.txt', 'utf8').trim());
|
||||
const marker = '<!-- clang-tidy-bot -->';
|
||||
const { data: comments } = await github.rest.issues.listComments({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
});
|
||||
const existing = comments.find(c => c.body.includes(marker));
|
||||
if (existing) {
|
||||
await github.rest.issues.updateComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
comment_id: existing.id,
|
||||
body,
|
||||
});
|
||||
core.info(`Updated existing comment ${existing.id}`);
|
||||
} else {
|
||||
await github.rest.issues.createComment({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
issue_number: prNumber,
|
||||
body,
|
||||
});
|
||||
core.info('Created new PR comment');
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
name: clang-tidy
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
permissions: {}
|
||||
|
||||
jobs:
|
||||
clang-tidy:
|
||||
if: github.repository_owner == 'facebook'
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
image: ghcr.io/facebook/rocksdb_ubuntu:24.0
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
with:
|
||||
fetch-depth: 2
|
||||
- name: Mark workspace as safe for git
|
||||
run: git config --global --add safe.directory $GITHUB_WORKSPACE
|
||||
- name: Determine diff base
|
||||
id: diff-base
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "pull_request" ]; then
|
||||
BASE="${{ github.event.pull_request.base.sha }}"
|
||||
git fetch --depth=1 origin "$BASE"
|
||||
else
|
||||
BASE="${{ github.event.before }}"
|
||||
if echo "$BASE" | grep -q '^0\{40\}$'; then
|
||||
echo "skip=true" >> "$GITHUB_OUTPUT"
|
||||
echo "New branch push; skipping clang-tidy."
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
echo "ref=$BASE" >> "$GITHUB_OUTPUT"
|
||||
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
|
||||
- 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 ..
|
||||
cd ..
|
||||
ln -sf build/compile_commands.json compile_commands.json
|
||||
- name: Run clang-tidy on changed files
|
||||
id: clang-tidy
|
||||
if: steps.diff-base.outputs.skip != 'true'
|
||||
run: |
|
||||
python3 tools/run_clang_tidy.py \
|
||||
-j 4 \
|
||||
--diff-base ${{ steps.diff-base.outputs.ref }} \
|
||||
--github-annotations \
|
||||
--github-step-summary \
|
||||
--comment-output clang-tidy-comment.md
|
||||
continue-on-error: true
|
||||
- name: Save PR number
|
||||
if: github.event_name == 'pull_request' && always()
|
||||
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt
|
||||
- name: Upload clang-tidy results
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v4.0.0
|
||||
with:
|
||||
name: clang-tidy-result
|
||||
path: |
|
||||
clang-tidy-comment.md
|
||||
pr_number.txt
|
||||
if-no-files-found: ignore
|
||||
- name: Fail if clang-tidy found issues
|
||||
if: steps.clang-tidy.outcome == 'failure'
|
||||
run: exit 1
|
||||
@@ -54,6 +54,7 @@ jobs:
|
||||
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"
|
||||
- run: LIB_MODE=static USE_CLANG=1 USE_FOLLY=1 COMPILE_WITH_UBSAN=1 COMPILE_WITH_ASAN=1 make -j32 check
|
||||
@@ -68,6 +69,7 @@ jobs:
|
||||
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/cache-folly"
|
||||
id: cache-folly
|
||||
@@ -86,6 +88,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/cache-getdeps-downloads"
|
||||
- uses: "./.github/actions/setup-folly"
|
||||
- run: "DEBUG_LEVEL=0 make -j20 build_folly"
|
||||
- run: "USE_FOLLY=1 LIB_MODE=static DEBUG_LEVEL=0 V=1 make -j20 release"
|
||||
@@ -162,6 +165,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/cache-getdeps-downloads"
|
||||
- uses: "./.github/actions/setup-folly"
|
||||
- run: "(mkdir build && cd build && cmake -DUSE_FOLLY_LITE=1 -DWITH_GFLAGS=1 -DCMAKE_CXX_FLAGS=-DGLOG_USE_GLOG_EXPORT .. && make VERBOSE=1 -j20 && ctest -j20)"
|
||||
- uses: "./.github/actions/post-steps"
|
||||
@@ -1,7 +1,18 @@
|
||||
name: facebook/rocksdb/pr-jobs
|
||||
on: [push, pull_request]
|
||||
permissions: {}
|
||||
env:
|
||||
# Set to a job name to run only that job (on any repo), or leave empty for
|
||||
# normal behavior (all jobs on facebook repo only).
|
||||
ONLY_JOB: ''
|
||||
jobs:
|
||||
config:
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
only_job: ${{ steps.set.outputs.only_job }}
|
||||
steps:
|
||||
- id: set
|
||||
run: echo "only_job=$ONLY_JOB" >> "$GITHUB_OUTPUT"
|
||||
# NOTE: multiple workflows would be recommended, but the current GHA UI in
|
||||
# PRs doesn't make it clear when there's an overall error with a workflow,
|
||||
# making it easy to overlook something broken. Grouping everything into one
|
||||
@@ -19,6 +30,10 @@ jobs:
|
||||
# increasing the risk of misconfiguration, especially on forks that might
|
||||
# want to run with this GHA setup.
|
||||
#
|
||||
# SELECTIVE JOB EXECUTION: Set the ONLY_JOB env var at the top of this file
|
||||
# to a job name (e.g. "build-linux-clang-tidy") to run only that job,
|
||||
# bypassing the repository owner check. Leave it empty for normal behavior.
|
||||
#
|
||||
# DEBUGGING WITH SSH: Temporarily add this as a job step, either before the
|
||||
# step of interest without the "if:" line or after the failing step with the
|
||||
# "if:" line. Then use ssh command printed in CI output.
|
||||
@@ -30,7 +45,8 @@ jobs:
|
||||
|
||||
# ======================== Fast Initial Checks ====================== #
|
||||
check-format-and-targets:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'check-format-and-targets' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
@@ -44,6 +60,10 @@ jobs:
|
||||
run: python -m pip install --upgrade pip
|
||||
- name: Install argparse
|
||||
run: pip install argparse
|
||||
- name: Install clang-format
|
||||
run: |
|
||||
pip install https://files.pythonhosted.org/packages/fb/ac/3c04772acc0257f5730e83adb542b2603c1a62d1315010ab593a980af404/clang_format-21.1.2-py2.py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
|
||||
clang-format --version
|
||||
- name: Download clang-format-diff.py
|
||||
run: wget https://rocksdb-deps.s3.us-west-2.amazonaws.com/llvm/llvm-project/release/12.x/clang/tools/clang-format/clang-format-diff.py
|
||||
- name: Check format
|
||||
@@ -62,7 +82,8 @@ jobs:
|
||||
SANITY_CHECK=1 LONG_TEST=1 tools/check_format_compatible.sh
|
||||
# ========================= Linux With Tests ======================== #
|
||||
build-linux:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -74,7 +95,8 @@ jobs:
|
||||
- run: make V=1 J=32 -j32 check
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-mingw:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-cmake-mingw' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
@@ -93,7 +115,8 @@ jobs:
|
||||
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
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-make-with-folly:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-make-with-folly' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -102,6 +125,7 @@ jobs:
|
||||
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/cache-folly"
|
||||
id: cache-folly
|
||||
@@ -111,7 +135,8 @@ jobs:
|
||||
- run: USE_FOLLY=1 LIB_MODE=static V=1 make -j32 check
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-make-with-folly-lite-no-test:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-make-with-folly-lite-no-test' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -120,11 +145,13 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4.1.0
|
||||
- uses: "./.github/actions/pre-steps"
|
||||
- uses: "./.github/actions/cache-getdeps-downloads"
|
||||
- uses: "./.github/actions/setup-folly"
|
||||
- run: USE_FOLLY_LITE=1 EXTRA_CXXFLAGS=-DGLOG_USE_GLOG_EXPORT V=1 make -j32 all
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-with-folly-coroutines:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-cmake-with-folly-coroutines' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -133,6 +160,7 @@ jobs:
|
||||
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/cache-folly"
|
||||
id: cache-folly
|
||||
@@ -142,7 +170,8 @@ jobs:
|
||||
- run: "(mkdir build && cd build && cmake -DUSE_COROUTINES=1 -DWITH_GFLAGS=1 -DROCKSDB_BUILD_SHARED=0 .. && make VERBOSE=1 -j20 && ctest -j20)"
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-cmake-with-benchmark-no-thread-status:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
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
|
||||
container:
|
||||
@@ -154,7 +183,8 @@ jobs:
|
||||
- run: mkdir build && cd build && cmake -DWITH_GFLAGS=1 -DWITH_BENCHMARK=1 -DCMAKE_CXX_FLAGS=-DNROCKSDB_THREAD_STATUS .. && make VERBOSE=1 -j20 && ctest -j20
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-encrypted_env-no_compression:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-encrypted_env-no_compression' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -168,7 +198,8 @@ jobs:
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ======================== Linux No Test Runs ======================= #
|
||||
build-linux-release:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-release' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -194,7 +225,8 @@ jobs:
|
||||
- run: if ./trace_analyzer --version; then false; else true; fi
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang-13-no_test_run:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang-13-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 8-core-ubuntu
|
||||
container:
|
||||
@@ -210,7 +242,8 @@ jobs:
|
||||
- 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
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang-18-no_test_run:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang-18-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -224,7 +257,8 @@ jobs:
|
||||
- run: CC=clang-18 CXX=clang++-18 USE_CLANG=1 DEBUG_LEVEL=0 make -j32 release
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-gcc-14-no_test_run:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-gcc-14-no_test_run' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -238,7 +272,8 @@ jobs:
|
||||
|
||||
# ======================== Linux Other Checks ======================= #
|
||||
build-linux-clang18-clang-analyze:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang18-clang-analyze' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -256,8 +291,10 @@ jobs:
|
||||
with:
|
||||
name: scan-build-report
|
||||
path: scan_build_report.tar.gz
|
||||
|
||||
build-linux-unity-and-headers:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-unity-and-headers' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
@@ -271,7 +308,8 @@ jobs:
|
||||
- run: make V=1 -j8 -k check-headers
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-mini-crashtest:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
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
|
||||
container:
|
||||
@@ -284,7 +322,8 @@ jobs:
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ======================= Linux with Sanitizers ===================== #
|
||||
build-linux-clang18-asan-ubsan:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang18-asan-ubsan' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 32-core-ubuntu
|
||||
container:
|
||||
@@ -296,7 +335,8 @@ jobs:
|
||||
- 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
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-clang18-mini-tsan:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-clang18-mini-tsan' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 32-core-ubuntu
|
||||
container:
|
||||
@@ -308,7 +348,8 @@ jobs:
|
||||
- run: COMPILE_WITH_TSAN=1 CC=clang-18 CXX=clang++-18 ROCKSDB_DISABLE_ALIGNED_NEW=1 USE_CLANG=1 make V=1 -j32 check
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-static_lib-alt_namespace-status_checked:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
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
|
||||
runs-on:
|
||||
labels: 16-core-ubuntu
|
||||
container:
|
||||
@@ -321,7 +362,8 @@ jobs:
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ========================= MacOS build only ======================== #
|
||||
build-macos:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-macos' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: macos-15-xlarge
|
||||
env:
|
||||
ROCKSDB_DISABLE_JEMALLOC: 1
|
||||
@@ -338,7 +380,8 @@ jobs:
|
||||
- uses: "./.github/actions/post-steps"
|
||||
# ========================= MacOS with Tests ======================== #
|
||||
build-macos-cmake:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-macos-cmake' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: macos-15-xlarge
|
||||
strategy:
|
||||
matrix:
|
||||
@@ -371,7 +414,8 @@ jobs:
|
||||
# ======================== Windows with Tests ======================= #
|
||||
# NOTE: some windows jobs are in "nightly" to save resources
|
||||
build-windows-vs2022:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
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
|
||||
env:
|
||||
CMAKE_GENERATOR: Visual Studio 17 2022
|
||||
@@ -381,7 +425,8 @@ jobs:
|
||||
- uses: "./.github/actions/windows-build-steps"
|
||||
# ============================ Java Jobs ============================ #
|
||||
build-linux-java:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-java' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
@@ -412,7 +457,8 @@ jobs:
|
||||
run: make V=1 J=8 -j8 jtest
|
||||
# post-steps skipped because of compatibility issues with docker image
|
||||
build-linux-java-static:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-java-static' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
@@ -443,7 +489,8 @@ jobs:
|
||||
run: make V=1 J=8 -j8 rocksdbjavastatic
|
||||
# post-steps skipped because of compatibility issues with docker image
|
||||
build-macos-java:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-macos-java' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: macos-15-xlarge
|
||||
env:
|
||||
JAVA_HOME: "/Library/Java/JavaVirtualMachines/liberica-jdk-8.jdk/Contents/Home"
|
||||
@@ -466,7 +513,8 @@ jobs:
|
||||
run: make V=1 J=16 -j16 jtest
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-macos-java-static:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-macos-java-static' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: macos-15-xlarge
|
||||
env:
|
||||
JAVA_HOME: "/Library/Java/JavaVirtualMachines/liberica-jdk-8.jdk/Contents/Home"
|
||||
@@ -488,7 +536,8 @@ jobs:
|
||||
run: make V=1 J=16 -j16 rocksdbjavastaticosx
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-macos-java-static-universal:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-macos-java-static-universal' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on: macos-15-xlarge
|
||||
env:
|
||||
JAVA_HOME: "/Library/Java/JavaVirtualMachines/liberica-jdk-8.jdk/Contents/Home"
|
||||
@@ -510,7 +559,8 @@ jobs:
|
||||
run: make V=1 J=16 -j16 rocksdbjavastaticosx_ub
|
||||
- uses: "./.github/actions/post-steps"
|
||||
build-linux-java-pmd:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-java-pmd' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu
|
||||
container:
|
||||
@@ -536,7 +586,8 @@ jobs:
|
||||
name: maven-site
|
||||
path: "${{ github.workspace }}/java/target/site"
|
||||
build-linux-arm:
|
||||
if: ${{ github.repository_owner == 'facebook' }}
|
||||
if: needs.config.outputs.only_job == 'build-linux-arm' || (needs.config.outputs.only_job == '' && github.repository_owner == 'facebook')
|
||||
needs: config
|
||||
runs-on:
|
||||
labels: 4-core-ubuntu-arm
|
||||
steps:
|
||||
|
||||
@@ -268,6 +268,7 @@ cpp_library_wrapper(name="rocksdb_lib", srcs=[
|
||||
"util/dynamic_bloom.cc",
|
||||
"util/file_checksum_helper.cc",
|
||||
"util/hash.cc",
|
||||
"util/io_dispatcher_imp.cc",
|
||||
"util/murmurhash.cc",
|
||||
"util/random.cc",
|
||||
"util/rate_limiter.cc",
|
||||
@@ -4822,6 +4823,12 @@ cpp_unittest_wrapper(name="db_clip_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_compaction_abort_test",
|
||||
srcs=["db/db_compaction_abort_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="db_compaction_filter_test",
|
||||
srcs=["db/db_compaction_filter_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
@@ -5214,6 +5221,12 @@ cpp_unittest_wrapper(name="interval_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="io_dispatcher_test",
|
||||
srcs=["util/io_dispatcher_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="io_posix_test",
|
||||
srcs=["env/io_posix_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
@@ -5718,6 +5731,12 @@ cpp_unittest_wrapper(name="write_prepared_transaction_test",
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="write_prepared_transaction_test_seqno",
|
||||
srcs=["utilities/transactions/write_prepared_transaction_test_seqno.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
extra_compiler_flags=[])
|
||||
|
||||
|
||||
cpp_unittest_wrapper(name="write_unprepared_transaction_test",
|
||||
srcs=["utilities/transactions/write_unprepared_transaction_test.cc"],
|
||||
deps=[":rocksdb_test_lib"],
|
||||
|
||||
@@ -0,0 +1,312 @@
|
||||
# RocksDB Code Generation and Review Guidance
|
||||
|
||||
This document provides guidance for generating and reviewing code in the RocksDB project, derived from analysis of code review feedback across hundreds of complex merged Pull Requests. Use this as a reference when writing code with AI assistants or conducting code reviews.
|
||||
|
||||
---
|
||||
|
||||
## General Best Practices
|
||||
|
||||
### Code Quality and Maintainability
|
||||
|
||||
**Clarity and Readability:** Write clear, self-documenting code. Use meaningful variable names, add comments for complex logic, and structure code to minimize cognitive load. Avoid clever tricks that sacrifice readability for marginal performance gains unless absolutely necessary.
|
||||
|
||||
**Consistent Style:** Follow existing code style conventions. RocksDB uses `.clang-format` for formatting, specific naming conventions, and structural patterns. Deviations from these patterns are frequently flagged in reviews.
|
||||
|
||||
**Error Handling:** Ensure robust error handling throughout the codebase. Use RocksDB's `Status` type consistently, propagate errors appropriately, and avoid silently ignoring failures. Reviewers pay close attention to edge cases and failure modes.
|
||||
|
||||
### Testing Philosophy
|
||||
|
||||
**Comprehensive Coverage:** Every change should include appropriate test coverage. This includes unit tests for isolated functionality, integration tests for component interactions, and stress tests for concurrency and performance validation. Reviewers will ask for additional tests if coverage is insufficient.
|
||||
|
||||
**Edge Cases and Failure Modes:** Tests should explicitly cover edge cases, boundary conditions, and potential failure scenarios. This is especially important for changes affecting core database operations, compaction, or recovery logic.
|
||||
|
||||
**Platform-Specific Testing:** RocksDB supports multiple platforms (Linux, Windows, macOS) and compilers (GCC, Clang, MSVC). Changes should be tested across relevant platforms, particularly when touching platform-specific code or using compiler-specific features.
|
||||
|
||||
### Performance Considerations
|
||||
|
||||
**⚠️ PERFORMANCE IS CRITICAL:** RocksDB is a high-performance storage engine where every CPU cycle and memory access matters. When writing code, always evaluate from a performance perspective. This is not optional—performance-aware coding is a fundamental requirement for all contributions.
|
||||
|
||||
**Benchmarking and Profiling:** Performance claims should be backed by empirical evidence. Use RocksDB's benchmarking tools (e.g., `db_bench`) to validate improvements. Reviewers will request benchmark results for changes that could impact performance.
|
||||
|
||||
**Memory Allocation:** Minimize dynamic memory allocations, especially in hot paths. Prefer stack allocation over heap allocation. Reuse buffers when possible. Consider using arena allocators or memory pools for frequent small allocations. Every `new`, `malloc`, or container resize has a cost.
|
||||
|
||||
**Memory Copy:** Avoid unnecessary memory copies. Use move semantics, `std::string_view`, `Slice`, and pass-by-reference where appropriate. Be aware of implicit copies in STL containers and function returns. Prefer in-place operations over copy-and-modify patterns.
|
||||
|
||||
**CPU Cache Efficiency:** Design data structures and access patterns to be cache-friendly. Keep frequently accessed data together (data locality). Prefer sequential memory access over random access. Be mindful of cache line sizes (typically 64 bytes) and avoid false sharing in concurrent code. Consider struct packing and field ordering to improve cache utilization.
|
||||
|
||||
**Loop Optimization:** Look for opportunities to collapse nested loops, reduce loop overhead, and minimize branch mispredictions. Hoist invariant computations out of loops. Consider loop unrolling for tight inner loops. Batch operations when possible to amortize per-operation overhead.
|
||||
|
||||
**SIMD and Vectorization:** Leverage SIMD instructions (SSE, AVX) for data-parallel operations when appropriate. Structure data to enable auto-vectorization by the compiler. Consider explicit SIMD intrinsics for critical hot paths like checksum computation, encoding/decoding, and bulk data processing.
|
||||
|
||||
**Branch Prediction:** Minimize unpredictable branches in hot paths. Use `LIKELY`/`UNLIKELY` macros to hint branch prediction. Consider branchless alternatives for simple conditionals. Order switch cases and if-else chains by frequency.
|
||||
|
||||
**Memory and Resource Management:** Be mindful of memory allocations, especially in hot paths. Use RAII patterns, smart pointers, and RocksDB's memory management utilities appropriately.
|
||||
|
||||
**Hot Path Analysis:** When deciding how aggressively to optimize code, consider whether it's on a hot path:
|
||||
- **Hot path** (executed thousands+ times, e.g., data access, iteration, compaction loops): Performance is paramount. Apply all optimization techniques—loop collapsing, SIMD, cache optimization, pre-allocation, etc. The cost of each operation is multiplied by execution frequency.
|
||||
- **Cold path** (executed rarely, e.g., DB open, configuration parsing, error handling): Maintainability and clarity are more important. Prefer readable code over micro-optimizations. Complex optimizations here add maintenance burden with negligible performance benefit.
|
||||
- **Warm path** (moderate frequency): Balance both concerns. Use profiling data to guide optimization decisions.
|
||||
|
||||
**Avoid Premature Optimization:** While performance is critical, focus on correctness first, then optimize based on profiling data. However, be performance-aware from the start—choosing the right algorithm and data structure upfront is not premature optimization. Use the hot path analysis above to decide how much optimization effort is warranted.
|
||||
|
||||
### API Design and Compatibility
|
||||
|
||||
**Backwards Compatibility:** RocksDB maintains strong backwards compatibility guarantees. Breaking changes are rare and require extensive justification. When deprecating features, follow the project's deprecation policy (typically spanning multiple releases).
|
||||
|
||||
**API Consistency:** New APIs should be consistent with existing patterns. Use similar naming conventions, parameter ordering, and return types. Reviewers will suggest changes to improve consistency with the broader codebase.
|
||||
|
||||
**Documentation:** Public APIs must be thoroughly documented. Include usage examples, parameter descriptions, and notes on thread safety, performance characteristics, and compatibility considerations.
|
||||
|
||||
---
|
||||
|
||||
## Component-Specific Guidance
|
||||
|
||||
### Database Core (`db`)
|
||||
|
||||
The database core handles write-ahead logging (WAL), memtables, compaction, and recovery. This component receives the most scrutiny in code reviews.
|
||||
|
||||
**Concurrency and Thread Safety:** Database operations are highly concurrent. Reviewers carefully examine locking strategies, atomic operations, and memory ordering. Document synchronization assumptions clearly. Use appropriate memory ordering semantics (`acquire`/`release` vs. `seq_cst`).
|
||||
|
||||
**Compaction Logic:** Changes to compaction are complex and high-risk. Ensure that compaction logic respects configured parameters, handles edge cases (empty databases, single-file compactions), and maintains correctness under concurrent operations.
|
||||
|
||||
**Error Propagation:** Database operations can fail in many ways (I/O errors, corruption, resource exhaustion). Ensure that errors are properly propagated, logged, and handled. Avoid assertions in production code paths.
|
||||
|
||||
**Testing:** Database core changes require extensive testing, including unit tests, integration tests, and stress tests. Test with various configurations, compaction styles, and concurrent workloads.
|
||||
|
||||
### Public Headers (`include`)
|
||||
|
||||
Public headers define RocksDB's API surface. Changes here have the highest compatibility impact.
|
||||
|
||||
**API Design:** New APIs should be intuitive, consistent with existing patterns, and well-documented. Consider how the API will be used in practice and avoid adding unnecessary complexity.
|
||||
|
||||
**Backwards Compatibility:** Breaking changes to public APIs require extensive justification and a deprecation plan. Maintain ABI compatibility for bug fixes and patch releases.
|
||||
|
||||
**Documentation:** Every public API must be thoroughly documented with usage examples, parameter descriptions, and notes on thread safety and performance characteristics.
|
||||
|
||||
**Deprecation:** When deprecating APIs, follow the project's policy. Mark deprecated APIs clearly, provide migration guidance, and maintain support for at least one major release.
|
||||
|
||||
### Internal Utilities (`util`)
|
||||
|
||||
Internal utilities provide common functionality used throughout the codebase.
|
||||
|
||||
**Code Reuse:** Utilities should be general-purpose and reusable. Avoid duplicating functionality that already exists elsewhere in the codebase.
|
||||
|
||||
**Error Handling:** Utility functions should handle errors robustly and propagate them appropriately. Consider edge cases like overflow, underflow, and invalid inputs.
|
||||
|
||||
**Testing:** Utility functions should have comprehensive test coverage, including edge cases and failure modes. Consider adding death tests for assertions.
|
||||
|
||||
**Performance:** Utilities are often used in hot paths. Ensure that implementations are efficient and avoid unnecessary allocations or copies.
|
||||
|
||||
### Table Management (`table`)
|
||||
|
||||
Table management handles SST file format, block-based tables, and table readers/writers.
|
||||
|
||||
**Block Format and Checksums:** Changes to block format require extreme care. Ensure that checksums are computed and verified correctly. Test with various compression algorithms and block sizes.
|
||||
|
||||
**Iterator Correctness:** Table iterators are used throughout the codebase. Ensure that iterator semantics (Seek, Next, Prev) are correct, especially at boundaries and with deletions.
|
||||
|
||||
**Caching and Prefetching:** Table readers interact with the block cache and prefetching logic. Ensure that cache keys are unique and that prefetching respects configured limits.
|
||||
|
||||
**Performance:** Table operations are performance-critical. Benchmark changes that could impact read or write performance.
|
||||
|
||||
### Utilities (`utilities`)
|
||||
|
||||
Utilities include optional features like transactions, backup engine, and checkpoint.
|
||||
|
||||
**Feature Isolation:** Utilities should be self-contained and not introduce unnecessary dependencies on core database internals.
|
||||
|
||||
**Deprecation and Cleanup:** Legacy features are being phased out. When removing deprecated code, ensure that migration paths are documented and that users have sufficient warning.
|
||||
|
||||
**Cross-Platform Compatibility:** Utilities often interact with OS-specific APIs. Ensure that code works on all supported platforms.
|
||||
|
||||
### Options and Configuration (`options`)
|
||||
|
||||
Options define RocksDB's configuration system.
|
||||
|
||||
**Type Safety:** Use appropriate types for options (e.g., `uint32_t` for flags, scoped enums for enumerated values).
|
||||
|
||||
**Deprecation Policy:** When deprecating options, follow the project's policy. Document the deprecation, provide migration guidance, and maintain support for at least one major release.
|
||||
|
||||
**Dynamic Configuration:** Some options can be changed dynamically. Ensure that dynamic changes are thread-safe and take effect correctly.
|
||||
|
||||
**Validation:** Validate option values and provide clear error messages for invalid configurations.
|
||||
|
||||
### Cache (`cache`)
|
||||
|
||||
Cache management is critical for RocksDB's performance.
|
||||
|
||||
**Concurrency:** Cache operations are highly concurrent. Ensure that implementations are thread-safe and use appropriate synchronization primitives.
|
||||
|
||||
**Performance:** Cache operations are in the hot path. Optimize for low latency and high throughput. Benchmark changes carefully.
|
||||
|
||||
**Memory Management:** Cache implementations must manage memory carefully to avoid leaks and excessive allocations.
|
||||
|
||||
**Eviction Policies:** Changes to eviction policies should be well-tested and benchmarked to ensure they improve overall performance.
|
||||
|
||||
---
|
||||
|
||||
## Code Review Checklist
|
||||
|
||||
When reviewing RocksDB code (or preparing code for review), use this checklist:
|
||||
|
||||
### Correctness
|
||||
- [ ] Does the change preserve database semantics (e.g., snapshot isolation, key ordering)?
|
||||
- [ ] Are all error cases handled appropriately?
|
||||
- [ ] Is the change thread-safe? Are synchronization primitives used correctly?
|
||||
- [ ] Are there any potential data races or deadlocks?
|
||||
|
||||
### Testing
|
||||
- [ ] Does the change include appropriate test coverage?
|
||||
- [ ] Are edge cases and failure modes tested?
|
||||
- [ ] Have the tests been run on all supported platforms?
|
||||
- [ ] Are stress tests passing?
|
||||
|
||||
### Performance
|
||||
- [ ] Are there benchmark results for performance-sensitive changes?
|
||||
- [ ] Does the change avoid unnecessary allocations or copies?
|
||||
- [ ] Are hot paths optimized appropriately?
|
||||
|
||||
### API and Compatibility
|
||||
- [ ] Is the change backwards compatible?
|
||||
- [ ] Are new APIs consistent with existing patterns?
|
||||
- [ ] Is the public API documented?
|
||||
- [ ] Are deprecated features handled according to policy?
|
||||
|
||||
### Code Quality
|
||||
- [ ] Does the code follow RocksDB's style conventions?
|
||||
- [ ] Is the code clear and maintainable?
|
||||
- [ ] Are comments and documentation sufficient?
|
||||
- [ ] Are there any code smells or anti-patterns?
|
||||
|
||||
---
|
||||
|
||||
## Common Review Feedback Patterns
|
||||
|
||||
The following patterns emerged as frequent sources of review feedback:
|
||||
|
||||
1. **Test Coverage:** Reviewers frequently request additional tests for edge cases, platform-specific behavior, and failure modes. Complex changes require comprehensive test coverage including unit tests, integration tests, and stress tests.
|
||||
|
||||
2. **Error Handling:** Ensure proper error propagation using RocksDB's `Status` type. Avoid silent failures and provide clear error messages that include context about what failed and why.
|
||||
|
||||
3. **API Design:** New APIs should be consistent with existing patterns. Use descriptive names that follow established conventions. Avoid breaking changes without strong justification and a clear deprecation plan.
|
||||
|
||||
4. **Documentation:** Public APIs must be documented with usage examples and notes on thread safety, performance characteristics, and compatibility considerations. Complex internal logic should also be well-commented.
|
||||
|
||||
5. **Performance:** Performance-sensitive changes require benchmark results to validate improvements. Use `db_bench` and other profiling tools to measure impact. Avoid premature optimization that adds complexity without measurable benefit.
|
||||
|
||||
6. **Concurrency:** Thread safety is critical in RocksDB. Document synchronization assumptions clearly. Use appropriate memory ordering semantics. Consider potential race conditions and deadlocks.
|
||||
|
||||
7. **Code Style:** Follow existing conventions for naming, formatting, and structure. Use `.clang-format` for consistent formatting. Prefer scoped enums (`enum class`) over unscoped enums.
|
||||
|
||||
8. **Backwards Compatibility:** RocksDB maintains strong compatibility guarantees. Breaking changes require extensive justification. When deprecating features, provide migration guidance and maintain support across multiple releases.
|
||||
|
||||
9. **Refactoring:** Reviewers appreciate refactoring that improves code readability and maintainability. Look for opportunities to deduplicate code and simplify complex logic.
|
||||
|
||||
10. **Platform Compatibility:** Ensure changes work correctly on all supported platforms (Linux, Windows, macOS) and with all supported compilers (GCC, Clang, MSVC).
|
||||
|
||||
---
|
||||
|
||||
## Important tips
|
||||
|
||||
### Build system
|
||||
* There are 3 build system. Make, CMake, BUCK(meta internal).
|
||||
* When a new .cc file is added, update Makefile, CMakeLists.txt, src.mk, BUCK.
|
||||
* Don't manually edit BUCK file, after updating src.mk, run
|
||||
/usr/local/bin/python3 buckifier/buckify_rocksdb.py to update it
|
||||
* Use make to build and run the test. CMake and BUCK are not used locally.
|
||||
* Use `make dbg` command to build all of the unit test in debug mode.
|
||||
* For -j in make command, use the number of CPU cores to decide it.
|
||||
|
||||
### Unit Test
|
||||
* After all of the unit tests are added, review them and try to extract common
|
||||
reusable utility functions to reduce code duplication due to copy past between
|
||||
unit tests. This should be done every time unit test is updated.
|
||||
* Don't use sleep to wait for certain events to happen. This will cause test to
|
||||
be flaky. Instead, use sync point to synchronize thread progress.
|
||||
* Cap unit test execution with 60 seconds timeout.
|
||||
* 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
|
||||
|
||||
### Unit test dedup guidelines
|
||||
* Extract helper functions for repeated patterns such as object
|
||||
construction, round-trip (encode → decode → verify), and common
|
||||
assertion sequences.
|
||||
* Use table-driven tests (struct array + loop) when multiple test cases
|
||||
share the same logic but differ only in input/expected data.
|
||||
* Prefer randomized tests over exhaustive parameter permutations. Use
|
||||
`Random` from `util/random.h` (not `std::mt19937`). Use a time-based
|
||||
seed with `SCOPED_TRACE("seed=" + std::to_string(seed))` so failures
|
||||
are reproducible.
|
||||
* Keep deterministic edge-case tests separate from randomized tests
|
||||
(error paths, boundary conditions, format verification).
|
||||
* Methods only used in tests should be private with `friend class` +
|
||||
`TEST_F` fixture wrappers. In wrappers, always fully qualify the
|
||||
target method to avoid infinite recursion.
|
||||
|
||||
### Adding new public API
|
||||
Refer to claude_md/add_public_api.md
|
||||
|
||||
### Adding new option
|
||||
Refer to claude_md/add_option.md
|
||||
|
||||
### Metrics
|
||||
* When adding a new feature, evaluate whether there is opportunity to add
|
||||
metrics. Try to avoid causing performance regression on hot path when adding
|
||||
metrics.
|
||||
|
||||
### Stress test
|
||||
* When adding a new feature, make sure stress test covers the new option.
|
||||
|
||||
### DB bench update
|
||||
* When adding a performance related feature, support it in db_bench
|
||||
|
||||
### Adding release note
|
||||
* Release note should be kept short at high level for external user consumption.
|
||||
|
||||
### Blog posts (docs/_posts)
|
||||
* Blog post authors must be defined in `docs/_data/authors.yml` to be displayed
|
||||
|
||||
### Final verification of the change
|
||||
* 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.
|
||||
|
||||
### Monitoring make check progress
|
||||
* Use `make check-progress` to get machine-parseable JSON progress while
|
||||
`make check` is running. This is useful for Claude Code to monitor long
|
||||
builds without timeout issues.
|
||||
* Run `make check` in background, then poll progress:
|
||||
```bash
|
||||
make check &
|
||||
# Poll periodically:
|
||||
make check-progress
|
||||
```
|
||||
* The output shows current phase and progress:
|
||||
```json
|
||||
{"status":"running","phase":"compiling","completed":300,"total":919,...}
|
||||
{"status":"running","phase":"testing","completed":1500,"total":29962,"failed":0,"percent":5,...}
|
||||
{"status":"completed","phase":"testing","completed":29962,"total":29962,"failed":0,"percent":100,...}
|
||||
```
|
||||
* Phases: `compiling` -> `linking` -> `generating` -> `testing` -> `completed`
|
||||
* Key fields: `status`, `phase`, `completed`, `total`, `failed`, `percent`
|
||||
* When tests fail, `failed_tests` array shows details (up to 10 failures):
|
||||
```json
|
||||
{"status":"running",...,"failed":3,"failed_tests":[
|
||||
{"test":"cache_test-CacheTest.Usage","exit_code":1,"signal":0,"output":"...test log..."},
|
||||
{"test":"env_test-EnvTest.Open","exit_code":0,"signal":11,"output":"...Segmentation fault..."}
|
||||
]}
|
||||
```
|
||||
* `exit_code`: non-zero means test assertion failed
|
||||
* `signal`: non-zero means test was killed (e.g., 9=SIGKILL, 6=SIGABRT, 11=SIGSEGV)
|
||||
* `output`: last 50 lines of test log including error messages and stack traces
|
||||
|
||||
### Executing benchmark using db_bench
|
||||
* Since the goal is to measure performance, we need to build a release binary
|
||||
using `make clean && DEBUG_LEVEL=0 make db_bench`. If there is an engine
|
||||
crash due to bug, we need to switch back to debug build. Make sure to run
|
||||
`make clean` before running `make dbg`.
|
||||
|
||||
### Formatting code
|
||||
* After making change, use `make format-auto` to auto-apply formatting without
|
||||
interactive prompts (Claude Code friendly).
|
||||
@@ -918,6 +918,7 @@ set(SOURCES
|
||||
util/data_structure.cc
|
||||
util/dynamic_bloom.cc
|
||||
util/hash.cc
|
||||
util/io_dispatcher_imp.cc
|
||||
util/murmurhash.cc
|
||||
util/random.cc
|
||||
util/rate_limiter.cc
|
||||
@@ -1381,6 +1382,7 @@ if(WITH_TESTS)
|
||||
db/db_bloom_filter_test.cc
|
||||
db/db_compaction_filter_test.cc
|
||||
db/db_compaction_test.cc
|
||||
db/db_compaction_abort_test.cc
|
||||
db/db_clip_test.cc
|
||||
db/db_dynamic_level_test.cc
|
||||
db/db_encryption_test.cc
|
||||
@@ -1546,6 +1548,7 @@ if(WITH_TESTS)
|
||||
utilities/transactions/lock/point/point_lock_manager_stress_test.cc
|
||||
utilities/transactions/write_committed_transaction_ts_test.cc
|
||||
utilities/transactions/write_prepared_transaction_test.cc
|
||||
utilities/transactions/write_prepared_transaction_test_seqno.cc
|
||||
utilities/transactions/write_unprepared_transaction_test.cc
|
||||
utilities/transactions/lock/range/range_locking_test.cc
|
||||
utilities/transactions/timestamped_snapshot_test.cc
|
||||
|
||||
+50
-2
@@ -1,9 +1,57 @@
|
||||
# Rocksdb Change Log
|
||||
> NOTE: Entries for next release do not go here. Follow instructions in `unreleased_history/README.txt`
|
||||
|
||||
## 10.10.1 (02/02/2026)
|
||||
## 11.0.4 (03/11/2026)
|
||||
### Public API Changes
|
||||
* Re-remove deprecated DB::Open raw pointer variants in db.h
|
||||
|
||||
## 11.0.3 (03/03/2026)
|
||||
### Public API Changes
|
||||
* Relax option sanitization for kv ratio compaction
|
||||
|
||||
## 11.0.2 (02/26/2026)
|
||||
### Public API Changes
|
||||
* Revert removing deprecated DB::Open raw pointer variants in db.h
|
||||
|
||||
## 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.
|
||||
* Added `CompactionOptionsFIFO::max_data_files_size` to support FIFO compaction trimming based on combined SST and blob file sizes. Added `CompactionOptionsFIFO::use_kv_ratio_compaction` to enable a capacity-derived intra-L0 compaction strategy optimized for BlobDB workloads, producing uniform-sized compacted files for predictable FIFO trimming.
|
||||
* Include interpolation search as an alternative to binary search, which typically performs better when keys are uniformly distributed. This is exposed as a new table option `index_block_search_type`. The default is `binary_search`.
|
||||
|
||||
### Public API Changes
|
||||
* Added new virtual methods `AbortAllCompactions()` and `ResumeAllCompactions()` to the `DB` class. Added new `Status::SubCode::kCompactionAborted` to indicate a compaction was aborted. Added `Status::IsCompactionAborted()` helper method to check if a status represents an aborted compaction.
|
||||
* Drop support for reading (and writing) SST files using `BlockBasedTableOptions.format_version` < 2, which hasn't been the default format for about 10 years. An upgrade path is still possible with full compaction using a RocksDB version >= 4.6.0 and < 11.0.0 and then using the newer version.
|
||||
* Remove deprecated raw `DB*` variants of `DB::Open` and related functions. Some other minor public APIs were updated as a result
|
||||
* Remove deprecated `DB::MaxMemCompactionLevel()`
|
||||
* Remove useless option `CompressedSecondaryCacheOptions::compress_format_version`
|
||||
* Remove deprecated DB option `skip_checking_sst_file_sizes_on_db_open`. The option was deprecated in 10.5.0 and has been a no-op since then. File size validation is now always performed in parallel during DB open.
|
||||
* Remove deprecated `SliceTransform::InRange()` virtual method and the `in_range` callback parameter from `rocksdb_slicetransform_create()` in the C API. `InRange()` was never called by RocksDB and existed only for backward compatibility.
|
||||
* Remove deprecated, unused APIs and options: `ReadOptions::managed` and `ColumnFamilyOptions::snap_refresh_nanos`. Corresponding C and Java APIs are also removed.
|
||||
* Remove deprecated `SstFileWriter::Add()` method (use `Put()` instead) and the deprecated `skip_filters` parameter from `SstFileWriter` constructors (use `BlockBasedTableOptions::filter_policy` set to `nullptr` to skip filter generation instead).
|
||||
|
||||
### Behavior Changes
|
||||
* Change the default value of `CompactionOptionsUniversal::reduce_file_locking` from `false` to `true` to improve write stall and reduce read regression
|
||||
|
||||
### Bug Fixes
|
||||
* Fix Windows VS 2022 build errors.
|
||||
* Fix longstanding failures that can arise from reading and/or compacting old DB dirs with range deletions (likely from version < 5.19.0) in many newer versions.
|
||||
* Fix a bug where WritePrepared/WriteUnprepared TransactionDB with two_write_queues=true could experience "sequence number going backwards" corruption during recovery from a background error, due to allocated-but-not-published sequence numbers not being synced before creating new WAL files.
|
||||
|
||||
### Performance Improvements
|
||||
* Add a new table option `separate_key_value_in_data_block`. When set to true keys and values will be stored separately in the data block, which can result in higher cpu cache hit rate and better compression. Works best with data blocks with sufficient restart intervals and large values. Previous versions of RocksDB will reject files written using this option.
|
||||
|
||||
## 10.11.0 (01/23/2026)
|
||||
### Public API Changes
|
||||
* New SetOptions API that allows setting options for multiple CFs, avoiding the need to reserialize OPTIONS file for each CF
|
||||
* Remove remaining pieces of Lua integration
|
||||
|
||||
### Behavior Changes
|
||||
* The new default for `BlockBasedTableOptions::format_version` is 7, which has been supported since RocksDB 10.4.0 and is required in order to use CompressionManagers supporting custom compression types.
|
||||
|
||||
### Bug Fixes
|
||||
* Fixed a small performance bug with `format_version=7` when decompressing formats other than Snappy and ZSTD.
|
||||
* Fixed an infinite compaction loop bug with User-Defined Timestamps (UDT) where bottommost files were repeatedly marked for compaction even though their timestamp could not be collapsed.
|
||||
* Bugfix for persisted UDT record sequence number zeroing logic.
|
||||
|
||||
## 10.10.0 (12/16/2025)
|
||||
### Bug Fixes
|
||||
|
||||
@@ -390,8 +390,6 @@ ifdef COMPILE_WITH_TSAN
|
||||
# Turn off -pg when enabling TSAN testing, because that induces
|
||||
# a link failure. TODO: find the root cause
|
||||
PROFILING_FLAGS =
|
||||
# LUA is not supported under TSAN
|
||||
LUA_PATH =
|
||||
# Limit keys for crash test under TSAN to avoid error:
|
||||
# "ThreadSanitizer: DenseSlabAllocator overflow. Dying."
|
||||
CRASH_TEST_EXT_ARGS += --max_key=1000000
|
||||
@@ -508,32 +506,6 @@ ifndef DISABLE_WARNING_AS_ERROR
|
||||
endif
|
||||
|
||||
|
||||
ifdef LUA_PATH
|
||||
|
||||
ifndef LUA_INCLUDE
|
||||
LUA_INCLUDE=$(LUA_PATH)/include
|
||||
endif
|
||||
|
||||
LUA_INCLUDE_FILE=$(LUA_INCLUDE)/lualib.h
|
||||
|
||||
ifeq ("$(wildcard $(LUA_INCLUDE_FILE))", "")
|
||||
# LUA_INCLUDE_FILE does not exist
|
||||
$(error Cannot find lualib.h under $(LUA_INCLUDE). Try to specify both LUA_PATH and LUA_INCLUDE manually)
|
||||
endif
|
||||
LUA_FLAGS = -I$(LUA_INCLUDE) -DLUA -DLUA_COMPAT_ALL
|
||||
CFLAGS += $(LUA_FLAGS)
|
||||
CXXFLAGS += $(LUA_FLAGS)
|
||||
|
||||
ifndef LUA_LIB
|
||||
LUA_LIB = $(LUA_PATH)/lib/liblua.a
|
||||
endif
|
||||
ifeq ("$(wildcard $(LUA_LIB))", "") # LUA_LIB does not exist
|
||||
$(error $(LUA_LIB) does not exist. Try to specify both LUA_PATH and LUA_LIB manually)
|
||||
endif
|
||||
EXEC_LDFLAGS += $(LUA_LIB)
|
||||
|
||||
endif
|
||||
|
||||
ifeq ($(NO_THREEWAY_CRC32C), 1)
|
||||
CXXFLAGS += -DNO_THREEWAY_CRC32C
|
||||
endif
|
||||
@@ -604,8 +576,8 @@ ifneq ($(filter check-headers, $(MAKECMDGOALS)),)
|
||||
# TODO: add/support JNI headers
|
||||
DEV_HEADER_DIRS := $(sort include/ $(dir $(ALL_SOURCES)))
|
||||
# Some headers like in port/ are platform-specific
|
||||
DEV_HEADERS_TO_CHECK := $(shell $(FIND) $(DEV_HEADER_DIRS) -type f -name '*.h' | grep -E -v 'port/|plugin/|lua/|range_tree/|secondary_index/')
|
||||
PUBLIC_HEADERS_TO_CHECK := $(shell $(FIND) include/ -type f -name '*.h' | grep -E -v 'lua/')
|
||||
DEV_HEADERS_TO_CHECK := $(shell $(FIND) $(DEV_HEADER_DIRS) -type f -name '*.h' | grep -E -v 'port/|plugin/|range_tree/|secondary_index/')
|
||||
PUBLIC_HEADERS_TO_CHECK := $(shell $(FIND) include/ -type f -name '*.h')
|
||||
else
|
||||
DEV_HEADERS_TO_CHECK :=
|
||||
PUBLIC_HEADERS_TO_CHECK :=
|
||||
@@ -833,7 +805,7 @@ endif # PLATFORM_SHARED_EXT
|
||||
.PHONY: check clean coverage ldb_tests package dbg gen-pc build_size \
|
||||
release tags tags0 valgrind_check format static_lib shared_lib all \
|
||||
rocksdbjavastatic rocksdbjava install install-static install-shared \
|
||||
uninstall analyze tools tools_lib check-headers checkout_folly
|
||||
uninstall analyze tools tools_lib check-headers checkout_folly clang-tidy
|
||||
|
||||
all: $(LIBRARY) $(BENCHMARKS) tools tools_lib test_libs $(TESTS)
|
||||
|
||||
@@ -1021,6 +993,11 @@ watch-log:
|
||||
dump-log:
|
||||
bash -c '$(quoted_perl_command)' < LOG
|
||||
|
||||
# Machine-parseable progress output for automated monitoring (e.g., Claude Code)
|
||||
# Outputs JSON: {"status":"running","completed":45,"total":100,"failed":0,"percent":45,"eta_seconds":120}
|
||||
check-progress:
|
||||
@build_tools/check_progress.sh
|
||||
|
||||
# If J != 1 and GNU parallel is installed, run the tests in parallel,
|
||||
# via the check_0 rule above. Otherwise, run them sequentially.
|
||||
check: all
|
||||
@@ -1232,6 +1209,10 @@ tags0:
|
||||
format:
|
||||
build_tools/format-diff.sh
|
||||
|
||||
# Non-interactive format (auto-apply without prompts, for CI/automation/Claude Code)
|
||||
format-auto:
|
||||
build_tools/format-diff.sh -y
|
||||
|
||||
check-format:
|
||||
build_tools/format-diff.sh -c
|
||||
|
||||
@@ -1241,6 +1222,15 @@ check-buck-targets:
|
||||
check-sources:
|
||||
build_tools/check-sources.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:
|
||||
# make clang-tidy CLANG_TIDY_BINARY=/usr/bin/clang-tidy CLANG_TIDY_JOBS=8
|
||||
CLANG_TIDY_BINARY ?= /opt/homebrew/opt/llvm/bin/clang-tidy
|
||||
CLANG_TIDY_JOBS ?= $(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
|
||||
clang-tidy:
|
||||
python3 tools/run_clang_tidy.py --clang-tidy-binary $(CLANG_TIDY_BINARY) -j $(CLANG_TIDY_JOBS)
|
||||
|
||||
package:
|
||||
bash build_tools/make_package.sh $(SHARED_MAJOR).$(SHARED_MINOR)
|
||||
|
||||
@@ -1470,6 +1460,9 @@ db_compaction_filter_test: $(OBJ_DIR)/db/db_compaction_filter_test.o $(TEST_LIBR
|
||||
db_compaction_test: $(OBJ_DIR)/db/db_compaction_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_compaction_abort_test: $(OBJ_DIR)/db/db_compaction_abort_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
db_clip_test: $(OBJ_DIR)/db/db_clip_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -1845,6 +1838,9 @@ write_committed_transaction_ts_test: $(OBJ_DIR)/utilities/transactions/write_com
|
||||
write_prepared_transaction_test: $(OBJ_DIR)/utilities/transactions/write_prepared_transaction_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
write_prepared_transaction_test_seqno: $(OBJ_DIR)/utilities/transactions/write_prepared_transaction_test_seqno.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
write_unprepared_transaction_test: $(OBJ_DIR)/utilities/transactions/write_unprepared_transaction_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -1950,6 +1946,9 @@ blob_source_test: $(OBJ_DIR)/db/blob/blob_source_test.o $(TEST_LIBRARY) $(LIBRAR
|
||||
blob_garbage_meter_test: $(OBJ_DIR)/db/blob/blob_garbage_meter_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
io_dispatcher_test: $(OBJ_DIR)/util/io_dispatcher_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
timer_test: $(OBJ_DIR)/util/timer_test.o $(TEST_LIBRARY) $(LIBRARY)
|
||||
$(AM_LINK)
|
||||
|
||||
@@ -2562,7 +2561,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 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 clang-tidy jclean jtest package analyze tags rocksdbjavastatic% unity.% unity_test checkout_folly, $(MAKECMDGOALS))
|
||||
ifneq ("$(ROCKS_DEP_RULES)", "")
|
||||
-include $(DEPFILES)
|
||||
endif
|
||||
@@ -831,7 +831,6 @@ fi
|
||||
if test -n "$WITH_JEMALLOC_FLAG"; then
|
||||
echo "WITH_JEMALLOC_FLAG=$WITH_JEMALLOC_FLAG" >> "$OUTPUT"
|
||||
fi
|
||||
echo "LUA_PATH=$LUA_PATH" >> "$OUTPUT"
|
||||
if test -n "$USE_FOLLY"; then
|
||||
echo "USE_FOLLY=$USE_FOLLY" >> "$OUTPUT"
|
||||
fi
|
||||
|
||||
@@ -8,7 +8,7 @@ BAD=""
|
||||
|
||||
# Look for potential for ODR violations caused by public headers depending on
|
||||
# build parameters that could vary between RocksDB build and application build.
|
||||
# * Cases like LUA, ROCKSDB_NAMESPACE, and ROCKSDB_ASSERT_STATUS_CHECKED are
|
||||
# * Cases like ROCKSDB_NAMESPACE, and ROCKSDB_ASSERT_STATUS_CHECKED are
|
||||
# intentional, hard to avoid. (We expect definitions to change and the user
|
||||
# should also.)
|
||||
# * Cases like _WIN32, OS_WIN, and __cplusplus are essentially ODR-safe.
|
||||
@@ -19,7 +19,7 @@ BAD=""
|
||||
# that should not cause ODR violations can be exempted with the ODR-SAFE
|
||||
# marker recognized here.
|
||||
|
||||
grep -nHE '^#if' -- "$@" | grep -vE 'ROCKSDB_NAMESPACE|ROCKSDB_ASSERT_STATUS_CHECKED|LUA|_WIN32|OS_WIN|ODR-SAFE|__cplusplus|ROCKSDB_DLL|ROCKSDB_LIBRARY_EXPORTS'
|
||||
grep -nHE '^#if' -- "$@" | grep -vE 'ROCKSDB_NAMESPACE|ROCKSDB_ASSERT_STATUS_CHECKED|_WIN32|OS_WIN|ODR-SAFE|__cplusplus|ROCKSDB_DLL|ROCKSDB_LIBRARY_EXPORTS'
|
||||
if [ "$?" != "1" ]; then
|
||||
echo "^^^^^ #if in public API could cause an ODR violation."
|
||||
echo " Add // ODR-SAFE if verified safe."
|
||||
|
||||
Executable
+231
@@ -0,0 +1,231 @@
|
||||
#!/bin/bash
|
||||
# Output test progress in JSON format for machine parsing
|
||||
# Usage: build_tools/check_progress.sh
|
||||
|
||||
LOG_FILE="LOG"
|
||||
T_DIR="t"
|
||||
SRC_MK="src.mk"
|
||||
|
||||
# Maximum lines of test output to include per failed test
|
||||
MAX_OUTPUT_LINES=50
|
||||
|
||||
# Helper to escape string for JSON (handles newlines, quotes, backslashes, tabs)
|
||||
json_escape() {
|
||||
local str="$1"
|
||||
# Use python for reliable JSON escaping if available, otherwise use sed
|
||||
if command -v python3 &>/dev/null; then
|
||||
printf '%s' "$str" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read())[1:-1], end="")'
|
||||
else
|
||||
printf '%s' "$str" | sed 's/\\/\\\\/g; s/"/\\"/g; s/\t/\\t/g; s/\r/\\r/g' | awk '{printf "%s\\n", $0}' | sed 's/\\n$//'
|
||||
fi
|
||||
}
|
||||
|
||||
# Helper to output JSON and exit
|
||||
output_json() {
|
||||
local status="$1"
|
||||
local completed="${2:-0}"
|
||||
local total="${3:-0}"
|
||||
local failed="${4:-0}"
|
||||
local percent="${5:-0}"
|
||||
local eta="${6:-0}"
|
||||
local avg_time="${7:-0}"
|
||||
local last_item="${8:-}"
|
||||
local phase="${9:-}"
|
||||
local failed_tests="${10:-}"
|
||||
|
||||
# Build JSON output
|
||||
local json="{\"status\":\"$status\""
|
||||
|
||||
if [[ -n "$phase" ]]; then
|
||||
json="$json,\"phase\":\"$phase\""
|
||||
fi
|
||||
|
||||
json="$json,\"completed\":$completed,\"total\":$total,\"failed\":$failed,\"percent\":$percent"
|
||||
json="$json,\"eta_seconds\":$eta,\"avg_time\":\"$avg_time\",\"last_item\":\"$(json_escape "$last_item")\""
|
||||
|
||||
if [[ -n "$failed_tests" ]]; then
|
||||
json="$json,\"failed_tests\":[$failed_tests]"
|
||||
fi
|
||||
|
||||
json="$json}"
|
||||
echo "$json"
|
||||
}
|
||||
|
||||
# Get failed test info with log output
|
||||
get_failed_tests_json() {
|
||||
local log_file="$1"
|
||||
local t_dir="$2"
|
||||
local max_failures=10
|
||||
local count=0
|
||||
local first=true
|
||||
|
||||
# Get failed tests from LOG file
|
||||
while IFS=$'\t' read -r seq host starttime runtime send recv exitval signal cmd; do
|
||||
# Skip header line
|
||||
[[ "$seq" == "Seq" ]] && continue
|
||||
|
||||
# Check if failed (exitval != 0 or signal != 0)
|
||||
if [[ "$exitval" != "0" || "$signal" != "0" ]]; then
|
||||
# Extract test name from command
|
||||
test_name=$(echo "$cmd" | sed 's,.*/run-,,;s, .*,,')
|
||||
|
||||
# Get log file path
|
||||
log_path="$t_dir/log-run-$test_name"
|
||||
|
||||
# Read test output (last N lines)
|
||||
if [[ -f "$log_path" ]]; then
|
||||
output=$(tail -n "$MAX_OUTPUT_LINES" "$log_path" 2>/dev/null)
|
||||
else
|
||||
output="(log file not found: $log_path)"
|
||||
fi
|
||||
|
||||
# Escape output for JSON
|
||||
escaped_output=$(json_escape "$output")
|
||||
|
||||
# Build JSON object for this failure
|
||||
if [[ "$first" == "true" ]]; then
|
||||
first=false
|
||||
else
|
||||
printf ","
|
||||
fi
|
||||
printf '{"test":"%s","exit_code":%d,"signal":%d,"output":"%s"}' \
|
||||
"$test_name" "$exitval" "$signal" "$escaped_output"
|
||||
|
||||
((count++))
|
||||
if [[ $count -ge $max_failures ]]; then
|
||||
break
|
||||
fi
|
||||
fi
|
||||
done < "$log_file"
|
||||
}
|
||||
|
||||
# Check if tests are running (LOG file exists)
|
||||
if [[ -f "$LOG_FILE" ]]; then
|
||||
# Count total tests from t/run-* files
|
||||
if [[ -d "$T_DIR" ]]; then
|
||||
total=$(find "$T_DIR" -name 'run-*' -type f 2>/dev/null | wc -l)
|
||||
else
|
||||
total=0
|
||||
fi
|
||||
|
||||
# If no parallel tests generated yet
|
||||
if [[ "$total" -eq 0 ]]; then
|
||||
output_json "running" 0 0 0 0 0 "0" "" "generating"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Parse LOG file (skip header line)
|
||||
# LOG format: Seq Host Starttime JobRuntime Send Receive Exitval Signal Command
|
||||
completed=$(tail -n +2 "$LOG_FILE" 2>/dev/null | wc -l)
|
||||
|
||||
# Count failures
|
||||
failed=$(awk -F'\t' 'NR>1 && ($7 != 0 || $8 != 0) {count++} END {print count+0}' "$LOG_FILE" 2>/dev/null)
|
||||
|
||||
# Get failed tests JSON with output (only if there are failures)
|
||||
if [[ "$failed" -gt 0 ]]; then
|
||||
failed_tests=$(get_failed_tests_json "$LOG_FILE" "$T_DIR")
|
||||
else
|
||||
failed_tests=""
|
||||
fi
|
||||
|
||||
# Calculate percentage
|
||||
if [[ "$total" -gt 0 ]]; then
|
||||
percent=$((completed * 100 / total))
|
||||
else
|
||||
percent=0
|
||||
fi
|
||||
|
||||
# Get last completed test name (extract from command column)
|
||||
last_test=$(tail -1 "$LOG_FILE" 2>/dev/null | awk -F'\t' '{print $9}' | sed 's,.*/run-,,;s, .*,,;s,^./,,')
|
||||
|
||||
# Calculate ETA based on average time
|
||||
if [[ "$completed" -gt 0 ]]; then
|
||||
avg_time=$(awk -F'\t' 'NR>1 {sum+=$4; count++} END {if(count>0) printf "%.1f", sum/count; else print "0"}' "$LOG_FILE")
|
||||
remaining=$((total - completed))
|
||||
eta=$(awk "BEGIN {printf \"%.0f\", $avg_time * $remaining}")
|
||||
else
|
||||
avg_time="0"
|
||||
eta="0"
|
||||
fi
|
||||
|
||||
# Determine status
|
||||
if [[ "$completed" -ge "$total" ]]; then
|
||||
status="completed"
|
||||
elif [[ "$completed" -gt 0 ]]; then
|
||||
status="running"
|
||||
else
|
||||
status="starting"
|
||||
fi
|
||||
|
||||
output_json "$status" "$completed" "$total" "$failed" "$percent" "$eta" "$avg_time" "$last_test" "testing" "$failed_tests"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# No LOG file - check if we're in compilation/linking phase
|
||||
# Count expected source files from src.mk
|
||||
if [[ -f "$SRC_MK" ]]; then
|
||||
# Count LIB_SOURCES (library object files to compile)
|
||||
expected_lib_objects=$(grep -E '\.cc\s*\\?$' "$SRC_MK" | grep -v '^#' | wc -l)
|
||||
|
||||
# Count TEST_MAIN_SOURCES (test binaries to link)
|
||||
expected_test_binaries=$(sed -n '/^TEST_MAIN_SOURCES =/,/^[^ ]/p' "$SRC_MK" | grep -cE '\.cc\s*\\?$' 2>/dev/null || echo 0)
|
||||
else
|
||||
expected_lib_objects=0
|
||||
expected_test_binaries=0
|
||||
fi
|
||||
|
||||
# Check for test generation phase (t/ directory being created)
|
||||
if [[ -d "$T_DIR" ]]; then
|
||||
total=$(find "$T_DIR" -name 'run-*' -type f 2>/dev/null | wc -l)
|
||||
if [[ "$total" -gt 0 ]]; then
|
||||
output_json "running" 0 "$total" 0 0 0 "0" "" "generating"
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Count compiled object files (in subdirectories matching source structure)
|
||||
# Object files are created as dir/file.o (e.g., cache/cache.o, db/db_impl.o)
|
||||
compiled_objects=0
|
||||
if [[ "$expected_lib_objects" -gt 0 ]]; then
|
||||
# Count .o files in source directories
|
||||
compiled_objects=$(find cache db env file logging memory memtable monitoring options port table test_util trace_replay util utilities -name '*.o' -type f 2>/dev/null | wc -l)
|
||||
fi
|
||||
|
||||
# Count linked test binaries (test binaries are in current directory with _test suffix)
|
||||
linked_tests=0
|
||||
if [[ "$expected_test_binaries" -gt 0 ]]; then
|
||||
linked_tests=$(find . -maxdepth 1 -name '*_test' -type f -executable 2>/dev/null | wc -l)
|
||||
fi
|
||||
|
||||
# Determine phase based on what exists
|
||||
if [[ "$compiled_objects" -eq 0 && "$linked_tests" -eq 0 ]]; then
|
||||
# Nothing compiled yet - not started or just beginning
|
||||
output_json "not_started" 0 0 0 0 0 "0" ""
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Calculate total work units: compiling + linking
|
||||
total_work=$((expected_lib_objects + expected_test_binaries))
|
||||
completed_work=$((compiled_objects + linked_tests))
|
||||
|
||||
if [[ "$total_work" -gt 0 ]]; then
|
||||
percent=$((completed_work * 100 / total_work))
|
||||
else
|
||||
percent=0
|
||||
fi
|
||||
|
||||
# Determine phase
|
||||
if [[ "$compiled_objects" -lt "$expected_lib_objects" ]]; then
|
||||
phase="compiling"
|
||||
# Get most recently modified .o file as last_item
|
||||
last_item=$(find cache db env file logging memory memtable monitoring options port table test_util trace_replay util utilities -name '*.o' -type f -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | cut -d' ' -f2- | sed 's,^\./,,;s,\.o$,,')
|
||||
elif [[ "$linked_tests" -lt "$expected_test_binaries" ]]; then
|
||||
phase="linking"
|
||||
# Get most recently modified test binary as last_item
|
||||
last_item=$(find . -maxdepth 1 -name '*_test' -type f -executable -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | cut -d' ' -f2- | sed 's,^\./,,')
|
||||
else
|
||||
phase="generating"
|
||||
last_item=""
|
||||
fi
|
||||
|
||||
output_json "running" "$completed_work" "$total_work" 0 "$percent" 0 "0" "$last_item" "$phase"
|
||||
@@ -19,4 +19,3 @@ BENCHMARK_BASE=/mnt/gvfs/third-party2/benchmark/780c7a0f9cf0967961e69ad08e61cddd
|
||||
KERNEL_HEADERS_BASE=/mnt/gvfs/third-party2/kernel-headers/624a2f8f6c93c3c1df8aa4a6255d8202631a6c80/fb/platform010/da39a3e
|
||||
BINUTILS_BASE=/mnt/gvfs/third-party2/binutils/39579e8603b48b3540f8b0633f43adf29acccb8b/2.37/centos8-native/da39a3e
|
||||
VALGRIND_BASE=/mnt/gvfs/third-party2/valgrind/cd9cc656d49ecb53797ce4d055e49fde29fd57ff/3.19.0/platform010/76ebdda
|
||||
LUA_BASE=/mnt/gvfs/third-party2/lua/363787fa5cac2a8aa20638909210443278fa138e/5.3.4/platform010/9079c97
|
||||
@@ -164,12 +164,4 @@ EXEC_LDFLAGS_SHARED="$SNAPPY_LIBS $ZLIB_LIBS $BZIP_LIBS $LZ4_LIBS $ZSTD_LIBS $GF
|
||||
|
||||
VALGRIND_VER="$VALGRIND_BASE/bin/"
|
||||
|
||||
LUA_PATH="$LUA_BASE"
|
||||
|
||||
if test -z $PIC_BUILD; then
|
||||
LUA_LIB=" $LUA_PATH/lib/liblua.a"
|
||||
else
|
||||
LUA_LIB=" $LUA_PATH/lib/liblua_pic.a"
|
||||
fi
|
||||
|
||||
export CC CXX AR CFLAGS CXXFLAGS EXEC_LDFLAGS EXEC_LDFLAGS_SHARED VALGRIND_VER JEMALLOC_LIB JEMALLOC_INCLUDE CLANG_ANALYZER CLANG_SCAN_BUILD LUA_PATH LUA_LIB
|
||||
export CC CXX AR CFLAGS CXXFLAGS EXEC_LDFLAGS EXEC_LDFLAGS_SHARED VALGRIND_VER JEMALLOC_LIB JEMALLOC_INCLUDE CLANG_ANALYZER CLANG_SCAN_BUILD
|
||||
@@ -172,4 +172,4 @@ EXEC_LDFLAGS_SHARED="$SNAPPY_LIBS $ZLIB_LIBS $BZIP_LIBS $LZ4_LIBS $ZSTD_LIBS $GF
|
||||
|
||||
VALGRIND_VER="$VALGRIND_BASE/bin/"
|
||||
|
||||
export CC CXX AR AS CFLAGS CXXFLAGS EXEC_LDFLAGS EXEC_LDFLAGS_SHARED VALGRIND_VER JEMALLOC_LIB JEMALLOC_INCLUDE CLANG_ANALYZER CLANG_SCAN_BUILD LUA_PATH LUA_LIB
|
||||
export CC CXX AR AS CFLAGS CXXFLAGS EXEC_LDFLAGS EXEC_LDFLAGS_SHARED VALGRIND_VER JEMALLOC_LIB JEMALLOC_INCLUDE CLANG_ANALYZER CLANG_SCAN_BUILD
|
||||
@@ -7,14 +7,18 @@ print_usage () {
|
||||
echo "Usage:"
|
||||
echo "format-diff.sh [OPTIONS]"
|
||||
echo "-c: check only."
|
||||
echo "-y: auto-apply formatting without prompts (non-interactive mode)."
|
||||
echo "-h: print this message."
|
||||
}
|
||||
|
||||
while getopts ':ch' OPTION; do
|
||||
while getopts ':cyh' OPTION; do
|
||||
case "$OPTION" in
|
||||
c)
|
||||
CHECK_ONLY=1
|
||||
;;
|
||||
y)
|
||||
AUTO_APPLY=1
|
||||
;;
|
||||
h)
|
||||
print_usage
|
||||
exit 1
|
||||
@@ -240,11 +244,16 @@ echo "$diffs" |
|
||||
sed -e "s/\(^-.*$\)/`echo -e \"$COLOR_RED\1$COLOR_END\"`/" |
|
||||
sed -e "s/\(^+.*$\)/`echo -e \"$COLOR_GREEN\1$COLOR_END\"`/"
|
||||
|
||||
echo -e "Would you like to fix the format automatically (y/n): \c"
|
||||
# Handle auto-apply mode (non-interactive)
|
||||
if [ "$AUTO_APPLY" ]; then
|
||||
to_fix="y"
|
||||
else
|
||||
echo -e "Would you like to fix the format automatically (y/n): \c"
|
||||
|
||||
# Make sure under any mode, we can read user input.
|
||||
exec < /dev/tty
|
||||
read to_fix
|
||||
# Make sure under any mode, we can read user input.
|
||||
exec < /dev/tty
|
||||
read to_fix
|
||||
fi
|
||||
|
||||
if [ "$to_fix" != "y" ]
|
||||
then
|
||||
@@ -261,7 +270,8 @@ fi
|
||||
echo "Files reformatted!"
|
||||
|
||||
# Amend to last commit if user do the post-commit format check
|
||||
if [ -z "$uncommitted_code" ]; then
|
||||
# Skip amend prompt in auto-apply mode (user can amend manually if desired)
|
||||
if [ -z "$uncommitted_code" ] && [ -z "$AUTO_APPLY" ]; then
|
||||
echo -e "Would you like to amend the changes to last commit (`git log HEAD --oneline | head -1`)? (y/n): \c"
|
||||
read to_amend
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Pre-download packages with unreliable mirrors using fallback mirrors.
|
||||
Reads package info from folly's getdeps manifest files.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import hashlib
|
||||
import subprocess
|
||||
import configparser
|
||||
|
||||
def sha256_file(path):
|
||||
"""Calculate SHA256 hash of a file."""
|
||||
h = hashlib.sha256()
|
||||
try:
|
||||
with open(path, 'rb') as f:
|
||||
for chunk in iter(lambda: f.read(65536), b''):
|
||||
h.update(chunk)
|
||||
return h.hexdigest()
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def parse_manifest(manifest_path):
|
||||
"""Parse a getdeps manifest file to extract download info."""
|
||||
config = configparser.ConfigParser()
|
||||
try:
|
||||
config.read(manifest_path)
|
||||
if 'download' in config:
|
||||
return {
|
||||
'url': config['download'].get('url', ''),
|
||||
'sha256': config['download'].get('sha256', ''),
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
def get_fallback_mirrors(url):
|
||||
"""Get fallback mirror URLs for a given URL."""
|
||||
# Fallback mirror patterns for known unreliable hosts
|
||||
mirror_fallbacks = {
|
||||
"ftp.gnu.org/gnu/": [
|
||||
"https://mirrors.kernel.org/gnu/",
|
||||
"https://ftpmirror.gnu.org/gnu/",
|
||||
"https://ftp.gnu.org/gnu/",
|
||||
],
|
||||
"ftpmirror.gnu.org/gnu/": [
|
||||
"https://mirrors.kernel.org/gnu/",
|
||||
"https://ftpmirror.gnu.org/gnu/",
|
||||
"https://ftp.gnu.org/gnu/",
|
||||
],
|
||||
}
|
||||
|
||||
for pattern, mirrors in mirror_fallbacks.items():
|
||||
if pattern in url:
|
||||
# Extract the path after the pattern
|
||||
path_start = url.find(pattern) + len(pattern)
|
||||
path = url[path_start:]
|
||||
return [mirror + path for mirror in mirrors]
|
||||
return [url] # No fallback, use original
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 4:
|
||||
print(f"Usage: {sys.argv[0]} <download_dir> <cache_dir> <manifests_dir>")
|
||||
sys.exit(1)
|
||||
|
||||
download_dir, cache_dir, manifests_dir = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||
|
||||
# Packages known to have unreliable mirrors
|
||||
packages_to_check = ["autoconf", "automake", "libtool"]
|
||||
|
||||
for package in packages_to_check:
|
||||
manifest_path = os.path.join(manifests_dir, package)
|
||||
if not os.path.exists(manifest_path):
|
||||
continue
|
||||
|
||||
info = parse_manifest(manifest_path)
|
||||
if not info or not info['url'] or not info['sha256']:
|
||||
continue
|
||||
|
||||
# Determine filename from URL
|
||||
url = info['url']
|
||||
expected_sha256 = info['sha256']
|
||||
url_filename = os.path.basename(url)
|
||||
|
||||
# getdeps uses format: {package}-{filename}
|
||||
filename = f"{package}-{url_filename}"
|
||||
filepath = os.path.join(download_dir, filename)
|
||||
cache_path = os.path.join(cache_dir, filename)
|
||||
|
||||
# Check if already valid
|
||||
if os.path.exists(filepath) and sha256_file(filepath) == expected_sha256:
|
||||
print(f" {filename}: OK (already downloaded)")
|
||||
continue
|
||||
|
||||
# Check cache
|
||||
if os.path.exists(cache_path) and sha256_file(cache_path) == expected_sha256:
|
||||
print(f" {filename}: OK (from cache)")
|
||||
subprocess.run(['cp', cache_path, filepath], check=True)
|
||||
continue
|
||||
|
||||
# Try fallback mirrors
|
||||
mirrors = get_fallback_mirrors(url)
|
||||
downloaded = False
|
||||
for mirror_url in mirrors:
|
||||
print(f" {filename}: trying {mirror_url}...")
|
||||
try:
|
||||
subprocess.run(['wget', '-q', '-O', filepath, mirror_url], check=True, timeout=120)
|
||||
if sha256_file(filepath) == expected_sha256:
|
||||
print(f" {filename}: OK (downloaded)")
|
||||
subprocess.run(['cp', filepath, cache_path], check=False)
|
||||
downloaded = True
|
||||
break
|
||||
else:
|
||||
os.remove(filepath)
|
||||
except Exception:
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
|
||||
if not downloaded:
|
||||
print(f" {filename}: WARNING - all mirrors failed")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -101,6 +101,5 @@ get_lib_base benchmark LATEST platform010
|
||||
get_lib_base kernel-headers fb platform010
|
||||
get_lib_base binutils LATEST centos8-native
|
||||
get_lib_base valgrind LATEST platform010
|
||||
get_lib_base lua 5.3.4 platform010
|
||||
|
||||
git diff $OUTPUT
|
||||
Vendored
-5
@@ -54,11 +54,6 @@ static std::unordered_map<std::string, OptionTypeInfo>
|
||||
{offsetof(struct CompressedSecondaryCacheOptions, compression_type),
|
||||
OptionType::kCompressionType, OptionVerificationType::kNormal,
|
||||
OptionTypeFlags::kMutable}},
|
||||
{"compress_format_version",
|
||||
{offsetof(struct CompressedSecondaryCacheOptions,
|
||||
compress_format_version),
|
||||
OptionType::kUInt32T, OptionVerificationType::kNormal,
|
||||
OptionTypeFlags::kMutable}},
|
||||
{"enable_custom_split_merge",
|
||||
{offsetof(struct CompressedSecondaryCacheOptions,
|
||||
enable_custom_split_merge),
|
||||
|
||||
Vendored
+9
-9
@@ -101,23 +101,23 @@ class CacheEntryStatsCollector {
|
||||
}
|
||||
|
||||
// Gets saved stats, regardless of age
|
||||
void GetStats(Stats *stats) {
|
||||
void GetStats(Stats* stats) {
|
||||
std::lock_guard<std::mutex> lock(saved_mutex_);
|
||||
*stats = saved_stats_;
|
||||
}
|
||||
|
||||
Cache *GetCache() const { return cache_; }
|
||||
Cache* GetCache() const { return cache_; }
|
||||
|
||||
// Gets or creates a shared instance of CacheEntryStatsCollector in the
|
||||
// cache itself, and saves into `ptr`. This shared_ptr will hold the
|
||||
// entry in cache until all refs are destroyed.
|
||||
static Status GetShared(Cache *raw_cache, SystemClock *clock,
|
||||
std::shared_ptr<CacheEntryStatsCollector> *ptr) {
|
||||
static Status GetShared(Cache* raw_cache, SystemClock* clock,
|
||||
std::shared_ptr<CacheEntryStatsCollector>* ptr) {
|
||||
assert(raw_cache);
|
||||
BasicTypedCacheInterface<CacheEntryStatsCollector, CacheEntryRole::kMisc>
|
||||
cache{raw_cache};
|
||||
|
||||
const Slice &cache_key = GetCacheKey();
|
||||
const Slice& cache_key = GetCacheKey();
|
||||
auto h = cache.Lookup(cache_key);
|
||||
if (h == nullptr) {
|
||||
// Not yet in cache, but Cache doesn't provide a built-in way to
|
||||
@@ -152,7 +152,7 @@ class CacheEntryStatsCollector {
|
||||
}
|
||||
|
||||
private:
|
||||
explicit CacheEntryStatsCollector(Cache *cache, SystemClock *clock)
|
||||
explicit CacheEntryStatsCollector(Cache* cache, SystemClock* clock)
|
||||
: saved_stats_(),
|
||||
working_stats_(),
|
||||
last_start_time_micros_(0),
|
||||
@@ -160,7 +160,7 @@ class CacheEntryStatsCollector {
|
||||
cache_(cache),
|
||||
clock_(clock) {}
|
||||
|
||||
static const Slice &GetCacheKey() {
|
||||
static const Slice& GetCacheKey() {
|
||||
// For each template instantiation
|
||||
static CacheKey ckey = CacheKey::CreateUniqueForProcessLifetime();
|
||||
static Slice ckey_slice = ckey.AsSlice();
|
||||
@@ -175,8 +175,8 @@ class CacheEntryStatsCollector {
|
||||
uint64_t last_start_time_micros_;
|
||||
uint64_t last_end_time_micros_;
|
||||
|
||||
Cache *const cache_;
|
||||
SystemClock *const clock_;
|
||||
Cache* const cache_;
|
||||
SystemClock* const clock_;
|
||||
};
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
Vendored
+3
-3
@@ -24,7 +24,7 @@ namespace ROCKSDB_NAMESPACE {
|
||||
// 0 | >= 1<<63 | CreateUniqueForProcessLifetime
|
||||
// > 0 | any | OffsetableCacheKey.WithOffset
|
||||
|
||||
CacheKey CacheKey::CreateUniqueForCacheLifetime(Cache *cache) {
|
||||
CacheKey CacheKey::CreateUniqueForCacheLifetime(Cache* cache) {
|
||||
// +1 so that we can reserve all zeros for "unset" cache key
|
||||
uint64_t id = cache->NewId() + 1;
|
||||
// Ensure we don't collide with CreateUniqueForProcessLifetime
|
||||
@@ -297,8 +297,8 @@ CacheKey CacheKey::CreateUniqueForProcessLifetime() {
|
||||
//
|
||||
// TODO: Nevertheless / regardless, an efficient way to detect (and thus
|
||||
// quantify) block cache corruptions, including collisions, should be added.
|
||||
OffsetableCacheKey::OffsetableCacheKey(const std::string &db_id,
|
||||
const std::string &db_session_id,
|
||||
OffsetableCacheKey::OffsetableCacheKey(const std::string& db_id,
|
||||
const std::string& db_session_id,
|
||||
uint64_t file_number) {
|
||||
UniqueId64x2 internal_id;
|
||||
Status s = GetSstInternalUniqueId(db_id, db_session_id, file_number,
|
||||
|
||||
Vendored
+5
-5
@@ -44,13 +44,13 @@ class CacheKey {
|
||||
inline Slice AsSlice() const {
|
||||
static_assert(sizeof(*this) == 16, "Standardized on 16-byte cache key");
|
||||
assert(!IsEmpty());
|
||||
return Slice(reinterpret_cast<const char *>(this), sizeof(*this));
|
||||
return Slice(reinterpret_cast<const char*>(this), sizeof(*this));
|
||||
}
|
||||
|
||||
// Create a CacheKey that is unique among others associated with this Cache
|
||||
// instance. Depends on Cache::NewId. This is useful for block cache
|
||||
// "reservations".
|
||||
static CacheKey CreateUniqueForCacheLifetime(Cache *cache);
|
||||
static CacheKey CreateUniqueForCacheLifetime(Cache* cache);
|
||||
|
||||
// Create a CacheKey that is unique among others for the lifetime of this
|
||||
// process. This is useful for saving in a static data member so that
|
||||
@@ -87,7 +87,7 @@ class OffsetableCacheKey : private CacheKey {
|
||||
|
||||
// Constructs an OffsetableCacheKey with the given information about a file.
|
||||
// This constructor never generates an "empty" base key.
|
||||
OffsetableCacheKey(const std::string &db_id, const std::string &db_session_id,
|
||||
OffsetableCacheKey(const std::string& db_id, const std::string& db_session_id,
|
||||
uint64_t file_number);
|
||||
|
||||
// Creates an OffsetableCacheKey from an SST unique ID, so that cache keys
|
||||
@@ -134,9 +134,9 @@ class OffsetableCacheKey : private CacheKey {
|
||||
static_assert(sizeof(file_num_etc64_) == kCommonPrefixSize,
|
||||
"8 byte common prefix expected");
|
||||
assert(!IsEmpty());
|
||||
assert(&this->file_num_etc64_ == static_cast<const void *>(this));
|
||||
assert(&this->file_num_etc64_ == static_cast<const void*>(this));
|
||||
|
||||
return Slice(reinterpret_cast<const char *>(this), kCommonPrefixSize);
|
||||
return Slice(reinterpret_cast<const char*>(this), kCommonPrefixSize);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Vendored
+16
-16
@@ -44,8 +44,8 @@ class CacheReservationManager {
|
||||
bool increase) = 0;
|
||||
virtual Status MakeCacheReservation(
|
||||
std::size_t incremental_memory_used,
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle>
|
||||
*handle) = 0;
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle>*
|
||||
handle) = 0;
|
||||
virtual std::size_t GetTotalReservedCacheSize() = 0;
|
||||
virtual std::size_t GetTotalMemoryUsed() = 0;
|
||||
};
|
||||
@@ -90,11 +90,11 @@ class CacheReservationManagerImpl
|
||||
bool delayed_decrease = false);
|
||||
|
||||
// no copy constructor, copy assignment, move constructor, move assignment
|
||||
CacheReservationManagerImpl(const CacheReservationManagerImpl &) = delete;
|
||||
CacheReservationManagerImpl &operator=(const CacheReservationManagerImpl &) =
|
||||
CacheReservationManagerImpl(const CacheReservationManagerImpl&) = delete;
|
||||
CacheReservationManagerImpl& operator=(const CacheReservationManagerImpl&) =
|
||||
delete;
|
||||
CacheReservationManagerImpl(CacheReservationManagerImpl &&) = delete;
|
||||
CacheReservationManagerImpl &operator=(CacheReservationManagerImpl &&) =
|
||||
CacheReservationManagerImpl(CacheReservationManagerImpl&&) = delete;
|
||||
CacheReservationManagerImpl& operator=(CacheReservationManagerImpl&&) =
|
||||
delete;
|
||||
|
||||
~CacheReservationManagerImpl() override;
|
||||
@@ -178,7 +178,7 @@ class CacheReservationManagerImpl
|
||||
// REQUIRES: handle != nullptr
|
||||
Status MakeCacheReservation(
|
||||
std::size_t incremental_memory_used,
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle> *handle)
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle>* handle)
|
||||
override;
|
||||
|
||||
// Return the size of the cache (which is a multiple of kSizeDummyEntry)
|
||||
@@ -200,7 +200,7 @@ class CacheReservationManagerImpl
|
||||
// For testing only - it is to help ensure the CacheItemHelperForRole<R>
|
||||
// accessed from CacheReservationManagerImpl and the one accessed from the
|
||||
// test are from the same translation units
|
||||
static const Cache::CacheItemHelper *TEST_GetCacheItemHelperForRole();
|
||||
static const Cache::CacheItemHelper* TEST_GetCacheItemHelperForRole();
|
||||
|
||||
private:
|
||||
static constexpr std::size_t kSizeDummyEntry = 256 * 1024;
|
||||
@@ -216,7 +216,7 @@ class CacheReservationManagerImpl
|
||||
bool delayed_decrease_;
|
||||
std::atomic<std::size_t> cache_allocated_size_;
|
||||
std::size_t memory_used_;
|
||||
std::vector<Cache::Handle *> dummy_handles_;
|
||||
std::vector<Cache::Handle*> dummy_handles_;
|
||||
CacheKey cache_key_;
|
||||
};
|
||||
|
||||
@@ -251,14 +251,14 @@ class ConcurrentCacheReservationManager
|
||||
std::shared_ptr<CacheReservationManager> cache_res_mgr) {
|
||||
cache_res_mgr_ = std::move(cache_res_mgr);
|
||||
}
|
||||
ConcurrentCacheReservationManager(const ConcurrentCacheReservationManager &) =
|
||||
ConcurrentCacheReservationManager(const ConcurrentCacheReservationManager&) =
|
||||
delete;
|
||||
ConcurrentCacheReservationManager &operator=(
|
||||
const ConcurrentCacheReservationManager &) = delete;
|
||||
ConcurrentCacheReservationManager(ConcurrentCacheReservationManager &&) =
|
||||
ConcurrentCacheReservationManager& operator=(
|
||||
const ConcurrentCacheReservationManager&) = delete;
|
||||
ConcurrentCacheReservationManager(ConcurrentCacheReservationManager&&) =
|
||||
delete;
|
||||
ConcurrentCacheReservationManager &operator=(
|
||||
ConcurrentCacheReservationManager &&) = delete;
|
||||
ConcurrentCacheReservationManager& operator=(
|
||||
ConcurrentCacheReservationManager&&) = delete;
|
||||
|
||||
~ConcurrentCacheReservationManager() override {}
|
||||
|
||||
@@ -286,7 +286,7 @@ class ConcurrentCacheReservationManager
|
||||
|
||||
inline Status MakeCacheReservation(
|
||||
std::size_t incremental_memory_used,
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle> *handle)
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle>* handle)
|
||||
override {
|
||||
std::unique_ptr<CacheReservationManager::CacheReservationHandle>
|
||||
wrapped_handle;
|
||||
|
||||
Vendored
+1
-1
@@ -644,7 +644,7 @@ using TypedHandle = SharedCache::TypedHandle;
|
||||
|
||||
TEST_P(CacheTest, SetCapacity) {
|
||||
if (IsHyperClock()) {
|
||||
// TODO: update test & code for limited supoort
|
||||
// TODO: update test & code for limited support
|
||||
ROCKSDB_GTEST_BYPASS(
|
||||
"HyperClockCache doesn't support arbitrary capacity "
|
||||
"adjustments.");
|
||||
|
||||
Vendored
+258
-294
@@ -38,13 +38,11 @@ namespace ROCKSDB_NAMESPACE {
|
||||
namespace clock_cache {
|
||||
|
||||
namespace {
|
||||
inline uint64_t GetRefcount(uint64_t meta) {
|
||||
return ((meta >> ClockHandle::kAcquireCounterShift) -
|
||||
(meta >> ClockHandle::kReleaseCounterShift)) &
|
||||
ClockHandle::kCounterMask;
|
||||
}
|
||||
using SlotMeta = ClockHandle::SlotMeta;
|
||||
using AcquireCounter = SlotMeta::AcquireCounter;
|
||||
using ReleaseCounter = SlotMeta::ReleaseCounter;
|
||||
|
||||
inline uint64_t GetInitialCountdown(Cache::Priority priority) {
|
||||
inline uint32_t GetInitialCountdown(Cache::Priority priority) {
|
||||
// Set initial clock data from priority
|
||||
// TODO: configuration parameters for priority handling and clock cycle
|
||||
// count?
|
||||
@@ -65,11 +63,11 @@ inline uint64_t GetInitialCountdown(Cache::Priority priority) {
|
||||
inline void MarkEmpty(ClockHandle& h) {
|
||||
#ifndef NDEBUG
|
||||
// Mark slot as empty, with assertion
|
||||
uint64_t meta = h.meta.Exchange(0);
|
||||
assert(meta >> ClockHandle::kStateShift == ClockHandle::kStateConstruction);
|
||||
auto old_meta = h.meta.Exchange({});
|
||||
assert(old_meta.IsUnderConstruction());
|
||||
#else
|
||||
// Mark slot as empty
|
||||
h.meta.Store(0);
|
||||
h.meta.Store({});
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -85,18 +83,20 @@ inline void FreeDataMarkEmpty(ClockHandle& h, MemoryAllocator* allocator) {
|
||||
|
||||
// Called to undo the effect of referencing an entry for internal purposes,
|
||||
// so it should not be marked as having been used.
|
||||
inline void Unref(const ClockHandle& h, uint64_t count = 1) {
|
||||
inline void Unref(const ClockHandle& h, uint32_t count = 1) {
|
||||
// Pretend we never took the reference
|
||||
// WART: there's a tiny chance we release last ref to invisible
|
||||
// entry here. If that happens, we let eviction take care of it.
|
||||
uint64_t old_meta = h.meta.FetchSub(ClockHandle::kAcquireIncrement * count);
|
||||
assert(GetRefcount(old_meta) != 0);
|
||||
SlotMeta old_meta;
|
||||
h.meta.Apply(AcquireCounter::MinusTransformPromiseNoUnderflow(count),
|
||||
&old_meta);
|
||||
assert(old_meta.GetRefcount() != 0);
|
||||
(void)old_meta;
|
||||
}
|
||||
|
||||
inline bool ClockUpdate(ClockHandle& h, BaseClockTable::EvictionData* data,
|
||||
bool* purgeable = nullptr) {
|
||||
uint64_t meta;
|
||||
SlotMeta meta;
|
||||
if (purgeable) {
|
||||
assert(*purgeable == false);
|
||||
// In AutoHCC, our eviction process follows the chain structure, so we
|
||||
@@ -110,46 +110,40 @@ inline bool ClockUpdate(ClockHandle& h, BaseClockTable::EvictionData* data,
|
||||
meta = h.meta.LoadRelaxed();
|
||||
}
|
||||
|
||||
if (((meta >> ClockHandle::kStateShift) & ClockHandle::kStateShareableBit) ==
|
||||
0) {
|
||||
if (!meta.IsShareable()) {
|
||||
// Only clock update Shareable entries
|
||||
if (purgeable) {
|
||||
*purgeable = true;
|
||||
// AutoHCC only: make sure we only attempt to update non-empty slots
|
||||
assert((meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateOccupiedBit);
|
||||
assert(!meta.IsEmpty());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
uint64_t acquire_count =
|
||||
(meta >> ClockHandle::kAcquireCounterShift) & ClockHandle::kCounterMask;
|
||||
uint64_t release_count =
|
||||
(meta >> ClockHandle::kReleaseCounterShift) & ClockHandle::kCounterMask;
|
||||
uint32_t acquire_count = meta.GetAcquireCounter();
|
||||
uint32_t release_count = meta.GetReleaseCounter();
|
||||
if (acquire_count != release_count) {
|
||||
// Only clock update entries with no outstanding refs
|
||||
data->seen_pinned_count++;
|
||||
return false;
|
||||
}
|
||||
if ((meta >> ClockHandle::kStateShift == ClockHandle::kStateVisible) &&
|
||||
acquire_count > 0) {
|
||||
if (meta.IsVisible() && acquire_count > 0) {
|
||||
// Decrement clock
|
||||
uint64_t new_count =
|
||||
std::min(acquire_count - 1, uint64_t{ClockHandle::kMaxCountdown} - 1);
|
||||
uint32_t new_count =
|
||||
std::min(acquire_count - 1, uint32_t{ClockHandle::kMaxCountdown} - 1);
|
||||
// Compare-exchange in the decremented clock info, but
|
||||
// not aggressively
|
||||
uint64_t new_meta =
|
||||
(uint64_t{ClockHandle::kStateVisible} << ClockHandle::kStateShift) |
|
||||
(meta & ClockHandle::kHitBitMask) |
|
||||
(new_count << ClockHandle::kReleaseCounterShift) |
|
||||
(new_count << ClockHandle::kAcquireCounterShift);
|
||||
SlotMeta new_meta = meta;
|
||||
new_meta.SetReleaseCounter(new_count);
|
||||
new_meta.SetAcquireCounter(new_count);
|
||||
h.meta.CasStrongRelaxed(meta, new_meta);
|
||||
return false;
|
||||
}
|
||||
// Otherwise, remove entry (either unreferenced invisible or
|
||||
// unreferenced and expired visible).
|
||||
if (h.meta.CasStrong(meta, (uint64_t{ClockHandle::kStateConstruction}
|
||||
<< ClockHandle::kStateShift) |
|
||||
(meta & ClockHandle::kHitBitMask))) {
|
||||
SlotMeta construction_meta;
|
||||
construction_meta.SetUnderConstruction();
|
||||
construction_meta.SetHit(meta.GetHit());
|
||||
if (h.meta.CasStrong(meta, construction_meta)) {
|
||||
// Took ownership.
|
||||
data->freed_charge += h.GetTotalCharge();
|
||||
data->freed_count += 1;
|
||||
@@ -215,39 +209,39 @@ inline bool ClockUpdate(ClockHandle& h, BaseClockTable::EvictionData* data,
|
||||
// counter to reach "high" state again and bumped back to "medium." (This
|
||||
// motivates only checking for release counter in high state, not both in high
|
||||
// state.)
|
||||
inline void CorrectNearOverflow(uint64_t old_meta,
|
||||
AcqRelAtomic<uint64_t>& meta) {
|
||||
inline void CorrectNearOverflow(SlotMeta old_meta,
|
||||
BitFieldsAtomic<SlotMeta>& meta) {
|
||||
// We clear both top-most counter bits at the same time.
|
||||
constexpr uint64_t kCounterTopBit = uint64_t{1}
|
||||
<< (ClockHandle::kCounterNumBits - 1);
|
||||
constexpr uint64_t kClearBits =
|
||||
(kCounterTopBit << ClockHandle::kAcquireCounterShift) |
|
||||
(kCounterTopBit << ClockHandle::kReleaseCounterShift);
|
||||
// A simple check that allows us to initiate clearing the top bits for
|
||||
// a large portion of the "high" state space on release counter.
|
||||
constexpr uint64_t kCheckBits =
|
||||
(kCounterTopBit | (ClockHandle::kMaxCountdown + 1))
|
||||
<< ClockHandle::kReleaseCounterShift;
|
||||
constexpr uint32_t kCounterTopBit = uint32_t{1}
|
||||
<< (SlotMeta::kCounterNumBits - 1);
|
||||
// The threshold for correcting "near overflow" is to ensure
|
||||
// (a) the value has a top bit set that can be cleared
|
||||
// (b) when we clear the top bit, the eviction state will be preserved
|
||||
// (everything >= kMaxCountdown is treated equivalently)
|
||||
// As mentioned above, we only check the release count.
|
||||
constexpr uint32_t kThreshold = kCounterTopBit + ClockHandle::kMaxCountdown;
|
||||
|
||||
if (UNLIKELY(old_meta & kCheckBits)) {
|
||||
meta.FetchAndRelaxed(~kClearBits);
|
||||
if (UNLIKELY(old_meta.GetReleaseCounter() > kThreshold)) {
|
||||
auto clear_transform = AcquireCounter::AndTransform(kCounterTopBit - 1) +
|
||||
ReleaseCounter::AndTransform(kCounterTopBit - 1);
|
||||
meta.ApplyRelaxed(clear_transform);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool BeginSlotInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
uint64_t initial_countdown, bool* already_matches) {
|
||||
uint32_t initial_countdown, bool* already_matches) {
|
||||
assert(*already_matches == false);
|
||||
// Optimistically transition the slot from "empty" to
|
||||
// "under construction" (no effect on other states)
|
||||
uint64_t old_meta = h.meta.FetchOr(uint64_t{ClockHandle::kStateOccupiedBit}
|
||||
<< ClockHandle::kStateShift);
|
||||
uint64_t old_state = old_meta >> ClockHandle::kStateShift;
|
||||
auto set_occupied = SlotMeta::OccupiedFlag::SetTransform();
|
||||
SlotMeta old_meta;
|
||||
h.meta.Apply(set_occupied, &old_meta);
|
||||
|
||||
if (old_state == ClockHandle::kStateEmpty) {
|
||||
if (old_meta.IsEmpty()) {
|
||||
// We've started inserting into an available slot, and taken
|
||||
// ownership.
|
||||
return true;
|
||||
} else if (old_state != ClockHandle::kStateVisible) {
|
||||
} else if (!old_meta.IsVisible()) {
|
||||
// Slot not usable / touchable now
|
||||
return false;
|
||||
}
|
||||
@@ -255,15 +249,17 @@ inline bool BeginSlotInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
// But first, we need to acquire a ref to read it. In fact, number of
|
||||
// refs for initial countdown, so that we boost the clock state if
|
||||
// this is a match.
|
||||
old_meta =
|
||||
h.meta.FetchAdd(ClockHandle::kAcquireIncrement * initial_countdown);
|
||||
auto add_acquire =
|
||||
AcquireCounter::PlusTransformPromiseNoOverflow(initial_countdown);
|
||||
h.meta.Apply(add_acquire, &old_meta);
|
||||
// Like Lookup
|
||||
if ((old_meta >> ClockHandle::kStateShift) == ClockHandle::kStateVisible) {
|
||||
if (old_meta.IsVisible()) {
|
||||
// Acquired a read reference
|
||||
if (h.hashed_key == proto.hashed_key) {
|
||||
// Match. Release in a way that boosts the clock state
|
||||
old_meta =
|
||||
h.meta.FetchAdd(ClockHandle::kReleaseIncrement * initial_countdown);
|
||||
auto add_release =
|
||||
ReleaseCounter::PlusTransformPromiseNoOverflow(initial_countdown);
|
||||
h.meta.Apply(add_release, &old_meta);
|
||||
// Correct for possible (but rare) overflow
|
||||
CorrectNearOverflow(old_meta, h.meta);
|
||||
// Insert detached instead (only if return handle needed)
|
||||
@@ -273,8 +269,7 @@ inline bool BeginSlotInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
// Mismatch.
|
||||
Unref(h, initial_countdown);
|
||||
}
|
||||
} else if (UNLIKELY((old_meta >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateInvisible)) {
|
||||
} else if (UNLIKELY(old_meta.IsInvisible())) {
|
||||
// Pretend we never took the reference
|
||||
Unref(h, initial_countdown);
|
||||
} else {
|
||||
@@ -286,25 +281,23 @@ inline bool BeginSlotInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
}
|
||||
|
||||
inline void FinishSlotInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
uint64_t initial_countdown, bool keep_ref) {
|
||||
uint32_t initial_countdown, bool keep_ref) {
|
||||
// Save data fields
|
||||
ClockHandleBasicData* h_alias = &h;
|
||||
*h_alias = proto;
|
||||
|
||||
// Transition from "under construction" state to "visible" state
|
||||
uint64_t new_meta = uint64_t{ClockHandle::kStateVisible}
|
||||
<< ClockHandle::kStateShift;
|
||||
SlotMeta new_meta;
|
||||
new_meta.SetVisible();
|
||||
|
||||
// Maybe with an outstanding reference
|
||||
new_meta |= initial_countdown << ClockHandle::kAcquireCounterShift;
|
||||
new_meta |= (initial_countdown - keep_ref)
|
||||
<< ClockHandle::kReleaseCounterShift;
|
||||
new_meta.SetAcquireCounter(initial_countdown);
|
||||
new_meta.SetReleaseCounter(initial_countdown - (keep_ref ? 1 : 0));
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Save the state transition, with assertion
|
||||
uint64_t old_meta = h.meta.Exchange(new_meta);
|
||||
assert(old_meta >> ClockHandle::kStateShift ==
|
||||
ClockHandle::kStateConstruction);
|
||||
auto old_meta = h.meta.Exchange(new_meta);
|
||||
assert(old_meta.IsUnderConstruction());
|
||||
#else
|
||||
// Save the state transition
|
||||
h.meta.Store(new_meta);
|
||||
@@ -312,7 +305,7 @@ inline void FinishSlotInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
}
|
||||
|
||||
bool TryInsert(const ClockHandleBasicData& proto, ClockHandle& h,
|
||||
uint64_t initial_countdown, bool keep_ref,
|
||||
uint32_t initial_countdown, bool keep_ref,
|
||||
bool* already_matches) {
|
||||
bool b = BeginSlotInsert(proto, h, initial_countdown, already_matches);
|
||||
if (b) {
|
||||
@@ -326,35 +319,32 @@ template <class HandleImpl, class Func>
|
||||
void ConstApplyToEntriesRange(const Func& func, const HandleImpl* begin,
|
||||
const HandleImpl* end,
|
||||
bool apply_if_will_be_deleted) {
|
||||
uint64_t check_state_mask = ClockHandle::kStateShareableBit;
|
||||
if (!apply_if_will_be_deleted) {
|
||||
check_state_mask |= ClockHandle::kStateVisibleBit;
|
||||
}
|
||||
|
||||
for (const HandleImpl* h = begin; h < end; ++h) {
|
||||
// Note: to avoid using compare_exchange, we have to be extra careful.
|
||||
uint64_t old_meta = h->meta.LoadRelaxed();
|
||||
SlotMeta old_meta = h->meta.LoadRelaxed();
|
||||
// Check if it's an entry visible to lookups
|
||||
if ((old_meta >> ClockHandle::kStateShift) & check_state_mask) {
|
||||
// Increment acquire counter. Note: it's possible that the entry has
|
||||
// completely changed since we loaded old_meta, but incrementing acquire
|
||||
// count is always safe. (Similar to optimistic Lookup here.)
|
||||
old_meta = h->meta.FetchAdd(ClockHandle::kAcquireIncrement);
|
||||
// Check whether we actually acquired a reference.
|
||||
if ((old_meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit) {
|
||||
// Apply func if appropriate
|
||||
if ((old_meta >> ClockHandle::kStateShift) & check_state_mask) {
|
||||
func(*h);
|
||||
if (apply_if_will_be_deleted || old_meta.IsVisible()) {
|
||||
if (old_meta.IsShareable()) {
|
||||
// Increment acquire counter. Note: it's possible that the entry has
|
||||
// completely changed since we loaded old_meta, but incrementing acquire
|
||||
// count is always safe. (Similar to optimistic Lookup here.)
|
||||
auto add_acquire = AcquireCounter::PlusTransformPromiseNoOverflow(1);
|
||||
h->meta.Apply(add_acquire, &old_meta);
|
||||
// Check whether we actually acquired a reference.
|
||||
if (old_meta.IsShareable()) {
|
||||
// Apply func if appropriate
|
||||
if (apply_if_will_be_deleted || old_meta.IsVisible()) {
|
||||
func(*h);
|
||||
}
|
||||
// Pretend we never took the reference
|
||||
Unref(*h);
|
||||
// No net change, so don't need to check for overflow
|
||||
} else {
|
||||
// For other states, incrementing the acquire counter has no effect
|
||||
// so we don't need to undo it. Furthermore, we cannot safely undo
|
||||
// it because we did not acquire a read reference to lock the
|
||||
// entry in a Shareable state.
|
||||
}
|
||||
// Pretend we never took the reference
|
||||
Unref(*h);
|
||||
// No net change, so don't need to check for overflow
|
||||
} else {
|
||||
// For other states, incrementing the acquire counter has no effect
|
||||
// so we don't need to undo it. Furthermore, we cannot safely undo
|
||||
// it because we did not acquire a read reference to lock the
|
||||
// entry in a Shareable state.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,9 +389,9 @@ HandleImpl* BaseClockTable::StandaloneInsert(
|
||||
h->SetStandalone();
|
||||
// Single reference (standalone entries only created if returning a refed
|
||||
// Handle back to user)
|
||||
uint64_t meta = uint64_t{ClockHandle::kStateInvisible}
|
||||
<< ClockHandle::kStateShift;
|
||||
meta |= uint64_t{1} << ClockHandle::kAcquireCounterShift;
|
||||
SlotMeta meta;
|
||||
meta.SetInvisible();
|
||||
meta.SetAcquireCounter(1);
|
||||
h->meta.Store(meta);
|
||||
// Keep track of how much of usage is standalone
|
||||
standalone_usage_.FetchAddRelaxed(proto.GetTotalCharge());
|
||||
@@ -564,11 +554,10 @@ void BaseClockTable::TrackAndReleaseEvictedEntry(ClockHandle* h) {
|
||||
if (eviction_callback_) {
|
||||
// For key reconstructed from hash
|
||||
UniqueId64x2 unhashed;
|
||||
took_value_ownership =
|
||||
eviction_callback_(ClockCacheShard<FixedHyperClockTable>::ReverseHash(
|
||||
h->GetHash(), &unhashed, hash_seed_),
|
||||
static_cast<Cache::Handle*>(h),
|
||||
h->meta.LoadRelaxed() & ClockHandle::kHitBitMask);
|
||||
took_value_ownership = eviction_callback_(
|
||||
ClockCacheShard<FixedHyperClockTable>::ReverseHash(
|
||||
h->GetHash(), &unhashed, hash_seed_),
|
||||
static_cast<Cache::Handle*>(h), h->meta.LoadRelaxed().GetHit());
|
||||
}
|
||||
if (!took_value_ownership) {
|
||||
h->FreeData(allocator_);
|
||||
@@ -648,7 +637,7 @@ Status BaseClockTable::Insert(const ClockHandleBasicData& proto,
|
||||
// * Have to insert into a suboptimal location (more probes) so that the
|
||||
// old entry can be kept around as well.
|
||||
|
||||
uint64_t initial_countdown = GetInitialCountdown(priority);
|
||||
uint32_t initial_countdown = GetInitialCountdown(priority);
|
||||
assert(initial_countdown > 0);
|
||||
|
||||
HandleImpl* e =
|
||||
@@ -693,34 +682,34 @@ Status BaseClockTable::Insert(const ClockHandleBasicData& proto,
|
||||
|
||||
void BaseClockTable::Ref(ClockHandle& h) {
|
||||
// Increment acquire counter
|
||||
uint64_t old_meta = h.meta.FetchAdd(ClockHandle::kAcquireIncrement);
|
||||
SlotMeta old_meta;
|
||||
h.meta.Apply(AcquireCounter::PlusTransformPromiseNoOverflow(1), &old_meta);
|
||||
|
||||
assert((old_meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit);
|
||||
assert(old_meta.IsShareable());
|
||||
// Must have already had a reference
|
||||
assert(GetRefcount(old_meta) > 0);
|
||||
assert(old_meta.GetRefcount() > 0);
|
||||
(void)old_meta;
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
void BaseClockTable::TEST_RefN(ClockHandle& h, size_t n) {
|
||||
void BaseClockTable::TEST_RefN(ClockHandle& h, uint32_t n) {
|
||||
// Increment acquire counter
|
||||
uint64_t old_meta = h.meta.FetchAdd(n * ClockHandle::kAcquireIncrement);
|
||||
SlotMeta old_meta;
|
||||
h.meta.Apply(AcquireCounter::PlusTransformPromiseNoOverflow(n), &old_meta);
|
||||
|
||||
assert((old_meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit);
|
||||
assert(old_meta.IsShareable());
|
||||
(void)old_meta;
|
||||
}
|
||||
|
||||
void BaseClockTable::TEST_ReleaseNMinus1(ClockHandle* h, size_t n) {
|
||||
void BaseClockTable::TEST_ReleaseNMinus1(ClockHandle* h, uint32_t n) {
|
||||
assert(n > 0);
|
||||
|
||||
// Like n-1 Releases, but assumes one more will happen in the caller to take
|
||||
// care of anything like erasing an unreferenced, invisible entry.
|
||||
uint64_t old_meta =
|
||||
h->meta.FetchAdd((n - 1) * ClockHandle::kReleaseIncrement);
|
||||
assert((old_meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit);
|
||||
SlotMeta old_meta;
|
||||
h->meta.Apply(ReleaseCounter::PlusTransformPromiseNoOverflow(n - 1),
|
||||
&old_meta);
|
||||
assert(old_meta.IsShareable());
|
||||
(void)old_meta;
|
||||
}
|
||||
#endif
|
||||
@@ -754,23 +743,20 @@ FixedHyperClockTable::~FixedHyperClockTable() {
|
||||
// in the table.
|
||||
for (size_t i = 0; i < GetTableSize(); i++) {
|
||||
HandleImpl& h = array_[i];
|
||||
switch (h.meta.LoadRelaxed() >> ClockHandle::kStateShift) {
|
||||
case ClockHandle::kStateEmpty:
|
||||
// noop
|
||||
break;
|
||||
case ClockHandle::kStateInvisible: // rare but possible
|
||||
case ClockHandle::kStateVisible:
|
||||
assert(GetRefcount(h.meta.LoadRelaxed()) == 0);
|
||||
h.FreeData(allocator_);
|
||||
SlotMeta meta = h.meta.LoadRelaxed();
|
||||
if (meta.IsShareable()) {
|
||||
// NOTE: Reaching here invisible is rare but possible
|
||||
assert(meta.GetRefcount() == 0);
|
||||
h.FreeData(allocator_);
|
||||
#ifndef NDEBUG
|
||||
Rollback(h.hashed_key, &h);
|
||||
ReclaimEntryUsage(h.GetTotalCharge());
|
||||
Rollback(h.hashed_key, &h);
|
||||
ReclaimEntryUsage(h.GetTotalCharge());
|
||||
#endif
|
||||
break;
|
||||
// otherwise
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
} else {
|
||||
// Should be no transient "under construction" states unless a thread
|
||||
// was killed or we are being destructed while another thread is still
|
||||
// operating on the structure
|
||||
assert(meta.IsEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -792,7 +778,7 @@ bool FixedHyperClockTable::GrowIfNeeded(size_t new_occupancy, InsertState&) {
|
||||
}
|
||||
|
||||
FixedHyperClockTable::HandleImpl* FixedHyperClockTable::DoInsert(
|
||||
const ClockHandleBasicData& proto, uint64_t initial_countdown,
|
||||
const ClockHandleBasicData& proto, uint32_t initial_countdown,
|
||||
bool keep_ref, InsertState&) {
|
||||
bool already_matches = false;
|
||||
HandleImpl* e = FindSlot(
|
||||
@@ -843,47 +829,46 @@ FixedHyperClockTable::HandleImpl* FixedHyperClockTable::Lookup(
|
||||
HandleImpl* e = FindSlot(
|
||||
hashed_key,
|
||||
[&](HandleImpl* h) {
|
||||
SlotMeta old_meta;
|
||||
// Mostly branch-free version (similar performance)
|
||||
/*
|
||||
uint64_t old_meta = h->meta.FetchAdd(ClockHandle::kAcquireIncrement,
|
||||
std::memory_order_acquire);
|
||||
bool Shareable = (old_meta >> (ClockHandle::kStateShift + 1)) & 1U;
|
||||
bool visible = (old_meta >> ClockHandle::kStateShift) & 1U;
|
||||
bool match = (h->key == key) & visible;
|
||||
h->meta.FetchSub(static_cast<uint64_t>(Shareable & !match) <<
|
||||
ClockHandle::kAcquireCounterShift); return
|
||||
match;
|
||||
h->meta.Apply(AcquireCounter::PlusTransformPromiseNoOverflow(1),
|
||||
&old_meta);
|
||||
bool shareable = old_meta.IsShareable();
|
||||
bool visible = old_meta.IsVisible();
|
||||
bool match = (h->hashed_key == hashed_key) & visible;
|
||||
h->meta.Apply(AcquireCounter::MinusTransformPromiseNoUnderflow(
|
||||
uint32_t{shareable} & uint32_t{!match}));
|
||||
h->meta.Apply(SlotMeta::HitFlag::Or(match));
|
||||
return match;
|
||||
*/
|
||||
// Optimistic lookup should pay off when the table is relatively
|
||||
// sparse.
|
||||
constexpr bool kOptimisticLookup = true;
|
||||
uint64_t old_meta;
|
||||
if (!kOptimisticLookup) {
|
||||
old_meta = h->meta.Load();
|
||||
if ((old_meta >> ClockHandle::kStateShift) !=
|
||||
ClockHandle::kStateVisible) {
|
||||
if (!old_meta.IsVisible()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// (Optimistically) increment acquire counter
|
||||
old_meta = h->meta.FetchAdd(ClockHandle::kAcquireIncrement);
|
||||
h->meta.Apply(AcquireCounter::PlusTransformPromiseNoOverflow(1),
|
||||
&old_meta);
|
||||
// Check if it's an entry visible to lookups
|
||||
if ((old_meta >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateVisible) {
|
||||
if (old_meta.IsVisible()) {
|
||||
// Acquired a read reference
|
||||
if (h->hashed_key == hashed_key) {
|
||||
// Match
|
||||
// Update the hit bit
|
||||
if (eviction_callback_) {
|
||||
h->meta.FetchOrRelaxed(uint64_t{1} << ClockHandle::kHitBitShift);
|
||||
h->meta.ApplyRelaxed(SlotMeta::HitFlag::SetTransform());
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
// Mismatch. Pretend we never took the reference
|
||||
Unref(*h);
|
||||
}
|
||||
} else if (UNLIKELY((old_meta >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateInvisible)) {
|
||||
} else if (UNLIKELY(old_meta.IsInvisible())) {
|
||||
// Pretend we never took the reference
|
||||
Unref(*h);
|
||||
} else {
|
||||
@@ -907,53 +892,49 @@ bool FixedHyperClockTable::Release(HandleImpl* h, bool useful,
|
||||
// is only freed up by EvictFromClock (called by Insert when space is needed)
|
||||
// and Erase. We do this to avoid an extra atomic read of the variable usage_.
|
||||
|
||||
uint64_t old_meta;
|
||||
SlotMeta old_meta;
|
||||
if (useful) {
|
||||
// Increment release counter to indicate was used
|
||||
old_meta = h->meta.FetchAdd(ClockHandle::kReleaseIncrement);
|
||||
auto add_release = ReleaseCounter::PlusTransformPromiseNoOverflow(1);
|
||||
h->meta.Apply(add_release, &old_meta);
|
||||
} else {
|
||||
// Decrement acquire counter to pretend it never happened
|
||||
old_meta = h->meta.FetchSub(ClockHandle::kAcquireIncrement);
|
||||
auto sub_acquire = AcquireCounter::MinusTransformPromiseNoUnderflow(1);
|
||||
h->meta.Apply(sub_acquire, &old_meta);
|
||||
}
|
||||
|
||||
assert((old_meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit);
|
||||
assert(old_meta.IsShareable());
|
||||
// No underflow
|
||||
assert(((old_meta >> ClockHandle::kAcquireCounterShift) &
|
||||
ClockHandle::kCounterMask) !=
|
||||
((old_meta >> ClockHandle::kReleaseCounterShift) &
|
||||
ClockHandle::kCounterMask));
|
||||
assert(old_meta.GetAcquireCounter() != old_meta.GetReleaseCounter());
|
||||
|
||||
if (erase_if_last_ref || UNLIKELY(old_meta >> ClockHandle::kStateShift ==
|
||||
ClockHandle::kStateInvisible)) {
|
||||
if (erase_if_last_ref || UNLIKELY(old_meta.IsInvisible())) {
|
||||
// FIXME: There's a chance here that another thread could replace this
|
||||
// entry and we end up erasing the wrong one.
|
||||
|
||||
// Update for last FetchAdd op
|
||||
// Update for last Apply op
|
||||
if (useful) {
|
||||
old_meta += ClockHandle::kReleaseIncrement;
|
||||
old_meta.SetReleaseCounter(old_meta.GetReleaseCounter() + 1);
|
||||
} else {
|
||||
old_meta -= ClockHandle::kAcquireIncrement;
|
||||
old_meta.SetAcquireCounter(old_meta.GetAcquireCounter() - 1);
|
||||
}
|
||||
// Take ownership if no refs
|
||||
SlotMeta construction_meta;
|
||||
construction_meta.SetUnderConstruction();
|
||||
do {
|
||||
if (GetRefcount(old_meta) != 0) {
|
||||
if (old_meta.GetRefcount() != 0) {
|
||||
// Not last ref at some point in time during this Release call
|
||||
// Correct for possible (but rare) overflow
|
||||
CorrectNearOverflow(old_meta, h->meta);
|
||||
return false;
|
||||
}
|
||||
if ((old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
||||
<< ClockHandle::kStateShift)) == 0) {
|
||||
if (!old_meta.IsShareable()) {
|
||||
// Someone else took ownership
|
||||
return false;
|
||||
}
|
||||
// Note that there's a small chance that we release, another thread
|
||||
// replaces this entry with another, reaches zero refs, and then we end
|
||||
// up erasing that other entry. That's an acceptable risk / imprecision.
|
||||
} while (
|
||||
!h->meta.CasWeak(old_meta, uint64_t{ClockHandle::kStateConstruction}
|
||||
<< ClockHandle::kStateShift));
|
||||
} while (!h->meta.CasWeak(old_meta, construction_meta));
|
||||
// Took ownership
|
||||
size_t total_charge = h->GetTotalCharge();
|
||||
if (UNLIKELY(h->IsStandalone())) {
|
||||
@@ -976,7 +957,7 @@ bool FixedHyperClockTable::Release(HandleImpl* h, bool useful,
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
void FixedHyperClockTable::TEST_ReleaseN(HandleImpl* h, size_t n) {
|
||||
void FixedHyperClockTable::TEST_ReleaseN(HandleImpl* h, uint32_t n) {
|
||||
if (n > 0) {
|
||||
// Do n-1 simple releases first
|
||||
TEST_ReleaseNMinus1(h, n);
|
||||
@@ -993,30 +974,29 @@ void FixedHyperClockTable::Erase(const UniqueId64x2& hashed_key) {
|
||||
[&](HandleImpl* h) {
|
||||
// Could be multiple entries in rare cases. Erase them all.
|
||||
// Optimistically increment acquire counter
|
||||
uint64_t old_meta = h->meta.FetchAdd(ClockHandle::kAcquireIncrement);
|
||||
auto add_acquire = AcquireCounter::PlusTransformPromiseNoOverflow(1);
|
||||
SlotMeta old_meta, meta;
|
||||
h->meta.Apply(add_acquire, &old_meta, &meta);
|
||||
// Check if it's an entry visible to lookups
|
||||
if ((old_meta >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateVisible) {
|
||||
if (meta.IsVisible()) {
|
||||
// Acquired a read reference
|
||||
if (h->hashed_key == hashed_key) {
|
||||
// Match. Set invisible.
|
||||
old_meta =
|
||||
h->meta.FetchAnd(~(uint64_t{ClockHandle::kStateVisibleBit}
|
||||
<< ClockHandle::kStateShift));
|
||||
// Apply update to local copy
|
||||
old_meta &= ~(uint64_t{ClockHandle::kStateVisibleBit}
|
||||
<< ClockHandle::kStateShift);
|
||||
// Match. Take ownership if no other refs, or set invisible other
|
||||
// refs exist.
|
||||
for (;;) {
|
||||
uint64_t refcount = GetRefcount(old_meta);
|
||||
uint32_t refcount = meta.GetRefcount();
|
||||
assert(refcount > 0);
|
||||
if (refcount > 1) {
|
||||
// Not last ref at some point in time during this Erase call
|
||||
// Pretend we never took the reference
|
||||
// Set invisible
|
||||
h->meta.Apply(SlotMeta::VisibleFlag::ClearTransform());
|
||||
// And pretend we never took the reference
|
||||
Unref(*h);
|
||||
break;
|
||||
} else if (h->meta.CasWeak(
|
||||
old_meta, uint64_t{ClockHandle::kStateConstruction}
|
||||
<< ClockHandle::kStateShift)) {
|
||||
}
|
||||
SlotMeta construction_meta;
|
||||
construction_meta.SetUnderConstruction();
|
||||
if (h->meta.CasWeak(meta, construction_meta)) {
|
||||
// Took ownership
|
||||
assert(hashed_key == h->hashed_key);
|
||||
size_t total_charge = h->GetTotalCharge();
|
||||
@@ -1032,8 +1012,7 @@ void FixedHyperClockTable::Erase(const UniqueId64x2& hashed_key) {
|
||||
// Mismatch. Pretend we never took the reference
|
||||
Unref(*h);
|
||||
}
|
||||
} else if (UNLIKELY((old_meta >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateInvisible)) {
|
||||
} else if (UNLIKELY(old_meta.IsInvisible())) {
|
||||
// Pretend we never took the reference
|
||||
Unref(*h);
|
||||
} else {
|
||||
@@ -1050,17 +1029,17 @@ void FixedHyperClockTable::EraseUnRefEntries() {
|
||||
for (size_t i = 0; i <= this->length_bits_mask_; i++) {
|
||||
HandleImpl& h = array_[i];
|
||||
|
||||
uint64_t old_meta = h.meta.LoadRelaxed();
|
||||
if (old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
||||
<< ClockHandle::kStateShift) &&
|
||||
GetRefcount(old_meta) == 0 &&
|
||||
h.meta.CasStrong(old_meta, uint64_t{ClockHandle::kStateConstruction}
|
||||
<< ClockHandle::kStateShift)) {
|
||||
// Took ownership
|
||||
size_t total_charge = h.GetTotalCharge();
|
||||
Rollback(h.hashed_key, &h);
|
||||
FreeDataMarkEmpty(h, allocator_);
|
||||
ReclaimEntryUsage(total_charge);
|
||||
SlotMeta old_meta = h.meta.LoadRelaxed();
|
||||
if (old_meta.IsShareable() && old_meta.GetRefcount() == 0) {
|
||||
SlotMeta construction_meta;
|
||||
construction_meta.SetUnderConstruction();
|
||||
if (h.meta.CasStrong(old_meta, construction_meta)) {
|
||||
// Took ownership
|
||||
size_t total_charge = h.GetTotalCharge();
|
||||
Rollback(h.hashed_key, &h);
|
||||
FreeDataMarkEmpty(h, allocator_);
|
||||
ReclaimEntryUsage(total_charge);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1320,12 +1299,12 @@ bool ClockCacheShard<Table>::Release(HandleImpl* handle, bool useful,
|
||||
|
||||
#ifndef NDEBUG
|
||||
template <class Table>
|
||||
void ClockCacheShard<Table>::TEST_RefN(HandleImpl* h, size_t n) {
|
||||
void ClockCacheShard<Table>::TEST_RefN(HandleImpl* h, uint32_t n) {
|
||||
table_.TEST_RefN(*h, n);
|
||||
}
|
||||
|
||||
template <class Table>
|
||||
void ClockCacheShard<Table>::TEST_ReleaseN(HandleImpl* h, size_t n) {
|
||||
void ClockCacheShard<Table>::TEST_ReleaseN(HandleImpl* h, uint32_t n) {
|
||||
table_.TEST_ReleaseN(h, n);
|
||||
}
|
||||
#endif
|
||||
@@ -1373,8 +1352,8 @@ size_t ClockCacheShard<Table>::GetPinnedUsage() const {
|
||||
metadata_charge_policy_ == kFullChargeCacheMetadata;
|
||||
ConstApplyToEntriesRange(
|
||||
[&table_pinned_usage, charge_metadata](const HandleImpl& h) {
|
||||
uint64_t meta = h.meta.LoadRelaxed();
|
||||
uint64_t refcount = GetRefcount(meta);
|
||||
SlotMeta meta = h.meta.LoadRelaxed();
|
||||
uint32_t refcount = meta.GetRefcount();
|
||||
// Holding one ref for ConstApplyToEntriesRange
|
||||
assert(refcount > 0);
|
||||
if (refcount > 1) {
|
||||
@@ -1494,7 +1473,7 @@ void AddShardEvaluation(const FixedHyperClockCache::Shard& shard,
|
||||
}
|
||||
|
||||
bool IsSlotOccupied(const ClockHandle& h) {
|
||||
return (h.meta.LoadRelaxed() >> ClockHandle::kStateShift) != 0;
|
||||
return !h.meta.LoadRelaxed().IsEmpty();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -1759,12 +1738,12 @@ inline bool MatchAndRef(const UniqueId64x2* hashed_key, const ClockHandle& h,
|
||||
// Must be at least something to match
|
||||
assert(hashed_key || shift > 0);
|
||||
|
||||
uint64_t old_meta;
|
||||
SlotMeta old_meta, new_meta;
|
||||
// (Optimistically) increment acquire counter.
|
||||
old_meta = h.meta.FetchAdd(ClockHandle::kAcquireIncrement);
|
||||
auto add_acquire = AcquireCounter::PlusTransformPromiseNoOverflow(1);
|
||||
h.meta.Apply(add_acquire, &old_meta, &new_meta);
|
||||
// Check if it's a referencable (sharable) entry
|
||||
if ((old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
||||
<< ClockHandle::kStateShift)) == 0) {
|
||||
if (!old_meta.IsShareable()) {
|
||||
// For non-sharable states, incrementing the acquire counter has no effect
|
||||
// so we don't need to undo it. Furthermore, we cannot safely undo
|
||||
// it because we did not acquire a read reference to lock the
|
||||
@@ -1775,10 +1754,9 @@ inline bool MatchAndRef(const UniqueId64x2* hashed_key, const ClockHandle& h,
|
||||
return false;
|
||||
}
|
||||
// Else acquired a read reference
|
||||
assert(GetRefcount(old_meta + ClockHandle::kAcquireIncrement) > 0);
|
||||
assert(new_meta.GetRefcount() > 0);
|
||||
if (hashed_key && h.hashed_key == *hashed_key &&
|
||||
LIKELY(old_meta & (uint64_t{ClockHandle::kStateVisibleBit}
|
||||
<< ClockHandle::kStateShift))) {
|
||||
LIKELY(old_meta.IsVisible())) {
|
||||
// Match on full key, visible
|
||||
if (full_match_or_unknown) {
|
||||
*full_match_or_unknown = true;
|
||||
@@ -1946,7 +1924,7 @@ class AutoHyperClockTable::ChainRewriteLock {
|
||||
}
|
||||
}
|
||||
|
||||
AcqRelBitFieldsAtomic<NextWithShift>* head_ptr_;
|
||||
BitFieldsAtomic<NextWithShift>* head_ptr_;
|
||||
NextWithShift saved_head_;
|
||||
};
|
||||
|
||||
@@ -2051,7 +2029,7 @@ AutoHyperClockTable::~AutoHyperClockTable() {
|
||||
HandleImpl::kUnusedMarker);
|
||||
assert(array_[i].chain_next_with_shift.LoadRelaxed() ==
|
||||
HandleImpl::kUnusedMarker);
|
||||
assert(array_[i].meta.LoadRelaxed() == 0);
|
||||
assert(array_[i].meta.LoadRelaxed() == SlotMeta{});
|
||||
}
|
||||
#endif // MUST_FREE_HEAP_ALLOCATIONS
|
||||
#ifndef NDEBUG // Extra invariant checking
|
||||
@@ -2060,30 +2038,27 @@ AutoHyperClockTable::~AutoHyperClockTable() {
|
||||
#endif // !NDEBUG
|
||||
for (size_t i = 0; i < used_end; i++) {
|
||||
HandleImpl& h = array_[i];
|
||||
switch (h.meta.LoadRelaxed() >> ClockHandle::kStateShift) {
|
||||
case ClockHandle::kStateEmpty:
|
||||
// noop
|
||||
break;
|
||||
case ClockHandle::kStateInvisible: // rare but possible
|
||||
case ClockHandle::kStateVisible:
|
||||
assert(GetRefcount(h.meta.LoadRelaxed()) == 0);
|
||||
h.FreeData(allocator_);
|
||||
SlotMeta meta = h.meta.LoadRelaxed();
|
||||
if (meta.IsShareable()) {
|
||||
// NOTE: Reaching here invisible is rare but possible
|
||||
assert(meta.GetRefcount() == 0);
|
||||
h.FreeData(allocator_);
|
||||
#ifndef NDEBUG // Extra invariant checking
|
||||
usage_.FetchSubRelaxed(h.total_charge);
|
||||
occupancy_.FetchSubRelaxed(1U);
|
||||
was_populated[i] = true;
|
||||
if (!h.chain_next_with_shift.LoadRelaxed().IsEnd()) {
|
||||
assert(!h.chain_next_with_shift.LoadRelaxed().IsLocked());
|
||||
size_t next = h.chain_next_with_shift.LoadRelaxed().GetNext();
|
||||
assert(!was_pointed_to[next]);
|
||||
was_pointed_to[next] = true;
|
||||
}
|
||||
usage_.FetchSubRelaxed(h.total_charge);
|
||||
occupancy_.FetchSubRelaxed(1U);
|
||||
was_populated[i] = true;
|
||||
if (!h.chain_next_with_shift.LoadRelaxed().IsEnd()) {
|
||||
assert(!h.chain_next_with_shift.LoadRelaxed().IsLocked());
|
||||
size_t next = h.chain_next_with_shift.LoadRelaxed().GetNext();
|
||||
assert(!was_pointed_to[next]);
|
||||
was_pointed_to[next] = true;
|
||||
}
|
||||
#endif // !NDEBUG
|
||||
break;
|
||||
// otherwise
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
} else {
|
||||
// Should be no transient "under construction" states unless a thread
|
||||
// was killed or we are being destructed while another thread is still
|
||||
// operating on the structure
|
||||
assert(meta.IsEmpty());
|
||||
}
|
||||
#ifndef NDEBUG // Extra invariant checking
|
||||
if (!h.head_next_with_shift.LoadRelaxed().IsEnd()) {
|
||||
@@ -2691,20 +2666,17 @@ void AutoHyperClockTable::PurgeImplLocked(OpData* op_data,
|
||||
op_data->push_back(h);
|
||||
// Entries for eviction become purgeable
|
||||
purgeable = true;
|
||||
assert((h->meta.Load() >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateConstruction);
|
||||
assert(h->meta.Load().IsUnderConstruction());
|
||||
}
|
||||
} else {
|
||||
(void)op_data;
|
||||
(void)data;
|
||||
purgeable = ((h->meta.Load() >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit) == 0;
|
||||
purgeable = !h->meta.Load().IsShareable();
|
||||
}
|
||||
}
|
||||
|
||||
if (purgeable) {
|
||||
assert((h->meta.Load() >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateConstruction);
|
||||
assert(h->meta.Load().IsUnderConstruction());
|
||||
pending_purge = true;
|
||||
} else if (pending_purge) {
|
||||
if (prev_to_keep) {
|
||||
@@ -2864,7 +2836,7 @@ void AutoHyperClockTable::PurgeImpl(OpData* op_data, size_t home,
|
||||
}
|
||||
|
||||
AutoHyperClockTable::HandleImpl* AutoHyperClockTable::DoInsert(
|
||||
const ClockHandleBasicData& proto, uint64_t initial_countdown,
|
||||
const ClockHandleBasicData& proto, uint32_t initial_countdown,
|
||||
bool take_ref, InsertState& state) {
|
||||
size_t home;
|
||||
int orig_home_shift;
|
||||
@@ -3149,14 +3121,14 @@ AutoHyperClockTable::HandleImpl* AutoHyperClockTable::Lookup(
|
||||
#endif
|
||||
if (probably_equal) {
|
||||
// Increment acquire counter for definitive check
|
||||
uint64_t old_meta = h->meta.FetchAdd(ClockHandle::kAcquireIncrement);
|
||||
auto add_acquire = AcquireCounter::PlusTransformPromiseNoOverflow(1);
|
||||
SlotMeta old_meta, new_meta;
|
||||
h->meta.Apply(add_acquire, &old_meta, &new_meta);
|
||||
// Check if it's a referencable (sharable) entry
|
||||
if (LIKELY(old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
||||
<< ClockHandle::kStateShift))) {
|
||||
assert(GetRefcount(old_meta + ClockHandle::kAcquireIncrement) > 0);
|
||||
if (LIKELY(old_meta.IsShareable())) {
|
||||
assert(new_meta.GetRefcount() > 0);
|
||||
if (LIKELY(h->hashed_key == hashed_key) &&
|
||||
LIKELY(old_meta & (uint64_t{ClockHandle::kStateVisibleBit}
|
||||
<< ClockHandle::kStateShift))) {
|
||||
LIKELY(old_meta.IsVisible())) {
|
||||
return h;
|
||||
} else {
|
||||
Unref(*h);
|
||||
@@ -3277,7 +3249,7 @@ AutoHyperClockTable::HandleImpl* AutoHyperClockTable::Lookup(
|
||||
}
|
||||
// Update the hit bit
|
||||
if (eviction_callback_) {
|
||||
h->meta.FetchOrRelaxed(uint64_t{1} << ClockHandle::kHitBitShift);
|
||||
h->meta.ApplyRelaxed(SlotMeta::HitFlag::SetTransform());
|
||||
}
|
||||
// All done.
|
||||
return h;
|
||||
@@ -3317,8 +3289,7 @@ AutoHyperClockTable::HandleImpl* AutoHyperClockTable::Lookup(
|
||||
}
|
||||
|
||||
void AutoHyperClockTable::Remove(HandleImpl* h) {
|
||||
assert((h->meta.Load() >> ClockHandle::kStateShift) ==
|
||||
ClockHandle::kStateConstruction);
|
||||
assert(h->meta.Load().IsUnderConstruction());
|
||||
|
||||
const HandleImpl& c_h = *h;
|
||||
PurgeImpl(&c_h.hashed_key);
|
||||
@@ -3326,26 +3297,23 @@ void AutoHyperClockTable::Remove(HandleImpl* h) {
|
||||
|
||||
bool AutoHyperClockTable::TryEraseHandle(HandleImpl* h, bool holding_ref,
|
||||
bool mark_invisible) {
|
||||
uint64_t meta;
|
||||
if (mark_invisible) {
|
||||
// Set invisible
|
||||
meta = h->meta.FetchAnd(
|
||||
~(uint64_t{ClockHandle::kStateVisibleBit} << ClockHandle::kStateShift));
|
||||
// To local variable also
|
||||
meta &=
|
||||
~(uint64_t{ClockHandle::kStateVisibleBit} << ClockHandle::kStateShift);
|
||||
} else {
|
||||
meta = h->meta.Load();
|
||||
}
|
||||
SlotMeta meta = h->meta.Load();
|
||||
assert(!holding_ref || meta.IsShareable());
|
||||
|
||||
// Take ownership if no other refs
|
||||
// Take ownership if no other refs, or set invisible if other refs exist (and
|
||||
// mark_invisible is set).
|
||||
SlotMeta construction_meta;
|
||||
construction_meta.SetUnderConstruction();
|
||||
do {
|
||||
if (GetRefcount(meta) != uint64_t{holding_ref}) {
|
||||
if (meta.GetRefcount() != uint32_t{holding_ref}) {
|
||||
// Not last ref at some point in time during this call
|
||||
if (mark_invisible) {
|
||||
// Set invisible
|
||||
h->meta.Apply(SlotMeta::VisibleFlag::ClearTransform());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if ((meta & (uint64_t{ClockHandle::kStateShareableBit}
|
||||
<< ClockHandle::kStateShift)) == 0) {
|
||||
if (!meta.IsShareable()) {
|
||||
// Someone else took ownership
|
||||
return false;
|
||||
}
|
||||
@@ -3353,8 +3321,7 @@ bool AutoHyperClockTable::TryEraseHandle(HandleImpl* h, bool holding_ref,
|
||||
// another thread replaces this entry with another, reaches zero refs, and
|
||||
// then we end up erasing that other entry. That's an acceptable risk /
|
||||
// imprecision.
|
||||
} while (!h->meta.CasWeak(meta, uint64_t{ClockHandle::kStateConstruction}
|
||||
<< ClockHandle::kStateShift));
|
||||
} while (!h->meta.CasWeak(meta, construction_meta));
|
||||
// Took ownership
|
||||
// TODO? Delay freeing?
|
||||
h->FreeData(allocator_);
|
||||
@@ -3381,27 +3348,24 @@ bool AutoHyperClockTable::Release(HandleImpl* h, bool useful,
|
||||
// is needed) and Erase. We do this to avoid an extra atomic read of the
|
||||
// variable usage_.
|
||||
|
||||
uint64_t old_meta;
|
||||
SlotMeta old_meta;
|
||||
if (useful) {
|
||||
// Increment release counter to indicate was used
|
||||
old_meta = h->meta.FetchAdd(ClockHandle::kReleaseIncrement);
|
||||
auto add_release = ReleaseCounter::PlusTransformPromiseNoOverflow(1);
|
||||
h->meta.Apply(add_release, &old_meta);
|
||||
// Correct for possible (but rare) overflow
|
||||
CorrectNearOverflow(old_meta, h->meta);
|
||||
} else {
|
||||
// Decrement acquire counter to pretend it never happened
|
||||
old_meta = h->meta.FetchSub(ClockHandle::kAcquireIncrement);
|
||||
auto sub_acquire = AcquireCounter::MinusTransformPromiseNoUnderflow(1);
|
||||
h->meta.Apply(sub_acquire, &old_meta);
|
||||
}
|
||||
|
||||
assert((old_meta >> ClockHandle::kStateShift) &
|
||||
ClockHandle::kStateShareableBit);
|
||||
assert(old_meta.IsShareable());
|
||||
// No underflow
|
||||
assert(((old_meta >> ClockHandle::kAcquireCounterShift) &
|
||||
ClockHandle::kCounterMask) !=
|
||||
((old_meta >> ClockHandle::kReleaseCounterShift) &
|
||||
ClockHandle::kCounterMask));
|
||||
assert(old_meta.GetAcquireCounter() != old_meta.GetReleaseCounter());
|
||||
|
||||
if ((erase_if_last_ref || UNLIKELY(old_meta >> ClockHandle::kStateShift ==
|
||||
ClockHandle::kStateInvisible))) {
|
||||
if ((erase_if_last_ref || UNLIKELY(old_meta.IsInvisible()))) {
|
||||
// FIXME: There's a chance here that another thread could replace this
|
||||
// entry and we end up erasing the wrong one.
|
||||
return TryEraseHandle(h, /*holding_ref=*/false, /*mark_invisible=*/false);
|
||||
@@ -3411,7 +3375,7 @@ bool AutoHyperClockTable::Release(HandleImpl* h, bool useful,
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
void AutoHyperClockTable::TEST_ReleaseN(HandleImpl* h, size_t n) {
|
||||
void AutoHyperClockTable::TEST_ReleaseN(HandleImpl* h, uint32_t n) {
|
||||
if (n > 0) {
|
||||
// Do n-1 simple releases first
|
||||
TEST_ReleaseNMinus1(h, n);
|
||||
@@ -3441,20 +3405,20 @@ void AutoHyperClockTable::EraseUnRefEntries() {
|
||||
for (size_t i = 0; i < usable_size; i++) {
|
||||
HandleImpl& h = array_[i];
|
||||
|
||||
uint64_t old_meta = h.meta.LoadRelaxed();
|
||||
if (old_meta & (uint64_t{ClockHandle::kStateShareableBit}
|
||||
<< ClockHandle::kStateShift) &&
|
||||
GetRefcount(old_meta) == 0 &&
|
||||
h.meta.CasStrong(old_meta, uint64_t{ClockHandle::kStateConstruction}
|
||||
<< ClockHandle::kStateShift)) {
|
||||
// Took ownership
|
||||
h.FreeData(allocator_);
|
||||
usage_.FetchSubRelaxed(h.total_charge);
|
||||
// NOTE: could be more efficient with a dedicated variant of
|
||||
// PurgeImpl, but this is not a common operation
|
||||
Remove(&h);
|
||||
MarkEmpty(h);
|
||||
occupancy_.FetchSub(1U);
|
||||
SlotMeta old_meta = h.meta.LoadRelaxed();
|
||||
if (old_meta.IsShareable() && old_meta.GetRefcount() == 0) {
|
||||
SlotMeta construction_meta;
|
||||
construction_meta.SetUnderConstruction();
|
||||
if (h.meta.CasStrong(old_meta, construction_meta)) {
|
||||
// Took ownership
|
||||
h.FreeData(allocator_);
|
||||
usage_.FetchSubRelaxed(h.total_charge);
|
||||
// NOTE: could be more efficient with a dedicated variant of
|
||||
// PurgeImpl, but this is not a common operation
|
||||
Remove(&h);
|
||||
MarkEmpty(h);
|
||||
occupancy_.FetchSub(1U);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+93
-44
@@ -317,40 +317,89 @@ struct ClockHandle : public ClockHandleBasicData {
|
||||
// | acquire counter | release counter | hit bit | state marker |
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
// For reading or updating counters in meta word.
|
||||
static constexpr uint8_t kCounterNumBits = 30;
|
||||
static constexpr uint64_t kCounterMask = (uint64_t{1} << kCounterNumBits) - 1;
|
||||
struct SlotMeta : public BitFields<uint64_t, SlotMeta> {
|
||||
// For reading or updating counters in meta word.
|
||||
static constexpr uint8_t kCounterNumBits = 30;
|
||||
// Number of times the a reference has been acquired (or attempted)
|
||||
// since last reset by eviction processing
|
||||
using AcquireCounter =
|
||||
UnsignedBitField<SlotMeta, kCounterNumBits, NoPrevBitField>;
|
||||
// Number of times the a reference has been released (or attempted)
|
||||
// since last reset by eviction processing
|
||||
using ReleaseCounter =
|
||||
UnsignedBitField<SlotMeta, kCounterNumBits, AcquireCounter>;
|
||||
// Metadata bit in support of secondary cache
|
||||
using HitFlag = BoolBitField<SlotMeta, ReleaseCounter>;
|
||||
// Occupied means any state other than empty
|
||||
using OccupiedFlag = BoolBitField<SlotMeta, HitFlag>;
|
||||
// Shareable means the entry is reference counted (visible or invisible)
|
||||
// (only set if also occupied)
|
||||
using ShareableFlag = BoolBitField<SlotMeta, OccupiedFlag>;
|
||||
// Visible is only set if also shareable (invisible can't be found by
|
||||
// Lookup)
|
||||
using VisibleFlag = BoolBitField<SlotMeta, ShareableFlag>;
|
||||
|
||||
static constexpr uint8_t kAcquireCounterShift = 0;
|
||||
static constexpr uint64_t kAcquireIncrement = uint64_t{1}
|
||||
<< kAcquireCounterShift;
|
||||
static constexpr uint8_t kReleaseCounterShift = kCounterNumBits;
|
||||
static constexpr uint64_t kReleaseIncrement = uint64_t{1}
|
||||
<< kReleaseCounterShift;
|
||||
// Convenience functions
|
||||
uint32_t GetAcquireCounter() const { return Get<AcquireCounter>(); }
|
||||
void SetAcquireCounter(uint32_t val) { Set<AcquireCounter>(val); }
|
||||
uint32_t GetReleaseCounter() const { return Get<ReleaseCounter>(); }
|
||||
void SetReleaseCounter(uint32_t val) { Set<ReleaseCounter>(val); }
|
||||
uint32_t GetRefcount() const {
|
||||
return Get<AcquireCounter>() - Get<ReleaseCounter>();
|
||||
}
|
||||
bool GetHit() const { return Get<HitFlag>(); }
|
||||
void SetHit(bool val) { Set<HitFlag>(val); }
|
||||
|
||||
// For setting the hit bit
|
||||
static constexpr uint8_t kHitBitShift = 2U * kCounterNumBits;
|
||||
static constexpr uint64_t kHitBitMask = uint64_t{1} << kHitBitShift;
|
||||
// Some distinct states for the various state flags
|
||||
bool IsEmpty() const {
|
||||
bool rv = !Get<OccupiedFlag>();
|
||||
if (rv) {
|
||||
assert(!Get<ShareableFlag>());
|
||||
assert(!Get<VisibleFlag>());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
// For reading or updating the state marker in meta word
|
||||
static constexpr uint8_t kStateShift = kHitBitShift + 1;
|
||||
bool IsUnderConstruction() const {
|
||||
bool rv = Get<OccupiedFlag>() && !Get<ShareableFlag>();
|
||||
if (rv) {
|
||||
assert(!Get<VisibleFlag>());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
void SetUnderConstruction() {
|
||||
Set<OccupiedFlag>(true);
|
||||
Set<ShareableFlag>(false);
|
||||
Set<VisibleFlag>(false);
|
||||
}
|
||||
|
||||
// Bits contribution to state marker.
|
||||
// Occupied means any state other than empty
|
||||
static constexpr uint8_t kStateOccupiedBit = 0b100;
|
||||
// Shareable means the entry is reference counted (visible or invisible)
|
||||
// (only set if also occupied)
|
||||
static constexpr uint8_t kStateShareableBit = 0b010;
|
||||
// Visible is only set if also shareable
|
||||
static constexpr uint8_t kStateVisibleBit = 0b001;
|
||||
bool IsShareable() const { return Get<ShareableFlag>(); }
|
||||
bool IsInvisible() const {
|
||||
bool rv = Get<ShareableFlag>() && !Get<VisibleFlag>();
|
||||
if (rv) {
|
||||
assert(Get<OccupiedFlag>());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
void SetInvisible() {
|
||||
Set<OccupiedFlag>(true);
|
||||
Set<ShareableFlag>(true);
|
||||
Set<VisibleFlag>(false);
|
||||
}
|
||||
|
||||
// Complete state markers (not shifted into full word)
|
||||
static constexpr uint8_t kStateEmpty = 0b000;
|
||||
static constexpr uint8_t kStateConstruction = kStateOccupiedBit;
|
||||
static constexpr uint8_t kStateInvisible =
|
||||
kStateOccupiedBit | kStateShareableBit;
|
||||
static constexpr uint8_t kStateVisible =
|
||||
kStateOccupiedBit | kStateShareableBit | kStateVisibleBit;
|
||||
bool IsVisible() const {
|
||||
bool rv = Get<ShareableFlag>() && Get<VisibleFlag>();
|
||||
if (rv) {
|
||||
assert(Get<OccupiedFlag>());
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
void SetVisible() {
|
||||
Set<OccupiedFlag>(true);
|
||||
Set<ShareableFlag>(true);
|
||||
Set<VisibleFlag>(true);
|
||||
}
|
||||
};
|
||||
|
||||
// Constants for initializing the countdown clock. (Countdown clock is only
|
||||
// in effect with zero refs, acquire counter == release counter, and in that
|
||||
@@ -364,7 +413,7 @@ struct ClockHandle : public ClockHandleBasicData {
|
||||
// TODO: make these coundown values tuning parameters for eviction?
|
||||
|
||||
// See above. Mutable for read reference counting.
|
||||
mutable AcqRelAtomic<uint64_t> meta{};
|
||||
mutable BitFieldsAtomic<SlotMeta> meta{};
|
||||
}; // struct ClockHandle
|
||||
|
||||
class BaseClockTable {
|
||||
@@ -431,9 +480,9 @@ class BaseClockTable {
|
||||
bool IsEvictionEffortExceeded(const BaseClockTable::EvictionData& data) const;
|
||||
#ifndef NDEBUG
|
||||
// Acquire N references
|
||||
void TEST_RefN(ClockHandle& handle, size_t n);
|
||||
void TEST_RefN(ClockHandle& handle, uint32_t n);
|
||||
// Helper for TEST_ReleaseN
|
||||
void TEST_ReleaseNMinus1(ClockHandle* handle, size_t n);
|
||||
void TEST_ReleaseNMinus1(ClockHandle* handle, uint32_t n);
|
||||
#endif
|
||||
|
||||
private: // fns
|
||||
@@ -489,13 +538,13 @@ class BaseClockTable {
|
||||
// TODO: is this separation needed if we don't do background evictions?
|
||||
ALIGN_AS(CACHE_LINE_SIZE)
|
||||
// Number of elements in the table.
|
||||
AcqRelAtomic<size_t> occupancy_{};
|
||||
Atomic<size_t> occupancy_{};
|
||||
|
||||
// Memory usage by entries tracked by the cache (including standalone)
|
||||
AcqRelAtomic<size_t> usage_{};
|
||||
Atomic<size_t> usage_{};
|
||||
|
||||
// Part of usage by standalone entries (not in table)
|
||||
AcqRelAtomic<size_t> standalone_usage_{};
|
||||
Atomic<size_t> standalone_usage_{};
|
||||
|
||||
// Maximum total charge of all elements stored in the table.
|
||||
// (Relaxed: eventual consistency/update is OK)
|
||||
@@ -586,7 +635,7 @@ class FixedHyperClockTable : public BaseClockTable {
|
||||
bool GrowIfNeeded(size_t new_occupancy, InsertState& state);
|
||||
|
||||
HandleImpl* DoInsert(const ClockHandleBasicData& proto,
|
||||
uint64_t initial_countdown, bool take_ref,
|
||||
uint32_t initial_countdown, bool take_ref,
|
||||
InsertState& state);
|
||||
|
||||
// Runs the clock eviction algorithm trying to reclaim at least
|
||||
@@ -614,7 +663,7 @@ class FixedHyperClockTable : public BaseClockTable {
|
||||
}
|
||||
|
||||
// Release N references
|
||||
void TEST_ReleaseN(HandleImpl* handle, size_t n);
|
||||
void TEST_ReleaseN(HandleImpl* handle, uint32_t n);
|
||||
#endif
|
||||
|
||||
// The load factor p is a real number in (0, 1) such that at all
|
||||
@@ -850,8 +899,8 @@ class AutoHyperClockTable : public BaseClockTable {
|
||||
|
||||
// See above. The head pointer is logically independent of the rest of
|
||||
// the entry, including the chain next pointer.
|
||||
AcqRelBitFieldsAtomic<NextWithShift> head_next_with_shift{kUnusedMarker};
|
||||
AcqRelBitFieldsAtomic<NextWithShift> chain_next_with_shift{kUnusedMarker};
|
||||
BitFieldsAtomic<NextWithShift> head_next_with_shift{kUnusedMarker};
|
||||
BitFieldsAtomic<NextWithShift> chain_next_with_shift{kUnusedMarker};
|
||||
|
||||
// For supporting CreateStandalone and some fallback cases.
|
||||
inline bool IsStandalone() const {
|
||||
@@ -897,7 +946,7 @@ class AutoHyperClockTable : public BaseClockTable {
|
||||
bool GrowIfNeeded(size_t new_occupancy, InsertState& state);
|
||||
|
||||
HandleImpl* DoInsert(const ClockHandleBasicData& proto,
|
||||
uint64_t initial_countdown, bool take_ref,
|
||||
uint32_t initial_countdown, bool take_ref,
|
||||
InsertState& state);
|
||||
|
||||
// Runs the clock eviction algorithm trying to reclaim at least
|
||||
@@ -925,7 +974,7 @@ class AutoHyperClockTable : public BaseClockTable {
|
||||
}
|
||||
|
||||
// Release N references
|
||||
void TEST_ReleaseN(HandleImpl* handle, size_t n);
|
||||
void TEST_ReleaseN(HandleImpl* handle, uint32_t n);
|
||||
#endif
|
||||
|
||||
// Maximum ratio of number of occupied slots to number of usable slots. The
|
||||
@@ -1007,7 +1056,7 @@ class AutoHyperClockTable : public BaseClockTable {
|
||||
// To maximize parallelization of Grow() operations, this field is only
|
||||
// updated opportunistically after Grow() operations and in DoInsert() where
|
||||
// it is found to be out-of-date. See CatchUpLengthInfoNoWait().
|
||||
AcqRelAtomic<uint64_t> length_info_;
|
||||
Atomic<uint64_t> length_info_;
|
||||
|
||||
// An already-computed version of the usable length times the max load
|
||||
// factor. Could be slightly out of date but GrowIfNeeded()/Grow() handle
|
||||
@@ -1130,8 +1179,8 @@ class ALIGN_AS(CACHE_LINE_SIZE) ClockCacheShard final : public CacheShardBase {
|
||||
return table_.TEST_MutableOccupancyLimit();
|
||||
}
|
||||
// Acquire/release N references
|
||||
void TEST_RefN(HandleImpl* handle, size_t n);
|
||||
void TEST_ReleaseN(HandleImpl* handle, size_t n);
|
||||
void TEST_RefN(HandleImpl* handle, uint32_t n);
|
||||
void TEST_ReleaseN(HandleImpl* handle, uint32_t n);
|
||||
#endif
|
||||
|
||||
private: // data
|
||||
|
||||
Vendored
+1
-5
@@ -50,8 +50,7 @@ CompressedSecondaryCache::CompressedSecondaryCache(
|
||||
std::make_shared<CacheReservationManagerImpl<CacheEntryRole::kMisc>>(
|
||||
cache_))),
|
||||
disable_cache_(opts.capacity == 0) {
|
||||
auto mgr =
|
||||
GetBuiltinCompressionManager(cache_options_.compress_format_version);
|
||||
auto mgr = GetBuiltinV2CompressionManager();
|
||||
compressor_ = mgr->GetCompressor(cache_options_.compression_opts,
|
||||
cache_options_.compression_type);
|
||||
decompressor_ =
|
||||
@@ -356,9 +355,6 @@ std::string CompressedSecondaryCache::GetPrintableOptions() const {
|
||||
const_cast<CompressionOptions&>(cache_options_.compression_opts))
|
||||
.c_str());
|
||||
ret.append(buffer);
|
||||
snprintf(buffer, kBufferSize, " compress_format_version : %d\n",
|
||||
cache_options_.compress_format_version);
|
||||
ret.append(buffer);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -856,8 +856,7 @@ TEST_P(CompressedSecondaryCacheTestWithCompressionParam, BasicTestFromString) {
|
||||
if (LZ4_Supported()) {
|
||||
sec_cache_uri =
|
||||
"compressed_secondary_cache://"
|
||||
"capacity=2048;num_shard_bits=0;compression_type=kLZ4Compression;"
|
||||
"compress_format_version=2";
|
||||
"capacity=2048;num_shard_bits=0;compression_type=kLZ4Compression";
|
||||
} else {
|
||||
ROCKSDB_GTEST_SKIP("This test requires LZ4 support.");
|
||||
sec_cache_uri =
|
||||
@@ -888,7 +887,7 @@ TEST_P(CompressedSecondaryCacheTestWithCompressionParam,
|
||||
sec_cache_uri =
|
||||
"compressed_secondary_cache://"
|
||||
"capacity=2048;num_shard_bits=0;compression_type=kLZ4Compression;"
|
||||
"compress_format_version=2;enable_custom_split_merge=true";
|
||||
"enable_custom_split_merge=true";
|
||||
} else {
|
||||
ROCKSDB_GTEST_SKIP("This test requires LZ4 support.");
|
||||
sec_cache_uri =
|
||||
|
||||
Vendored
+18
-18
@@ -1503,7 +1503,7 @@ TEST_P(BasicSecondaryCacheTest, FullCapacityTest) {
|
||||
/*context*/ this, Cache::Priority::LOW);
|
||||
ASSERT_EQ(handle1, nullptr);
|
||||
|
||||
// k1 promotion can fail with strict_capacit_limit=true, but Lookup still
|
||||
// k1 promotion can fail with strict_capacity_limit=true, but Lookup still
|
||||
// succeeds using a standalone handle
|
||||
handle1 = cache->Lookup(k1.AsSlice(), GetHelper(),
|
||||
/*context*/ this, Cache::Priority::LOW);
|
||||
@@ -1680,7 +1680,7 @@ TEST_P(DBSecondaryCacheTest, TestSecondaryCacheCorrectness2) {
|
||||
// After Flush is successful, RocksDB will do the paranoid check for the new
|
||||
// SST file. Meta blocks are always cached in the block cache and they
|
||||
// will not be evicted. When block_2 is cache miss and read out, it is
|
||||
// inserted to the block cache. Thefore, block_1 is evicted from block
|
||||
// inserted to the block cache. Therefore, block_1 is evicted from block
|
||||
// cache and successfully inserted to the secondary cache. Here are 2
|
||||
// lookups in the secondary cache for block_1 and block_2.
|
||||
ASSERT_EQ(secondary_cache->num_inserts(), 1u);
|
||||
@@ -1721,7 +1721,7 @@ TEST_P(DBSecondaryCacheTest, TestSecondaryCacheCorrectness2) {
|
||||
v = Get(Key(0));
|
||||
ASSERT_EQ(1007, v.size());
|
||||
// This Get needs to access block_1, since block_1 is not in block cache
|
||||
// there is one econdary cache lookup. Then, block_1 is cached in the
|
||||
// there is one secondary cache lookup. Then, block_1 is cached in the
|
||||
// block cache.
|
||||
ASSERT_EQ(secondary_cache->num_inserts(), 2u);
|
||||
ASSERT_EQ(secondary_cache->num_lookups(), 5u);
|
||||
@@ -1785,7 +1785,7 @@ TEST_P(DBSecondaryCacheTest, NoSecondaryCacheInsertion) {
|
||||
std::string v = Get(Key(0));
|
||||
ASSERT_EQ(1000, v.size());
|
||||
// Since the block cache is large enough, all the blocks are cached. we
|
||||
// do not need to lookup the seondary cache.
|
||||
// do not need to lookup the secondary cache.
|
||||
ASSERT_EQ(secondary_cache->num_inserts(), 0u);
|
||||
ASSERT_EQ(secondary_cache->num_lookups(), 2u);
|
||||
|
||||
@@ -2150,7 +2150,7 @@ TEST_P(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
|
||||
ASSERT_OK(Flush());
|
||||
Compact("a", "z");
|
||||
|
||||
// do th eread for all the key value pairs, so all the blocks should be in
|
||||
// do the read for all the key value pairs, so all the blocks should be in
|
||||
// cache
|
||||
uint32_t start_insert = cache->GetInsertCount();
|
||||
uint32_t start_lookup = cache->GetLookupcount();
|
||||
@@ -2179,7 +2179,7 @@ TEST_P(DBSecondaryCacheTest, LRUCacheDumpLoadBasic) {
|
||||
&cache_dumper);
|
||||
ASSERT_OK(s);
|
||||
std::vector<DB*> db_list;
|
||||
db_list.push_back(db_);
|
||||
db_list.push_back(db_.get());
|
||||
s = cache_dumper->SetDumpFilter(db_list);
|
||||
ASSERT_OK(s);
|
||||
s = cache_dumper->DumpCacheEntriesToWriter();
|
||||
@@ -2263,11 +2263,11 @@ TEST_P(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
|
||||
options.env = fault_env_.get();
|
||||
std::string dbname1 = test::PerThreadDBPath("db_1");
|
||||
ASSERT_OK(DestroyDB(dbname1, options));
|
||||
DB* db1 = nullptr;
|
||||
std::unique_ptr<DB> db1;
|
||||
ASSERT_OK(DB::Open(options, dbname1, &db1));
|
||||
std::string dbname2 = test::PerThreadDBPath("db_2");
|
||||
ASSERT_OK(DestroyDB(dbname2, options));
|
||||
DB* db2 = nullptr;
|
||||
std::unique_ptr<DB> db2;
|
||||
ASSERT_OK(DB::Open(options, dbname2, &db2));
|
||||
fault_fs_->SetFailGetUniqueId(true);
|
||||
|
||||
@@ -2335,7 +2335,7 @@ TEST_P(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
|
||||
&cache_dumper);
|
||||
ASSERT_OK(s);
|
||||
std::vector<DB*> db_list;
|
||||
db_list.push_back(db1);
|
||||
db_list.push_back(db1.get());
|
||||
s = cache_dumper->SetDumpFilter(db_list);
|
||||
ASSERT_OK(s);
|
||||
s = cache_dumper->DumpCacheEntriesToWriter();
|
||||
@@ -2377,7 +2377,7 @@ TEST_P(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
|
||||
ASSERT_OK(s);
|
||||
|
||||
ASSERT_OK(db1->Close());
|
||||
delete db1;
|
||||
db1.reset();
|
||||
ASSERT_OK(DB::Open(options, dbname1, &db1));
|
||||
|
||||
// After load, we do the Get again. To validate the cache, we do not allow any
|
||||
@@ -2406,8 +2406,8 @@ TEST_P(DBSecondaryCacheTest, LRUCacheDumpLoadWithFilter) {
|
||||
ASSERT_EQ(256, static_cast<int>(block_lookup));
|
||||
fault_fs_->SetFailGetUniqueId(false);
|
||||
fault_fs_->SetFilesystemActive(true);
|
||||
delete db1;
|
||||
delete db2;
|
||||
db1.reset();
|
||||
db2.reset();
|
||||
ASSERT_OK(DestroyDB(dbname1, options));
|
||||
ASSERT_OK(DestroyDB(dbname2, options));
|
||||
}
|
||||
@@ -2464,7 +2464,7 @@ TEST_P(DBSecondaryCacheTest, TestSecondaryCacheOptionBasic) {
|
||||
std::string v = Get(Key(0));
|
||||
ASSERT_EQ(1007, v.size());
|
||||
|
||||
// Check the data in first block. Cache miss, direclty read from SST file.
|
||||
// Check the data in first block. Cache miss, directly read from SST file.
|
||||
ASSERT_EQ(secondary_cache->num_inserts(), 0u);
|
||||
ASSERT_EQ(secondary_cache->num_lookups(), 0u);
|
||||
|
||||
@@ -2598,7 +2598,7 @@ TEST_P(DBSecondaryCacheTest, TestSecondaryCacheOptionChange) {
|
||||
}
|
||||
|
||||
// Two DB test. We create 2 DBs sharing the same block cache and secondary
|
||||
// cache. We diable the secondary cache option for DB2.
|
||||
// cache. We disable the secondary cache option for DB2.
|
||||
TEST_P(DBSecondaryCacheTest, TestSecondaryCacheOptionTwoDB) {
|
||||
if (IsHyperClock()) {
|
||||
ROCKSDB_GTEST_BYPASS("Test depends on LRUCache-specific behaviors");
|
||||
@@ -2619,11 +2619,11 @@ TEST_P(DBSecondaryCacheTest, TestSecondaryCacheOptionTwoDB) {
|
||||
options.paranoid_file_checks = true;
|
||||
std::string dbname1 = test::PerThreadDBPath("db_t_1");
|
||||
ASSERT_OK(DestroyDB(dbname1, options));
|
||||
DB* db1 = nullptr;
|
||||
std::unique_ptr<DB> db1;
|
||||
ASSERT_OK(DB::Open(options, dbname1, &db1));
|
||||
std::string dbname2 = test::PerThreadDBPath("db_t_2");
|
||||
ASSERT_OK(DestroyDB(dbname2, options));
|
||||
DB* db2 = nullptr;
|
||||
std::unique_ptr<DB> db2;
|
||||
Options options2 = options;
|
||||
options2.lowest_used_cache_tier = CacheTier::kVolatileTier;
|
||||
ASSERT_OK(DB::Open(options2, dbname2, &db2));
|
||||
@@ -2700,8 +2700,8 @@ TEST_P(DBSecondaryCacheTest, TestSecondaryCacheOptionTwoDB) {
|
||||
|
||||
fault_fs_->SetFailGetUniqueId(false);
|
||||
fault_fs_->SetFilesystemActive(true);
|
||||
delete db1;
|
||||
delete db2;
|
||||
db1.reset();
|
||||
db2.reset();
|
||||
ASSERT_OK(DestroyDB(dbname1, options));
|
||||
ASSERT_OK(DestroyDB(dbname2, options));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,512 @@
|
||||
# Adding New Options to RocksDB Public API
|
||||
|
||||
This document provides guidance on how to add new options to RocksDB's public API. There are two main categories of options:
|
||||
|
||||
1. **Standard Column Family Options** (Options/DBOptions/AdvancedColumnFamilyOptions)
|
||||
2. **BlockBasedTableOptions** (options specific to block-based table format)
|
||||
|
||||
## Overview of Files to Modify
|
||||
|
||||
### For Standard Column Family Options
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `include/rocksdb/advanced_options.h` | Define the option with documentation |
|
||||
| `include/rocksdb/options.h` | Add reference in related option groups if needed |
|
||||
| `options/cf_options.h` | Add to `MutableCFOptions` or `ImmutableCFOptions` struct |
|
||||
| `options/cf_options.cc` | Register option for serialization/deserialization and logging |
|
||||
| `options/options_helper.cc` | Add to `UpdateColumnFamilyOptions()` for mutable options |
|
||||
| `options/options_settable_test.cc` | Add to test string for option parsing |
|
||||
| `db_stress_tool/db_stress_common.h` | Declare gflag |
|
||||
| `db_stress_tool/db_stress_gflags.cc` | Define gflag with default value |
|
||||
| `db_stress_tool/db_stress_test_base.cc` | Apply flag to options |
|
||||
| `tools/db_bench_tool.cc` | Add flag definition and apply to options |
|
||||
| `tools/db_crashtest.py` | Add randomized values for stress testing |
|
||||
| `unreleased_history/new_features/` | Add release note markdown file |
|
||||
|
||||
### For BlockBasedTableOptions
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `include/rocksdb/table.h` | Define the option in `BlockBasedTableOptions` struct |
|
||||
| `table/block_based/block_based_table_factory.cc` | Register for serialization, validation, and printing |
|
||||
| `options/options_settable_test.cc` | Add to `BlockBasedTableOptionsAllFieldsSettable` test |
|
||||
| `options/options_test.cc` | Add to `MutableCFOptions` test if applicable |
|
||||
| `db_stress_tool/db_stress_common.h` | Declare gflag |
|
||||
| `db_stress_tool/db_stress_gflags.cc` | Define gflag |
|
||||
| `db_stress_tool/db_stress_test_base.cc` | Apply flag to `block_based_options` |
|
||||
| `tools/db_bench_tool.cc` | Add flag definition and apply to `block_based_options` |
|
||||
| `tools/db_crashtest.py` | Add randomized values |
|
||||
| `java/src/main/java/org/rocksdb/BlockBasedTableConfig.java` | Java API |
|
||||
| `java/rocksjni/portal.h` | JNI portal for Java bindings |
|
||||
| `java/rocksjni/table.cc` | JNI implementation |
|
||||
| `java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java` | Java unit test |
|
||||
|
||||
---
|
||||
|
||||
## Pattern 1: Adding a Standard Column Family Option
|
||||
|
||||
Example reference: commit `94e65a2e0b4f817aa4bfa4c96cdf867e7980d7bc` (memtable_veirfy_per_key_checksum_on_seek)
|
||||
|
||||
### Step 1: Define the Option in Public Header
|
||||
|
||||
**File: `include/rocksdb/advanced_options.h`**
|
||||
|
||||
Add the option with documentation in `AdvancedColumnFamilyOptions` struct:
|
||||
|
||||
```cpp
|
||||
// Enables additional integrity checks during seek.
|
||||
// Specifically, for skiplist-based memtables, key checksum validation could
|
||||
// be enabled during seek optionally. This is helpful to detect corrupted
|
||||
// memtable keys during reads. Enabling this feature incurs a performance
|
||||
// overhead due to additional key checksum validation during memtable seek
|
||||
// operation.
|
||||
// This option depends on memtable_protection_bytes_per_key to be non zero.
|
||||
// If memtable_protection_bytes_per_key is zero, no validation is performed.
|
||||
bool memtable_veirfy_per_key_checksum_on_seek = false;
|
||||
```
|
||||
|
||||
### Step 2: Add to Internal Options Structs
|
||||
|
||||
**File: `options/cf_options.h`**
|
||||
|
||||
Add to `MutableCFOptions` struct (or `ImmutableCFOptions` for immutable options):
|
||||
|
||||
```cpp
|
||||
// In MutableCFOptions constructor from Options:
|
||||
memtable_veirfy_per_key_checksum_on_seek(
|
||||
options.memtable_veirfy_per_key_checksum_on_seek),
|
||||
|
||||
// In MutableCFOptions default constructor:
|
||||
memtable_veirfy_per_key_checksum_on_seek(false),
|
||||
|
||||
// In MutableCFOptions struct member declarations:
|
||||
bool memtable_veirfy_per_key_checksum_on_seek;
|
||||
```
|
||||
|
||||
### Step 3: Register for Serialization/Deserialization
|
||||
|
||||
**File: `options/cf_options.cc`**
|
||||
|
||||
Add to the options type info map for serialization:
|
||||
|
||||
```cpp
|
||||
{"memtable_veirfy_per_key_checksum_on_seek",
|
||||
{offsetof(struct MutableCFOptions,
|
||||
memtable_veirfy_per_key_checksum_on_seek),
|
||||
OptionType::kBoolean, OptionVerificationType::kNormal,
|
||||
OptionTypeFlags::kMutable}},
|
||||
```
|
||||
|
||||
Add logging in `MutableCFOptions::Dump()`:
|
||||
|
||||
```cpp
|
||||
ROCKS_LOG_INFO(log, "memtable_veirfy_per_key_checksum_on_seek: %d",
|
||||
memtable_veirfy_per_key_checksum_on_seek);
|
||||
```
|
||||
|
||||
### Step 4: Update Options Helper
|
||||
|
||||
**File: `options/options_helper.cc`**
|
||||
|
||||
Add to `UpdateColumnFamilyOptions()`:
|
||||
|
||||
```cpp
|
||||
cf_opts->memtable_veirfy_per_key_checksum_on_seek =
|
||||
moptions.memtable_veirfy_per_key_checksum_on_seek;
|
||||
```
|
||||
|
||||
### Step 5: Add to Options Settable Test
|
||||
|
||||
**File: `options/options_settable_test.cc`**
|
||||
|
||||
Add to the test string in `ColumnFamilyOptionsAllFieldsSettable`:
|
||||
|
||||
```cpp
|
||||
"memtable_veirfy_per_key_checksum_on_seek=1;"
|
||||
```
|
||||
|
||||
### Step 6: Add db_stress Support
|
||||
|
||||
**File: `db_stress_tool/db_stress_common.h`**
|
||||
|
||||
```cpp
|
||||
DECLARE_bool(memtable_veirfy_per_key_checksum_on_seek);
|
||||
```
|
||||
|
||||
**File: `db_stress_tool/db_stress_gflags.cc`**
|
||||
|
||||
```cpp
|
||||
DEFINE_bool(
|
||||
memtable_veirfy_per_key_checksum_on_seek,
|
||||
ROCKSDB_NAMESPACE::Options().memtable_veirfy_per_key_checksum_on_seek,
|
||||
"Sets CF option memtable_veirfy_per_key_checksum_on_seek.");
|
||||
```
|
||||
|
||||
**File: `db_stress_tool/db_stress_test_base.cc`**
|
||||
|
||||
```cpp
|
||||
options.memtable_veirfy_per_key_checksum_on_seek =
|
||||
FLAGS_memtable_veirfy_per_key_checksum_on_seek;
|
||||
```
|
||||
|
||||
### Step 7: Add db_bench Support
|
||||
|
||||
**File: `tools/db_bench_tool.cc`**
|
||||
|
||||
```cpp
|
||||
// Flag definition (near related flags):
|
||||
DEFINE_bool(memtable_veirfy_per_key_checksum_on_seek, false,
|
||||
"Sets CF option memtable_veirfy_per_key_checksum_on_seek");
|
||||
|
||||
// Apply flag to options (in InitializeOptionsFromFlags or similar):
|
||||
options.memtable_veirfy_per_key_checksum_on_seek =
|
||||
FLAGS_memtable_veirfy_per_key_checksum_on_seek;
|
||||
```
|
||||
|
||||
### Step 8: Add Crash Test Support
|
||||
|
||||
**File: `tools/db_crashtest.py`**
|
||||
|
||||
```python
|
||||
"memtable_veirfy_per_key_checksum_on_seek": lambda: random.choice([0] * 7 + [1]),
|
||||
```
|
||||
|
||||
Also add constraint handling in `finalize_and_sanitize()` if needed:
|
||||
|
||||
```python
|
||||
# only skip list memtable representation supports paranoid memory checks
|
||||
if dest_params.get("memtablerep") != "skip_list":
|
||||
dest_params["memtable_veirfy_per_key_checksum_on_seek"] = 0
|
||||
```
|
||||
|
||||
### Step 9: Add Release Note
|
||||
|
||||
**File: `unreleased_history/new_features/<descriptive_name>.md`**
|
||||
|
||||
```markdown
|
||||
A new flag memtable_veirfy_per_key_checksum_on_seek is added to AdvancedColumnFamilyOptions. When it is enabled, it will validate key checksum along the binary search path on skiplist based memtable during seek operation.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Pattern 2: Adding a BlockBasedTableOptions Option
|
||||
|
||||
Example reference: commit `742741b175c5f238374c1714f9db3340d49de569` (super_block_alignment_size)
|
||||
|
||||
### Step 1: Define the Option in Public Header
|
||||
|
||||
**File: `include/rocksdb/table.h`**
|
||||
|
||||
Add to `BlockBasedTableOptions` struct with documentation:
|
||||
|
||||
```cpp
|
||||
// Align data blocks on super block alignment. Avoid a data block split across
|
||||
// super block boundaries. Works with/without compression.
|
||||
//
|
||||
// Here a "super block" refers to an aligned unit of underlying Filesystem
|
||||
// storage for which there is an extra cost when a random read involves two
|
||||
// such super blocks instead of just one. Configuring that size here suggests
|
||||
// inserting padding in the SST file to avoid a single SST block splitting
|
||||
// across two super blocks. Only power-of-two sizes are supported. See also
|
||||
// super_block_alignment_space_overhead_ratio. Default to 0, which means super
|
||||
// block alignment is disabled.
|
||||
size_t super_block_alignment_size = 0;
|
||||
|
||||
// This option controls the storage space overhead of super block alignment.
|
||||
// It is used to calculate the max padding size allowed for super block
|
||||
// alignment. It is calculated in this way. If super_block_alignment_size is
|
||||
// 2MB, and super_block_alignment_overhead_ratio is 128, then the max padding
|
||||
// size allowed for super block alignment is 2MB / 128 = 16KB.
|
||||
// Note that, when it is set to 0, super block alignment is disabled.
|
||||
size_t super_block_alignment_space_overhead_ratio = 128;
|
||||
```
|
||||
|
||||
### Step 2: Register for Serialization in Table Factory
|
||||
|
||||
**File: `table/block_based/block_based_table_factory.cc`**
|
||||
|
||||
Add to the type info map:
|
||||
|
||||
```cpp
|
||||
{"super_block_alignment_size",
|
||||
{offsetof(struct BlockBasedTableOptions, super_block_alignment_size),
|
||||
OptionType::kSizeT, OptionVerificationType::kNormal}},
|
||||
{"super_block_alignment_space_overhead_ratio",
|
||||
{offsetof(struct BlockBasedTableOptions,
|
||||
super_block_alignment_space_overhead_ratio),
|
||||
OptionType::kSizeT, OptionVerificationType::kNormal}},
|
||||
```
|
||||
|
||||
Add validation in `ValidateOptions()`:
|
||||
|
||||
```cpp
|
||||
if ((table_options_.super_block_alignment_size &
|
||||
(table_options_.super_block_alignment_size - 1))) {
|
||||
return Status::InvalidArgument(
|
||||
"Super Block alignment requested but super block alignment size is not "
|
||||
"a power of 2");
|
||||
}
|
||||
if (table_options_.super_block_alignment_size >
|
||||
std::numeric_limits<uint32_t>::max()) {
|
||||
return Status::InvalidArgument(
|
||||
"Super block alignment size exceeds maximum number (4GiB) allowed");
|
||||
}
|
||||
```
|
||||
|
||||
Add printing in `GetPrintableOptions()`:
|
||||
|
||||
```cpp
|
||||
snprintf(buffer, kBufferSize,
|
||||
" super_block_alignment_size: %" ROCKSDB_PRIszt "\n",
|
||||
table_options_.super_block_alignment_size);
|
||||
ret.append(buffer);
|
||||
```
|
||||
|
||||
### Step 3: Add to Options Settable Test
|
||||
|
||||
**File: `options/options_settable_test.cc`**
|
||||
|
||||
Add to `BlockBasedTableOptionsAllFieldsSettable` test:
|
||||
|
||||
```cpp
|
||||
"super_block_alignment_size=65536;"
|
||||
"super_block_alignment_space_overhead_ratio=4096;"
|
||||
```
|
||||
|
||||
### Step 4: Add to Options Test
|
||||
|
||||
**File: `options/options_test.cc`**
|
||||
|
||||
```cpp
|
||||
ASSERT_OK(GetColumnFamilyOptionsFromString(
|
||||
config_options, cf_opts,
|
||||
"block_based_table_factory.super_block_alignment_size=65536; "
|
||||
"block_based_table_factory.super_block_alignment_space_overhead_ratio=4096;",
|
||||
&cf_opts));
|
||||
ASSERT_EQ(bbto->super_block_alignment_size, 65536);
|
||||
ASSERT_EQ(bbto->super_block_alignment_space_overhead_ratio, 4096);
|
||||
```
|
||||
|
||||
### Step 5: Add db_stress Support
|
||||
|
||||
**File: `db_stress_tool/db_stress_common.h`**
|
||||
|
||||
```cpp
|
||||
DECLARE_uint64(super_block_alignment_size);
|
||||
DECLARE_uint64(super_block_alignment_space_overhead_ratio);
|
||||
```
|
||||
|
||||
**File: `db_stress_tool/db_stress_gflags.cc`**
|
||||
|
||||
```cpp
|
||||
DEFINE_uint64(
|
||||
super_block_alignment_size,
|
||||
ROCKSDB_NAMESPACE::BlockBasedTableOptions().super_block_alignment_size,
|
||||
"BlockBasedTableOptions.super_block_alignment_size");
|
||||
|
||||
DEFINE_uint64(
|
||||
super_block_alignment_space_overhead_ratio,
|
||||
ROCKSDB_NAMESPACE::BlockBasedTableOptions()
|
||||
.super_block_alignment_space_overhead_ratio,
|
||||
"BlockBasedTableOptions.super_block_alignment_space_overhead_ratio");
|
||||
```
|
||||
|
||||
**File: `db_stress_tool/db_stress_test_base.cc`**
|
||||
|
||||
```cpp
|
||||
block_based_options.super_block_alignment_size =
|
||||
fLU64::FLAGS_super_block_alignment_size;
|
||||
block_based_options.super_block_alignment_space_overhead_ratio =
|
||||
fLU64::FLAGS_super_block_alignment_space_overhead_ratio;
|
||||
```
|
||||
|
||||
### Step 6: Add db_bench Support
|
||||
|
||||
**File: `tools/db_bench_tool.cc`**
|
||||
|
||||
```cpp
|
||||
// Flag definitions:
|
||||
DEFINE_uint64(
|
||||
super_block_alignment_size,
|
||||
ROCKSDB_NAMESPACE::BlockBasedTableOptions().super_block_alignment_size,
|
||||
"Configure super block size");
|
||||
|
||||
DEFINE_uint64(super_block_alignment_space_overhead_ratio,
|
||||
ROCKSDB_NAMESPACE::BlockBasedTableOptions()
|
||||
.super_block_alignment_space_overhead_ratio,
|
||||
"Configure space overhead for super block alignment");
|
||||
|
||||
// Apply to block_based_options (in the block where other options are set):
|
||||
block_based_options.super_block_alignment_size = FLAGS_super_block_alignment_size;
|
||||
block_based_options.super_block_alignment_space_overhead_ratio =
|
||||
FLAGS_super_block_alignment_space_overhead_ratio;
|
||||
```
|
||||
|
||||
### Step 7: Add Crash Test Support
|
||||
|
||||
**File: `tools/db_crashtest.py`**
|
||||
|
||||
```python
|
||||
"super_block_alignment_size": lambda: random.choice(
|
||||
[0, 128 * 1024, 512 * 1024, 2 * 1024 * 1024]
|
||||
),
|
||||
"super_block_alignment_space_overhead_ratio": lambda: random.choice([0, 32, 4096]),
|
||||
```
|
||||
|
||||
### Step 8: Add Java API Support
|
||||
|
||||
**File: `java/src/main/java/org/rocksdb/BlockBasedTableConfig.java`**
|
||||
|
||||
Add getter and setter methods:
|
||||
|
||||
```java
|
||||
/**
|
||||
* Get the super block alignment size.
|
||||
*
|
||||
* @return the super block alignment size.
|
||||
*/
|
||||
public long superBlockAlignmentSize() {
|
||||
return superBlockAlignmentSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the super block alignment size.
|
||||
* When set to 0, super block alignment is disabled.
|
||||
*
|
||||
* @param superBlockAlignmentSize the super block alignment size.
|
||||
*
|
||||
* @return the reference to the current option.
|
||||
*/
|
||||
public BlockBasedTableConfig setSuperBlockAlignmentSize(final long superBlockAlignmentSize) {
|
||||
this.superBlockAlignmentSize = superBlockAlignmentSize;
|
||||
return this;
|
||||
}
|
||||
```
|
||||
|
||||
Add member variable:
|
||||
|
||||
```java
|
||||
private long superBlockAlignmentSize;
|
||||
```
|
||||
|
||||
Update constructor and native method signature.
|
||||
|
||||
**File: `java/rocksjni/portal.h`**
|
||||
|
||||
Update `GetMethodID` signature and add fields to Java object construction.
|
||||
|
||||
**File: `java/rocksjni/table.cc`**
|
||||
|
||||
Add parameters to JNI function and apply to options.
|
||||
|
||||
**File: `java/src/test/java/org/rocksdb/BlockBasedTableConfigTest.java`**
|
||||
|
||||
Add unit tests:
|
||||
|
||||
```java
|
||||
@Test
|
||||
public void superBlockAlignmentSize() {
|
||||
final BlockBasedTableConfig blockBasedTableConfig = new BlockBasedTableConfig();
|
||||
blockBasedTableConfig.setSuperBlockAlignmentSize(1024 * 1024);
|
||||
assertThat(blockBasedTableConfig.superBlockAlignmentSize()).isEqualTo(1024 * 1024);
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Pattern 3: Adding C API for Existing Option
|
||||
|
||||
Example reference: commit `429b36c22d76403d275dd0e6877b08d4cea2bc90` (block_align C API)
|
||||
|
||||
If an option already exists but needs C API support:
|
||||
|
||||
**File: `db/c.cc`**
|
||||
|
||||
```cpp
|
||||
void rocksdb_block_based_options_set_block_align(
|
||||
rocksdb_block_based_table_options_t* options, unsigned char v) {
|
||||
options->rep.block_align = v;
|
||||
}
|
||||
```
|
||||
|
||||
**File: `include/rocksdb/c.h`**
|
||||
|
||||
```cpp
|
||||
extern ROCKSDB_LIBRARY_API void rocksdb_block_based_options_set_block_align(
|
||||
rocksdb_block_based_table_options_t*, unsigned char);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Unit Testing Guidelines
|
||||
|
||||
### For Standard Options
|
||||
|
||||
Add tests in appropriate test files (e.g., `db/db_memtable_test.cc`, `db/db_options_test.cc`):
|
||||
|
||||
```cpp
|
||||
TEST_F(DBMemTableTest, YourOptionTest) {
|
||||
Options options;
|
||||
options.your_new_option = true;
|
||||
Reopen(options);
|
||||
// Test the behavior
|
||||
}
|
||||
```
|
||||
|
||||
### For BlockBasedTableOptions
|
||||
|
||||
Add tests in `db/db_flush_test.cc`, `table/block_based/block_based_table_reader_test.cc`, or `table/table_test.cc`:
|
||||
|
||||
```cpp
|
||||
TEST_P(DBFlushYourFeatureTest, YourFeature) {
|
||||
Options options;
|
||||
BlockBasedTableOptions block_options;
|
||||
block_options.your_new_option = some_value;
|
||||
options.table_factory.reset(NewBlockBasedTableFactory(block_options));
|
||||
|
||||
ASSERT_OK(options.table_factory->ValidateOptions(
|
||||
DBOptions(options), ColumnFamilyOptions(options)));
|
||||
|
||||
Reopen(options);
|
||||
// Test the behavior
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Option Type Reference
|
||||
|
||||
Common option types used in serialization:
|
||||
|
||||
| OptionType | C++ Type | Example |
|
||||
|------------|----------|---------|
|
||||
| `kBoolean` | `bool` | `paranoid_memory_checks` |
|
||||
| `kInt` | `int` | `max_write_buffer_number` |
|
||||
| `kInt32T` | `int32_t` | `level0_file_num_compaction_trigger` |
|
||||
| `kUInt32T` | `uint32_t` | `memtable_protection_bytes_per_key` |
|
||||
| `kUInt64T` | `uint64_t` | `target_file_size_base` |
|
||||
| `kSizeT` | `size_t` | `block_size` |
|
||||
| `kDouble` | `double` | `compression_ratio` |
|
||||
| `kString` | `std::string` | `db_log_dir` |
|
||||
|
||||
---
|
||||
|
||||
## Checklist Summary
|
||||
|
||||
- [ ] Public header file with option definition and documentation
|
||||
- [ ] Internal options struct (MutableCFOptions or ImmutableCFOptions)
|
||||
- [ ] Options serialization/deserialization registration
|
||||
- [ ] Options logging in Dump() method
|
||||
- [ ] UpdateColumnFamilyOptions() for mutable options
|
||||
- [ ] options_settable_test.cc
|
||||
- [ ] db_stress_common.h (DECLARE)
|
||||
- [ ] db_stress_gflags.cc (DEFINE)
|
||||
- [ ] db_stress_test_base.cc (apply flag)
|
||||
- [ ] db_bench_tool.cc (DEFINE and apply)
|
||||
- [ ] db_crashtest.py (randomized values)
|
||||
- [ ] Unit tests
|
||||
- [ ] unreleased_history markdown file
|
||||
- [ ] Java API (for BlockBasedTableOptions)
|
||||
- [ ] C API (if needed)
|
||||
|
||||
@@ -0,0 +1,504 @@
|
||||
# RocksDB API Development Guide
|
||||
|
||||
This document provides guidance for adding new public APIs to RocksDB, following the established patterns used by existing APIs like `CompactRange`.
|
||||
|
||||
## API Layer Architecture
|
||||
|
||||
RocksDB exposes public APIs through multiple layers. Users can access RocksDB through any of the three public APIs: C++ headers, C headers, or Java bindings.
|
||||
|
||||
Here is an example for public header db.h:
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────────────────┐
|
||||
│ Level 1: Public APIs (User Entry Points) │
|
||||
├───────────────────────┬─────────────────────────┬───────────────────────────┤
|
||||
│ C++ Public API │ C API Bindings │ Java/JNI API │
|
||||
│ include/rocksdb/db.h │ include/rocksdb/c.h │ java/src/.../RocksDB.java │
|
||||
│ include/rocksdb/*.h │ │ java/src/.../*.java │
|
||||
└───────────────────────┴────────────┬────────────┴───────────────────────────┘
|
||||
↓
|
||||
┌─────────────────────────────────────────────────────────────────────────────┐
|
||||
│ Level 2: C++ Implementation (Internal Core) │
|
||||
│ db/db_impl/db_impl*.cc, db/c.cc, java/rocksjni/*.cc │
|
||||
└─────────────────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Step-by-Step Guide: Adding a New Public API
|
||||
|
||||
### Step 1: Define the C++ Public Interface
|
||||
|
||||
**File:** `include/rocksdb/db.h`
|
||||
|
||||
Add the virtual method declaration in the `DB` class:
|
||||
|
||||
\`\`\`cpp
|
||||
// Pure virtual - must be implemented by DBImpl
|
||||
virtual Status YourNewAPI(const YourAPIOptions& options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
/* other params */) = 0;
|
||||
|
||||
// Convenience overload for default column family
|
||||
virtual Status YourNewAPI(const YourAPIOptions& options,
|
||||
/* other params */) {
|
||||
return YourNewAPI(options, DefaultColumnFamily(), /* other params */);
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**Key Patterns:**
|
||||
- Use `Status` return type for error handling
|
||||
- Use `OptSlice` to avoid unnecessary levels of indirection and use of raw pointers.
|
||||
- Use `ColumnFamilyHandle*` for column family support
|
||||
- Provide convenience overloads for the default column family
|
||||
|
||||
### Step 2: Define Options Struct (If Needed)
|
||||
|
||||
**File:** `include/rocksdb/options.h`
|
||||
|
||||
If your API has multiple configuration options, define an options struct:
|
||||
|
||||
\`\`\`cpp
|
||||
struct YourAPIOptions {
|
||||
// Document each option with clear comments
|
||||
bool some_boolean_option = false;
|
||||
|
||||
// Default value explanation
|
||||
int some_int_option = -1;
|
||||
|
||||
// Pointer options require careful lifetime management
|
||||
std::atomic<bool>* canceled = nullptr;
|
||||
|
||||
// Enum options for multi-choice settings
|
||||
YourEnumType some_enum = YourEnumType::kDefault;
|
||||
};
|
||||
\`\`\`
|
||||
|
||||
**Key Patterns:**
|
||||
- Use sensible default values specified inline (e.g., `= false`, `= -1`)
|
||||
- Do NOT redundantly document the default value in comments; instead, document the rationale (why this default), historical context, and how different values are interpreted
|
||||
- Group related options logically
|
||||
- Consider thread-safety for pointer options
|
||||
|
||||
### Step 3: Implement in DBImpl
|
||||
|
||||
**Header:** `db/db_impl/db_impl.h`
|
||||
|
||||
\`\`\`cpp
|
||||
using DB::YourNewAPI;
|
||||
Status YourNewAPI(const YourAPIOptions& options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
/* other params */) override;
|
||||
|
||||
// Private internal implementation if needed
|
||||
Status YourNewAPIInternal(const YourAPIOptions& options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
/* other params */);
|
||||
\`\`\`
|
||||
|
||||
**Implementation:** `db/db_impl/db_impl_<category>.cc`
|
||||
|
||||
Choose the appropriate implementation file based on functionality:
|
||||
- `db_impl_compaction_flush.cc` - Compaction and flush operations
|
||||
- `db_impl_write.cc` - Write operations
|
||||
- `db_impl_open.cc` - DB opening/closing
|
||||
- `db_impl_files.cc` - File operations
|
||||
- `db_impl.cc` - General operations
|
||||
|
||||
\`\`\`cpp
|
||||
Status DBImpl::YourNewAPI(const YourAPIOptions& options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
/* other params */) {
|
||||
// 1. Input validation
|
||||
if (/* invalid input */) {
|
||||
return Status::InvalidArgument("Error message");
|
||||
}
|
||||
|
||||
// 2. Check for cancellation/abort conditions
|
||||
if (options.canceled && options.canceled->load(std::memory_order_acquire)) {
|
||||
return Status::Incomplete(Status::SubCode::kManualCompactionPaused);
|
||||
}
|
||||
|
||||
// 3. Get column family data
|
||||
auto cfh = static_cast<ColumnFamilyHandleImpl*>(column_family);
|
||||
auto cfd = cfh->cfd();
|
||||
|
||||
// 4. Core implementation logic
|
||||
// ...
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
### Step 4: Handle Special DB Types
|
||||
|
||||
**StackableDB (Wrapper DBs):**
|
||||
**File:** `include/rocksdb/utilities/stackable_db.h`
|
||||
|
||||
\`\`\`cpp
|
||||
using DB::YourNewAPI;
|
||||
Status YourNewAPI(const YourAPIOptions& options,
|
||||
ColumnFamilyHandle* column_family,
|
||||
/* other params */) override {
|
||||
return db_->YourNewAPI(options, column_family, /* other params */);
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**Secondary DB (Read-Only):**
|
||||
**File:** `db/db_impl/db_impl_secondary.h`
|
||||
|
||||
\`\`\`cpp
|
||||
using DBImpl::YourNewAPI;
|
||||
Status YourNewAPI(const YourAPIOptions& /*options*/,
|
||||
ColumnFamilyHandle* /*column_family*/,
|
||||
/* other params */) override {
|
||||
return Status::NotSupported("Not supported in secondary DB");
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**CompactedDB (Read-Only):**
|
||||
**File:** `db/db_impl/compacted_db_impl.h`
|
||||
|
||||
\`\`\`cpp
|
||||
using DBImpl::YourNewAPI;
|
||||
Status YourNewAPI(const YourAPIOptions& /*options*/,
|
||||
ColumnFamilyHandle* /*column_family*/,
|
||||
/* other params */) override {
|
||||
return Status::NotSupported("Not supported for read-only DB");
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
### Step 5: Add C API Bindings
|
||||
|
||||
**Header:** `include/rocksdb/c.h`
|
||||
|
||||
\`\`\`c
|
||||
// Basic version
|
||||
extern ROCKSDB_LIBRARY_API void rocksdb_your_new_api(
|
||||
rocksdb_t* db,
|
||||
const char* start_key, size_t start_key_len,
|
||||
const char* limit_key, size_t limit_key_len);
|
||||
|
||||
// Column family version
|
||||
extern ROCKSDB_LIBRARY_API void rocksdb_your_new_api_cf(
|
||||
rocksdb_t* db, rocksdb_column_family_handle_t* column_family,
|
||||
const char* start_key, size_t start_key_len,
|
||||
const char* limit_key, size_t limit_key_len);
|
||||
|
||||
// With options and error handling
|
||||
extern ROCKSDB_LIBRARY_API void rocksdb_your_new_api_opt(
|
||||
rocksdb_t* db, rocksdb_your_api_options_t* opt,
|
||||
const char* start_key, size_t start_key_len,
|
||||
const char* limit_key, size_t limit_key_len,
|
||||
char** errptr);
|
||||
\`\`\`
|
||||
|
||||
**Implementation:** `db/c.cc`
|
||||
|
||||
\`\`\`cpp
|
||||
void rocksdb_your_new_api(rocksdb_t* db, const char* start_key,
|
||||
size_t start_key_len, const char* limit_key,
|
||||
size_t limit_key_len) {
|
||||
Slice a, b;
|
||||
db->rep->YourNewAPI(
|
||||
YourAPIOptions(), // Default options
|
||||
(start_key ? (a = Slice(start_key, start_key_len), &a) : nullptr),
|
||||
(limit_key ? (b = Slice(limit_key, limit_key_len), &b) : nullptr));
|
||||
}
|
||||
|
||||
void rocksdb_your_new_api_cf(rocksdb_t* db,
|
||||
rocksdb_column_family_handle_t* column_family,
|
||||
const char* start_key, size_t start_key_len,
|
||||
const char* limit_key, size_t limit_key_len) {
|
||||
Slice a, b;
|
||||
db->rep->YourNewAPI(
|
||||
YourAPIOptions(),
|
||||
column_family->rep,
|
||||
(start_key ? (a = Slice(start_key, start_key_len), &a) : nullptr),
|
||||
(limit_key ? (b = Slice(limit_key, limit_key_len), &b) : nullptr));
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**If you have options, also add:**
|
||||
|
||||
\`\`\`cpp
|
||||
// Options struct wrapper
|
||||
struct rocksdb_your_api_options_t {
|
||||
YourAPIOptions rep;
|
||||
};
|
||||
|
||||
rocksdb_your_api_options_t* rocksdb_your_api_options_create() {
|
||||
return new rocksdb_your_api_options_t;
|
||||
}
|
||||
|
||||
void rocksdb_your_api_options_destroy(rocksdb_your_api_options_t* opt) {
|
||||
delete opt;
|
||||
}
|
||||
|
||||
void rocksdb_your_api_options_set_some_option(
|
||||
rocksdb_your_api_options_t* opt, unsigned char value) {
|
||||
opt->rep.some_boolean_option = value;
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
### Step 6: Add Java Bindings
|
||||
|
||||
**Java API:** `java/src/main/java/org/rocksdb/RocksDB.java`
|
||||
|
||||
\`\`\`java
|
||||
// Basic version
|
||||
public void yourNewAPI() throws RocksDBException {
|
||||
yourNewAPI(null);
|
||||
}
|
||||
|
||||
// Column family version
|
||||
public void yourNewAPI(ColumnFamilyHandle columnFamilyHandle)
|
||||
throws RocksDBException {
|
||||
yourNewAPI(nativeHandle_, null, -1, null, -1, 0,
|
||||
columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
|
||||
}
|
||||
|
||||
// Range version
|
||||
public void yourNewAPI(final byte[] begin, final byte[] end)
|
||||
throws RocksDBException {
|
||||
yourNewAPI(null, begin, end);
|
||||
}
|
||||
|
||||
// Full-featured version with options
|
||||
public void yourNewAPI(ColumnFamilyHandle columnFamilyHandle,
|
||||
final byte[] begin, final byte[] end,
|
||||
final YourAPIOptions options)
|
||||
throws RocksDBException {
|
||||
yourNewAPI(nativeHandle_,
|
||||
begin, begin == null ? -1 : begin.length,
|
||||
end, end == null ? -1 : end.length,
|
||||
options.nativeHandle_,
|
||||
columnFamilyHandle == null ? 0 : columnFamilyHandle.nativeHandle_);
|
||||
}
|
||||
|
||||
// Native method declaration
|
||||
private static native void yourNewAPI(final long handle,
|
||||
/* @Nullable */ final byte[] begin, final int beginLen,
|
||||
/* @Nullable */ final byte[] end, final int endLen,
|
||||
final long optionsHandle,
|
||||
final long cfHandle);
|
||||
\`\`\`
|
||||
|
||||
**Options Class:** `java/src/main/java/org/rocksdb/YourAPIOptions.java`
|
||||
|
||||
\`\`\`java
|
||||
public class YourAPIOptions extends RocksObject {
|
||||
|
||||
public YourAPIOptions() {
|
||||
super(newYourAPIOptions());
|
||||
}
|
||||
|
||||
// Builder pattern setters
|
||||
public YourAPIOptions setSomeBooleanOption(boolean value) {
|
||||
setSomeBooleanOption(nativeHandle_, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public boolean someBooleanOption() {
|
||||
return someBooleanOption(nativeHandle_);
|
||||
}
|
||||
|
||||
// Native method declarations
|
||||
private static native long newYourAPIOptions();
|
||||
private static native void disposeInternalJni(long handle);
|
||||
private static native void setSomeBooleanOption(long handle, boolean value);
|
||||
private static native boolean someBooleanOption(long handle);
|
||||
|
||||
@Override
|
||||
protected final void disposeInternal(final long handle) {
|
||||
disposeInternalJni(handle);
|
||||
}
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**JNI Implementation:** `java/rocksjni/rocksjni.cc`
|
||||
|
||||
\`\`\`cpp
|
||||
void Java_org_rocksdb_RocksDB_yourNewAPI(
|
||||
JNIEnv* env, jclass,
|
||||
jlong jdb_handle, jbyteArray jbegin, jint jbegin_len,
|
||||
jbyteArray jend, jint jend_len,
|
||||
jlong joptions_handle, jlong jcf_handle) {
|
||||
|
||||
// 1. Convert Java byte arrays to C++ strings
|
||||
jboolean has_exception = JNI_FALSE;
|
||||
std::string str_begin;
|
||||
if (jbegin_len > 0) {
|
||||
str_begin = ROCKSDB_NAMESPACE::JniUtil::byteString<std::string>(
|
||||
env, jbegin, jbegin_len,
|
||||
[](const char* str, const size_t len) { return std::string(str, len); },
|
||||
&has_exception);
|
||||
if (has_exception == JNI_TRUE) return;
|
||||
}
|
||||
|
||||
std::string str_end;
|
||||
if (jend_len > 0) {
|
||||
str_end = ROCKSDB_NAMESPACE::JniUtil::byteString<std::string>(
|
||||
env, jend, jend_len,
|
||||
[](const char* str, const size_t len) { return std::string(str, len); },
|
||||
&has_exception);
|
||||
if (has_exception == JNI_TRUE) return;
|
||||
}
|
||||
|
||||
// 2. Get or create options
|
||||
ROCKSDB_NAMESPACE::YourAPIOptions* options = nullptr;
|
||||
if (joptions_handle == 0) {
|
||||
options = new ROCKSDB_NAMESPACE::YourAPIOptions();
|
||||
} else {
|
||||
options = reinterpret_cast<ROCKSDB_NAMESPACE::YourAPIOptions*>(joptions_handle);
|
||||
}
|
||||
|
||||
// 3. Unwrap handles
|
||||
auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(jdb_handle);
|
||||
ROCKSDB_NAMESPACE::ColumnFamilyHandle* cf_handle =
|
||||
jcf_handle == 0 ? db->DefaultColumnFamily()
|
||||
: reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
|
||||
|
||||
// 4. Create Slices
|
||||
std::unique_ptr<ROCKSDB_NAMESPACE::Slice> begin;
|
||||
std::unique_ptr<ROCKSDB_NAMESPACE::Slice> end;
|
||||
if (jbegin_len > 0) begin.reset(new ROCKSDB_NAMESPACE::Slice(str_begin));
|
||||
if (jend_len > 0) end.reset(new ROCKSDB_NAMESPACE::Slice(str_end));
|
||||
|
||||
// 5. Call C++ API
|
||||
ROCKSDB_NAMESPACE::Status s = db->YourNewAPI(*options, cf_handle, begin.get(), end.get());
|
||||
|
||||
// 6. Cleanup if we created options
|
||||
if (joptions_handle == 0) delete options;
|
||||
|
||||
// 7. Throw Java exception on error
|
||||
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**Options JNI:** `java/rocksjni/your_api_options.cc`
|
||||
|
||||
\`\`\`cpp
|
||||
jlong Java_org_rocksdb_YourAPIOptions_newYourAPIOptions(JNIEnv*, jclass) {
|
||||
auto* options = new ROCKSDB_NAMESPACE::YourAPIOptions();
|
||||
return GET_CPLUSPLUS_POINTER(options);
|
||||
}
|
||||
|
||||
void Java_org_rocksdb_YourAPIOptions_disposeInternalJni(JNIEnv*, jclass, jlong jhandle) {
|
||||
auto* options = reinterpret_cast<ROCKSDB_NAMESPACE::YourAPIOptions*>(jhandle);
|
||||
delete options;
|
||||
}
|
||||
|
||||
void Java_org_rocksdb_YourAPIOptions_setSomeBooleanOption(
|
||||
JNIEnv*, jclass, jlong jhandle, jboolean value) {
|
||||
auto* options = reinterpret_cast<ROCKSDB_NAMESPACE::YourAPIOptions*>(jhandle);
|
||||
options->some_boolean_option = static_cast<bool>(value);
|
||||
}
|
||||
|
||||
jboolean Java_org_rocksdb_YourAPIOptions_someBooleanOption(JNIEnv*, jclass, jlong jhandle) {
|
||||
auto* options = reinterpret_cast<ROCKSDB_NAMESPACE::YourAPIOptions*>(jhandle);
|
||||
return static_cast<jboolean>(options->some_boolean_option);
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
### Step 7: Update Build Files
|
||||
|
||||
**Java CMakeLists.txt:** `java/CMakeLists.txt`
|
||||
|
||||
Add your new Java source files:
|
||||
\`\`\`cmake
|
||||
src/main/java/org/rocksdb/YourAPIOptions.java
|
||||
src/test/java/org/rocksdb/YourAPIOptionsTest.java
|
||||
\`\`\`
|
||||
|
||||
### Step 8: Add Release Notes
|
||||
|
||||
**Directory:** `unreleased_history/`
|
||||
|
||||
RocksDB uses individual files in the `unreleased_history/` directory rather than directly editing `HISTORY.md`. This avoids merge conflicts and ensures changes are attributed to the correct release version.
|
||||
|
||||
Add a file to the appropriate subdirectory:
|
||||
- `unreleased_history/new_features/` - For new functionality
|
||||
- `unreleased_history/public_api_changes/` - For API changes
|
||||
- `unreleased_history/behavior_changes/` - For behavior modifications
|
||||
- `unreleased_history/bug_fixes/` - For bug fixes
|
||||
|
||||
**Example:** `unreleased_history/new_features/your_new_api.md`
|
||||
|
||||
\`\`\`markdown
|
||||
Added `YourNewAPI()` to support [describe functionality]. See `YourAPIOptions` for configuration.
|
||||
\`\`\`
|
||||
|
||||
**Example:** `unreleased_history/public_api_changes/your_api_options.md`
|
||||
|
||||
**Note:** Files should contain one line of markdown. The "* " prefix is automatically added if not included. These files are compiled into `HISTORY.md` during the release process.
|
||||
|
||||
### Step 9: Add Tests
|
||||
|
||||
**C++ Unit Tests:** `db/db_your_api_test.cc` or add to existing test file
|
||||
|
||||
\`\`\`cpp
|
||||
TEST_F(DBTest, YourNewAPIBasic) {
|
||||
Options options = CurrentOptions();
|
||||
CreateAndReopenWithCF({"pikachu"}, options);
|
||||
|
||||
// Setup test data
|
||||
ASSERT_OK(Put(1, "key1", "value1"));
|
||||
ASSERT_OK(Put(1, "key2", "value2"));
|
||||
|
||||
// Test your API
|
||||
YourAPIOptions api_options;
|
||||
api_options.some_boolean_option = true;
|
||||
ASSERT_OK(db_->YourNewAPI(api_options, handles_[1], nullptr, nullptr));
|
||||
|
||||
// Verify results
|
||||
// ...
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
**Java Tests:** `java/src/test/java/org/rocksdb/YourAPIOptionsTest.java`
|
||||
|
||||
\`\`\`java
|
||||
public class YourAPIOptionsTest {
|
||||
@Test
|
||||
public void yourAPIOptions() {
|
||||
try (final YourAPIOptions options = new YourAPIOptions()) {
|
||||
assertFalse(options.someBooleanOption());
|
||||
options.setSomeBooleanOption(true);
|
||||
assertTrue(options.someBooleanOption());
|
||||
}
|
||||
}
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
## File Summary Checklist
|
||||
|
||||
|
||||
| Component | File(s) | Required |
|
||||
|-----------|---------|----------|
|
||||
| C++ Public Interface | `include/rocksdb/db.h` | ✓ |
|
||||
| Options Struct | `include/rocksdb/options.h` | If needed |
|
||||
| DBImpl Declaration | `db/db_impl/db_impl.h` | ✓ |
|
||||
| DBImpl Implementation | `db/db_impl/db_impl_*.cc` | ✓ |
|
||||
| StackableDB | `include/rocksdb/utilities/stackable_db.h` | ✓ |
|
||||
| Secondary DB | `db/db_impl/db_impl_secondary.h` | If not supported |
|
||||
| Compacted DB | `db/db_impl/compacted_db_impl.h` | If not supported |
|
||||
| C API Header | `include/rocksdb/c.h` | ✓ |
|
||||
| C API Implementation | `db/c.cc` | ✓ |
|
||||
| Java API | `java/src/main/java/org/rocksdb/RocksDB.java` | ✓ |
|
||||
| Java Options | `java/src/main/java/org/rocksdb/YourAPIOptions.java` | If needed |
|
||||
| JNI Implementation | `java/rocksjni/rocksjni.cc` | ✓ |
|
||||
| JNI Options | `java/rocksjni/your_api_options.cc` | If needed |
|
||||
| Java CMake | `java/CMakeLists.txt` | If new files |
|
||||
| Changelog | `unreleased_history/*.md` | ✓ |
|
||||
| C++ Tests | `db/db_*_test.cc` | ✓ |
|
||||
| Java Tests | `java/src/test/java/org/rocksdb/*Test.java` | ✓ |
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Error Handling**: Always return `Status` objects in C++, throw exceptions in Java
|
||||
2. **Default Values**: Provide sensible defaults for all options
|
||||
3. **Documentation**: Add clear comments for all public methods and options
|
||||
4. **Column Family Support**: Always support column family operations
|
||||
5. **Thread Safety**: Document thread-safety guarantees
|
||||
6. **Backward Compatibility**: Avoid breaking existing API contracts
|
||||
7. **Testing**: Add comprehensive unit tests for all code paths
|
||||
+5
-1
@@ -8,7 +8,7 @@ DB_STRESS_CMD?=./db_stress
|
||||
include common.mk
|
||||
|
||||
CRASHTEST_MAKE=$(MAKE) -f crash_test.mk
|
||||
CRASHTEST_PY=$(PYTHON) -u tools/db_crashtest.py --stress_cmd=$(DB_STRESS_CMD) --cleanup_cmd='$(DB_CLEANUP_CMD)'
|
||||
CRASHTEST_PY=$(PYTHON) -u tools/db_crashtest.py --stress_cmd=$(DB_STRESS_CMD) --cleanup_cmd='$(DB_CLEANUP_CMD)' --destroy_db_initially=1
|
||||
|
||||
.PHONY: crash_test crash_test_with_atomic_flush crash_test_with_txn \
|
||||
crash_test_with_wc_txn crash_test_with_wp_txn crash_test_with_wup_txn \
|
||||
@@ -34,6 +34,7 @@ CRASHTEST_PY=$(PYTHON) -u tools/db_crashtest.py --stress_cmd=$(DB_STRESS_CMD) --
|
||||
whitebox_crash_test_with_txn whitebox_crash_test_with_ts \
|
||||
whitebox_crash_test_with_optimistic_txn \
|
||||
whitebox_crash_test_with_tiered_storage \
|
||||
crash_test_db_cleanup \
|
||||
|
||||
crash_test: $(DB_STRESS_CMD)
|
||||
# Do not parallelize
|
||||
@@ -161,6 +162,9 @@ whitebox_crash_test_with_optimistic_txn: $(DB_STRESS_CMD)
|
||||
$(CRASHTEST_PY) --optimistic_txn whitebox --random_kill_odd \
|
||||
$(CRASH_TEST_KILL_ODD) $(CRASH_TEST_EXT_ARGS)
|
||||
|
||||
crash_test_db_cleanup: $(DB_STRESS_CMD)
|
||||
$(DB_STRESS_CMD) --delete_dir_and_exit=$(TEST_TMPDIR)
|
||||
|
||||
# Old names DEPRECATED
|
||||
crash_test_with_txn: crash_test_with_wc_txn
|
||||
whitebox_crash_test_with_txn: whitebox_crash_test_with_wc_txn
|
||||
|
||||
@@ -67,6 +67,16 @@ 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_)),
|
||||
blob_compressor_wa_(blob_compressor_
|
||||
? blob_compressor_->ObtainWorkingArea()
|
||||
: Compressor::ManagedWorkingArea{}),
|
||||
prepopulate_blob_cache_(mutable_cf_options->prepopulate_blob_cache),
|
||||
file_options_(file_options),
|
||||
write_options_(write_options),
|
||||
@@ -113,7 +123,7 @@ Status BlobFileBuilder::Add(const Slice& key, const Slice& value,
|
||||
}
|
||||
|
||||
Slice blob = value;
|
||||
std::string compressed_blob;
|
||||
GrowableBuffer compressed_blob;
|
||||
|
||||
{
|
||||
const Status s = CompressBlobIfNeeded(&blob, &compressed_blob);
|
||||
@@ -254,36 +264,27 @@ Status BlobFileBuilder::OpenBlobFileIfNeeded() {
|
||||
}
|
||||
|
||||
Status BlobFileBuilder::CompressBlobIfNeeded(
|
||||
Slice* blob, std::string* compressed_blob) const {
|
||||
Slice* blob, GrowableBuffer* compressed_blob) const {
|
||||
assert(blob);
|
||||
assert(compressed_blob);
|
||||
assert(compressed_blob->empty());
|
||||
assert(immutable_options_);
|
||||
|
||||
if (blob_compression_type_ == kNoCompression) {
|
||||
if (!blob_compressor_) {
|
||||
assert(blob_compression_type_ == kNoCompression);
|
||||
return Status::OK();
|
||||
}
|
||||
assert(blob_compression_type_ != kNoCompression);
|
||||
|
||||
// TODO: allow user CompressionOptions, including max_compressed_bytes_per_kb
|
||||
CompressionOptions opts;
|
||||
CompressionContext context(blob_compression_type_, opts);
|
||||
// WART: always stored as compressed even when that increases the size.
|
||||
|
||||
CompressionInfo info(opts, context, CompressionDict::GetEmptyDict(),
|
||||
blob_compression_type_);
|
||||
|
||||
constexpr uint32_t compression_format_version = 2;
|
||||
|
||||
bool success = false;
|
||||
|
||||
{
|
||||
StopWatch stop_watch(immutable_options_->clock, immutable_options_->stats,
|
||||
BLOB_DB_COMPRESSION_MICROS);
|
||||
success = OLD_CompressData(*blob, info, compression_format_version,
|
||||
compressed_blob);
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
return Status::Corruption("Error compressing blob");
|
||||
Status s;
|
||||
StopWatch stop_watch(immutable_options_->clock, immutable_options_->stats,
|
||||
BLOB_DB_COMPRESSION_MICROS);
|
||||
s = LegacyForceBuiltinCompression(*blob_compressor_, &blob_compressor_wa_,
|
||||
*blob, compressed_blob);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
|
||||
*blob = Slice(*compressed_blob);
|
||||
|
||||
@@ -10,12 +10,14 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "rocksdb/advanced_compression.h"
|
||||
#include "rocksdb/advanced_options.h"
|
||||
#include "rocksdb/compression_type.h"
|
||||
#include "rocksdb/env.h"
|
||||
#include "rocksdb/options.h"
|
||||
#include "rocksdb/rocksdb_namespace.h"
|
||||
#include "rocksdb/types.h"
|
||||
#include "util/aligned_buffer.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
@@ -76,7 +78,8 @@ class BlobFileBuilder {
|
||||
private:
|
||||
bool IsBlobFileOpen() const;
|
||||
Status OpenBlobFileIfNeeded();
|
||||
Status CompressBlobIfNeeded(Slice* blob, std::string* compressed_blob) const;
|
||||
Status CompressBlobIfNeeded(Slice* blob,
|
||||
GrowableBuffer* compressed_blob) const;
|
||||
Status WriteBlobToFile(const Slice& key, const Slice& blob,
|
||||
uint64_t* blob_file_number, uint64_t* blob_offset);
|
||||
Status CloseBlobFile();
|
||||
@@ -91,6 +94,8 @@ class BlobFileBuilder {
|
||||
uint64_t min_blob_size_;
|
||||
uint64_t blob_file_size_;
|
||||
CompressionType blob_compression_type_;
|
||||
std::unique_ptr<Compressor> blob_compressor_;
|
||||
mutable Compressor::ManagedWorkingArea blob_compressor_wa_;
|
||||
PrepopulateBlobCache prepopulate_blob_cache_;
|
||||
const FileOptions* file_options_;
|
||||
const WriteOptions* write_options_;
|
||||
|
||||
@@ -403,22 +403,19 @@ TEST_F(BlobFileBuilderTest, Compression) {
|
||||
ASSERT_EQ(blob_file_addition.GetBlobFileNumber(), blob_file_number);
|
||||
ASSERT_EQ(blob_file_addition.GetTotalBlobCount(), 1);
|
||||
|
||||
CompressionOptions opts;
|
||||
CompressionContext context(kSnappyCompression, opts);
|
||||
|
||||
CompressionInfo info(opts, context, CompressionDict::GetEmptyDict(),
|
||||
kSnappyCompression);
|
||||
|
||||
std::string compressed_value;
|
||||
ASSERT_TRUE(Snappy_Compress(info, uncompressed_value.data(),
|
||||
uncompressed_value.size(), &compressed_value));
|
||||
auto compressor =
|
||||
GetBuiltinV2CompressionManager()->GetCompressor({}, kSnappyCompression);
|
||||
GrowableBuffer compressed_value;
|
||||
ASSERT_OK(LegacyForceBuiltinCompression(*compressor, /*working_area=*/nullptr,
|
||||
uncompressed_value,
|
||||
&compressed_value));
|
||||
|
||||
ASSERT_EQ(blob_file_addition.GetTotalBlobBytes(),
|
||||
BlobLogRecord::kHeaderSize + key_size + compressed_value.size());
|
||||
|
||||
// Verify the contents of the new blob file as well as the blob reference
|
||||
std::vector<std::pair<std::string, std::string>> expected_key_value_pairs{
|
||||
{key, compressed_value}};
|
||||
{key, compressed_value.AsSlice().ToString()}};
|
||||
std::vector<std::string> blob_indexes{blob_index};
|
||||
|
||||
VerifyBlobFile(blob_file_number, blob_file_path, column_family_id,
|
||||
@@ -457,11 +454,12 @@ TEST_F(BlobFileBuilderTest, CompressionError) {
|
||||
nullptr /*IOTracer*/, nullptr /*BlobFileCompletionCallback*/,
|
||||
BlobFileCreationReason::kFlush, &blob_file_paths, &blob_file_additions);
|
||||
|
||||
SyncPoint::GetInstance()->SetCallBack("CompressData:TamperWithReturnValue",
|
||||
[](void* arg) {
|
||||
bool* ret = static_cast<bool*>(arg);
|
||||
*ret = false;
|
||||
});
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"LegacyForceBuiltinCompression:TamperWithStatus", [](void* arg) {
|
||||
Status* ret = static_cast<Status*>(arg);
|
||||
ASSERT_OK(*ret);
|
||||
*ret = Status::Corruption("Tampered result");
|
||||
});
|
||||
SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
constexpr char key[] = "1";
|
||||
@@ -469,7 +467,7 @@ TEST_F(BlobFileBuilderTest, CompressionError) {
|
||||
|
||||
std::string blob_index;
|
||||
|
||||
ASSERT_TRUE(builder.Add(key, value, &blob_index).IsCorruption());
|
||||
ASSERT_EQ(builder.Add(key, value, &blob_index).code(), Status::kCorruption);
|
||||
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
+37
-25
@@ -17,10 +17,10 @@
|
||||
#include "rocksdb/file_system.h"
|
||||
#include "rocksdb/slice.h"
|
||||
#include "rocksdb/status.h"
|
||||
#include "table/format.h"
|
||||
#include "table/multiget_context.h"
|
||||
#include "test_util/sync_point.h"
|
||||
#include "util/compression.h"
|
||||
#include "util/crc32c.h"
|
||||
#include "util/stop_watch.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
@@ -69,9 +69,16 @@ Status BlobFileReader::Create(
|
||||
}
|
||||
}
|
||||
|
||||
blob_file_reader->reset(
|
||||
new BlobFileReader(std::move(file_reader), file_size, compression_type,
|
||||
immutable_options.clock, statistics));
|
||||
std::shared_ptr<Decompressor> decompressor;
|
||||
if (compression_type != kNoCompression) {
|
||||
// The blob format has always used compression format 2
|
||||
decompressor = GetBuiltinV2CompressionManager()->GetDecompressorOptimizeFor(
|
||||
compression_type);
|
||||
}
|
||||
|
||||
blob_file_reader->reset(new BlobFileReader(
|
||||
std::move(file_reader), file_size, compression_type,
|
||||
std::move(decompressor), immutable_options.clock, statistics));
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
@@ -282,11 +289,13 @@ Status BlobFileReader::ReadFromFile(const RandomAccessFileReader* file_reader,
|
||||
|
||||
BlobFileReader::BlobFileReader(
|
||||
std::unique_ptr<RandomAccessFileReader>&& file_reader, uint64_t file_size,
|
||||
CompressionType compression_type, SystemClock* clock,
|
||||
CompressionType compression_type,
|
||||
std::shared_ptr<Decompressor> decompressor, SystemClock* clock,
|
||||
Statistics* statistics)
|
||||
: file_reader_(std::move(file_reader)),
|
||||
file_size_(file_size),
|
||||
compression_type_(compression_type),
|
||||
decompressor_(std::move(decompressor)),
|
||||
clock_(clock),
|
||||
statistics_(statistics) {
|
||||
assert(file_reader_);
|
||||
@@ -375,8 +384,9 @@ Status BlobFileReader::GetBlob(
|
||||
const Slice value_slice(record_slice.data() + adjustment, value_size);
|
||||
|
||||
{
|
||||
const Status s = UncompressBlobIfNeeded(
|
||||
value_slice, compression_type, allocator, clock_, statistics_, result);
|
||||
const Status s = UncompressBlobIfNeeded(value_slice, compression_type,
|
||||
decompressor_.get(), allocator,
|
||||
clock_, statistics_, result);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
@@ -524,9 +534,9 @@ void BlobFileReader::MultiGetBlob(
|
||||
|
||||
// Uncompress blob if needed
|
||||
Slice value_slice(record_slice.data() + adjustments[i], req->len);
|
||||
*req->status =
|
||||
UncompressBlobIfNeeded(value_slice, compression_type_, allocator,
|
||||
clock_, statistics_, &blob_reqs[i].second);
|
||||
*req->status = UncompressBlobIfNeeded(
|
||||
value_slice, compression_type_, decompressor_.get(), allocator, clock_,
|
||||
statistics_, &blob_reqs[i].second);
|
||||
if (req->status->ok()) {
|
||||
total_bytes += record_slice.size();
|
||||
}
|
||||
@@ -583,8 +593,8 @@ Status BlobFileReader::VerifyBlob(const Slice& record_slice,
|
||||
|
||||
Status BlobFileReader::UncompressBlobIfNeeded(
|
||||
const Slice& value_slice, CompressionType compression_type,
|
||||
MemoryAllocator* allocator, SystemClock* clock, Statistics* statistics,
|
||||
std::unique_ptr<BlobContents>* result) {
|
||||
Decompressor* decompressor, MemoryAllocator* allocator, SystemClock* clock,
|
||||
Statistics* statistics, std::unique_ptr<BlobContents>* result) {
|
||||
assert(result);
|
||||
|
||||
if (compression_type == kNoCompression) {
|
||||
@@ -593,31 +603,33 @@ Status BlobFileReader::UncompressBlobIfNeeded(
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
UncompressionContext context(compression_type);
|
||||
UncompressionInfo info(context, UncompressionDict::GetEmptyDict(),
|
||||
compression_type);
|
||||
assert(decompressor);
|
||||
|
||||
size_t uncompressed_size = 0;
|
||||
constexpr uint32_t compression_format_version = 2;
|
||||
Decompressor::Args args;
|
||||
args.compression_type = compression_type;
|
||||
args.compressed_data = value_slice;
|
||||
|
||||
CacheAllocationPtr output;
|
||||
Status s = decompressor->ExtractUncompressedSize(args);
|
||||
if (!s.ok()) {
|
||||
return Status::Corruption(s.ToString());
|
||||
}
|
||||
|
||||
CacheAllocationPtr output = AllocateBlock(args.uncompressed_size, allocator);
|
||||
|
||||
{
|
||||
PERF_TIMER_GUARD(blob_decompress_time);
|
||||
StopWatch stop_watch(clock, statistics, BLOB_DB_DECOMPRESSION_MICROS);
|
||||
output = OLD_UncompressData(info, value_slice.data(), value_slice.size(),
|
||||
&uncompressed_size, compression_format_version,
|
||||
allocator);
|
||||
s = decompressor->DecompressBlock(args, output.get());
|
||||
}
|
||||
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
"BlobFileReader::UncompressBlobIfNeeded:TamperWithResult", &output);
|
||||
"BlobFileReader::UncompressBlobIfNeeded:TamperWithResult", &s);
|
||||
|
||||
if (!output) {
|
||||
return Status::Corruption("Unable to uncompress blob");
|
||||
if (!s.ok()) {
|
||||
return Status::Corruption(s.ToString());
|
||||
}
|
||||
|
||||
result->reset(new BlobContents(std::move(output), uncompressed_size));
|
||||
result->reset(new BlobContents(std::move(output), args.uncompressed_size));
|
||||
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "db/blob/blob_read_request.h"
|
||||
#include "file/random_access_file_reader.h"
|
||||
#include "rocksdb/advanced_compression.h"
|
||||
#include "rocksdb/compression_type.h"
|
||||
#include "rocksdb/rocksdb_namespace.h"
|
||||
#include "util/autovector.h"
|
||||
@@ -64,7 +65,8 @@ class BlobFileReader {
|
||||
private:
|
||||
BlobFileReader(std::unique_ptr<RandomAccessFileReader>&& file_reader,
|
||||
uint64_t file_size, CompressionType compression_type,
|
||||
SystemClock* clock, Statistics* statistics);
|
||||
std::shared_ptr<Decompressor> decompressor, SystemClock* clock,
|
||||
Statistics* statistics);
|
||||
|
||||
static Status OpenFile(const ImmutableOptions& immutable_options,
|
||||
const FileOptions& file_opts,
|
||||
@@ -96,6 +98,7 @@ class BlobFileReader {
|
||||
|
||||
static Status UncompressBlobIfNeeded(const Slice& value_slice,
|
||||
CompressionType compression_type,
|
||||
Decompressor* decompressor,
|
||||
MemoryAllocator* allocator,
|
||||
SystemClock* clock,
|
||||
Statistics* statistics,
|
||||
@@ -104,6 +107,7 @@ class BlobFileReader {
|
||||
std::unique_ptr<RandomAccessFileReader> file_reader_;
|
||||
uint64_t file_size_;
|
||||
CompressionType compression_type_;
|
||||
std::shared_ptr<Decompressor> decompressor_;
|
||||
SystemClock* clock_;
|
||||
Statistics* statistics_;
|
||||
};
|
||||
|
||||
@@ -65,7 +65,7 @@ void WriteBlobFile(const ImmutableOptions& immutable_options,
|
||||
|
||||
ASSERT_OK(blob_log_writer.WriteHeader(WriteOptions(), header));
|
||||
|
||||
std::vector<std::string> compressed_blobs(num);
|
||||
std::vector<GrowableBuffer> compressed_blobs(num);
|
||||
std::vector<Slice> blobs_to_write(num);
|
||||
if (kNoCompression == compression) {
|
||||
for (size_t i = 0; i < num; ++i) {
|
||||
@@ -73,16 +73,13 @@ void WriteBlobFile(const ImmutableOptions& immutable_options,
|
||||
blob_sizes[i] = blobs[i].size();
|
||||
}
|
||||
} else {
|
||||
CompressionOptions opts;
|
||||
CompressionContext context(compression, opts);
|
||||
CompressionInfo info(opts, context, CompressionDict::GetEmptyDict(),
|
||||
compression);
|
||||
|
||||
constexpr uint32_t compression_format_version = 2;
|
||||
auto compressor =
|
||||
GetBuiltinV2CompressionManager()->GetCompressor({}, compression);
|
||||
|
||||
for (size_t i = 0; i < num; ++i) {
|
||||
ASSERT_TRUE(OLD_CompressData(blobs[i], info, compression_format_version,
|
||||
&compressed_blobs[i]));
|
||||
ASSERT_OK(LegacyForceBuiltinCompression(*compressor,
|
||||
/*working_area=*/nullptr,
|
||||
blobs[i], &compressed_blobs[i]));
|
||||
blobs_to_write[i] = compressed_blobs[i];
|
||||
blob_sizes[i] = compressed_blobs[i].size();
|
||||
}
|
||||
@@ -809,11 +806,10 @@ TEST_F(BlobFileReaderTest, UncompressionError) {
|
||||
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"BlobFileReader::UncompressBlobIfNeeded:TamperWithResult", [](void* arg) {
|
||||
CacheAllocationPtr* const output =
|
||||
static_cast<CacheAllocationPtr*>(arg);
|
||||
assert(output);
|
||||
auto* result = static_cast<Status*>(arg);
|
||||
assert(result);
|
||||
|
||||
output->reset();
|
||||
*result = Status::Corruption("Injected result");
|
||||
});
|
||||
|
||||
SyncPoint::GetInstance()->EnableProcessing();
|
||||
@@ -824,11 +820,12 @@ TEST_F(BlobFileReaderTest, UncompressionError) {
|
||||
std::unique_ptr<BlobContents> value;
|
||||
uint64_t bytes_read = 0;
|
||||
|
||||
ASSERT_TRUE(reader
|
||||
->GetBlob(ReadOptions(), key, blob_offset, blob_size,
|
||||
kSnappyCompression, prefetch_buffer, allocator,
|
||||
&value, &bytes_read)
|
||||
.IsCorruption());
|
||||
ASSERT_EQ(reader
|
||||
->GetBlob(ReadOptions(), key, blob_offset, blob_size,
|
||||
kSnappyCompression, prefetch_buffer, allocator,
|
||||
&value, &bytes_read)
|
||||
.code(),
|
||||
Status::Code::kCorruption);
|
||||
ASSERT_EQ(value, nullptr);
|
||||
ASSERT_EQ(bytes_read, 0);
|
||||
|
||||
|
||||
@@ -137,6 +137,18 @@ class BlobIndex {
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
// Encode this blob index into dst based on its type.
|
||||
void EncodeTo(std::string* dst) const {
|
||||
if (IsInlined()) {
|
||||
EncodeInlinedTTL(dst, expiration_, value_);
|
||||
} else if (HasTTL()) {
|
||||
EncodeBlobTTL(dst, expiration_, file_number_, offset_, size_,
|
||||
compression_);
|
||||
} else {
|
||||
EncodeBlob(dst, file_number_, offset_, size_, compression_);
|
||||
}
|
||||
}
|
||||
|
||||
static void EncodeInlinedTTL(std::string* dst, uint64_t expiration,
|
||||
const Slice& value) {
|
||||
assert(dst != nullptr);
|
||||
|
||||
@@ -67,7 +67,7 @@ void WriteBlobFile(const ImmutableOptions& immutable_options,
|
||||
|
||||
ASSERT_OK(blob_log_writer.WriteHeader(WriteOptions(), header));
|
||||
|
||||
std::vector<std::string> compressed_blobs(num);
|
||||
std::vector<GrowableBuffer> compressed_blobs(num);
|
||||
std::vector<Slice> blobs_to_write(num);
|
||||
if (kNoCompression == compression) {
|
||||
for (size_t i = 0; i < num; ++i) {
|
||||
@@ -75,16 +75,13 @@ void WriteBlobFile(const ImmutableOptions& immutable_options,
|
||||
blob_sizes[i] = blobs[i].size();
|
||||
}
|
||||
} else {
|
||||
CompressionOptions opts;
|
||||
CompressionContext context(compression, opts);
|
||||
CompressionInfo info(opts, context, CompressionDict::GetEmptyDict(),
|
||||
compression);
|
||||
|
||||
constexpr uint32_t compression_format_version = 2;
|
||||
auto compressor =
|
||||
GetBuiltinV2CompressionManager()->GetCompressor({}, compression);
|
||||
|
||||
for (size_t i = 0; i < num; ++i) {
|
||||
ASSERT_TRUE(OLD_CompressData(blobs[i], info, compression_format_version,
|
||||
&compressed_blobs[i]));
|
||||
ASSERT_OK(LegacyForceBuiltinCompression(*compressor,
|
||||
/*working_area=*/nullptr,
|
||||
blobs[i], &compressed_blobs[i]));
|
||||
blobs_to_write[i] = compressed_blobs[i];
|
||||
blob_sizes[i] = compressed_blobs[i].size();
|
||||
}
|
||||
|
||||
@@ -56,6 +56,18 @@ TableBuilder* NewTableBuilder(const TableBuilderOptions& tboptions,
|
||||
return tboptions.moptions.table_factory->NewTableBuilder(tboptions, file);
|
||||
}
|
||||
|
||||
void ExtractTimestampFromTableProperties(const TableProperties& tp,
|
||||
FileMetaData* meta) {
|
||||
auto min_ts_iter = tp.user_collected_properties.find("rocksdb.timestamp_min");
|
||||
if (min_ts_iter != tp.user_collected_properties.end()) {
|
||||
meta->min_timestamp = min_ts_iter->second;
|
||||
}
|
||||
auto max_ts_iter = tp.user_collected_properties.find("rocksdb.timestamp_max");
|
||||
if (max_ts_iter != tp.user_collected_properties.end()) {
|
||||
meta->max_timestamp = max_ts_iter->second;
|
||||
}
|
||||
}
|
||||
|
||||
Status BuildTable(
|
||||
const std::string& dbname, VersionSet* versions,
|
||||
const ImmutableDBOptions& db_options, const TableBuilderOptions& tboptions,
|
||||
@@ -355,6 +367,7 @@ Status BuildTable(
|
||||
assert(meta->fd.GetFileSize() > 0);
|
||||
tp = builder
|
||||
->GetTableProperties(); // refresh now that builder is finished
|
||||
ExtractTimestampFromTableProperties(tp, meta);
|
||||
if (memtable_payload_bytes != nullptr &&
|
||||
memtable_garbage_bytes != nullptr) {
|
||||
const CompactionIterationStats& ci_stats = c_iter.iter_stats();
|
||||
|
||||
@@ -41,6 +41,13 @@ class BlobFileCompletionCallback;
|
||||
TableBuilder* NewTableBuilder(const TableBuilderOptions& tboptions,
|
||||
WritableFileWriter* file);
|
||||
|
||||
// Extract min/max timestamps from table properties and populate FileMetaData.
|
||||
// This is used by both flush (BuildTable) and compaction (CompactionOutputs)
|
||||
// to populate timestamp range in FileMetaData from the TimestampTableProperties
|
||||
// collector output.
|
||||
void ExtractTimestampFromTableProperties(const TableProperties& tp,
|
||||
FileMetaData* meta);
|
||||
|
||||
// Build a Table file from the contents of *iter. The generated file
|
||||
// will be named according to number specified in meta. On success, the rest of
|
||||
// *meta will be filled with metadata about the generated table.
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
@@ -89,10 +90,12 @@ using ROCKSDB_NAMESPACE::EnvOptions;
|
||||
using ROCKSDB_NAMESPACE::EventListener;
|
||||
using ROCKSDB_NAMESPACE::ExportImportFilesMetaData;
|
||||
using ROCKSDB_NAMESPACE::ExternalFileIngestionInfo;
|
||||
using ROCKSDB_NAMESPACE::FileChecksumGenFactory;
|
||||
using ROCKSDB_NAMESPACE::FileLock;
|
||||
using ROCKSDB_NAMESPACE::FilterPolicy;
|
||||
using ROCKSDB_NAMESPACE::FlushJobInfo;
|
||||
using ROCKSDB_NAMESPACE::FlushOptions;
|
||||
using ROCKSDB_NAMESPACE::GetFileChecksumGenCrc32cFactory;
|
||||
using ROCKSDB_NAMESPACE::HistogramData;
|
||||
using ROCKSDB_NAMESPACE::HyperClockCacheOptions;
|
||||
using ROCKSDB_NAMESPACE::ImportColumnFamilyOptions;
|
||||
@@ -112,6 +115,7 @@ using ROCKSDB_NAMESPACE::NewCompactOnDeletionCollectorFactory;
|
||||
using ROCKSDB_NAMESPACE::NewGenericRateLimiter;
|
||||
using ROCKSDB_NAMESPACE::NewLRUCache;
|
||||
using ROCKSDB_NAMESPACE::NewRibbonFilterPolicy;
|
||||
using ROCKSDB_NAMESPACE::NewSstPartitionerFixedPrefixFactory;
|
||||
using ROCKSDB_NAMESPACE::OpenAndCompactOptions;
|
||||
using ROCKSDB_NAMESPACE::OptimisticTransactionDB;
|
||||
using ROCKSDB_NAMESPACE::OptimisticTransactionOptions;
|
||||
@@ -133,9 +137,11 @@ using ROCKSDB_NAMESPACE::Snapshot;
|
||||
using ROCKSDB_NAMESPACE::SstFileManager;
|
||||
using ROCKSDB_NAMESPACE::SstFileMetaData;
|
||||
using ROCKSDB_NAMESPACE::SstFileWriter;
|
||||
using ROCKSDB_NAMESPACE::SstPartitionerFactory;
|
||||
using ROCKSDB_NAMESPACE::Status;
|
||||
using ROCKSDB_NAMESPACE::StderrLogger;
|
||||
using ROCKSDB_NAMESPACE::SubcompactionJobInfo;
|
||||
using ROCKSDB_NAMESPACE::TableFactory;
|
||||
using ROCKSDB_NAMESPACE::TablePropertiesCollectorFactory;
|
||||
using ROCKSDB_NAMESPACE::Transaction;
|
||||
using ROCKSDB_NAMESPACE::TransactionDB;
|
||||
@@ -235,6 +241,15 @@ struct rocksdb_filelock_t {
|
||||
struct rocksdb_logger_t {
|
||||
std::shared_ptr<Logger> rep;
|
||||
};
|
||||
struct rocksdb_file_checksum_gen_factory_t {
|
||||
std::shared_ptr<FileChecksumGenFactory> rep;
|
||||
};
|
||||
struct rocksdb_sst_partitioner_factory_t {
|
||||
std::shared_ptr<SstPartitionerFactory> rep;
|
||||
};
|
||||
struct rocksdb_table_properties_collector_factory_t {
|
||||
std::shared_ptr<TablePropertiesCollectorFactory> rep;
|
||||
};
|
||||
struct rocksdb_lru_cache_options_t {
|
||||
LRUCacheOptions rep;
|
||||
};
|
||||
@@ -581,7 +596,6 @@ struct rocksdb_slicetransform_t : public SliceTransform {
|
||||
char* (*transform_)(void*, const char* key, size_t length,
|
||||
size_t* dst_length);
|
||||
unsigned char (*in_domain_)(void*, const char* key, size_t length);
|
||||
unsigned char (*in_range_)(void*, const char* key, size_t length);
|
||||
|
||||
~rocksdb_slicetransform_t() override { (*destructor_)(state_); }
|
||||
|
||||
@@ -596,10 +610,6 @@ struct rocksdb_slicetransform_t : public SliceTransform {
|
||||
bool InDomain(const Slice& src) const override {
|
||||
return (*in_domain_)(state_, src.data(), src.size());
|
||||
}
|
||||
|
||||
bool InRange(const Slice& src) const override {
|
||||
return (*in_range_)(state_, src.data(), src.size());
|
||||
}
|
||||
};
|
||||
|
||||
struct rocksdb_universal_compaction_options_t {
|
||||
@@ -920,6 +930,38 @@ rocksdb_compaction_service_options_override_create() {
|
||||
return new rocksdb_compaction_service_options_override_t;
|
||||
}
|
||||
|
||||
rocksdb_compaction_service_options_override_t*
|
||||
rocksdb_compaction_service_options_override_create_from_options(
|
||||
rocksdb_options_t* options) {
|
||||
if (!options) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
rocksdb_compaction_service_options_override_t* override_opts =
|
||||
new rocksdb_compaction_service_options_override_t;
|
||||
|
||||
// Copy all relevant options from rocksdb_options_t
|
||||
override_opts->rep.env = options->rep.env;
|
||||
override_opts->rep.file_checksum_gen_factory =
|
||||
options->rep.file_checksum_gen_factory;
|
||||
override_opts->rep.comparator = options->rep.comparator;
|
||||
override_opts->rep.merge_operator = options->rep.merge_operator;
|
||||
override_opts->rep.compaction_filter = options->rep.compaction_filter;
|
||||
override_opts->rep.compaction_filter_factory =
|
||||
options->rep.compaction_filter_factory;
|
||||
override_opts->rep.prefix_extractor = options->rep.prefix_extractor;
|
||||
override_opts->rep.table_factory = options->rep.table_factory;
|
||||
override_opts->rep.sst_partitioner_factory =
|
||||
options->rep.sst_partitioner_factory;
|
||||
override_opts->rep.listeners = options->rep.listeners;
|
||||
override_opts->rep.statistics = options->rep.statistics;
|
||||
override_opts->rep.info_log = options->rep.info_log;
|
||||
override_opts->rep.table_properties_collector_factories =
|
||||
options->rep.table_properties_collector_factories;
|
||||
|
||||
return override_opts;
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_destroy(
|
||||
rocksdb_compaction_service_options_override_t* override_options) {
|
||||
if (override_options) {
|
||||
@@ -943,6 +985,111 @@ void rocksdb_compaction_service_options_override_set_comparator(
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_merge_operator(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_mergeoperator_t* merge_operator) {
|
||||
if (override_options && merge_operator) {
|
||||
override_options->rep.merge_operator =
|
||||
std::shared_ptr<MergeOperator>(merge_operator);
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_compaction_filter(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_compactionfilter_t* compaction_filter) {
|
||||
if (override_options && compaction_filter) {
|
||||
override_options->rep.compaction_filter = compaction_filter;
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_compaction_filter_factory(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_compactionfilterfactory_t* compaction_filter_factory) {
|
||||
if (override_options && compaction_filter_factory) {
|
||||
override_options->rep.compaction_filter_factory =
|
||||
std::shared_ptr<CompactionFilterFactory>(compaction_filter_factory);
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_prefix_extractor(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_slicetransform_t* prefix_extractor) {
|
||||
if (override_options && prefix_extractor) {
|
||||
override_options->rep.prefix_extractor =
|
||||
std::shared_ptr<const SliceTransform>(prefix_extractor);
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_block_based_table_factory(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_block_based_table_options_t* table_options) {
|
||||
if (override_options && table_options) {
|
||||
override_options->rep.table_factory = std::shared_ptr<TableFactory>(
|
||||
NewBlockBasedTableFactory(table_options->rep));
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_cuckoo_table_factory(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_cuckoo_table_options_t* table_options) {
|
||||
if (override_options && table_options) {
|
||||
override_options->rep.table_factory = std::shared_ptr<TableFactory>(
|
||||
NewCuckooTableFactory(table_options->rep));
|
||||
}
|
||||
}
|
||||
|
||||
// Note: add_event_listener is defined later after rocksdb_eventlistener_t
|
||||
// struct
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_statistics(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_options_t* options) {
|
||||
if (override_options && options) {
|
||||
override_options->rep.statistics = options->rep.statistics;
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_info_log(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_logger_t* logger) {
|
||||
if (override_options && logger) {
|
||||
override_options->rep.info_log = logger->rep;
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_option(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
const char* key, const char* value) {
|
||||
if (override_options && key && value) {
|
||||
override_options->rep.options_map[std::string(key)] = std::string(value);
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_file_checksum_gen_factory(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_file_checksum_gen_factory_t* factory) {
|
||||
if (override_options && factory) {
|
||||
override_options->rep.file_checksum_gen_factory = factory->rep;
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_set_sst_partitioner_factory(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_sst_partitioner_factory_t* factory) {
|
||||
if (override_options && factory) {
|
||||
override_options->rep.sst_partitioner_factory = factory->rep;
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_add_table_properties_collector_factory(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_table_properties_collector_factory_t* factory) {
|
||||
if (override_options && factory) {
|
||||
override_options->rep.table_properties_collector_factories.push_back(
|
||||
factory->rep);
|
||||
}
|
||||
}
|
||||
|
||||
// Atomic bool management for cancellation
|
||||
unsigned char* rocksdb_open_and_compact_canceled_create() {
|
||||
return reinterpret_cast<unsigned char*>(new std::atomic<bool>(false));
|
||||
@@ -1071,12 +1218,12 @@ char* rocksdb_open_and_compact_with_options(
|
||||
|
||||
rocksdb_t* rocksdb_open(const rocksdb_options_t* options, const char* name,
|
||||
char** errptr) {
|
||||
DB* db;
|
||||
if (SaveError(errptr, DB::Open(options->rep, std::string(name), &db))) {
|
||||
std::unique_ptr<DB> dbptr;
|
||||
if (SaveError(errptr, DB::Open(options->rep, std::string(name), &dbptr))) {
|
||||
return nullptr;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1096,13 +1243,14 @@ rocksdb_t* rocksdb_open_for_read_only(const rocksdb_options_t* options,
|
||||
const char* name,
|
||||
unsigned char error_if_wal_file_exists,
|
||||
char** errptr) {
|
||||
DB* db;
|
||||
if (SaveError(errptr, DB::OpenForReadOnly(options->rep, std::string(name),
|
||||
&db, error_if_wal_file_exists))) {
|
||||
std::unique_ptr<DB> dbptr;
|
||||
if (SaveError(errptr,
|
||||
DB::OpenForReadOnly(options->rep, std::string(name), &dbptr,
|
||||
error_if_wal_file_exists))) {
|
||||
return nullptr;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1110,14 +1258,14 @@ rocksdb_t* rocksdb_open_as_secondary(const rocksdb_options_t* options,
|
||||
const char* name,
|
||||
const char* secondary_path,
|
||||
char** errptr) {
|
||||
DB* db;
|
||||
std::unique_ptr<DB> dbptr;
|
||||
if (SaveError(errptr,
|
||||
DB::OpenAsSecondary(options->rep, std::string(name),
|
||||
std::string(secondary_path), &db))) {
|
||||
std::string(secondary_path), &dbptr))) {
|
||||
return nullptr;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1431,11 +1579,11 @@ rocksdb_t* rocksdb_open_and_trim_history(
|
||||
|
||||
std::string trim_ts_(trim_ts, trim_tslen);
|
||||
|
||||
DB* db;
|
||||
std::unique_ptr<DB> dbptr;
|
||||
std::vector<ColumnFamilyHandle*> handles;
|
||||
if (SaveError(errptr, DB::OpenAndTrimHistory(
|
||||
DBOptions(db_options->rep), std::string(name),
|
||||
column_families, &handles, &db, trim_ts_))) {
|
||||
column_families, &handles, &dbptr, trim_ts_))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1447,7 +1595,7 @@ rocksdb_t* rocksdb_open_and_trim_history(
|
||||
column_family_handles[i] = c_handle;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1463,10 +1611,10 @@ rocksdb_t* rocksdb_open_column_families(
|
||||
ColumnFamilyOptions(column_family_options[i]->rep));
|
||||
}
|
||||
|
||||
DB* db;
|
||||
std::unique_ptr<DB> dbptr;
|
||||
std::vector<ColumnFamilyHandle*> handles;
|
||||
if (SaveError(errptr, DB::Open(DBOptions(db_options->rep), std::string(name),
|
||||
column_families, &handles, &db))) {
|
||||
column_families, &handles, &dbptr))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1478,7 +1626,7 @@ rocksdb_t* rocksdb_open_column_families(
|
||||
column_family_handles[i] = c_handle;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1531,12 +1679,12 @@ rocksdb_t* rocksdb_open_for_read_only_column_families(
|
||||
ColumnFamilyOptions(column_family_options[i]->rep));
|
||||
}
|
||||
|
||||
DB* db;
|
||||
std::unique_ptr<DB> dbptr;
|
||||
std::vector<ColumnFamilyHandle*> handles;
|
||||
if (SaveError(errptr,
|
||||
DB::OpenForReadOnly(DBOptions(db_options->rep),
|
||||
std::string(name), column_families,
|
||||
&handles, &db, error_if_wal_file_exists))) {
|
||||
if (SaveError(errptr, DB::OpenForReadOnly(DBOptions(db_options->rep),
|
||||
std::string(name), column_families,
|
||||
&handles, &dbptr,
|
||||
error_if_wal_file_exists))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1548,7 +1696,7 @@ rocksdb_t* rocksdb_open_for_read_only_column_families(
|
||||
column_family_handles[i] = c_handle;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1564,12 +1712,12 @@ rocksdb_t* rocksdb_open_as_secondary_column_families(
|
||||
std::string(column_family_names[i]),
|
||||
ColumnFamilyOptions(column_family_options[i]->rep));
|
||||
}
|
||||
DB* db;
|
||||
std::unique_ptr<DB> dbptr;
|
||||
std::vector<ColumnFamilyHandle*> handles;
|
||||
if (SaveError(errptr, DB::OpenAsSecondary(DBOptions(db_options->rep),
|
||||
std::string(name),
|
||||
std::string(secondary_path),
|
||||
column_families, &handles, &db))) {
|
||||
if (SaveError(errptr, DB::OpenAsSecondary(
|
||||
DBOptions(db_options->rep), std::string(name),
|
||||
std::string(secondary_path), column_families,
|
||||
&handles, &dbptr))) {
|
||||
return nullptr;
|
||||
}
|
||||
for (size_t i = 0; i != handles.size(); ++i) {
|
||||
@@ -1580,7 +1728,7 @@ rocksdb_t* rocksdb_open_as_secondary_column_families(
|
||||
column_family_handles[i] = c_handle;
|
||||
}
|
||||
rocksdb_t* result = new rocksdb_t;
|
||||
result->rep = db;
|
||||
result->rep = dbptr.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -2891,12 +3039,19 @@ class H : public WriteBatch::Handler {
|
||||
void* state_;
|
||||
void (*put_)(void*, const char* k, size_t klen, const char* v, size_t vlen);
|
||||
void (*deleted_)(void*, const char* k, size_t klen);
|
||||
void (*log_data_)(void*, const char* blob, size_t blob_len);
|
||||
|
||||
void Put(const Slice& key, const Slice& value) override {
|
||||
(*put_)(state_, key.data(), key.size(), value.data(), value.size());
|
||||
}
|
||||
void Delete(const Slice& key) override {
|
||||
(*deleted_)(state_, key.data(), key.size());
|
||||
}
|
||||
void LogData(const Slice& blob) override {
|
||||
if (log_data_) {
|
||||
(*log_data_)(state_, blob.data(), blob.size());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class HCF : public WriteBatch::Handler {
|
||||
@@ -2907,6 +3062,8 @@ class HCF : public WriteBatch::Handler {
|
||||
void (*deleted_cf_)(void*, uint32_t cfid, const char* k, size_t klen);
|
||||
void (*merge_cf_)(void*, uint32_t cfid, const char* k, size_t klen,
|
||||
const char* v, size_t vlen);
|
||||
void (*log_data_)(void*, const char* blob, size_t blob_len);
|
||||
|
||||
Status PutCF(uint32_t column_family_id, const Slice& key,
|
||||
const Slice& value) override {
|
||||
(*put_cf_)(state_, column_family_id, key.data(), key.size(), value.data(),
|
||||
@@ -2923,6 +3080,11 @@ class HCF : public WriteBatch::Handler {
|
||||
value.size());
|
||||
return Status::OK();
|
||||
}
|
||||
void LogData(const Slice& blob) override {
|
||||
if (log_data_) {
|
||||
(*log_data_)(state_, blob.data(), blob.size());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void rocksdb_writebatch_iterate(rocksdb_writebatch_t* b, void* state,
|
||||
@@ -2934,6 +3096,20 @@ void rocksdb_writebatch_iterate(rocksdb_writebatch_t* b, void* state,
|
||||
handler.state_ = state;
|
||||
handler.put_ = put;
|
||||
handler.deleted_ = deleted;
|
||||
handler.log_data_ = nullptr;
|
||||
b->rep.Iterate(&handler);
|
||||
}
|
||||
|
||||
void rocksdb_writebatch_iterate_ld(
|
||||
rocksdb_writebatch_t* b, void* state,
|
||||
void (*put)(void*, const char* k, size_t klen, const char* v, size_t vlen),
|
||||
void (*deleted)(void*, const char* k, size_t klen),
|
||||
void (*log_data)(void*, const char* blob, size_t blob_len)) {
|
||||
H handler;
|
||||
handler.state_ = state;
|
||||
handler.put_ = put;
|
||||
handler.deleted_ = deleted;
|
||||
handler.log_data_ = log_data;
|
||||
b->rep.Iterate(&handler);
|
||||
}
|
||||
|
||||
@@ -2949,6 +3125,24 @@ void rocksdb_writebatch_iterate_cf(
|
||||
handler.put_cf_ = put_cf;
|
||||
handler.deleted_cf_ = deleted_cf;
|
||||
handler.merge_cf_ = merge_cf;
|
||||
handler.log_data_ = nullptr;
|
||||
b->rep.Iterate(&handler);
|
||||
}
|
||||
|
||||
void rocksdb_writebatch_iterate_cf_ld(
|
||||
rocksdb_writebatch_t* b, void* state,
|
||||
void (*put_cf)(void*, uint32_t cfid, const char* k, size_t klen,
|
||||
const char* v, size_t vlen),
|
||||
void (*deleted_cf)(void*, uint32_t cfid, const char* k, size_t klen),
|
||||
void (*merge_cf)(void*, uint32_t cfid, const char* k, size_t klen,
|
||||
const char* v, size_t vlen),
|
||||
void (*log_data)(void*, const char* blob, size_t blob_len)) {
|
||||
HCF handler;
|
||||
handler.state_ = state;
|
||||
handler.put_cf_ = put_cf;
|
||||
handler.deleted_cf_ = deleted_cf;
|
||||
handler.merge_cf_ = merge_cf;
|
||||
handler.log_data_ = log_data;
|
||||
b->rep.Iterate(&handler);
|
||||
}
|
||||
|
||||
@@ -3549,6 +3743,11 @@ void rocksdb_block_based_options_set_format_version(
|
||||
options->rep.format_version = v;
|
||||
}
|
||||
|
||||
void rocksdb_block_based_options_set_separate_key_value_in_data_block(
|
||||
rocksdb_block_based_table_options_t* options, unsigned char v) {
|
||||
options->rep.separate_key_value_in_data_block = v;
|
||||
}
|
||||
|
||||
void rocksdb_block_based_options_set_index_type(
|
||||
rocksdb_block_based_table_options_t* options, int v) {
|
||||
options->rep.index_type = static_cast<BlockBasedTableOptions::IndexType>(v);
|
||||
@@ -3560,6 +3759,12 @@ void rocksdb_block_based_options_set_data_block_index_type(
|
||||
static_cast<BlockBasedTableOptions::DataBlockIndexType>(v);
|
||||
}
|
||||
|
||||
void rocksdb_block_based_options_set_index_block_search_type(
|
||||
rocksdb_block_based_table_options_t* options, int v) {
|
||||
options->rep.index_block_search_type =
|
||||
static_cast<BlockBasedTableOptions::BlockSearchType>(v);
|
||||
}
|
||||
|
||||
void rocksdb_block_based_options_set_data_block_hash_ratio(
|
||||
rocksdb_block_based_table_options_t* options, double v) {
|
||||
options->rep.data_block_hash_table_util_ratio = v;
|
||||
@@ -3612,6 +3817,11 @@ void rocksdb_block_based_options_set_unpartitioned_pinning_tier(
|
||||
static_cast<ROCKSDB_NAMESPACE::PinningTier>(v);
|
||||
}
|
||||
|
||||
void rocksdb_block_based_options_set_block_align(
|
||||
rocksdb_block_based_table_options_t* options, unsigned char v) {
|
||||
options->rep.block_align = v;
|
||||
}
|
||||
|
||||
/* FlushJobInfo */
|
||||
|
||||
const char* rocksdb_flushjobinfo_cf_name(const rocksdb_flushjobinfo_t* info,
|
||||
@@ -3967,6 +4177,15 @@ void rocksdb_options_add_eventlistener(rocksdb_options_t* opt,
|
||||
opt->rep.listeners.emplace_back(std::shared_ptr<EventListener>(t));
|
||||
}
|
||||
|
||||
void rocksdb_compaction_service_options_override_add_event_listener(
|
||||
rocksdb_compaction_service_options_override_t* override_options,
|
||||
rocksdb_eventlistener_t* event_listener) {
|
||||
if (override_options && event_listener) {
|
||||
override_options->rep.listeners.emplace_back(
|
||||
std::shared_ptr<EventListener>(event_listener));
|
||||
}
|
||||
}
|
||||
|
||||
rocksdb_cuckoo_table_options_t* rocksdb_cuckoo_options_create() {
|
||||
return new rocksdb_cuckoo_table_options_t;
|
||||
}
|
||||
@@ -4178,6 +4397,65 @@ rocksdb_logger_t* rocksdb_logger_create_callback_logger(
|
||||
|
||||
void rocksdb_logger_destroy(rocksdb_logger_t* logger) { delete logger; }
|
||||
|
||||
/* File Checksum Gen Factory */
|
||||
|
||||
rocksdb_file_checksum_gen_factory_t*
|
||||
rocksdb_file_checksum_gen_crc32c_factory_create() {
|
||||
rocksdb_file_checksum_gen_factory_t* factory =
|
||||
new rocksdb_file_checksum_gen_factory_t;
|
||||
factory->rep = GetFileChecksumGenCrc32cFactory();
|
||||
return factory;
|
||||
}
|
||||
|
||||
void rocksdb_file_checksum_gen_factory_destroy(
|
||||
rocksdb_file_checksum_gen_factory_t* factory) {
|
||||
delete factory;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_file_checksum_gen_factory(
|
||||
rocksdb_options_t* opt, rocksdb_file_checksum_gen_factory_t* factory) {
|
||||
if (opt && factory) {
|
||||
opt->rep.file_checksum_gen_factory = factory->rep;
|
||||
}
|
||||
}
|
||||
|
||||
/* SST Partitioner Factory */
|
||||
|
||||
rocksdb_sst_partitioner_factory_t*
|
||||
rocksdb_sst_partitioner_fixed_prefix_factory_create(size_t prefix_len) {
|
||||
rocksdb_sst_partitioner_factory_t* factory =
|
||||
new rocksdb_sst_partitioner_factory_t;
|
||||
factory->rep = NewSstPartitionerFixedPrefixFactory(prefix_len);
|
||||
return factory;
|
||||
}
|
||||
|
||||
void rocksdb_sst_partitioner_factory_destroy(
|
||||
rocksdb_sst_partitioner_factory_t* factory) {
|
||||
delete factory;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_sst_partitioner_factory(
|
||||
rocksdb_options_t* opt, rocksdb_sst_partitioner_factory_t* factory) {
|
||||
if (opt && factory) {
|
||||
opt->rep.sst_partitioner_factory = factory->rep;
|
||||
}
|
||||
}
|
||||
|
||||
/* Table Properties Collector Factory */
|
||||
|
||||
void rocksdb_table_properties_collector_factory_destroy(
|
||||
rocksdb_table_properties_collector_factory_t* factory) {
|
||||
delete factory;
|
||||
}
|
||||
|
||||
void rocksdb_options_add_table_properties_collector_factory(
|
||||
rocksdb_options_t* opt,
|
||||
rocksdb_table_properties_collector_factory_t* factory) {
|
||||
if (opt && factory) {
|
||||
opt->rep.table_properties_collector_factories.push_back(factory->rep);
|
||||
}
|
||||
}
|
||||
|
||||
void rocksdb_options_set_env(rocksdb_options_t* opt, rocksdb_env_t* env) {
|
||||
opt->rep.env = (env ? env->rep : nullptr);
|
||||
}
|
||||
@@ -4394,16 +4672,6 @@ unsigned char rocksdb_options_get_skip_stats_update_on_db_open(
|
||||
return opt->rep.skip_stats_update_on_db_open;
|
||||
}
|
||||
|
||||
void rocksdb_options_set_skip_checking_sst_file_sizes_on_db_open(
|
||||
rocksdb_options_t* opt, unsigned char val) {
|
||||
opt->rep.skip_checking_sst_file_sizes_on_db_open = val;
|
||||
}
|
||||
|
||||
unsigned char rocksdb_options_get_skip_checking_sst_file_sizes_on_db_open(
|
||||
rocksdb_options_t* opt) {
|
||||
return opt->rep.skip_checking_sst_file_sizes_on_db_open;
|
||||
}
|
||||
|
||||
/* Blob Options Settings */
|
||||
void rocksdb_options_set_enable_blob_files(rocksdb_options_t* opt,
|
||||
unsigned char val) {
|
||||
@@ -5819,11 +6087,6 @@ unsigned char rocksdb_readoptions_get_tailing(rocksdb_readoptions_t* opt) {
|
||||
return opt->rep.tailing;
|
||||
}
|
||||
|
||||
void rocksdb_readoptions_set_managed(rocksdb_readoptions_t* opt,
|
||||
unsigned char v) {
|
||||
opt->rep.managed = v;
|
||||
}
|
||||
|
||||
void rocksdb_readoptions_set_readahead_size(rocksdb_readoptions_t* opt,
|
||||
size_t v) {
|
||||
opt->rep.readahead_size = v;
|
||||
@@ -6624,14 +6887,12 @@ rocksdb_slicetransform_t* rocksdb_slicetransform_create(
|
||||
char* (*transform)(void*, const char* key, size_t length,
|
||||
size_t* dst_length),
|
||||
unsigned char (*in_domain)(void*, const char* key, size_t length),
|
||||
unsigned char (*in_range)(void*, const char* key, size_t length),
|
||||
const char* (*name)(void*)) {
|
||||
rocksdb_slicetransform_t* result = new rocksdb_slicetransform_t;
|
||||
result->state_ = state;
|
||||
result->destructor_ = destructor;
|
||||
result->transform_ = transform;
|
||||
result->in_domain_ = in_domain;
|
||||
result->in_range_ = in_range;
|
||||
result->name_ = name;
|
||||
return result;
|
||||
}
|
||||
@@ -6647,7 +6908,6 @@ struct SliceTransformWrapper : public rocksdb_slicetransform_t {
|
||||
return rep_->Transform(src);
|
||||
}
|
||||
bool InDomain(const Slice& src) const override { return rep_->InDomain(src); }
|
||||
bool InRange(const Slice& src) const override { return rep_->InRange(src); }
|
||||
static void DoNothing(void*) {}
|
||||
};
|
||||
|
||||
@@ -6771,6 +7031,27 @@ uint64_t rocksdb_fifo_compaction_options_get_max_table_files_size(
|
||||
return fifo_opts->rep.max_table_files_size;
|
||||
}
|
||||
|
||||
void rocksdb_fifo_compaction_options_set_max_data_files_size(
|
||||
rocksdb_fifo_compaction_options_t* fifo_opts, uint64_t size) {
|
||||
fifo_opts->rep.max_data_files_size = size;
|
||||
}
|
||||
|
||||
uint64_t rocksdb_fifo_compaction_options_get_max_data_files_size(
|
||||
rocksdb_fifo_compaction_options_t* fifo_opts) {
|
||||
return fifo_opts->rep.max_data_files_size;
|
||||
}
|
||||
|
||||
void rocksdb_fifo_compaction_options_set_use_kv_ratio_compaction(
|
||||
rocksdb_fifo_compaction_options_t* fifo_opts,
|
||||
unsigned char use_kv_ratio_compaction) {
|
||||
fifo_opts->rep.use_kv_ratio_compaction = use_kv_ratio_compaction;
|
||||
}
|
||||
|
||||
unsigned char rocksdb_fifo_compaction_options_get_use_kv_ratio_compaction(
|
||||
rocksdb_fifo_compaction_options_t* fifo_opts) {
|
||||
return fifo_opts->rep.use_kv_ratio_compaction;
|
||||
}
|
||||
|
||||
void rocksdb_fifo_compaction_options_destroy(
|
||||
rocksdb_fifo_compaction_options_t* fifo_opts) {
|
||||
delete fifo_opts;
|
||||
@@ -8346,6 +8627,14 @@ void rocksdb_enable_manual_compaction(rocksdb_t* db) {
|
||||
db->rep->EnableManualCompaction();
|
||||
}
|
||||
|
||||
void rocksdb_abort_all_compactions(rocksdb_t* db) {
|
||||
db->rep->AbortAllCompactions();
|
||||
}
|
||||
|
||||
void rocksdb_resume_all_compactions(rocksdb_t* db) {
|
||||
db->rep->ResumeAllCompactions();
|
||||
}
|
||||
|
||||
rocksdb_statistics_histogram_data_t*
|
||||
rocksdb_statistics_histogram_data_create() {
|
||||
return new rocksdb_statistics_histogram_data_t{};
|
||||
|
||||
+86
-1
@@ -3596,6 +3596,14 @@ int main(int argc, char** argv) {
|
||||
100000 ==
|
||||
rocksdb_fifo_compaction_options_get_max_table_files_size(fco));
|
||||
|
||||
rocksdb_fifo_compaction_options_set_max_data_files_size(fco, 200000);
|
||||
CheckCondition(
|
||||
200000 == rocksdb_fifo_compaction_options_get_max_data_files_size(fco));
|
||||
|
||||
rocksdb_fifo_compaction_options_set_use_kv_ratio_compaction(fco, 1);
|
||||
CheckCondition(
|
||||
1 == rocksdb_fifo_compaction_options_get_use_kv_ratio_compaction(fco));
|
||||
|
||||
rocksdb_fifo_compaction_options_destroy(fco);
|
||||
}
|
||||
|
||||
@@ -4447,7 +4455,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
StartPhase("statistics");
|
||||
{
|
||||
const uint32_t BYTES_WRITTEN_TICKER = 60;
|
||||
const uint32_t BYTES_WRITTEN_TICKER = 61;
|
||||
const uint32_t DB_WRITE_HIST = 1;
|
||||
|
||||
rocksdb_statistics_histogram_data_t* hist =
|
||||
@@ -4717,9 +4725,86 @@ int main(int argc, char** argv) {
|
||||
rocksdb_compaction_service_options_override_set_comparator(override_opts,
|
||||
cmp);
|
||||
|
||||
// Test file checksum gen factory
|
||||
rocksdb_file_checksum_gen_factory_t* checksum_factory =
|
||||
rocksdb_file_checksum_gen_crc32c_factory_create();
|
||||
CheckCondition(checksum_factory != NULL);
|
||||
rocksdb_compaction_service_options_override_set_file_checksum_gen_factory(
|
||||
override_opts, checksum_factory);
|
||||
|
||||
// Test SST partitioner factory
|
||||
rocksdb_sst_partitioner_factory_t* partitioner_factory =
|
||||
rocksdb_sst_partitioner_fixed_prefix_factory_create(4);
|
||||
CheckCondition(partitioner_factory != NULL);
|
||||
rocksdb_compaction_service_options_override_set_sst_partitioner_factory(
|
||||
override_opts, partitioner_factory);
|
||||
|
||||
// Test merge operator
|
||||
rocksdb_compaction_service_options_override_set_merge_operator(
|
||||
override_opts, NULL);
|
||||
|
||||
// Test compaction filter
|
||||
rocksdb_compaction_service_options_override_set_compaction_filter(
|
||||
override_opts, NULL);
|
||||
|
||||
// Test prefix extractor
|
||||
rocksdb_compaction_service_options_override_set_prefix_extractor(
|
||||
override_opts, NULL);
|
||||
|
||||
// Test table factory - block based
|
||||
rocksdb_block_based_table_options_t* table_opts =
|
||||
rocksdb_block_based_options_create();
|
||||
rocksdb_compaction_service_options_override_set_block_based_table_factory(
|
||||
override_opts, table_opts);
|
||||
rocksdb_block_based_options_destroy(table_opts);
|
||||
|
||||
// Test statistics via options
|
||||
rocksdb_options_t* stats_opts = rocksdb_options_create();
|
||||
rocksdb_options_enable_statistics(stats_opts);
|
||||
rocksdb_compaction_service_options_override_set_statistics(override_opts,
|
||||
stats_opts);
|
||||
rocksdb_options_destroy(stats_opts);
|
||||
|
||||
// Test info log
|
||||
rocksdb_logger_t* logger =
|
||||
rocksdb_logger_create_stderr_logger(1, "test_prefix");
|
||||
rocksdb_compaction_service_options_override_set_info_log(override_opts,
|
||||
logger);
|
||||
rocksdb_logger_destroy(logger);
|
||||
|
||||
// Test options map
|
||||
rocksdb_compaction_service_options_override_set_option(
|
||||
override_opts, "max_bytes_for_level_base", "67108864");
|
||||
|
||||
// Cleanup
|
||||
rocksdb_file_checksum_gen_factory_destroy(checksum_factory);
|
||||
rocksdb_sst_partitioner_factory_destroy(partitioner_factory);
|
||||
rocksdb_compaction_service_options_override_destroy(override_opts);
|
||||
}
|
||||
|
||||
StartPhase("factory_options_on_regular_options");
|
||||
{
|
||||
// Test that the new factory types work with regular rocksdb_options_t
|
||||
rocksdb_options_t* test_opts = rocksdb_options_create();
|
||||
|
||||
// Test file checksum gen factory on regular options
|
||||
rocksdb_file_checksum_gen_factory_t* checksum_factory =
|
||||
rocksdb_file_checksum_gen_crc32c_factory_create();
|
||||
CheckCondition(checksum_factory != NULL);
|
||||
rocksdb_options_set_file_checksum_gen_factory(test_opts, checksum_factory);
|
||||
|
||||
// Test SST partitioner factory on regular options
|
||||
rocksdb_sst_partitioner_factory_t* partitioner_factory =
|
||||
rocksdb_sst_partitioner_fixed_prefix_factory_create(8);
|
||||
CheckCondition(partitioner_factory != NULL);
|
||||
rocksdb_options_set_sst_partitioner_factory(test_opts, partitioner_factory);
|
||||
|
||||
// Cleanup
|
||||
rocksdb_file_checksum_gen_factory_destroy(checksum_factory);
|
||||
rocksdb_sst_partitioner_factory_destroy(partitioner_factory);
|
||||
rocksdb_options_destroy(test_opts);
|
||||
}
|
||||
|
||||
StartPhase("remote_compaction_null_callback_handling");
|
||||
{
|
||||
// Test that NULL callback returns are handled gracefully
|
||||
|
||||
+9
-3
@@ -401,7 +401,13 @@ ColumnFamilyOptions SanitizeCfOptions(const ImmutableDBOptions& db_options,
|
||||
}
|
||||
|
||||
if (result.max_compaction_bytes == 0) {
|
||||
result.max_compaction_bytes = result.target_file_size_base * 25;
|
||||
// For FIFO with use_kv_ratio_compaction, leave max_compaction_bytes as 0
|
||||
// to signal "auto-calculate target from capacity and SST/blob ratio."
|
||||
// When explicitly set by the user, it overrides the auto-calculated target.
|
||||
if (result.compaction_style != kCompactionStyleFIFO ||
|
||||
!result.compaction_options_fifo.use_kv_ratio_compaction) {
|
||||
result.max_compaction_bytes = result.target_file_size_base * 25;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_block_based_table = (result.table_factory->IsInstanceOf(
|
||||
@@ -1248,7 +1254,7 @@ Compaction* ColumnFamilyData::PickCompaction(
|
||||
auto* result = compaction_picker_->PickCompaction(
|
||||
GetName(), mutable_options, mutable_db_options, existing_snapshots,
|
||||
snapshot_checker, current_->storage_info(), log_buffer,
|
||||
require_max_output_level);
|
||||
GetFullHistoryTsLow(), require_max_output_level);
|
||||
if (result != nullptr) {
|
||||
result->FinalizeInputInfo(current_);
|
||||
}
|
||||
@@ -1336,7 +1342,7 @@ Compaction* ColumnFamilyData::CompactRange(
|
||||
GetName(), mutable_cf_options, mutable_db_options,
|
||||
current_->storage_info(), input_level, output_level,
|
||||
compact_range_options, begin, end, compaction_end, conflict,
|
||||
max_file_num_to_ignore, trim_ts);
|
||||
max_file_num_to_ignore, trim_ts, GetFullHistoryTsLow());
|
||||
if (result != nullptr) {
|
||||
result->FinalizeInputInfo(current_);
|
||||
}
|
||||
|
||||
+10
-13
@@ -118,8 +118,7 @@ class ColumnFamilyTestBase : public testing::Test {
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (flush_every != 0 && i != 0 && i % flush_every == 0) {
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
dbi->TEST_FlushMemTable();
|
||||
dbfull()->TEST_FlushMemTable();
|
||||
}
|
||||
|
||||
int keyi = base + i;
|
||||
@@ -177,8 +176,7 @@ class ColumnFamilyTestBase : public testing::Test {
|
||||
}
|
||||
handles_.clear();
|
||||
names_.clear();
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
}
|
||||
|
||||
Status TryOpen(std::vector<std::string> cf,
|
||||
@@ -218,7 +216,7 @@ class ColumnFamilyTestBase : public testing::Test {
|
||||
|
||||
void Open() { Open({"default"}); }
|
||||
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_); }
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_.get()); }
|
||||
|
||||
int GetProperty(int cf, std::string property) {
|
||||
std::string value;
|
||||
@@ -500,7 +498,7 @@ class ColumnFamilyTestBase : public testing::Test {
|
||||
ColumnFamilyOptions column_family_options_;
|
||||
DBOptions db_options_;
|
||||
std::string dbname_;
|
||||
DB* db_ = nullptr;
|
||||
std::unique_ptr<DB> db_;
|
||||
EnvCounter* env_;
|
||||
std::shared_ptr<Env> env_guard_;
|
||||
Random rnd_;
|
||||
@@ -517,7 +515,7 @@ class ColumnFamilyTest
|
||||
INSTANTIATE_TEST_CASE_P(FormatDef, ColumnFamilyTest,
|
||||
testing::Values(test::kDefaultFormatVersion));
|
||||
INSTANTIATE_TEST_CASE_P(FormatLatest, ColumnFamilyTest,
|
||||
testing::Values(kLatestFormatVersion));
|
||||
testing::Values(kLatestBbtFormatVersion));
|
||||
|
||||
TEST_P(ColumnFamilyTest, DontReuseColumnFamilyID) {
|
||||
for (int iter = 0; iter < 3; ++iter) {
|
||||
@@ -707,8 +705,8 @@ INSTANTIATE_TEST_CASE_P(
|
||||
std::make_tuple(test::kDefaultFormatVersion, false)));
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
FormatLatest, FlushEmptyCFTestWithParam,
|
||||
testing::Values(std::make_tuple(kLatestFormatVersion, true),
|
||||
std::make_tuple(kLatestFormatVersion, false)));
|
||||
testing::Values(std::make_tuple(kLatestBbtFormatVersion, true),
|
||||
std::make_tuple(kLatestBbtFormatVersion, false)));
|
||||
|
||||
TEST_P(ColumnFamilyTest, AddDrop) {
|
||||
Open();
|
||||
@@ -3542,11 +3540,10 @@ TEST_P(ColumnFamilyTest, MultipleCFPathsTest) {
|
||||
|
||||
// Re-open and verify the keys.
|
||||
Reopen({ColumnFamilyOptions(), cf_opt1, cf_opt2});
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
for (int cf = 1; cf != 3; ++cf) {
|
||||
ReadOptions read_options;
|
||||
read_options.readahead_size = 0;
|
||||
auto it = dbi->NewIterator(read_options, handles_[cf]);
|
||||
auto it = db_->NewIterator(read_options, handles_[cf]);
|
||||
for (it->SeekToFirst(); it->Valid(); it->Next()) {
|
||||
ASSERT_OK(it->status());
|
||||
Slice key(it->key());
|
||||
@@ -3636,7 +3633,7 @@ TEST(ColumnFamilyTest, ValidateMemtableKVChecksumOption) {
|
||||
// the behavior of manual flush is that it skips retaining UDTs.
|
||||
class ColumnFamilyRetainUDTTest : public ColumnFamilyTestBase {
|
||||
public:
|
||||
ColumnFamilyRetainUDTTest() : ColumnFamilyTestBase(kLatestFormatVersion) {}
|
||||
ColumnFamilyRetainUDTTest() : ColumnFamilyTestBase(kLatestBbtFormatVersion) {}
|
||||
|
||||
void SetUp() override {
|
||||
db_options_.allow_concurrent_memtable_write = false;
|
||||
@@ -3886,7 +3883,7 @@ TEST_F(ManualFlushSkipRetainUDTTest, FlushRemovesStaleEntries) {
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(cfh)->cfd();
|
||||
for (int version = 0; version < 100; version++) {
|
||||
if (version == 50) {
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db_)->TEST_SwitchMemtable(cfd));
|
||||
ASSERT_OK(dbfull()->TEST_SwitchMemtable(cfd));
|
||||
}
|
||||
ASSERT_OK(
|
||||
Put(0, "foo", EncodeAsUint64(version), "v" + std::to_string(version)));
|
||||
|
||||
+35
-48
@@ -75,10 +75,9 @@ TEST_F(CompactFilesTest, L0ConflictsFiles) {
|
||||
options.level0_file_num_compaction_trigger = kLevel0Trigger;
|
||||
options.compression = kNoCompression;
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
assert(s.ok());
|
||||
ASSERT_OK(DB::Open(options, db_name_, &db));
|
||||
assert(db);
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
||||
@@ -114,7 +113,6 @@ TEST_F(CompactFilesTest, L0ConflictsFiles) {
|
||||
}
|
||||
}
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, MultipleLevel) {
|
||||
@@ -128,11 +126,11 @@ TEST_F(CompactFilesTest, MultipleLevel) {
|
||||
FlushedFileCollector* collector = new FlushedFileCollector();
|
||||
options.listeners.emplace_back(collector);
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
ASSERT_NE(db, nullptr);
|
||||
ASSERT_NE(db.get(), nullptr);
|
||||
|
||||
// create couple files in L0, L3, L4 and L5
|
||||
for (int i = 5; i > 2; --i) {
|
||||
@@ -141,7 +139,8 @@ TEST_F(CompactFilesTest, MultipleLevel) {
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
// Ensure background work is fully finished including listener callbacks
|
||||
// before accessing listener state.
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForBackgroundWork());
|
||||
ASSERT_OK(
|
||||
static_cast_with_check<DBImpl>(db.get())->TEST_WaitForBackgroundWork());
|
||||
auto l0_files = collector->GetFlushedFiles();
|
||||
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, i));
|
||||
|
||||
@@ -191,8 +190,6 @@ TEST_F(CompactFilesTest, MultipleLevel) {
|
||||
ASSERT_OK(db->CompactFiles(CompactionOptions(), files, 5));
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
thread.join();
|
||||
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, ObsoleteFiles) {
|
||||
@@ -212,11 +209,11 @@ TEST_F(CompactFilesTest, ObsoleteFiles) {
|
||||
FlushedFileCollector* collector = new FlushedFileCollector();
|
||||
options.listeners.emplace_back(collector);
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
ASSERT_NE(db, nullptr);
|
||||
ASSERT_NE(db.get(), nullptr);
|
||||
|
||||
// create couple files
|
||||
for (int i = 1000; i < 2000; ++i) {
|
||||
@@ -226,13 +223,12 @@ TEST_F(CompactFilesTest, ObsoleteFiles) {
|
||||
|
||||
auto l0_files = collector->GetFlushedFiles();
|
||||
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files, 1));
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForCompact());
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db.get())->TEST_WaitForCompact());
|
||||
|
||||
// verify all compaction input files are deleted
|
||||
for (const auto& fname : l0_files) {
|
||||
ASSERT_EQ(Status::NotFound(), env_->FileExists(fname));
|
||||
}
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, NotCutOutputOnLevel0) {
|
||||
@@ -251,10 +247,9 @@ TEST_F(CompactFilesTest, NotCutOutputOnLevel0) {
|
||||
FlushedFileCollector* collector = new FlushedFileCollector();
|
||||
options.listeners.emplace_back(collector);
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
assert(s.ok());
|
||||
ASSERT_OK(DB::Open(options, db_name_, &db));
|
||||
assert(db);
|
||||
|
||||
// create couple files
|
||||
@@ -262,19 +257,20 @@ TEST_F(CompactFilesTest, NotCutOutputOnLevel0) {
|
||||
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
||||
std::string(1000, 'a' + (i % 26))));
|
||||
}
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForFlushMemTable());
|
||||
ASSERT_OK(
|
||||
static_cast_with_check<DBImpl>(db.get())->TEST_WaitForFlushMemTable());
|
||||
auto l0_files_1 = collector->GetFlushedFiles();
|
||||
collector->ClearFlushedFiles();
|
||||
for (int i = 0; i < 500; ++i) {
|
||||
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
||||
std::string(1000, 'a' + (i % 26))));
|
||||
}
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForFlushMemTable());
|
||||
ASSERT_OK(
|
||||
static_cast_with_check<DBImpl>(db.get())->TEST_WaitForFlushMemTable());
|
||||
auto l0_files_2 = collector->GetFlushedFiles();
|
||||
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files_1, 0));
|
||||
ASSERT_OK(db->CompactFiles(CompactionOptions(), l0_files_2, 0));
|
||||
// no assertion failure
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, CapturingPendingFiles) {
|
||||
@@ -289,7 +285,7 @@ TEST_F(CompactFilesTest, CapturingPendingFiles) {
|
||||
FlushedFileCollector* collector = new FlushedFileCollector();
|
||||
options.listeners.emplace_back(collector);
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
@@ -303,7 +299,8 @@ TEST_F(CompactFilesTest, CapturingPendingFiles) {
|
||||
|
||||
// Ensure background work is fully finished including listener callbacks
|
||||
// before accessing listener state.
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForBackgroundWork());
|
||||
ASSERT_OK(
|
||||
static_cast_with_check<DBImpl>(db.get())->TEST_WaitForBackgroundWork());
|
||||
auto l0_files = collector->GetFlushedFiles();
|
||||
EXPECT_EQ(5, l0_files.size());
|
||||
|
||||
@@ -327,13 +324,12 @@ TEST_F(CompactFilesTest, CapturingPendingFiles) {
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
|
||||
// Make sure we can reopen the DB.
|
||||
s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
assert(db);
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, CompactionFilterWithGetSv) {
|
||||
@@ -365,12 +361,12 @@ TEST_F(CompactFilesTest, CompactionFilterWithGetSv) {
|
||||
options.create_if_missing = true;
|
||||
options.compaction_filter = cf.get();
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
|
||||
cf->SetDB(db);
|
||||
cf->SetDB(db.get());
|
||||
|
||||
// Write one L0 file
|
||||
ASSERT_OK(db->Put(WriteOptions(), "K1", "V1"));
|
||||
@@ -384,8 +380,6 @@ TEST_F(CompactFilesTest, CompactionFilterWithGetSv) {
|
||||
ASSERT_OK(
|
||||
db->CompactFiles(ROCKSDB_NAMESPACE::CompactionOptions(), {fname}, 0));
|
||||
}
|
||||
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, SentinelCompressionType) {
|
||||
@@ -413,7 +407,7 @@ TEST_F(CompactFilesTest, SentinelCompressionType) {
|
||||
options.create_if_missing = true;
|
||||
FlushedFileCollector* collector = new FlushedFileCollector();
|
||||
options.listeners.emplace_back(collector);
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, db_name_, &db));
|
||||
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key", "val"));
|
||||
@@ -421,7 +415,8 @@ TEST_F(CompactFilesTest, SentinelCompressionType) {
|
||||
|
||||
// Ensure background work is fully finished including listener callbacks
|
||||
// before accessing listener state.
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForBackgroundWork());
|
||||
ASSERT_OK(
|
||||
static_cast_with_check<DBImpl>(db.get())->TEST_WaitForBackgroundWork());
|
||||
auto l0_files = collector->GetFlushedFiles();
|
||||
ASSERT_EQ(1, l0_files.size());
|
||||
|
||||
@@ -433,10 +428,10 @@ TEST_F(CompactFilesTest, SentinelCompressionType) {
|
||||
ROCKSDB_NAMESPACE::TablePropertiesCollection all_tables_props;
|
||||
ASSERT_OK(db->GetPropertiesOfAllTables(&all_tables_props));
|
||||
for (const auto& name_and_table_props : all_tables_props) {
|
||||
ASSERT_EQ(CompressionTypeToString(CompressionType::kZlibCompression),
|
||||
name_and_table_props.second->compression_name);
|
||||
// As of format_version 7, more elaborate information is encoded into the
|
||||
// compression_name property
|
||||
ASSERT_EQ("BuiltinV2;02;", name_and_table_props.second->compression_name);
|
||||
}
|
||||
delete db;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,11 +456,7 @@ TEST_F(CompactFilesTest, CompressionWithBlockAlign) {
|
||||
}
|
||||
|
||||
std::unique_ptr<DB> db;
|
||||
{
|
||||
DB* _db = nullptr;
|
||||
ASSERT_OK(DB::Open(options, db_name_, &_db));
|
||||
db.reset(_db);
|
||||
}
|
||||
ASSERT_OK(DB::Open(options, db_name_, &db));
|
||||
|
||||
ASSERT_OK(db->Put(WriteOptions(), "key", "val"));
|
||||
ASSERT_OK(db->Flush(FlushOptions()));
|
||||
@@ -504,7 +495,7 @@ TEST_F(CompactFilesTest, GetCompactionJobInfo) {
|
||||
FlushedFileCollector* collector = new FlushedFileCollector();
|
||||
options.listeners.emplace_back(collector);
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
@@ -515,7 +506,8 @@ TEST_F(CompactFilesTest, GetCompactionJobInfo) {
|
||||
ASSERT_OK(db->Put(WriteOptions(), std::to_string(i),
|
||||
std::string(1000, 'a' + (i % 26))));
|
||||
}
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db)->TEST_WaitForFlushMemTable());
|
||||
ASSERT_OK(
|
||||
static_cast_with_check<DBImpl>(db.get())->TEST_WaitForFlushMemTable());
|
||||
auto l0_files_1 = collector->GetFlushedFiles();
|
||||
CompactionOptions co;
|
||||
co.compression = CompressionType::kLZ4Compression;
|
||||
@@ -531,7 +523,6 @@ TEST_F(CompactFilesTest, GetCompactionJobInfo) {
|
||||
ASSERT_EQ(compaction_job_info.output_level, 0);
|
||||
ASSERT_OK(compaction_job_info.status);
|
||||
// no assertion failure
|
||||
delete db;
|
||||
}
|
||||
|
||||
// Helper function to generate zero-padded keys
|
||||
@@ -547,11 +538,11 @@ TEST_F(CompactFilesTest, TrivialMoveNonOverlappingFiles) {
|
||||
options.compression = kNoCompression;
|
||||
options.level_compaction_dynamic_level_bytes = false;
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
ASSERT_NE(db, nullptr);
|
||||
ASSERT_NE(db.get(), nullptr);
|
||||
|
||||
// Create 3 non-overlapping files in L0
|
||||
// File 1: keys [a00-a99]
|
||||
@@ -664,8 +655,6 @@ TEST_F(CompactFilesTest, TrivialMoveNonOverlappingFiles) {
|
||||
ASSERT_OK(db->Get(ReadOptions(), key, &value));
|
||||
ASSERT_EQ(value, "value_" + key);
|
||||
}
|
||||
|
||||
delete db;
|
||||
}
|
||||
|
||||
TEST_F(CompactFilesTest, TrivialMoveBlockedByOverlap) {
|
||||
@@ -676,11 +665,11 @@ TEST_F(CompactFilesTest, TrivialMoveBlockedByOverlap) {
|
||||
options.level_compaction_dynamic_level_bytes = false;
|
||||
options.num_levels = 7;
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DestroyDB(db_name_, options));
|
||||
Status s = DB::Open(options, db_name_, &db);
|
||||
ASSERT_OK(s);
|
||||
ASSERT_NE(db, nullptr);
|
||||
ASSERT_NE(db.get(), nullptr);
|
||||
|
||||
// Create a file in L6 with keys [m00-m99] (wide range)
|
||||
for (int i = 0; i < 100; i++) {
|
||||
@@ -756,8 +745,6 @@ TEST_F(CompactFilesTest, TrivialMoveBlockedByOverlap) {
|
||||
ASSERT_OK(db->Get(ReadOptions(), key, &value));
|
||||
ASSERT_EQ(value, "updated_value_" + key);
|
||||
}
|
||||
|
||||
delete db;
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
@@ -1310,14 +1310,14 @@ void CompactionIterator::PrepareOutput() {
|
||||
validity_info_.rep);
|
||||
assert(false);
|
||||
}
|
||||
ikey_.sequence = 0;
|
||||
last_key_seq_zeroed_ = true;
|
||||
TEST_SYNC_POINT_CALLBACK("CompactionIterator::PrepareOutput:ZeroingSeq",
|
||||
&ikey_);
|
||||
|
||||
bool zeroed_seqno = false;
|
||||
if (!timestamp_size_) {
|
||||
current_key_.UpdateInternalKey(0, ikey_.type);
|
||||
zeroed_seqno = true;
|
||||
} else if (full_history_ts_low_ && cmp_with_history_ts_low_ < 0) {
|
||||
// We can also zero out timestamp for better compression.
|
||||
// For UDT, the seqno and timestamp could only be zeroed out after the
|
||||
// key is below history_ts_low_.
|
||||
// For the same user key (excluding timestamp), the timestamp-based
|
||||
// history can be collapsed to save some space if the timestamp is
|
||||
// older than *full_history_ts_low_.
|
||||
@@ -1325,6 +1325,14 @@ void CompactionIterator::PrepareOutput() {
|
||||
const Slice ts_slice = kTsMin;
|
||||
ikey_.SetTimestamp(ts_slice);
|
||||
current_key_.UpdateInternalKey(0, ikey_.type, &ts_slice);
|
||||
zeroed_seqno = true;
|
||||
}
|
||||
|
||||
if (zeroed_seqno) {
|
||||
ikey_.sequence = 0;
|
||||
last_key_seq_zeroed_ = true;
|
||||
TEST_SYNC_POINT_CALLBACK("CompactionIterator::PrepareOutput:ZeroingSeq",
|
||||
&ikey_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+152
-30
@@ -128,6 +128,10 @@ const char* GetCompactionProximalOutputRangeTypeString(
|
||||
}
|
||||
}
|
||||
|
||||
// Static constant for compaction abort flag - always false, used for
|
||||
// compaction service jobs that don't support abort signaling
|
||||
const std::atomic<int> CompactionJob::kCompactionAbortedFalse{0};
|
||||
|
||||
CompactionJob::CompactionJob(
|
||||
int job_id, Compaction* compaction, const ImmutableDBOptions& db_options,
|
||||
const MutableDBOptions& mutable_db_options, const FileOptions& file_options,
|
||||
@@ -141,10 +145,10 @@ CompactionJob::CompactionJob(
|
||||
CompactionJobStats* compaction_job_stats, Env::Priority thread_pri,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
const std::atomic<bool>& manual_compaction_canceled,
|
||||
const std::string& db_id, const std::string& db_session_id,
|
||||
std::string full_history_ts_low, std::string trim_ts,
|
||||
BlobFileCompletionCallback* blob_callback, int* bg_compaction_scheduled,
|
||||
int* bg_bottom_compaction_scheduled)
|
||||
const std::atomic<int>& compaction_aborted, const std::string& db_id,
|
||||
const std::string& db_session_id, std::string full_history_ts_low,
|
||||
std::string trim_ts, BlobFileCompletionCallback* blob_callback,
|
||||
int* bg_compaction_scheduled, int* bg_bottom_compaction_scheduled)
|
||||
: compact_(new CompactionState(compaction)),
|
||||
internal_stats_(compaction->compaction_reason(), 1),
|
||||
db_options_(db_options),
|
||||
@@ -168,6 +172,7 @@ CompactionJob::CompactionJob(
|
||||
versions_(versions),
|
||||
shutting_down_(shutting_down),
|
||||
manual_compaction_canceled_(manual_compaction_canceled),
|
||||
compaction_aborted_(compaction_aborted),
|
||||
db_directory_(db_directory),
|
||||
blob_output_directory_(blob_output_directory),
|
||||
db_mutex_(db_mutex),
|
||||
@@ -708,6 +713,7 @@ void CompactionJob::InitializeCompactionRun() {
|
||||
}
|
||||
|
||||
void CompactionJob::RunSubcompactions() {
|
||||
TEST_SYNC_POINT("CompactionJob::RunSubcompactions:BeforeStart");
|
||||
const size_t num_threads = compact_->sub_compact_states.size();
|
||||
assert(num_threads > 0);
|
||||
compact_->compaction->GetOrInitInputTableProperties();
|
||||
@@ -753,6 +759,71 @@ void CompactionJob::RemoveEmptyOutputs() {
|
||||
}
|
||||
}
|
||||
|
||||
void CompactionJob::CleanupAbortedSubcompactions() {
|
||||
ColumnFamilyData* cfd = compact_->compaction->column_family_data();
|
||||
|
||||
uint64_t total_sst_files_deleted = 0;
|
||||
uint64_t total_blob_files_deleted = 0;
|
||||
|
||||
// Track the first file deletion error to report at the end
|
||||
Status first_error;
|
||||
int deletion_errors = 0;
|
||||
|
||||
// Mark all subcompactions as aborted and delete their output files
|
||||
for (auto& sub_compact : compact_->sub_compact_states) {
|
||||
// Mark this subcompaction as aborted
|
||||
sub_compact.status =
|
||||
Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
|
||||
// Delete all files (SST and blob) tracked during compaction.
|
||||
// GetOutputFilePaths() contains ALL file paths created, including
|
||||
// in-progress files that may have been removed from outputs_ or
|
||||
// blob_file_additions_.
|
||||
for (const bool is_proximal_level : {false, true}) {
|
||||
if (is_proximal_level &&
|
||||
!compact_->compaction->SupportsPerKeyPlacement()) {
|
||||
continue;
|
||||
}
|
||||
for (const std::string& file_path :
|
||||
sub_compact.Outputs(is_proximal_level)->GetOutputFilePaths()) {
|
||||
Status s = env_->DeleteFile(file_path);
|
||||
if (s.ok()) {
|
||||
// Count SST vs blob files by checking extension
|
||||
if (file_path.find(".sst") != std::string::npos) {
|
||||
total_sst_files_deleted++;
|
||||
} else if (file_path.find(".blob") != std::string::npos) {
|
||||
total_blob_files_deleted++;
|
||||
}
|
||||
} else if (!s.IsNotFound()) {
|
||||
if (first_error.ok()) {
|
||||
first_error = s;
|
||||
}
|
||||
deletion_errors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
sub_compact.CleanupOutputs();
|
||||
}
|
||||
|
||||
if (stats_) {
|
||||
RecordTick(stats_, COMPACTION_ABORTED);
|
||||
}
|
||||
|
||||
ROCKS_LOG_INFO(db_options_.info_log,
|
||||
"[%s] [JOB %d] Compaction aborted: deleted %" PRIu64
|
||||
" SST files and %" PRIu64 " blob files",
|
||||
cfd->GetName().c_str(), job_id_, total_sst_files_deleted,
|
||||
total_blob_files_deleted);
|
||||
|
||||
if (!first_error.ok()) {
|
||||
ROCKS_LOG_ERROR(db_options_.info_log,
|
||||
"[%s] [JOB %d] Cleanup completed with %d file deletion "
|
||||
"errors. First error: %s",
|
||||
cfd->GetName().c_str(), job_id_, deletion_errors,
|
||||
first_error.ToString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
bool CompactionJob::HasNewBlobFiles() const {
|
||||
for (const auto& state : compact_->sub_compact_states) {
|
||||
if (state.Current().HasBlobFileAdditions()) {
|
||||
@@ -1004,6 +1075,15 @@ Status CompactionJob::Run() {
|
||||
|
||||
Status status = CollectSubcompactionErrors();
|
||||
|
||||
// If compaction was aborted or manually paused, clean up any output files
|
||||
// from completed subcompactions to prevent orphaned files on disk.
|
||||
// Skip cleanup for resumable compaction (when progress writer is set)
|
||||
// because the output files are needed for resumption.
|
||||
if ((status.IsCompactionAborted() || status.IsManualCompactionPaused()) &&
|
||||
compaction_progress_writer_ == nullptr) {
|
||||
CleanupAbortedSubcompactions();
|
||||
}
|
||||
|
||||
if (status.ok()) {
|
||||
status = SyncOutputDirectories();
|
||||
}
|
||||
@@ -1415,10 +1495,10 @@ InternalIterator* CompactionJob::CreateInputIterator(
|
||||
return input;
|
||||
}
|
||||
|
||||
void CompactionJob::CreateBlobFileBuilder(SubcompactionState* sub_compact,
|
||||
ColumnFamilyData* cfd,
|
||||
BlobFileResources& blob_resources,
|
||||
const WriteOptions& write_options) {
|
||||
void CompactionJob::CreateBlobFileBuilder(
|
||||
SubcompactionState* sub_compact, ColumnFamilyData* cfd,
|
||||
std::unique_ptr<BlobFileBuilder>& blob_file_builder,
|
||||
const WriteOptions& write_options) {
|
||||
const auto& mutable_cf_options =
|
||||
sub_compact->compaction->mutable_cf_options();
|
||||
|
||||
@@ -1427,24 +1507,24 @@ void CompactionJob::CreateBlobFileBuilder(SubcompactionState* sub_compact,
|
||||
if (mutable_cf_options.enable_blob_files &&
|
||||
sub_compact->compaction->output_level() >=
|
||||
mutable_cf_options.blob_file_starting_level) {
|
||||
blob_resources.blob_file_builder = std::make_unique<BlobFileBuilder>(
|
||||
blob_file_builder = std::make_unique<BlobFileBuilder>(
|
||||
versions_, fs_.get(), &sub_compact->compaction->immutable_options(),
|
||||
&mutable_cf_options, &file_options_, &write_options, db_id_,
|
||||
db_session_id_, job_id_, cfd->GetID(), cfd->GetName(), write_hint_,
|
||||
io_tracer_, blob_callback_, BlobFileCreationReason::kCompaction,
|
||||
&blob_resources.blob_file_paths,
|
||||
sub_compact->Current().GetOutputFilePathsPtr(),
|
||||
sub_compact->Current().GetBlobFileAdditionsPtr());
|
||||
} else {
|
||||
blob_resources.blob_file_builder = nullptr;
|
||||
blob_file_builder = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<CompactionIterator> CompactionJob::CreateCompactionIterator(
|
||||
SubcompactionState* sub_compact, ColumnFamilyData* cfd,
|
||||
InternalIterator* input, const CompactionFilter* compaction_filter,
|
||||
MergeHelper& merge, BlobFileResources& blob_resources,
|
||||
MergeHelper& merge, std::unique_ptr<BlobFileBuilder>& blob_file_builder,
|
||||
const WriteOptions& write_options) {
|
||||
CreateBlobFileBuilder(sub_compact, cfd, blob_resources, write_options);
|
||||
CreateBlobFileBuilder(sub_compact, cfd, blob_file_builder, write_options);
|
||||
|
||||
const std::string* const full_history_ts_low =
|
||||
full_history_ts_low_.empty() ? nullptr : &full_history_ts_low_;
|
||||
@@ -1456,7 +1536,7 @@ std::unique_ptr<CompactionIterator> CompactionJob::CreateCompactionIterator(
|
||||
job_context_->earliest_write_conflict_snapshot,
|
||||
job_context_->GetJobSnapshotSequence(), job_context_->snapshot_checker,
|
||||
env_, ShouldReportDetailedTime(env_, stats_), sub_compact->RangeDelAgg(),
|
||||
blob_resources.blob_file_builder.get(), db_options_.allow_data_in_errors,
|
||||
blob_file_builder.get(), db_options_.allow_data_in_errors,
|
||||
db_options_.enforce_single_del_contracts, manual_compaction_canceled_,
|
||||
sub_compact->compaction
|
||||
->DoesInputReferenceBlobFiles() /* must_count_input_entries */,
|
||||
@@ -1495,10 +1575,17 @@ Status CompactionJob::ProcessKeyValue(
|
||||
SubcompactionState* sub_compact, ColumnFamilyData* cfd,
|
||||
CompactionIterator* c_iter, const CompactionFileOpenFunc& open_file_func,
|
||||
const CompactionFileCloseFunc& close_file_func, uint64_t& prev_cpu_micros) {
|
||||
Status status;
|
||||
const uint64_t kRecordStatsEvery = 1000;
|
||||
// Cron interval for periodic operations: stats update, abort check,
|
||||
// and sync points. Uses 1024 (power of 2) for efficient bitwise check.
|
||||
const uint64_t kCronEveryMask = (1 << 10) - 1;
|
||||
[[maybe_unused]] const std::optional<const Slice> end = sub_compact->end;
|
||||
|
||||
// Check for abort signal before starting key processing
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
return Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
}
|
||||
|
||||
Status status;
|
||||
IterKey prev_iter_output_key;
|
||||
ParsedInternalKey prev_iter_output_internal_key;
|
||||
|
||||
@@ -1511,8 +1598,16 @@ Status CompactionJob::ProcessKeyValue(
|
||||
assert(!end.has_value() ||
|
||||
cfd->user_comparator()->Compare(c_iter->user_key(), *end) < 0);
|
||||
|
||||
if (c_iter->iter_stats().num_input_records % kRecordStatsEvery ==
|
||||
kRecordStatsEvery - 1) {
|
||||
const uint64_t num_records = c_iter->iter_stats().num_input_records;
|
||||
|
||||
// Periodic cron operations: stats update, abort check.
|
||||
if ((num_records & kCronEveryMask) == kCronEveryMask) {
|
||||
// Check for abort signal periodically
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
status = Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
break;
|
||||
}
|
||||
|
||||
UpdateSubcompactionJobStatsIncrementally(
|
||||
c_iter, &sub_compact->compaction_job_stats,
|
||||
db_options_.clock->CPUMicros(), prev_cpu_micros);
|
||||
@@ -1719,6 +1814,7 @@ Status CompactionJob::FinalizeBlobFiles(SubcompactionState* sub_compact,
|
||||
}
|
||||
|
||||
void CompactionJob::ProcessKeyValueCompaction(SubcompactionState* sub_compact) {
|
||||
TEST_SYNC_POINT("CompactionJob::ProcessKeyValueCompaction:Start");
|
||||
assert(sub_compact);
|
||||
assert(sub_compact->compaction);
|
||||
|
||||
@@ -1772,11 +1868,11 @@ void CompactionJob::ProcessKeyValueCompaction(SubcompactionState* sub_compact) {
|
||||
false /* internal key corruption is expected */,
|
||||
job_context_->GetLatestSnapshotSequence(), job_context_->snapshot_checker,
|
||||
compact_->compaction->level(), db_options_.stats);
|
||||
BlobFileResources blob_resources;
|
||||
std::unique_ptr<BlobFileBuilder> blob_file_builder;
|
||||
|
||||
auto c_iter =
|
||||
CreateCompactionIterator(sub_compact, cfd, input_iter, compaction_filter,
|
||||
merge, blob_resources, write_options);
|
||||
merge, blob_file_builder, write_options);
|
||||
assert(c_iter);
|
||||
c_iter->SeekToFirst();
|
||||
|
||||
@@ -1794,9 +1890,8 @@ void CompactionJob::ProcessKeyValueCompaction(SubcompactionState* sub_compact) {
|
||||
status = FinalizeProcessKeyValueStatus(cfd, input_iter, c_iter.get(), status);
|
||||
|
||||
FinalizeSubcompaction(sub_compact, status, open_file_func, close_file_func,
|
||||
blob_resources.blob_file_builder.get(), c_iter.get(),
|
||||
input_iter, start_cpu_micros, prev_cpu_micros,
|
||||
io_stats);
|
||||
blob_file_builder.get(), c_iter.get(), input_iter,
|
||||
start_cpu_micros, prev_cpu_micros, io_stats);
|
||||
|
||||
NotifyOnSubcompactionCompleted(sub_compact);
|
||||
}
|
||||
@@ -2295,6 +2390,10 @@ Status CompactionJob::OpenCompactionOutputFile(SubcompactionState* sub_compact,
|
||||
Status s;
|
||||
IOStatus io_s = NewWritableFile(fs_.get(), fname, &writable_file, fo_copy);
|
||||
s = io_s;
|
||||
if (io_s.ok()) {
|
||||
// Track the SST file path for cleanup on abort.
|
||||
outputs.AddOutputFilePath(fname);
|
||||
}
|
||||
if (sub_compact->io_status.ok()) {
|
||||
sub_compact->io_status = io_s;
|
||||
// Since this error is really a copy of the io_s that is checked below as s,
|
||||
@@ -2438,6 +2537,32 @@ bool CompactionJob::UpdateInternalStatsFromInputFiles(
|
||||
bool has_error = false;
|
||||
const ReadOptions read_options(Env::IOActivity::kCompaction);
|
||||
const auto& input_table_properties = compaction->GetInputTableProperties();
|
||||
|
||||
// Check all input files for old block-based SST format_version. Why? Old
|
||||
// block-based SST files from roughly version 5.0 to 5.18 could produce
|
||||
// inaccurate num_entries counts due to the evolution of its handling along
|
||||
// with num_range_deletions. We have to disable some paranoid checks when
|
||||
// compacting files from such an old release. However, we don't have great
|
||||
// information to identify those files, so we heuristically over-approximate
|
||||
// that set of files using
|
||||
// (a) format_version < 5, which will be true for any files from RocksDB <
|
||||
// 6.6.0 and should not be true for any recent production files
|
||||
// (b) to avoid including non-block-based SST files (which still use older
|
||||
// format_version markers, and do not support DeleteRange), we also require
|
||||
// the presence of the user property "rocksdb.block.based.table.index.type",
|
||||
// which was added in RocksDB 2.8 and is always present in block-based tables.
|
||||
for (const auto& tp_pair : input_table_properties) {
|
||||
if (tp_pair.second && tp_pair.second->format_version < 5) {
|
||||
// Check for block-based table by looking for its index type property
|
||||
const auto& user_props = tp_pair.second->user_collected_properties;
|
||||
if (user_props.find(BlockBasedTablePropertyNames::kIndexType) !=
|
||||
user_props.end()) {
|
||||
job_stats_->has_accurate_num_input_records = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int input_level = 0;
|
||||
input_level < static_cast<int>(compaction->num_input_levels());
|
||||
++input_level) {
|
||||
@@ -2682,7 +2807,10 @@ Status CompactionJob::ReadTablePropertiesDirectly(
|
||||
std::shared_ptr<const TableProperties>* tp) {
|
||||
std::unique_ptr<FSRandomAccessFile> file;
|
||||
std::string file_name = GetTableFileName(file_meta->fd.GetNumber());
|
||||
Status s = ioptions.fs->NewRandomAccessFile(file_name, file_options_, &file,
|
||||
FileOptions fopts = file_options_;
|
||||
fopts.file_checksum = file_meta->file_checksum;
|
||||
fopts.file_checksum_func_name = file_meta->file_checksum_func_name;
|
||||
Status s = ioptions.fs->NewRandomAccessFile(file_name, fopts, &file,
|
||||
nullptr /* dbg */);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
@@ -2804,12 +2932,6 @@ void CompactionJob::RestoreCompactionOutputs(
|
||||
// - Status::NotFound(): No valid progress to resume from
|
||||
// - Status::Corruption(): Resume key is invalid, beyond input range, or output
|
||||
// restoration failed
|
||||
// - Other non-OK status: Iterator errors or file system issues during
|
||||
// restoration
|
||||
//
|
||||
// The caller must check for Status::IsIncomplete() to distinguish between
|
||||
// "no resume needed" (proceed with `InternalIterator::SeekToFirst()`) vs
|
||||
// "resume failed" scenarios.
|
||||
Status CompactionJob::MaybeResumeSubcompactionProgressOnInputIterator(
|
||||
SubcompactionState* sub_compact, InternalIterator* input_iter) {
|
||||
const ReadOptions read_options(Env::IOActivity::kCompaction);
|
||||
|
||||
@@ -142,6 +142,9 @@ class SubcompactionState;
|
||||
|
||||
class CompactionJob {
|
||||
public:
|
||||
// Constant false aborted flag, used for compaction service jobs
|
||||
static const std::atomic<int> kCompactionAbortedFalse;
|
||||
|
||||
CompactionJob(int job_id, Compaction* compaction,
|
||||
const ImmutableDBOptions& db_options,
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
@@ -157,6 +160,7 @@ class CompactionJob {
|
||||
Env::Priority thread_pri,
|
||||
const std::shared_ptr<IOTracer>& io_tracer,
|
||||
const std::atomic<bool>& manual_compaction_canceled,
|
||||
const std::atomic<int>& compaction_aborted,
|
||||
const std::string& db_id = "",
|
||||
const std::string& db_session_id = "",
|
||||
std::string full_history_ts_low = "", std::string trim_ts = "",
|
||||
@@ -250,6 +254,10 @@ class CompactionJob {
|
||||
// @param num_input_range_del if non-null, will be set to the number of range
|
||||
// deletion entries in this compaction input.
|
||||
//
|
||||
// If any input file has potentially unreliable num_entries count (old SST
|
||||
// files - details in implementation),
|
||||
// job_stats_->has_accurate_num_input_records is set to false.
|
||||
//
|
||||
// Returns true iff internal_stats_.output_level_stats.num_input_records and
|
||||
// num_input_range_del are calculated successfully.
|
||||
//
|
||||
@@ -299,6 +307,7 @@ class CompactionJob {
|
||||
void RunSubcompactions();
|
||||
void UpdateTimingStats(uint64_t start_micros);
|
||||
void RemoveEmptyOutputs();
|
||||
void CleanupAbortedSubcompactions();
|
||||
bool HasNewBlobFiles() const;
|
||||
Status CollectSubcompactionErrors();
|
||||
Status SyncOutputDirectories();
|
||||
@@ -363,11 +372,6 @@ class CompactionJob {
|
||||
std::unique_ptr<InternalIterator> trim_history_iter;
|
||||
};
|
||||
|
||||
struct BlobFileResources {
|
||||
std::vector<std::string> blob_file_paths;
|
||||
std::unique_ptr<BlobFileBuilder> blob_file_builder;
|
||||
};
|
||||
|
||||
bool ShouldUseLocalCompaction(SubcompactionState* sub_compact);
|
||||
CompactionIOStatsSnapshot InitializeIOStats();
|
||||
Status SetupAndValidateCompactionFilter(
|
||||
@@ -382,14 +386,14 @@ class CompactionJob {
|
||||
SubcompactionState* sub_compact, ColumnFamilyData* cfd,
|
||||
SubcompactionInternalIterators& iterators,
|
||||
SubcompactionKeyBoundaries& boundaries, ReadOptions& read_options);
|
||||
void CreateBlobFileBuilder(SubcompactionState* sub_compact,
|
||||
ColumnFamilyData* cfd,
|
||||
BlobFileResources& blob_resources,
|
||||
const WriteOptions& write_options);
|
||||
void CreateBlobFileBuilder(
|
||||
SubcompactionState* sub_compact, ColumnFamilyData* cfd,
|
||||
std::unique_ptr<BlobFileBuilder>& blob_file_builder,
|
||||
const WriteOptions& write_options);
|
||||
std::unique_ptr<CompactionIterator> CreateCompactionIterator(
|
||||
SubcompactionState* sub_compact, ColumnFamilyData* cfd,
|
||||
InternalIterator* input_iter, const CompactionFilter* compaction_filter,
|
||||
MergeHelper& merge, BlobFileResources& blob_resources,
|
||||
MergeHelper& merge, std::unique_ptr<BlobFileBuilder>& blob_file_builder,
|
||||
const WriteOptions& write_options);
|
||||
std::pair<CompactionFileOpenFunc, CompactionFileCloseFunc> CreateFileHandlers(
|
||||
SubcompactionState* sub_compact, SubcompactionKeyBoundaries& boundaries);
|
||||
@@ -461,6 +465,7 @@ class CompactionJob {
|
||||
VersionSet* versions_;
|
||||
const std::atomic<bool>* shutting_down_;
|
||||
const std::atomic<bool>& manual_compaction_canceled_;
|
||||
const std::atomic<int>& compaction_aborted_;
|
||||
FSDirectory* db_directory_;
|
||||
FSDirectory* blob_output_directory_;
|
||||
InstrumentedMutex* db_mutex_;
|
||||
|
||||
@@ -82,7 +82,7 @@ class CompactionJobStatsTest : public testing::Test,
|
||||
std::string dbname_;
|
||||
std::string alternative_wal_dir_;
|
||||
Env* env_;
|
||||
DB* db_;
|
||||
std::unique_ptr<DB> db_;
|
||||
std::vector<ColumnFamilyHandle*> handles_;
|
||||
uint32_t max_subcompactions_;
|
||||
|
||||
@@ -123,7 +123,7 @@ class CompactionJobStatsTest : public testing::Test,
|
||||
static void SetUpTestCase() {}
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_); }
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_.get()); }
|
||||
|
||||
void CreateColumnFamilies(const std::vector<std::string>& cfs,
|
||||
const Options& options) {
|
||||
@@ -162,7 +162,8 @@ class CompactionJobStatsTest : public testing::Test,
|
||||
column_families.emplace_back(cfs[i], options[i]);
|
||||
}
|
||||
DBOptions db_opts = DBOptions(options[0]);
|
||||
return DB::Open(db_opts, dbname_, column_families, &handles_, &db_);
|
||||
auto s = DB::Open(db_opts, dbname_, column_families, &handles_, &db_);
|
||||
return s;
|
||||
}
|
||||
|
||||
Status TryReopenWithColumnFamilies(const std::vector<std::string>& cfs,
|
||||
@@ -179,8 +180,7 @@ class CompactionJobStatsTest : public testing::Test,
|
||||
delete h;
|
||||
}
|
||||
handles_.clear();
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
}
|
||||
|
||||
void DestroyAndReopen(const Options& options) {
|
||||
@@ -743,7 +743,7 @@ TEST_P(CompactionJobStatsTest, CompactionJobStatsTest) {
|
||||
}
|
||||
|
||||
ASSERT_OK(Flush(1));
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db_)->TEST_WaitForCompact());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
|
||||
stats_checker->set_verify_next_comp_io_stats(true);
|
||||
std::atomic<bool> first_prepare_write(true);
|
||||
@@ -944,7 +944,7 @@ TEST_P(CompactionJobStatsTest, UniversalCompactionTest) {
|
||||
start_key += key_base) {
|
||||
MakeTableWithKeyValues(&rnd, start_key, start_key + key_base - 1, kKeySize,
|
||||
kValueSize, key_interval, compression_ratio, 1);
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db_)->TEST_WaitForCompact());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
}
|
||||
ASSERT_EQ(stats_checker->NumberOfUnverifiedStats(), 0U);
|
||||
}
|
||||
|
||||
@@ -676,8 +676,8 @@ class CompactionJobTestBase : public testing::Test {
|
||||
&event_logger, false, false, dbname_, &compaction_job_stats_,
|
||||
Env::Priority::USER, nullptr /* IOTracer */,
|
||||
/*manual_compaction_canceled=*/kManualCompactionCanceledFalse,
|
||||
env_->GenerateUniqueId(), DBImpl::GenerateDbSessionId(nullptr),
|
||||
full_history_ts_low_);
|
||||
CompactionJob::kCompactionAbortedFalse, env_->GenerateUniqueId(),
|
||||
DBImpl::GenerateDbSessionId(nullptr), full_history_ts_low_);
|
||||
VerifyInitializationOfCompactionJobStats(compaction_job_stats_);
|
||||
|
||||
compaction_job.Prepare(std::nullopt /*subcompact to be computed*/);
|
||||
@@ -2409,459 +2409,6 @@ TEST_F(CompactionJobIOPriorityTest, GetRateLimiterPriority) {
|
||||
kMaxSequenceNumber, 1, false, {kInvalidBlobFileNumber}, true,
|
||||
Env::IO_LOW, Env::IO_LOW);
|
||||
}
|
||||
|
||||
class ResumableCompactionJobTest : public CompactionJobTestBase {
|
||||
public:
|
||||
ResumableCompactionJobTest()
|
||||
: CompactionJobTestBase(
|
||||
test::PerThreadDBPath("allow_resumption_job_test"),
|
||||
BytewiseComparator(), [](uint64_t /*ts*/) { return ""; },
|
||||
/*test_io_priority=*/false, TableTypeForTest::kBlockBasedTable) {}
|
||||
|
||||
protected:
|
||||
static constexpr const char* kCancelBeforeThisKey = "cancel_before_this_key";
|
||||
std::string progress_dir_;
|
||||
bool enable_cancel_ = false;
|
||||
std::atomic<int> stop_count_{0};
|
||||
std::atomic<bool> cancel_{false};
|
||||
SequenceNumber cancel_before_seqno = kMaxSequenceNumber;
|
||||
|
||||
void SetUp() override {
|
||||
CompactionJobTestBase::SetUp();
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"CompactionOutputs::ShouldStopBefore::manual_decision",
|
||||
[this](void* p) {
|
||||
auto* pair = static_cast<std::pair<bool*, const Slice>*>(p);
|
||||
*(pair->first) = true;
|
||||
|
||||
// Cancel after outputting a specific key
|
||||
if (enable_cancel_) {
|
||||
ParsedInternalKey parsed_key;
|
||||
if (ParseInternalKey(pair->second, &parsed_key, true).ok()) {
|
||||
if (parsed_key.user_key == kCancelBeforeThisKey &&
|
||||
(cancel_before_seqno == kMaxSequenceNumber ||
|
||||
parsed_key.sequence == cancel_before_seqno)) {
|
||||
cancel_.store(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
SyncPoint::GetInstance()->EnableProcessing();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
if (env_->FileExists(progress_dir_).ok()) {
|
||||
std::vector<std::string> files;
|
||||
EXPECT_OK(env_->GetChildren(progress_dir_, &files));
|
||||
for (const auto& file : files) {
|
||||
if (file != "." && file != "..") {
|
||||
EXPECT_OK(env_->DeleteFile(progress_dir_ + "/" + file));
|
||||
}
|
||||
}
|
||||
EXPECT_OK(env_->DeleteDir(progress_dir_));
|
||||
}
|
||||
|
||||
CompactionJobTestBase::TearDown();
|
||||
}
|
||||
|
||||
void NewDB() {
|
||||
if (env_->FileExists(progress_dir_).ok()) {
|
||||
std::vector<std::string> files;
|
||||
EXPECT_OK(env_->GetChildren(progress_dir_, &files));
|
||||
for (const auto& file : files) {
|
||||
if (file != "." && file != "..") {
|
||||
EXPECT_OK(env_->DeleteFile(progress_dir_ + "/" + file));
|
||||
}
|
||||
}
|
||||
EXPECT_OK(env_->DeleteDir(progress_dir_));
|
||||
}
|
||||
|
||||
CompactionJobTestBase::NewDB();
|
||||
|
||||
progress_dir_ = test::PerThreadDBPath("compaction_progress");
|
||||
ASSERT_OK(env_->CreateDirIfMissing(progress_dir_));
|
||||
}
|
||||
|
||||
void EnableCompactionCancel() { enable_cancel_ = true; }
|
||||
|
||||
void DisableCompactionCancel() {
|
||||
enable_cancel_ = false;
|
||||
cancel_.store(false);
|
||||
}
|
||||
|
||||
std::unique_ptr<log::Writer> CreateCompactionProgressWriter(
|
||||
const std::string& compaction_progress_file) {
|
||||
std::unique_ptr<FSWritableFile> file;
|
||||
EXPECT_OK(fs_->NewWritableFile(compaction_progress_file, FileOptions(),
|
||||
&file, nullptr));
|
||||
auto file_writer = std::make_unique<WritableFileWriter>(
|
||||
std::move(file), compaction_progress_file, FileOptions());
|
||||
auto compaction_progress_writer =
|
||||
std::make_unique<log::Writer>(std::move(file_writer), 0, false);
|
||||
return compaction_progress_writer;
|
||||
}
|
||||
|
||||
Status RunCompactionWithProgressTracking(
|
||||
const CompactionProgress& compaction_progress,
|
||||
log::Writer* compaction_progress_writer,
|
||||
std::vector<SequenceNumber> snapshots = {},
|
||||
std::shared_ptr<Statistics> stats = nullptr) {
|
||||
mutex_.Lock();
|
||||
|
||||
auto cfd = versions_->GetColumnFamilySet()->GetDefault();
|
||||
auto files = cfd->current()->storage_info()->LevelFiles(0);
|
||||
|
||||
db_options_.statistics = stats;
|
||||
db_options_.stats = db_options_.statistics.get();
|
||||
|
||||
std::vector<CompactionInputFiles> compaction_input_files;
|
||||
CompactionInputFiles level;
|
||||
level.level = 0;
|
||||
level.files = files;
|
||||
compaction_input_files.push_back(level);
|
||||
|
||||
Compaction compaction(
|
||||
cfd->current()->storage_info(), cfd->ioptions(),
|
||||
cfd->GetLatestMutableCFOptions(), mutable_db_options_,
|
||||
compaction_input_files, 1, mutable_cf_options_.target_file_size_base,
|
||||
mutable_cf_options_.max_compaction_bytes, 0, kNoCompression,
|
||||
cfd->GetLatestMutableCFOptions().compression_opts,
|
||||
Temperature::kUnknown, 0, {}, std::nullopt, nullptr,
|
||||
CompactionReason::kManualCompaction);
|
||||
compaction.FinalizeInputInfo(cfd->current());
|
||||
|
||||
LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, db_options_.info_log.get());
|
||||
EventLogger event_logger(db_options_.info_log.get());
|
||||
JobContext job_context(1, false);
|
||||
job_context.InitSnapshotContext(nullptr, nullptr, kMaxSequenceNumber,
|
||||
std::move(snapshots));
|
||||
CompactionJobStats job_stats;
|
||||
|
||||
CompactionJob compaction_job(
|
||||
0, &compaction, db_options_, mutable_db_options_, env_options_,
|
||||
versions_.get(), &shutting_down_, &log_buffer, nullptr, nullptr,
|
||||
nullptr, stats.get(), &mutex_, &error_handler_, &job_context,
|
||||
table_cache_, &event_logger, false, false, dbname_, &job_stats,
|
||||
Env::Priority::USER, nullptr, cancel_, env_->GenerateUniqueId(),
|
||||
DBImpl::GenerateDbSessionId(nullptr), "");
|
||||
|
||||
compaction_job.Prepare(std::nullopt, compaction_progress,
|
||||
compaction_progress_writer);
|
||||
mutex_.Unlock();
|
||||
|
||||
compaction_job.Run().PermitUncheckedError();
|
||||
EXPECT_OK(compaction_job.io_status());
|
||||
|
||||
mutex_.Lock();
|
||||
|
||||
bool compaction_released = false;
|
||||
Status s = compaction_job.Install(&compaction_released);
|
||||
|
||||
mutex_.Unlock();
|
||||
if (!compaction_released) {
|
||||
compaction.ReleaseCompactionFiles(s);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
SubcompactionProgress ReadAndParseProgress(
|
||||
const std::string& compaction_progress_file) {
|
||||
std::unique_ptr<FSSequentialFile> seq_file;
|
||||
EXPECT_OK(fs_->NewSequentialFile(compaction_progress_file, FileOptions(),
|
||||
&seq_file, nullptr));
|
||||
auto file_reader = std::make_unique<SequentialFileReader>(
|
||||
std::move(seq_file), compaction_progress_file, 0, nullptr);
|
||||
log::Reader reader(nullptr, std::move(file_reader), nullptr, true, 0);
|
||||
|
||||
SubcompactionProgressBuilder builder;
|
||||
std::string record;
|
||||
Slice slice;
|
||||
|
||||
while (reader.ReadRecord(&slice, &record)) {
|
||||
VersionEdit edit;
|
||||
if (!edit.DecodeFrom(slice).ok()) {
|
||||
continue;
|
||||
}
|
||||
builder.ProcessVersionEdit(edit);
|
||||
}
|
||||
|
||||
EXPECT_TRUE(builder.HasAccumulatedSubcompactionProgress());
|
||||
|
||||
return builder.GetAccumulatedSubcompactionProgress();
|
||||
}
|
||||
|
||||
// Test utility function to verify that compaction progress was correctly
|
||||
// persisted to the progress file after compaction interruption.
|
||||
//
|
||||
// VERIFIES:
|
||||
// - Progress file exists and has expected size (empty if no progress
|
||||
// expected)
|
||||
// - Next internal key to compact matches expected user key with proper format
|
||||
// - Number of processed input records matches position in ordered input keys
|
||||
// - Number of processed output records equals number of processed input
|
||||
// records (by test design to simplify verification)
|
||||
// - Each output file contains exactly one user key (by test design to
|
||||
// simplify verification)
|
||||
void VerifyCompactionProgressPersisted(
|
||||
const std::string& compaction_progress_file,
|
||||
const std::string& next_user_key_to_compact,
|
||||
const std::vector<std::string>& ordered_intput_keys) {
|
||||
ASSERT_OK(env_->FileExists(compaction_progress_file));
|
||||
|
||||
uint64_t file_size;
|
||||
ASSERT_OK(env_->GetFileSize(compaction_progress_file, &file_size));
|
||||
|
||||
if (next_user_key_to_compact.empty()) {
|
||||
ASSERT_EQ(file_size, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& subcompaction_progress =
|
||||
ReadAndParseProgress(compaction_progress_file);
|
||||
|
||||
ASSERT_FALSE(subcompaction_progress.next_internal_key_to_compact.empty());
|
||||
ParsedInternalKey parsed_next_key;
|
||||
ASSERT_OK(
|
||||
ParseInternalKey(subcompaction_progress.next_internal_key_to_compact,
|
||||
&parsed_next_key, true /* log_err_key */));
|
||||
ASSERT_EQ(parsed_next_key.user_key, next_user_key_to_compact);
|
||||
ASSERT_EQ(parsed_next_key.sequence, kMaxSequenceNumber);
|
||||
ASSERT_EQ(parsed_next_key.type, kValueTypeForSeek);
|
||||
|
||||
auto it = std::find(ordered_intput_keys.begin(), ordered_intput_keys.end(),
|
||||
next_user_key_to_compact);
|
||||
ASSERT_TRUE(it != ordered_intput_keys.end());
|
||||
|
||||
auto next_key_index = std::distance(ordered_intput_keys.begin(), it);
|
||||
|
||||
ASSERT_EQ(subcompaction_progress.num_processed_input_records,
|
||||
next_key_index);
|
||||
|
||||
ASSERT_EQ(subcompaction_progress.output_level_progress
|
||||
.GetNumProcessedOutputRecords(),
|
||||
next_key_index);
|
||||
|
||||
ASSERT_EQ(
|
||||
subcompaction_progress.output_level_progress.GetOutputFiles().size(),
|
||||
|
||||
next_key_index);
|
||||
|
||||
for (size_t i = 0;
|
||||
i <
|
||||
subcompaction_progress.output_level_progress.GetOutputFiles().size();
|
||||
++i) {
|
||||
const auto& output_file =
|
||||
subcompaction_progress.output_level_progress.GetOutputFiles()[i];
|
||||
ASSERT_EQ(output_file.smallest.user_key().ToString(),
|
||||
output_file.largest.user_key().ToString());
|
||||
ASSERT_EQ(output_file.largest.user_key().ToString(),
|
||||
ordered_intput_keys[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void RunCancelAndResumeTest(
|
||||
const std::initializer_list<mock::KVPair>& input_file_1,
|
||||
const std::initializer_list<mock::KVPair>& input_file_2,
|
||||
uint64_t last_sequence, const std::vector<uint64_t>& snapshots,
|
||||
const std::string& expected_next_key_to_compact,
|
||||
const std::vector<std::string>& expected_input_keys,
|
||||
bool cancelled_past_mid_point = false) {
|
||||
std::shared_ptr<Statistics> stats = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
||||
|
||||
auto file1 = mock::MakeMockFile(input_file_1);
|
||||
AddMockFile(file1);
|
||||
auto file2 = mock::MakeMockFile(input_file_2);
|
||||
AddMockFile(file2);
|
||||
SetLastSequence(last_sequence);
|
||||
|
||||
// First compaction (will be cancelled)
|
||||
std::string compaction_progress_file =
|
||||
CompactionProgressFileName(progress_dir_, 123);
|
||||
std::unique_ptr<log::Writer> compaction_progress_writer =
|
||||
CreateCompactionProgressWriter(compaction_progress_file);
|
||||
|
||||
ASSERT_OK(stats->Reset());
|
||||
EnableCompactionCancel();
|
||||
|
||||
Status status = RunCompactionWithProgressTracking(
|
||||
CompactionProgress{}, compaction_progress_writer.get(), snapshots,
|
||||
stats);
|
||||
|
||||
ASSERT_TRUE(status.IsManualCompactionPaused());
|
||||
DisableCompactionCancel();
|
||||
|
||||
HistogramData cancelled_compaction_stats;
|
||||
stats->histogramData(FILE_WRITE_COMPACTION_MICROS,
|
||||
&cancelled_compaction_stats);
|
||||
|
||||
VerifyCompactionProgressPersisted(compaction_progress_file,
|
||||
expected_next_key_to_compact,
|
||||
expected_input_keys);
|
||||
|
||||
// Resume compaction
|
||||
CompactionProgress compaction_progress;
|
||||
if (expected_next_key_to_compact != "") {
|
||||
compaction_progress.push_back(
|
||||
ReadAndParseProgress(compaction_progress_file));
|
||||
}
|
||||
|
||||
std::string compaction_progress_file_2 =
|
||||
CompactionProgressFileName(progress_dir_, 234);
|
||||
std::unique_ptr<log::Writer> compaction_progress_writer_2 =
|
||||
CreateCompactionProgressWriter(compaction_progress_file_2);
|
||||
|
||||
ASSERT_OK(stats->Reset());
|
||||
|
||||
status = RunCompactionWithProgressTracking(
|
||||
compaction_progress, compaction_progress_writer_2.get(),
|
||||
{} /* snapshots */, stats);
|
||||
|
||||
ASSERT_OK(status);
|
||||
|
||||
if (cancelled_past_mid_point) {
|
||||
HistogramData resumed_compaction_stats;
|
||||
stats->histogramData(FILE_WRITE_COMPACTION_MICROS,
|
||||
&resumed_compaction_stats);
|
||||
ASSERT_GT(cancelled_compaction_stats.count,
|
||||
resumed_compaction_stats.count);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, BasicProgressPersistence) {
|
||||
NewDB();
|
||||
|
||||
auto file1 = mock::MakeMockFile({
|
||||
{KeyStr("a", 1U, kTypeValue), "val1"},
|
||||
{KeyStr("b", 2U, kTypeValue), "val2"},
|
||||
});
|
||||
AddMockFile(file1);
|
||||
|
||||
auto file2 = mock::MakeMockFile({
|
||||
{KeyStr("c", 3U, kTypeValue), "val3"},
|
||||
{KeyStr("d", 4U, kTypeValue), "val4"},
|
||||
});
|
||||
AddMockFile(file2);
|
||||
|
||||
SetLastSequence(4U);
|
||||
|
||||
std::string compaction_progress_file =
|
||||
CompactionProgressFileName(progress_dir_, 123);
|
||||
|
||||
std::unique_ptr<log::Writer> compaction_progress_writer =
|
||||
CreateCompactionProgressWriter(compaction_progress_file);
|
||||
|
||||
Status status = RunCompactionWithProgressTracking(
|
||||
CompactionProgress(), compaction_progress_writer.get());
|
||||
|
||||
ASSERT_OK(status);
|
||||
|
||||
VerifyCompactionProgressPersisted(
|
||||
compaction_progress_file, "d" /* next_user_key_to_compact */,
|
||||
{"a", "b", "c", "d"} /* ordered_intput_keys */);
|
||||
}
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, BasicProgressResume) {
|
||||
NewDB();
|
||||
|
||||
RunCancelAndResumeTest(
|
||||
{{KeyStr("a", 1U, kTypeValue), "val1"},
|
||||
{KeyStr("b", 2U, kTypeValue), "val2"}} /* input_file_1 */,
|
||||
{{KeyStr("bb", 3U, kTypeValue), "val3"},
|
||||
{KeyStr(kCancelBeforeThisKey, 4U, kTypeValue),
|
||||
"val4"}} /* input_file_2 */,
|
||||
4U /* last_sequence */, {} /* snapshots */,
|
||||
kCancelBeforeThisKey /* expected_next_key_to_compact */,
|
||||
{"a", "b", "bb", kCancelBeforeThisKey} /* expected_input_keys */,
|
||||
true /* cancelled_past_mid_point */);
|
||||
}
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, NoProgressResumeOnSameKey) {
|
||||
NewDB();
|
||||
|
||||
// `cancel_before_seqno` is set to 0U to force cancellation after
|
||||
// `kCancelBeforeThisKey@1` instead of `kCancelBeforeThisKey@2`.
|
||||
// The seqno is 0 because `kCancelBeforeThisKey@1` will have its sequence
|
||||
// number zeroed during compaction while `kCancelBeforeThisKey@2` won't be
|
||||
cancel_before_seqno = 0U;
|
||||
RunCancelAndResumeTest(
|
||||
{{KeyStr(kCancelBeforeThisKey, 1U, kTypeValue),
|
||||
"val1"}} /* input_file_1 */,
|
||||
{{KeyStr(kCancelBeforeThisKey, 2U, kTypeValue), "val11"},
|
||||
{KeyStr("d", 3U, kTypeValue), "val2"}} /* input_file_2 */,
|
||||
3U /* last_sequence */, {1U} /* snapshots */,
|
||||
"" /* expected_next_key_to_compact */,
|
||||
{kCancelBeforeThisKey, kCancelBeforeThisKey,
|
||||
"d"} /* expected_input_keys */);
|
||||
}
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, NoProgressResumeOnDeleteRange) {
|
||||
NewDB();
|
||||
|
||||
RunCancelAndResumeTest(
|
||||
{{KeyStr("a", 1U, kTypeValue), "val1"},
|
||||
{KeyStr("b", 2U, kTypeValue), "val2"},
|
||||
{KeyStr(kCancelBeforeThisKey, 3U, kTypeValue),
|
||||
"val3"}} /* input_file_1 */,
|
||||
{{KeyStr(kCancelBeforeThisKey, 4U, kTypeRangeDeletion),
|
||||
"range_deletion_end_key"},
|
||||
{KeyStr("d", 5U, kTypeValue), "val4"}} /* input_file_2 */,
|
||||
5U /* last_sequence */, {3U} /* snapshots */,
|
||||
"b" /* expected_next_key_to_compact */,
|
||||
{"a", "b", kCancelBeforeThisKey, kCancelBeforeThisKey,
|
||||
"d"} /* expected_input_keys */);
|
||||
}
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, NoProgressResumeOnMerge) {
|
||||
merge_op_ = MergeOperators::CreateStringAppendOperator();
|
||||
NewDB();
|
||||
|
||||
RunCancelAndResumeTest(
|
||||
{{KeyStr("a", 1U, kTypeValue), "val1"},
|
||||
{KeyStr("b", 2U, kTypeValue), "val2"}} /* input_file_1 */,
|
||||
{{KeyStr("bb", 3U, kTypeValue), "val3"},
|
||||
{KeyStr(kCancelBeforeThisKey, 4U, kTypeMerge),
|
||||
"val4"}} /* input_file_2 */,
|
||||
4U /* last_sequence */, {} /* snapshots */,
|
||||
"bb" /* expected_next_key_to_compact */,
|
||||
{"a", "b", "bb", kCancelBeforeThisKey} /* expected_input_keys */);
|
||||
}
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, NoProgressResumeOnSingleDelete) {
|
||||
NewDB();
|
||||
|
||||
RunCancelAndResumeTest(
|
||||
{{KeyStr("a", 1U, kTypeValue), "val1"},
|
||||
{KeyStr("b", 2U, kTypeValue), "val2"},
|
||||
{KeyStr(kCancelBeforeThisKey, 3U, kTypeValue),
|
||||
"val3"}} /* input_file_1 */,
|
||||
{{KeyStr(kCancelBeforeThisKey, 4U, kTypeSingleDeletion), ""},
|
||||
{KeyStr("d", 5U, kTypeValue), "val4"}} /* input_file_2 */,
|
||||
5U /* last_sequence */, {3U} /* snapshots */,
|
||||
"b" /* expected_next_key_to_compact */,
|
||||
{"a", "b", kCancelBeforeThisKey, kCancelBeforeThisKey,
|
||||
"d"} /* expected_input_keys */);
|
||||
}
|
||||
|
||||
TEST_F(ResumableCompactionJobTest, NoProgressResumeOnDeletionAtBottom) {
|
||||
NewDB();
|
||||
|
||||
RunCancelAndResumeTest(
|
||||
{{KeyStr("a", 1U, kTypeValue), "val1"},
|
||||
{KeyStr("b", 2U, kTypeValue), "val2"},
|
||||
{KeyStr(kCancelBeforeThisKey, 3U, kTypeValue),
|
||||
"val3"}} /* input_file_1 */,
|
||||
{{KeyStr(kCancelBeforeThisKey, 4U, kTypeDeletion), ""},
|
||||
{KeyStr("d", 5U, kTypeValue), "val4"}} /* input_file_2 */,
|
||||
5U /* last_sequence */, {3U} /* snapshots */,
|
||||
"b" /* expected_next_key_to_compact */,
|
||||
{"a", "b", kCancelBeforeThisKey, kCancelBeforeThisKey,
|
||||
"d"} /* expected_input_keys */);
|
||||
}
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -49,8 +49,10 @@ Status CompactionOutputs::Finish(
|
||||
meta->fd.file_size = current_bytes;
|
||||
meta->tail_size = builder_->GetTailSize();
|
||||
meta->marked_for_compaction = builder_->NeedCompact();
|
||||
meta->user_defined_timestamps_persisted = static_cast<bool>(
|
||||
builder_->GetTableProperties().user_defined_timestamps_persisted);
|
||||
const TableProperties& tp = builder_->GetTableProperties();
|
||||
meta->user_defined_timestamps_persisted =
|
||||
static_cast<bool>(tp.user_defined_timestamps_persisted);
|
||||
ExtractTimestampFromTableProperties(tp, meta);
|
||||
}
|
||||
current_output().finished = true;
|
||||
stats_.bytes_written += current_bytes;
|
||||
|
||||
@@ -84,6 +84,19 @@ class CompactionOutputs {
|
||||
|
||||
bool HasBlobFileAdditions() const { return !blob_file_additions_.empty(); }
|
||||
|
||||
// Get all file paths (SST and blob) created during compaction.
|
||||
const std::vector<std::string>& GetOutputFilePaths() const {
|
||||
return output_file_paths_;
|
||||
}
|
||||
|
||||
std::vector<std::string>* GetOutputFilePathsPtr() {
|
||||
return &output_file_paths_;
|
||||
}
|
||||
|
||||
void AddOutputFilePath(const std::string& path) {
|
||||
output_file_paths_.push_back(path);
|
||||
}
|
||||
|
||||
BlobGarbageMeter* CreateBlobGarbageMeter() {
|
||||
assert(!is_proximal_level_);
|
||||
blob_garbage_meter_ = std::make_unique<BlobGarbageMeter>();
|
||||
@@ -321,6 +334,12 @@ class CompactionOutputs {
|
||||
std::vector<BlobFileAddition> blob_file_additions_;
|
||||
std::unique_ptr<BlobGarbageMeter> blob_garbage_meter_;
|
||||
|
||||
// All file paths (SST and blob) created during compaction.
|
||||
// Used for cleanup on abort - ensures orphan files are deleted even if
|
||||
// they were removed from outputs_ or blob_file_additions_ (e.g., by
|
||||
// RemoveLastEmptyOutput when file_size is 0 because builder was abandoned).
|
||||
std::vector<std::string> output_file_paths_;
|
||||
|
||||
// Per level's output stat
|
||||
InternalStats::CompactionStats stats_;
|
||||
|
||||
|
||||
@@ -27,12 +27,68 @@
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
bool FindIntraL0Compaction(const std::vector<FileMetaData*>& level_files,
|
||||
size_t min_files_to_compact,
|
||||
uint64_t max_compact_bytes_per_del_file,
|
||||
uint64_t max_compaction_bytes,
|
||||
CompactionInputFiles* comp_inputs) {
|
||||
TEST_SYNC_POINT("FindIntraL0Compaction");
|
||||
#ifndef NDEBUG
|
||||
static void AssertCleanCut(const InternalKeyComparator* icmp,
|
||||
VersionStorageInfo* vstorage,
|
||||
CompactionInputFiles* inputs, int level,
|
||||
Logger* logger) {
|
||||
const std::vector<FileMetaData*>& level_files = vstorage->LevelFiles(level);
|
||||
if (inputs->files.empty() || level_files.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Comparator* ucmp = icmp->user_comparator();
|
||||
|
||||
// Find first and last input file indices in level
|
||||
int first_input_idx = -1;
|
||||
int last_input_idx = -1;
|
||||
for (size_t i = 0; i < level_files.size(); i++) {
|
||||
if (level_files[i] == inputs->files.front()) {
|
||||
first_input_idx = static_cast<int>(i);
|
||||
}
|
||||
if (level_files[i] == inputs->files.back()) {
|
||||
last_input_idx = static_cast<int>(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Check file before first input
|
||||
if (first_input_idx > 0) {
|
||||
const FileMetaData* prev_file = level_files[first_input_idx - 1];
|
||||
const FileMetaData* first_file = inputs->files.front();
|
||||
int cmp = sstableKeyCompare(ucmp, prev_file->largest, first_file->smallest);
|
||||
if (cmp == 0) {
|
||||
ROCKS_LOG_ERROR(logger,
|
||||
"Clean cut violated: L%d unselected file %" PRIu64
|
||||
" adjacent to first selected file %" PRIu64,
|
||||
level, prev_file->fd.GetNumber(),
|
||||
first_file->fd.GetNumber());
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Check file after last input
|
||||
if (last_input_idx >= 0 &&
|
||||
static_cast<size_t>(last_input_idx) < level_files.size() - 1) {
|
||||
const FileMetaData* last_file = inputs->files.back();
|
||||
const FileMetaData* next_file = level_files[last_input_idx + 1];
|
||||
int cmp = sstableKeyCompare(ucmp, last_file->largest, next_file->smallest);
|
||||
if (cmp == 0) {
|
||||
ROCKS_LOG_ERROR(logger,
|
||||
"Clean cut violated: L%d unselected file %" PRIu64
|
||||
" adjacent to last selected file %" PRIu64,
|
||||
level, next_file->fd.GetNumber(),
|
||||
last_file->fd.GetNumber());
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // NDEBUG
|
||||
|
||||
bool PickCostBasedIntraL0Compaction(
|
||||
const std::vector<FileMetaData*>& level_files, size_t min_files_to_compact,
|
||||
uint64_t max_compact_bytes_per_del_file, uint64_t max_compaction_bytes,
|
||||
CompactionInputFiles* comp_inputs) {
|
||||
TEST_SYNC_POINT("PickCostBasedIntraL0Compaction");
|
||||
|
||||
size_t start = 0;
|
||||
|
||||
@@ -250,6 +306,10 @@ bool CompactionPicker::ExpandInputsToCleanCut(const std::string& /*cf_name*/,
|
||||
// inputs. thus, inputs should be non-empty here
|
||||
assert(!inputs->empty());
|
||||
|
||||
#ifndef NDEBUG
|
||||
AssertCleanCut(icmp_, vstorage, inputs, level, ioptions_.logger);
|
||||
#endif // NDEBUG
|
||||
|
||||
// If, after the expansion, there are files that are already under
|
||||
// compaction, then we must drop/cancel this compaction.
|
||||
if (AreFilesInCompaction(inputs->files)) {
|
||||
@@ -611,7 +671,8 @@ Compaction* CompactionPicker::PickCompactionForCompactRange(
|
||||
int input_level, int output_level,
|
||||
const CompactRangeOptions& compact_range_options, const InternalKey* begin,
|
||||
const InternalKey* end, InternalKey** compaction_end, bool* manual_conflict,
|
||||
uint64_t max_file_num_to_ignore, const std::string& trim_ts) {
|
||||
uint64_t max_file_num_to_ignore, const std::string& trim_ts,
|
||||
const std::string& full_history_ts_low) {
|
||||
// CompactionPickerFIFO has its own implementation of compact range
|
||||
assert(ioptions_.compaction_style != kCompactionStyleFIFO);
|
||||
|
||||
@@ -690,7 +751,8 @@ Compaction* CompactionPicker::PickCompactionForCompactRange(
|
||||
compact_range_options.blob_garbage_collection_age_cutoff);
|
||||
|
||||
RegisterCompaction(c);
|
||||
vstorage->ComputeCompactionScore(ioptions_, mutable_cf_options);
|
||||
vstorage->ComputeCompactionScore(ioptions_, mutable_cf_options,
|
||||
full_history_ts_low);
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -887,7 +949,8 @@ Compaction* CompactionPicker::PickCompactionForCompactRange(
|
||||
// takes running compactions into account (by skipping files that are already
|
||||
// being compacted). Since we just changed compaction score, we recalculate it
|
||||
// here
|
||||
vstorage->ComputeCompactionScore(ioptions_, mutable_cf_options);
|
||||
vstorage->ComputeCompactionScore(ioptions_, mutable_cf_options,
|
||||
full_history_ts_low);
|
||||
|
||||
return compaction;
|
||||
}
|
||||
|
||||
@@ -65,7 +65,8 @@ class CompactionPicker {
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
const std::vector<SequenceNumber>& existing_snapshots,
|
||||
const SnapshotChecker* snapshot_checker, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer, bool require_max_output_level) = 0;
|
||||
LogBuffer* log_buffer, const std::string& full_history_ts_low,
|
||||
bool require_max_output_level = false) = 0;
|
||||
|
||||
// The returned Compaction might not include the whole requested range.
|
||||
// In that case, compaction_end will be set to the next key that needs
|
||||
@@ -82,7 +83,8 @@ class CompactionPicker {
|
||||
const CompactRangeOptions& compact_range_options,
|
||||
const InternalKey* begin, const InternalKey* end,
|
||||
InternalKey** compaction_end, bool* manual_conflict,
|
||||
uint64_t max_file_num_to_ignore, const std::string& trim_ts);
|
||||
uint64_t max_file_num_to_ignore, const std::string& trim_ts,
|
||||
const std::string& full_history_ts_low);
|
||||
|
||||
// The maximum allowed output level. Default value is NumberLevels() - 1.
|
||||
virtual int MaxOutputLevel() const { return NumberLevels() - 1; }
|
||||
@@ -284,7 +286,8 @@ class NullCompactionPicker : public CompactionPicker {
|
||||
const std::vector<SequenceNumber>& /*existing_snapshots*/,
|
||||
const SnapshotChecker* /*snapshot_checker*/,
|
||||
VersionStorageInfo* /*vstorage*/, LogBuffer* /* log_buffer */,
|
||||
bool /*require_max_output_level*/ = false) override {
|
||||
const std::string& /*full_history_ts_low*/,
|
||||
bool /*require_max_output_level*/) override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -298,8 +301,8 @@ class NullCompactionPicker : public CompactionPicker {
|
||||
const CompactRangeOptions& /*compact_range_options*/,
|
||||
const InternalKey* /*begin*/, const InternalKey* /*end*/,
|
||||
InternalKey** /*compaction_end*/, bool* /*manual_conflict*/,
|
||||
uint64_t /*max_file_num_to_ignore*/,
|
||||
const std::string& /*trim_ts*/) override {
|
||||
uint64_t /*max_file_num_to_ignore*/, const std::string& /*trim_ts*/,
|
||||
const std::string& /*full_history_ts_low*/) override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -325,11 +328,10 @@ class NullCompactionPicker : public CompactionPicker {
|
||||
// files. Cannot be nullptr.
|
||||
//
|
||||
// @return true iff compaction was found.
|
||||
bool FindIntraL0Compaction(const std::vector<FileMetaData*>& level_files,
|
||||
size_t min_files_to_compact,
|
||||
uint64_t max_compact_bytes_per_del_file,
|
||||
uint64_t max_compaction_bytes,
|
||||
CompactionInputFiles* comp_inputs);
|
||||
bool PickCostBasedIntraL0Compaction(
|
||||
const std::vector<FileMetaData*>& level_files, size_t min_files_to_compact,
|
||||
uint64_t max_compact_bytes_per_del_file, uint64_t max_compaction_bytes,
|
||||
CompactionInputFiles* comp_inputs);
|
||||
|
||||
CompressionType GetCompressionType(const VersionStorageInfo* vstorage,
|
||||
const MutableCFOptions& mutable_cf_options,
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "db/compaction/compaction_picker_fifo.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -31,6 +32,29 @@ uint64_t GetTotalFilesSize(const std::vector<FileMetaData*>& files) {
|
||||
}
|
||||
return total_size;
|
||||
}
|
||||
|
||||
// Compute effective data size and capacity limit for FIFO compaction.
|
||||
// When max_data_files_size > 0 (blob-aware mode), the effective size includes
|
||||
// both SST and blob file sizes, and the limit is max_data_files_size.
|
||||
// Otherwise, only SST sizes are used with max_table_files_size as the limit.
|
||||
void GetEffectiveSizeAndLimit(const CompactionOptionsFIFO& fifo_opts,
|
||||
uint64_t total_sst_size, uint64_t total_blob_size,
|
||||
uint64_t* effective_size,
|
||||
uint64_t* effective_max) {
|
||||
*effective_size = total_sst_size;
|
||||
*effective_max = fifo_opts.max_table_files_size;
|
||||
if (fifo_opts.max_data_files_size > 0) {
|
||||
*effective_size += total_blob_size;
|
||||
*effective_max = fifo_opts.max_data_files_size;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the effective capacity limit for FIFO compaction.
|
||||
// Convenience wrapper when only the limit is needed (e.g., PickTTLCompaction).
|
||||
uint64_t GetEffectiveMax(const CompactionOptionsFIFO& fifo_opts) {
|
||||
return fifo_opts.max_data_files_size > 0 ? fifo_opts.max_data_files_size
|
||||
: fifo_opts.max_table_files_size;
|
||||
}
|
||||
} // anonymous namespace
|
||||
|
||||
bool FIFOCompactionPicker::NeedsCompaction(
|
||||
@@ -98,10 +122,43 @@ Compaction* FIFOCompactionPicker::PickTTLCompaction(
|
||||
// Return a nullptr and proceed to size-based FIFO compaction if:
|
||||
// 1. there are no files older than ttl OR
|
||||
// 2. there are a few files older than ttl, but deleting them will not bring
|
||||
// the total size to be less than max_table_files_size threshold.
|
||||
if (inputs[0].files.empty() ||
|
||||
total_size >
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size) {
|
||||
// the total size to be less than the size threshold.
|
||||
uint64_t effective_max =
|
||||
GetEffectiveMax(mutable_cf_options.compaction_options_fifo);
|
||||
// Estimate the effective remaining data after dropping TTL-expired SSTs.
|
||||
// Each dropped SST also frees a proportional share of blob data.
|
||||
//
|
||||
// In multi-level FIFO (migration), we must use total SST across ALL levels
|
||||
// as the reference, because total_blob covers all levels. Using only L0
|
||||
// SST would inflate the blob estimate.
|
||||
uint64_t effective_remaining = total_size;
|
||||
if (mutable_cf_options.compaction_options_fifo.max_data_files_size > 0) {
|
||||
uint64_t total_blob = vstorage->GetBlobStats().total_file_size;
|
||||
// Compute total SST across all levels so the reference scope matches
|
||||
// total_blob's scope (all levels).
|
||||
uint64_t total_sst_all_levels = GetTotalFilesSize(level_files);
|
||||
for (int level = 1; level < vstorage->num_levels(); ++level) {
|
||||
total_sst_all_levels += GetTotalFilesSize(vstorage->LevelFiles(level));
|
||||
}
|
||||
// remaining_sst_all = total_sst_all - dropped_l0_sst
|
||||
// total_size is the remaining L0 SST after removing expired files;
|
||||
// original L0 SST minus remaining L0 SST = dropped.
|
||||
uint64_t original_l0_sst = GetTotalFilesSize(level_files);
|
||||
uint64_t dropped_sst = original_l0_sst - total_size;
|
||||
uint64_t remaining_sst_all = total_sst_all_levels - dropped_sst;
|
||||
// Proportional blob estimate: each SST byte "owns" a proportional
|
||||
// share of blob bytes. Both reference sizes must come from the same
|
||||
// scope (all levels) to avoid inflated estimates.
|
||||
if (total_sst_all_levels > 0 && total_blob > 0) {
|
||||
effective_remaining =
|
||||
remaining_sst_all +
|
||||
static_cast<uint64_t>(static_cast<double>(remaining_sst_all) /
|
||||
total_sst_all_levels * total_blob);
|
||||
} else {
|
||||
effective_remaining = remaining_sst_all;
|
||||
}
|
||||
}
|
||||
if (inputs[0].files.empty() || effective_remaining > effective_max) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -151,7 +208,9 @@ Compaction* FIFOCompactionPicker::PickSizeCompaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer) {
|
||||
// compute the total size and identify the last non-empty level
|
||||
const auto& fifo_opts = mutable_cf_options.compaction_options_fifo;
|
||||
|
||||
// compute the total SST size and identify the last non-empty level
|
||||
int last_level = 0;
|
||||
uint64_t total_size = 0;
|
||||
for (int level = 0; level < vstorage->num_levels(); ++level) {
|
||||
@@ -164,52 +223,13 @@ Compaction* FIFOCompactionPicker::PickSizeCompaction(
|
||||
const std::vector<FileMetaData*>& last_level_files =
|
||||
vstorage->LevelFiles(last_level);
|
||||
|
||||
if (last_level == 0 &&
|
||||
total_size <=
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size) {
|
||||
// total size not exceeded, try to find intra level 0 compaction if enabled
|
||||
const std::vector<FileMetaData*>& level0_files = vstorage->LevelFiles(0);
|
||||
if (mutable_cf_options.compaction_options_fifo.allow_compaction &&
|
||||
level0_files.size() > 0) {
|
||||
CompactionInputFiles comp_inputs;
|
||||
// try to prevent same files from being compacted multiple times, which
|
||||
// could produce large files that may never TTL-expire. Achieve this by
|
||||
// disallowing compactions with files larger than memtable (inflate its
|
||||
// size by 10% to account for uncompressed L0 files that may have size
|
||||
// slightly greater than memtable size limit).
|
||||
size_t max_compact_bytes_per_del_file =
|
||||
static_cast<size_t>(MultiplyCheckOverflow(
|
||||
static_cast<uint64_t>(mutable_cf_options.write_buffer_size),
|
||||
1.1));
|
||||
if (FindIntraL0Compaction(
|
||||
level0_files,
|
||||
mutable_cf_options
|
||||
.level0_file_num_compaction_trigger /* min_files_to_compact */
|
||||
,
|
||||
max_compact_bytes_per_del_file,
|
||||
mutable_cf_options.max_compaction_bytes, &comp_inputs)) {
|
||||
Compaction* c = new Compaction(
|
||||
vstorage, ioptions_, mutable_cf_options, mutable_db_options,
|
||||
{comp_inputs}, 0, 16 * 1024 * 1024 /* output file size limit */,
|
||||
0 /* max compaction bytes, not applicable */,
|
||||
0 /* output path ID */, mutable_cf_options.compression,
|
||||
mutable_cf_options.compression_opts, Temperature::kUnknown,
|
||||
0 /* max_subcompactions */, {},
|
||||
/* earliest_snapshot */ std::nullopt,
|
||||
/* snapshot_checker */ nullptr,
|
||||
CompactionReason::kFIFOReduceNumFiles,
|
||||
/* trim_ts */ "", vstorage->CompactionScore(0),
|
||||
/* l0_files_might_overlap */ true);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
// Compute effective size and limit for comparison.
|
||||
uint64_t effective_size, effective_max;
|
||||
GetEffectiveSizeAndLimit(fifo_opts, total_size,
|
||||
vstorage->GetBlobStats().total_file_size,
|
||||
&effective_size, &effective_max);
|
||||
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer,
|
||||
"[%s] FIFO compaction: nothing to do. Total size %" PRIu64
|
||||
", max size %" PRIu64 "\n",
|
||||
cf_name.c_str(), total_size,
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size);
|
||||
if (last_level == 0 && effective_size <= effective_max) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -227,11 +247,29 @@ Compaction* FIFOCompactionPicker::PickSizeCompaction(
|
||||
inputs[0].level = last_level;
|
||||
|
||||
if (last_level == 0) {
|
||||
// When using blob-aware sizing, use proportional estimation (same
|
||||
// principle as EstimateTotalDataForSST): each SST "owns"
|
||||
// effective_size / num_files of total data. This is an approximation
|
||||
// — individual SSTs may reference different amounts of blob data,
|
||||
// but uniform distribution is a reasonable estimate for FIFO dropping.
|
||||
uint64_t remaining_size = effective_size;
|
||||
const uint64_t num_files = last_level_files.size();
|
||||
// Proportional estimate of data per file (SST + blob).
|
||||
// Use max(1) to prevent stalling when effective_size < num_files.
|
||||
const uint64_t data_per_file =
|
||||
(fifo_opts.max_data_files_size > 0 && num_files > 0)
|
||||
? std::max(effective_size / num_files, uint64_t{1})
|
||||
: 0;
|
||||
|
||||
// In L0, right-most files are the oldest files.
|
||||
for (auto ritr = last_level_files.rbegin(); ritr != last_level_files.rend();
|
||||
++ritr) {
|
||||
auto f = *ritr;
|
||||
total_size -= f->fd.file_size;
|
||||
if (fifo_opts.max_data_files_size > 0) {
|
||||
remaining_size -= std::min(remaining_size, data_per_file);
|
||||
} else {
|
||||
remaining_size -= std::min(remaining_size, f->fd.file_size);
|
||||
}
|
||||
inputs[0].files.push_back(f);
|
||||
char tmp_fsize[16];
|
||||
AppendHumanBytes(f->fd.GetFileSize(), tmp_fsize, sizeof(tmp_fsize));
|
||||
@@ -239,13 +277,11 @@ Compaction* FIFOCompactionPicker::PickSizeCompaction(
|
||||
"[%s] FIFO compaction: picking file %" PRIu64
|
||||
" with size %s for deletion",
|
||||
cf_name.c_str(), f->fd.GetNumber(), tmp_fsize);
|
||||
if (total_size <=
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size) {
|
||||
if (remaining_size <= effective_max) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (total_size >
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size) {
|
||||
} else if (effective_size > effective_max) {
|
||||
// If the last level is non-L0, we actually don't know which file is
|
||||
// logically the oldest since the file creation time only represents
|
||||
// when this file was compacted to this level, which is independent
|
||||
@@ -255,34 +291,36 @@ Compaction* FIFOCompactionPicker::PickSizeCompaction(
|
||||
// file with the smallest key will be deleted first. This design decision
|
||||
// better serves a major type of FIFO use cases where smaller keys are
|
||||
// associated with older data.
|
||||
const uint64_t num_files = last_level_files.size();
|
||||
// Proportional estimate of data per file (SST + blob), same as L0 path.
|
||||
const uint64_t data_per_file =
|
||||
(fifo_opts.max_data_files_size > 0 && num_files > 0)
|
||||
? std::max(effective_size / num_files, uint64_t{1})
|
||||
: 0;
|
||||
for (const auto& f : last_level_files) {
|
||||
if (f->being_compacted) {
|
||||
continue;
|
||||
}
|
||||
total_size -= f->fd.file_size;
|
||||
if (fifo_opts.max_data_files_size > 0) {
|
||||
effective_size -= std::min(effective_size, data_per_file);
|
||||
} else {
|
||||
effective_size -= std::min(effective_size, f->fd.file_size);
|
||||
}
|
||||
inputs[0].files.push_back(f);
|
||||
char tmp_fsize[16];
|
||||
AppendHumanBytes(f->fd.GetFileSize(), tmp_fsize, sizeof(tmp_fsize));
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer,
|
||||
"[%s] FIFO compaction: picking file %" PRIu64
|
||||
" with size %s for deletion under total size %" PRIu64
|
||||
" vs max table files size %" PRIu64,
|
||||
cf_name.c_str(), f->fd.GetNumber(), tmp_fsize, total_size,
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size);
|
||||
ROCKS_LOG_BUFFER(log_buffer,
|
||||
"[%s] FIFO compaction: picking file %" PRIu64
|
||||
" with size %s for deletion under total size %" PRIu64
|
||||
" vs max size %" PRIu64,
|
||||
cf_name.c_str(), f->fd.GetNumber(), tmp_fsize,
|
||||
effective_size, effective_max);
|
||||
|
||||
if (total_size <=
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size) {
|
||||
if (effective_size <= effective_max) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer,
|
||||
"[%s] FIFO compaction: nothing to do. Total size %" PRIu64
|
||||
", max size %" PRIu64 "\n",
|
||||
cf_name.c_str(), total_size,
|
||||
mutable_cf_options.compaction_options_fifo.max_table_files_size);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -419,12 +457,282 @@ Compaction* FIFOCompactionPicker::PickTemperatureChangeCompaction(
|
||||
return c;
|
||||
}
|
||||
|
||||
Compaction* FIFOCompactionPicker::PickIntraL0Compaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer) {
|
||||
const auto& fifo_opts = mutable_cf_options.compaction_options_fifo;
|
||||
|
||||
if (!fifo_opts.allow_compaction) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::vector<FileMetaData*>& level0_files = vstorage->LevelFiles(0);
|
||||
if (level0_files.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (fifo_opts.use_kv_ratio_compaction) {
|
||||
if (fifo_opts.max_data_files_size == 0) {
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer,
|
||||
"[%s] FIFO kv-ratio compaction: skipping — "
|
||||
"max_data_files_size is 0, cannot compute target file size. ",
|
||||
cf_name.c_str());
|
||||
} else if (fifo_opts.max_data_files_size < fifo_opts.max_table_files_size) {
|
||||
ROCKS_LOG_BUFFER(log_buffer,
|
||||
"[%s] FIFO kv-ratio compaction: skipping — "
|
||||
"max_data_files_size (%" PRIu64
|
||||
") < max_table_files_size "
|
||||
"(%" PRIu64 ").",
|
||||
cf_name.c_str(), fifo_opts.max_data_files_size,
|
||||
fifo_opts.max_table_files_size);
|
||||
} else {
|
||||
return PickRatioBasedIntraL0Compaction(cf_name, mutable_cf_options,
|
||||
mutable_db_options, vstorage,
|
||||
log_buffer);
|
||||
}
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer, "[%s] FIFO: falling back to cost-based intra-L0 compaction",
|
||||
cf_name.c_str());
|
||||
}
|
||||
|
||||
// Old intra-L0 path: merge small files using PickCostBasedIntraL0Compaction.
|
||||
// Minimum files to compact follows level0_file_num_compaction_trigger.
|
||||
// Try to prevent same files from being compacted multiple times, which
|
||||
// could produce large files that may never TTL-expire. Achieve this by
|
||||
// disallowing compactions with files larger than memtable (inflate its
|
||||
// size by 10% to account for uncompressed L0 files that may have size
|
||||
// slightly greater than memtable size limit).
|
||||
|
||||
CompactionInputFiles comp_inputs;
|
||||
size_t max_compact_bytes_per_del_file =
|
||||
static_cast<size_t>(MultiplyCheckOverflow(
|
||||
static_cast<uint64_t>(mutable_cf_options.write_buffer_size), 1.1));
|
||||
if (PickCostBasedIntraL0Compaction(
|
||||
level0_files,
|
||||
mutable_cf_options
|
||||
.level0_file_num_compaction_trigger /* min_files_to_compact */,
|
||||
max_compact_bytes_per_del_file,
|
||||
mutable_cf_options.max_compaction_bytes, &comp_inputs)) {
|
||||
Compaction* c = new Compaction(
|
||||
vstorage, ioptions_, mutable_cf_options, mutable_db_options,
|
||||
{comp_inputs}, 0, 16 * 1024 * 1024 /* output file size limit */,
|
||||
0 /* max compaction bytes, not applicable */, 0 /* output path ID */,
|
||||
mutable_cf_options.compression, mutable_cf_options.compression_opts,
|
||||
Temperature::kUnknown, 0 /* max_subcompactions */, {},
|
||||
/* earliest_snapshot */ std::nullopt,
|
||||
/* snapshot_checker */ nullptr, CompactionReason::kFIFOReduceNumFiles,
|
||||
/* trim_ts */ "", vstorage->CompactionScore(0),
|
||||
/* l0_files_might_overlap */ true);
|
||||
return c;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Compaction* FIFOCompactionPicker::PickRatioBasedIntraL0Compaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer) {
|
||||
const auto& fifo_opts = mutable_cf_options.compaction_options_fifo;
|
||||
assert(fifo_opts.use_kv_ratio_compaction);
|
||||
assert(fifo_opts.max_data_files_size > 0);
|
||||
|
||||
// During migration from level/universal compaction to FIFO, non-L0 levels
|
||||
// may still contain files. The ratio-based algorithm only operates on L0,
|
||||
// so skip it until PickSizeCompaction has drained all non-L0 levels.
|
||||
// Once levels collapse to L0-only, this algorithm will kick in.
|
||||
for (int level = 1; level < vstorage->num_levels(); ++level) {
|
||||
if (!vstorage->LevelFiles(level).empty()) {
|
||||
ROCKS_LOG_BUFFER(log_buffer,
|
||||
"[%s] FIFO kv-ratio compaction: skipping — non-L0 "
|
||||
"level %d still has %" ROCKSDB_PRIszt
|
||||
" files (migration in progress)",
|
||||
cf_name.c_str(), level,
|
||||
vstorage->LevelFiles(level).size());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (!level0_compactions_in_progress_.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::vector<FileMetaData*>& level0_files = vstorage->LevelFiles(0);
|
||||
if (mutable_cf_options.level0_file_num_compaction_trigger <= 1) {
|
||||
// trigger <= 0 is invalid; trigger == 1 means compact after every flush,
|
||||
// which doesn't make sense for tiered merging (the tier boundary loop
|
||||
// divides by trigger, so trigger == 1 would cause an infinite loop).
|
||||
return nullptr;
|
||||
}
|
||||
const size_t trigger = static_cast<size_t>(
|
||||
mutable_cf_options.level0_file_num_compaction_trigger);
|
||||
if (level0_files.size() < trigger) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Determine the target compacted file size.
|
||||
//
|
||||
// When max_compaction_bytes > 0 (explicitly set by user), use it directly
|
||||
// as the target. This allows users to override the auto-calculated value.
|
||||
//
|
||||
// When max_compaction_bytes == 0 (default), auto-calculate from the data
|
||||
// capacity and observed SST/blob ratio:
|
||||
// target = max_data_files_size * sst_ratio / trigger
|
||||
//
|
||||
// This is recomputed on every PickCompaction call. The computation is
|
||||
// trivial (sum file sizes + arithmetic) and PickCompaction is only called
|
||||
// once per flush or compaction completion, so no caching is needed.
|
||||
uint64_t target = 0;
|
||||
if (mutable_cf_options.max_compaction_bytes > 0) {
|
||||
// User explicitly set max_compaction_bytes — use it as target
|
||||
target = mutable_cf_options.max_compaction_bytes;
|
||||
} else {
|
||||
// Auto-calculate from capacity and observed SST/blob ratio
|
||||
uint64_t total_sst = GetTotalFilesSize(level0_files);
|
||||
uint64_t total_blob = vstorage->GetBlobStats().total_file_size;
|
||||
uint64_t total_data = total_sst + total_blob;
|
||||
|
||||
if (total_data == 0 || total_sst == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Compute sst_ratio (inverse of EstimateTotalDataForSST's proportion):
|
||||
// when no blob files exist, sst_ratio is 1.0 and the target becomes
|
||||
// max_data_files_size / trigger, which is large. The algorithm will
|
||||
// naturally not find small enough files to compact.
|
||||
double sst_ratio =
|
||||
(total_blob > 0) ? static_cast<double>(total_sst) / total_data : 1.0;
|
||||
|
||||
uint64_t total_sst_at_cap =
|
||||
static_cast<uint64_t>(fifo_opts.max_data_files_size * sst_ratio);
|
||||
target = total_sst_at_cap / trigger;
|
||||
|
||||
ROCKS_LOG_BUFFER(log_buffer,
|
||||
"[%s] FIFO ratio-based compaction: sst_ratio=%.4f, "
|
||||
"target_file_size=%" PRIu64,
|
||||
cf_name.c_str(), sst_ratio, target);
|
||||
}
|
||||
if (target == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Tiered size-based file selection.
|
||||
//
|
||||
// Tier boundaries form a geometric sequence descending from target:
|
||||
// ..., target/trigger^2, target/trigger, target
|
||||
// For each boundary (smallest first), find contiguous L0 files with
|
||||
// size < boundary. If their accumulated bytes >= boundary, merge them.
|
||||
// The output (~boundary bytes) advances to the next tier. Files that
|
||||
// reach target are "graduated" and never compacted again.
|
||||
//
|
||||
// Trade-off: write amplification vs L0 file count.
|
||||
//
|
||||
// Write amp: O(log(target/flush) / log(trigger)) per byte, instead of
|
||||
// O(target / (trigger * flush)) from flat merging. Each byte is
|
||||
// rewritten once per tier crossing.
|
||||
//
|
||||
// L0 file count: trigger + k * (trigger - 1) at steady state, where
|
||||
// k = ceil(log(target/flush) / log(trigger)). This is higher than
|
||||
// the original trigger target because intermediate tier files
|
||||
// accumulate while waiting for the next tier merge. The trade-off
|
||||
// is explicit: more L0 files in exchange for logarithmic (instead
|
||||
// of linear) write amplification.
|
||||
|
||||
// Build tier boundaries from smallest to largest.
|
||||
// Stop at 10KB minimum — SST files of most workloads are larger than
|
||||
// this, so lower boundaries would only waste CPU scanning L0 files.
|
||||
// Files smaller than the lowest boundary simply merge at that boundary.
|
||||
static constexpr uint64_t kMinTierBoundary = 10 * 1024; // 10KB
|
||||
std::vector<uint64_t> boundaries;
|
||||
for (uint64_t b = target; b >= kMinTierBoundary; b /= trigger) {
|
||||
boundaries.push_back(b);
|
||||
}
|
||||
if (boundaries.empty()) {
|
||||
// target itself is below kMinTierBoundary — use target as the
|
||||
// sole boundary so we can still compact at the target size.
|
||||
boundaries.push_back(target);
|
||||
}
|
||||
std::reverse(boundaries.begin(), boundaries.end());
|
||||
|
||||
// For each tier boundary (smallest first), scan L0 for mergeable batches.
|
||||
// L0 files are stored newest-first; oldest is at the end.
|
||||
for (const uint64_t boundary : boundaries) {
|
||||
for (size_t scan = level0_files.size(); scan > 0;) {
|
||||
// Skip files >= boundary (they belong to higher tiers) or in-progress
|
||||
if (level0_files[scan - 1]->fd.file_size >= boundary ||
|
||||
level0_files[scan - 1]->being_compacted) {
|
||||
--scan;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Found a file < boundary — collect contiguous batch
|
||||
std::vector<FileMetaData*> batch;
|
||||
uint64_t accumulated = 0;
|
||||
size_t pos = scan;
|
||||
while (pos > 0 && level0_files[pos - 1]->fd.file_size < boundary &&
|
||||
!level0_files[pos - 1]->being_compacted) {
|
||||
// Don't let output exceed 2x boundary (prevent tier-skipping)
|
||||
if (accumulated >= boundary &&
|
||||
accumulated + level0_files[pos - 1]->fd.file_size > boundary * 2) {
|
||||
break;
|
||||
}
|
||||
batch.push_back(level0_files[pos - 1]);
|
||||
accumulated += level0_files[pos - 1]->fd.file_size;
|
||||
--pos;
|
||||
}
|
||||
|
||||
// Viable: >= 2 files and accumulated >= boundary
|
||||
if (batch.size() >= 2 && accumulated >= boundary) {
|
||||
CompactionInputFiles comp_inputs;
|
||||
comp_inputs.level = 0;
|
||||
comp_inputs.files = std::move(batch);
|
||||
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer,
|
||||
"[%s] FIFO kv-ratio compaction: picking %" ROCKSDB_PRIszt
|
||||
" files (%" PRIu64 " bytes) at tier boundary %" PRIu64
|
||||
" for intra-L0 compaction, target=%" PRIu64,
|
||||
cf_name.c_str(), comp_inputs.files.size(), accumulated, boundary,
|
||||
target);
|
||||
|
||||
Compaction* c = new Compaction(
|
||||
vstorage, ioptions_, mutable_cf_options, mutable_db_options,
|
||||
{comp_inputs}, 0, boundary /* output file size limit */,
|
||||
0 /* max compaction bytes, not applicable */,
|
||||
0 /* output path ID */, mutable_cf_options.compression,
|
||||
mutable_cf_options.compression_opts, Temperature::kUnknown,
|
||||
0 /* max_subcompactions */, {},
|
||||
/* earliest_snapshot */ std::nullopt,
|
||||
/* snapshot_checker */ nullptr,
|
||||
CompactionReason::kFIFOReduceNumFiles,
|
||||
/* trim_ts */ "", vstorage->CompactionScore(0),
|
||||
/* l0_files_might_overlap */ true);
|
||||
return c;
|
||||
}
|
||||
|
||||
// This batch wasn't enough — advance past it
|
||||
scan = pos;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// The full_history_ts_low parameter is used to control bottommost file marking
|
||||
// for compaction when user-defined timestamps (UDT) are enabled.
|
||||
|
||||
// TODO leverage full_history_ts_low for FIFO compaction, by trigggerring
|
||||
// compaction early for data that has already expired to achieve the goal of TTL
|
||||
// enforced compliance.
|
||||
Compaction* FIFOCompactionPicker::PickCompaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
const std::vector<SequenceNumber>& /* existing_snapshots */,
|
||||
const SnapshotChecker* /* snapshot_checker */, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer, bool /* require_max_output_level*/) {
|
||||
LogBuffer* log_buffer, const std::string& /* full_history_ts_low */,
|
||||
bool /* require_max_output_level*/) {
|
||||
Compaction* c = nullptr;
|
||||
if (mutable_cf_options.ttl > 0) {
|
||||
c = PickTTLCompaction(cf_name, mutable_cf_options, mutable_db_options,
|
||||
@@ -434,10 +742,22 @@ Compaction* FIFOCompactionPicker::PickCompaction(
|
||||
c = PickSizeCompaction(cf_name, mutable_cf_options, mutable_db_options,
|
||||
vstorage, log_buffer);
|
||||
}
|
||||
// Intra-L0 compaction merges small files to reduce file count.
|
||||
// It runs after size-based dropping: if PickSizeCompaction dropped files,
|
||||
// it returned non-null and we skip this. Otherwise, we try to reduce
|
||||
// L0 file count by merging small files together.
|
||||
if (c == nullptr) {
|
||||
c = PickIntraL0Compaction(cf_name, mutable_cf_options, mutable_db_options,
|
||||
vstorage, log_buffer);
|
||||
}
|
||||
if (c == nullptr) {
|
||||
c = PickTemperatureChangeCompaction(
|
||||
cf_name, mutable_cf_options, mutable_db_options, vstorage, log_buffer);
|
||||
}
|
||||
if (c == nullptr) {
|
||||
ROCKS_LOG_BUFFER(log_buffer, "[%s] FIFO compaction: no compaction picked",
|
||||
cf_name.c_str());
|
||||
}
|
||||
RegisterCompaction(c);
|
||||
return c;
|
||||
}
|
||||
@@ -449,7 +769,8 @@ Compaction* FIFOCompactionPicker::PickCompactionForCompactRange(
|
||||
const CompactRangeOptions& /*compact_range_options*/,
|
||||
const InternalKey* /*begin*/, const InternalKey* /*end*/,
|
||||
InternalKey** compaction_end, bool* /*manual_conflict*/,
|
||||
uint64_t /*max_file_num_to_ignore*/, const std::string& /*trim_ts*/) {
|
||||
uint64_t /*max_file_num_to_ignore*/, const std::string& /*trim_ts*/,
|
||||
const std::string& full_history_ts_low) {
|
||||
#ifdef NDEBUG
|
||||
(void)input_level;
|
||||
(void)output_level;
|
||||
@@ -458,10 +779,10 @@ Compaction* FIFOCompactionPicker::PickCompactionForCompactRange(
|
||||
assert(output_level == 0);
|
||||
*compaction_end = nullptr;
|
||||
LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, ioptions_.logger);
|
||||
Compaction* c =
|
||||
PickCompaction(cf_name, mutable_cf_options, mutable_db_options,
|
||||
/*existing_snapshots*/ {}, /*snapshot_checker*/ nullptr,
|
||||
vstorage, &log_buffer);
|
||||
Compaction* c = PickCompaction(
|
||||
cf_name, mutable_cf_options, mutable_db_options,
|
||||
/*existing_snapshots*/ {}, /*snapshot_checker*/ nullptr, vstorage,
|
||||
&log_buffer, full_history_ts_low, /* require_max_output_level */ false);
|
||||
log_buffer.FlushBufferToLog();
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ class FIFOCompactionPicker : public CompactionPicker {
|
||||
const std::vector<SequenceNumber>& /* existing_snapshots */,
|
||||
const SnapshotChecker* /* snapshot_checker */,
|
||||
VersionStorageInfo* version, LogBuffer* log_buffer,
|
||||
const std::string& /* full_history_ts_low */,
|
||||
bool /* require_max_output_level*/ = false) override;
|
||||
|
||||
Compaction* PickCompactionForCompactRange(
|
||||
@@ -33,7 +34,8 @@ class FIFOCompactionPicker : public CompactionPicker {
|
||||
const CompactRangeOptions& compact_range_options,
|
||||
const InternalKey* begin, const InternalKey* end,
|
||||
InternalKey** compaction_end, bool* manual_conflict,
|
||||
uint64_t max_file_num_to_ignore, const std::string& trim_ts) override;
|
||||
uint64_t max_file_num_to_ignore, const std::string& trim_ts,
|
||||
const std::string& full_history_ts_low) override;
|
||||
|
||||
// The maximum allowed output level. Always returns 0.
|
||||
int MaxOutputLevel() const override { return 0; }
|
||||
@@ -53,6 +55,28 @@ class FIFOCompactionPicker : public CompactionPicker {
|
||||
VersionStorageInfo* version,
|
||||
LogBuffer* log_buffer);
|
||||
|
||||
// Intra-L0 compaction: merges small L0 files to reduce file count.
|
||||
// Dispatches between two strategies based on configuration:
|
||||
// - use_kv_ratio_compaction = true: PickRatioBasedIntraL0Compaction
|
||||
// (BlobDB-optimized)
|
||||
// - use_kv_ratio_compaction = false: PickCostBasedIntraL0Compaction
|
||||
// (original)
|
||||
// Only active when allow_compaction = true.
|
||||
Compaction* PickIntraL0Compaction(const std::string& cf_name,
|
||||
const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer);
|
||||
|
||||
// Capacity-derived intra-L0 compaction for BlobDB workloads.
|
||||
// Uses the observed SST/blob ratio to compute a target file size,
|
||||
// producing uniform files for predictable FIFO trimming.
|
||||
// Called from PickIntraL0Compaction when use_kv_ratio_compaction = true.
|
||||
Compaction* PickRatioBasedIntraL0Compaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer);
|
||||
|
||||
// Will pick one file to compact at a time, starting from the oldest file.
|
||||
Compaction* PickTemperatureChangeCompaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
|
||||
@@ -61,14 +61,16 @@ class LevelCompactionBuilder {
|
||||
LogBuffer* log_buffer,
|
||||
const MutableCFOptions& mutable_cf_options,
|
||||
const ImmutableOptions& ioptions,
|
||||
const MutableDBOptions& mutable_db_options)
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
const std::string& full_history_ts_low)
|
||||
: cf_name_(cf_name),
|
||||
vstorage_(vstorage),
|
||||
compaction_picker_(compaction_picker),
|
||||
log_buffer_(log_buffer),
|
||||
mutable_cf_options_(mutable_cf_options),
|
||||
ioptions_(ioptions),
|
||||
mutable_db_options_(mutable_db_options) {}
|
||||
mutable_db_options_(mutable_db_options),
|
||||
full_history_ts_low_(full_history_ts_low) {}
|
||||
|
||||
// Pick and return a compaction.
|
||||
Compaction* PickCompaction();
|
||||
@@ -155,6 +157,7 @@ class LevelCompactionBuilder {
|
||||
const MutableCFOptions& mutable_cf_options_;
|
||||
const ImmutableOptions& ioptions_;
|
||||
const MutableDBOptions& mutable_db_options_;
|
||||
const std::string& full_history_ts_low_;
|
||||
// Pick a path ID to place a newly generated file, with its level
|
||||
static uint32_t GetPathId(const ImmutableCFOptions& ioptions,
|
||||
const MutableCFOptions& mutable_cf_options,
|
||||
@@ -571,7 +574,8 @@ Compaction* LevelCompactionBuilder::GetCompaction() {
|
||||
// takes running compactions into account (by skipping files that are already
|
||||
// being compacted). Since we just changed compaction score, we recalculate it
|
||||
// here
|
||||
vstorage_->ComputeCompactionScore(ioptions_, mutable_cf_options_);
|
||||
vstorage_->ComputeCompactionScore(ioptions_, mutable_cf_options_,
|
||||
full_history_ts_low_);
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -910,10 +914,10 @@ bool LevelCompactionBuilder::PickIntraL0Compaction() {
|
||||
// resort to L0->L0 compaction yet.
|
||||
return false;
|
||||
}
|
||||
return FindIntraL0Compaction(level_files, kMinFilesForIntraL0Compaction,
|
||||
std::numeric_limits<uint64_t>::max(),
|
||||
mutable_cf_options_.max_compaction_bytes,
|
||||
&start_level_inputs_);
|
||||
return PickCostBasedIntraL0Compaction(
|
||||
level_files, kMinFilesForIntraL0Compaction,
|
||||
std::numeric_limits<uint64_t>::max(),
|
||||
mutable_cf_options_.max_compaction_bytes, &start_level_inputs_);
|
||||
}
|
||||
|
||||
bool LevelCompactionBuilder::PickSizeBasedIntraL0Compaction() {
|
||||
@@ -976,10 +980,11 @@ Compaction* LevelCompactionPicker::PickCompaction(
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
const std::vector<SequenceNumber>& /*existing_snapshots */,
|
||||
const SnapshotChecker* /*snapshot_checker*/, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer, bool /* require_max_output_level*/) {
|
||||
LogBuffer* log_buffer, const std::string& full_history_ts_low,
|
||||
bool /* require_max_output_level*/) {
|
||||
LevelCompactionBuilder builder(cf_name, vstorage, this, log_buffer,
|
||||
mutable_cf_options, ioptions_,
|
||||
mutable_db_options);
|
||||
mutable_db_options, full_history_ts_low);
|
||||
return builder.PickCompaction();
|
||||
}
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
@@ -26,6 +26,7 @@ class LevelCompactionPicker : public CompactionPicker {
|
||||
const std::vector<SequenceNumber>& /* existing_snapshots */,
|
||||
const SnapshotChecker* /* snapshot_checker */,
|
||||
VersionStorageInfo* vstorage, LogBuffer* log_buffer,
|
||||
const std::string& full_history_ts_low,
|
||||
bool /*require_max_output_level*/ = false) override;
|
||||
|
||||
bool NeedsCompaction(const VersionStorageInfo* vstorage) const override;
|
||||
|
||||
+1405
-103
File diff suppressed because it is too large.
Load diff
@@ -39,7 +39,7 @@ class UniversalCompactionBuilder {
|
||||
const std::vector<SequenceNumber>& existing_snapshots,
|
||||
const SnapshotChecker* snapshot_checker, VersionStorageInfo* vstorage,
|
||||
UniversalCompactionPicker* picker, LogBuffer* log_buffer,
|
||||
bool require_max_output_level)
|
||||
bool require_max_output_level, const std::string& full_history_ts_low)
|
||||
: ioptions_(ioptions),
|
||||
icmp_(icmp),
|
||||
cf_name_(cf_name),
|
||||
@@ -50,7 +50,8 @@ class UniversalCompactionBuilder {
|
||||
log_buffer_(log_buffer),
|
||||
require_max_output_level_(require_max_output_level),
|
||||
allow_ingest_behind_(ioptions.cf_allow_ingest_behind ||
|
||||
ioptions.allow_ingest_behind) {
|
||||
ioptions.allow_ingest_behind),
|
||||
full_history_ts_low_(full_history_ts_low) {
|
||||
assert(icmp_);
|
||||
const auto* ucmp = icmp_->user_comparator();
|
||||
assert(ucmp);
|
||||
@@ -450,6 +451,7 @@ class UniversalCompactionBuilder {
|
||||
std::map<uint64_t, size_t> file_marked_for_compaction_to_sorted_run_index_;
|
||||
bool require_max_output_level_;
|
||||
bool allow_ingest_behind_;
|
||||
const std::string& full_history_ts_low_;
|
||||
|
||||
std::vector<UniversalCompactionBuilder::SortedRun> CalculateSortedRuns(
|
||||
const VersionStorageInfo& vstorage, int last_level,
|
||||
@@ -595,16 +597,20 @@ bool UniversalCompactionPicker::NeedsCompaction(
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO leverage full_history_ts_low in universal compaction picking. It could
|
||||
// help reduce the same infinite compaction loop issue found in level
|
||||
// compaction.
|
||||
Compaction* UniversalCompactionPicker::PickCompaction(
|
||||
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
const std::vector<SequenceNumber>& existing_snapshots,
|
||||
const SnapshotChecker* snapshot_checker, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer, bool require_max_output_level) {
|
||||
LogBuffer* log_buffer, const std::string& full_history_ts_low,
|
||||
bool require_max_output_level) {
|
||||
UniversalCompactionBuilder builder(
|
||||
ioptions_, icmp_, cf_name, mutable_cf_options, mutable_db_options,
|
||||
existing_snapshots, snapshot_checker, vstorage, this, log_buffer,
|
||||
require_max_output_level);
|
||||
require_max_output_level, full_history_ts_low);
|
||||
return builder.PickCompaction();
|
||||
}
|
||||
|
||||
@@ -825,7 +831,8 @@ Compaction* UniversalCompactionBuilder::PickCompaction() {
|
||||
RecordInHistogram(ioptions_.stats, NUM_FILES_IN_SINGLE_COMPACTION, num_files);
|
||||
|
||||
picker_->RegisterCompaction(c);
|
||||
vstorage_->ComputeCompactionScore(ioptions_, mutable_cf_options_);
|
||||
vstorage_->ComputeCompactionScore(ioptions_, mutable_cf_options_,
|
||||
full_history_ts_low_);
|
||||
|
||||
TEST_SYNC_POINT_CALLBACK("UniversalCompactionBuilder::PickCompaction:Return",
|
||||
c);
|
||||
|
||||
@@ -26,7 +26,8 @@ class UniversalCompactionPicker : public CompactionPicker {
|
||||
const MutableDBOptions& mutable_db_options,
|
||||
const std::vector<SequenceNumber>& existing_snapshots,
|
||||
const SnapshotChecker* snapshot_checker, VersionStorageInfo* vstorage,
|
||||
LogBuffer* log_buffer, bool require_max_output_level = false) override;
|
||||
LogBuffer* log_buffer, const std::string& full_history_ts_low,
|
||||
bool require_max_output_level = false) override;
|
||||
int MaxOutputLevel() const override { return NumberLevels() - 1; }
|
||||
|
||||
bool NeedsCompaction(const VersionStorageInfo* vstorage) const override;
|
||||
|
||||
@@ -117,6 +117,14 @@ CompactionJob::ProcessKeyValueCompactionWithCompactionService(
|
||||
std::string debug_str_before_wait =
|
||||
compaction->input_version()->DebugString(/*hex=*/true);
|
||||
|
||||
// TODO: Update CompactionService API to support abort and resume
|
||||
// functionality. Currently, remote compaction jobs cannot be aborted via
|
||||
// AbortAllCompactions() because the CompactionService interface lacks methods
|
||||
// to signal abort to remote workers and to properly resume after an abort.
|
||||
// The API needs to be extended with:
|
||||
// - A method to signal abort to running remote compaction jobs
|
||||
// - A method to resume/re-enable scheduling after an abort is lifted
|
||||
|
||||
ROCKS_LOG_INFO(db_options_.info_log,
|
||||
"[%s] [JOB %d] Waiting for remote compaction...",
|
||||
compaction->column_family_data()->GetName().c_str(), job_id_);
|
||||
@@ -312,16 +320,17 @@ CompactionServiceCompactionJob::CompactionServiceCompactionJob(
|
||||
std::string output_path,
|
||||
const CompactionServiceInput& compaction_service_input,
|
||||
CompactionServiceResult* compaction_service_result)
|
||||
: CompactionJob(job_id, compaction, db_options, mutable_db_options,
|
||||
file_options, versions, shutting_down, log_buffer, nullptr,
|
||||
output_directory, nullptr, stats, db_mutex,
|
||||
db_error_handler, job_context, std::move(table_cache),
|
||||
event_logger,
|
||||
compaction->mutable_cf_options().paranoid_file_checks,
|
||||
compaction->mutable_cf_options().report_bg_io_stats, dbname,
|
||||
&(compaction_service_result->stats), Env::Priority::USER,
|
||||
io_tracer, manual_compaction_canceled, db_id, db_session_id,
|
||||
compaction->column_family_data()->GetFullHistoryTsLow()),
|
||||
: CompactionJob(
|
||||
job_id, compaction, db_options, mutable_db_options, file_options,
|
||||
versions, shutting_down, log_buffer, nullptr, output_directory,
|
||||
nullptr, stats, db_mutex, db_error_handler, job_context,
|
||||
std::move(table_cache), event_logger,
|
||||
compaction->mutable_cf_options().paranoid_file_checks,
|
||||
compaction->mutable_cf_options().report_bg_io_stats, dbname,
|
||||
&(compaction_service_result->stats), Env::Priority::USER, io_tracer,
|
||||
manual_compaction_canceled, CompactionJob::kCompactionAbortedFalse,
|
||||
db_id, db_session_id,
|
||||
compaction->column_family_data()->GetFullHistoryTsLow()),
|
||||
output_path_(std::move(output_path)),
|
||||
compaction_input_(compaction_service_input),
|
||||
compaction_result_(compaction_service_result) {}
|
||||
|
||||
@@ -195,9 +195,11 @@ class MyTestCompactionService : public CompactionService {
|
||||
std::vector<std::shared_ptr<EventListener>> listeners_;
|
||||
std::vector<std::shared_ptr<TablePropertiesCollectorFactory>>
|
||||
table_properties_collector_factories_;
|
||||
std::atomic_bool canceled_{false};
|
||||
std::atomic<CompactionServiceJobStatus> final_updated_status_{
|
||||
CompactionServiceJobStatus::kUseLocal};
|
||||
|
||||
protected:
|
||||
std::atomic_bool canceled_{false};
|
||||
};
|
||||
|
||||
class CompactionServiceTest : public DBTestBase {
|
||||
@@ -1378,8 +1380,9 @@ TEST_F(CompactionServiceTest, CancelCompactionOnPrimarySide) {
|
||||
|
||||
// Primary DB calls CancelAllBackgroundWork() while the compaction is running
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"CompactionJob::Run():Inprogress",
|
||||
[&](void* /*arg*/) { CancelAllBackgroundWork(db_, false /*wait*/); });
|
||||
"CompactionJob::Run():Inprogress", [&](void* /*arg*/) {
|
||||
CancelAllBackgroundWork(db_.get(), false /*wait*/);
|
||||
});
|
||||
|
||||
SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
@@ -2137,6 +2140,12 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
{} /* table_properties_collector_factories */),
|
||||
scenario_(scenario) {}
|
||||
|
||||
// Set the user key where cancellation should happen.
|
||||
void SetCancelAtKey(const std::string& key, SequenceNumber seqno) {
|
||||
cancel_at_key_ = key;
|
||||
cancel_at_seqno_ = seqno;
|
||||
}
|
||||
|
||||
CompactionServiceJobStatus Wait(const std::string& scheduled_job_id,
|
||||
std::string* result) override {
|
||||
std::string compaction_input = ExtractCompactionInput(scheduled_job_id);
|
||||
@@ -2149,25 +2158,50 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
// ASSUMPTION: This makes stats.count directly proportional to keys
|
||||
// processed.
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"CompactionOutputs::ShouldStopBefore::manual_decision", [](void* p) {
|
||||
"CompactionOutputs::ShouldStopBefore::manual_decision",
|
||||
[this](void* p) {
|
||||
auto* pair = static_cast<std::pair<bool*, const Slice>*>(p);
|
||||
*(pair->first) = true;
|
||||
*(pair->first) = true; // Force file cut at every key
|
||||
|
||||
// If cancel_at_key_ is set, cancel when we encounter that key
|
||||
if (!cancel_at_key_.empty() && !already_canceled_) {
|
||||
ParsedInternalKey parsed_key;
|
||||
if (ParseInternalKey(pair->second, &parsed_key, true).ok()) {
|
||||
if (parsed_key.user_key.ToString() == cancel_at_key_) {
|
||||
// Check sequence number if specified
|
||||
if (cancel_at_seqno_ == kMaxSequenceNumber ||
|
||||
parsed_key.sequence == cancel_at_seqno_) {
|
||||
canceled_ = true;
|
||||
already_canceled_ = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// If no cancel_at_key_ is set, use the original behavior:
|
||||
// Simulate cancelled compaction by overriding status at completion. So
|
||||
// compaction processes all keys before this point to make stats.count
|
||||
// comparison straightforward.
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"DBImplSecondary::CompactWithoutInstallation::End", [&](void* status) {
|
||||
auto s = static_cast<Status*>(status);
|
||||
*s = Status::Incomplete(Status::SubCode::kManualCompactionPaused);
|
||||
});
|
||||
if (cancel_at_key_.empty()) {
|
||||
SyncPoint::GetInstance()->SetCallBack(
|
||||
"DBImplSecondary::CompactWithoutInstallation::End",
|
||||
[&](void* status) {
|
||||
auto s = static_cast<Status*>(status);
|
||||
*s = Status::Incomplete(Status::SubCode::kManualCompactionPaused);
|
||||
});
|
||||
}
|
||||
SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
// Phase 1: Run compaction with resumption enabled and cancel it
|
||||
// - Processes all input keys
|
||||
// - Processes input keys until cancellation point
|
||||
// - Creates output files and saves progress
|
||||
// - Status overridden to "paused"
|
||||
open_and_compaction_options.allow_resumption = true;
|
||||
open_and_compaction_options.canceled = &canceled_;
|
||||
already_canceled_ = false;
|
||||
canceled_ = false;
|
||||
|
||||
auto phase1_stats =
|
||||
RunCancelledCompaction(open_and_compaction_options, scheduled_job_id,
|
||||
compaction_input, override_options);
|
||||
@@ -2188,6 +2222,9 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
EXPECT_TRUE(cleanup_status.ok());
|
||||
EXPECT_OK(override_options.env->CreateDir(output_dir));
|
||||
|
||||
already_canceled_ = false;
|
||||
canceled_ = false;
|
||||
|
||||
phase2_stats =
|
||||
RunCancelledCompaction(open_and_compaction_options, scheduled_job_id,
|
||||
compaction_input, override_options);
|
||||
@@ -2199,9 +2236,6 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
EXPECT_EQ(phase2_stats.count, phase1_stats.count);
|
||||
}
|
||||
|
||||
SyncPoint::GetInstance()->ClearCallBack(
|
||||
"DBImplSecondary::CompactWithoutInstallation::End");
|
||||
|
||||
// Final phase: Run compaction to completion (no cancellation)
|
||||
if (scenario_ == TestScenario::kMultipleCancelToggleResumption) {
|
||||
// Attempt to resume but it ends up starting fresh
|
||||
@@ -2220,6 +2254,12 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
EXPECT_OK(override_options.env->CreateDir(output_dir));
|
||||
}
|
||||
|
||||
// Prevent triggering of cancellation
|
||||
SyncPoint::GetInstance()->ClearCallBack(
|
||||
"DBImplSecondary::CompactWithoutInstallation::End");
|
||||
already_canceled_ = true;
|
||||
canceled_ = false;
|
||||
|
||||
auto final_phase_stats =
|
||||
RunCompaction(open_and_compaction_options, scheduled_job_id,
|
||||
compaction_input, override_options, result);
|
||||
@@ -2227,36 +2267,38 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
|
||||
// Validate statistics based on scenario
|
||||
if (scenario_ == TestScenario::kMultipleCancelToggleResumption) {
|
||||
// ASSUMPTION: Phase 1 processes all keys before cancellation
|
||||
EXPECT_GT(phase1_stats.count, 0);
|
||||
// Validate statistics based on scenario (only when cancelling at end)
|
||||
if (cancel_at_key_.empty()) {
|
||||
if (scenario_ == TestScenario::kMultipleCancelToggleResumption) {
|
||||
// ASSUMPTION: Phase 1 processes all keys before cancellation
|
||||
EXPECT_GT(phase1_stats.count, 0);
|
||||
|
||||
// ASSUMPTION: Phase 2 runs with allow_resumption=false and an empty
|
||||
// folder. Phase 2 then creates its own output files (but doesn't save
|
||||
// progress). When Phase 3 starts with allow_resumption=true, it finds no
|
||||
// progress file exists, so it cannot resume and must start from scratch,
|
||||
// processing all input keys again.
|
||||
// Result: Phase 3 does the same amount of work as Phase 1.
|
||||
EXPECT_EQ(final_phase_stats.count, phase1_stats.count);
|
||||
// ASSUMPTION: Phase 2 runs with allow_resumption=false and an empty
|
||||
// folder. Phase 2 then creates its own output files (but doesn't save
|
||||
// progress). When Phase 3 starts with allow_resumption=true, it finds
|
||||
// no progress file exists, so it cannot resume and must start from
|
||||
// scratch, processing all input keys again. Result: Phase 3 does the
|
||||
// same amount of work as Phase 1.
|
||||
EXPECT_EQ(final_phase_stats.count, phase1_stats.count);
|
||||
|
||||
} else if (scenario_ == TestScenario::kCancelThenResume) {
|
||||
// ASSUMPTION: Phase 1 processes all keys before cancellation
|
||||
EXPECT_GT(phase1_stats.count, 0);
|
||||
} else if (scenario_ == TestScenario::kCancelThenResume) {
|
||||
// ASSUMPTION: Phase 1 processes all keys before cancellation
|
||||
EXPECT_GT(phase1_stats.count, 0);
|
||||
|
||||
// ASSUMPTION: Phase 1 processes all keys and saves progress before
|
||||
// cancellation. Final phase resumes from Phase 1's saved progress.
|
||||
// Since Phase 1 completed all processing before being cancelled, the
|
||||
// final phase should do less work than Phase 1.
|
||||
EXPECT_LT(final_phase_stats.count, phase1_stats.count);
|
||||
// ASSUMPTION: Phase 1 processes all keys and saves progress before
|
||||
// cancellation. Final phase resumes from Phase 1's saved progress.
|
||||
// Since Phase 1 completed all processing before being cancelled, the
|
||||
// final phase should do less work than Phase 1.
|
||||
EXPECT_LT(final_phase_stats.count, phase1_stats.count);
|
||||
|
||||
} else { // kCancelThenFreshStart
|
||||
// ASSUMPTION: Phase 1 processes all keys before cancellation
|
||||
EXPECT_GT(phase1_stats.count, 0);
|
||||
} else { // kCancelThenFreshStart
|
||||
// ASSUMPTION: Phase 1 processes all keys before cancellation
|
||||
EXPECT_GT(phase1_stats.count, 0);
|
||||
|
||||
// ASSUMPTION: Final phase starts fresh without resumption, so it
|
||||
// processes all input keys again and creates the same number of files
|
||||
EXPECT_EQ(final_phase_stats.count, phase1_stats.count);
|
||||
// ASSUMPTION: Final phase starts fresh without resumption, so it
|
||||
// processes all input keys again and creates the same number of files
|
||||
EXPECT_EQ(final_phase_stats.count, phase1_stats.count);
|
||||
}
|
||||
}
|
||||
|
||||
StoreResult(*result);
|
||||
@@ -2326,6 +2368,9 @@ class ResumableCompactionService : public MyTestCompactionService {
|
||||
}
|
||||
|
||||
TestScenario scenario_;
|
||||
std::string cancel_at_key_;
|
||||
SequenceNumber cancel_at_seqno_ = kMaxSequenceNumber;
|
||||
std::atomic<bool> already_canceled_{false};
|
||||
};
|
||||
|
||||
class ResumableCompactionServiceTest : public CompactionServiceTest {
|
||||
@@ -2432,6 +2477,387 @@ TEST_F(ResumableCompactionServiceTest,
|
||||
RunCompactionCancelTest(ResumableCompactionService::TestScenario::
|
||||
kMultipleCancelToggleResumption);
|
||||
}
|
||||
|
||||
class ResumableCompactionKeyTypeTest : public CompactionServiceTest {
|
||||
public:
|
||||
explicit ResumableCompactionKeyTypeTest() : CompactionServiceTest() {}
|
||||
|
||||
protected:
|
||||
void SetupResumableCompactionService(
|
||||
Options& options, const std::string& cancel_at_key = "",
|
||||
SequenceNumber cancel_at_seqno = kMaxSequenceNumber) {
|
||||
options.disable_auto_compactions = true;
|
||||
statistics_ = CreateDBStatistics();
|
||||
|
||||
resume_cs_ = std::make_shared<ResumableCompactionService>(
|
||||
dbname_, options, statistics_,
|
||||
ResumableCompactionService::TestScenario::kCancelThenResume);
|
||||
|
||||
if (!cancel_at_key.empty()) {
|
||||
resume_cs_->SetCancelAtKey(cancel_at_key, cancel_at_seqno);
|
||||
}
|
||||
|
||||
options.compaction_service = resume_cs_;
|
||||
DestroyAndReopen(options);
|
||||
}
|
||||
|
||||
void ResetStatistics() { ASSERT_OK(statistics_->Reset()); }
|
||||
|
||||
void VerifyResumeBytes() {
|
||||
uint64_t resumed_bytes =
|
||||
statistics_->getTickerCount(REMOTE_COMPACT_RESUMED_BYTES);
|
||||
ASSERT_GT(resumed_bytes, 0);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<ResumableCompactionService> resume_cs_;
|
||||
std::shared_ptr<Statistics> statistics_;
|
||||
};
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// deletion at the non-bottom level. When resumed, compaction will continue
|
||||
// from this deletion.
|
||||
TEST_F(ResumableCompactionKeyTypeTest,
|
||||
CancelAndResumeWithDeleteAtNonBottomLevel) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
SetupResumableCompactionService(options, "c");
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
ASSERT_OK(Put("c_placeholder", "placeholder"));
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(options.num_levels - 1);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ASSERT_OK(Delete("c"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::vector<std::string> input_files;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
db_->GetColumnFamilyMetaData(&cf_meta);
|
||||
|
||||
for (const auto& file : cf_meta.levels[0].files) {
|
||||
input_files.push_back(file.name);
|
||||
}
|
||||
|
||||
ASSERT_EQ(input_files.size(), 2);
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
CompactionOptions compact_options;
|
||||
ASSERT_OK(
|
||||
db_->CompactFiles(compact_options, input_files, 1 /* output_level*/));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "NOT_FOUND");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// deletion at the ottom level. When resumed, compaction will continue from
|
||||
// the last saved progress point before the delete.
|
||||
TEST_F(ResumableCompactionKeyTypeTest, CancelAndResumeWithDeleteAtBottomLevel) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
SetupResumableCompactionService(options, "c");
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
const Snapshot* snapshot = db_->GetSnapshot();
|
||||
ASSERT_OK(Delete("c"));
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(options.num_levels - 1);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
ASSERT_OK(db_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "NOT_FOUND");
|
||||
ASSERT_EQ(Get("c", snapshot), "old_value");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
db_->ReleaseSnapshot(snapshot);
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// merge operand. When resumed, compaction will continue from the last saved
|
||||
// progress point before the merge operand.
|
||||
TEST_F(ResumableCompactionKeyTypeTest, CancelAndResumeWithMerge) {
|
||||
Options options = CurrentOptions();
|
||||
options.merge_operator = MergeOperators::CreateStringAppendOperator();
|
||||
|
||||
SetupResumableCompactionService(options, "c");
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
ASSERT_OK(Put("c_placeholder", "placeholder"));
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(options.num_levels - 1);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ASSERT_OK(Merge("c", "new_value"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::vector<std::string> input_files;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
db_->GetColumnFamilyMetaData(&cf_meta);
|
||||
|
||||
for (const auto& file : cf_meta.levels[0].files) {
|
||||
input_files.push_back(file.name);
|
||||
}
|
||||
|
||||
ASSERT_EQ(input_files.size(), 2);
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
CompactionOptions compact_options;
|
||||
ASSERT_OK(
|
||||
db_->CompactFiles(compact_options, input_files, 1 /* output_level*/));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "old_value,new_value");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// single delete. When resumed, compaction will continue from the last saved
|
||||
// progress point before the single delete.
|
||||
TEST_F(ResumableCompactionKeyTypeTest, CancelAndResumeWithSingleDelete) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
SetupResumableCompactionService(options, "c");
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
ASSERT_OK(Put("c_placeholder", "placeholder"));
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(options.num_levels - 1);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ASSERT_OK(SingleDelete("c"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::vector<std::string> input_files;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
db_->GetColumnFamilyMetaData(&cf_meta);
|
||||
|
||||
for (const auto& file : cf_meta.levels[0].files) {
|
||||
input_files.push_back(file.name);
|
||||
}
|
||||
|
||||
ASSERT_EQ(input_files.size(), 2);
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
CompactionOptions compact_options;
|
||||
ASSERT_OK(
|
||||
db_->CompactFiles(compact_options, input_files, 1 /* output_level*/));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "NOT_FOUND");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// range delete. When resumed, compaction will continue from the last saved
|
||||
// progress point before the range delete.
|
||||
TEST_F(ResumableCompactionKeyTypeTest, CancelAndResumeWithRangeDelete) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
SetupResumableCompactionService(options, "c");
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
ASSERT_OK(Put("c_placeholder", "placeholder"));
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(options.num_levels - 1);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ASSERT_OK(
|
||||
db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(), "c", "c_"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::vector<std::string> input_files;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
db_->GetColumnFamilyMetaData(&cf_meta);
|
||||
|
||||
for (const auto& file : cf_meta.levels[0].files) {
|
||||
input_files.push_back(file.name);
|
||||
}
|
||||
|
||||
ASSERT_EQ(input_files.size(), 2);
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
CompactionOptions compact_options;
|
||||
ASSERT_OK(
|
||||
db_->CompactFiles(compact_options, input_files, 1 /* output_level*/));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "NOT_FOUND");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Test resumption when a key has multiple versions spanning across file
|
||||
// boundaries (i.e., the same key exists in multiple SST files).
|
||||
//
|
||||
// Scenario:
|
||||
// File 1 largest key: key "b"
|
||||
// File 2 smallest key: key "c" with seqno=4 (older version)
|
||||
// File 3 largest key: key "c" with seqno=5 (newer version)
|
||||
//
|
||||
// Cancel compaction right before processing the older version of key "c".
|
||||
// Upon resumption, compaction continues from the saved progress point "b" and
|
||||
// correctly processes both versions
|
||||
TEST_F(ResumableCompactionKeyTypeTest,
|
||||
CancelAndResumeWithKeySpanningFileBoundaries) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
// Set up cancellation at the older version of the key which will have
|
||||
// sequence number zero-ed out
|
||||
SetupResumableCompactionService(options, "c" /*cancel_at_key*/, 0 /*seqno*/);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
const Snapshot* snapshot = db_->GetSnapshot();
|
||||
ASSERT_OK(Put("c", "new_value"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
ASSERT_OK(db_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "new_value");
|
||||
ASSERT_EQ(Get("c", snapshot), "old_value");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
db_->ReleaseSnapshot(snapshot);
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// wide column. When resumed, compaction will continue
|
||||
// from the wide column.
|
||||
TEST_F(ResumableCompactionKeyTypeTest, CancelAndResumeWithWideColumn) {
|
||||
Options options = CurrentOptions();
|
||||
|
||||
SetupResumableCompactionService(options, "c" /*cancel_at_key*/);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
WideColumns columns{{"col1", "value1"}, {"col2", "value2"}};
|
||||
ASSERT_OK(
|
||||
db_->PutEntity(WriteOptions(), db_->DefaultColumnFamily(), "c", columns));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
ASSERT_OK(db_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
|
||||
PinnableWideColumns result;
|
||||
ASSERT_OK(
|
||||
db_->GetEntity(ReadOptions(), db_->DefaultColumnFamily(), "c", &result));
|
||||
WideColumns expected{{"col1", "value1"}, {"col2", "value2"}};
|
||||
ASSERT_EQ(result.columns(), expected);
|
||||
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
|
||||
// Cancel compaction right before processing key "c" to test resumption at a
|
||||
// timed put. When resumed, compaction will continue
|
||||
// from the timed put.
|
||||
TEST_F(ResumableCompactionKeyTypeTest, CancelAndResumeWithTimedPut) {
|
||||
Options options = CurrentOptions();
|
||||
options.preclude_last_level_data_seconds = 86400; // Enable TimedPut feature
|
||||
options.preserve_internal_time_seconds = 86400; // Preserve write time
|
||||
|
||||
SetupResumableCompactionService(options, "c" /*cancel_at_key*/);
|
||||
|
||||
ASSERT_OK(Put("c", "old_value"));
|
||||
ASSERT_OK(Put("c_placeholder", "placeholder"));
|
||||
ASSERT_OK(Flush());
|
||||
MoveFilesToLevel(options.num_levels - 1);
|
||||
|
||||
ASSERT_OK(Put("a", "val1"));
|
||||
ASSERT_OK(Put("b", "val2"));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Use TimedPut for key "c" with current write time
|
||||
uint64_t write_time = env_->NowMicros() / 1000000;
|
||||
ASSERT_OK(TimedPut("c", "val3", write_time /*write_unix_time*/));
|
||||
ASSERT_OK(Put("d", "val4"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
std::vector<std::string> input_files;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
db_->GetColumnFamilyMetaData(&cf_meta);
|
||||
|
||||
for (const auto& file : cf_meta.levels[0].files) {
|
||||
input_files.push_back(file.name);
|
||||
}
|
||||
|
||||
ASSERT_EQ(input_files.size(), 2);
|
||||
|
||||
ResetStatistics();
|
||||
|
||||
CompactionOptions compact_options;
|
||||
ASSERT_OK(
|
||||
db_->CompactFiles(compact_options, input_files, 1 /* output_level*/));
|
||||
|
||||
ASSERT_EQ(Get("a"), "val1");
|
||||
ASSERT_EQ(Get("b"), "val2");
|
||||
ASSERT_EQ(Get("c"), "val3");
|
||||
ASSERT_EQ(Get("d"), "val4");
|
||||
|
||||
VerifyResumeBytes();
|
||||
}
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -95,6 +95,14 @@ class SubcompactionState {
|
||||
proximal_level_outputs_.RemoveLastEmptyOutput();
|
||||
}
|
||||
|
||||
// Cleanup output builders for abandoning in-progress files.
|
||||
void CleanupOutputs() {
|
||||
compaction_outputs_.Cleanup();
|
||||
if (compaction->SupportsPerKeyPlacement()) {
|
||||
proximal_level_outputs_.Cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
void BuildSubcompactionJobInfo(
|
||||
SubcompactionJobInfo& subcompaction_job_info) const {
|
||||
const Compaction* c = compaction;
|
||||
|
||||
@@ -1717,8 +1717,8 @@ TEST_P(PrecludeLastLevelTest, MigrationFromPreserveTimePartial) {
|
||||
ASSERT_EQ("0,0,0,0,0,0,1", FilesPerLevel());
|
||||
|
||||
std::vector<KeyVersion> key_versions;
|
||||
ASSERT_OK(GetAllKeyVersions(db_, {}, {}, std::numeric_limits<size_t>::max(),
|
||||
&key_versions));
|
||||
ASSERT_OK(GetAllKeyVersions(
|
||||
db_.get(), {}, {}, std::numeric_limits<size_t>::max(), &key_versions));
|
||||
|
||||
// make sure there're more than 300 keys and first 100 keys are having seqno
|
||||
// zeroed out, the last 100 key seqno not zeroed out
|
||||
@@ -2319,8 +2319,8 @@ TEST_P(PrecludeLastLevelTest, LastLevelOnlyCompactionPartial) {
|
||||
ASSERT_GT(GetSstSizeHelper(Temperature::kUnknown), 0);
|
||||
|
||||
std::vector<KeyVersion> key_versions;
|
||||
ASSERT_OK(GetAllKeyVersions(db_, {}, {}, std::numeric_limits<size_t>::max(),
|
||||
&key_versions));
|
||||
ASSERT_OK(GetAllKeyVersions(
|
||||
db_.get(), {}, {}, std::numeric_limits<size_t>::max(), &key_versions));
|
||||
|
||||
// make sure there're more than 300 keys and first 100 keys are having seqno
|
||||
// zeroed out, the last 100 key seqno not zeroed out
|
||||
|
||||
@@ -258,12 +258,12 @@ class ComparatorDBTest
|
||||
private:
|
||||
std::string dbname_;
|
||||
Env* env_;
|
||||
DB* db_;
|
||||
std::unique_ptr<DB> db_;
|
||||
Options last_options_;
|
||||
std::unique_ptr<const Comparator> comparator_guard;
|
||||
|
||||
public:
|
||||
ComparatorDBTest() : env_(Env::Default()), db_(nullptr) {
|
||||
ComparatorDBTest() : env_(Env::Default()) {
|
||||
kTestComparator = BytewiseComparator();
|
||||
dbname_ = test::PerThreadDBPath("comparator_db_test");
|
||||
BlockBasedTableOptions toptions;
|
||||
@@ -274,12 +274,12 @@ class ComparatorDBTest
|
||||
}
|
||||
|
||||
~ComparatorDBTest() override {
|
||||
delete db_;
|
||||
db_.reset();
|
||||
EXPECT_OK(DestroyDB(dbname_, last_options_));
|
||||
kTestComparator = BytewiseComparator();
|
||||
}
|
||||
|
||||
DB* GetDB() { return db_; }
|
||||
DB* GetDB() { return db_.get(); }
|
||||
|
||||
void SetOwnedComparator(const Comparator* cmp, bool owner = true) {
|
||||
if (owner) {
|
||||
@@ -301,14 +301,12 @@ class ComparatorDBTest
|
||||
}
|
||||
|
||||
void Destroy() {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, last_options_));
|
||||
}
|
||||
|
||||
Status TryReopen() {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
last_options_.create_if_missing = true;
|
||||
|
||||
return DB::Open(last_options_, dbname_, &db_);
|
||||
@@ -318,7 +316,7 @@ class ComparatorDBTest
|
||||
INSTANTIATE_TEST_CASE_P(FormatDef, ComparatorDBTest,
|
||||
testing::Values(test::kDefaultFormatVersion));
|
||||
INSTANTIATE_TEST_CASE_P(FormatLatest, ComparatorDBTest,
|
||||
testing::Values(kLatestFormatVersion));
|
||||
testing::Values(kLatestBbtFormatVersion));
|
||||
|
||||
TEST_P(ComparatorDBTest, Bytewise) {
|
||||
for (int rand_seed = 301; rand_seed < 306; rand_seed++) {
|
||||
|
||||
+4
-4
@@ -65,7 +65,7 @@ Status VerifySstFileChecksum(const Options& options,
|
||||
}
|
||||
|
||||
Status VerifySstFileChecksumInternal(const Options& options,
|
||||
const EnvOptions& env_options,
|
||||
const FileOptions& file_options,
|
||||
const ReadOptions& read_options,
|
||||
const std::string& file_path,
|
||||
const SequenceNumber& largest_seqno) {
|
||||
@@ -74,8 +74,8 @@ Status VerifySstFileChecksumInternal(const Options& options,
|
||||
InternalKeyComparator internal_comparator(options.comparator);
|
||||
ImmutableOptions ioptions(options);
|
||||
|
||||
Status s = ioptions.fs->NewRandomAccessFile(
|
||||
file_path, FileOptions(env_options), &file, nullptr);
|
||||
Status s =
|
||||
ioptions.fs->NewRandomAccessFile(file_path, file_options, &file, nullptr);
|
||||
if (s.ok()) {
|
||||
s = ioptions.fs->GetFileSize(file_path, IOOptions(), &file_size, nullptr);
|
||||
} else {
|
||||
@@ -94,7 +94,7 @@ Status VerifySstFileChecksumInternal(const Options& options,
|
||||
const bool kImmortal = true;
|
||||
auto reader_options = TableReaderOptions(
|
||||
ioptions, options.prefix_extractor, options.compression_manager.get(),
|
||||
env_options, internal_comparator, options.block_protection_bytes_per_key,
|
||||
file_options, internal_comparator, options.block_protection_bytes_per_key,
|
||||
false /* skip_filters */, !kImmortal, false /* force_direct_prefetch */,
|
||||
-1 /* level */);
|
||||
reader_options.largest_seqno = largest_seqno;
|
||||
|
||||
@@ -5,10 +5,11 @@
|
||||
|
||||
#pragma once
|
||||
#include "rocksdb/db.h"
|
||||
#include "rocksdb/file_system.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
Status VerifySstFileChecksumInternal(const Options& options,
|
||||
const EnvOptions& env_options,
|
||||
const FileOptions& file_options,
|
||||
const ReadOptions& read_options,
|
||||
const std::string& file_path,
|
||||
const SequenceNumber& largest_seqno = 0);
|
||||
|
||||
+60
-88
@@ -73,7 +73,7 @@ class CorruptionTest : public testing::Test {
|
||||
std::string dbname_;
|
||||
std::shared_ptr<Cache> tiny_cache_;
|
||||
Options options_;
|
||||
DB* db_;
|
||||
std::unique_ptr<DB> db_;
|
||||
|
||||
CorruptionTest() {
|
||||
// If LRU cache shard bit is smaller than 2 (or -1 which will automatically
|
||||
@@ -105,8 +105,7 @@ class CorruptionTest : public testing::Test {
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
SyncPoint::GetInstance()->LoadDependency({});
|
||||
SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
if (getenv("KEEP_DB")) {
|
||||
fprintf(stdout, "db is still at %s\n", dbname_.c_str());
|
||||
} else {
|
||||
@@ -116,14 +115,12 @@ class CorruptionTest : public testing::Test {
|
||||
}
|
||||
}
|
||||
|
||||
void CloseDb() {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
}
|
||||
void CloseDb() { db_.reset(); }
|
||||
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_.get()); }
|
||||
|
||||
Status TryReopen(Options* options = nullptr) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
Options opt = (options ? *options : options_);
|
||||
if (opt.env == Options().env) {
|
||||
// If env is not overridden, replace it with ErrorEnv.
|
||||
@@ -141,8 +138,7 @@ class CorruptionTest : public testing::Test {
|
||||
void Reopen(Options* options = nullptr) { ASSERT_OK(TryReopen(options)); }
|
||||
|
||||
void RepairDB() {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(::ROCKSDB_NAMESPACE::RepairDB(dbname_, options_));
|
||||
}
|
||||
|
||||
@@ -151,8 +147,7 @@ class CorruptionTest : public testing::Test {
|
||||
WriteBatch batch;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (flush_every != 0 && i != 0 && i % flush_every == 0) {
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
}
|
||||
// if ((i % 100) == 0) fprintf(stderr, "@ %d of %d\n", i, n);
|
||||
Slice key = Key(i + start, &key_space);
|
||||
@@ -436,14 +431,14 @@ TEST_F(CorruptionTest, NewFileErrorDuringWrite) {
|
||||
|
||||
TEST_F(CorruptionTest, TableFile) {
|
||||
Build(100);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
DBImpl* dbi = dbfull();
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
||||
ASSERT_OK(dbi->TEST_CompactRange(1, nullptr, nullptr));
|
||||
|
||||
Corrupt(kTableFile, 100, 1);
|
||||
Check(99, 99);
|
||||
ASSERT_NOK(dbi->VerifyChecksum());
|
||||
ASSERT_NOK(db_->VerifyChecksum());
|
||||
}
|
||||
|
||||
TEST_F(CorruptionTest, VerifyChecksumReadahead) {
|
||||
@@ -460,14 +455,14 @@ TEST_F(CorruptionTest, VerifyChecksumReadahead) {
|
||||
Reopen(&options);
|
||||
|
||||
Build(10000);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
DBImpl* dbi = dbfull();
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
||||
ASSERT_OK(dbi->TEST_CompactRange(1, nullptr, nullptr));
|
||||
|
||||
senv.count_random_reads_ = true;
|
||||
senv.random_read_counter_.Reset();
|
||||
ASSERT_OK(dbi->VerifyChecksum());
|
||||
ASSERT_OK(db_->VerifyChecksum());
|
||||
|
||||
// Make sure the counter is enabled.
|
||||
ASSERT_GT(senv.random_read_counter_.Read(), 0);
|
||||
@@ -480,7 +475,7 @@ TEST_F(CorruptionTest, VerifyChecksumReadahead) {
|
||||
senv.random_read_bytes_counter_ = 0;
|
||||
ReadOptions ro;
|
||||
ro.readahead_size = size_t{32 * 1024};
|
||||
ASSERT_OK(dbi->VerifyChecksum(ro));
|
||||
ASSERT_OK(db_->VerifyChecksum(ro));
|
||||
// The SST file is about 10MB. We set readahead size to 32KB.
|
||||
// Give 0 to 20 reads for metadata blocks, and allow real read
|
||||
// to range from 24KB to 48KB. The lower bound would be:
|
||||
@@ -494,8 +489,7 @@ TEST_F(CorruptionTest, VerifyChecksumReadahead) {
|
||||
// disabled).
|
||||
options.allow_mmap_reads = true;
|
||||
Reopen(&options);
|
||||
dbi = static_cast<DBImpl*>(db_);
|
||||
ASSERT_OK(dbi->VerifyChecksum(ro));
|
||||
ASSERT_OK(db_->VerifyChecksum(ro));
|
||||
|
||||
CloseDb();
|
||||
}
|
||||
@@ -508,18 +502,16 @@ TEST_F(CorruptionTest, TableFileIndexData) {
|
||||
Reopen(&options);
|
||||
// build 2 tables, flush at 5000
|
||||
Build(10000, 5000);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
|
||||
// corrupt an index block of an entire file
|
||||
Corrupt(kTableFile, -2000, 500);
|
||||
options.paranoid_checks = false;
|
||||
Reopen(&options);
|
||||
dbi = static_cast_with_check<DBImpl>(db_);
|
||||
// one full file may be readable, since only one was corrupted
|
||||
// the other file should be fully non-readable, since index was corrupted
|
||||
Check(0, 5000, ReadOptions(true, true));
|
||||
ASSERT_NOK(dbi->VerifyChecksum());
|
||||
ASSERT_NOK(db_->VerifyChecksum());
|
||||
|
||||
// In paranoid mode, the db cannot be opened due to the corrupted file.
|
||||
ASSERT_TRUE(TryReopen().IsCorruption());
|
||||
@@ -527,8 +519,7 @@ TEST_F(CorruptionTest, TableFileIndexData) {
|
||||
|
||||
TEST_F(CorruptionTest, TableFileFooterMagic) {
|
||||
Build(100);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
Check(100, 100);
|
||||
// Corrupt the whole footer
|
||||
Corrupt(kTableFile, -100, 100);
|
||||
@@ -543,8 +534,7 @@ TEST_F(CorruptionTest, TableFileFooterMagic) {
|
||||
|
||||
TEST_F(CorruptionTest, TableFileFooterNotMagic) {
|
||||
Build(100);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
Check(100, 100);
|
||||
// Corrupt footer except magic number
|
||||
Corrupt(kTableFile, -100, 92);
|
||||
@@ -579,8 +569,7 @@ TEST_F(CorruptionTest, DBOpenWithWrongFileSize) {
|
||||
for (auto* cfh : cfhs) {
|
||||
delete cfh;
|
||||
}
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
|
||||
// ********************************************
|
||||
// Corrupt the file by making the file bigger
|
||||
@@ -601,7 +590,8 @@ TEST_F(CorruptionTest, DBOpenWithWrongFileSize) {
|
||||
// true
|
||||
options_.paranoid_checks = true;
|
||||
std::vector<ColumnFamilyHandle*> cfhs;
|
||||
auto s = DB::Open(options_, dbname_, cf_descs, &cfhs, &db_);
|
||||
Status s;
|
||||
s = DB::Open(options_, dbname_, cf_descs, &cfhs, &db_);
|
||||
ASSERT_TRUE(s.IsCorruption());
|
||||
ASSERT_TRUE(s.ToString().find("file size mismatch") != std::string::npos);
|
||||
|
||||
@@ -626,8 +616,7 @@ TEST_F(CorruptionTest, DBOpenWithWrongFileSize) {
|
||||
|
||||
TEST_F(CorruptionTest, TableFileWrongSize) {
|
||||
Build(100);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
Check(100, 100);
|
||||
|
||||
// ********************************************
|
||||
@@ -710,12 +699,11 @@ TEST_F(CorruptionTest, SequenceNumberRecovery) {
|
||||
|
||||
TEST_F(CorruptionTest, CorruptedDescriptor) {
|
||||
ASSERT_OK(db_->Put(WriteOptions(), "foo", "hello"));
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
ASSERT_OK(
|
||||
dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr));
|
||||
db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr));
|
||||
|
||||
Corrupt(kDescriptorFile, 0, 1000);
|
||||
Status s = TryReopen();
|
||||
@@ -734,7 +722,7 @@ TEST_F(CorruptionTest, CompactionInputError) {
|
||||
options.env = env_.get();
|
||||
Reopen(&options);
|
||||
Build(10);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
DBImpl* dbi = dbfull();
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbi->TEST_CompactRange(0, nullptr, nullptr));
|
||||
ASSERT_OK(dbi->TEST_CompactRange(1, nullptr, nullptr));
|
||||
@@ -742,12 +730,12 @@ TEST_F(CorruptionTest, CompactionInputError) {
|
||||
|
||||
Corrupt(kTableFile, 100, 1);
|
||||
Check(9, 9);
|
||||
ASSERT_NOK(dbi->VerifyChecksum());
|
||||
ASSERT_NOK(db_->VerifyChecksum());
|
||||
|
||||
// Force compactions by writing lots of values
|
||||
Build(10000);
|
||||
Check(10000, 10000);
|
||||
ASSERT_NOK(dbi->VerifyChecksum());
|
||||
ASSERT_NOK(db_->VerifyChecksum());
|
||||
}
|
||||
|
||||
TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
||||
@@ -758,14 +746,14 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
||||
options.write_buffer_size = 131072;
|
||||
options.max_write_buffer_number = 2;
|
||||
Reopen(&options);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
DBImpl* dbi = dbfull();
|
||||
|
||||
// Fill levels >= 1
|
||||
for (int level = 1; level < dbi->NumberLevels(); level++) {
|
||||
ASSERT_OK(dbi->Put(WriteOptions(), "", "begin"));
|
||||
ASSERT_OK(dbi->Put(WriteOptions(), "~", "end"));
|
||||
for (int level = 1; level < db_->NumberLevels(); level++) {
|
||||
ASSERT_OK(db_->Put(WriteOptions(), "", "begin"));
|
||||
ASSERT_OK(db_->Put(WriteOptions(), "~", "end"));
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
for (int comp_level = 0; comp_level < dbi->NumberLevels() - level;
|
||||
for (int comp_level = 0; comp_level < db_->NumberLevels() - level;
|
||||
++comp_level) {
|
||||
ASSERT_OK(dbi->TEST_CompactRange(comp_level, nullptr, nullptr));
|
||||
}
|
||||
@@ -773,7 +761,7 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
||||
|
||||
Reopen(&options);
|
||||
|
||||
dbi = static_cast_with_check<DBImpl>(db_);
|
||||
dbi = dbfull();
|
||||
Build(10);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbi->TEST_WaitForCompact());
|
||||
@@ -781,7 +769,7 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
||||
|
||||
CorruptTableFileAtLevel(0, 100, 1);
|
||||
Check(9, 9);
|
||||
ASSERT_NOK(dbi->VerifyChecksum());
|
||||
ASSERT_NOK(db_->VerifyChecksum());
|
||||
|
||||
// Write must eventually fail because of corrupted table
|
||||
Status s;
|
||||
@@ -800,17 +788,16 @@ TEST_F(CorruptionTest, CompactionInputErrorParanoid) {
|
||||
|
||||
TEST_F(CorruptionTest, UnrelatedKeys) {
|
||||
Build(10);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
Corrupt(kTableFile, 100, 1);
|
||||
ASSERT_NOK(dbi->VerifyChecksum());
|
||||
ASSERT_NOK(db_->VerifyChecksum());
|
||||
|
||||
std::string tmp1, tmp2;
|
||||
ASSERT_OK(db_->Put(WriteOptions(), Key(1000, &tmp1), Value(1000, &tmp2)));
|
||||
std::string v;
|
||||
ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
|
||||
ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
ASSERT_OK(db_->Get(ReadOptions(), Key(1000, &tmp1), &v));
|
||||
ASSERT_EQ(Value(1000, &tmp2).ToString(), v);
|
||||
}
|
||||
@@ -857,14 +844,12 @@ TEST_F(CorruptionTest, FileSystemStateCorrupted) {
|
||||
Reopen(&options);
|
||||
Build(10);
|
||||
ASSERT_OK(db_->Flush(FlushOptions()));
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
std::vector<LiveFileMetaData> metadata;
|
||||
dbi->GetLiveFilesMetaData(&metadata);
|
||||
db_->GetLiveFilesMetaData(&metadata);
|
||||
ASSERT_GT(metadata.size(), 0);
|
||||
std::string filename = dbname_ + metadata[0].name;
|
||||
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
|
||||
if (iter == 0) { // corrupt file size
|
||||
std::unique_ptr<WritableFile> file;
|
||||
@@ -896,8 +881,7 @@ TEST_F(CorruptionTest, ParanoidFileChecksOnFlush) {
|
||||
options.create_if_missing = true;
|
||||
Status s;
|
||||
for (const auto& mode : corruption_modes) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
s = DestroyDB(dbname_, options);
|
||||
ASSERT_OK(s);
|
||||
std::shared_ptr<mock::MockTableFactory> mock =
|
||||
@@ -924,8 +908,7 @@ TEST_F(CorruptionTest, ParanoidFileChecksOnCompact) {
|
||||
options.create_if_missing = true;
|
||||
Status s;
|
||||
for (const auto& mode : corruption_modes) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
s = DestroyDB(dbname_, options);
|
||||
ASSERT_OK(s);
|
||||
std::shared_ptr<mock::MockTableFactory> mock =
|
||||
@@ -934,12 +917,11 @@ TEST_F(CorruptionTest, ParanoidFileChecksOnCompact) {
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
||||
assert(db_ != nullptr); // suppress false clang-analyze report
|
||||
Build(100, 2);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
mock->SetCorruptionMode(mode);
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
s = dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr);
|
||||
s = db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr);
|
||||
if (mode == mock::MockTableFactory::kCorruptNone) {
|
||||
ASSERT_OK(s);
|
||||
} else {
|
||||
@@ -955,8 +937,7 @@ TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRangeFirst) {
|
||||
options.paranoid_file_checks = true;
|
||||
options.create_if_missing = true;
|
||||
for (bool do_flush : {true, false}) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, options));
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
||||
std::string start, end;
|
||||
@@ -973,12 +954,11 @@ TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRangeFirst) {
|
||||
if (do_flush) {
|
||||
ASSERT_OK(db_->Flush(FlushOptions()));
|
||||
} else {
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
ASSERT_OK(
|
||||
dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr));
|
||||
db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr));
|
||||
}
|
||||
db_->ReleaseSnapshot(snap);
|
||||
}
|
||||
@@ -991,8 +971,7 @@ TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRange) {
|
||||
options.paranoid_file_checks = true;
|
||||
options.create_if_missing = true;
|
||||
for (bool do_flush : {true, false}) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, options));
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
||||
assert(db_ != nullptr); // suppress false clang-analyze report
|
||||
@@ -1012,12 +991,11 @@ TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRange) {
|
||||
if (do_flush) {
|
||||
ASSERT_OK(db_->Flush(FlushOptions()));
|
||||
} else {
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
ASSERT_OK(
|
||||
dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr));
|
||||
db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr));
|
||||
}
|
||||
db_->ReleaseSnapshot(snap);
|
||||
}
|
||||
@@ -1030,8 +1008,7 @@ TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRangeLast) {
|
||||
options.paranoid_file_checks = true;
|
||||
options.create_if_missing = true;
|
||||
for (bool do_flush : {true, false}) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, options));
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db_));
|
||||
assert(db_ != nullptr); // suppress false clang-analyze report
|
||||
@@ -1048,12 +1025,11 @@ TEST_F(CorruptionTest, ParanoidFileChecksWithDeleteRangeLast) {
|
||||
if (do_flush) {
|
||||
ASSERT_OK(db_->Flush(FlushOptions()));
|
||||
} else {
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
ASSERT_OK(
|
||||
dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr));
|
||||
db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr));
|
||||
}
|
||||
db_->ReleaseSnapshot(snap);
|
||||
}
|
||||
@@ -1066,8 +1042,7 @@ TEST_F(CorruptionTest, LogCorruptionErrorsInCompactionIterator) {
|
||||
options.create_if_missing = true;
|
||||
options.allow_data_in_errors = true;
|
||||
auto mode = mock::MockTableFactory::kCorruptKey;
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, options));
|
||||
|
||||
std::shared_ptr<mock::MockTableFactory> mock =
|
||||
@@ -1079,12 +1054,11 @@ TEST_F(CorruptionTest, LogCorruptionErrorsInCompactionIterator) {
|
||||
assert(db_ != nullptr); // suppress false clang-analyze report
|
||||
Build(100, 2);
|
||||
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
Status s =
|
||||
dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr);
|
||||
db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr);
|
||||
ASSERT_NOK(s);
|
||||
ASSERT_TRUE(s.IsCorruption());
|
||||
}
|
||||
@@ -1095,8 +1069,7 @@ TEST_F(CorruptionTest, CompactionKeyOrderCheck) {
|
||||
options.env = env_.get();
|
||||
options.paranoid_file_checks = false;
|
||||
options.create_if_missing = true;
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, options));
|
||||
std::shared_ptr<mock::MockTableFactory> mock =
|
||||
std::make_shared<mock::MockTableFactory>();
|
||||
@@ -1105,14 +1078,13 @@ TEST_F(CorruptionTest, CompactionKeyOrderCheck) {
|
||||
assert(db_ != nullptr); // suppress false clang-analyze report
|
||||
mock->SetCorruptionMode(mock::MockTableFactory::kCorruptReorderKey);
|
||||
Build(100, 2);
|
||||
DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
|
||||
ASSERT_OK(dbi->TEST_FlushMemTable());
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
|
||||
mock->SetCorruptionMode(mock::MockTableFactory::kCorruptNone);
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
ASSERT_NOK(
|
||||
dbi->CompactRange(cro, dbi->DefaultColumnFamily(), nullptr, nullptr));
|
||||
db_->CompactRange(cro, db_->DefaultColumnFamily(), nullptr, nullptr));
|
||||
}
|
||||
|
||||
TEST_F(CorruptionTest, FlushKeyOrderCheck) {
|
||||
@@ -1139,7 +1111,7 @@ TEST_F(CorruptionTest, FlushKeyOrderCheck) {
|
||||
}
|
||||
});
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
Status s = static_cast_with_check<DBImpl>(db_)->TEST_FlushMemTable();
|
||||
Status s = dbfull()->TEST_FlushMemTable();
|
||||
ASSERT_NOK(s);
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
@@ -1263,7 +1235,7 @@ TEST_P(CrashDuringRecoveryWithCorruptionTest, CrashDuringRecovery) {
|
||||
// while other don't.
|
||||
{
|
||||
ASSERT_OK(DB::Open(options, dbname_, cf_descs, &handles, &db_));
|
||||
auto* dbimpl = static_cast_with_check<DBImpl>(db_);
|
||||
auto* dbimpl = dbfull();
|
||||
assert(dbimpl);
|
||||
|
||||
// Write one key to test_cf.
|
||||
|
||||
+10
-12
@@ -21,18 +21,18 @@ class CuckooTableDBTest : public testing::Test {
|
||||
private:
|
||||
std::string dbname_;
|
||||
Env* env_;
|
||||
DB* db_;
|
||||
std::unique_ptr<DB> db_;
|
||||
|
||||
public:
|
||||
CuckooTableDBTest() : env_(Env::Default()) {
|
||||
dbname_ = test::PerThreadDBPath("cuckoo_table_db_test");
|
||||
EXPECT_OK(DestroyDB(dbname_, Options()));
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
Reopen();
|
||||
}
|
||||
|
||||
~CuckooTableDBTest() override {
|
||||
delete db_;
|
||||
db_.reset();
|
||||
EXPECT_OK(DestroyDB(dbname_, Options()));
|
||||
}
|
||||
|
||||
@@ -47,12 +47,11 @@ class CuckooTableDBTest : public testing::Test {
|
||||
return options;
|
||||
}
|
||||
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_); }
|
||||
DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_.get()); }
|
||||
|
||||
// The following util methods are copied from plain_table_db_test.
|
||||
void Reopen(Options* options = nullptr) {
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
Options opts;
|
||||
if (options != nullptr) {
|
||||
opts = *options;
|
||||
@@ -66,8 +65,7 @@ class CuckooTableDBTest : public testing::Test {
|
||||
void DestroyAndReopen(Options* options) {
|
||||
assert(options);
|
||||
ASSERT_OK(db_->Close());
|
||||
delete db_;
|
||||
db_ = nullptr;
|
||||
db_.reset();
|
||||
ASSERT_OK(DestroyDB(dbname_, *options));
|
||||
Reopen(options);
|
||||
}
|
||||
@@ -130,7 +128,7 @@ TEST_F(CuckooTableDBTest, Flush) {
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
|
||||
TablePropertiesCollection ptc;
|
||||
ASSERT_OK(static_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc));
|
||||
ASSERT_OK(dbfull()->GetPropertiesOfAllTables(&ptc));
|
||||
VerifySstUniqueIds(ptc);
|
||||
ASSERT_EQ(1U, ptc.size());
|
||||
ASSERT_EQ(3U, ptc.begin()->second->num_entries);
|
||||
@@ -147,7 +145,7 @@ TEST_F(CuckooTableDBTest, Flush) {
|
||||
ASSERT_OK(Put("key6", "v6"));
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
|
||||
ASSERT_OK(static_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc));
|
||||
ASSERT_OK(dbfull()->GetPropertiesOfAllTables(&ptc));
|
||||
VerifySstUniqueIds(ptc);
|
||||
ASSERT_EQ(2U, ptc.size());
|
||||
auto row = ptc.begin();
|
||||
@@ -165,7 +163,7 @@ TEST_F(CuckooTableDBTest, Flush) {
|
||||
ASSERT_OK(Delete("key5"));
|
||||
ASSERT_OK(Delete("key4"));
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
ASSERT_OK(static_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc));
|
||||
ASSERT_OK(dbfull()->GetPropertiesOfAllTables(&ptc));
|
||||
VerifySstUniqueIds(ptc);
|
||||
ASSERT_EQ(3U, ptc.size());
|
||||
row = ptc.begin();
|
||||
@@ -190,7 +188,7 @@ TEST_F(CuckooTableDBTest, FlushWithDuplicateKeys) {
|
||||
ASSERT_OK(dbfull()->TEST_FlushMemTable());
|
||||
|
||||
TablePropertiesCollection ptc;
|
||||
ASSERT_OK(static_cast<DB*>(dbfull())->GetPropertiesOfAllTables(&ptc));
|
||||
ASSERT_OK(dbfull()->GetPropertiesOfAllTables(&ptc));
|
||||
VerifySstUniqueIds(ptc);
|
||||
ASSERT_EQ(1U, ptc.size());
|
||||
ASSERT_EQ(2U, ptc.begin()->second->num_entries);
|
||||
|
||||
+158
-40
@@ -91,17 +91,15 @@ class DBBasicTest : public DBTestBase {
|
||||
TEST_F(DBBasicTest, OpenWhenOpen) {
|
||||
Options options = CurrentOptions();
|
||||
options.env = env_;
|
||||
DB* db2 = nullptr;
|
||||
std::unique_ptr<DB> db2;
|
||||
Status s = DB::Open(options, dbname_, &db2);
|
||||
ASSERT_NOK(s) << [db2]() {
|
||||
delete db2;
|
||||
ASSERT_NOK(s) << [&db2]() {
|
||||
db2.reset();
|
||||
return "db2 open: ok";
|
||||
}();
|
||||
ASSERT_EQ(Status::Code::kIOError, s.code());
|
||||
ASSERT_EQ(Status::SubCode::kNone, s.subcode());
|
||||
ASSERT_TRUE(strstr(s.getState(), "lock ") != nullptr);
|
||||
|
||||
delete db2;
|
||||
}
|
||||
|
||||
TEST_F(DBBasicTest, EnableDirectIOWithZeroBuf) {
|
||||
@@ -242,6 +240,43 @@ TEST_F(DBBasicTest, ReadOnlyDB) {
|
||||
Status::Code::kNotSupported);
|
||||
}
|
||||
|
||||
TEST_F(DBBasicTest, ReadOnlyDBFlushWAL) {
|
||||
// Test that FlushWAL returns NotSupported on read-only DB, and that
|
||||
// GetLiveFilesStorageInfo works correctly even with manual_wal_flush=true.
|
||||
// This is a regression test for a bug where GetLiveFilesStorageInfo would
|
||||
// crash on read-only DBs with manual_wal_flush=true because FlushWAL
|
||||
// accessed logs_.back() on an empty deque.
|
||||
auto options = CurrentOptions();
|
||||
options.manual_wal_flush = true;
|
||||
DestroyAndReopen(options);
|
||||
ASSERT_OK(Put("foo", "v1"));
|
||||
ASSERT_OK(Put("bar", "v2"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(Put("baz", "v3")); // Unflushed data in WAL
|
||||
Close();
|
||||
|
||||
// Reopen as read-only
|
||||
ASSERT_OK(ReadOnlyReopen(options));
|
||||
ASSERT_EQ("v1", Get("foo"));
|
||||
ASSERT_EQ("v2", Get("bar"));
|
||||
ASSERT_EQ("v3", Get("baz"));
|
||||
|
||||
// FlushWAL should return NotSupported (not crash)
|
||||
ASSERT_EQ(db_->FlushWAL(/*sync=*/false).code(), Status::Code::kNotSupported);
|
||||
ASSERT_EQ(db_->FlushWAL(/*sync=*/true).code(), Status::Code::kNotSupported);
|
||||
|
||||
// GetLiveFilesStorageInfo should succeed (previously crashed with
|
||||
// manual_wal_flush=true because it called FlushWAL which accessed
|
||||
// logs_.back() on empty deque)
|
||||
LiveFilesStorageInfoOptions lfsi_opts;
|
||||
lfsi_opts.wal_size_for_flush = 0;
|
||||
std::vector<LiveFileStorageInfo> files;
|
||||
ASSERT_OK(db_->GetLiveFilesStorageInfo(lfsi_opts, &files));
|
||||
ASSERT_GT(files.size(), 0);
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
TEST_F(DBBasicTest, ReadOnlyDBWithWriteDBIdToManifestSet) {
|
||||
auto options = CurrentOptions();
|
||||
options.write_dbid_to_manifest = false;
|
||||
@@ -549,14 +584,14 @@ TEST_F(DBBasicTest, GetSnapshot) {
|
||||
|
||||
TEST_F(DBBasicTest, CheckLock) {
|
||||
do {
|
||||
DB* localdb = nullptr;
|
||||
std::unique_ptr<DB> localdb;
|
||||
Options options = CurrentOptions();
|
||||
ASSERT_OK(TryReopen(options));
|
||||
|
||||
// second open should fail
|
||||
Status s = DB::Open(options, dbname_, &localdb);
|
||||
ASSERT_NOK(s) << [localdb]() {
|
||||
delete localdb;
|
||||
ASSERT_NOK(s) << [&localdb]() {
|
||||
localdb.reset();
|
||||
return "localdb open: ok";
|
||||
}();
|
||||
#ifdef OS_LINUX
|
||||
@@ -825,7 +860,7 @@ TEST_F(DBBasicTest, Snapshot) {
|
||||
ASSERT_OK(Put(1, "foo", "1v3"));
|
||||
|
||||
{
|
||||
ManagedSnapshot s3(db_);
|
||||
ManagedSnapshot s3(db_.get());
|
||||
ASSERT_EQ(3U, GetNumSnapshots());
|
||||
ASSERT_EQ(time_snap1, GetTimeOldestSnapshots());
|
||||
ASSERT_EQ(GetSequenceOldestSnapshots(), s1->GetSequenceNumber());
|
||||
@@ -948,7 +983,7 @@ TEST_F(DBBasicTest, DBOpen_Options) {
|
||||
Destroy(options);
|
||||
|
||||
// Does not exist, and create_if_missing == false: error
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
options.create_if_missing = false;
|
||||
Status s = DB::Open(options, dbname_, &db);
|
||||
ASSERT_TRUE(strstr(s.ToString().c_str(), "does not exist") != nullptr);
|
||||
@@ -960,8 +995,7 @@ TEST_F(DBBasicTest, DBOpen_Options) {
|
||||
ASSERT_OK(s);
|
||||
ASSERT_TRUE(db != nullptr);
|
||||
|
||||
delete db;
|
||||
db = nullptr;
|
||||
db.reset();
|
||||
|
||||
// Does exist, and error_if_exists == true: error
|
||||
options.create_if_missing = false;
|
||||
@@ -977,8 +1011,7 @@ TEST_F(DBBasicTest, DBOpen_Options) {
|
||||
ASSERT_OK(s);
|
||||
ASSERT_TRUE(db != nullptr);
|
||||
|
||||
delete db;
|
||||
db = nullptr;
|
||||
db.reset();
|
||||
}
|
||||
|
||||
TEST_F(DBBasicTest, CompactOnFlush) {
|
||||
@@ -1283,7 +1316,7 @@ TEST_F(DBBasicTest, DBClose) {
|
||||
std::string dbname = test::PerThreadDBPath("db_close_test");
|
||||
ASSERT_OK(DestroyDB(dbname, options));
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
TestEnv* env = new TestEnv(env_);
|
||||
std::unique_ptr<TestEnv> local_env_guard(env);
|
||||
options.create_if_missing = true;
|
||||
@@ -1296,14 +1329,14 @@ TEST_F(DBBasicTest, DBClose) {
|
||||
ASSERT_EQ(env->GetCloseCount(), 1);
|
||||
ASSERT_EQ(s, Status::IOError());
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(env->GetCloseCount(), 1);
|
||||
|
||||
// Do not call DB::Close() and ensure our logger Close() still gets called
|
||||
s = DB::Open(options, dbname, &db);
|
||||
ASSERT_OK(s);
|
||||
ASSERT_TRUE(db != nullptr);
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(env->GetCloseCount(), 2);
|
||||
|
||||
// close by WaitForCompact() with close_db option
|
||||
@@ -1318,7 +1351,7 @@ TEST_F(DBBasicTest, DBClose) {
|
||||
// see TestLogger::CloseHelper()
|
||||
ASSERT_EQ(s, Status::IOError());
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(env->GetCloseCount(), 3);
|
||||
|
||||
// Provide our own logger and ensure DB::Close() does not close it
|
||||
@@ -1329,7 +1362,7 @@ TEST_F(DBBasicTest, DBClose) {
|
||||
|
||||
s = db->Close();
|
||||
ASSERT_EQ(s, Status::OK());
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(env->GetCloseCount(), 3);
|
||||
options.info_log.reset();
|
||||
ASSERT_EQ(env->GetCloseCount(), 4);
|
||||
@@ -1347,7 +1380,7 @@ TEST_F(DBBasicTest, DBCloseAllDirectoryFDs) {
|
||||
|
||||
ASSERT_OK(DestroyDB(dbname, options));
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
std::unique_ptr<Env> env = NewCompositeEnv(
|
||||
std::make_shared<CountedFileSystem>(FileSystem::Default()));
|
||||
options.create_if_missing = true;
|
||||
@@ -1365,7 +1398,7 @@ TEST_F(DBBasicTest, DBCloseAllDirectoryFDs) {
|
||||
ASSERT_EQ(counted_fs->counters()->dir_opens,
|
||||
counted_fs->counters()->dir_closes);
|
||||
ASSERT_OK(s);
|
||||
delete db;
|
||||
db.reset();
|
||||
}
|
||||
|
||||
TEST_F(DBBasicTest, DBCloseFlushError) {
|
||||
@@ -1427,7 +1460,7 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCF) {
|
||||
}
|
||||
|
||||
int get_sv_count = 0;
|
||||
ROCKSDB_NAMESPACE::DBImpl* db = static_cast_with_check<DBImpl>(db_);
|
||||
ROCKSDB_NAMESPACE::DBImpl* db = dbfull();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"DBImpl::MultiCFSnapshot::AfterRefSV", [&](void* /*arg*/) {
|
||||
if (++get_sv_count == 2) {
|
||||
@@ -1499,10 +1532,9 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCF) {
|
||||
ASSERT_EQ(values[2], std::get<2>(cf_kv_vec[1]) + "_2");
|
||||
|
||||
for (int cf = 0; cf < 8; ++cf) {
|
||||
auto* cfd =
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
static_cast_with_check<DBImpl>(db_)->GetColumnFamilyHandle(cf))
|
||||
->cfd();
|
||||
auto* cfd = static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
dbfull()->GetColumnFamilyHandle(cf))
|
||||
->cfd();
|
||||
ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse);
|
||||
ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVObsolete);
|
||||
}
|
||||
@@ -1588,10 +1620,9 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFMutex) {
|
||||
"cf" + std::to_string(j) + "_val" + std::to_string(retries));
|
||||
}
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
auto* cfd =
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
static_cast_with_check<DBImpl>(db_)->GetColumnFamilyHandle(i))
|
||||
->cfd();
|
||||
auto* cfd = static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
dbfull()->GetColumnFamilyHandle(i))
|
||||
->cfd();
|
||||
ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse);
|
||||
}
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
@@ -1615,7 +1646,7 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFSnapshot) {
|
||||
}
|
||||
|
||||
int get_sv_count = 0;
|
||||
ROCKSDB_NAMESPACE::DBImpl* db = static_cast_with_check<DBImpl>(db_);
|
||||
ROCKSDB_NAMESPACE::DBImpl* db = dbfull();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"DBImpl::MultiCFSnapshot::AfterRefSV", [&](void* /*arg*/) {
|
||||
if (++get_sv_count == 2) {
|
||||
@@ -1656,10 +1687,9 @@ TEST_P(DBMultiGetTestWithParam, MultiGetMultiCFSnapshot) {
|
||||
ASSERT_EQ(values[j], "cf" + std::to_string(j) + "_val");
|
||||
}
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
auto* cfd =
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
static_cast_with_check<DBImpl>(db_)->GetColumnFamilyHandle(i))
|
||||
->cfd();
|
||||
auto* cfd = static_cast_with_check<ColumnFamilyHandleImpl>(
|
||||
dbfull()->GetColumnFamilyHandle(i))
|
||||
->cfd();
|
||||
ASSERT_NE(cfd->TEST_GetLocalSV()->Get(), SuperVersion::kSVInUse);
|
||||
}
|
||||
}
|
||||
@@ -3264,8 +3294,8 @@ TEST_F(DBBasicTest, GetAllKeyVersions) {
|
||||
ASSERT_OK(Delete(std::to_string(i)));
|
||||
}
|
||||
std::vector<KeyVersion> key_versions;
|
||||
ASSERT_OK(GetAllKeyVersions(db_, {}, {}, std::numeric_limits<size_t>::max(),
|
||||
&key_versions));
|
||||
ASSERT_OK(GetAllKeyVersions(
|
||||
db_.get(), {}, {}, std::numeric_limits<size_t>::max(), &key_versions));
|
||||
ASSERT_EQ(kNumInserts + kNumDeletes + kNumUpdates, key_versions.size());
|
||||
for (size_t i = 0; i < kNumInserts + kNumDeletes + kNumUpdates; i++) {
|
||||
if (i % 3 == 0) {
|
||||
@@ -3274,7 +3304,7 @@ TEST_F(DBBasicTest, GetAllKeyVersions) {
|
||||
ASSERT_EQ(key_versions[i].GetTypeName(), "TypeValue");
|
||||
}
|
||||
}
|
||||
ASSERT_OK(GetAllKeyVersions(db_, handles_[0], {}, {},
|
||||
ASSERT_OK(GetAllKeyVersions(db_.get(), handles_[0], {}, {},
|
||||
std::numeric_limits<size_t>::max(),
|
||||
&key_versions));
|
||||
ASSERT_EQ(kNumInserts + kNumDeletes + kNumUpdates, key_versions.size());
|
||||
@@ -3289,14 +3319,14 @@ TEST_F(DBBasicTest, GetAllKeyVersions) {
|
||||
for (size_t i = 0; i + 1 != kNumDeletes; ++i) {
|
||||
ASSERT_OK(Delete(1, std::to_string(i)));
|
||||
}
|
||||
ASSERT_OK(GetAllKeyVersions(db_, handles_[1], {}, {},
|
||||
ASSERT_OK(GetAllKeyVersions(db_.get(), handles_[1], {}, {},
|
||||
std::numeric_limits<size_t>::max(),
|
||||
&key_versions));
|
||||
ASSERT_EQ(kNumInserts + kNumDeletes + kNumUpdates - 3, key_versions.size());
|
||||
|
||||
// Change from historical behavior: empty key is now interpreted literally as
|
||||
// a legal key (rather than as a "not present" key)
|
||||
ASSERT_OK(GetAllKeyVersions(db_, handles_[1], Slice(), Slice(),
|
||||
ASSERT_OK(GetAllKeyVersions(db_.get(), handles_[1], Slice(), Slice(),
|
||||
std::numeric_limits<size_t>::max(),
|
||||
&key_versions));
|
||||
ASSERT_EQ(key_versions.size(), 0);
|
||||
@@ -5437,6 +5467,94 @@ INSTANTIATE_TEST_CASE_P(DBBasicTestDeadline, DBBasicTestDeadline,
|
||||
::testing::Values(std::make_tuple(true, false),
|
||||
std::make_tuple(false, true),
|
||||
std::make_tuple(true, true)));
|
||||
|
||||
// FileSystemWrapper that captures FileOptions passed to NewRandomAccessFile
|
||||
// for .sst files, so we can verify file_checksum fields are populated.
|
||||
class ChecksumCapturingFS : public FileSystemWrapper {
|
||||
public:
|
||||
explicit ChecksumCapturingFS(const std::shared_ptr<FileSystem>& base)
|
||||
: FileSystemWrapper(base) {}
|
||||
|
||||
static const char* kClassName() { return "ChecksumCapturingFS"; }
|
||||
const char* Name() const override { return kClassName(); }
|
||||
|
||||
IOStatus NewRandomAccessFile(const std::string& fname,
|
||||
const FileOptions& opts,
|
||||
std::unique_ptr<FSRandomAccessFile>* result,
|
||||
IODebugContext* dbg) override {
|
||||
if (fname.find(".sst") != std::string::npos) {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
captured_file_checksum_ = opts.file_checksum;
|
||||
captured_file_checksum_func_name_ = opts.file_checksum_func_name;
|
||||
capture_count_++;
|
||||
}
|
||||
return target()->NewRandomAccessFile(fname, opts, result, dbg);
|
||||
}
|
||||
|
||||
std::string GetCapturedFileChecksum() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
return captured_file_checksum_;
|
||||
}
|
||||
|
||||
std::string GetCapturedFileChecksumFuncName() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
return captured_file_checksum_func_name_;
|
||||
}
|
||||
|
||||
int GetCaptureCount() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
return capture_count_;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
captured_file_checksum_.clear();
|
||||
captured_file_checksum_func_name_.clear();
|
||||
capture_count_ = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mu_;
|
||||
std::string captured_file_checksum_;
|
||||
std::string captured_file_checksum_func_name_;
|
||||
int capture_count_ = 0;
|
||||
};
|
||||
|
||||
TEST_F(DBBasicTest, FileChecksumInFileOptions) {
|
||||
// Verify that file_checksum and file_checksum_func_name from FileMetaData
|
||||
// are propagated through FileOptions when opening SST files.
|
||||
auto capturing_fs =
|
||||
std::make_shared<ChecksumCapturingFS>(env_->GetFileSystem());
|
||||
std::unique_ptr<Env> env(new CompositeEnvWrapper(env_, capturing_fs));
|
||||
|
||||
Options options = GetDefaultOptions();
|
||||
options.create_if_missing = true;
|
||||
options.env = env.get();
|
||||
options.file_checksum_gen_factory = GetFileChecksumGenCrc32cFactory();
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Write data and flush to create an SST with a file checksum.
|
||||
ASSERT_OK(Put("key1", "value1"));
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Reset captures, then reopen to trigger TableCache SST open.
|
||||
capturing_fs->Reset();
|
||||
Reopen(options);
|
||||
|
||||
// Read to trigger SST open through TableCache::GetTableReader.
|
||||
ASSERT_EQ("value1", Get("key1"));
|
||||
|
||||
// Verify that checksum fields were populated.
|
||||
ASSERT_GT(capturing_fs->GetCaptureCount(), 0);
|
||||
ASSERT_FALSE(capturing_fs->GetCapturedFileChecksum().empty());
|
||||
ASSERT_NE(capturing_fs->GetCapturedFileChecksumFuncName(),
|
||||
capturing_fs->GetCapturedFileChecksum());
|
||||
ASSERT_EQ(capturing_fs->GetCapturedFileChecksumFuncName(),
|
||||
"FileChecksumCrc32c");
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -1661,7 +1661,7 @@ TEST_P(DBBlockCacheKeyTest, StableCacheKeys) {
|
||||
std::string export_files_dir = dbname_ + "/exported";
|
||||
ExportImportFilesMetaData* metadata_ptr_ = nullptr;
|
||||
Checkpoint* checkpoint;
|
||||
ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
|
||||
ASSERT_OK(Checkpoint::Create(db_.get(), &checkpoint));
|
||||
ASSERT_OK(checkpoint->ExportColumnFamily(handles_[1], export_files_dir,
|
||||
&metadata_ptr_));
|
||||
ASSERT_NE(metadata_ptr_, nullptr);
|
||||
@@ -1698,7 +1698,7 @@ TEST_P(DBBlockCacheKeyTest, StableCacheKeys) {
|
||||
// StableCacheKeyTestFS, Checkpoint will resort to full copy not hard link.
|
||||
// (Checkpoint not available in LITE mode to test this.)
|
||||
auto db_copy_name = dbname_ + "-copy";
|
||||
ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
|
||||
ASSERT_OK(Checkpoint::Create(db_.get(), &checkpoint));
|
||||
ASSERT_OK(checkpoint->CreateCheckpoint(db_copy_name));
|
||||
delete checkpoint;
|
||||
|
||||
|
||||
@@ -137,11 +137,6 @@ class SliceTransformLimitedDomainGeneric : public SliceTransform {
|
||||
// prefix will be x????
|
||||
return src.size() >= 5;
|
||||
}
|
||||
|
||||
bool InRange(const Slice& dst) const override {
|
||||
// prefix will be x????
|
||||
return dst.size() == 5;
|
||||
}
|
||||
};
|
||||
|
||||
// KeyMayExist can lead to a few false positives, but not false negatives.
|
||||
@@ -922,14 +917,14 @@ INSTANTIATE_TEST_CASE_P(
|
||||
::testing::Values(
|
||||
std::make_tuple(kAutoBloom,
|
||||
FilterPartitioning::kCoupledPartitionedFilter,
|
||||
kLatestFormatVersion),
|
||||
kLatestBbtFormatVersion),
|
||||
std::make_tuple(kAutoBloom,
|
||||
FilterPartitioning::kDecoupledPartitionedFilter,
|
||||
kLatestFormatVersion),
|
||||
kLatestBbtFormatVersion),
|
||||
std::make_tuple(kAutoBloom, FilterPartitioning::kUnpartitionedFilter,
|
||||
kLatestFormatVersion),
|
||||
kLatestBbtFormatVersion),
|
||||
std::make_tuple(kAutoRibbon, FilterPartitioning::kUnpartitionedFilter,
|
||||
kLatestFormatVersion)));
|
||||
kLatestBbtFormatVersion)));
|
||||
#endif // !defined(ROCKSDB_VALGRIND_RUN) || defined(ROCKSDB_FULL_VALGRIND_RUN)
|
||||
|
||||
TEST_F(DBBloomFilterTest, BloomFilterRate) {
|
||||
@@ -2077,11 +2072,6 @@ class SliceTransformLimitedDomain : public SliceTransform {
|
||||
// prefix will be x????
|
||||
return src.size() >= 5 && src[0] == 'x';
|
||||
}
|
||||
|
||||
bool InRange(const Slice& dst) const override {
|
||||
// prefix will be x????
|
||||
return dst.size() == 5 && dst[0] == 'x';
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(DBBloomFilterTest, PrefixExtractorWithFilter1) {
|
||||
@@ -4145,7 +4135,7 @@ TEST_F(DBBloomFilterTest, SstQueryFilter) {
|
||||
|
||||
using Keys = std::vector<std::string>;
|
||||
auto RangeQuery =
|
||||
[factory, db = db_](
|
||||
[factory, db = db_.get()](
|
||||
std::string lb, std::string ub,
|
||||
std::shared_ptr<SstQueryFilterConfigsManager::Factory> alt_factory =
|
||||
nullptr) {
|
||||
|
||||
@@ -0,0 +1,993 @@
|
||||
// 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 <atomic>
|
||||
#include <set>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "db/compaction/compaction_job.h"
|
||||
#include "db/db_impl/db_impl_secondary.h"
|
||||
#include "db/db_test_util.h"
|
||||
#include "options/options_helper.h"
|
||||
#include "port/stack_trace.h"
|
||||
#include "rocksdb/db.h"
|
||||
#include "rocksdb/sst_file_writer.h"
|
||||
#include "test_util/sync_point.h"
|
||||
#include "test_util/testharness.h"
|
||||
|
||||
namespace ROCKSDB_NAMESPACE {
|
||||
|
||||
// Helper class to manage abort synchronization in tests.
|
||||
//
|
||||
// Compaction abort could happen at various stage of compaction.
|
||||
// To test this, we need to trigger abort at different stage. This requires
|
||||
// precise control on the timing of abort API invocation. To achieve this in a
|
||||
// consistent way across various tests, we invoke AbortAllCompactions() within
|
||||
// the sync point callback, that is added at various stages of compaction.
|
||||
// However as the abort API is a blocking call, calling it within the sync point
|
||||
// callback on the compaction thread would cause deadlock. This test helper
|
||||
// class is designed to solve this challenge.
|
||||
//
|
||||
// 1. Abort must happen from a different thread:
|
||||
// AbortAllCompactions() is typically called from the compaction thread
|
||||
// via a sync point callback, so that we could precisely control the time of
|
||||
// API invocation to simulate abort at different stage of compaction.
|
||||
// However, we can't block the compaction thread waiting for the abort to
|
||||
// complete - the compaction needs to continue executing to actually check
|
||||
// the abort flag and exit. So we spawn a separate thread to call
|
||||
// AbortAllCompactions().
|
||||
//
|
||||
// 2. We need to know when abort completes:
|
||||
// After compaction returns (with aborted status), we often need to:
|
||||
// - Verify state (e.g., no output files created)
|
||||
// - Call ResumeAllCompactions()
|
||||
// - Run compaction again to verify it succeeds
|
||||
// We must wait for the abort thread to finish before proceeding, otherwise
|
||||
// we might call Resume before Abort completes, causing race conditions.
|
||||
//
|
||||
// 3. Sync point callbacks may fire multiple times:
|
||||
// With multiple subcompactions, a callback like
|
||||
// "CompactionJob::ProcessKeyValueCompaction:Start" fires once per
|
||||
// subcompaction. We only want to trigger abort once, so we use
|
||||
// abort_triggered_ as a guard.
|
||||
//
|
||||
// 4. Tests may need multiple abort cycles:
|
||||
// Some tests (e.g., MultipleAbortResumeSequence) do abort->resume->abort
|
||||
// multiple times. The class supports this by auto-resetting when a
|
||||
// previous abort has completed.
|
||||
class AbortSynchronizer {
|
||||
public:
|
||||
AbortSynchronizer() : abort_cv_(&abort_mutex_) {}
|
||||
|
||||
~AbortSynchronizer() {
|
||||
// Join the thread if it was started - ensures clean shutdown
|
||||
if (abort_thread_.joinable()) {
|
||||
abort_thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
// Non-copyable, non-movable due to thread member
|
||||
AbortSynchronizer(const AbortSynchronizer&) = delete;
|
||||
AbortSynchronizer& operator=(const AbortSynchronizer&) = delete;
|
||||
|
||||
// Trigger abort from a separate thread.
|
||||
// - Safe to call multiple times; only first call in each cycle spawns thread
|
||||
// - If a previous abort has completed, automatically resets state first
|
||||
// - The spawned thread calls AbortAllCompactions() and signals completion
|
||||
void TriggerAbort(DBImpl* db) {
|
||||
// If previous abort completed, reset state to allow new abort
|
||||
if (abort_triggered_.load() && abort_completed_.load()) {
|
||||
Reset();
|
||||
}
|
||||
|
||||
if (!abort_triggered_.exchange(true)) {
|
||||
abort_thread_ = std::thread([this, db]() {
|
||||
db->AbortAllCompactions();
|
||||
SignalAbortCompleted();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the abort thread to complete.
|
||||
// Call this AFTER compaction returns to ensure the abort thread has finished
|
||||
// before proceeding with Resume or other operations.
|
||||
void WaitForAbortCompletion() {
|
||||
MutexLock l(&abort_mutex_);
|
||||
while (!abort_completed_.load()) {
|
||||
abort_cv_.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
// Reset state for reuse. Joins any previous thread first.
|
||||
// Called automatically by TriggerAbort() if previous abort completed,
|
||||
// but can also be called explicitly for clarity.
|
||||
void Reset() {
|
||||
if (abort_thread_.joinable()) {
|
||||
abort_thread_.join();
|
||||
}
|
||||
abort_triggered_.store(false);
|
||||
abort_completed_.store(false);
|
||||
}
|
||||
|
||||
bool IsAbortTriggered() const { return abort_triggered_.load(); }
|
||||
|
||||
private:
|
||||
void SignalAbortCompleted() {
|
||||
MutexLock l(&abort_mutex_);
|
||||
abort_completed_.store(true);
|
||||
abort_cv_.SignalAll();
|
||||
}
|
||||
|
||||
std::atomic<bool> abort_triggered_{false}; // Guards against multiple spawns
|
||||
std::atomic<bool> abort_completed_{false}; // Signals thread completion
|
||||
port::Mutex abort_mutex_;
|
||||
port::CondVar abort_cv_;
|
||||
std::thread abort_thread_; // The thread that calls AbortAllCompactions()
|
||||
};
|
||||
|
||||
// Helper to clean up SyncPoint state after tests
|
||||
inline void CleanupSyncPoints() {
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
||||
}
|
||||
|
||||
// Helper class that combines AbortSynchronizer with sync point setup for
|
||||
// deterministic abort triggering. This adds sync point coordination on top
|
||||
// of AbortSynchronizer:
|
||||
//
|
||||
// This is useful when you need deterministic timing - the callback won't
|
||||
// return until AbortAllCompactions() has actually set the abort flag,
|
||||
// guaranteeing the compaction will see it on the next check.
|
||||
class SyncPointAbortHelper {
|
||||
public:
|
||||
explicit SyncPointAbortHelper(const std::string& trigger_point)
|
||||
: trigger_point_(trigger_point) {}
|
||||
|
||||
// Set up sync points and callbacks. Call this before starting compaction.
|
||||
void Setup(DBImpl* db_impl) {
|
||||
db_impl_ = db_impl;
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
||||
{"DBImpl::AbortAllCompactions:FlagSet", kWaitPointName},
|
||||
});
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
trigger_point_, [this](void* /*arg*/) {
|
||||
// Use AbortSynchronizer to handle the abort in a separate thread
|
||||
abort_sync_.TriggerAbort(db_impl_);
|
||||
|
||||
// Wait for abort flag to be set via sync point dependency
|
||||
// This ensures deterministic timing - compaction will see the flag
|
||||
TEST_SYNC_POINT_CALLBACK(kWaitPointName, nullptr);
|
||||
});
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
}
|
||||
|
||||
// Wait for the abort to complete. Call this after compaction returns.
|
||||
void WaitForAbortCompletion() { abort_sync_.WaitForAbortCompletion(); }
|
||||
|
||||
// Clean up sync points and wait for abort completion in one call
|
||||
void CleanupAndWait() {
|
||||
CleanupSyncPoints();
|
||||
WaitForAbortCompletion();
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr const char* kWaitPointName =
|
||||
"SyncPointAbortHelper::WaitForAbort";
|
||||
std::string trigger_point_;
|
||||
DBImpl* db_impl_{nullptr};
|
||||
AbortSynchronizer abort_sync_;
|
||||
};
|
||||
|
||||
class DBCompactionAbortTest : public DBTestBase {
|
||||
public:
|
||||
DBCompactionAbortTest()
|
||||
: DBTestBase("db_compaction_abort_test", /*env_do_fsync=*/false) {}
|
||||
|
||||
protected:
|
||||
// Map to track the latest value of each key for verification
|
||||
std::unordered_map<std::string, std::string> expected_values_;
|
||||
|
||||
// Statistics object for verifying compaction metrics
|
||||
std::shared_ptr<Statistics> stats_;
|
||||
|
||||
// Get current options with statistics enabled
|
||||
Options GetOptionsWithStats() {
|
||||
Options options = CurrentOptions();
|
||||
stats_ = CreateDBStatistics();
|
||||
options.statistics = stats_;
|
||||
return options;
|
||||
}
|
||||
|
||||
// Populate database with test data.
|
||||
// If overlapping=true, uses the same key range (0 to keys_per_file-1) in each
|
||||
// file to ensure compaction has work to do.
|
||||
// If overlapping=false, uses non-overlapping keys across files.
|
||||
void PopulateData(int num_files, int keys_per_file, int value_size,
|
||||
bool overlapping = true, int seed = 301) {
|
||||
Random rnd(seed);
|
||||
for (int i = 0; i < num_files; ++i) {
|
||||
for (int j = 0; j < keys_per_file; ++j) {
|
||||
int key_index = overlapping ? j : (j + i * keys_per_file);
|
||||
std::string key = Key(key_index);
|
||||
std::string value = rnd.RandomString(value_size);
|
||||
ASSERT_OK(Put(key, value));
|
||||
expected_values_[key] = value;
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
}
|
||||
}
|
||||
|
||||
// Verify data integrity by reading all keys and comparing with expected
|
||||
// values
|
||||
void VerifyDataIntegrity(int num_keys, int start_key = 0) {
|
||||
std::string val;
|
||||
for (int j = start_key; j < start_key + num_keys; ++j) {
|
||||
std::string key = Key(j);
|
||||
ASSERT_OK(dbfull()->Get(ReadOptions(), key, &val));
|
||||
auto it = expected_values_.find(key);
|
||||
if (it != expected_values_.end()) {
|
||||
ASSERT_EQ(it->second, val) << "Value mismatch for key: " << key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear expected values (useful when reopening DB or between tests)
|
||||
void ClearExpectedValues() { expected_values_.clear(); }
|
||||
|
||||
// Run the common abort test pattern with SyncPointAbortHelper:
|
||||
// 1. Set up sync point abort helper
|
||||
// 2. Run compaction and verify it's aborted
|
||||
// 3. Verify COMPACTION_ABORTED stat increased (if stats enabled)
|
||||
// 4. Clean up, resume, and verify compaction succeeds
|
||||
// 5. Verify COMPACT_WRITE_BYTES increased (if stats enabled)
|
||||
void RunSyncPointAbortTest(const std::string& trigger_point,
|
||||
CompactRangeOptions cro = CompactRangeOptions()) {
|
||||
// Capture stats and file counts before abort
|
||||
uint64_t aborted_before = 0;
|
||||
uint64_t write_bytes_before = 0;
|
||||
if (stats_) {
|
||||
aborted_before = stats_->getTickerCount(COMPACTION_ABORTED);
|
||||
write_bytes_before = stats_->getTickerCount(COMPACT_WRITE_BYTES);
|
||||
}
|
||||
|
||||
SyncPointAbortHelper helper(trigger_point);
|
||||
helper.Setup(dbfull());
|
||||
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
// Verify abort was counted
|
||||
if (stats_) {
|
||||
uint64_t aborted_after = stats_->getTickerCount(COMPACTION_ABORTED);
|
||||
ASSERT_GT(aborted_after, aborted_before)
|
||||
<< "COMPACTION_ABORTED stat should increase after abort";
|
||||
}
|
||||
|
||||
helper.CleanupAndWait();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
// Verify compaction completed and wrote bytes
|
||||
if (stats_) {
|
||||
uint64_t write_bytes_after = stats_->getTickerCount(COMPACT_WRITE_BYTES);
|
||||
ASSERT_GT(write_bytes_after, write_bytes_before)
|
||||
<< "COMPACT_WRITE_BYTES should increase after successful compaction";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Parameterized test for abort with different number of max subcompactions.
|
||||
// This consolidates tests that were essentially duplicates with different
|
||||
// max_subcompactions values
|
||||
class DBCompactionAbortSubcompactionTest
|
||||
: public DBCompactionAbortTest,
|
||||
public ::testing::WithParamInterface<int> {};
|
||||
|
||||
TEST_P(DBCompactionAbortSubcompactionTest, AbortWithVaryingSubcompactions) {
|
||||
int max_subcompactions = GetParam();
|
||||
|
||||
Options options = GetOptionsWithStats();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = max_subcompactions;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/100);
|
||||
|
||||
RunSyncPointAbortTest("CompactionJob::RunSubcompactions:BeforeStart");
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(SubcompactionVariants,
|
||||
DBCompactionAbortSubcompactionTest,
|
||||
::testing::Values(1, 2, 4),
|
||||
[](const ::testing::TestParamInfo<int>& param_info) {
|
||||
return "MaxSubcompactionCount_" +
|
||||
std::to_string(param_info.param);
|
||||
});
|
||||
|
||||
// Parameterized test for abort with different compaction styles
|
||||
// This consolidates tests for Level, Universal, and FIFO compaction styles
|
||||
class DBCompactionAbortStyleTest
|
||||
: public DBCompactionAbortTest,
|
||||
public ::testing::WithParamInterface<CompactionStyle> {
|
||||
protected:
|
||||
// Configure options based on compaction style
|
||||
void ConfigureOptionsForStyle(Options& options, CompactionStyle style) {
|
||||
options.compaction_style = style;
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.disable_auto_compactions = true;
|
||||
|
||||
switch (style) {
|
||||
case kCompactionStyleLevel:
|
||||
// Level compaction uses default settings
|
||||
break;
|
||||
case kCompactionStyleUniversal:
|
||||
options.compaction_options_universal.size_ratio = 10;
|
||||
break;
|
||||
case kCompactionStyleFIFO:
|
||||
// Set a large max_table_files_size to avoid deletion compaction
|
||||
options.compaction_options_fifo.max_table_files_size =
|
||||
100 * 1024 * 1024;
|
||||
// Enable intra-L0 compaction which goes through normal compaction path
|
||||
options.compaction_options_fifo.allow_compaction = true;
|
||||
options.max_open_files = -1; // Required for FIFO compaction
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(DBCompactionAbortStyleTest, AbortCompaction) {
|
||||
CompactionStyle style = GetParam();
|
||||
|
||||
Options options = GetOptionsWithStats();
|
||||
options.max_subcompactions = 1;
|
||||
ConfigureOptionsForStyle(options, style);
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/100);
|
||||
|
||||
RunSyncPointAbortTest("CompactionJob::RunSubcompactions:BeforeStart");
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(
|
||||
CompactionStyleVariants, DBCompactionAbortStyleTest,
|
||||
::testing::Values(kCompactionStyleLevel, kCompactionStyleUniversal,
|
||||
kCompactionStyleFIFO),
|
||||
[](const ::testing::TestParamInfo<CompactionStyle>& param_info) {
|
||||
return OptionsHelper::compaction_style_to_string.at(param_info.param);
|
||||
});
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortManualCompaction) {
|
||||
Options options = GetOptionsWithStats();
|
||||
options.level0_file_num_compaction_trigger = 10;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/5, /*keys_per_file=*/100, /*value_size=*/1000);
|
||||
|
||||
CompactRangeOptions cro;
|
||||
cro.exclusive_manual_compaction = true;
|
||||
RunSyncPointAbortTest("CompactionJob::ProcessKeyValueCompaction:Start", cro);
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortAutomaticCompaction) {
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = 2;
|
||||
options.disable_auto_compactions = false;
|
||||
Reopen(options);
|
||||
|
||||
Random rnd(301);
|
||||
AbortSynchronizer abort_sync;
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"CompactionJob::ProcessKeyValueCompaction:Start",
|
||||
[&](void* /*arg*/) { abort_sync.TriggerAbort(dbfull()); });
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 100; ++j) {
|
||||
ASSERT_OK(Put(Key(j), rnd.RandomString(1000)));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
}
|
||||
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
|
||||
CleanupSyncPoints();
|
||||
|
||||
abort_sync.WaitForAbortCompletion();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
for (int j = 0; j < 100; ++j) {
|
||||
ASSERT_OK(Put(Key(j), rnd.RandomString(1000)));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
|
||||
std::string val;
|
||||
for (int j = 0; j < 100; ++j) {
|
||||
ASSERT_OK(dbfull()->Get(ReadOptions(), Key(j), &val));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortAndVerifyNoOutputFiles) {
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/1000);
|
||||
|
||||
int num_l0_files_before = NumTableFilesAtLevel(0);
|
||||
int num_l1_files_before = NumTableFilesAtLevel(1);
|
||||
|
||||
SyncPointAbortHelper helper("CompactionJob::ProcessKeyValueCompaction:Start");
|
||||
helper.Setup(dbfull());
|
||||
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
CleanupSyncPoints();
|
||||
|
||||
int num_l0_files_after = NumTableFilesAtLevel(0);
|
||||
int num_l1_files_after = NumTableFilesAtLevel(1);
|
||||
|
||||
ASSERT_EQ(num_l0_files_before, num_l0_files_after);
|
||||
ASSERT_EQ(num_l1_files_before, num_l1_files_after);
|
||||
|
||||
helper.WaitForAbortCompletion();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
int num_l0_files_final = NumTableFilesAtLevel(0);
|
||||
int num_l1_files_final = NumTableFilesAtLevel(1);
|
||||
|
||||
ASSERT_EQ(0, num_l0_files_final);
|
||||
ASSERT_GT(num_l1_files_final, 0);
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, MultipleAbortResumeSequence) {
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/1000);
|
||||
|
||||
for (int round = 0; round < 3; ++round) {
|
||||
// Use SyncPointAbortHelper for deterministic abort timing - it waits
|
||||
// for the abort flag to be set via sync point dependency
|
||||
SyncPointAbortHelper helper(
|
||||
"CompactionJob::ProcessKeyValueCompaction:Start");
|
||||
helper.Setup(dbfull());
|
||||
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
helper.CleanupAndWait();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
}
|
||||
|
||||
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortWithOutputFilesCleanup) {
|
||||
Options options = CurrentOptions();
|
||||
options.num_levels = 2; // Ensure compaction output goes to L1
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
options.target_file_size_base = 50 * 1024;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/100);
|
||||
|
||||
SyncPointAbortHelper helper("CompactionJob::RunSubcompactions:BeforeStart");
|
||||
helper.Setup(dbfull());
|
||||
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
CleanupSyncPoints();
|
||||
|
||||
int num_l1_files_after_abort = NumTableFilesAtLevel(1);
|
||||
ASSERT_EQ(0, num_l1_files_after_abort);
|
||||
|
||||
helper.WaitForAbortCompletion();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
// Verify L0 files are compacted and L1 has output files
|
||||
int num_l0_files_final = NumTableFilesAtLevel(0);
|
||||
int num_l1_files_final = NumTableFilesAtLevel(1);
|
||||
ASSERT_EQ(0, num_l0_files_final)
|
||||
<< "L0 should be empty after successful compaction";
|
||||
ASSERT_GT(num_l1_files_final, 0)
|
||||
<< "L1 should have files after successful compaction";
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, NestedAbortResumeCalls) {
|
||||
// Test that nested AbortAllCompactions() calls work correctly with the
|
||||
// counter
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/1000);
|
||||
|
||||
// First abort call
|
||||
dbfull()->AbortAllCompactions();
|
||||
|
||||
// Nested abort call (counter should be 2)
|
||||
dbfull()->AbortAllCompactions();
|
||||
|
||||
// Compaction should still be blocked after one resume
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
// Compaction should still return aborted because counter is still 1
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
// Second resume - counter should be 0 now
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
// Compaction should succeed now
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortCompactFilesAPI) {
|
||||
// Test that AbortAllCompactions works with CompactFiles API
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 100; // Disable auto compaction
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/1000);
|
||||
|
||||
// Get the L0 file names
|
||||
std::vector<std::string> files_to_compact;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
dbfull()->GetColumnFamilyMetaData(dbfull()->DefaultColumnFamily(), &cf_meta);
|
||||
for (const auto& file : cf_meta.levels[0].files) {
|
||||
files_to_compact.push_back(file.name);
|
||||
}
|
||||
ASSERT_GE(files_to_compact.size(), 2);
|
||||
|
||||
SyncPointAbortHelper helper("CompactionJob::ProcessKeyValueCompaction:Start");
|
||||
helper.Setup(dbfull());
|
||||
|
||||
CompactionOptions compact_options;
|
||||
Status s = dbfull()->CompactFiles(compact_options, files_to_compact, 1);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
helper.CleanupAndWait();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
// CompactFiles should work after resume
|
||||
ASSERT_OK(dbfull()->CompactFiles(compact_options, files_to_compact, 1));
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortDoesNotAffectFlush) {
|
||||
// Test that AbortAllCompactions does not affect flush operations
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 100;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
Random rnd(301);
|
||||
for (int j = 0; j < 100; ++j) {
|
||||
ASSERT_OK(Put(Key(j), rnd.RandomString(1000)));
|
||||
}
|
||||
|
||||
// Abort compactions
|
||||
dbfull()->AbortAllCompactions();
|
||||
|
||||
// Flush should still work
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Write more data
|
||||
for (int j = 100; j < 200; ++j) {
|
||||
ASSERT_OK(Put(Key(j), rnd.RandomString(1000)));
|
||||
}
|
||||
|
||||
// Flush should still work
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
// Resume compactions
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/200);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortBeforeCompactionStarts) {
|
||||
// Test aborting before any compaction has started
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/1000);
|
||||
|
||||
// Abort before starting compaction
|
||||
dbfull()->AbortAllCompactions();
|
||||
|
||||
// Compaction should immediately return aborted
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
// Resume
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
// Now compaction should work
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
// Verify L0 files are compacted
|
||||
ASSERT_EQ(0, NumTableFilesAtLevel(0));
|
||||
}
|
||||
|
||||
// Test that in-progress blob and SST files are properly cleaned up when
|
||||
// compaction is aborted. This specifically tests the case where abort happens
|
||||
// while files are being written (opened but not yet completed/closed).
|
||||
// This catches the bug where files exist on disk but are removed from the
|
||||
// outputs_ vector (e.g., by RemoveLastEmptyOutput when file_size is 0 because
|
||||
// the builder was abandoned), leaving orphan files.
|
||||
TEST_F(DBCompactionAbortTest, AbortWithInProgressFileCleanup) {
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions =
|
||||
1; // Single subcompaction for deterministic behavior
|
||||
options.disable_auto_compactions = true;
|
||||
options.target_file_size_base = 32 * 1024; // 32KB
|
||||
|
||||
// Enable BlobDB with garbage collection to force blob rewriting during
|
||||
// compaction
|
||||
options.enable_blob_files = true;
|
||||
options.min_blob_size = 0; // All values go to blob files
|
||||
options.blob_file_size =
|
||||
1024 * 1024; // 1MB - large enough to not close during test
|
||||
// Enable blob garbage collection - this forces blob data to be rewritten
|
||||
// during compaction, creating new blob files
|
||||
options.enable_blob_garbage_collection = true;
|
||||
options.blob_garbage_collection_age_cutoff = 1.0; // Include all blob files
|
||||
options.blob_garbage_collection_force_threshold = 0.0; // Always force GC
|
||||
|
||||
Reopen(options);
|
||||
|
||||
// Write enough data to trigger the periodic abort check (every 1000 records).
|
||||
// 4 files * 2000 keys = 2000 unique overlapping keys processed during
|
||||
// compaction. The sync point triggers at 999, 1999, etc.
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/2000, /*value_size=*/500);
|
||||
|
||||
// Helper function to get blob files on disk with their names
|
||||
auto GetBlobFilesOnDisk = [this]() -> std::vector<std::string> {
|
||||
std::vector<std::string> blob_files;
|
||||
std::vector<std::string> files;
|
||||
EXPECT_OK(env_->GetChildren(dbname_, &files));
|
||||
for (const auto& f : files) {
|
||||
if (f.find(".blob") != std::string::npos) {
|
||||
blob_files.push_back(f);
|
||||
}
|
||||
}
|
||||
std::sort(blob_files.begin(), blob_files.end());
|
||||
return blob_files;
|
||||
};
|
||||
|
||||
// Helper function to get blob file count in metadata
|
||||
auto GetBlobFilesInMetadata = [this]() -> std::vector<uint64_t> {
|
||||
std::vector<uint64_t> blob_file_numbers;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
dbfull()->GetColumnFamilyMetaData(db_->DefaultColumnFamily(), &cf_meta);
|
||||
for (const auto& blob_meta : cf_meta.blob_files) {
|
||||
blob_file_numbers.push_back(blob_meta.blob_file_number);
|
||||
}
|
||||
std::sort(blob_file_numbers.begin(), blob_file_numbers.end());
|
||||
return blob_file_numbers;
|
||||
};
|
||||
|
||||
// Helper function to get SST files on disk
|
||||
auto GetSstFilesOnDisk = [this]() -> std::vector<std::string> {
|
||||
std::vector<std::string> sst_files;
|
||||
std::vector<std::string> files;
|
||||
EXPECT_OK(env_->GetChildren(dbname_, &files));
|
||||
for (const auto& f : files) {
|
||||
if (f.find(".sst") != std::string::npos) {
|
||||
sst_files.push_back(f);
|
||||
}
|
||||
}
|
||||
std::sort(sst_files.begin(), sst_files.end());
|
||||
return sst_files;
|
||||
};
|
||||
|
||||
// Helper function to get SST file numbers in metadata
|
||||
auto GetSstFilesInMetadata = [this]() -> std::vector<uint64_t> {
|
||||
std::vector<uint64_t> sst_file_numbers;
|
||||
ColumnFamilyMetaData cf_meta;
|
||||
dbfull()->GetColumnFamilyMetaData(db_->DefaultColumnFamily(), &cf_meta);
|
||||
for (const auto& level : cf_meta.levels) {
|
||||
for (const auto& file : level.files) {
|
||||
// Extract file number from the file name (e.g., "000010.sst" -> 10)
|
||||
uint64_t file_num = 0;
|
||||
std::string fname = file.name;
|
||||
// Remove leading path separators if present
|
||||
size_t pos = fname.rfind('/');
|
||||
if (pos != std::string::npos) {
|
||||
fname = fname.substr(pos + 1);
|
||||
}
|
||||
if (sscanf(fname.c_str(), "%" PRIu64, &file_num) == 1) {
|
||||
sst_file_numbers.push_back(file_num);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::sort(sst_file_numbers.begin(), sst_file_numbers.end());
|
||||
return sst_file_numbers;
|
||||
};
|
||||
|
||||
std::vector<std::string> initial_blob_files = GetBlobFilesOnDisk();
|
||||
std::vector<uint64_t> initial_meta_blobs = GetBlobFilesInMetadata();
|
||||
std::vector<std::string> initial_sst_files = GetSstFilesOnDisk();
|
||||
std::vector<uint64_t> initial_meta_ssts = GetSstFilesInMetadata();
|
||||
|
||||
ASSERT_GT(initial_blob_files.size(), 0u) << "Expected initial blob files";
|
||||
ASSERT_EQ(initial_blob_files.size(), initial_meta_blobs.size())
|
||||
<< "Initial blob files should match between disk and metadata";
|
||||
ASSERT_GT(initial_sst_files.size(), 0u) << "Expected initial SST files";
|
||||
ASSERT_EQ(initial_sst_files.size(), initial_meta_ssts.size())
|
||||
<< "Initial SST files should match between disk and metadata";
|
||||
|
||||
// Tracking variables for blob file lifecycle
|
||||
std::atomic<int> blob_writes{0};
|
||||
std::atomic<bool> abort_triggered{false};
|
||||
AbortSynchronizer abort_sync;
|
||||
|
||||
// Set up dependency: the wait point will block until FlagSet is hit
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
||||
{"DBImpl::AbortAllCompactions:FlagSet",
|
||||
"DBCompactionAbortTest::InProgressBlob:WaitForAbort"},
|
||||
});
|
||||
|
||||
// Trigger abort after some blob writes during compaction output.
|
||||
// This ensures we have an in-progress blob file when abort happens.
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"BlobFileBuilder::WriteBlobToFile:AddRecord", [&](void* /*arg*/) {
|
||||
int count = blob_writes.fetch_add(1) + 1;
|
||||
|
||||
// Trigger abort after 100 blob writes - this ensures:
|
||||
// 1. A blob file has been opened (for writing)
|
||||
// 2. Some data has been written to it
|
||||
// 3. But it's not yet completed (blob_file_size is 1MB)
|
||||
if (count == 100 && !abort_triggered.exchange(true)) {
|
||||
abort_sync.TriggerAbort(dbfull());
|
||||
// Wait for abort flag to be set - this sync point blocks until
|
||||
// FlagSet is processed
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
"DBCompactionAbortTest::InProgressBlob:WaitForAbort", nullptr);
|
||||
}
|
||||
});
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
|
||||
// Run compaction - it should be aborted while blob file is in-progress
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
|
||||
ASSERT_TRUE(s.IsIncomplete())
|
||||
<< "Expected compaction to be aborted, got: " << s.ToString();
|
||||
|
||||
CleanupSyncPoints();
|
||||
abort_sync.WaitForAbortCompletion();
|
||||
|
||||
// Check state after abort
|
||||
std::vector<std::string> post_abort_disk_blobs = GetBlobFilesOnDisk();
|
||||
std::vector<uint64_t> post_abort_meta_blobs = GetBlobFilesInMetadata();
|
||||
std::vector<std::string> post_abort_disk_ssts = GetSstFilesOnDisk();
|
||||
std::vector<uint64_t> post_abort_meta_ssts = GetSstFilesInMetadata();
|
||||
|
||||
// This is the key assertion for blob files: files on disk should match
|
||||
// metadata. If the in-progress blob file was NOT cleaned up, there will be an
|
||||
// extra file on disk that's not in metadata (orphan).
|
||||
ASSERT_EQ(post_abort_disk_blobs.size(), post_abort_meta_blobs.size())
|
||||
<< "Orphan blob file detected! In-progress blob file was not cleaned up "
|
||||
"after abort. Files on disk: "
|
||||
<< post_abort_disk_blobs.size()
|
||||
<< ", Files in metadata: " << post_abort_meta_blobs.size()
|
||||
<< ". The difference indicates orphaned in-progress blob file(s).";
|
||||
|
||||
// This is the key assertion for SST files: files on disk should match
|
||||
// metadata. If the in-progress SST file was NOT cleaned up, there will be an
|
||||
// extra file on disk that's not in metadata (orphan).
|
||||
ASSERT_EQ(post_abort_disk_ssts.size(), post_abort_meta_ssts.size())
|
||||
<< "Orphan SST file detected! In-progress SST file was not cleaned up "
|
||||
"after abort. Files on disk: "
|
||||
<< post_abort_disk_ssts.size()
|
||||
<< ", Files in metadata: " << post_abort_meta_ssts.size()
|
||||
<< ". The difference indicates orphaned in-progress SST file(s).";
|
||||
|
||||
// Resume and complete compaction to verify DB is still functional
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
// Verify data integrity - we wrote 4 files * 2000 keys with overlapping keys
|
||||
VerifyDataIntegrity(/*num_keys=*/2000);
|
||||
}
|
||||
|
||||
TEST_F(DBCompactionAbortTest, AbortBottommostLevelCompaction) {
|
||||
Options options = CurrentOptions();
|
||||
options.num_levels = 4;
|
||||
options.level0_file_num_compaction_trigger = 2;
|
||||
options.max_bytes_for_level_base = 1024 * 10; // 10KB
|
||||
options.max_bytes_for_level_multiplier = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
// Write data to fill multiple levels (non-overlapping keys)
|
||||
PopulateData(/*num_files=*/6, /*keys_per_file=*/100,
|
||||
/*value_size=*/500, /*overlapping=*/false);
|
||||
|
||||
// First compact to push data to lower levels
|
||||
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
|
||||
// Write more data to L0 (overlapping keys)
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/500);
|
||||
|
||||
SyncPointAbortHelper helper("CompactionJob::ProcessKeyValueCompaction:Start");
|
||||
helper.Setup(dbfull());
|
||||
|
||||
// Trigger bottommost level compaction
|
||||
CompactRangeOptions cro;
|
||||
cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
helper.CleanupAndWait();
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
ASSERT_OK(dbfull()->CompactRange(cro, nullptr, nullptr));
|
||||
|
||||
VerifyDataIntegrity(/*num_keys=*/600);
|
||||
}
|
||||
|
||||
// Test that while compactions are aborted, atomic range replace
|
||||
// (IngestExternalFiles with atomic_replace_range) works correctly.
|
||||
// This verifies that the abort state doesn't block other write operations
|
||||
// like atomic range replace.
|
||||
TEST_F(DBCompactionAbortTest, AbortThenAtomicRangeReplace) {
|
||||
Options options = CurrentOptions();
|
||||
options.level0_file_num_compaction_trigger = 4;
|
||||
options.max_subcompactions = 2;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
// Create a directory for SST files
|
||||
std::string sst_files_dir = dbname_ + "_sst_files/";
|
||||
ASSERT_OK(env_->CreateDirIfMissing(sst_files_dir));
|
||||
|
||||
// Populate initial data with overlapping keys
|
||||
PopulateData(/*num_files=*/4, /*keys_per_file=*/100, /*value_size=*/500);
|
||||
|
||||
// Verify initial data
|
||||
VerifyDataIntegrity(/*num_keys=*/100);
|
||||
|
||||
// Trigger compaction and abort it
|
||||
SyncPointAbortHelper helper("CompactionJob::ProcessKeyValueCompaction:Start");
|
||||
helper.Setup(dbfull());
|
||||
|
||||
CompactRangeOptions cro;
|
||||
Status s = dbfull()->CompactRange(cro, nullptr, nullptr);
|
||||
ASSERT_TRUE(s.IsIncomplete());
|
||||
ASSERT_TRUE(s.IsCompactionAborted());
|
||||
|
||||
helper.CleanupAndWait();
|
||||
|
||||
// While compaction is still aborted, perform atomic range replace using
|
||||
// IngestExternalFiles with atomic_replace_range. This verifies that the
|
||||
// abort state doesn't block other write operations.
|
||||
// Using RangeOpt() (empty range) means replace everything in the CF.
|
||||
|
||||
// Create an SST file with new data for keys 0-49 (replacing keys 0-99)
|
||||
std::string sst_file_path = sst_files_dir + "atomic_replace_1.sst";
|
||||
SstFileWriter sst_file_writer(EnvOptions(), options);
|
||||
ASSERT_OK(sst_file_writer.Open(sst_file_path));
|
||||
|
||||
// Write new values for keys 0-49
|
||||
Random rnd(42);
|
||||
std::unordered_map<std::string, std::string> new_values;
|
||||
for (int j = 0; j < 50; ++j) {
|
||||
std::string key = Key(j);
|
||||
std::string value = "replaced_" + rnd.RandomString(100);
|
||||
ASSERT_OK(sst_file_writer.Put(key, value));
|
||||
new_values[key] = value;
|
||||
}
|
||||
ASSERT_OK(sst_file_writer.Finish());
|
||||
|
||||
// Perform atomic range replace for the entire column family.
|
||||
// Using RangeOpt() (default constructor) means replace everything in the CF.
|
||||
IngestExternalFileArg arg;
|
||||
arg.column_family = db_->DefaultColumnFamily();
|
||||
arg.external_files = {sst_file_path};
|
||||
arg.atomic_replace_range = RangeOpt();
|
||||
// snapshot_consistency must be false when using atomic_replace_range
|
||||
arg.options.snapshot_consistency = false;
|
||||
|
||||
// Atomic range replace should work even while compactions are aborted
|
||||
ASSERT_OK(db_->IngestExternalFiles({arg}));
|
||||
|
||||
// Now resume compactions after the atomic range replace
|
||||
dbfull()->ResumeAllCompactions();
|
||||
|
||||
// Verify that the atomic range replace worked correctly:
|
||||
// 1. Keys 0-49 should have new replaced values
|
||||
std::string val;
|
||||
for (int j = 0; j < 50; ++j) {
|
||||
std::string key = Key(j);
|
||||
ASSERT_OK(db_->Get(ReadOptions(), key, &val));
|
||||
auto it = new_values.find(key);
|
||||
ASSERT_NE(it, new_values.end());
|
||||
ASSERT_EQ(it->second, val) << "Value mismatch for replaced key: " << key;
|
||||
}
|
||||
|
||||
// 2. Keys 50-99 should not exist (they were replaced/deleted by atomic
|
||||
// replace)
|
||||
for (int j = 50; j < 100; ++j) {
|
||||
std::string key = Key(j);
|
||||
Status get_status = db_->Get(ReadOptions(), key, &val);
|
||||
ASSERT_TRUE(get_status.IsNotFound())
|
||||
<< "Key " << key << " should not exist after full CF replace";
|
||||
}
|
||||
|
||||
// Clean up SST files directory
|
||||
ASSERT_OK(DestroyDir(env_, sst_files_dir));
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
+24
-19
@@ -2208,7 +2208,8 @@ TEST_P(DBDeleteFileRangeTest, DeleteFileRange) {
|
||||
std::string end_string = Key(2000);
|
||||
Slice begin(begin_string);
|
||||
Slice end(end_string);
|
||||
ASSERT_OK(DeleteFilesInRange(db_, db_->DefaultColumnFamily(), &begin, &end));
|
||||
ASSERT_OK(
|
||||
DeleteFilesInRange(db_.get(), db_->DefaultColumnFamily(), &begin, &end));
|
||||
|
||||
int32_t deleted_count = 0;
|
||||
for (int32_t i = 0; i < 4300; i++) {
|
||||
@@ -2229,8 +2230,8 @@ TEST_P(DBDeleteFileRangeTest, DeleteFileRange) {
|
||||
Slice begin1(begin_string);
|
||||
Slice end1(end_string);
|
||||
// Try deleting files in range which contain no keys
|
||||
ASSERT_OK(
|
||||
DeleteFilesInRange(db_, db_->DefaultColumnFamily(), &begin1, &end1));
|
||||
ASSERT_OK(DeleteFilesInRange(db_.get(), db_->DefaultColumnFamily(), &begin1,
|
||||
&end1));
|
||||
|
||||
// Push data from level 0 to level 1 to force all data to be deleted
|
||||
// Note that we don't delete level 0 files
|
||||
@@ -2239,8 +2240,8 @@ TEST_P(DBDeleteFileRangeTest, DeleteFileRange) {
|
||||
ASSERT_OK(db_->CompactRange(compact_options, nullptr, nullptr));
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
|
||||
ASSERT_OK(
|
||||
DeleteFilesInRange(db_, db_->DefaultColumnFamily(), nullptr, nullptr));
|
||||
ASSERT_OK(DeleteFilesInRange(db_.get(), db_->DefaultColumnFamily(), nullptr,
|
||||
nullptr));
|
||||
|
||||
int32_t deleted_count2 = 0;
|
||||
for (int32_t i = 0; i < 4300; i++) {
|
||||
@@ -2308,7 +2309,7 @@ TEST_P(DBDeleteFileRangeTest, DeleteFilesInRanges) {
|
||||
ranges.emplace_back(begin_str1, end_str1);
|
||||
ranges.emplace_back(begin_str2, end_str2);
|
||||
ranges.emplace_back(begin_str3, end_str3);
|
||||
ASSERT_OK(DeleteFilesInRanges(db_, db_->DefaultColumnFamily(),
|
||||
ASSERT_OK(DeleteFilesInRanges(db_.get(), db_->DefaultColumnFamily(),
|
||||
ranges.data(), ranges.size()));
|
||||
ASSERT_EQ("0,3,7", FilesPerLevel(0));
|
||||
|
||||
@@ -2335,7 +2336,7 @@ TEST_P(DBDeleteFileRangeTest, DeleteFilesInRanges) {
|
||||
ranges.emplace_back(&begin1, &end1);
|
||||
ranges.emplace_back(&begin2, &end2);
|
||||
ranges.emplace_back(&begin3, &end3);
|
||||
ASSERT_OK(DeleteFilesInRanges(db_, db_->DefaultColumnFamily(),
|
||||
ASSERT_OK(DeleteFilesInRanges(db_.get(), db_->DefaultColumnFamily(),
|
||||
ranges.data(), ranges.size(), false));
|
||||
ASSERT_EQ("0,1,4", FilesPerLevel(0));
|
||||
|
||||
@@ -2356,7 +2357,8 @@ TEST_P(DBDeleteFileRangeTest, DeleteFilesInRanges) {
|
||||
// Delete all files.
|
||||
{
|
||||
RangeOpt range;
|
||||
ASSERT_OK(DeleteFilesInRanges(db_, db_->DefaultColumnFamily(), &range, 1));
|
||||
ASSERT_OK(
|
||||
DeleteFilesInRanges(db_.get(), db_->DefaultColumnFamily(), &range, 1));
|
||||
ASSERT_EQ("", FilesPerLevel(0));
|
||||
|
||||
for (auto i = 0; i < 1000; i++) {
|
||||
@@ -2418,7 +2420,8 @@ TEST_P(DBDeleteFileRangeTest, DeleteFileRangeFileEndpointsOverlapBug) {
|
||||
// "1 -> vals[0]" to reappear.
|
||||
std::string begin_str = Key(0), end_str = Key(1);
|
||||
Slice begin = begin_str, end = end_str;
|
||||
ASSERT_OK(DeleteFilesInRange(db_, db_->DefaultColumnFamily(), &begin, &end));
|
||||
ASSERT_OK(
|
||||
DeleteFilesInRange(db_.get(), db_->DefaultColumnFamily(), &begin, &end));
|
||||
ASSERT_EQ(vals[1], GetValue(Key(1)));
|
||||
|
||||
db_->ReleaseSnapshot(snapshot);
|
||||
@@ -3657,7 +3660,7 @@ TEST_F(DBCompactionTest, SuggestCompactRangeNoTwoLevel0Compactions) {
|
||||
|
||||
GenerateNewRandomFile(&rnd, /* nowait */ true);
|
||||
ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
|
||||
ASSERT_OK(experimental::SuggestCompactRange(db_, nullptr, nullptr));
|
||||
ASSERT_OK(experimental::SuggestCompactRange(db_.get(), nullptr, nullptr));
|
||||
for (int num = 0; num < options.level0_file_num_compaction_trigger + 1;
|
||||
num++) {
|
||||
GenerateNewRandomFile(&rnd, /* nowait */ true);
|
||||
@@ -4533,7 +4536,8 @@ TEST_F(DBCompactionTest, DeleteFilesInRangeConflictWithCompaction) {
|
||||
std::string end_string = Key(kMaxKey + 1);
|
||||
Slice begin(begin_string);
|
||||
Slice end(end_string);
|
||||
ASSERT_OK(DeleteFilesInRange(db_, db_->DefaultColumnFamily(), &begin, &end));
|
||||
ASSERT_OK(
|
||||
DeleteFilesInRange(db_.get(), db_->DefaultColumnFamily(), &begin, &end));
|
||||
SyncPoint::GetInstance()->DisableProcessing();
|
||||
}
|
||||
|
||||
@@ -7840,7 +7844,7 @@ class DBCompactionTestL0FilesMisorderCorruption : public DBCompactionTest {
|
||||
options_.level0_file_num_compaction_trigger = 3;
|
||||
|
||||
CompactionOptionsFIFO fifo_options;
|
||||
if (compaction_path_to_test == "FindIntraL0Compaction" ||
|
||||
if (compaction_path_to_test == "PickCostBasedIntraL0Compaction" ||
|
||||
compaction_path_to_test == "CompactRange") {
|
||||
fifo_options.allow_compaction = true;
|
||||
} else if (compaction_path_to_test == "CompactFile") {
|
||||
@@ -7940,7 +7944,7 @@ class DBCompactionTestL0FilesMisorderCorruption : public DBCompactionTest {
|
||||
|
||||
void SetupSyncPoints(const std::string& compaction_path_to_test) {
|
||||
compaction_path_sync_point_called_.store(false);
|
||||
if (compaction_path_to_test == "FindIntraL0Compaction" &&
|
||||
if (compaction_path_to_test == "PickCostBasedIntraL0Compaction" &&
|
||||
options_.compaction_style == CompactionStyle::kCompactionStyleLevel) {
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"PostPickFileToCompact", [&](void* arg) {
|
||||
@@ -7950,7 +7954,7 @@ class DBCompactionTestL0FilesMisorderCorruption : public DBCompactionTest {
|
||||
*picked_file_to_compact = false;
|
||||
});
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"FindIntraL0Compaction", [&](void* /*arg*/) {
|
||||
"PickCostBasedIntraL0Compaction", [&](void* /*arg*/) {
|
||||
compaction_path_sync_point_called_.store(true);
|
||||
});
|
||||
|
||||
@@ -7986,12 +7990,12 @@ class DBCompactionTestL0FilesMisorderCorruption : public DBCompactionTest {
|
||||
"PickDeleteTriggeredCompactionReturnNonnullptr", [&](void* /*arg*/) {
|
||||
compaction_path_sync_point_called_.store(true);
|
||||
});
|
||||
} else if ((compaction_path_to_test == "FindIntraL0Compaction" ||
|
||||
} else if ((compaction_path_to_test == "PickCostBasedIntraL0Compaction" ||
|
||||
compaction_path_to_test == "CompactRange") &&
|
||||
options_.compaction_style ==
|
||||
CompactionStyle::kCompactionStyleFIFO) {
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"FindIntraL0Compaction", [&](void* /*arg*/) {
|
||||
"PickCostBasedIntraL0Compaction", [&](void* /*arg*/) {
|
||||
compaction_path_sync_point_called_.store(true);
|
||||
});
|
||||
}
|
||||
@@ -8063,7 +8067,7 @@ TEST_F(DBCompactionTest, CompactFilesSupportKeyPlacementRangeConflict) {
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(Put("k4", "v"));
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(experimental::PromoteL0(db_, db_->DefaultColumnFamily(), 1));
|
||||
ASSERT_OK(experimental::PromoteL0(db_.get(), db_->DefaultColumnFamily(), 1));
|
||||
ASSERT_EQ("0,2,1", FilesPerLevel());
|
||||
|
||||
ASSERT_OK(Put("k2", "v"));
|
||||
@@ -8151,7 +8155,7 @@ TEST_F(DBCompactionTestL0FilesMisorderCorruption,
|
||||
IngestOneKeyValue(dbfull(), Key(i), "new", options_);
|
||||
}
|
||||
|
||||
SetupSyncPoints("FindIntraL0Compaction");
|
||||
SetupSyncPoints("PickCostBasedIntraL0Compaction");
|
||||
ResumeCompactionThread();
|
||||
|
||||
ASSERT_OK(dbfull()->TEST_WaitForCompact());
|
||||
@@ -8284,7 +8288,8 @@ TEST_F(DBCompactionTestL0FilesMisorderCorruption,
|
||||
|
||||
TEST_F(DBCompactionTestL0FilesMisorderCorruption,
|
||||
FlushAfterIntraL0FIFOCompactionWithIngestedFile) {
|
||||
for (const std::string compaction_path_to_test : {"FindIntraL0Compaction"}) {
|
||||
for (const std::string compaction_path_to_test :
|
||||
{"PickCostBasedIntraL0Compaction"}) {
|
||||
SetupOptions(CompactionStyle::kCompactionStyleFIFO,
|
||||
compaction_path_to_test);
|
||||
DestroyAndReopen(options_);
|
||||
|
||||
+12
-13
@@ -101,7 +101,7 @@ TEST_F(DBFlushTest, SyncFail) {
|
||||
TEST_SYNC_POINT("DBFlushTest::SyncFail:2");
|
||||
fault_injection_env->SetFilesystemActive(true);
|
||||
// Now the background job will do the flush; wait for it.
|
||||
// Returns the IO error happend during flush.
|
||||
// Returns the IO error happened during flush.
|
||||
ASSERT_NOK(dbfull()->TEST_WaitForFlushMemTable());
|
||||
ASSERT_EQ("", FilesPerLevel()); // flush failed.
|
||||
Destroy(options);
|
||||
@@ -518,11 +518,11 @@ TEST_F(DBFlushTest, StatisticsGarbageInsertAndDeletes) {
|
||||
// Note : one set of delete for KEY1, KEY2, KEY3 is written to
|
||||
// SSTable to propagate the delete operations to K-V pairs
|
||||
// that could have been inserted into the database during past Flush
|
||||
// opeartions.
|
||||
// operations.
|
||||
EXPECTED_MEMTABLE_GARBAGE_BYTES_AT_FLUSH -=
|
||||
KEY1.size() + KEY2.size() + KEY3.size() + 3 * sizeof(uint64_t);
|
||||
|
||||
// Additional useful paylaod.
|
||||
// Additional useful payload.
|
||||
ASSERT_OK(Delete(KEY4));
|
||||
ASSERT_OK(Delete(KEY5));
|
||||
ASSERT_OK(Delete(KEY6));
|
||||
@@ -614,7 +614,7 @@ TEST_F(DBFlushTest, StatisticsGarbageRangeDeletes) {
|
||||
|
||||
// Note : one set of deleteRange for (KEY1, KEY2) and (KEY2, KEY3) is written
|
||||
// to SSTable to propagate the deleteRange operations to K-V pairs that could
|
||||
// have been inserted into the database during past Flush opeartions.
|
||||
// have been inserted into the database during past Flush operations.
|
||||
EXPECTED_MEMTABLE_GARBAGE_BYTES_AT_FLUSH -=
|
||||
(KEY1.size() + KEY2.size() + sizeof(uint64_t)) +
|
||||
(KEY2.size() + KEY3.size() + sizeof(uint64_t));
|
||||
@@ -709,7 +709,7 @@ class TestFlushListener : public EventListener {
|
||||
// that assumption does not hold (see the test case MultiDBMultiListeners
|
||||
// below).
|
||||
ASSERT_TRUE(test_);
|
||||
if (db == test_->db_) {
|
||||
if (db == test_->db_.get()) {
|
||||
std::vector<std::vector<FileMetaData>> files_by_level;
|
||||
test_->dbfull()->TEST_GetFilesMetaData(db->DefaultColumnFamily(),
|
||||
&files_by_level);
|
||||
@@ -842,7 +842,7 @@ TEST_F(DBFlushTest, FixFlushReasonRaceFromConcurrentFlushes) {
|
||||
ASSERT_OK(Put(1, Key(idx), std::string(1, 'v')));
|
||||
}
|
||||
|
||||
// To coerce a manual flush happenning in the middle of GetLiveFiles's flush,
|
||||
// To coerce a manual flush happening in the middle of GetLiveFiles's flush,
|
||||
// we need to pause background flush thread and enable it later.
|
||||
std::shared_ptr<test::SleepingBackgroundTask> sleeping_task =
|
||||
std::make_shared<test::SleepingBackgroundTask>();
|
||||
@@ -851,7 +851,7 @@ TEST_F(DBFlushTest, FixFlushReasonRaceFromConcurrentFlushes) {
|
||||
sleeping_task.get(), Env::Priority::HIGH);
|
||||
sleeping_task->WaitUntilSleeping();
|
||||
|
||||
// Coerce a manual flush happenning in the middle of GetLiveFiles's flush
|
||||
// Coerce a manual flush happening in the middle of GetLiveFiles's flush
|
||||
bool get_live_files_paused_at_sync_point = false;
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
||||
"DBImpl::AtomicFlushMemTables:AfterScheduleFlush", [&](void* /* arg */) {
|
||||
@@ -1428,7 +1428,7 @@ TEST_F(DBFlushTest, MemPurgeDeleteAndDeleteRange) {
|
||||
Close();
|
||||
}
|
||||
|
||||
// Create a Compaction Fitler that will be invoked
|
||||
// Create a Compaction Filter that will be invoked
|
||||
// at flush time and will update the value of a KV pair
|
||||
// if the key string is "lower" than the filter_key_ string.
|
||||
class ConditionalUpdateFilter : public CompactionFilter {
|
||||
@@ -2533,7 +2533,7 @@ TEST_F(DBFlushTest, TombstoneVisibleInSnapshot) {
|
||||
|
||||
ASSERT_OK(db_->Put(WriteOptions(), "foo", "value0"));
|
||||
|
||||
ManagedSnapshot snapshot_guard(db_);
|
||||
ManagedSnapshot snapshot_guard(db_.get());
|
||||
|
||||
ColumnFamilyHandle* default_cf = db_->DefaultColumnFamily();
|
||||
ASSERT_OK(db_->Flush(FlushOptions(), default_cf));
|
||||
@@ -2574,7 +2574,7 @@ TEST_P(DBAtomicFlushTest, ManualFlushUnder2PC) {
|
||||
txn_db_opts.write_policy = TxnDBWritePolicy::WRITE_COMMITTED;
|
||||
ASSERT_OK(TransactionDB::Open(options, txn_db_opts, dbname_, &txn_db));
|
||||
ASSERT_NE(txn_db, nullptr);
|
||||
db_ = txn_db;
|
||||
db_.reset(txn_db);
|
||||
|
||||
// Create two more columns other than default CF.
|
||||
std::vector<std::string> cfs = {"puppy", "kitty"};
|
||||
@@ -2638,9 +2638,8 @@ TEST_P(DBAtomicFlushTest, ManualFlushUnder2PC) {
|
||||
// it means atomic flush didn't write the min_log_number_to_keep to MANIFEST.
|
||||
cfs.push_back(kDefaultColumnFamilyName);
|
||||
ASSERT_OK(TryReopenWithColumnFamilies(cfs, options));
|
||||
DBImpl* db_impl = static_cast<DBImpl*>(db_);
|
||||
ASSERT_TRUE(db_impl->allow_2pc());
|
||||
ASSERT_NE(db_impl->MinLogNumberToKeep(), 0);
|
||||
ASSERT_TRUE(dbfull()->allow_2pc());
|
||||
ASSERT_NE(dbfull()->MinLogNumberToKeep(), 0);
|
||||
}
|
||||
|
||||
TEST_P(DBAtomicFlushTest, ManualAtomicFlush) {
|
||||
|
||||
+126
-57
@@ -320,6 +320,22 @@ Status DBImpl::ResumeImpl(DBRecoverContext context) {
|
||||
|
||||
WaitForBackgroundWork();
|
||||
|
||||
TEST_SYNC_POINT("DBImpl::ResumeImpl:Start");
|
||||
|
||||
// With two_write_queues=true, sequence numbers are allocated via
|
||||
// FetchAddLastAllocatedSequence() before writes complete, but only
|
||||
// published via SetLastSequence() after success. If we're recovering from
|
||||
// an error, there may be allocated-but-not-published sequence numbers.
|
||||
// We must sync last_sequence_ with last_allocated_sequence_ before creating
|
||||
// any new memtables/WALs, otherwise the new WAL could start with a sequence
|
||||
// number lower than what was already written, causing "sequence number
|
||||
// going backwards" corruption on subsequent recovery.
|
||||
if (immutable_db_options_.two_write_queues) {
|
||||
versions_->SyncLastSequenceWithAllocated();
|
||||
}
|
||||
|
||||
TEST_SYNC_POINT("DBImpl::ResumeImpl:AfterSyncSeq");
|
||||
|
||||
Status s;
|
||||
if (shutdown_initiated_) {
|
||||
// Returning shutdown status to SFM during auto recovery will cause it
|
||||
@@ -1177,23 +1193,38 @@ FSDirectory* DBImpl::GetDataDir(ColumnFamilyData* cfd, size_t path_id) const {
|
||||
}
|
||||
|
||||
Status DBImpl::SetOptions(
|
||||
ColumnFamilyHandle* column_family,
|
||||
const std::unordered_map<std::string, std::string>& options_map) {
|
||||
const std::unordered_map<ColumnFamilyHandle*,
|
||||
std::unordered_map<std::string, std::string>>&
|
||||
column_families_opts_map) {
|
||||
// TODO: plumb Env::IOActivity, Env::IOPriority
|
||||
const ReadOptions read_options;
|
||||
const WriteOptions write_options;
|
||||
|
||||
auto* cfd =
|
||||
static_cast_with_check<ColumnFamilyHandleImpl>(column_family)->cfd();
|
||||
if (options_map.empty()) {
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"SetOptions() on column family [%s], empty input",
|
||||
cfd->GetName().c_str());
|
||||
return Status::InvalidArgument("empty input");
|
||||
if (column_families_opts_map.empty()) {
|
||||
return Status::OK();
|
||||
}
|
||||
|
||||
for (const auto& cf_opts : column_families_opts_map) {
|
||||
if (cf_opts.second.empty()) {
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"SetOptions() on column family [%s], empty input",
|
||||
cf_opts.first->GetName().c_str());
|
||||
return Status::InvalidArgument("empty input");
|
||||
}
|
||||
}
|
||||
|
||||
autovector<std::pair<ColumnFamilyData*,
|
||||
const std::unordered_map<std::string, std::string>*>>
|
||||
column_family_datas;
|
||||
for (const auto& cf_opts : column_families_opts_map) {
|
||||
column_family_datas.push_back(
|
||||
{static_cast_with_check<ColumnFamilyHandleImpl>(cf_opts.first)->cfd(),
|
||||
&cf_opts.second});
|
||||
}
|
||||
|
||||
InstrumentedMutexLock ol(&options_mutex_);
|
||||
MutableCFOptions new_options_copy; // For logging outside of DB mutex
|
||||
autovector<MutableCFOptions>
|
||||
new_options_copy; // For logging outside of DB mutex
|
||||
Status s;
|
||||
Status persist_options_status;
|
||||
SuperVersionContext sv_context(/* create_superversion */ true);
|
||||
@@ -1216,68 +1247,104 @@ Status DBImpl::SetOptions(
|
||||
// Thus aren't releasing the DB mutex from LogAndApply calling pre_cb,
|
||||
// through installing the new Version until the end of this block, after
|
||||
// installing the new SuperVersion.
|
||||
auto pre_cb = [&]() -> Status {
|
||||
Status cb_s = cfd->SetOptions(db_options, options_map);
|
||||
if (cb_s.ok()) {
|
||||
new_options_copy = cfd->GetLatestMutableCFOptions();
|
||||
}
|
||||
return cb_s;
|
||||
};
|
||||
VersionEdit dummy_edit;
|
||||
dummy_edit.MarkNoManifestWriteDummy();
|
||||
TEST_SYNC_POINT_CALLBACK("DBImpl::SetOptions:dummy_edit", &dummy_edit);
|
||||
s = versions_->LogAndApply(
|
||||
cfd, read_options, write_options, &dummy_edit, &mutex_,
|
||||
directories_.GetDbDir(), false /*new_descriptor_log=*/,
|
||||
nullptr /*new_opts*/, {} /*manifest_wcb*/, pre_cb);
|
||||
if (!versions_->io_status().ok()) {
|
||||
assert(!s.ok());
|
||||
error_handler_.SetBGError(versions_->io_status(),
|
||||
BackgroundErrorReason::kManifestWrite);
|
||||
for (const auto& cfd_opts : column_family_datas) {
|
||||
auto* cfd = cfd_opts.first;
|
||||
const auto* options_map_ptr = cfd_opts.second;
|
||||
auto pre_cb = [&]() -> Status {
|
||||
Status cb_s = cfd->SetOptions(db_options, *options_map_ptr);
|
||||
if (cb_s.ok()) {
|
||||
new_options_copy.emplace_back(cfd->GetLatestMutableCFOptions());
|
||||
}
|
||||
return cb_s;
|
||||
};
|
||||
|
||||
s = versions_->LogAndApply(
|
||||
cfd, read_options, write_options, &dummy_edit, &mutex_,
|
||||
directories_.GetDbDir(), false /*new_descriptor_log=*/,
|
||||
nullptr /*new_opts*/, {} /*manifest_wcb*/, pre_cb);
|
||||
if (!versions_->io_status().ok()) {
|
||||
assert(!s.ok());
|
||||
error_handler_.SetBGError(versions_->io_status(),
|
||||
BackgroundErrorReason::kManifestWrite);
|
||||
}
|
||||
if (!s.ok()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (s.ok()) {
|
||||
// Trigger possible flush/compactions. This has to be before we persist
|
||||
// options to file, otherwise there will be a deadlock with writer
|
||||
// thread.
|
||||
InstallSuperVersionForConfigChange(cfd, &sv_context);
|
||||
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();
|
||||
|
||||
assert(new_options_copy == cfd->GetLatestMutableCFOptions());
|
||||
assert(cfd->GetLatestMutableCFOptions() ==
|
||||
cfd->GetCurrentMutableCFOptions());
|
||||
assert(cfd->GetCurrentMutableCFOptions() ==
|
||||
cfd->current()->GetMutableCFOptions());
|
||||
#ifndef NDEBUG
|
||||
for (size_t i = 0; i < column_family_datas.size(); ++i) {
|
||||
auto* cfd = column_family_datas[i].first;
|
||||
assert(new_options_copy[i] == cfd->GetLatestMutableCFOptions());
|
||||
assert(cfd->GetLatestMutableCFOptions() ==
|
||||
cfd->GetCurrentMutableCFOptions());
|
||||
assert(cfd->GetCurrentMutableCFOptions() ==
|
||||
cfd->current()->GetMutableCFOptions());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
sv_context.Clean();
|
||||
|
||||
if (s.ok() && (options_map.count("preserve_internal_time_seconds") > 0 ||
|
||||
options_map.count("preclude_last_level_data_seconds") > 0)) {
|
||||
s = RegisterRecordSeqnoTimeWorker();
|
||||
if (s.ok()) {
|
||||
bool needs_seqno_worker = false;
|
||||
for (const auto& cf_opts : column_families_opts_map) {
|
||||
if (cf_opts.second.count("preserve_internal_time_seconds") > 0 ||
|
||||
cf_opts.second.count("preclude_last_level_data_seconds") > 0) {
|
||||
needs_seqno_worker = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (needs_seqno_worker) {
|
||||
s = RegisterRecordSeqnoTimeWorker();
|
||||
}
|
||||
}
|
||||
|
||||
ROCKS_LOG_INFO(
|
||||
immutable_db_options_.info_log,
|
||||
"SetOptions() on column family [%s], inputs:", cfd->GetName().c_str());
|
||||
for (const auto& o : options_map) {
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log, "%s: %s\n", o.first.c_str(),
|
||||
o.second.c_str());
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
"SetOptions() on [%zu] column families, inputs:",
|
||||
column_family_datas.size());
|
||||
for (size_t i = 0; i < column_family_datas.size(); ++i) {
|
||||
const auto* cfd = column_family_datas[i].first;
|
||||
const auto* options_map_ptr = column_family_datas[i].second;
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
"Set options on column family [%s] (%zu/%zu), inputs:",
|
||||
cfd->GetName().c_str(), i, column_family_datas.size());
|
||||
for (const auto& o : *options_map_ptr) {
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log, "%s: %s\n",
|
||||
o.first.c_str(), o.second.c_str());
|
||||
}
|
||||
}
|
||||
if (s.ok()) {
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
"[%s] SetOptions() succeeded", cfd->GetName().c_str());
|
||||
new_options_copy.Dump(immutable_db_options_.info_log.get());
|
||||
for (size_t i = 0; i < column_family_datas.size(); ++i) {
|
||||
const auto* cfd = column_family_datas[i].first;
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
"Set options on column family [%s] (%zu/%zu) succeeded, "
|
||||
"updated CF options:",
|
||||
cfd->GetName().c_str(), i, column_family_datas.size());
|
||||
new_options_copy[i].Dump(immutable_db_options_.info_log.get());
|
||||
}
|
||||
if (!persist_options_status.ok()) {
|
||||
// NOTE: WriteOptionsFile already logs on failure
|
||||
s = persist_options_status;
|
||||
}
|
||||
} else {
|
||||
persist_options_status.PermitUncheckedError(); // less important
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log, "[%s] SetOptions() failed",
|
||||
cfd->GetName().c_str());
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log, "SetOptions() failed: %s",
|
||||
s.ToString().c_str());
|
||||
}
|
||||
LogFlush(immutable_db_options_.info_log);
|
||||
return s;
|
||||
@@ -3858,10 +3925,6 @@ Iterator* DBImpl::NewIterator(const ReadOptions& _read_options,
|
||||
read_options.io_activity = Env::IOActivity::kDBIterator;
|
||||
}
|
||||
|
||||
if (read_options.managed) {
|
||||
return NewErrorIterator(
|
||||
Status::NotSupported("Managed iterator is not supported anymore."));
|
||||
}
|
||||
Iterator* result = nullptr;
|
||||
if (read_options.read_tier == kPersistedTier) {
|
||||
return NewErrorIterator(Status::NotSupported(
|
||||
@@ -4061,9 +4124,6 @@ Status DBImpl::NewIterators(
|
||||
if (read_options.io_activity == Env::IOActivity::kUnknown) {
|
||||
read_options.io_activity = Env::IOActivity::kDBIterator;
|
||||
}
|
||||
if (read_options.managed) {
|
||||
return Status::NotSupported("Managed iterator is not supported anymore.");
|
||||
}
|
||||
if (read_options.read_tier == kPersistedTier) {
|
||||
return Status::NotSupported(
|
||||
"ReadTier::kPersistedData is not yet supported in iterators.");
|
||||
@@ -4351,7 +4411,8 @@ void DBImpl::ReleaseSnapshot(const Snapshot* s) {
|
||||
for (auto* cfd : *versions_->GetColumnFamilySet()) {
|
||||
if (!cfd->AllowIngestBehind()) {
|
||||
cfd->current()->storage_info()->UpdateOldestSnapshot(
|
||||
oldest_snapshot, /*allow_ingest_behind=*/false);
|
||||
oldest_snapshot, /*allow_ingest_behind=*/false,
|
||||
cfd->ioptions().user_comparator, cfd->GetFullHistoryTsLow());
|
||||
if (!cfd->current()
|
||||
->storage_info()
|
||||
->BottommostFilesMarkedForCompaction()
|
||||
@@ -4987,7 +5048,8 @@ Status DBImpl::DeleteFilesInRanges(ColumnFamilyHandle* column_family,
|
||||
}
|
||||
if (!deleted_files.empty()) {
|
||||
vstorage->ComputeCompactionScore(cfd->ioptions(),
|
||||
cfd->GetLatestMutableCFOptions());
|
||||
cfd->GetLatestMutableCFOptions(),
|
||||
cfd->GetFullHistoryTsLow());
|
||||
}
|
||||
if (edit.GetDeletedFiles().empty()) {
|
||||
job_context.Clean();
|
||||
@@ -6418,8 +6480,11 @@ Status DBImpl::VerifyChecksumInternal(const ReadOptions& read_options,
|
||||
fmeta->file_checksum_func_name, fname,
|
||||
read_options);
|
||||
} else {
|
||||
FileOptions fopts = file_options_;
|
||||
fopts.file_checksum = fmeta->file_checksum;
|
||||
fopts.file_checksum_func_name = fmeta->file_checksum_func_name;
|
||||
s = ROCKSDB_NAMESPACE::VerifySstFileChecksumInternal(
|
||||
opts, file_options_, read_options, fname, fd.largest_seqno);
|
||||
opts, fopts, read_options, fname, fd.largest_seqno);
|
||||
}
|
||||
RecordTick(stats_, VERIFY_CHECKSUM_READ_BYTES,
|
||||
IOSTATS(bytes_read) - prev_bytes_read);
|
||||
@@ -6487,12 +6552,15 @@ Status DBImpl::VerifyFullFileChecksum(const std::string& file_checksum_expected,
|
||||
}
|
||||
std::string file_checksum;
|
||||
std::string func_name;
|
||||
FileOptions fopts;
|
||||
fopts.file_checksum = file_checksum_expected;
|
||||
fopts.file_checksum_func_name = func_name_expected;
|
||||
s = ROCKSDB_NAMESPACE::GenerateOneFileChecksum(
|
||||
fs_.get(), fname, immutable_db_options_.file_checksum_gen_factory.get(),
|
||||
func_name_expected, &file_checksum, &func_name,
|
||||
read_options.readahead_size, immutable_db_options_.allow_mmap_reads,
|
||||
io_tracer_, immutable_db_options_.rate_limiter.get(), read_options,
|
||||
immutable_db_options_.stats, immutable_db_options_.clock);
|
||||
immutable_db_options_.stats, immutable_db_options_.clock, fopts);
|
||||
if (s.ok()) {
|
||||
assert(func_name_expected == func_name);
|
||||
if (file_checksum != file_checksum_expected) {
|
||||
@@ -6851,7 +6919,8 @@ void DBImpl::TriggerPeriodicCompaction() {
|
||||
if (cfd->GetLatestCFOptions().periodic_compaction_seconds &&
|
||||
!cfd->queued_for_compaction()) {
|
||||
cfd->current()->storage_info()->ComputeCompactionScore(
|
||||
cfd->ioptions(), cfd->GetLatestMutableCFOptions());
|
||||
cfd->ioptions(), cfd->GetLatestMutableCFOptions(),
|
||||
cfd->GetFullHistoryTsLow());
|
||||
EnqueuePendingCompaction(cfd);
|
||||
if (cfd->queued_for_compaction()) {
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
|
||||
+13
-4
@@ -455,19 +455,20 @@ class DBImpl : public DB {
|
||||
|
||||
void EnableManualCompaction() override;
|
||||
void DisableManualCompaction() override;
|
||||
void AbortAllCompactions() override;
|
||||
void ResumeAllCompactions() override;
|
||||
|
||||
using DB::SetOptions;
|
||||
Status SetOptions(
|
||||
ColumnFamilyHandle* column_family,
|
||||
const std::unordered_map<std::string, std::string>& options_map) override;
|
||||
const std::unordered_map<ColumnFamilyHandle*,
|
||||
std::unordered_map<std::string, std::string>>&
|
||||
column_families_opts_map) override;
|
||||
|
||||
Status SetDBOptions(
|
||||
const std::unordered_map<std::string, std::string>& options_map) override;
|
||||
|
||||
using DB::NumberLevels;
|
||||
int NumberLevels(ColumnFamilyHandle* column_family) override;
|
||||
using DB::MaxMemCompactionLevel;
|
||||
int MaxMemCompactionLevel(ColumnFamilyHandle* column_family) override;
|
||||
using DB::Level0StopWriteTrigger;
|
||||
int Level0StopWriteTrigger(ColumnFamilyHandle* column_family) override;
|
||||
const std::string& GetName() const override;
|
||||
@@ -2788,6 +2789,14 @@ class DBImpl : public DB {
|
||||
// compaction code paths.
|
||||
std::atomic<int> manual_compaction_paused_ = false;
|
||||
|
||||
// If non-zero, all compaction jobs (background automatic compactions,
|
||||
// manual compactions via CompactRange, and foreground CompactFiles calls)
|
||||
// are being aborted. Compactions will be signaled to stop. Any new
|
||||
// compaction job would fail immediately. The value indicates how many threads
|
||||
// have called AbortAllCompactions(). It is accessed in read mode outside the
|
||||
// DB mutex in compaction code paths.
|
||||
std::atomic<int> compaction_aborted_ = 0;
|
||||
|
||||
// This condition variable is signaled on these conditions:
|
||||
// * whenever bg_compaction_scheduled_ goes down to 0
|
||||
// * if AnyManualCompaction, whenever a compaction finishes, even if it hasn't
|
||||
|
||||
@@ -955,6 +955,10 @@ Status DBImpl::CompactRange(const CompactRangeOptions& options,
|
||||
return Status::Incomplete(Status::SubCode::kManualCompactionPaused);
|
||||
}
|
||||
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
return Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
}
|
||||
|
||||
if (options.canceled && options.canceled->load(std::memory_order_acquire)) {
|
||||
return Status::Incomplete(Status::SubCode::kManualCompactionPaused);
|
||||
}
|
||||
@@ -1449,7 +1453,8 @@ Status DBImpl::PerformTrivialMove(Compaction& c, LogBuffer* log_buffer,
|
||||
f->file_creation_time, f->epoch_number,
|
||||
f->file_checksum, f->file_checksum_func_name,
|
||||
f->unique_id, f->compensated_range_deletion_size,
|
||||
f->tail_size, f->user_defined_timestamps_persisted);
|
||||
f->tail_size, f->user_defined_timestamps_persisted,
|
||||
f->min_timestamp, f->max_timestamp);
|
||||
moved_bytes += static_cast<size_t>(c.input(l, i)->fd.GetFileSize());
|
||||
ROCKS_LOG_BUFFER(
|
||||
log_buffer, "[%s] Moved #%" PRIu64 " to level-%d %" PRIu64 " bytes\n",
|
||||
@@ -1486,6 +1491,11 @@ Status DBImpl::CompactFilesImpl(
|
||||
return Status::ShutdownInProgress();
|
||||
}
|
||||
|
||||
// triggered by AbortAllCompactions
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
return Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
}
|
||||
|
||||
// triggered by DisableManualCompactions or by user-set canceled flag in
|
||||
// CompactionOptions
|
||||
if (manual_compaction_paused_.load(std::memory_order_acquire) > 0 ||
|
||||
@@ -1636,17 +1646,17 @@ Status DBImpl::CompactFilesImpl(
|
||||
c->mutable_cf_options().paranoid_file_checks,
|
||||
c->mutable_cf_options().report_bg_io_stats, dbname_,
|
||||
&compaction_job_stats, Env::Priority::USER, io_tracer_,
|
||||
kManualCompactionCanceledFalse_, db_id_, db_session_id_,
|
||||
c->column_family_data()->GetFullHistoryTsLow(), c->trim_ts(),
|
||||
&blob_callback_, &bg_compaction_scheduled_,
|
||||
kManualCompactionCanceledFalse_, compaction_aborted_, db_id_,
|
||||
db_session_id_, c->column_family_data()->GetFullHistoryTsLow(),
|
||||
c->trim_ts(), &blob_callback_, &bg_compaction_scheduled_,
|
||||
&bg_bottom_compaction_scheduled_);
|
||||
|
||||
// Creating a compaction influences the compaction score because the score
|
||||
// takes running compactions into account (by skipping files that are already
|
||||
// being compacted). Since we just changed compaction score, we recalculate it
|
||||
// here.
|
||||
version->storage_info()->ComputeCompactionScore(cfd->ioptions(),
|
||||
c->mutable_cf_options());
|
||||
version->storage_info()->ComputeCompactionScore(
|
||||
cfd->ioptions(), c->mutable_cf_options(), cfd->GetFullHistoryTsLow());
|
||||
|
||||
compaction_job.Prepare(std::nullopt /*subcompact to be computed*/);
|
||||
|
||||
@@ -1709,6 +1719,11 @@ Status DBImpl::CompactFilesImpl(
|
||||
"[%s] [JOB %d] Stopping manual compaction",
|
||||
c->column_family_data()->GetName().c_str(),
|
||||
job_context->job_id);
|
||||
} else if (status.IsCompactionAborted()) {
|
||||
// Don't report aborted compaction as error
|
||||
ROCKS_LOG_INFO(
|
||||
immutable_db_options_.info_log, "[%s] [JOB %d] Compaction aborted",
|
||||
c->column_family_data()->GetName().c_str(), job_context->job_id);
|
||||
} else {
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"[%s] [JOB %d] Compaction error: %s",
|
||||
@@ -1971,7 +1986,8 @@ Status DBImpl::ReFitLevel(ColumnFamilyData* cfd, int level, int target_level) {
|
||||
f->oldest_ancester_time, f->file_creation_time, f->epoch_number,
|
||||
f->file_checksum, f->file_checksum_func_name, f->unique_id,
|
||||
f->compensated_range_deletion_size, f->tail_size,
|
||||
f->user_defined_timestamps_persisted);
|
||||
f->user_defined_timestamps_persisted, f->min_timestamp,
|
||||
f->max_timestamp);
|
||||
}
|
||||
ROCKS_LOG_DEBUG(immutable_db_options_.info_log,
|
||||
"[%s] Apply version edit:\n%s", cfd->GetName().c_str(),
|
||||
@@ -2009,10 +2025,6 @@ int DBImpl::NumberLevels(ColumnFamilyHandle* column_family) {
|
||||
return cfh->cfd()->NumberLevels();
|
||||
}
|
||||
|
||||
int DBImpl::MaxMemCompactionLevel(ColumnFamilyHandle* /*column_family*/) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DBImpl::Level0StopWriteTrigger(ColumnFamilyHandle* column_family) {
|
||||
auto cfh = static_cast_with_check<ColumnFamilyHandleImpl>(column_family);
|
||||
InstrumentedMutexLock l(&mutex_);
|
||||
@@ -2168,6 +2180,17 @@ Status DBImpl::RunManualCompaction(
|
||||
return manual.status;
|
||||
}
|
||||
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
// All compactions are being aborted. Return immediately.
|
||||
int counter = compaction_aborted_.load(std::memory_order_acquire);
|
||||
ROCKS_LOG_INFO(
|
||||
immutable_db_options_.info_log,
|
||||
"RunManualCompaction: Aborting due to compaction_aborted_=%d", counter);
|
||||
manual.status = Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
manual.done = true;
|
||||
return manual.status;
|
||||
}
|
||||
|
||||
// When a manual compaction arrives, temporarily disable scheduling of
|
||||
// non-manual compactions and wait until the number of scheduled compaction
|
||||
// jobs drops to zero. This used to be needed to ensure that this manual
|
||||
@@ -2192,6 +2215,13 @@ Status DBImpl::RunManualCompaction(
|
||||
// and `CompactRangeOptions::canceled` might not work well together.
|
||||
while (bg_bottom_compaction_scheduled_ > 0 ||
|
||||
bg_compaction_scheduled_ > 0) {
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
// Pretend the error came from compaction so the below cleanup/error
|
||||
// handling code can process it.
|
||||
manual.done = true;
|
||||
manual.status = Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
break;
|
||||
}
|
||||
if (manual_compaction_paused_ > 0 || manual.canceled == true) {
|
||||
// Pretend the error came from compaction so the below cleanup/error
|
||||
// handling code can process it.
|
||||
@@ -2310,7 +2340,12 @@ Status DBImpl::RunManualCompaction(
|
||||
if (!scheduled) {
|
||||
// There is nothing scheduled to wait on, so any cancellation can end the
|
||||
// manual now.
|
||||
if (manual_compaction_paused_ > 0 || manual.canceled == true) {
|
||||
if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
// Stop waiting since it was canceled. Pretend the error came from
|
||||
// compaction so the below cleanup/error handling code can process it.
|
||||
manual.done = true;
|
||||
manual.status = Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
} else if (manual_compaction_paused_ > 0 || manual.canceled == true) {
|
||||
// Stop waiting since it was canceled. Pretend the error came from
|
||||
// compaction so the below cleanup/error handling code can process it.
|
||||
manual.done = true;
|
||||
@@ -2889,16 +2924,8 @@ Status DBImpl::WaitForFlushMemTables(
|
||||
|
||||
Status DBImpl::EnableAutoCompaction(
|
||||
const std::vector<ColumnFamilyHandle*>& column_family_handles) {
|
||||
Status s;
|
||||
for (auto cf_ptr : column_family_handles) {
|
||||
Status status =
|
||||
this->SetOptions(cf_ptr, {{"disable_auto_compactions", "false"}});
|
||||
if (!status.ok()) {
|
||||
s = status;
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
return SetOptions(column_family_handles,
|
||||
{{"disable_auto_compactions", "false"}});
|
||||
}
|
||||
|
||||
// NOTE: Calling DisableManualCompaction() may overwrite the
|
||||
@@ -2936,6 +2963,61 @@ void DBImpl::EnableManualCompaction() {
|
||||
manual_compaction_paused_.fetch_sub(1, std::memory_order_release);
|
||||
}
|
||||
|
||||
void DBImpl::AbortAllCompactions() {
|
||||
InstrumentedMutexLock l(&mutex_);
|
||||
|
||||
// Increment the abort counter to signal all compactions to abort
|
||||
compaction_aborted_.fetch_add(1, std::memory_order_release);
|
||||
|
||||
TEST_SYNC_POINT("DBImpl::AbortAllCompactions:FlagSet");
|
||||
|
||||
// Mark all manual compactions as canceled
|
||||
for (const auto& manual_compaction : manual_compaction_dequeue_) {
|
||||
manual_compaction->canceled = true;
|
||||
}
|
||||
|
||||
// Wake up any waiting compaction threads to check the abort signal
|
||||
bg_cv_.SignalAll();
|
||||
|
||||
// Wait for all running compactions (both manual and automatic) to finish
|
||||
// or abort before returning.
|
||||
// Note: bg_cv_.Wait() releases the mutex while waiting, so other threads
|
||||
// can make progress and signal when compactions complete.
|
||||
while (bg_bottom_compaction_scheduled_ > 0 || bg_compaction_scheduled_ > 0 ||
|
||||
HasPendingManualCompaction()) {
|
||||
bg_cv_.Wait();
|
||||
}
|
||||
}
|
||||
|
||||
void DBImpl::ResumeAllCompactions() {
|
||||
InstrumentedMutexLock l(&mutex_);
|
||||
int before = compaction_aborted_.load(std::memory_order_acquire);
|
||||
|
||||
// Guard against calling Resume without prior Abort
|
||||
if (before <= 0) {
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"ResumeAllCompactions called without prior "
|
||||
"AbortAllCompactions (counter=%d)",
|
||||
before);
|
||||
return;
|
||||
}
|
||||
|
||||
// Decrement the abort counter
|
||||
compaction_aborted_.fetch_sub(1, std::memory_order_release);
|
||||
|
||||
// As the operation is executed under db mutex, we could just use before value
|
||||
// to calculate the current value.
|
||||
int current = before - 1;
|
||||
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
||||
"ResumeAllCompactions: counter %d -> %d", before, current);
|
||||
|
||||
// If this is the last resume call (abort counter back to 0), schedule
|
||||
// compactions that may have been waiting
|
||||
if (current == 0) {
|
||||
MaybeScheduleFlushOrCompaction();
|
||||
}
|
||||
}
|
||||
|
||||
void DBImpl::MaybeScheduleFlushOrCompaction() {
|
||||
mutex_.AssertHeld();
|
||||
TEST_SYNC_POINT("DBImpl::MaybeScheduleFlushOrCompaction:Start");
|
||||
@@ -3000,6 +3082,9 @@ void DBImpl::MaybeScheduleFlushOrCompaction() {
|
||||
if (bg_compaction_paused_ > 0) {
|
||||
// we paused the background compaction
|
||||
return;
|
||||
} else if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
// we are aborting all compactions
|
||||
return;
|
||||
} else if (error_handler_.IsBGWorkStopped()) {
|
||||
// Compaction is not part of the recovery sequence from a hard error. We
|
||||
// might get here because recovery might do a flush and install a new
|
||||
@@ -3537,7 +3622,8 @@ void DBImpl::BackgroundCallCompaction(PrepickedCompaction* prepicked_compaction,
|
||||
10000); // prevent hot loop
|
||||
mutex_.Lock();
|
||||
} else if (!s.ok() && !s.IsShutdownInProgress() &&
|
||||
!s.IsManualCompactionPaused() && !s.IsColumnFamilyDropped()) {
|
||||
!s.IsManualCompactionPaused() && !s.IsColumnFamilyDropped() &&
|
||||
!s.IsCompactionAborted()) {
|
||||
// Wait a little bit before retrying background compaction in
|
||||
// case this is an environmental problem and we do not want to
|
||||
// chew up resources for failed compactions for the duration of
|
||||
@@ -3569,6 +3655,7 @@ void DBImpl::BackgroundCallCompaction(PrepickedCompaction* prepicked_compaction,
|
||||
// case of a failure). Thus, we force full scan in FindObsoleteFiles()
|
||||
FindObsoleteFiles(&job_context, !s.ok() && !s.IsShutdownInProgress() &&
|
||||
!s.IsManualCompactionPaused() &&
|
||||
!s.IsCompactionAborted() &&
|
||||
!s.IsColumnFamilyDropped() &&
|
||||
!s.IsBusy());
|
||||
TEST_SYNC_POINT("DBImpl::BackgroundCallCompaction:FoundObsoleteFiles");
|
||||
@@ -3673,6 +3760,8 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
|
||||
if (!error_handler_.IsBGWorkStopped()) {
|
||||
if (shutting_down_.load(std::memory_order_acquire)) {
|
||||
status = Status::ShutdownInProgress();
|
||||
} else if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
|
||||
status = Status::Incomplete(Status::SubCode::kCompactionAborted);
|
||||
} else if (is_manual &&
|
||||
manual_compaction->canceled.load(std::memory_order_acquire)) {
|
||||
status = Status::Incomplete(Status::SubCode::kManualCompactionPaused);
|
||||
@@ -3839,7 +3928,8 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
|
||||
->current()
|
||||
->storage_info()
|
||||
->ComputeCompactionScore(c->immutable_options(),
|
||||
c->mutable_cf_options());
|
||||
c->mutable_cf_options(),
|
||||
cfd->GetFullHistoryTsLow());
|
||||
EnqueuePendingCompaction(cfd);
|
||||
|
||||
c.reset();
|
||||
@@ -4096,7 +4186,9 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
|
||||
in_file->unique_id,
|
||||
in_file->compensated_range_deletion_size,
|
||||
in_file->tail_size,
|
||||
in_file->user_defined_timestamps_persisted};
|
||||
in_file->user_defined_timestamps_persisted,
|
||||
in_file->min_timestamp,
|
||||
in_file->max_timestamp};
|
||||
|
||||
out_files.push_back(std::move(out_file_metadata));
|
||||
}
|
||||
@@ -4286,8 +4378,9 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
|
||||
&compaction_job_stats, thread_pri, io_tracer_,
|
||||
is_manual ? manual_compaction->canceled
|
||||
: kManualCompactionCanceledFalse_,
|
||||
db_id_, db_session_id_, c->column_family_data()->GetFullHistoryTsLow(),
|
||||
c->trim_ts(), &blob_callback_, &bg_compaction_scheduled_,
|
||||
compaction_aborted_, db_id_, db_session_id_,
|
||||
c->column_family_data()->GetFullHistoryTsLow(), c->trim_ts(),
|
||||
&blob_callback_, &bg_compaction_scheduled_,
|
||||
&bg_bottom_compaction_scheduled_);
|
||||
compaction_job.Prepare(std::nullopt /*subcompact to be computed*/);
|
||||
|
||||
@@ -4370,7 +4463,7 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
|
||||
}
|
||||
|
||||
if (status.ok() || status.IsCompactionTooLarge() ||
|
||||
status.IsManualCompactionPaused()) {
|
||||
status.IsManualCompactionPaused() || status.IsCompactionAborted()) {
|
||||
// Done
|
||||
} else if (status.IsColumnFamilyDropped() || status.IsShutdownInProgress()) {
|
||||
// Ignore compaction errors found during shutting down
|
||||
@@ -4401,7 +4494,8 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
|
||||
->current()
|
||||
->storage_info()
|
||||
->ComputeCompactionScore(c->immutable_options(),
|
||||
c->mutable_cf_options());
|
||||
c->mutable_cf_options(),
|
||||
cfd->GetFullHistoryTsLow());
|
||||
EnqueuePendingCompaction(cfd);
|
||||
}
|
||||
}
|
||||
@@ -4507,7 +4601,7 @@ Compaction* DBImpl::CreateIntendedCompactionForwardedToBottomPriorityPool(
|
||||
nullptr /* snapshot_checker */, c->compaction_reason());
|
||||
|
||||
cfd->compaction_picker()->RegisterCompaction(intended_compaction);
|
||||
vstorage->ComputeCompactionScore(io, mo);
|
||||
vstorage->ComputeCompactionScore(io, mo, cfd->GetFullHistoryTsLow());
|
||||
intended_compaction->FinalizeInputInfo(cfd->current());
|
||||
|
||||
return intended_compaction;
|
||||
@@ -4632,6 +4726,7 @@ void DBImpl::BuildCompactionJobInfo(
|
||||
compaction_job_info->cf_id = cfd->GetID();
|
||||
compaction_job_info->cf_name = cfd->GetName();
|
||||
compaction_job_info->status = st;
|
||||
compaction_job_info->aborted = st.IsCompactionAborted();
|
||||
compaction_job_info->thread_id = env_->GetThreadID();
|
||||
compaction_job_info->job_id = job_id;
|
||||
compaction_job_info->base_input_level = c->start_level();
|
||||
@@ -4879,7 +4974,8 @@ void DBImpl::ResetBottomPriCompactionIntent(ColumnFamilyData* cfd,
|
||||
std::unique_ptr<Compaction>& c) {
|
||||
c->ReleaseCompactionFiles(Status::OK());
|
||||
cfd->current()->storage_info()->ComputeCompactionScore(
|
||||
c->immutable_options(), c->mutable_cf_options());
|
||||
c->immutable_options(), c->mutable_cf_options(),
|
||||
cfd->GetFullHistoryTsLow());
|
||||
c.reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,8 @@ Status DBImpl::SuggestCompactRange(ColumnFamilyHandle* column_family,
|
||||
// Since we have some more files to compact, we should also recompute
|
||||
// compaction score
|
||||
vstorage->ComputeCompactionScore(cfd->ioptions(),
|
||||
cfd->GetLatestMutableCFOptions());
|
||||
cfd->GetLatestMutableCFOptions(),
|
||||
cfd->GetFullHistoryTsLow());
|
||||
EnqueuePendingCompaction(cfd);
|
||||
MaybeScheduleFlushOrCompaction();
|
||||
}
|
||||
@@ -143,7 +144,8 @@ Status DBImpl::PromoteL0(ColumnFamilyHandle* column_family, int target_level) {
|
||||
f->file_creation_time, f->epoch_number, f->file_checksum,
|
||||
f->file_checksum_func_name, f->unique_id,
|
||||
f->compensated_range_deletion_size, f->tail_size,
|
||||
f->user_defined_timestamps_persisted);
|
||||
f->user_defined_timestamps_persisted, f->min_timestamp,
|
||||
f->max_timestamp);
|
||||
}
|
||||
|
||||
status = versions_->LogAndApply(cfd, read_options, write_options, &edit,
|
||||
|
||||
@@ -657,7 +657,8 @@ Status DBImpl::Recover(
|
||||
f->file_creation_time, f->epoch_number,
|
||||
f->file_checksum, f->file_checksum_func_name,
|
||||
f->unique_id, f->compensated_range_deletion_size,
|
||||
f->tail_size, f->user_defined_timestamps_persisted);
|
||||
f->tail_size, f->user_defined_timestamps_persisted,
|
||||
f->min_timestamp, f->max_timestamp);
|
||||
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
||||
"[%s] Moving #%" PRIu64
|
||||
" from from_level-%d to from_level-%d %" PRIu64
|
||||
@@ -2263,7 +2264,7 @@ Status DB::OpenAndTrimHistory(
|
||||
return s;
|
||||
}
|
||||
|
||||
DB* db = nullptr;
|
||||
std::unique_ptr<DB> db;
|
||||
s = DB::Open(db_options, dbname, column_families, handles, &db);
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
@@ -2272,7 +2273,7 @@ Status DB::OpenAndTrimHistory(
|
||||
CompactRangeOptions options;
|
||||
options.bottommost_level_compaction =
|
||||
BottommostLevelCompaction::kForceOptimized;
|
||||
auto db_impl = static_cast_with_check<DBImpl>(db);
|
||||
auto db_impl = static_cast_with_check<DBImpl>(db.get());
|
||||
for (auto handle : *handles) {
|
||||
assert(handle != nullptr);
|
||||
auto cfh = static_cast_with_check<ColumnFamilyHandleImpl>(handle);
|
||||
@@ -2294,14 +2295,14 @@ Status DB::OpenAndTrimHistory(
|
||||
assert(temp_s.ok());
|
||||
}
|
||||
handles->clear();
|
||||
delete db;
|
||||
db.reset();
|
||||
};
|
||||
if (!s.ok()) {
|
||||
clean_op();
|
||||
return s;
|
||||
}
|
||||
|
||||
dbptr->reset(db);
|
||||
*dbptr = std::move(db);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +121,11 @@ class DBImplReadOnly : public DBImpl {
|
||||
return Status::NotSupported("Not supported operation in read only mode.");
|
||||
}
|
||||
|
||||
using DBImpl::FlushWAL;
|
||||
Status FlushWAL(const FlushWALOptions& /*options*/) override {
|
||||
return Status::NotSupported("Not supported operation in read only mode.");
|
||||
}
|
||||
|
||||
using DB::IngestExternalFile;
|
||||
Status IngestExternalFile(
|
||||
ColumnFamilyHandle* /*column_family*/,
|
||||
|
||||
@@ -509,10 +509,6 @@ Iterator* DBImplSecondary::NewIterator(const ReadOptions& _read_options,
|
||||
if (read_options.io_activity == Env::IOActivity::kUnknown) {
|
||||
read_options.io_activity = Env::IOActivity::kDBIterator;
|
||||
}
|
||||
if (read_options.managed) {
|
||||
return NewErrorIterator(
|
||||
Status::NotSupported("Managed iterator is not supported anymore."));
|
||||
}
|
||||
if (read_options.read_tier == kPersistedTier) {
|
||||
return NewErrorIterator(Status::NotSupported(
|
||||
"ReadTier::kPersistedData is not yet supported in iterators."));
|
||||
@@ -588,9 +584,6 @@ Status DBImplSecondary::NewIterators(
|
||||
if (read_options.io_activity == Env::IOActivity::kUnknown) {
|
||||
read_options.io_activity = Env::IOActivity::kDBIterator;
|
||||
}
|
||||
if (read_options.managed) {
|
||||
return Status::NotSupported("Managed iterator is not supported anymore.");
|
||||
}
|
||||
if (read_options.read_tier == kPersistedTier) {
|
||||
return Status::NotSupported(
|
||||
"ReadTier::kPersistedData is not yet supported in iterators.");
|
||||
@@ -987,11 +980,8 @@ Status DBImplSecondary::ParseCompactionProgressFile(
|
||||
Slice slice;
|
||||
std::string record;
|
||||
|
||||
while (compaction_progress_reader.ReadRecord(&slice, &record)) {
|
||||
if (!reader_status.ok()) {
|
||||
return reader_status;
|
||||
}
|
||||
|
||||
while (compaction_progress_reader.ReadRecord(&slice, &record) &&
|
||||
reader_status.ok()) {
|
||||
VersionEdit edit;
|
||||
s = edit.DecodeFrom(slice);
|
||||
if (!s.ok()) {
|
||||
@@ -1004,6 +994,10 @@ Status DBImplSecondary::ParseCompactionProgressFile(
|
||||
}
|
||||
}
|
||||
|
||||
if (!reader_status.ok()) {
|
||||
return reader_status;
|
||||
}
|
||||
|
||||
if (!s.ok()) {
|
||||
return s;
|
||||
}
|
||||
@@ -1545,7 +1539,7 @@ Status DB::OpenAndCompact(
|
||||
}
|
||||
|
||||
// 5. Open db As Secondary
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
std::vector<ColumnFamilyHandle*> handles;
|
||||
s = DB::OpenAsSecondary(db_options, name, output_directory, column_families,
|
||||
&handles, &db);
|
||||
@@ -1555,7 +1549,7 @@ Status DB::OpenAndCompact(
|
||||
assert(db);
|
||||
|
||||
TEST_SYNC_POINT_CALLBACK(
|
||||
"DBImplSecondary::OpenAndCompact::AfterOpenAsSecondary:0", db);
|
||||
"DBImplSecondary::OpenAndCompact::AfterOpenAsSecondary:0", db.get());
|
||||
|
||||
// 6. Find the handle of the Column Family that this will compact
|
||||
ColumnFamilyHandle* cfh = nullptr;
|
||||
@@ -1570,7 +1564,8 @@ Status DB::OpenAndCompact(
|
||||
// 7. Run the compaction without installation.
|
||||
// Output will be stored in the directory specified by output_directory
|
||||
CompactionServiceResult compaction_result;
|
||||
DBImplSecondary* db_secondary = static_cast_with_check<DBImplSecondary>(db);
|
||||
DBImplSecondary* db_secondary =
|
||||
static_cast_with_check<DBImplSecondary>(db.get());
|
||||
s = db_secondary->CompactWithoutInstallation(options, cfh, compaction_input,
|
||||
&compaction_result);
|
||||
|
||||
@@ -1581,7 +1576,7 @@ Status DB::OpenAndCompact(
|
||||
for (auto& handle : handles) {
|
||||
delete handle;
|
||||
}
|
||||
delete db;
|
||||
db.reset();
|
||||
if (s.ok()) {
|
||||
return serialization_status;
|
||||
} else {
|
||||
|
||||
@@ -216,9 +216,9 @@ class DBImplSecondary : public DBImpl {
|
||||
|
||||
using DBImpl::SetOptions;
|
||||
Status SetOptions(
|
||||
ColumnFamilyHandle* /*cfd*/,
|
||||
const std::unordered_map<std::string, std::string>& /*options_map*/)
|
||||
override {
|
||||
const std::unordered_map<ColumnFamilyHandle*,
|
||||
std::unordered_map<std::string, std::string>>&
|
||||
/*column_families_opts_map*/) override {
|
||||
// Currently not supported because changing certain options may cause
|
||||
// flush/compaction and/or write to MANIFEST.
|
||||
return Status::NotSupported("Not supported operation in secondary mode.");
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "memory/arena.h"
|
||||
#include "monitoring/perf_context_imp.h"
|
||||
#include "rocksdb/env.h"
|
||||
#include "rocksdb/io_dispatcher.h"
|
||||
#include "rocksdb/iterator.h"
|
||||
#include "rocksdb/merge_operator.h"
|
||||
#include "rocksdb/options.h"
|
||||
@@ -1621,6 +1622,14 @@ void DBIter::Prepare(const MultiScanArgs& scan_opts) {
|
||||
new_scan_opts.emplace(scan_opts);
|
||||
scan_opts_.swap(new_scan_opts);
|
||||
scan_index_ = 0;
|
||||
|
||||
// Create a shared IODispatcher if not provided. This allows all
|
||||
// BlockBasedTableIterators in this scan to share a single dispatcher,
|
||||
// enabling better IO coordination and future rate limiting.
|
||||
if (!scan_opts_.value().io_dispatcher) {
|
||||
scan_opts_->io_dispatcher.reset(NewIODispatcher());
|
||||
}
|
||||
|
||||
if (!scan_opts.empty()) {
|
||||
iter_.Prepare(&scan_opts_.value());
|
||||
} else {
|
||||
|
||||
+371
-6
@@ -16,6 +16,7 @@
|
||||
#include "db/db_test_util.h"
|
||||
#include "port/port.h"
|
||||
#include "port/stack_trace.h"
|
||||
#include "rocksdb/io_dispatcher.h"
|
||||
#include "rocksdb/iostats_context.h"
|
||||
#include "rocksdb/perf_context.h"
|
||||
#include "table/block_based/flush_block_policy_impl.h"
|
||||
@@ -1841,11 +1842,6 @@ class SliceTransformLimitedDomainGeneric : public SliceTransform {
|
||||
// prefix will be x????
|
||||
return src.size() >= 1;
|
||||
}
|
||||
|
||||
bool InRange(const Slice& dst) const override {
|
||||
// prefix will be x????
|
||||
return dst.size() == 1;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(DBIteratorTest, IterSeekForPrevCrossingFiles) {
|
||||
@@ -2573,7 +2569,7 @@ TEST_P(DBIteratorTest, AutoRefreshIterator) {
|
||||
ReadOptions read_options;
|
||||
std::unique_ptr<ManagedSnapshot> snapshot = nullptr;
|
||||
if (explicit_snapshot) {
|
||||
snapshot = std::make_unique<ManagedSnapshot>(db_);
|
||||
snapshot = std::make_unique<ManagedSnapshot>(db_.get());
|
||||
}
|
||||
read_options.snapshot =
|
||||
explicit_snapshot ? snapshot->snapshot() : nullptr;
|
||||
@@ -5027,6 +5023,375 @@ TEST_P(DBMultiScanIteratorTest, AsyncPrefetchWithExternalFileIngestion) {
|
||||
ASSERT_EQ(total_keys, 400);
|
||||
iter.reset();
|
||||
}
|
||||
|
||||
TEST_P(DBMultiScanIteratorTest, IODispatcherStatsVerification) {
|
||||
// Test that verifies all IOs go through the IODispatcher by checking stats
|
||||
auto options = CurrentOptions();
|
||||
options.target_file_size_base = 1 << 15; // 32KiB
|
||||
options.compaction_style = kCompactionStyleUniversal;
|
||||
options.num_levels = 50;
|
||||
options.compression = kNoCompression;
|
||||
DestroyAndReopen(options);
|
||||
|
||||
Random rnd(307);
|
||||
|
||||
// Create data - enough to create multiple data blocks
|
||||
for (int i = 0; i < 500; ++i) {
|
||||
std::stringstream ss;
|
||||
ss << "k" << std::setw(5) << std::setfill('0') << i;
|
||||
ASSERT_OK(Put(ss.str(), rnd.RandomString(1 << 10))); // 1KiB values
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(db_->CompactRange({}, nullptr, nullptr));
|
||||
|
||||
// Set up scan ranges
|
||||
std::vector<std::string> key_ranges({"k00000", "k00200", "k00300", "k00400"});
|
||||
ReadOptions ro;
|
||||
ro.fill_cache = GetParam();
|
||||
|
||||
ColumnFamilyHandle* cfh = dbfull()->DefaultColumnFamily();
|
||||
|
||||
// Create a tracking IODispatcher to verify IO statistics
|
||||
auto tracking_dispatcher = std::make_shared<TrackingIODispatcher>();
|
||||
|
||||
MultiScanArgs scan_options(BytewiseComparator());
|
||||
scan_options.use_async_io = false; // Use sync IO for predictable stats
|
||||
scan_options.io_dispatcher = tracking_dispatcher;
|
||||
scan_options.insert(key_ranges[0], key_ranges[1]);
|
||||
scan_options.insert(key_ranges[2], key_ranges[3]);
|
||||
|
||||
std::unique_ptr<MultiScan> iter =
|
||||
dbfull()->NewMultiScan(ro, cfh, scan_options);
|
||||
ASSERT_NE(iter, nullptr);
|
||||
|
||||
// Scan through all data
|
||||
int total_keys = 0;
|
||||
try {
|
||||
for (auto range : *iter) {
|
||||
for (auto it : range) {
|
||||
it.first.ToString();
|
||||
total_keys++;
|
||||
}
|
||||
}
|
||||
} catch (MultiScanException& ex) {
|
||||
ASSERT_NOK(ex.status());
|
||||
std::cerr << "Iterator returned status " << ex.what();
|
||||
abort();
|
||||
} catch (std::logic_error& ex) {
|
||||
std::cerr << "Iterator returned logic error " << ex.what();
|
||||
abort();
|
||||
}
|
||||
|
||||
// We scanned ~200 keys in range 1 and ~100 keys in range 2
|
||||
ASSERT_EQ(total_keys, 300);
|
||||
|
||||
// Verify that IO operations went through the IODispatcher
|
||||
// The total IO operations should be > 0 (either sync reads, async reads, or
|
||||
// cache hits)
|
||||
uint64_t total_ops = tracking_dispatcher->GetTotalIOOperations();
|
||||
ASSERT_GT(total_ops, 0) << "Expected some IO operations through IODispatcher";
|
||||
|
||||
// Verify that we have at least one ReadSet created
|
||||
ASSERT_GT(tracking_dispatcher->GetReadSets().size(), 0)
|
||||
<< "Expected at least one ReadSet to be created";
|
||||
|
||||
// Since we used sync IO, we should have sync reads (or cache hits if cached)
|
||||
uint64_t sync_reads = tracking_dispatcher->GetTotalSyncReads();
|
||||
uint64_t cache_hits = tracking_dispatcher->GetTotalCacheHits();
|
||||
ASSERT_GT(sync_reads + cache_hits, 0)
|
||||
<< "Expected sync reads or cache hits for sync IO mode";
|
||||
|
||||
iter.reset();
|
||||
}
|
||||
|
||||
TEST_P(DBMultiScanIteratorTest, IODispatcherPrefetchKnownBlocks) {
|
||||
// Test that verifies we prefetch a known/expected number of blocks.
|
||||
// Uses FlushBlockEveryKeyPolicyFactory to create exactly one block per key,
|
||||
// making the block count predictable and verifiable.
|
||||
auto options = CurrentOptions();
|
||||
options.compression = kNoCompression;
|
||||
options.disable_auto_compactions = true;
|
||||
|
||||
// Configure to create exactly one block per key
|
||||
BlockBasedTableOptions table_options;
|
||||
table_options.flush_block_policy_factory =
|
||||
std::make_shared<FlushBlockEveryKeyPolicyFactory>();
|
||||
// Use a block cache (required by IODispatcher), but use a fresh one
|
||||
// that won't have any cached data
|
||||
table_options.block_cache = NewLRUCache(10 * 1024 * 1024); // 10MB cache
|
||||
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Create exactly 100 keys, each in its own block
|
||||
const int kNumKeys = 100;
|
||||
const int kValueSize = 100; // Fixed value size for predictability
|
||||
std::string value(kValueSize, 'v');
|
||||
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
std::stringstream ss;
|
||||
ss << "k" << std::setw(3) << std::setfill('0') << i;
|
||||
ASSERT_OK(Put(ss.str(), value));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ColumnFamilyHandle* cfh = dbfull()->DefaultColumnFamily();
|
||||
|
||||
// Create a tracking IODispatcher to verify IO statistics
|
||||
auto tracking_dispatcher = std::make_shared<TrackingIODispatcher>();
|
||||
|
||||
// Define scan ranges with known block counts:
|
||||
// Range 1: k000 to k020 (20 keys = 20 blocks)
|
||||
// Range 2: k050 to k060 (10 keys = 10 blocks)
|
||||
// Total expected blocks to read: 30
|
||||
MultiScanArgs scan_options(BytewiseComparator());
|
||||
scan_options.use_async_io = false; // Use sync IO for predictable stats
|
||||
scan_options.io_dispatcher = tracking_dispatcher;
|
||||
scan_options.insert("k000", "k020");
|
||||
scan_options.insert("k050", "k060");
|
||||
|
||||
ReadOptions ro;
|
||||
ro.fill_cache = false; // Don't fill cache, ensure fresh reads
|
||||
|
||||
std::unique_ptr<MultiScan> iter =
|
||||
dbfull()->NewMultiScan(ro, cfh, scan_options);
|
||||
ASSERT_NE(iter, nullptr);
|
||||
|
||||
// Scan through all data and count keys
|
||||
int total_keys = 0;
|
||||
try {
|
||||
for (auto range : *iter) {
|
||||
for (auto it : range) {
|
||||
it.first.ToString();
|
||||
total_keys++;
|
||||
}
|
||||
}
|
||||
} catch (MultiScanException& ex) {
|
||||
ASSERT_NOK(ex.status());
|
||||
std::cerr << "Iterator returned status " << ex.what();
|
||||
abort();
|
||||
} catch (std::logic_error& ex) {
|
||||
std::cerr << "Iterator returned logic error " << ex.what();
|
||||
abort();
|
||||
}
|
||||
|
||||
// Verify we scanned the expected number of keys
|
||||
// Range 1: k000-k019 = 20 keys, Range 2: k050-k059 = 10 keys
|
||||
ASSERT_EQ(total_keys, 30) << "Expected 30 keys from two ranges";
|
||||
|
||||
// Verify IODispatcher statistics
|
||||
uint64_t total_ops = tracking_dispatcher->GetTotalIOOperations();
|
||||
uint64_t sync_reads = tracking_dispatcher->GetTotalSyncReads();
|
||||
|
||||
// We should have at least as many IO operations as blocks we need to read
|
||||
// (could be more due to index/filter blocks)
|
||||
ASSERT_GE(total_ops, 30)
|
||||
<< "Expected at least 30 IO operations for 30 data blocks";
|
||||
|
||||
// Since cache is fresh and fill_cache=false, all should be sync reads
|
||||
ASSERT_GE(sync_reads, 30)
|
||||
<< "Expected at least 30 sync reads for 30 data blocks";
|
||||
|
||||
// Verify we created ReadSets (one per range)
|
||||
size_t num_readsets = tracking_dispatcher->GetReadSets().size();
|
||||
ASSERT_GE(num_readsets, 1) << "Expected at least one ReadSet";
|
||||
|
||||
// Log the stats for debugging
|
||||
std::cout << "IODispatcher Stats: total_ops=" << total_ops
|
||||
<< ", sync_reads=" << sync_reads
|
||||
<< ", async_reads=" << tracking_dispatcher->GetTotalAsyncReads()
|
||||
<< ", cache_hits=" << tracking_dispatcher->GetTotalCacheHits()
|
||||
<< ", readsets=" << num_readsets << std::endl;
|
||||
|
||||
iter.reset();
|
||||
}
|
||||
|
||||
TEST_P(DBMultiScanIteratorTest, IODispatcherCacheHitVerification) {
|
||||
// Test that verifies cache hits are properly tracked through IODispatcher.
|
||||
// First scan populates cache, second scan should show cache hits.
|
||||
auto options = CurrentOptions();
|
||||
options.compression = kNoCompression;
|
||||
options.disable_auto_compactions = true;
|
||||
|
||||
BlockBasedTableOptions table_options;
|
||||
table_options.flush_block_policy_factory =
|
||||
std::make_shared<FlushBlockEveryKeyPolicyFactory>();
|
||||
// Enable block cache with enough space for all blocks
|
||||
table_options.block_cache = NewLRUCache(10 * 1024 * 1024); // 10MB cache
|
||||
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Create 50 keys, each in its own block
|
||||
const int kNumKeys = 50;
|
||||
std::string value(100, 'v');
|
||||
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
std::stringstream ss;
|
||||
ss << "k" << std::setw(3) << std::setfill('0') << i;
|
||||
ASSERT_OK(Put(ss.str(), value));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
|
||||
ColumnFamilyHandle* cfh = dbfull()->DefaultColumnFamily();
|
||||
|
||||
// First scan: populate the cache
|
||||
{
|
||||
auto dispatcher1 = std::make_shared<TrackingIODispatcher>();
|
||||
MultiScanArgs scan_options(BytewiseComparator());
|
||||
scan_options.use_async_io = false;
|
||||
scan_options.io_dispatcher = dispatcher1;
|
||||
scan_options.insert("k000", "k025"); // 25 keys
|
||||
|
||||
ReadOptions ro;
|
||||
ro.fill_cache = true; // Fill cache on first scan
|
||||
|
||||
std::unique_ptr<MultiScan> iter =
|
||||
dbfull()->NewMultiScan(ro, cfh, scan_options);
|
||||
ASSERT_NE(iter, nullptr);
|
||||
|
||||
int count = 0;
|
||||
try {
|
||||
for (auto range : *iter) {
|
||||
for (auto it : range) {
|
||||
it.first.ToString();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
} catch (MultiScanException& ex) {
|
||||
FAIL() << "First scan failed: " << ex.what();
|
||||
}
|
||||
ASSERT_EQ(count, 25);
|
||||
|
||||
// First scan should have sync reads (cache was empty)
|
||||
uint64_t first_sync = dispatcher1->GetTotalSyncReads();
|
||||
ASSERT_GE(first_sync, 25) << "First scan should have sync reads";
|
||||
|
||||
std::cout << "First scan stats: sync_reads=" << first_sync
|
||||
<< ", cache_hits=" << dispatcher1->GetTotalCacheHits()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
// Second scan: should get cache hits
|
||||
{
|
||||
auto dispatcher2 = std::make_shared<TrackingIODispatcher>();
|
||||
MultiScanArgs scan_options(BytewiseComparator());
|
||||
scan_options.use_async_io = false;
|
||||
scan_options.io_dispatcher = dispatcher2;
|
||||
scan_options.insert("k000", "k025"); // Same range as before
|
||||
|
||||
ReadOptions ro;
|
||||
ro.fill_cache = true;
|
||||
|
||||
std::unique_ptr<MultiScan> iter =
|
||||
dbfull()->NewMultiScan(ro, cfh, scan_options);
|
||||
ASSERT_NE(iter, nullptr);
|
||||
|
||||
int count = 0;
|
||||
try {
|
||||
for (auto range : *iter) {
|
||||
for (auto it : range) {
|
||||
it.first.ToString();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
} catch (MultiScanException& ex) {
|
||||
FAIL() << "Second scan failed: " << ex.what();
|
||||
}
|
||||
ASSERT_EQ(count, 25);
|
||||
|
||||
// Second scan should have cache hits (blocks were cached in first scan)
|
||||
uint64_t second_cache_hits = dispatcher2->GetTotalCacheHits();
|
||||
uint64_t second_sync = dispatcher2->GetTotalSyncReads();
|
||||
|
||||
std::cout << "Second scan stats: sync_reads=" << second_sync
|
||||
<< ", cache_hits=" << second_cache_hits << std::endl;
|
||||
|
||||
// We expect cache hits on the second scan for data blocks
|
||||
// Note: Some blocks might still need sync reads (e.g., if cache was
|
||||
// evicted)
|
||||
ASSERT_GE(second_cache_hits, 20)
|
||||
<< "Second scan should have cache hits for most blocks";
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(DBMultiScanIteratorTest, WastedBlocksTracking) {
|
||||
// Test that verifies wasted prefetch blocks are properly tracked.
|
||||
// When blocks are prefetched but skipped (e.g., due to seek), they should
|
||||
// be counted as wasted and recorded to MULTISCAN_PREFETCH_BLOCKS_WASTED.
|
||||
auto options = CurrentOptions();
|
||||
options.compression = kNoCompression;
|
||||
options.disable_auto_compactions = true;
|
||||
options.statistics = CreateDBStatistics();
|
||||
|
||||
BlockBasedTableOptions table_options;
|
||||
table_options.flush_block_policy_factory =
|
||||
std::make_shared<FlushBlockEveryKeyPolicyFactory>();
|
||||
table_options.block_cache = NewLRUCache(10 * 1024 * 1024); // 10MB cache
|
||||
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Create 100 keys, each in its own block
|
||||
const int kNumKeys = 100;
|
||||
std::string value(100, 'v');
|
||||
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
std::stringstream ss;
|
||||
ss << "k" << std::setw(3) << std::setfill('0') << i;
|
||||
ASSERT_OK(Put(ss.str(), value));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
ASSERT_OK(db_->CompactRange({}, nullptr, nullptr));
|
||||
|
||||
ColumnFamilyHandle* cfh = dbfull()->DefaultColumnFamily();
|
||||
|
||||
// Reset the wasted blocks counter before test
|
||||
options.statistics->setTickerCount(MULTISCAN_PREFETCH_BLOCKS_WASTED, 0);
|
||||
|
||||
// Set up MultiScan with two non-contiguous ranges:
|
||||
// Range 1: k000-k020 (20 keys/blocks)
|
||||
// Range 2: k050-k070 (20 keys/blocks)
|
||||
// The blocks between k020-k050 (30 blocks) should be wasted if prefetched
|
||||
MultiScanArgs scan_options(BytewiseComparator());
|
||||
scan_options.use_async_io = false;
|
||||
scan_options.insert("k000", "k020");
|
||||
scan_options.insert("k050", "k070");
|
||||
|
||||
ReadOptions ro;
|
||||
ro.fill_cache = GetParam();
|
||||
|
||||
{
|
||||
std::unique_ptr<MultiScan> iter =
|
||||
dbfull()->NewMultiScan(ro, cfh, scan_options);
|
||||
ASSERT_NE(iter, nullptr);
|
||||
|
||||
int count = 0;
|
||||
try {
|
||||
for (auto range : *iter) {
|
||||
for (auto it : range) {
|
||||
it.first.ToString();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
} catch (MultiScanException& ex) {
|
||||
FAIL() << "Scan failed: " << ex.what();
|
||||
}
|
||||
|
||||
// We should have scanned 40 keys total (20 + 20)
|
||||
ASSERT_EQ(count, 40);
|
||||
} // Iterator destroyed here, wasted blocks recorded
|
||||
|
||||
// Check that wasted blocks were recorded
|
||||
// The exact count depends on how many blocks were prefetched between ranges
|
||||
uint64_t wasted =
|
||||
options.statistics->getTickerCount(MULTISCAN_PREFETCH_BLOCKS_WASTED);
|
||||
|
||||
// We expect some wasted blocks due to the gap between ranges
|
||||
// The exact number depends on prefetch behavior, but should be > 0
|
||||
// if blocks between k020-k050 were prefetched
|
||||
std::cout << "Wasted blocks: " << wasted << std::endl;
|
||||
|
||||
// Note: The test verifies the tracking mechanism works.
|
||||
// The actual count depends on prefetch heuristics which may vary.
|
||||
}
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
@@ -180,13 +180,15 @@ TEST_F(DBTestXactLogIterator, TransactionLogIteratorCheckWhenArchive) {
|
||||
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
||||
ASSERT_OK(dbfull()->Flush(FlushOptions(), cf));
|
||||
// Try lots of things to ensure callback is triggered
|
||||
ASSERT_OK(dbfull()->TEST_SwitchWAL());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForBackgroundWork());
|
||||
ASSERT_OK(dbfull()->TEST_WaitForPurge());
|
||||
delete cf;
|
||||
// Normally hit several times; WART: perhaps more in parallel after flush
|
||||
// FIXME: this test is flaky
|
||||
// ASSERT_TRUE(callback_hit.LoadRelaxed());
|
||||
ASSERT_TRUE(callback_hit.LoadRelaxed());
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
Close();
|
||||
} while (ChangeCompactOptions());
|
||||
Close();
|
||||
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, OpenClose) {
|
||||
options.db_paths = {{data_path_0_, 2048}, {data_path_1_, 2048}};
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
if (!i) {
|
||||
printf("Open\n");
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
@@ -82,7 +82,6 @@ TEST_F(DBLogicalBlockSizeCacheTest, OpenClose) {
|
||||
ASSERT_EQ(1, cache_->GetRefCount(data_path_1_));
|
||||
ASSERT_OK(db->Close());
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
delete db;
|
||||
}
|
||||
ASSERT_OK(DestroyDB(dbname_, options, {}));
|
||||
}
|
||||
@@ -95,7 +94,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, OpenDelete) {
|
||||
options.env = env_.get();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
if (!i) {
|
||||
printf("Open\n");
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
@@ -106,7 +105,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, OpenDelete) {
|
||||
ASSERT_EQ(1, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(dbname_));
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
}
|
||||
ASSERT_OK(DestroyDB(dbname_, options, {}));
|
||||
@@ -122,7 +121,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, CreateColumnFamily) {
|
||||
ColumnFamilyOptions cf_options;
|
||||
cf_options.cf_paths = {{cf_path_0_, 1024}, {cf_path_1_, 2048}};
|
||||
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
ASSERT_EQ(1, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
@@ -153,7 +152,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, CreateColumnFamily) {
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(dbname_));
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
ASSERT_OK(DestroyDB(dbname_, options, {{"cf", cf_options}}));
|
||||
}
|
||||
@@ -173,7 +172,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, CreateColumnFamilies) {
|
||||
ColumnFamilyOptions cf_options;
|
||||
cf_options.cf_paths = {{cf_path_0_, 1024}};
|
||||
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
ASSERT_EQ(1, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
@@ -211,7 +210,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, CreateColumnFamilies) {
|
||||
ASSERT_OK(db->DestroyColumnFamilyHandle(cfs[1]));
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(dbname_));
|
||||
delete db;
|
||||
db.reset();
|
||||
|
||||
// Now cf_path_0_ in cache_ has been properly decreased and cf_path_0_'s entry
|
||||
// is dropped from cache
|
||||
@@ -233,15 +232,15 @@ TEST_F(DBLogicalBlockSizeCacheTest, OpenWithColumnFamilies) {
|
||||
cf_options.cf_paths = {{cf_path_0_, 1024}};
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
ColumnFamilyHandle* cf1 = nullptr;
|
||||
ColumnFamilyHandle* cf2 = nullptr;
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
ASSERT_OK(db->CreateColumnFamily(cf_options, "cf1", &cf1));
|
||||
ASSERT_OK(db->CreateColumnFamily(cf_options, "cf2", &cf2));
|
||||
ASSERT_OK(db->DestroyColumnFamilyHandle(cf1));
|
||||
ASSERT_OK(db->DestroyColumnFamilyHandle(cf2));
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
|
||||
std::vector<ColumnFamilyHandle*> cfs;
|
||||
@@ -298,7 +297,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, OpenWithColumnFamilies) {
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(dbname_));
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
}
|
||||
ASSERT_OK(
|
||||
@@ -315,7 +314,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, DestroyColumnFamilyHandle) {
|
||||
ColumnFamilyOptions cf_options;
|
||||
cf_options.cf_paths = {{cf_path_0_, 1024}};
|
||||
|
||||
DB* db;
|
||||
std::unique_ptr<DB> db;
|
||||
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||||
ASSERT_EQ(1, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(dbname_));
|
||||
@@ -336,7 +335,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, DestroyColumnFamilyHandle) {
|
||||
ASSERT_TRUE(cache_->Contains(cf_path_0_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(cf_path_0_));
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
|
||||
// Open with column families.
|
||||
@@ -369,7 +368,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, DestroyColumnFamilyHandle) {
|
||||
ASSERT_TRUE(cache_->Contains(cf_path_0_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(cf_path_0_));
|
||||
|
||||
delete db;
|
||||
db.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
}
|
||||
ASSERT_OK(DestroyDB(dbname_, options, {{"cf", cf_options}}));
|
||||
@@ -384,7 +383,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithDifferentPaths) {
|
||||
|
||||
ASSERT_OK(env_->CreateDirIfMissing(dbname_));
|
||||
|
||||
DB* db0;
|
||||
std::unique_ptr<DB> db0;
|
||||
ASSERT_OK(DB::Open(options, data_path_0_, &db0));
|
||||
ASSERT_EQ(1, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(data_path_0_));
|
||||
@@ -399,7 +398,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithDifferentPaths) {
|
||||
ASSERT_TRUE(cache_->Contains(cf_path_0_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(cf_path_0_));
|
||||
|
||||
DB* db1;
|
||||
std::unique_ptr<DB> db1;
|
||||
ASSERT_OK(DB::Open(options, data_path_1_, &db1));
|
||||
ASSERT_EQ(3, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(data_path_0_));
|
||||
@@ -424,7 +423,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithDifferentPaths) {
|
||||
ASSERT_EQ(1, cache_->GetRefCount(cf_path_1_));
|
||||
|
||||
ASSERT_OK(db0->DestroyColumnFamilyHandle(cf0));
|
||||
delete db0;
|
||||
db0.reset();
|
||||
ASSERT_EQ(2, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(data_path_1_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(data_path_1_));
|
||||
@@ -433,7 +432,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithDifferentPaths) {
|
||||
ASSERT_OK(DestroyDB(data_path_0_, options, {{"cf", cf_options0}}));
|
||||
|
||||
ASSERT_OK(db1->DestroyColumnFamilyHandle(cf1));
|
||||
delete db1;
|
||||
db1.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
ASSERT_OK(DestroyDB(data_path_1_, options, {{"cf", cf_options1}}));
|
||||
}
|
||||
@@ -450,7 +449,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithSamePaths) {
|
||||
|
||||
ASSERT_OK(env_->CreateDirIfMissing(dbname_));
|
||||
|
||||
DB* db0;
|
||||
std::unique_ptr<DB> db0;
|
||||
ASSERT_OK(DB::Open(options, dbname_ + "/db0", &db0));
|
||||
ASSERT_EQ(1, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(data_path_0_));
|
||||
@@ -464,7 +463,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithSamePaths) {
|
||||
ASSERT_TRUE(cache_->Contains(cf_path_0_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(cf_path_0_));
|
||||
|
||||
DB* db1;
|
||||
std::unique_ptr<DB> db1;
|
||||
ASSERT_OK(DB::Open(options, dbname_ + "/db1", &db1));
|
||||
ASSERT_EQ(2, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(data_path_0_));
|
||||
@@ -481,7 +480,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithSamePaths) {
|
||||
ASSERT_EQ(2, cache_->GetRefCount(cf_path_0_));
|
||||
|
||||
ASSERT_OK(db0->DestroyColumnFamilyHandle(cf0));
|
||||
delete db0;
|
||||
db0.reset();
|
||||
ASSERT_EQ(2, cache_->Size());
|
||||
ASSERT_TRUE(cache_->Contains(data_path_0_));
|
||||
ASSERT_EQ(1, cache_->GetRefCount(data_path_0_));
|
||||
@@ -490,7 +489,7 @@ TEST_F(DBLogicalBlockSizeCacheTest, MultiDBWithSamePaths) {
|
||||
ASSERT_OK(DestroyDB(dbname_ + "/db0", options, {{"cf", cf_options}}));
|
||||
|
||||
ASSERT_OK(db1->DestroyColumnFamilyHandle(cf1));
|
||||
delete db1;
|
||||
db1.reset();
|
||||
ASSERT_EQ(0, cache_->Size());
|
||||
ASSERT_OK(DestroyDB(dbname_ + "/db1", options, {{"cf", cf_options}}));
|
||||
}
|
||||
|
||||
@@ -117,8 +117,6 @@ class TestPrefixExtractor : public SliceTransform {
|
||||
return separator(key) != nullptr;
|
||||
}
|
||||
|
||||
bool InRange(const Slice& /*key*/) const override { return false; }
|
||||
|
||||
private:
|
||||
const char* separator(const Slice& key) const {
|
||||
return static_cast<const char*>(memchr(key.data(), '_', key.size()));
|
||||
|
||||
@@ -386,7 +386,7 @@ TEST_F(DBMergeOperatorTest, MergeOperandThresholdExceeded) {
|
||||
snapshots.reserve(3);
|
||||
|
||||
for (size_t i = 0; i < keys.size(); ++i) {
|
||||
snapshots.emplace_back(db_);
|
||||
snapshots.emplace_back(db_.get());
|
||||
|
||||
const std::string suffix = std::to_string(i + 1);
|
||||
|
||||
@@ -985,7 +985,7 @@ TEST_F(DBMergeOperatorTest, MaxSuccessiveMergesBaseValues) {
|
||||
// max_successive_merges.
|
||||
constexpr size_t max_key_versions = 8;
|
||||
std::vector<KeyVersion> key_versions;
|
||||
ASSERT_OK(GetAllKeyVersions(db_, db_->DefaultColumnFamily(), key, key,
|
||||
ASSERT_OK(GetAllKeyVersions(db_.get(), db_->DefaultColumnFamily(), key, key,
|
||||
max_key_versions, &key_versions));
|
||||
ASSERT_EQ(key_versions.size(), 2);
|
||||
ASSERT_EQ(key_versions[0].type, kTypeValue);
|
||||
@@ -1009,7 +1009,7 @@ TEST_F(DBMergeOperatorTest, MaxSuccessiveMergesBaseValues) {
|
||||
// max_successive_merges.
|
||||
constexpr size_t max_key_versions = 8;
|
||||
std::vector<KeyVersion> key_versions;
|
||||
ASSERT_OK(GetAllKeyVersions(db_, db_->DefaultColumnFamily(), key, key,
|
||||
ASSERT_OK(GetAllKeyVersions(db_.get(), db_->DefaultColumnFamily(), key, key,
|
||||
max_key_versions, &key_versions));
|
||||
ASSERT_EQ(key_versions.size(), 3);
|
||||
ASSERT_EQ(key_versions[0].type, kTypeValue);
|
||||
@@ -1038,7 +1038,7 @@ TEST_F(DBMergeOperatorTest, MaxSuccessiveMergesBaseValues) {
|
||||
// max_successive_merges.
|
||||
constexpr size_t max_key_versions = 8;
|
||||
std::vector<KeyVersion> key_versions;
|
||||
ASSERT_OK(GetAllKeyVersions(db_, db_->DefaultColumnFamily(), key, key,
|
||||
ASSERT_OK(GetAllKeyVersions(db_.get(), db_->DefaultColumnFamily(), key, key,
|
||||
max_key_versions, &key_versions));
|
||||
ASSERT_EQ(key_versions.size(), 3);
|
||||
ASSERT_EQ(key_versions[0].type, kTypeWideColumnEntity);
|
||||
|
||||
@@ -1689,6 +1689,46 @@ TEST_F(DBOptionsTest, SetOptionsNoManifestWrite) {
|
||||
ASSERT_EQ(Get("x"), "x");
|
||||
}
|
||||
|
||||
TEST_F(DBOptionsTest, SetOptionsMultipleColumnFamilies) {
|
||||
Options options;
|
||||
options.create_if_missing = true;
|
||||
options.env = CurrentOptions().env;
|
||||
options.disable_auto_compactions = true;
|
||||
Reopen(options);
|
||||
|
||||
// Create two additional column families
|
||||
CreateColumnFamilies({"cf1", "cf2"}, options);
|
||||
ReopenWithColumnFamilies({"default", "cf1", "cf2"}, options);
|
||||
|
||||
// Verify initial state - auto compaction should be disabled
|
||||
ASSERT_TRUE(dbfull()->GetOptions(handles_[0]).disable_auto_compactions);
|
||||
ASSERT_TRUE(dbfull()->GetOptions(handles_[1]).disable_auto_compactions);
|
||||
ASSERT_TRUE(dbfull()->GetOptions(handles_[2]).disable_auto_compactions);
|
||||
|
||||
// Set options on multiple column families at once
|
||||
ASSERT_OK(dbfull()->SetOptions({handles_[1], handles_[2]},
|
||||
{{"disable_auto_compactions", "false"}}));
|
||||
|
||||
ASSERT_TRUE(
|
||||
dbfull()->GetOptions(handles_[0]).disable_auto_compactions); // unchanged
|
||||
ASSERT_FALSE(
|
||||
dbfull()->GetOptions(handles_[1]).disable_auto_compactions); // changed
|
||||
ASSERT_FALSE(
|
||||
dbfull()->GetOptions(handles_[2]).disable_auto_compactions); // changed
|
||||
|
||||
std::unordered_map<ColumnFamilyHandle*,
|
||||
std::unordered_map<std::string, std::string>>
|
||||
options_map;
|
||||
options_map[handles_[0]] = {{"disable_auto_compactions", "false"}};
|
||||
options_map[handles_[1]] = {{"disable_auto_compactions", "true"}};
|
||||
options_map[handles_[2]] = {{"disable_auto_compactions", "true"}};
|
||||
ASSERT_OK(dbfull()->SetOptions(options_map));
|
||||
|
||||
ASSERT_FALSE(dbfull()->GetOptions(handles_[0]).disable_auto_compactions);
|
||||
ASSERT_TRUE(dbfull()->GetOptions(handles_[1]).disable_auto_compactions);
|
||||
ASSERT_TRUE(dbfull()->GetOptions(handles_[2]).disable_auto_compactions);
|
||||
}
|
||||
|
||||
} // namespace ROCKSDB_NAMESPACE
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
+32
-13
@@ -384,7 +384,7 @@ TEST_F(DBPropertiesTest, AggregatedTableProperties) {
|
||||
|
||||
// Hold open a snapshot to prevent range tombstones from being compacted
|
||||
// away.
|
||||
ManagedSnapshot snapshot(db_);
|
||||
ManagedSnapshot snapshot(db_.get());
|
||||
|
||||
Random rnd(5632);
|
||||
for (int table = 1; table <= kTableCount; ++table) {
|
||||
@@ -582,7 +582,7 @@ TEST_F(DBPropertiesTest, AggregatedTablePropertiesAtLevel) {
|
||||
DestroyAndReopen(options);
|
||||
|
||||
// Hold open a snapshot to prevent range tombstones from being compacted away.
|
||||
ManagedSnapshot snapshot(db_);
|
||||
ManagedSnapshot snapshot(db_.get());
|
||||
|
||||
std::string level_tp_strings[kMaxLevel];
|
||||
std::string tp_string;
|
||||
@@ -1519,16 +1519,14 @@ TEST_F(DBPropertiesTest, NeedCompactHintPersistentTest) {
|
||||
|
||||
// Excluded from RocksDB lite tests due to `GetPropertiesOfAllTables()` usage.
|
||||
TEST_F(DBPropertiesTest, BlockAddForCompressionSampling) {
|
||||
// Sampled compression requires at least one of the following four types.
|
||||
if (!Snappy_Supported() && !Zlib_Supported() && !LZ4_Supported() &&
|
||||
!ZSTD_Supported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Options options = CurrentOptions();
|
||||
options.disable_auto_compactions = true;
|
||||
options.table_properties_collector_factories.emplace_back(
|
||||
std::make_shared<BlockCountingTablePropertiesCollectorFactory>());
|
||||
options.compression = kNoCompression;
|
||||
|
||||
bool fast_sampling_supported = Snappy_Supported() || LZ4_Supported();
|
||||
bool slow_sampling_supported = ZSTD_Supported() || Zlib_Supported();
|
||||
|
||||
for (bool sample_for_compression : {false, true}) {
|
||||
// For simplicity/determinism, sample 100% when enabled, or 0% when disabled
|
||||
@@ -1542,10 +1540,11 @@ TEST_F(DBPropertiesTest, BlockAddForCompressionSampling) {
|
||||
// L1_0 ["a", "b"]
|
||||
//
|
||||
// L0_0 was created by flush. L1_0 was created by compaction. Each file
|
||||
// contains one data block.
|
||||
// contains one data block with enough data to be compressible.
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
ASSERT_OK(Put("a", "val"));
|
||||
ASSERT_OK(Put("b", "val"));
|
||||
for (int j = 0; j < 50; ++j) {
|
||||
ASSERT_OK(Put(std::to_string(j), "thisismyvalue"));
|
||||
}
|
||||
ASSERT_OK(Flush());
|
||||
if (i == 1) {
|
||||
ASSERT_OK(db_->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
||||
@@ -1558,13 +1557,33 @@ TEST_F(DBPropertiesTest, BlockAddForCompressionSampling) {
|
||||
ASSERT_OK(db_->GetPropertiesOfAllTables(&file_to_props));
|
||||
ASSERT_EQ(2, file_to_props.size());
|
||||
for (const auto& file_and_props : file_to_props) {
|
||||
auto& user_props = file_and_props.second->user_collected_properties;
|
||||
auto& props = *file_and_props.second;
|
||||
auto& user_props = props.user_collected_properties;
|
||||
ASSERT_TRUE(user_props.find(BlockCountingTablePropertiesCollector::
|
||||
kNumSampledBlocksPropertyName) !=
|
||||
user_props.end());
|
||||
ASSERT_EQ(user_props.at(BlockCountingTablePropertiesCollector::
|
||||
kNumSampledBlocksPropertyName),
|
||||
std::to_string(sample_for_compression ? 1 : 0));
|
||||
if (sample_for_compression) {
|
||||
EXPECT_GT(props.fast_compression_estimated_data_size, 0);
|
||||
EXPECT_GT(props.slow_compression_estimated_data_size, 0);
|
||||
if (fast_sampling_supported) {
|
||||
EXPECT_LT(props.fast_compression_estimated_data_size,
|
||||
props.data_size);
|
||||
if (slow_sampling_supported) {
|
||||
EXPECT_LT(props.slow_compression_estimated_data_size,
|
||||
props.fast_compression_estimated_data_size);
|
||||
}
|
||||
}
|
||||
if (slow_sampling_supported) {
|
||||
EXPECT_LT(props.slow_compression_estimated_data_size,
|
||||
props.data_size);
|
||||
}
|
||||
} else {
|
||||
EXPECT_EQ(props.fast_compression_estimated_data_size, 0);
|
||||
EXPECT_EQ(props.slow_compression_estimated_data_size, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1845,7 +1864,7 @@ TEST_F(DBPropertiesTest, MinObsoleteSstNumberToKeep) {
|
||||
options.listeners.push_back(listener);
|
||||
options.level0_file_num_compaction_trigger = kNumL0Files;
|
||||
DestroyAndReopen(options);
|
||||
listener->SetDB(db_);
|
||||
listener->SetDB(db_.get());
|
||||
|
||||
for (int i = 0; i < kNumL0Files; ++i) {
|
||||
// Make sure they overlap in keyspace to prevent trivial move
|
||||
|
||||
@@ -2047,7 +2047,7 @@ TEST_F(DBRangeDelTest, IteratorReseek) {
|
||||
// Immutable memtable
|
||||
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(), Key(1),
|
||||
Key(2)));
|
||||
ASSERT_OK(static_cast_with_check<DBImpl>(db_)->TEST_SwitchMemtable());
|
||||
ASSERT_OK(dbfull()->TEST_SwitchMemtable());
|
||||
std::string value;
|
||||
ASSERT_TRUE(dbfull()->GetProperty(db_->DefaultColumnFamily(),
|
||||
"rocksdb.num-immutable-mem-table", &value));
|
||||
|
||||
@@ -237,7 +237,7 @@ TEST_F(DBReadOnlyTestWithTimestamp, IteratorAndGet) {
|
||||
it->Next(), ++count, ++key) {
|
||||
CheckIterUserEntry(it.get(), Key1(key), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
@@ -250,7 +250,7 @@ TEST_F(DBReadOnlyTestWithTimestamp, IteratorAndGet) {
|
||||
it->Prev(), ++count, --key) {
|
||||
CheckIterUserEntry(it.get(), Key1(key), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
@@ -272,7 +272,7 @@ TEST_F(DBReadOnlyTestWithTimestamp, IteratorAndGet) {
|
||||
it->Valid(); it->Next(), ++key, ++count) {
|
||||
CheckIterUserEntry(it.get(), Key1(key), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
@@ -282,7 +282,7 @@ TEST_F(DBReadOnlyTestWithTimestamp, IteratorAndGet) {
|
||||
it->Valid(); it->Prev(), --key, ++count) {
|
||||
CheckIterUserEntry(it.get(), Key1(key - 1), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
|
||||
+14
-15
@@ -56,12 +56,11 @@ class DBSecondaryTestBase : public DBBasicTestWithTimestampBase {
|
||||
ASSERT_OK(db_secondary_->DestroyColumnFamilyHandle(h));
|
||||
}
|
||||
handles_secondary_.clear();
|
||||
delete db_secondary_;
|
||||
db_secondary_ = nullptr;
|
||||
db_secondary_.reset();
|
||||
}
|
||||
|
||||
DBImplSecondary* db_secondary_full() {
|
||||
return static_cast<DBImplSecondary*>(db_secondary_);
|
||||
return static_cast<DBImplSecondary*>(db_secondary_.get());
|
||||
}
|
||||
|
||||
void CheckFileTypeCounts(const std::string& dir, int expected_log,
|
||||
@@ -69,7 +68,7 @@ class DBSecondaryTestBase : public DBBasicTestWithTimestampBase {
|
||||
|
||||
std::string secondary_path_;
|
||||
std::vector<ColumnFamilyHandle*> handles_secondary_;
|
||||
DB* db_secondary_;
|
||||
std::unique_ptr<DB> db_secondary_;
|
||||
};
|
||||
|
||||
void DBSecondaryTestBase::OpenSecondary(const Options& options) {
|
||||
@@ -152,8 +151,8 @@ TEST_F(DBSecondaryTest, NonExistingDb) {
|
||||
options.env = env_;
|
||||
options.max_open_files = -1;
|
||||
const std::string dbname = "/doesnt/exist";
|
||||
Status s =
|
||||
DB::OpenAsSecondary(options, dbname, secondary_path_, &db_secondary_);
|
||||
std::unique_ptr<DB> dbptr;
|
||||
Status s = DB::OpenAsSecondary(options, dbname, secondary_path_, &dbptr);
|
||||
ASSERT_TRUE(s.IsIOError());
|
||||
}
|
||||
|
||||
@@ -182,7 +181,7 @@ TEST_F(DBSecondaryTest, ReopenAsSecondary) {
|
||||
|
||||
ReadOptions ropts;
|
||||
ropts.verify_checksums = true;
|
||||
auto db1 = static_cast<DBImplSecondary*>(db_);
|
||||
auto db1 = static_cast<DBImplSecondary*>(db_.get());
|
||||
ASSERT_NE(nullptr, db1);
|
||||
Iterator* iter = db1->NewIterator(ropts);
|
||||
ASSERT_NE(nullptr, iter);
|
||||
@@ -834,7 +833,7 @@ TEST_F(DBSecondaryTest, OpenWithSubsetOfColumnFamilies) {
|
||||
options1.max_open_files = -1;
|
||||
OpenSecondary(options1);
|
||||
ASSERT_EQ(0, handles_secondary_.size());
|
||||
ASSERT_NE(nullptr, db_secondary_);
|
||||
ASSERT_NE(nullptr, db_secondary_.get());
|
||||
|
||||
ASSERT_OK(Put(0 /*cf*/, "foo", "foo_value"));
|
||||
ASSERT_OK(Put(1 /*cf*/, "foo", "foo_value"));
|
||||
@@ -1152,7 +1151,7 @@ TEST_F(DBSecondaryTest, DISABLED_SwitchWAL) {
|
||||
for (int k = 0; k != 16; ++k) {
|
||||
ASSERT_OK(Put("key" + std::to_string(k), "value" + std::to_string(k)));
|
||||
ASSERT_OK(db_secondary_->TryCatchUpWithPrimary());
|
||||
verify_db(dbfull(), db_secondary_);
|
||||
verify_db(dbfull(), db_secondary_.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1221,7 +1220,7 @@ TEST_F(DBSecondaryTest, DISABLED_SwitchWALMultiColumnFamilies) {
|
||||
TEST_SYNC_POINT(
|
||||
"DBSecondaryTest::SwitchWALMultipleColumnFamilies:BeforeCatchUp");
|
||||
ASSERT_OK(db_secondary_->TryCatchUpWithPrimary());
|
||||
verify_db(dbfull(), handles_, db_secondary_, handles_secondary_);
|
||||
verify_db(dbfull(), handles_, db_secondary_.get(), handles_secondary_);
|
||||
SyncPoint::GetInstance()->ClearTrace();
|
||||
}
|
||||
}
|
||||
@@ -1357,7 +1356,7 @@ TEST_F(DBSecondaryTest, OpenWithTransactionDB) {
|
||||
TransactionDBOptions txn_db_opts;
|
||||
ASSERT_OK(TransactionDB::Open(options, txn_db_opts, dbname_, &txn_db));
|
||||
ASSERT_NE(txn_db, nullptr);
|
||||
db_ = txn_db;
|
||||
db_.reset(txn_db);
|
||||
|
||||
std::vector<std::string> cfs = {"new_CF"};
|
||||
CreateColumnFamilies(cfs, options);
|
||||
@@ -1561,7 +1560,7 @@ TEST_F(DBSecondaryTestWithTimestamp, IteratorAndGet) {
|
||||
it->Next(), ++count, ++key) {
|
||||
CheckIterUserEntry(it.get(), Key1(key), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
@@ -1574,7 +1573,7 @@ TEST_F(DBSecondaryTestWithTimestamp, IteratorAndGet) {
|
||||
it->Prev(), ++count, --key) {
|
||||
CheckIterUserEntry(it.get(), Key1(key), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
@@ -1596,7 +1595,7 @@ TEST_F(DBSecondaryTestWithTimestamp, IteratorAndGet) {
|
||||
it->Valid(); it->Next(), ++key, ++count) {
|
||||
CheckIterUserEntry(it.get(), Key1(key), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
@@ -1606,7 +1605,7 @@ TEST_F(DBSecondaryTestWithTimestamp, IteratorAndGet) {
|
||||
it->Valid(); it->Prev(), --key, ++count) {
|
||||
CheckIterUserEntry(it.get(), Key1(key - 1), kTypeValue,
|
||||
"value" + std::to_string(i), write_timestamps[i]);
|
||||
get_value_and_check(db_, read_opts, it->key(), it->value(),
|
||||
get_value_and_check(db_.get(), read_opts, it->key(), it->value(),
|
||||
write_timestamps[i]);
|
||||
}
|
||||
ASSERT_OK(it->status());
|
||||
|
||||
Loaded 100 of 418 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user