Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
57 changes: 57 additions & 0 deletions core/src/main/java/io/github/dfa1/rocksdbffm/NativeDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,6 +29,61 @@ abstract class NativeDB extends NativeObjectWithChildren {
private final AtomicReference<SequencedMap<ColumnFamilyName, ColumnFamilyHandle>> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}
}
}
Loading
Loading