From 2e5ec1a6796fdee3bb22d2f1328f2b2fc171cf4a Mon Sep 17 00:00:00 2001 From: Davide Angelocola Date: Sun, 15 Nov 2026 17:08:58 +0100 Subject: [PATCH 1/2] SharedDbReadBenchmark: point reads on one database from many threads Co-Authored-By: Claude Sonnet 5 --- .../benchmark/SharedDbReadBenchmark.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 benchmarks/src/test/java/io/github/dfa1/rocksdbffm/benchmark/SharedDbReadBenchmark.java diff --git a/benchmarks/src/test/java/io/github/dfa1/rocksdbffm/benchmark/SharedDbReadBenchmark.java b/benchmarks/src/test/java/io/github/dfa1/rocksdbffm/benchmark/SharedDbReadBenchmark.java new file mode 100644 index 00000000..33a6f08b --- /dev/null +++ b/benchmarks/src/test/java/io/github/dfa1/rocksdbffm/benchmark/SharedDbReadBenchmark.java @@ -0,0 +1,83 @@ +package io.github.dfa1.rocksdbffm.benchmark; + +import io.github.dfa1.rocksdbffm.Options; +import io.github.dfa1.rocksdbffm.ReadWriteDB; +import io.github.dfa1.rocksdbffm.RocksDB; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; + +import java.lang.foreign.Arena; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.TimeUnit; + +/// Point reads against one database shared by every benchmark thread: run with `-t N` to see whether +/// per-call bookkeeping on the shared handle scales. +@BenchmarkMode(Mode.Throughput) +@OutputTimeUnit(TimeUnit.SECONDS) +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@Fork(value = 1, jvmArgsPrepend = {"--enable-native-access=ALL-UNNAMED", "--sun-misc-unsafe-memory-access=allow"}) +public class SharedDbReadBenchmark { + + private ReadWriteDB db; + private Path dbPath; + + /// One key segment per thread: a confined arena cannot be shared. + @State(Scope.Thread) + public static class PerThread { + Arena arena; + MemorySegment key; + byte[] keyBytes; + + @Setup(Level.Trial) + public void setup() { + arena = Arena.ofConfined(); + keyBytes = TestData.READ_KEY_BYTES.clone(); + key = arena.allocateFrom(ValueLayout.JAVA_BYTE, keyBytes); + } + + @TearDown(Level.Trial) + public void teardown() { + arena.close(); + } + } + + @Setup(Level.Trial) + public void setup() throws Exception { + dbPath = Files.createTempDirectory("bench-shared-"); + try (Options createOptions = Options.newOptions().setCreateIfMissing(true)) { + db = RocksDB.openReadWrite(dbPath, createOptions); + } + db.put(TestData.READ_KEY_BYTES, TestData.READ_VALUE_BYTES); + } + + @TearDown(Level.Trial) + public void teardown() throws Exception { + db.close(); + TestData.deleteDir(dbPath); + } + + @Benchmark + public byte[] getByteArray(PerThread thread) { + return db.get(thread.keyBytes); + } + + @Benchmark + public long getZeroCopy(PerThread thread) { + return db.get(thread.key, MemorySegment::byteSize); + } +} From 886dd8e9e626ba264a1e5cd507757a3ae6ce8cae Mon Sep 17 00:00:00 2001 From: Davide Angelocola Date: Sun, 15 Nov 2026 17:29:01 +0100 Subject: [PATCH 2/2] Proposal: a database close() waits for calls in flight on other threads 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 --- .../io/github/dfa1/rocksdbffm/NativeDB.java | 57 +++++++ .../rocksdbffm/NativeObjectWithChildren.java | 6 + .../RocksDBCompactionOperationsBindings.java | 33 ++++ .../RocksDBReadOperationsBindings.java | 39 +++++ .../RocksDBTracingOperationsBindings.java | 9 ++ .../RocksDBWriteOperationsBindings.java | 63 ++++++++ .../github/dfa1/rocksdbffm/RocksIterator.java | 153 ++++++++++++++---- .../CloseWaitsForInFlightCallsTest.java | 72 +++++++++ 8 files changed, 402 insertions(+), 30 deletions(-) create mode 100644 core/src/test/java/io/github/dfa1/rocksdbffm/CloseWaitsForInFlightCallsTest.java diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeDB.java b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeDB.java index a91af1ae..09363c06 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeDB.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeDB.java @@ -7,7 +7,9 @@ import java.util.Objects; import java.util.SequencedMap; import java.util.Set; +import java.util.concurrent.atomic.AtomicLongArray; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.LockSupport; /// Base of every database type: a [NativeObjectWithChildren] that exposes the `rocksdb_t*` the shared /// capability interfaces ([RocksDBReadOperations], [RocksDBWriteOperations], ...) operate on. @@ -27,6 +29,61 @@ abstract class NativeDB extends NativeObjectWithChildren { private final AtomicReference> columnFamilies = new AtomicReference<>(Collections.unmodifiableSequencedMap(new LinkedHashMap<>())); + /// Calls in flight on this database or on an iterator borrowed from it, counted in per-thread stripes so + /// threads sharing the database do not contend on one counter. Each stripe is padded to its own cache lines. + private final AtomicLongArray inFlight = new AtomicLongArray((STRIPES + 2) * STRIPE_PADDING); + + private static final int STRIPES = 64; + private static final int STRIPE_PADDING = 16; + + private static int stripe() { + return (((int) Thread.currentThread().threadId() & (STRIPES - 1)) + 1) * STRIPE_PADDING; + } + + /// [#enter()] on `db`, which is always a [NativeDB]: every implementor of the capability interfaces extends it. + static NativeDB enter(Object db) { + NativeDB guard = (NativeDB) db; + guard.enter(); + return guard; + } + + /// Marks a native call as in flight; pair with [#exit()] in a `finally`. The count is raised before the + /// closed check, and [#close()] swaps the pointer before reading the counts, so one side always sees the + /// other: either this throws, or the close waits for [#exit()]. + /// + /// @throws IllegalStateException if this database has been closed + final void enter() { + int stripe = stripe(); + inFlight.getAndIncrement(stripe); + if (isClosed()) { + inFlight.getAndDecrement(stripe); + throw new IllegalStateException("native object is closed or ownership has been transferred"); + } + } + + final void exit() { + inFlight.getAndDecrement(stripe()); + } + + @Override + final void awaitInFlightCalls() { + for (int spins = 0; inFlightCalls() != 0; spins++) { + if (spins < 1_000) { + Thread.onSpinWait(); + } else { + LockSupport.parkNanos(100_000); + } + } + } + + private long inFlightCalls() { + long sum = 0; + for (int i = 1; i <= STRIPES; i++) { + sum += inFlight.get(i * STRIPE_PADDING); + } + return sum; + } + protected NativeDB(MemorySegment ptr) { super(ptr); } diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectWithChildren.java b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectWithChildren.java index 457d6e69..db320a27 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectWithChildren.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/NativeObjectWithChildren.java @@ -114,12 +114,18 @@ protected final void tryClose(MemorySegment ptr) throws Throwable { // own; anything still present after this loop is exactly the registerChild-added-during- // this-very-sweep stragglers described there, and registerChild's own recheck cleans // those up (whether that happens before or after this method returns). + awaitInFlightCalls(); for (NativeObject child : children) { child.release(); } tryCloseResource(ptr); } + /// Runs after this object's pointer has been swapped to NULL and before anything is released: a subclass that + /// tracks calls in flight on other threads waits here until they have returned. + void awaitInFlightCalls() { + } + /// Closes this object's own native resource. Same contract as /// [NativeObject#tryClose(MemorySegment)] — renamed only because `tryClose` itself is /// final here, to guarantee children are always closed first. diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBCompactionOperationsBindings.java b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBCompactionOperationsBindings.java index f03eb344..c69bcd9e 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBCompactionOperationsBindings.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBCompactionOperationsBindings.java @@ -77,6 +77,7 @@ private RocksDBCompactionOperationsBindings() { } static void compactRangeBytes(RocksDBCompactionOperations db, byte[] startKey, byte[] endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment s = startKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, startKey); MemorySegment e = endKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, endKey); @@ -85,10 +86,13 @@ static void compactRangeBytes(RocksDBCompactionOperations db, byte[] startKey, b e, endKey == null ? 0L : (long) endKey.length); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("compactRange failed", t); + } finally { + guard.exit(); } } static void compactRangeCfBytes(RocksDBCompactionOperations db, ColumnFamilyHandle cf, byte[] startKey, byte[] endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment s = startKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, startKey); MemorySegment e = endKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, endKey); @@ -97,10 +101,13 @@ static void compactRangeCfBytes(RocksDBCompactionOperations db, ColumnFamilyHand e, endKey == null ? 0L : (long) endKey.length); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("compactRange failed", t); + } finally { + guard.exit(); } } static void compactRangeBuffer(RocksDBCompactionOperations db, ByteBuffer startKey, ByteBuffer endKey) { + NativeDB guard = NativeDB.enter(db); try { MemorySegment s = startKey == null ? MemorySegment.NULL : MemorySegment.ofBuffer(startKey); MemorySegment e = endKey == null ? MemorySegment.NULL : MemorySegment.ofBuffer(endKey); @@ -109,10 +116,13 @@ static void compactRangeBuffer(RocksDBCompactionOperations db, ByteBuffer startK e, endKey == null ? 0L : (long) endKey.remaining()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("compactRange failed", t); + } finally { + guard.exit(); } } static void compactRangeSegment(RocksDBCompactionOperations db, MemorySegment startKey, MemorySegment endKey) { + NativeDB guard = NativeDB.enter(db); try { MemorySegment s = startKey == null ? MemorySegment.NULL : startKey; MemorySegment e = endKey == null ? MemorySegment.NULL : endKey; @@ -121,10 +131,13 @@ static void compactRangeSegment(RocksDBCompactionOperations db, MemorySegment st e, e == MemorySegment.NULL ? 0L : e.byteSize()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("compactRange failed", t); + } finally { + guard.exit(); } } static void compactRangeOptBytes(RocksDBCompactionOperations db, CompactOptions opts, byte[] startKey, byte[] endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment s = startKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, startKey); MemorySegment e = endKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, endKey); @@ -133,10 +146,13 @@ static void compactRangeOptBytes(RocksDBCompactionOperations db, CompactOptions e, endKey == null ? 0L : (long) endKey.length); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("compactRange failed", t); + } finally { + guard.exit(); } } static void suggestCompactRangeBytes(RocksDBCompactionOperations db, byte[] startKey, byte[] endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment s = startKey == null ? MemorySegment.NULL : NativeCalls.toNative(arena, startKey); @@ -147,52 +163,69 @@ static void suggestCompactRangeBytes(RocksDBCompactionOperations db, byte[] star NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("suggestCompactRange failed", t); + } finally { + guard.exit(); } } static void disableFileDeletions(RocksDBCompactionOperations db) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DISABLE_FILE_DELETIONS.invokeExact(db.dbPtr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("disableFileDeletions failed", t); + } finally { + guard.exit(); } } static void enableFileDeletions(RocksDBCompactionOperations db) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_ENABLE_FILE_DELETIONS.invokeExact(db.dbPtr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("enableFileDeletions failed", t); + } finally { + guard.exit(); } } static void disableManualCompaction(RocksDBCompactionOperations db) { + NativeDB guard = NativeDB.enter(db); try { MH_DISABLE_MANUAL_COMPACTION.invokeExact(db.dbPtr()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("disableManualCompaction failed", t); + } finally { + guard.exit(); } } static void enableManualCompaction(RocksDBCompactionOperations db) { + NativeDB guard = NativeDB.enter(db); try { MH_ENABLE_MANUAL_COMPACTION.invokeExact(db.dbPtr()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("enableManualCompaction failed", t); + } finally { + guard.exit(); } } static void waitForCompact(RocksDBCompactionOperations db, WaitForCompactOptions options) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_WAIT_FOR_COMPACT.invokeExact(db.dbPtr(), options.ptr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("waitForCompact failed", t); + } finally { + guard.exit(); } } } diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBReadOperationsBindings.java b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBReadOperationsBindings.java index 54f6e82f..77ec4d42 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBReadOperationsBindings.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBReadOperationsBindings.java @@ -131,6 +131,7 @@ private RocksDBReadOperationsBindings() { /// /// Returns `null` if not found. static byte[] getBytes(RocksDBReadOperations db, ReadOptions readOpts, byte[] key) { + NativeDB guard = NativeDB.enter(db); // Single arena: it already needs one to marshal `key`, and withPinned would open // its own, paying for two Arena.ofConfined() per get instead of one. try (Arena arena = Arena.ofConfined()) { @@ -147,12 +148,15 @@ static byte[] getBytes(RocksDBReadOperations db, ReadOptions readOpts, byte[] ke } } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get failed", t); + } finally { + guard.exit(); } } /// ByteBuffer get via `rocksdb_get_into_buffer` — copies directly into the caller's buffer, /// with no intermediate PinnableSlice. Copies nothing when the buffer is too small. static CopyResult getIntoBuffer(RocksDBReadOperations db, ReadOptions readOpts, MemorySegment key, ByteBuffer value) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment valLenSeg = arena.allocate(NativeCalls.SIZE_T); @@ -171,12 +175,15 @@ static CopyResult getIntoBuffer(RocksDBReadOperations db, ReadOptions readOpts, return CopyResult.Copied.INSTANCE; } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get failed", t); + } finally { + guard.exit(); } } /// MemorySegment get via `rocksdb_get_into_buffer` — copies directly into the caller's /// segment, with no intermediate PinnableSlice. Copies nothing when `value` is too small. static CopyResult getIntoSegment(RocksDBReadOperations db, ReadOptions readOpts, MemorySegment key, MemorySegment value) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment valLenSeg = arena.allocate(NativeCalls.SIZE_T); @@ -194,12 +201,15 @@ static CopyResult getIntoSegment(RocksDBReadOperations db, ReadOptions readOpts, return CopyResult.Copied.INSTANCE; } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get failed", t); + } finally { + guard.exit(); } } /// Scoped zero-copy get via `rocksdb_get_pinned_v2`. [PinnableHandle] owns the pinned /// value's lifetime and every way it gets consumed. static R withPinned(RocksDBReadOperations db, ReadOptions readOpts, MemorySegment key, Mapper fn) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment handle = (MemorySegment) MH_GET_PINNED_V2.invokeExact(db.dbPtr(), readOpts.readPtr(), key, key.byteSize(), err); @@ -212,6 +222,8 @@ static R withPinned(RocksDBReadOperations db, ReadOptions readOpts, MemorySe } } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get_pinned failed", t); + } finally { + guard.exit(); } } @@ -219,6 +231,7 @@ static R withPinned(RocksDBReadOperations db, ReadOptions readOpts, MemorySe /// for the lifetime contract. static R withPinnedCf(RocksDBReadOperations db, ReadOptions readOpts, ColumnFamilyHandle cf, MemorySegment key, Mapper fn) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment handle = (MemorySegment) MH_GET_PINNED_CF_V2.invokeExact( @@ -232,6 +245,8 @@ static R withPinnedCf(RocksDBReadOperations db, ReadOptions readOpts, Column } } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get_pinned failed", t); + } finally { + guard.exit(); } } @@ -240,6 +255,7 @@ static R withPinnedCf(RocksDBReadOperations db, ReadOptions readOpts, Column /// Returns `null` if not found. static byte[] getCfBytes(RocksDBReadOperations db, ReadOptions readOpts, ColumnFamilyHandle cf, byte[] key) { + NativeDB guard = NativeDB.enter(db); // Single arena, same reasoning as getBytes above. try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); @@ -255,6 +271,8 @@ static byte[] getCfBytes(RocksDBReadOperations db, ReadOptions readOpts, ColumnF } } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get failed", t); + } finally { + guard.exit(); } } @@ -262,6 +280,7 @@ static byte[] getCfBytes(RocksDBReadOperations db, ReadOptions readOpts, ColumnF /// Copies nothing when the buffer is too small. static CopyResult getCfIntoBuffer(RocksDBReadOperations db, ReadOptions readOpts, ColumnFamilyHandle cf, MemorySegment key, ByteBuffer value) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment valLenSeg = arena.allocate(NativeCalls.SIZE_T); @@ -280,6 +299,8 @@ static CopyResult getCfIntoBuffer(RocksDBReadOperations db, ReadOptions readOpts return CopyResult.Copied.INSTANCE; } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get failed", t); + } finally { + guard.exit(); } } @@ -287,6 +308,7 @@ static CopyResult getCfIntoBuffer(RocksDBReadOperations db, ReadOptions readOpts /// copies directly into the caller's segment. Copies nothing when `value` is too small. static CopyResult getCfIntoSegment(RocksDBReadOperations db, ReadOptions readOpts, ColumnFamilyHandle cf, MemorySegment key, MemorySegment value) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment valLenSeg = arena.allocate(NativeCalls.SIZE_T); @@ -304,16 +326,21 @@ static CopyResult getCfIntoSegment(RocksDBReadOperations db, ReadOptions readOpt return CopyResult.Copied.INSTANCE; } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("get failed", t); + } finally { + guard.exit(); } } static boolean keyMayExistSegment(RocksDBReadOperations db, ReadOptions roOpts, MemorySegment key) { + NativeDB guard = NativeDB.enter(db); try { return NativeCalls.fromByte((byte) MH_KEY_MAY_EXIST.invokeExact(db.dbPtr(), roOpts.readPtr(), key, key.byteSize(), MemorySegment.NULL, MemorySegment.NULL, MemorySegment.NULL, 0L, MemorySegment.NULL)); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("keyMayExist failed", t); + } finally { + guard.exit(); } } @@ -327,12 +354,15 @@ static boolean keyMayExistBytes(RocksDBReadOperations db, ReadOptions roOpts, by static boolean keyMayExistCfSegment(RocksDBReadOperations db, ReadOptions roOpts, ColumnFamilyHandle cf, MemorySegment key) { + NativeDB guard = NativeDB.enter(db); try { return NativeCalls.fromByte((byte) MH_KEY_MAY_EXIST_CF.invokeExact(db.dbPtr(), roOpts.readPtr(), cf.ptr(), key, key.byteSize(), MemorySegment.NULL, MemorySegment.NULL, MemorySegment.NULL, 0L, MemorySegment.NULL)); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("keyMayExist failed", t); + } finally { + guard.exit(); } } @@ -347,6 +377,7 @@ static boolean keyMayExistCfBytes(RocksDBReadOperations db, ReadOptions roOpts, static RocksIterator createIteratorCf(RocksDBReadOperations db, ReadOptions readOpts, ColumnFamilyHandle cf) { + NativeDB guard = NativeDB.enter(db); try { MemorySegment iterPtr = (MemorySegment) MH_CREATE_ITERATOR_CF.invokeExact( db.dbPtr(), readOpts.readPtr(), cf.ptr()); @@ -354,6 +385,8 @@ static RocksIterator createIteratorCf(RocksDBReadOperations db, ReadOptions read return RocksIterator.create((NativeObjectWithChildren) db, readOpts, iterPtr); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("newIterator failed", t); + } finally { + guard.exit(); } } @@ -367,16 +400,20 @@ static Snapshot createSnapshot(NativeObjectWithChildren owningDb, MemorySegment } static Optional getProperty(RocksDBReadOperations db, Property property) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment propSeg = arena.allocateFrom(property.propertyName()); MemorySegment result = (MemorySegment) MH_PROPERTY_VALUE.invokeExact(db.dbPtr(), propSeg); return NativeCalls.toOptionalString(result); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("getProperty failed", t); + } finally { + guard.exit(); } } static OptionalLong getLongProperty(RocksDBReadOperations db, Property property) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment propSeg = arena.allocateFrom(property.propertyName()); MemorySegment out = arena.allocate(ValueLayout.JAVA_LONG); @@ -387,6 +424,8 @@ static OptionalLong getLongProperty(RocksDBReadOperations db, Property property) return OptionalLong.of(out.get(ValueLayout.JAVA_LONG, 0)); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("getLongProperty failed", t); + } finally { + guard.exit(); } } diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBTracingOperationsBindings.java b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBTracingOperationsBindings.java index 6739b0ac..54444a4d 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBTracingOperationsBindings.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBTracingOperationsBindings.java @@ -105,12 +105,15 @@ static void startTrace(RocksDBTracingOperations db, } static void endTrace(RocksDBTracingOperations db) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_END_TRACE.invokeExact(db.dbPtr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("endTrace failed", t); + } finally { + guard.exit(); } } @@ -136,12 +139,15 @@ static void startIoTrace(RocksDBTracingOperations db, } static void endIoTrace(RocksDBTracingOperations db) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_END_IO_TRACE.invokeExact(db.dbPtr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("endIoTrace failed", t); + } finally { + guard.exit(); } } @@ -194,12 +200,15 @@ static void startBlockCacheTrace(RocksDBTracingOperations db, } static void endBlockCacheTrace(RocksDBTracingOperations db) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_END_BLOCK_CACHE_TRACE.invokeExact(db.dbPtr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("endBlockCacheTrace failed", t); + } finally { + guard.exit(); } } } diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBWriteOperationsBindings.java b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBWriteOperationsBindings.java index 4ba310c6..9440e07a 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBWriteOperationsBindings.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksDBWriteOperationsBindings.java @@ -141,6 +141,7 @@ private RocksDBWriteOperationsBindings() { /// byte[] put — slow path, allocates native memory. static void putBytes(RocksDBWriteOperations db, WriteOptions writeOpts, byte[] key, byte[] value) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment k = NativeCalls.toNative(arena, key); @@ -149,6 +150,8 @@ static void putBytes(RocksDBWriteOperations db, WriteOptions writeOpts, byte[] k NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("put failed", t); + } finally { + guard.exit(); } } @@ -168,12 +171,15 @@ static void putBytes(Arena arena, RocksDBWriteOperations db, WriteOptions writeO /// MemorySegment put — zero-copy, caller supplies pre-allocated native segments. static void putSegment(RocksDBWriteOperations db, WriteOptions writeOpts, MemorySegment key, MemorySegment val) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_PUT.invokeExact(db.dbPtr(), writeOpts.ptr(), key, key.byteSize(), val, val.byteSize(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("put failed", t); + } finally { + guard.exit(); } } @@ -225,6 +231,7 @@ static void mergeSegment(Arena arena, RocksDBWriteOperations db, WriteOptions wr /// byte[] delete — slow path. static void deleteBytes(RocksDBWriteOperations db, WriteOptions writeOpts, byte[] key) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment k = NativeCalls.toNative(arena, key); @@ -232,48 +239,63 @@ static void deleteBytes(RocksDBWriteOperations db, WriteOptions writeOpts, byte[ NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("delete failed", t); + } finally { + guard.exit(); } } /// MemorySegment delete — zero-copy. static void deleteSegment(RocksDBWriteOperations db, WriteOptions writeOpts, MemorySegment key) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DELETE.invokeExact(db.dbPtr(), writeOpts.ptr(), key, key.byteSize(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("delete failed", t); + } finally { + guard.exit(); } } static void flush(RocksDBWriteOperations db, FlushOptions flushOptions) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_FLUSH.invokeExact(db.dbPtr(), flushOptions.ptr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("flush failed", t); + } finally { + guard.exit(); } } static void cancelAllBackgroundWork(RocksDBWriteOperations db, boolean wait) { + NativeDB guard = NativeDB.enter(db); try { MH_CANCEL_ALL_BACKGROUND_WORK.invokeExact(db.dbPtr(), NativeCalls.toByte(wait)); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("cancelAllBackgroundWork failed", t); + } finally { + guard.exit(); } } static SequenceNumber getLatestSequenceNumber(RocksDBWriteOperations db) { + NativeDB guard = NativeDB.enter(db); try { long seq = (long) MH_GET_LATEST_SEQUENCE_NUMBER.invokeExact(db.dbPtr()); return SequenceNumber.of(seq); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("getLatestSequenceNumber failed", t); + } finally { + guard.exit(); } } static WalIterator getUpdatesSince(RocksDBWriteOperations db, SequenceNumber sequenceNumber) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment iterPtr = (MemorySegment) MH_GET_UPDATES_SINCE.invokeExact( @@ -283,20 +305,26 @@ static WalIterator getUpdatesSince(RocksDBWriteOperations db, SequenceNumber seq return WalIterator.wrap((NativeObjectWithChildren) db, iterPtr); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("getUpdatesSince failed", t); + } finally { + guard.exit(); } } static void flushWal(RocksDBWriteOperations db, boolean sync) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_FLUSH_WAL.invokeExact(db.dbPtr(), NativeCalls.toByte(sync), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("flushWal failed", t); + } finally { + guard.exit(); } } static void deleteRangeCfBytes(RocksDBWriteOperations db, WriteOptions writeOpts, byte[] startKey, byte[] endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment cf = ((NativeDB) db).getColumnFamily(ColumnFamilyName.DEFAULT).ptr(); @@ -306,11 +334,14 @@ static void deleteRangeCfBytes(RocksDBWriteOperations db, WriteOptions writeOpts NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("deleteRange failed", t); + } finally { + guard.exit(); } } static void deleteRangeCfBuffer(RocksDBWriteOperations db, WriteOptions writeOpts, ByteBuffer startKey, ByteBuffer endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment cf = ((NativeDB) db).getColumnFamily(ColumnFamilyName.DEFAULT).ptr(); @@ -320,11 +351,14 @@ static void deleteRangeCfBuffer(RocksDBWriteOperations db, WriteOptions writeOpt NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("deleteRange failed", t); + } finally { + guard.exit(); } } static void deleteRangeCfSegment(RocksDBWriteOperations db, WriteOptions writeOpts, MemorySegment startKey, MemorySegment endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MemorySegment cf = ((NativeDB) db).getColumnFamily(ColumnFamilyName.DEFAULT).ptr(); @@ -333,16 +367,21 @@ static void deleteRangeCfSegment(RocksDBWriteOperations db, WriteOptions writeOp NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("deleteRange failed", t); + } finally { + guard.exit(); } } static void writeBatch(RocksDBWriteOperations db, WriteOptions writeOpts, WriteBatch batch) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_WRITE.invokeExact(db.dbPtr(), writeOpts.ptr(), batch.ptr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("write failed", t); + } finally { + guard.exit(); } } @@ -408,6 +447,7 @@ static void dropCf(NativeDB db, ColumnFamilyHandle handle) { /// byte[] put with explicit column family — slow path. static void putCfBytes(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, byte[] key, byte[] value) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_PUT_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), @@ -416,18 +456,23 @@ static void putCfBytes(RocksDBWriteOperations db, WriteOptions writeOpts, Column NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("put failed", t); + } finally { + guard.exit(); } } /// MemorySegment put with explicit column family — zero-copy. static void putCfSegment(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, MemorySegment key, MemorySegment val) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_PUT_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), key, key.byteSize(), val, val.byteSize(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("put failed", t); + } finally { + guard.exit(); } } @@ -470,6 +515,7 @@ static void mergeCfSegment(Arena arena, RocksDBWriteOperations db, WriteOptions /// byte[] delete with explicit column family — slow path. static void deleteCfBytes(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, byte[] key) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DELETE_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), @@ -477,18 +523,23 @@ static void deleteCfBytes(RocksDBWriteOperations db, WriteOptions writeOpts, Col NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("delete failed", t); + } finally { + guard.exit(); } } /// MemorySegment delete with explicit column family — zero-copy. static void deleteCfSegment(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, MemorySegment key) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DELETE_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), key, key.byteSize(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("delete failed", t); + } finally { + guard.exit(); } } @@ -496,6 +547,7 @@ static void deleteCfSegment(RocksDBWriteOperations db, WriteOptions writeOpts, C static void deleteRangeCfBytesExplicit(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, byte[] startKey, byte[] endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DELETE_RANGE_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), @@ -504,6 +556,8 @@ static void deleteRangeCfBytesExplicit(RocksDBWriteOperations db, WriteOptions w NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("deleteRange failed", t); + } finally { + guard.exit(); } } @@ -511,6 +565,7 @@ static void deleteRangeCfBytesExplicit(RocksDBWriteOperations db, WriteOptions w static void deleteRangeCfBufferExplicit(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, ByteBuffer startKey, ByteBuffer endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DELETE_RANGE_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), @@ -519,6 +574,8 @@ static void deleteRangeCfBufferExplicit(RocksDBWriteOperations db, WriteOptions NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("deleteRange failed", t); + } finally { + guard.exit(); } } @@ -526,6 +583,7 @@ static void deleteRangeCfBufferExplicit(RocksDBWriteOperations db, WriteOptions static void deleteRangeCfSegmentExplicit(RocksDBWriteOperations db, WriteOptions writeOpts, ColumnFamilyHandle cf, MemorySegment startKey, MemorySegment endKey) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_DELETE_RANGE_CF.invokeExact(db.dbPtr(), writeOpts.ptr(), cf.ptr(), @@ -533,16 +591,21 @@ static void deleteRangeCfSegmentExplicit(RocksDBWriteOperations db, WriteOptions NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("deleteRange failed", t); + } finally { + guard.exit(); } } static void flushCf(RocksDBWriteOperations db, FlushOptions flushOptions, ColumnFamilyHandle cf) { + NativeDB guard = NativeDB.enter(db); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_FLUSH_CF.invokeExact(db.dbPtr(), flushOptions.ptr(), cf.ptr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("flush failed", t); + } finally { + guard.exit(); } } } diff --git a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksIterator.java b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksIterator.java index e6b260c6..72149fdd 100644 --- a/core/src/main/java/io/github/dfa1/rocksdbffm/RocksIterator.java +++ b/core/src/main/java/io/github/dfa1/rocksdbffm/RocksIterator.java @@ -107,6 +107,9 @@ public final class RocksIterator extends NativeObject { // Reused across all key/value calls to avoid per-call Arena allocation. private final MemorySegment lenSegment; + /// The database whose in-flight count covers this iterator's calls, or `null`. + private final NativeDB guard; + /// Package-private: created via [#create]. private RocksIterator(NativeObjectWithChildren owningDb, ReadOptions readOptions, MemorySegment ptr) { super(ptr); @@ -115,17 +118,35 @@ private RocksIterator(NativeObjectWithChildren owningDb, ReadOptions readOptions this.lenSegment = NativeCalls.malloc(NativeCalls.SIZE_T.byteSize()); // Two parents: the database or transaction, and the options, whose iterate bounds the native iterator keeps raw // pointers into. Holding them also keeps the options' bounds memory reachable. + this.guard = owningDb instanceof NativeDB db ? db : null; borrowedFrom(owningDb, readOptions); } + /// Marks a call on this iterator as in flight on its database, so the database's close waits for it instead + /// of freeing the iterator under it. A transaction's iterator has no guard yet. + private void enter() { + if (guard != null) { + guard.enter(); + } + } + + private void exit() { + if (guard != null) { + guard.exit(); + } + } + /// Package-private factory called by RocksDB. static RocksIterator create(RocksDBReadOperations db, ReadOptions readOptions) { + NativeDB dbGuard = NativeDB.enter(db); try { MemorySegment iterPtr = (MemorySegment) MH_CREATE.invokeExact(db.dbPtr(), readOptions.readPtr()); // Every implementor extends NativeObjectWithChildren (see RocksDBReadOperations#getSnapshot()). return new RocksIterator((NativeObjectWithChildren) db, readOptions, iterPtr); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("iterator create failed", t); + } finally { + dbGuard.exit(); } } @@ -141,19 +162,25 @@ static RocksIterator create(NativeObjectWithChildren owningDb, ReadOptions readO /// Positions the iterator at the first key in the database. public void seekToFirst() { + enter(); try { MH_SEEK_TO_FIRST.invokeExact(ptr()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seekToFirst failed", t); + } finally { + exit(); } } /// Positions the iterator at the last key in the database. public void seekToLast() { + enter(); try { MH_SEEK_TO_LAST.invokeExact(ptr()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seekToLast failed", t); + } finally { + exit(); } } @@ -161,10 +188,13 @@ public void seekToLast() { /// /// @param target the seek target key public void seek(byte[] target) { + enter(); try (Arena arena = Arena.ofConfined()) { MH_SEEK.invokeExact(ptr(), NativeCalls.toNative(arena, target), (long) target.length); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seek failed", t); + } finally { + exit(); } } @@ -172,10 +202,13 @@ public void seek(byte[] target) { /// /// @param target direct [ByteBuffer] containing the seek target key public void seek(ByteBuffer target) { + enter(); try { MH_SEEK.invokeExact(ptr(), MemorySegment.ofBuffer(target), (long) target.remaining()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seek failed", t); + } finally { + exit(); } } @@ -183,10 +216,13 @@ public void seek(ByteBuffer target) { /// /// @param target native segment containing the seek target key public void seek(MemorySegment target) { + enter(); try { MH_SEEK.invokeExact(ptr(), target, target.byteSize()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seek failed", t); + } finally { + exit(); } } @@ -194,10 +230,13 @@ public void seek(MemorySegment target) { /// /// @param target the seek target key public void seekForPrev(byte[] target) { + enter(); try (Arena arena = Arena.ofConfined()) { MH_SEEK_FOR_PREV.invokeExact(ptr(), NativeCalls.toNative(arena, target), (long) target.length); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seekForPrev failed", t); + } finally { + exit(); } } @@ -205,10 +244,13 @@ public void seekForPrev(byte[] target) { /// /// @param target direct [ByteBuffer] containing the seek target key public void seekForPrev(ByteBuffer target) { + enter(); try { MH_SEEK_FOR_PREV.invokeExact(ptr(), MemorySegment.ofBuffer(target), (long) target.remaining()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seekForPrev failed", t); + } finally { + exit(); } } @@ -216,28 +258,37 @@ public void seekForPrev(ByteBuffer target) { /// /// @param target native segment containing the seek target key public void seekForPrev(MemorySegment target) { + enter(); try { MH_SEEK_FOR_PREV.invokeExact(ptr(), target, target.byteSize()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("seekForPrev failed", t); + } finally { + exit(); } } /// Moves to the next key. Only call when [#isValid()] is true. public void next() { + enter(); try { MH_NEXT.invokeExact(ptr()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("next failed", t); + } finally { + exit(); } } /// Moves to the previous key. Only call when [#isValid()] is true. public void prev() { + enter(); try { MH_PREV.invokeExact(ptr()); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("prev failed", t); + } finally { + exit(); } } @@ -249,10 +300,13 @@ public void prev() { /// /// @return `true` if the iterator has a valid current position public boolean isValid() { + enter(); try { return NativeCalls.fromByte((byte) MH_VALID.invokeExact(ptr())); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("isValid failed", t); + } finally { + exit(); } } @@ -260,12 +314,15 @@ public boolean isValid() { /// Always call after an iteration loop to detect background errors. /// Throws [RocksDBException] if an error occurred. public void checkError() { + enter(); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_GET_ERROR.invokeExact(ptr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("getError failed", t); + } finally { + exit(); } } @@ -276,6 +333,7 @@ public void checkError() { /// /// @return empty if no error, otherwise the error message public Optional error() { + enter(); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_GET_ERROR.invokeExact(ptr(), err); @@ -283,6 +341,8 @@ public Optional error() { return NativeCalls.toOptionalString(errPtr); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("error failed", t); + } finally { + exit(); } } @@ -290,12 +350,15 @@ public Optional error() { /// The iterator is invalidated by this call regardless of the outcome; a subsequent /// `seek*` call is required to reposition it before further navigation. public void refresh() { + enter(); try (Arena arena = Arena.ofConfined()) { MemorySegment err = NativeCalls.errHolder(arena); MH_REFRESH.invokeExact(ptr(), err); NativeCalls.checkError(err); } catch (Throwable t) { throw NativeCalls.wrapInvokeFailure("refresh failed", t); + } finally { + exit(); } } @@ -321,12 +384,17 @@ public void refresh() { /// @throws NullPointerException if `fn` returns `null` /// @return the result of `fn` public R key(Mapper fn) { - try (Arena arena = Arena.ofConfined()) { - MemorySegment raw = readKey(); - MemorySegment view = raw.reinterpret(lenSegment.get(NativeCalls.SIZE_T, 0), arena, null).asReadOnly(); - R result = fn.map(view); - Objects.requireNonNull(result, "Mapper.map(MemorySegment) must not return null"); - return result; + enter(); + try { + try (Arena arena = Arena.ofConfined()) { + MemorySegment raw = readKey(); + MemorySegment view = raw.reinterpret(lenSegment.get(NativeCalls.SIZE_T, 0), arena, null).asReadOnly(); + R result = fn.map(view); + Objects.requireNonNull(result, "Mapper.map(MemorySegment) must not return null"); + return result; + } + } finally { + exit(); } } @@ -340,12 +408,17 @@ public R key(Mapper fn) { /// @throws NullPointerException if `fn` returns `null` /// @return the result of `fn` public R value(Mapper fn) { - try (Arena arena = Arena.ofConfined()) { - MemorySegment raw = readValue(); - MemorySegment view = raw.reinterpret(lenSegment.get(NativeCalls.SIZE_T, 0), arena, null).asReadOnly(); - R result = fn.map(view); - Objects.requireNonNull(result, "Mapper.map(MemorySegment) must not return null"); - return result; + enter(); + try { + try (Arena arena = Arena.ofConfined()) { + MemorySegment raw = readValue(); + MemorySegment view = raw.reinterpret(lenSegment.get(NativeCalls.SIZE_T, 0), arena, null).asReadOnly(); + R result = fn.map(view); + Objects.requireNonNull(result, "Mapper.map(MemorySegment) must not return null"); + return result; + } + } finally { + exit(); } } @@ -404,14 +477,19 @@ public Stream> stream(Mapper keyMapper, Mapper value /// @return [CopyResult.Copied] if copied, or [CopyResult.NotEnoughCapacity] if `dst` is too small @CheckReturnValue public CopyResult key(ByteBuffer dst) { - MemorySegment raw = readKey(); - long len = lenSegment.get(NativeCalls.SIZE_T, 0); - if (len > dst.remaining()) { - return new CopyResult.NotEnoughCapacity(len); + enter(); + try { + MemorySegment raw = readKey(); + long len = lenSegment.get(NativeCalls.SIZE_T, 0); + if (len > dst.remaining()) { + return new CopyResult.NotEnoughCapacity(len); + } + MemorySegment.ofBuffer(dst).copyFrom(raw.reinterpret(len)); + dst.position(dst.position() + (int) len); + return CopyResult.Copied.INSTANCE; + } finally { + exit(); } - MemorySegment.ofBuffer(dst).copyFrom(raw.reinterpret(len)); - dst.position(dst.position() + (int) len); - return CopyResult.Copied.INSTANCE; } /// Copies the current value into `dst`. Copies nothing when `dst`'s remaining capacity is @@ -425,14 +503,19 @@ public CopyResult key(ByteBuffer dst) { /// @return [CopyResult.Copied] if copied, or [CopyResult.NotEnoughCapacity] if `dst` is too small @CheckReturnValue public CopyResult value(ByteBuffer dst) { - MemorySegment raw = readValue(); - long len = lenSegment.get(NativeCalls.SIZE_T, 0); - if (len > dst.remaining()) { - return new CopyResult.NotEnoughCapacity(len); + enter(); + try { + MemorySegment raw = readValue(); + long len = lenSegment.get(NativeCalls.SIZE_T, 0); + if (len > dst.remaining()) { + return new CopyResult.NotEnoughCapacity(len); + } + MemorySegment.ofBuffer(dst).copyFrom(raw.reinterpret(len)); + dst.position(dst.position() + (int) len); + return CopyResult.Copied.INSTANCE; + } finally { + exit(); } - MemorySegment.ofBuffer(dst).copyFrom(raw.reinterpret(len)); - dst.position(dst.position() + (int) len); - return CopyResult.Copied.INSTANCE; } // ----------------------------------------------------------------------- @@ -445,8 +528,13 @@ public CopyResult value(ByteBuffer dst) { /// /// @return current key as a newly allocated byte array public byte[] key() { - MemorySegment raw = readKey(); - return NativeCalls.toByteArray(raw, lenSegment.get(NativeCalls.SIZE_T, 0)); + enter(); + try { + MemorySegment raw = readKey(); + return NativeCalls.toByteArray(raw, lenSegment.get(NativeCalls.SIZE_T, 0)); + } finally { + exit(); + } } /// Returns a copy of the current value as a byte array. @@ -455,8 +543,13 @@ public byte[] key() { /// /// @return current value as a newly allocated byte array public byte[] value() { - MemorySegment raw = readValue(); - return NativeCalls.toByteArray(raw, lenSegment.get(NativeCalls.SIZE_T, 0)); + enter(); + try { + MemorySegment raw = readValue(); + return NativeCalls.toByteArray(raw, lenSegment.get(NativeCalls.SIZE_T, 0)); + } finally { + exit(); + } } /// Invokes `MH_KEY`. Returns the raw, unsized key pointer — every caller reinterprets diff --git a/core/src/test/java/io/github/dfa1/rocksdbffm/CloseWaitsForInFlightCallsTest.java b/core/src/test/java/io/github/dfa1/rocksdbffm/CloseWaitsForInFlightCallsTest.java new file mode 100644 index 00000000..e12e8a43 --- /dev/null +++ b/core/src/test/java/io/github/dfa1/rocksdbffm/CloseWaitsForInFlightCallsTest.java @@ -0,0 +1,72 @@ +package io.github.dfa1.rocksdbffm; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.lang.foreign.Arena; +import java.lang.foreign.ValueLayout; +import java.nio.file.Path; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; + +/// A database closed while another thread is inside a call on it waits for that call instead of freeing the +/// database under it. Before, an iterator scan or a `compactRange()` racing a close killed the JVM. +class CloseWaitsForInFlightCallsTest { + + private static final byte[] KEY = "k".getBytes(); + private static final byte[] VALUE = "v".getBytes(); + + @Test + void close_waitsForACallInFlightOnAnotherThread(@TempDir Path dir) throws Exception { + // Given -- a reader parked inside a pinned get, holding a value that points into the database + var inMapper = new CountDownLatch(1); + var releaseMapper = new CountDownLatch(1); + try (var opts = Options.newOptions().setCreateIfMissing(true)) { + var db = RocksDB.openReadWrite(dir, opts); + db.put(KEY, VALUE); + var reader = CompletableFuture.supplyAsync(() -> { + try (var arena = Arena.ofConfined()) { + return db.get(arena.allocateFrom(ValueLayout.JAVA_BYTE, KEY), value -> { + inMapper.countDown(); + await(releaseMapper); + return value.byteSize(); + }); + } + }); + inMapper.await(); + + // When + var close = CompletableFuture.runAsync(db::close); + var closeFinishedEarly = isDoneWithin(close, 300); + releaseMapper.countDown(); + var read = reader.get(10, TimeUnit.SECONDS); + close.get(10, TimeUnit.SECONDS); + + // Then -- the read finished against a live database, and only then did the close go through + assertThat(closeFinishedEarly).isFalse(); + assertThat(read).isEqualTo((long) VALUE.length); + assertThat(catchThrowable(() -> db.get(KEY))).isInstanceOf(IllegalStateException.class); + } + } + + private static boolean isDoneWithin(CompletableFuture future, long millis) { + try { + future.get(millis, TimeUnit.MILLISECONDS); + return true; + } catch (Exception e) { + return false; + } + } + + private static void await(CountDownLatch latch) { + try { + latch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +}