Repository navigation
Conversation
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Each guarded call raises a per-thread striped counter on its database before reading the pointer; close() swaps the pointer, waits for the counters to drain, then releases children and the database. Prototype: covers the four Bindings classes and RocksIterator, not TransactionDB, Transaction or ReadBatch. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Draft: a prototype to decide on, not ready to merge.
Closing a database while another thread is inside a call on it crashes the JVM today. Reproduced in forked JVMs (eight threads against a close, 400 open/close rounds):
mainseekToFirst/next/value)rocksdb_iter_nextcompactRange()rocksdb::EnvLogger::FlushLockedget(byte[]),put(byte[], byte[])This is the close-versus-call window
docs/explanation.mddocuments. #342 could refuse a close from inside a callback because the caller is on the closing thread's own stack; here the caller is on another thread, soclose()needs shared state.How
NativeDBholds a counter of calls in flight, striped per thread (64 padded slots in anAtomicLongArray) so threads sharing a database do not contend on one cache line.enter()(raise the counter, then check the pointer) andexit()in afinally.close()swaps the pointer to NULL, then waits for the counters to drain, then releases children and the database. The counter is raised before the pointer is read and the pointer is swapped before the counters are read, so one side always sees the other: either the call throwsIllegalStateException, or the close waits for it.Behaviour change:
close()blocks until in-flight calls return.Cost
JMH on an Apple M5, 2 forks,
mainagainst this branch:mainFfmBenchmark.readsBytesFfmBenchmark.readsMemorySegmentFfmBenchmark.readsInstantViaPinnedFfmBenchmark.writesBytesNoWalFfmScaleBenchmark.iterateByteArrayFfmScaleBenchmark.iterateZeroCopyScans are the real cost: about 7 to 9 ns per entry, since each entry makes three guarded calls. Four threads sharing one database (
SharedDbReadBenchmark, added here) was inconclusive: run-to-run error was ±12 to 20%, larger than any effect of the guard.Open questions
close()currently waits; a thread in a longcompactRange()holds it until it returns.RocksDB*OperationsBindingsclasses, plusRocksIterator. Not guarded yet:TransactionDB's own calls,Transactionand its iterators,ReadBatch,ingestExternalFile, and a few tracing and monitoring calls.ReadOptionsorWriteOptionsthat another thread is passing to a call is still a race.NativeDBto reach the guard; a finished version needs a cleaner seam.Checklist
@TempDir, Given / When / Then (CloseWaitsForInFlightCallsTest)./mvnw test -pl core -ampasses./mvnw package -pl core,benchmarks -am -DskipTestspassesCHANGELOG.md: left for the maintainer🤖 Generated with Claude Code