diff --git a/java/rocksjni/write_buffer_manager.cc b/java/rocksjni/write_buffer_manager.cc index a52daed7d1..e5c1e23f3e 100644 --- a/java/rocksjni/write_buffer_manager.cc +++ b/java/rocksjni/write_buffer_manager.cc @@ -16,14 +16,15 @@ * Signature: (JJ)J */ jlong Java_org_rocksdb_WriteBufferManager_newWriteBufferManager( - JNIEnv* /*env*/, jclass /*jclazz*/, jlong jbuffer_size, jlong jcache_handle) { + JNIEnv* /*env*/, jclass /*jclazz*/, jlong jbuffer_size, jlong jcache_handle, + jboolean allow_stall) { auto* cache_ptr = reinterpret_cast*>( jcache_handle); auto* write_buffer_manager = new std::shared_ptr( - std::make_shared(jbuffer_size, - *cache_ptr)); + std::make_shared( + jbuffer_size, *cache_ptr, allow_stall)); return reinterpret_cast(write_buffer_manager); } diff --git a/java/src/main/java/org/rocksdb/WriteBufferManager.java b/java/src/main/java/org/rocksdb/WriteBufferManager.java index b244aa9522..8ec9639586 100644 --- a/java/src/main/java/org/rocksdb/WriteBufferManager.java +++ b/java/src/main/java/org/rocksdb/WriteBufferManager.java @@ -22,12 +22,29 @@ public class WriteBufferManager extends RocksObject { * * @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 + * @param allowStall if set true, it will enable stalling of writes when memory_usage() exceeds + * buffer_size. + * It will wait for flush to complete and memory usage to drop down. */ - public WriteBufferManager(final long bufferSizeBytes, final Cache cache){ - super(newWriteBufferManager(bufferSizeBytes, cache.nativeHandle_)); + public WriteBufferManager( + final long bufferSizeBytes, final Cache cache, final boolean allowStall) { + super(newWriteBufferManager(bufferSizeBytes, cache.nativeHandle_, allowStall)); + this.allowStall_ = allowStall; } - private native static long newWriteBufferManager(final long bufferSizeBytes, final long cacheHandle); + public WriteBufferManager(final long bufferSizeBytes, final Cache cache){ + this(bufferSizeBytes, cache, false); + } + + public boolean allowStall() { + return allowStall_; + } + + private native static long newWriteBufferManager( + final long bufferSizeBytes, final long cacheHandle, final boolean allowStall); + @Override protected native void disposeInternal(final long handle); + + private boolean allowStall_; } diff --git a/java/src/test/java/org/rocksdb/OptionsTest.java b/java/src/test/java/org/rocksdb/OptionsTest.java index 5d732adc80..1bb0550425 100644 --- a/java/src/test/java/org/rocksdb/OptionsTest.java +++ b/java/src/test/java/org/rocksdb/OptionsTest.java @@ -686,6 +686,16 @@ public class OptionsTest { } } + @Test + public void setWriteBufferManagerWithAllowStall() throws RocksDBException { + try (final Options opt = new Options(); final Cache cache = new LRUCache(1 * 1024 * 1024); + final WriteBufferManager writeBufferManager = new WriteBufferManager(2000l, cache, true)) { + opt.setWriteBufferManager(writeBufferManager); + assertThat(opt.writeBufferManager()).isEqualTo(writeBufferManager); + assertThat(opt.writeBufferManager().allowStall()).isEqualTo(true); + } + } + @Test public void accessHintOnCompactionStart() { try (final Options opt = new Options()) {