mirror of https://github.com/facebook/rocksdb.git
Expose OccValidationPolicy to Java API
Allow users of RocksDB JNI to specify the OCC Validation Policy when using OptimisticTransactionOptionsDB.
This commit is contained in:
parent
8897bf2d04
commit
15fff9e4fb
|
@ -3115,6 +3115,10 @@ void StressTest::Open(SharedState* shared, bool reopen) {
|
|||
} else {
|
||||
options_.two_write_queues = FLAGS_two_write_queues;
|
||||
}
|
||||
if (FLAGS_use_optimistic_txn) {
|
||||
assert(optimistic_txn_db_->GetValidatePolicy() ==
|
||||
static_cast<OccValidationPolicy>(FLAGS_occ_validation_policy));
|
||||
}
|
||||
txn_db_options.wp_snapshot_cache_bits =
|
||||
static_cast<size_t>(FLAGS_wp_snapshot_cache_bits);
|
||||
txn_db_options.wp_commit_cache_bits =
|
||||
|
|
|
@ -118,6 +118,9 @@ class OptimisticTransactionDB : public StackableDB {
|
|||
OptimisticTransactionOptions(),
|
||||
Transaction* old_txn = nullptr) = 0;
|
||||
|
||||
// Get the OCC Validation Policy specified for this instance.
|
||||
virtual OccValidationPolicy GetValidatePolicy() const = 0;
|
||||
|
||||
OptimisticTransactionDB(const OptimisticTransactionDB&) = delete;
|
||||
void operator=(const OptimisticTransactionDB&) = delete;
|
||||
|
||||
|
|
|
@ -56,6 +56,7 @@ set(JNI_NATIVE_SOURCES
|
|||
rocksjni/merge_operator.cc
|
||||
rocksjni/native_comparator_wrapper_test.cc
|
||||
rocksjni/optimistic_transaction_db.cc
|
||||
rocksjni/optimistic_transaction_db_options.cc
|
||||
rocksjni/optimistic_transaction_options.cc
|
||||
rocksjni/options.cc
|
||||
rocksjni/options_util.cc
|
||||
|
@ -206,9 +207,11 @@ set(JAVA_MAIN_CLASSES
|
|||
src/main/java/org/rocksdb/MutableOptionValue.java
|
||||
src/main/java/org/rocksdb/NativeComparatorWrapper.java
|
||||
src/main/java/org/rocksdb/NativeLibraryLoader.java
|
||||
src/main/java/org/rocksdb/OccValidationPolicy.java
|
||||
src/main/java/org/rocksdb/OperationStage.java
|
||||
src/main/java/org/rocksdb/OperationType.java
|
||||
src/main/java/org/rocksdb/OptimisticTransactionDB.java
|
||||
src/main/java/org/rocksdb/OptimisticTransactionDBOptions.java
|
||||
src/main/java/org/rocksdb/OptimisticTransactionOptions.java
|
||||
src/main/java/org/rocksdb/OptionString.java
|
||||
src/main/java/org/rocksdb/Options.java
|
||||
|
@ -366,6 +369,7 @@ set(JAVA_TEST_CLASSES
|
|||
src/test/java/org/rocksdb/MutableOptionsGetSetTest.java
|
||||
src/test/java/org/rocksdb/NativeLibraryLoaderTest.java
|
||||
src/test/java/org/rocksdb/OptimisticTransactionDBTest.java
|
||||
src/test/java/org/rocksdb/OptimisticTransactionOptionsDBTest.java
|
||||
src/test/java/org/rocksdb/OptimisticTransactionOptionsTest.java
|
||||
src/test/java/org/rocksdb/OptimisticTransactionTest.java
|
||||
src/test/java/org/rocksdb/OptionsTest.java
|
||||
|
@ -485,6 +489,7 @@ set(JAVA_TEST_RUNNING_CLASSES
|
|||
org.rocksdb.NativeComparatorWrapperTest
|
||||
org.rocksdb.NativeLibraryLoaderTest
|
||||
org.rocksdb.OptimisticTransactionDBTest
|
||||
org.rocksdb.OptimisticTransactionOptionsDBTest
|
||||
org.rocksdb.OptimisticTransactionOptionsTest
|
||||
org.rocksdb.OptimisticTransactionTest
|
||||
org.rocksdb.OptionsTest
|
||||
|
|
|
@ -165,6 +165,7 @@ JAVA_TESTS = \
|
|||
org.rocksdb.NativeLibraryLoaderTest\
|
||||
org.rocksdb.OptimisticTransactionTest\
|
||||
org.rocksdb.OptimisticTransactionDBTest\
|
||||
org.rocksdb.OptimisticTransactionOptionsDBTest\
|
||||
org.rocksdb.OptimisticTransactionOptionsTest\
|
||||
org.rocksdb.OptionsUtilTest\
|
||||
org.rocksdb.OptionsTest\
|
||||
|
|
|
@ -48,11 +48,12 @@ jlong Java_org_rocksdb_OptimisticTransactionDB_open__JLjava_lang_String_2(
|
|||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDB
|
||||
* Method: open
|
||||
* Signature: (JLjava/lang/String;[[B[J)[J
|
||||
* Signature: (JJLjava/lang/String;[[B[J)[J
|
||||
*/
|
||||
jlongArray
|
||||
Java_org_rocksdb_OptimisticTransactionDB_open__JLjava_lang_String_2_3_3B_3J(
|
||||
JNIEnv* env, jclass, jlong jdb_options_handle, jstring jdb_path,
|
||||
JNIEnv* env, jclass, jlong jdb_options_handle,
|
||||
jlong jdb_optimistic_options_handle, jstring jdb_path,
|
||||
jobjectArray jcolumn_names, jlongArray jcolumn_options_handles) {
|
||||
const char* db_path = env->GetStringUTFChars(jdb_path, nullptr);
|
||||
if (db_path == nullptr) {
|
||||
|
@ -103,13 +104,21 @@ Java_org_rocksdb_OptimisticTransactionDB_open__JLjava_lang_String_2_3_3B_3J(
|
|||
env->ReleaseLongArrayElements(jcolumn_options_handles, jco, JNI_ABORT);
|
||||
}
|
||||
|
||||
auto* db_options =
|
||||
const auto* db_options =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::DBOptions*>(jdb_options_handle);
|
||||
std::vector<ROCKSDB_NAMESPACE::ColumnFamilyHandle*> handles;
|
||||
ROCKSDB_NAMESPACE::Status s;
|
||||
ROCKSDB_NAMESPACE::OptimisticTransactionDB* otdb = nullptr;
|
||||
const ROCKSDB_NAMESPACE::Status s =
|
||||
ROCKSDB_NAMESPACE::OptimisticTransactionDB::Open(
|
||||
*db_options, db_path, column_families, &handles, &otdb);
|
||||
if (jdb_options_handle) {
|
||||
const auto* otdb_options =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions*>(
|
||||
jdb_options_handle);
|
||||
s = ROCKSDB_NAMESPACE::OptimisticTransactionDB::Open(
|
||||
*db_options, *otdb_options, db_path, column_families, &handles, &otdb);
|
||||
} else {
|
||||
s = ROCKSDB_NAMESPACE::OptimisticTransactionDB::Open(
|
||||
*db_options, db_path, column_families, &handles, &otdb);
|
||||
}
|
||||
|
||||
env->ReleaseStringUTFChars(jdb_path, db_path);
|
||||
|
||||
|
@ -257,6 +266,19 @@ jlong Java_org_rocksdb_OptimisticTransactionDB_beginTransaction_1withOld__JJJJ(
|
|||
return GET_CPLUSPLUS_POINTER(txn);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDB
|
||||
* Method: getOccValidationPolicy
|
||||
* Signature: (J)B
|
||||
*/
|
||||
jbyte Java_org_rocksdb_OptimisticTransactionDB_getOccValidationPolicy(
|
||||
JNIEnv*, jclass, jlong jhandle) {
|
||||
const auto* optimistic_txn_db =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDB*>(jhandle);
|
||||
return ROCKSDB_NAMESPACE::OccValidationPolicyJni::toJavaOccValidationPolicy(
|
||||
optimistic_txn_db->GetValidatePolicy());
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDB
|
||||
* Method: getBaseDB
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
// 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).
|
||||
//
|
||||
// This file implements the "bridge" between Java and C++
|
||||
// for ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions.
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "include/org_rocksdb_OptimisticTransactionOptions.h"
|
||||
#include "rocksdb/comparator.h"
|
||||
#include "rocksdb/utilities/optimistic_transaction_db.h"
|
||||
#include "rocksjni/cplusplus_to_java_convert.h"
|
||||
#include "rocksjni/portal.h"
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDBOptions
|
||||
* Method: newOptimisticTransactionDBOptions
|
||||
* Signature: ()J
|
||||
*/
|
||||
jlong Java_org_rocksdb_OptimisticTransactionDBOptions_newOptimisticTransactionDBOptions(
|
||||
JNIEnv* /*env*/, jclass /*jcls*/) {
|
||||
ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions* opts =
|
||||
new ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions();
|
||||
return GET_CPLUSPLUS_POINTER(opts);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDBOptions
|
||||
* Method: setOccValidationPolicy
|
||||
* Signature: (JB)V
|
||||
*/
|
||||
void Java_org_rocksdb_OptimisticTransactionDBOptions_setOccValidationPolicy(
|
||||
JNIEnv* /*env*/, jclass /*jcls*/, jlong jhandle, jbyte policy) {
|
||||
auto* opts =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions*>(
|
||||
jhandle);
|
||||
opts->validate_policy =
|
||||
ROCKSDB_NAMESPACE::OccValidationPolicyJni::toCppOccValidationPolicy(
|
||||
policy);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDBOptions
|
||||
* Method: getOccValidationPolicy
|
||||
* Signature: (J)B
|
||||
*/
|
||||
jbyte Java_org_rocksdb_OptimisticTransactionDBOptions_getOccValidationPolicy(
|
||||
JNIEnv* /*env*/, jclass /*jcls*/, jlong jhandle) {
|
||||
auto* opts =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions*>(
|
||||
jhandle);
|
||||
return ROCKSDB_NAMESPACE::OccValidationPolicyJni::toJavaOccValidationPolicy(
|
||||
opts->validate_policy);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDBOptions
|
||||
* Method: setOccValidationPolicy
|
||||
* Signature: (JJ)V
|
||||
*/
|
||||
void Java_org_rocksdb_OptimisticTransactionDBOptions_setOccLockBuckets(
|
||||
JNIEnv* /*env*/, jclass /*jcls*/, jlong jhandle, jlong occ_lock_buckets) {
|
||||
auto* opts =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions*>(
|
||||
jhandle);
|
||||
opts->occ_lock_buckets = static_cast<uint32_t>(occ_lock_buckets);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDBOptions
|
||||
* Method: getOccValidationPolicy
|
||||
* Signature: (J)J
|
||||
*/
|
||||
jlong Java_org_rocksdb_OptimisticTransactionDBOptions_getOccLockBuckets(
|
||||
JNIEnv* /*env*/, jclass /*jcls*/, jlong jhandle) {
|
||||
auto* opts =
|
||||
reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions*>(
|
||||
jhandle);
|
||||
return static_cast<long>(opts->occ_lock_buckets);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: org_rocksdb_OptimisticTransactionDBOptions
|
||||
* Method: disposeInternal
|
||||
* Signature: (J)V
|
||||
*/
|
||||
void Java_org_rocksdb_OptimisticTransactionDBOptions_disposeInternalJni(
|
||||
JNIEnv* /*env*/, jclass /*jcls*/, jlong jhandle) {
|
||||
delete reinterpret_cast<ROCKSDB_NAMESPACE::OptimisticTransactionDBOptions*>(
|
||||
jhandle);
|
||||
}
|
|
@ -33,6 +33,7 @@
|
|||
#include "rocksdb/table.h"
|
||||
#include "rocksdb/utilities/backup_engine.h"
|
||||
#include "rocksdb/utilities/memory_util.h"
|
||||
#include "rocksdb/utilities/optimistic_transaction_db.h"
|
||||
#include "rocksdb/utilities/transaction_db.h"
|
||||
#include "rocksdb/utilities/write_batch_with_index.h"
|
||||
#include "rocksjni/compaction_filter_factory_jnicallback.h"
|
||||
|
@ -6358,6 +6359,31 @@ class TransactionDBJni : public JavaClass {
|
|||
return jdeadlock_info;
|
||||
}
|
||||
};
|
||||
// The portal class for org.rocksdb.OccValidationPolicy.
|
||||
class OccValidationPolicyJni {
|
||||
public:
|
||||
static jbyte toJavaOccValidationPolicy(
|
||||
const ROCKSDB_NAMESPACE::OccValidationPolicy policy) {
|
||||
switch (policy) {
|
||||
case ROCKSDB_NAMESPACE::OccValidationPolicy::kValidateSerial:
|
||||
return 0x0;
|
||||
case ROCKSDB_NAMESPACE::OccValidationPolicy::kValidateParallel:
|
||||
default:
|
||||
return 0x1;
|
||||
}
|
||||
}
|
||||
|
||||
static ROCKSDB_NAMESPACE::OccValidationPolicy toCppOccValidationPolicy(
|
||||
const jbyte policy) {
|
||||
switch (policy) {
|
||||
case 0x0:
|
||||
return ROCKSDB_NAMESPACE::OccValidationPolicy::kValidateSerial;
|
||||
case 0x1:
|
||||
default:
|
||||
return ROCKSDB_NAMESPACE::OccValidationPolicy::kValidateParallel;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// The portal class for org.rocksdb.TxnDBWritePolicy
|
||||
class TxnDBWritePolicyJni {
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* Defines the policy for optimistic concurrency control validation.
|
||||
* This enum specifies the manner in which the validation occurs
|
||||
* during the commit stage.
|
||||
*/
|
||||
public enum OccValidationPolicy {
|
||||
/**
|
||||
* Validate serially at commit stage, AFTER entering the write-group.
|
||||
* This method processes isolation validation in a single-threaded manner
|
||||
* within the write-group, potentially suffering from high mutex contention
|
||||
* as discussed in the following issue:
|
||||
* <a href="https://github.com/facebook/rocksdb/issues/4402">GitHub 4402</a>
|
||||
*/
|
||||
VALIDATE_SERIAL((byte) 0),
|
||||
|
||||
/**
|
||||
* Validate parallelly before the commit stage, BEFORE entering the write-group.
|
||||
* This approach aims to reduce mutex contention by having each
|
||||
* transaction acquire locks for its write-set records in a well-defined
|
||||
* order prior to entering the write-group.
|
||||
*/
|
||||
VALIDATE_PARALLEL((byte) 1);
|
||||
|
||||
private final byte _value;
|
||||
|
||||
/**
|
||||
* Constructor for the OccValidationPolicy enum.
|
||||
* @param _value the byte representation that corresponds to
|
||||
* one of the above enums.
|
||||
*/
|
||||
OccValidationPolicy(final byte _value) {
|
||||
this._value = _value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the byte representation associated with this validation policy.
|
||||
* @return the byte representation of the validation policy.
|
||||
*/
|
||||
public byte getValue() {
|
||||
return _value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a byte representation of a value, convert it to {@link OccValidationPolicy}.
|
||||
*
|
||||
* @param policy the byte representation of the policy.
|
||||
* @return the matching OccValidationPolicy.
|
||||
* @throws IllegalArgumentException if no matching policy is found.
|
||||
*/
|
||||
public static OccValidationPolicy getOccValidationPolicy(final byte policy) {
|
||||
for (OccValidationPolicy value : OccValidationPolicy.values()) {
|
||||
if (value.getValue() == policy) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown OccValidationPolicy constant : " + policy);
|
||||
}
|
||||
}
|
|
@ -65,11 +65,32 @@ public class OptimisticTransactionDB extends RocksDB
|
|||
*
|
||||
* @throws RocksDBException if an error occurs whilst opening the database.
|
||||
*/
|
||||
public static OptimisticTransactionDB open(final DBOptions dbOptions,
|
||||
final String path,
|
||||
public static OptimisticTransactionDB open(final DBOptions dbOptions, final String path,
|
||||
final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
|
||||
final List<ColumnFamilyHandle> columnFamilyHandles)
|
||||
throws RocksDBException {
|
||||
final List<ColumnFamilyHandle> columnFamilyHandles) throws RocksDBException {
|
||||
return open(dbOptions, new OptimisticTransactionDBOptions(), path, columnFamilyDescriptors,
|
||||
columnFamilyHandles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open an OptimisticTransactionDB similar to
|
||||
* {@link RocksDB#open(DBOptions, String, List, List)}.
|
||||
*
|
||||
* @param dbOptions {@link org.rocksdb.DBOptions} instance.
|
||||
* @param optimisticDbOptions {@link org.rocksdb.OptimisticTransactionDBOptions} instance.
|
||||
* @param path the path to the rocksdb.
|
||||
* @param columnFamilyDescriptors list of column family descriptors
|
||||
* @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
|
||||
*
|
||||
* @return a {@link OptimisticTransactionDB} instance on success, null if the
|
||||
* specified {@link OptimisticTransactionDB} can not be opened.
|
||||
*
|
||||
* @throws RocksDBException if an error occurs whilst opening the database.
|
||||
*/
|
||||
public static OptimisticTransactionDB open(final DBOptions dbOptions,
|
||||
final OptimisticTransactionDBOptions optimisticDbOptions, final String path,
|
||||
final List<ColumnFamilyDescriptor> columnFamilyDescriptors,
|
||||
final List<ColumnFamilyHandle> columnFamilyHandles) throws RocksDBException {
|
||||
int defaultColumnFamilyIndex = -1;
|
||||
final byte[][] cfNames = new byte[columnFamilyDescriptors.size()][];
|
||||
final long[] cfOptionHandles = new long[columnFamilyDescriptors.size()];
|
||||
|
@ -87,8 +108,8 @@ public class OptimisticTransactionDB extends RocksDB
|
|||
"You must provide the default column family in your columnFamilyDescriptors");
|
||||
}
|
||||
|
||||
final long[] handles = open(dbOptions.nativeHandle_, path, cfNames,
|
||||
cfOptionHandles);
|
||||
final long[] handles = open(
|
||||
dbOptions.nativeHandle_, optimisticDbOptions.nativeHandle_, path, cfNames, cfOptionHandles);
|
||||
final OptimisticTransactionDB otdb =
|
||||
new OptimisticTransactionDB(handles[0]);
|
||||
|
||||
|
@ -212,6 +233,15 @@ public class OptimisticTransactionDB extends RocksDB
|
|||
return oldTransaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying {@link OccValidationPolicy}.
|
||||
*
|
||||
* @return The underlying {@link OccValidationPolicy}.
|
||||
*/
|
||||
public OccValidationPolicy occValidationPolicy() {
|
||||
return OccValidationPolicy.getOccValidationPolicy(occValidationPolicy(nativeHandle_));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying database that was opened.
|
||||
*
|
||||
|
@ -231,8 +261,9 @@ public class OptimisticTransactionDB extends RocksDB
|
|||
|
||||
protected static native long open(final long optionsHandle,
|
||||
final String path) throws RocksDBException;
|
||||
protected static native long[] open(final long handle, final String path,
|
||||
final byte[][] columnFamilyNames, final long[] columnFamilyOptions);
|
||||
protected static native long[] open(final long optionsHandle,
|
||||
final long optimisticDbOptionsHandle, final String path, final byte[][] columnFamilyNames,
|
||||
final long[] columnFamilyOptions);
|
||||
private static native void closeDatabase(final long handle) throws RocksDBException;
|
||||
private static native long beginTransaction(final long handle, final long writeOptionsHandle);
|
||||
private static native long beginTransaction(final long handle, final long writeOptionsHandle,
|
||||
|
@ -242,5 +273,6 @@ public class OptimisticTransactionDB extends RocksDB
|
|||
private static native long beginTransaction_withOld(final long handle,
|
||||
final long writeOptionsHandle, final long optimisticTransactionOptionsHandle,
|
||||
final long oldTransactionHandle);
|
||||
private static native byte occValidationPolicy(final long handle);
|
||||
private static native long getBaseDB(final long handle);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
// 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;
|
||||
|
||||
/**
|
||||
* Used to control the Optimistic Concurrency Control (OCC) aspect of a database.
|
||||
* It will be used during the creation of a
|
||||
* {@link org.rocksdb.OptimisticTransactionDB} (i.e., OptimisticTransactionDB.open()).
|
||||
* <p>
|
||||
* As a descendent of {@link AbstractNativeReference}, this class is {@link AutoCloseable}
|
||||
* and will be automatically released if opened in the preamble of a try with resources block.
|
||||
*/
|
||||
public class OptimisticTransactionDBOptions extends RocksObject {
|
||||
public OptimisticTransactionDBOptions() {
|
||||
super(newOptimisticTransactionOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set OccValidationPolicy for this instance.
|
||||
* See {@link OccValidationPolicy}.
|
||||
*
|
||||
* @param policy The type of OCC Validation Policy
|
||||
*
|
||||
* @return this OptimisticTransactionOptions instance
|
||||
*/
|
||||
public OptimisticTransactionDBOptions setOccValidationPolicy(final OccValidationPolicy policy) {
|
||||
assert (isOwningHandle());
|
||||
setOccValidationPolicy(nativeHandle_, policy.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get OccValidationPolicy for this instance.
|
||||
* See {@link OccValidationPolicy}.
|
||||
*
|
||||
* @return The type of OCC Validation Policy used.
|
||||
*/
|
||||
public OccValidationPolicy occValidationPolicy() {
|
||||
assert (isOwningHandle());
|
||||
return OccValidationPolicy.getOccValidationPolicy(occValidationPolicy(getNativeHandle()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of striped/bucketed mutex locks for validating transactions.
|
||||
* Used on only if validate_policy == {@link OccValidationPolicy}::VALIDATE_PARALLEL
|
||||
* and shared_lock_buckets (below) is empty. Larger number potentially
|
||||
* reduces contention but uses more memory.
|
||||
*
|
||||
* @param occLongBuckets Number of striped/bucketed mutex locks.
|
||||
*
|
||||
* @return this OptimisticTransactionOptions instance
|
||||
*/
|
||||
public OptimisticTransactionDBOptions setOccLockBuckets(final long occLongBuckets) {
|
||||
assert (isOwningHandle());
|
||||
setOccLockBuckets(nativeHandle_, occLongBuckets);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of striped/bucketed mutex locks for validating transactions.
|
||||
* Used on only if validate_policy == {@link OccValidationPolicy}::VALIDATE_PARALLEL
|
||||
* and shared_lock_buckets (below) is empty. Larger number potentially
|
||||
* reduces contention but uses more memory.
|
||||
**
|
||||
* @return Number of striped/bucketed mutex locks.
|
||||
*/
|
||||
public long getOccLockBuckets() {
|
||||
assert (isOwningHandle());
|
||||
return getOccLockBuckets(nativeHandle_);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
protected final void disposeInternal(final long handle) {
|
||||
disposeInternalJni(handle);
|
||||
}
|
||||
|
||||
private static native long newOptimisticTransactionOptions();
|
||||
private static native void setOccValidationPolicy(final long handle, final byte policy);
|
||||
private static native byte occValidationPolicy(final long handle);
|
||||
private static native void setOccLockBuckets(final long handle, final long occLongBuckets);
|
||||
private static native byte getOccLockBuckets(final long handle);
|
||||
private static native void disposeInternalJni(final long handle);
|
||||
}
|
|
@ -29,6 +29,36 @@ public class OptimisticTransactionDBTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void open_OptimisticTransactionDBOptions() throws RocksDBException {
|
||||
try (final DBOptions dbOptions =
|
||||
new DBOptions().setCreateIfMissing(true).setCreateMissingColumnFamilies(true);
|
||||
final OptimisticTransactionDBOptions optimisticOptions =
|
||||
new OptimisticTransactionDBOptions().setOccValidationPolicy(
|
||||
OccValidationPolicy.VALIDATE_SERIAL);
|
||||
final ColumnFamilyOptions myCfOpts = new ColumnFamilyOptions()) {
|
||||
final List<ColumnFamilyDescriptor> columnFamilyDescriptors =
|
||||
Arrays.asList(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY),
|
||||
new ColumnFamilyDescriptor("myCf".getBytes(), myCfOpts));
|
||||
|
||||
final List<ColumnFamilyHandle> columnFamilyHandles = new ArrayList<>();
|
||||
|
||||
try (final OptimisticTransactionDB otdb = OptimisticTransactionDB.open(dbOptions,
|
||||
optimisticOptions, dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors,
|
||||
columnFamilyHandles)) {
|
||||
try {
|
||||
assertThat(otdb).isNotNull();
|
||||
assertThat(otdb.occValidationPolicy())
|
||||
.isEqualTo(optimisticOptions.occValidationPolicy());
|
||||
} finally {
|
||||
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
|
||||
handle.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void open_columnFamilies() throws RocksDBException {
|
||||
try(final DBOptions dbOptions = new DBOptions().setCreateIfMissing(true)
|
||||
|
@ -47,6 +77,8 @@ public class OptimisticTransactionDBTest {
|
|||
columnFamilyDescriptors, columnFamilyHandles)) {
|
||||
try {
|
||||
assertThat(otdb).isNotNull();
|
||||
assertThat(otdb.occValidationPolicy())
|
||||
.isEqualTo(OccValidationPolicy.VALIDATE_PARALLEL);
|
||||
} finally {
|
||||
for (final ColumnFamilyHandle handle : columnFamilyHandles) {
|
||||
handle.close();
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
// 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;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Random;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class OptimisticTransactionOptionsDBTest {
|
||||
private static final Random rand = PlatformRandomHelper.getPlatformSpecificRandomFactory();
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeAll() {
|
||||
RocksDB.loadLibrary();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lockBucketCount() {
|
||||
try (final OptimisticTransactionDBOptions options = new OptimisticTransactionDBOptions()) {
|
||||
final long lockBucketCount = rand.nextInt(Integer.MAX_VALUE) + 1;
|
||||
options.setOccLockBuckets(lockBucketCount);
|
||||
assertThat(options.getOccLockBuckets()).isEqualTo(lockBucketCount);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setOccValidationPolicy() {
|
||||
try (final OptimisticTransactionDBOptions options = new OptimisticTransactionDBOptions()) {
|
||||
options.setOccValidationPolicy(OccValidationPolicy.VALIDATE_SERIAL);
|
||||
assertThat(options.occValidationPolicy()).isEqualTo(OccValidationPolicy.VALIDATE_SERIAL);
|
||||
|
||||
options.setOccValidationPolicy(OccValidationPolicy.VALIDATE_PARALLEL);
|
||||
assertThat(options.occValidationPolicy()).isEqualTo(OccValidationPolicy.VALIDATE_PARALLEL);
|
||||
}
|
||||
}
|
||||
}
|
1
src.mk
1
src.mk
|
@ -687,6 +687,7 @@ JNI_NATIVE_SOURCES = \
|
|||
java/rocksjni/merge_operator.cc \
|
||||
java/rocksjni/native_comparator_wrapper_test.cc \
|
||||
java/rocksjni/optimistic_transaction_db.cc \
|
||||
java/rocksjni/optimistic_transaction_db_options.cc \
|
||||
java/rocksjni/optimistic_transaction_options.cc \
|
||||
java/rocksjni/options.cc \
|
||||
java/rocksjni/options_util.cc \
|
||||
|
|
|
@ -88,7 +88,9 @@ class OptimisticTransactionDBImpl : public OptimisticTransactionDB {
|
|||
return OptimisticTransactionDB::Write(write_opts, batch);
|
||||
}
|
||||
|
||||
OccValidationPolicy GetValidatePolicy() const { return validate_policy_; }
|
||||
OccValidationPolicy GetValidatePolicy() const override {
|
||||
return validate_policy_;
|
||||
}
|
||||
|
||||
port::Mutex& GetLockBucket(const Slice& key, uint64_t seed) {
|
||||
return bucketed_locks_->GetLockBucket(key, seed);
|
||||
|
|
Loading…
Reference in New Issue