Skip to content

Commit

Permalink
Plumb WriteBufferManager through JNI (#4492)
Browse files Browse the repository at this point in the history
Summary:
Allow rocks java to explicitly create WriteBufferManager by plumbing it to the native code through JNI.
Pull Request resolved: facebook/rocksdb#4492

Differential Revision: D10428506

Pulled By: sagar0

fbshipit-source-id: cd9dd8c2ef745a0303416b44e2080547bdcca1fd
  • Loading branch information
jbhati authored and StephanEwen committed Nov 21, 2019
1 parent 810b42a commit fa60a7c
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 1 deletion.
3 changes: 3 additions & 0 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(JNI_NATIVE_SOURCES
rocksjni/writebatchhandlerjnicallback.cc
rocksjni/write_batch_test.cc
rocksjni/write_batch_with_index.cc
rocksjni/write_buffer_manager.cc
)

set(NATIVE_JAVA_CLASSES
Expand Down Expand Up @@ -145,6 +146,7 @@ set(NATIVE_JAVA_CLASSES
org.rocksdb.SnapshotTest
org.rocksdb.WriteBatchTest
org.rocksdb.WriteBatchTestInternalHelper
org.rocksdb.WriteBufferManager
)

include(FindJava)
Expand Down Expand Up @@ -283,6 +285,7 @@ add_jar(
src/main/java/org/rocksdb/WriteBatch.java
src/main/java/org/rocksdb/WriteBatchWithIndex.java
src/main/java/org/rocksdb/WriteOptions.java
src/main/java/org/rocksdb/WriteBufferManager.java
src/main/java/org/rocksdb/util/BytewiseComparator.java
src/main/java/org/rocksdb/util/DirectBytewiseComparator.java
src/main/java/org/rocksdb/util/Environment.java
Expand Down
1 change: 1 addition & 0 deletions java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractCompactionFilter\
org.rocksdb.WriteBatch.Handler\
org.rocksdb.WriteOptions\
org.rocksdb.WriteBatchWithIndex\
org.rocksdb.WriteBufferManager\
org.rocksdb.WBWIRocksIterator

NATIVE_JAVA_TEST_CLASSES = org.rocksdb.RocksDBExceptionTest\
Expand Down
14 changes: 14 additions & 0 deletions java/rocksjni/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ void Java_org_rocksdb_Options_setWriteBufferSize(JNIEnv* env, jobject /*jobj*/,
}
}

/*
* Class: org_rocksdb_Options
* Method: setWriteBufferManager
* Signature: (JJ)V
*/
void Java_org_rocksdb_Options_setWriteBufferManager(JNIEnv* /*env*/, jobject /*jobj*/,
jlong joptions_handle,
jlong jwrite_buffer_manager_handle) {
auto* write_buffer_manager =
reinterpret_cast<std::shared_ptr<rocksdb::WriteBufferManager> *>(jwrite_buffer_manager_handle);
reinterpret_cast<rocksdb::Options*>(joptions_handle)->write_buffer_manager =
*write_buffer_manager;
}

/*
* Class: org_rocksdb_Options
* Method: writeBufferSize
Expand Down
38 changes: 38 additions & 0 deletions java/rocksjni/write_buffer_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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).

#include <jni.h>

#include "include/org_rocksdb_WriteBufferManager.h"

#include "rocksdb/cache.h"
#include "rocksdb/write_buffer_manager.h"

/*
* Class: org_rocksdb_WriteBufferManager
* Method: newWriteBufferManager
* Signature: (JJ)J
*/
jlong Java_org_rocksdb_WriteBufferManager_newWriteBufferManager(
JNIEnv* /*env*/, jclass /*jclazz*/, jlong jbuffer_size, jlong jcache_handle) {
auto* cache_ptr =
reinterpret_cast<std::shared_ptr<rocksdb::Cache> *>(jcache_handle);
auto* write_buffer_manager = new std::shared_ptr<rocksdb::WriteBufferManager>(
std::make_shared<rocksdb::WriteBufferManager>(jbuffer_size, *cache_ptr));
return reinterpret_cast<jlong>(write_buffer_manager);
}

/*
* Class: org_rocksdb_WriteBufferManager
* Method: disposeInternal
* Signature: (J)V
*/
void Java_org_rocksdb_WriteBufferManager_disposeInternal(
JNIEnv* /*env*/, jobject /*jobj*/, jlong jhandle) {
auto* write_buffer_manager =
reinterpret_cast<std::shared_ptr<rocksdb::WriteBufferManager> *>(jhandle);
assert(write_buffer_manager != nullptr);
delete write_buffer_manager;
}
9 changes: 9 additions & 0 deletions java/src/main/java/org/rocksdb/DBOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,13 @@ public DBOptions setDbWriteBufferSize(final long dbWriteBufferSize) {
return this;
}

@Override
public DBOptions setWriteBufferManager(final WriteBufferManager writeBufferManager) {
assert(isOwningHandle());
setWriteBufferManager(nativeHandle_, writeBufferManager.nativeHandle_);
return this;
}

@Override
public long dbWriteBufferSize() {
assert(isOwningHandle());
Expand Down Expand Up @@ -1087,6 +1094,8 @@ private native void setAdviseRandomOnOpen(
private native boolean adviseRandomOnOpen(long handle);
private native void setDbWriteBufferSize(final long handle,
final long dbWriteBufferSize);
private native void setWriteBufferManager(final long dbOptionsHandle,
final long writeBufferManagerHandle);
private native long dbWriteBufferSize(final long handle);
private native void setAccessHintOnCompactionStart(final long handle,
final byte accessHintOnCompactionStart);
Expand Down
13 changes: 13 additions & 0 deletions java/src/main/java/org/rocksdb/DBOptionsInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,19 @@ public interface DBOptionsInterface<T extends DBOptionsInterface> {
*/
T setDbWriteBufferSize(long dbWriteBufferSize);

/**
* Use passed {@link WriteBufferManager} to control memory usage across
* multiple column families and/or DB instances.
*
* Check <a href="https://github.com/facebook/rocksdb/wiki/Write-Buffer-Manager">
* https://github.com/facebook/rocksdb/wiki/Write-Buffer-Manager</a>
* for more details on when to use it
*
* @param writeBufferManager The WriteBufferManager to use
* @return the reference of the current options.
*/
T setWriteBufferManager(final WriteBufferManager writeBufferManager);

/**
* Amount of data to build up in memtables across all column
* families before writing to disk.
Expand Down
9 changes: 9 additions & 0 deletions java/src/main/java/org/rocksdb/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,13 @@ public Options setDbWriteBufferSize(final long dbWriteBufferSize) {
return this;
}

@Override
public Options setWriteBufferManager(final WriteBufferManager writeBufferManager) {
assert(isOwningHandle());
setWriteBufferManager(nativeHandle_, writeBufferManager.nativeHandle_);
return this;
}

@Override
public long dbWriteBufferSize() {
assert(isOwningHandle());
Expand Down Expand Up @@ -1690,6 +1697,8 @@ private native void setAdviseRandomOnOpen(
private native boolean adviseRandomOnOpen(long handle);
private native void setDbWriteBufferSize(final long handle,
final long dbWriteBufferSize);
private native void setWriteBufferManager(final long handle,
final long writeBufferManagerHandle);
private native long dbWriteBufferSize(final long handle);
private native void setAccessHintOnCompactionStart(final long handle,
final byte accessHintOnCompactionStart);
Expand Down
30 changes: 30 additions & 0 deletions java/src/main/java/org/rocksdb/WriteBufferManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.rocksdb;

import org.rocksdb.Cache;

/**
* Java wrapper over native write_buffer_manager class
*/
public class WriteBufferManager extends RocksObject {
static {
RocksDB.loadLibrary();
}

/**
* Construct a new instance of WriteBufferManager.
*
* Check <a href="https://github.com/facebook/rocksdb/wiki/Write-Buffer-Manager">
* https://github.com/facebook/rocksdb/wiki/Write-Buffer-Manager</a>
* for more details on when to use it
*
* @param bufferSizeBytes buffer size(in bytes) to use for native write_buffer_manager
* @param cache cache whose memory should be bounded by this write buffer manager
*/
public WriteBufferManager(final long bufferSizeBytes, final Cache cache){
super(newWriteBufferManager(bufferSizeBytes, cache.nativeHandle_));
}

private native static long newWriteBufferManager(final long bufferSizeBytes, final long cacheHandle);
@Override
protected native void disposeInternal(final long handle);
}
18 changes: 18 additions & 0 deletions java/src/test/java/org/rocksdb/DBOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,24 @@ public void dbWriteBufferSize() {
}
}

@Test
public void setWriteBufferManager() throws RocksDBException {
try (final Options opt = new Options();
final Cache cache = new LRUCache(1 * 1024 * 1024);
final WriteBufferManager writeBufferManager = new WriteBufferManager(2000l, cache)) {
opt.setWriteBufferManager(writeBufferManager);
}
}

@Test
public void setWriteBufferManagerWithZeroBufferSize() throws RocksDBException {
try (final Options opt = new Options();
final Cache cache = new LRUCache(1 * 1024 * 1024);
final WriteBufferManager writeBufferManager = new WriteBufferManager(0l, cache)) {
opt.setWriteBufferManager(writeBufferManager);
}
}

@Test
public void accessHintOnCompactionStart() {
try(final DBOptions opt = new DBOptions()) {
Expand Down
18 changes: 18 additions & 0 deletions java/src/test/java/org/rocksdb/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,24 @@ public void dbWriteBufferSize() {
}
}

@Test
public void setWriteBufferManager() throws RocksDBException {
try (final Options opt = new Options();
final Cache cache = new LRUCache(1 * 1024 * 1024);
final WriteBufferManager writeBufferManager = new WriteBufferManager(2000l, cache)) {
opt.setWriteBufferManager(writeBufferManager);
}
}

@Test
public void setWriteBufferManagerWithZeroBufferSize() throws RocksDBException {
try (final Options opt = new Options();
final Cache cache = new LRUCache(1 * 1024 * 1024);
final WriteBufferManager writeBufferManager = new WriteBufferManager(0l, cache)) {
opt.setWriteBufferManager(writeBufferManager);
}
}

@Test
public void accessHintOnCompactionStart() {
try (final Options opt = new Options()) {
Expand Down
3 changes: 2 additions & 1 deletion src.mk
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,5 @@ JNI_NATIVE_SOURCES = \
java/rocksjni/write_batch.cc \
java/rocksjni/writebatchhandlerjnicallback.cc \
java/rocksjni/write_batch_test.cc \
java/rocksjni/write_batch_with_index.cc
java/rocksjni/write_batch_with_index.cc \
java/rocksjni/write_buffer_manager.cc

0 comments on commit fa60a7c

Please sign in to comment.