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

Add the write buffer manager to JNI #4

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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
28 changes: 28 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 Expand Up @@ -5518,6 +5532,20 @@ void Java_org_rocksdb_DBOptions_setDbWriteBufferSize(
opt->db_write_buffer_size = static_cast<size_t>(jdb_write_buffer_size);
}

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

/*
* Class: org_rocksdb_DBOptions
* Method: dbWriteBufferSize
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;
}
18 changes: 18 additions & 0 deletions java/src/main/java/org/rocksdb/DBOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public DBOptions(DBOptions other) {
this.numShardBits_ = other.numShardBits_;
this.rateLimiter_ = other.rateLimiter_;
this.rowCache_ = other.rowCache_;
this.writeBufferManager_ = other.writeBufferManager_;
}

/**
Expand Down Expand Up @@ -668,6 +669,20 @@ public DBOptions setDbWriteBufferSize(final long dbWriteBufferSize) {
}

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

@Override
public WriteBufferManager writeBufferManager() {
assert(isOwningHandle());
return this.writeBufferManager_;
}

@Override
public long dbWriteBufferSize() {
assert(isOwningHandle());
return dbWriteBufferSize(nativeHandle_);
Expand Down Expand Up @@ -1087,6 +1102,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 Expand Up @@ -1158,4 +1175,5 @@ private native void setAvoidFlushDuringShutdown(final long handle,
private int numShardBits_;
private RateLimiter rateLimiter_;
private Cache rowCache_;
private WriteBufferManager writeBufferManager_;
}
22 changes: 22 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,28 @@ 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);

/**
* Reference to {@link WriteBufferManager} used by it. <br>
*
* Default: null (Disabled)
*
* @return a reference to WriteBufferManager
*/
WriteBufferManager writeBufferManager();

/**
* Amount of data to build up in memtables across all column
* families before writing to disk.
Expand Down
18 changes: 18 additions & 0 deletions java/src/main/java/org/rocksdb/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public Options(Options other) {
this.compactionOptionsFIFO_ = other.compactionOptionsFIFO_;
this.compressionOptions_ = other.compressionOptions_;
this.rowCache_ = other.rowCache_;
this.writeBufferManager_ = other.writeBufferManager_;
}

@Override
Expand Down Expand Up @@ -724,6 +725,20 @@ public Options setDbWriteBufferSize(final long dbWriteBufferSize) {
}

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

@Override
public WriteBufferManager writeBufferManager() {
assert(isOwningHandle());
return this.writeBufferManager_;
}

@Override
public long dbWriteBufferSize() {
assert(isOwningHandle());
return dbWriteBufferSize(nativeHandle_);
Expand Down Expand Up @@ -1690,6 +1705,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 Expand Up @@ -1909,4 +1926,5 @@ private native void setForceConsistencyChecks(final long handle,
private CompactionOptionsFIFO compactionOptionsFIFO_;
private CompressionOptions compressionOptions_;
private Cache rowCache_;
private WriteBufferManager writeBufferManager_;
}
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);
}
20 changes: 20 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,26 @@ public void dbWriteBufferSize() {
}
}

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

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

@Test
public void accessHintOnCompactionStart() {
try(final DBOptions opt = new DBOptions()) {
Expand Down
20 changes: 20 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,26 @@ 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);
assertThat(opt.writeBufferManager()).isEqualTo(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);
assertThat(opt.writeBufferManager()).isEqualTo(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