Skip to content

Commit

Permalink
Add unordered write option rocksjava (facebook#5839)
Browse files Browse the repository at this point in the history
Summary:
Add unordered_write option api and related ut to rocksjava
Pull Request resolved: facebook#5839

Differential Revision: D17604446

Pulled By: maysamyabandeh

fbshipit-source-id: c6b07e85ca9d5e3a92973ddb6ab2bc079e53c9c1
  • Loading branch information
liam-thunder authored and facebook-github-bot committed Sep 27, 2019
1 parent 9b92786 commit 1afd061
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
44 changes: 44 additions & 0 deletions java/rocksjni/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,27 @@ jboolean Java_org_rocksdb_Options_enablePipelinedWrite(
return static_cast<jboolean>(opt->enable_pipelined_write);
}

/*
* Class: org_rocksdb_Options
* Method: setUnorderedWrite
* Signature: (JZ)V
*/
void Java_org_rocksdb_Options_setUnorderedWrite(
JNIEnv*, jobject, jlong jhandle, jboolean unordered_write) {
reinterpret_cast<rocksdb::DBOptions*>(jhandle)
->unordered_write = static_cast<bool>(unordered_write);
}

/*
* Class: org_rocksdb_Options
* Method: unorderedWrite
* Signature: (J)Z
*/
jboolean Java_org_rocksdb_Options_unorderedWrite(
JNIEnv*, jobject, jlong jhandle) {
return reinterpret_cast<rocksdb::Options*>(jhandle)->unordered_write;
}

/*
* Class: org_rocksdb_Options
* Method: setAllowConcurrentMemtableWrite
Expand Down Expand Up @@ -5717,6 +5738,29 @@ jboolean Java_org_rocksdb_DBOptions_enablePipelinedWrite(
return static_cast<jboolean>(opt->enable_pipelined_write);
}

/*
* Class: org_rocksdb_DBOptions
* Method: setUnorderedWrite
* Signature: (JZ)V
*/
void Java_org_rocksdb_DBOptions_setUnorderedWrite(
JNIEnv*, jobject, jlong jhandle, jboolean junordered_write) {
auto* opt = reinterpret_cast<rocksdb::DBOptions*>(jhandle);
opt->unordered_write = junordered_write == JNI_TRUE;
}

/*
* Class: org_rocksdb_DBOptions
* Method: unorderedWrite
* Signature: (J)Z
*/
jboolean Java_org_rocksdb_DBOptions_unorderedWrite(
JNIEnv*, jobject, jlong jhandle) {
auto* opt = reinterpret_cast<rocksdb::DBOptions*>(jhandle);
return static_cast<jboolean>(opt->unordered_write);
}


/*
* Class: org_rocksdb_DBOptions
* Method: setEnableThreadTracking
Expand Down
15 changes: 15 additions & 0 deletions java/src/main/java/org/rocksdb/DBOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,18 @@ public boolean enablePipelinedWrite() {
return enablePipelinedWrite(nativeHandle_);
}

@Override
public DBOptions setUnorderedWrite(final boolean unorderedWrite) {
setUnorderedWrite(nativeHandle_, unorderedWrite);
return this;
}

@Override
public boolean unorderedWrite() {
return unorderedWrite(nativeHandle_);
}


@Override
public DBOptions setAllowConcurrentMemtableWrite(
final boolean allowConcurrentMemtableWrite) {
Expand Down Expand Up @@ -1266,6 +1278,9 @@ private native void setEnableThreadTracking(long handle,
private native void setEnablePipelinedWrite(final long handle,
final boolean enablePipelinedWrite);
private native boolean enablePipelinedWrite(final long handle);
private native void setUnorderedWrite(final long handle,
final boolean unorderedWrite);
private native boolean unorderedWrite(final long handle);
private native void setAllowConcurrentMemtableWrite(long handle,
boolean allowConcurrentMemtableWrite);
private native boolean allowConcurrentMemtableWrite(long handle);
Expand Down
38 changes: 38 additions & 0 deletions java/src/main/java/org/rocksdb/DBOptionsInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,44 @@ T setNewTableReaderForCompactionInputs(
*/
boolean enablePipelinedWrite();

/**
* Setting {@link #unorderedWrite()} to true trades higher write throughput with
* relaxing the immutability guarantee of snapshots. This violates the
* repeatability one expects from ::Get from a snapshot, as well as
* ::MultiGet and Iterator's consistent-point-in-time view property.
* If the application cannot tolerate the relaxed guarantees, it can implement
* its own mechanisms to work around that and yet benefit from the higher
* throughput. Using TransactionDB with WRITE_PREPARED write policy and
* {@link #twoWriteQueues()} true is one way to achieve immutable snapshots despite
* unordered_write.
*
* By default, i.e., when it is false, rocksdb does not advance the sequence
* number for new snapshots unless all the writes with lower sequence numbers
* are already finished. This provides the immutability that we except from
* snapshots. Moreover, since Iterator and MultiGet internally depend on
* snapshots, the snapshot immutability results into Iterator and MultiGet
* offering consistent-point-in-time view. If set to true, although
* Read-Your-Own-Write property is still provided, the snapshot immutability
* property is relaxed: the writes issued after the snapshot is obtained (with
* larger sequence numbers) will be still not visible to the reads from that
* snapshot, however, there still might be pending writes (with lower sequence
* number) that will change the state visible to the snapshot after they are
* landed to the memtable.
*
* @param unorderedWrite true to enabled unordered write
*
* @return the reference to the current options.
*/
T setUnorderedWrite(final boolean unorderedWrite);

/**
* Returns true if unordered write are enabled.
* See {@link #setUnorderedWrite(boolean)}.
*
* @return true if unordered write are enabled, false otherwise.
*/
boolean unorderedWrite();

/**
* If true, allow multi-writers to update mem tables in parallel.
* Only some memtable factorys support concurrent writes; currently it
Expand Down
14 changes: 14 additions & 0 deletions java/src/main/java/org/rocksdb/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,17 @@ public boolean enablePipelinedWrite() {
return enablePipelinedWrite(nativeHandle_);
}

@Override
public Options setUnorderedWrite(final boolean unorderedWrite) {
setUnorderedWrite(nativeHandle_, unorderedWrite);
return this;
}

@Override
public boolean unorderedWrite() {
return unorderedWrite(nativeHandle_);
}

@Override
public Options setAllowConcurrentMemtableWrite(
final boolean allowConcurrentMemtableWrite) {
Expand Down Expand Up @@ -1886,6 +1897,9 @@ private native void setEnableThreadTracking(long handle,
private native void setEnablePipelinedWrite(final long handle,
final boolean pipelinedWrite);
private native boolean enablePipelinedWrite(final long handle);
private native void setUnorderedWrite(final long handle,
final boolean unorderedWrite);
private native boolean unorderedWrite(final long handle);
private native void setAllowConcurrentMemtableWrite(long handle,
boolean allowConcurrentMemtableWrite);
private native boolean allowConcurrentMemtableWrite(long handle);
Expand Down
9 changes: 9 additions & 0 deletions java/src/test/java/org/rocksdb/DBOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,15 @@ public void enablePipelinedWrite() {
}
}

@Test
public void unordredWrite() {
try(final DBOptions opt = new DBOptions()) {
assertThat(opt.unorderedWrite()).isFalse();
opt.setUnorderedWrite(true);
assertThat(opt.unorderedWrite()).isTrue();
}
}

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

@Test
public void unordredWrite() {
try(final Options opt = new Options()) {
assertThat(opt.unorderedWrite()).isFalse();
opt.setUnorderedWrite(true);
assertThat(opt.unorderedWrite()).isTrue();
}
}

@Test
public void allowConcurrentMemtableWrite() {
try (final Options opt = new Options()) {
Expand Down

0 comments on commit 1afd061

Please sign in to comment.