Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance bug in Java API get() functions when key is NotFound #13049

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 21 additions & 2 deletions java/jmh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ These are micro-benchmarks for RocksJava functionality, using [JMH (Java Microbe

**Note**: This uses a specific build of RocksDB that is set in the `<version>` element of the `dependencies` section of the `pom.xml` file. If you are testing local changes you should build and install a SNAPSHOT version of rocksdbjni, and update the `pom.xml` of rocksdbjni-jmh file to test with this.

For instance, this is how to install the OSX jar you just built for 8.11.0
For instance, this is how to install the OSX jar you just built for 9.8.0

```bash
$ mvn install:install-file -Dfile=./java/target/rocksdbjni-8.11.0-SNAPSHOT-osx.jar -DgroupId=org.rocksdb -DartifactId=rocksdbjni -Dversion=8.11.0-SNAPSHOT -Dpackaging=jar
$ mvn install:install-file -Dfile=./java/target/rocksdbjni-9.8.0-osx.jar -DgroupId=org.rocksdb -DartifactId=rocksdbjni -Dversion=9.8.0-SNAPSHOT -Dpackaging=jar
```

```bash
Expand All @@ -22,3 +22,22 @@ $ java -jar target/rocksdbjni-jmh-1.0-SNAPSHOT-benchmarks.jar
```

NOTE: you can append `-help` to the command above to see all of the JMH runtime options.

### Performance bug in Java `get()`

See this [issue](https://github.com/facebook/rocksdb/issues/13023)

#### Before fix (single)

Benchmark (columnFamilyTestType) (keyCount) (keySize) (nthMissingKey) (valueSize) Mode Cnt Score Error Units
GetNotFoundBenchmarks.getNotFoundEven no_column_family 100000 12 2 16 thrpt 15 289245.405 ± 1727.615 ops/s
GetNotFoundBenchmarks.getNotFoundOdd no_column_family 100000 12 2 16 thrpt 15 717300.753 ± 7040.178 ops/s

#### After fix (single)

Benchmark (columnFamilyTestType) (keyCount) (keySize) (nthMissingKey) (valueSize) Mode Cnt Score Error Units
GetNotFoundBenchmarks.getNotFoundEven no_column_family 100000 12 2 16 thrpt 15 856149.231 ± 10159.858 ops/s
GetNotFoundBenchmarks.getNotFoundOdd no_column_family 100000 12 2 16 thrpt 15 726021.153 ± 4047.159 ops/s



2 changes: 1 addition & 1 deletion java/jmh/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.rocksdb</groupId>
<artifactId>rocksdbjni</artifactId>
<version>9.0.0</version>
<version>9.8.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
191 changes: 191 additions & 0 deletions java/jmh/src/main/java/org/rocksdb/jmh/GetNotFoundBenchmarks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/**
* Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
* This source code is licensed under both the GPLv2 (found in the
* COPYING file in the root directory) and Apache 2.0 License
* (found in the LICENSE.Apache file in the root directory).
*/
package org.rocksdb.jmh;

import static org.rocksdb.util.KVUtils.ba;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.rocksdb.*;
import org.rocksdb.util.FileUtils;

@State(Scope.Benchmark)
public class GetNotFoundBenchmarks {
@Param({"no_column_family", "20_column_families"}) String columnFamilyTestType;

@Param({"1000", "100000"}) int keyCount;
private final static int extraKeyCount =
16; // some slack to deal with odd/even tests rounding up key values

@Param({"12"}) int keySize;

@Param({"16"}) int valueSize;

/**
* Don't create every n-th key
* Usually, just use "2" for making the half-present array
* 0 means no missing keys
*/
@Param({"2", "16", "0"}) int nthMissingKey;

Path dbDir;
DBOptions options;
ReadOptions readOptions;
int cfs = 0; // number of column families
private AtomicInteger cfHandlesIdx;
ColumnFamilyHandle[] cfHandles;
RocksDB db;
private final AtomicInteger keyIndex = new AtomicInteger();
private ByteBuffer keyBuf;
private ByteBuffer valueBuf;
private byte[] keyArr;
private byte[] valueArr;

@Setup(Level.Trial)
public void setup() throws IOException, RocksDBException {
RocksDB.loadLibrary();

dbDir = Files.createTempDirectory("rocksjava-get-benchmarks");

options = new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true);
readOptions = new ReadOptions();

final List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY));

if ("20_column_families".equals(columnFamilyTestType)) {
cfs = 20;
}

if (cfs > 0) {
cfHandlesIdx = new AtomicInteger(1);
for (int i = 1; i <= cfs; i++) {
cfDescriptors.add(new ColumnFamilyDescriptor(ba("cf" + i)));
}
}

final List<ColumnFamilyHandle> cfHandlesList = new ArrayList<>(cfDescriptors.size());
db = RocksDB.open(options, dbDir.toAbsolutePath().toString(), cfDescriptors, cfHandlesList);
cfHandles = cfHandlesList.toArray(new ColumnFamilyHandle[0]);

// store initial data for retrieving via get
keyArr = new byte[keySize];
valueArr = new byte[valueSize];
Arrays.fill(keyArr, (byte) 0x30);
Arrays.fill(valueArr, (byte) 0x30);
for (int i = 0; i <= cfs; i++) {
for (int j = 0; j < keyCount; j++) {
if (nthMissingKey <= 0 || j % nthMissingKey != 0) {
final byte[] keyPrefix = ba("key" + j);
final byte[] valuePrefix = ba("value" + j);
System.arraycopy(keyPrefix, 0, keyArr, 0, keyPrefix.length);
System.arraycopy(valuePrefix, 0, valueArr, 0, valuePrefix.length);
db.put(cfHandles[i], keyArr, valueArr);
}
}
}

try (final FlushOptions flushOptions = new FlushOptions().setWaitForFlush(true)) {
db.flush(flushOptions);
}

keyBuf = ByteBuffer.allocateDirect(keySize);
valueBuf = ByteBuffer.allocateDirect(valueSize);
Arrays.fill(keyArr, (byte) 0x30);
Arrays.fill(valueArr, (byte) 0x30);
keyBuf.put(keyArr);
keyBuf.flip();
valueBuf.put(valueArr);
valueBuf.flip();
}

@TearDown(Level.Trial)
public void cleanup() throws IOException {
for (final ColumnFamilyHandle cfHandle : cfHandles) {
cfHandle.close();
}
db.close();
options.close();
readOptions.close();
FileUtils.delete(dbDir);
}

private ColumnFamilyHandle getColumnFamily() {
if (cfs == 0) {
return cfHandles[0];
} else if (cfs == 1) {
return cfHandles[1];
} else {
int idx = cfHandlesIdx.getAndIncrement();
if (idx > cfs) {
cfHandlesIdx.set(1); // doesn't ensure a perfect distribution, but it's ok
idx = 0;
}
return cfHandles[idx];
}
}

/**
* Takes the next position in the index.
*/
private int next(final int increment) {
int idx;
int nextIdx;
while (true) {
idx = keyIndex.get();
nextIdx = idx + increment;
if (nextIdx >= keyCount) {
nextIdx = nextIdx % keyCount;
}
if (keyIndex.compareAndSet(idx, nextIdx)) {
break;
}
}

return idx;
}

private int next() {
return next(1);
}

// String -> byte[]
private byte[] getKeyArr(final int keyOffset, final int increment) {
final int MAX_LEN = 9; // key100000
final int keyIdx = next(increment);
final byte[] keyPrefix = ba("key" + (keyIdx + keyOffset));
System.arraycopy(keyPrefix, 0, keyArr, 0, keyPrefix.length);
Arrays.fill(keyArr, keyPrefix.length, MAX_LEN, (byte) 0x30);
return keyArr;
}

/**
* Get even values, these should not be present
* @throws RocksDBException
*/
@Benchmark
public void getNotFoundEven(Blackhole blackhole) throws RocksDBException {
blackhole.consume(db.get(getColumnFamily(), getKeyArr(0, 2)));
}

/**
* Get odd values 1, 3, 5, these should be present
* @throws RocksDBException
*/
@Benchmark
public void getNotFoundOdd(Blackhole blackhole) throws RocksDBException {
blackhole.consume(db.get(getColumnFamily(), getKeyArr(1, 2)));
}
}
Loading
Loading