Implement Java API for ConcurrentTaskLimiter class and compaction_thread_limiter field in ColumnFamilyOptions (#7347)
Summary: as title Pull Request resolved: https://github.com/facebook/rocksdb/pull/7347 Test Plan: unit tests included Reviewed By: jay-zhuang Differential Revision: D23592552 Pulled By: pdillinger fbshipit-source-id: 1c3571b6f42bfd0cfd723ff49d01fbc02a1be45bmain
parent
5c39d8df69
commit
ec5add398c
@ -0,0 +1,90 @@ |
|||||||
|
#include "rocksdb/concurrent_task_limiter.h" |
||||||
|
|
||||||
|
#include <jni.h> |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "include/org_rocksdb_ConcurrentTaskLimiterImpl.h" |
||||||
|
#include "rocksjni/portal.h" |
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_rocksdb_ConcurrentTaskLimiterImpl |
||||||
|
* Method: newConcurrentTaskLimiterImpl0 |
||||||
|
* Signature: (Ljava/lang/String;I)J |
||||||
|
*/ |
||||||
|
jlong Java_org_rocksdb_ConcurrentTaskLimiterImpl_newConcurrentTaskLimiterImpl0( |
||||||
|
JNIEnv* env, jclass, jstring jname, jint limit) { |
||||||
|
jboolean has_exception; |
||||||
|
std::string name = |
||||||
|
ROCKSDB_NAMESPACE::JniUtil::copyStdString(env, jname, &has_exception); |
||||||
|
if (JNI_TRUE == has_exception) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
auto* ptr = new std::shared_ptr<ROCKSDB_NAMESPACE::ConcurrentTaskLimiter>( |
||||||
|
ROCKSDB_NAMESPACE::NewConcurrentTaskLimiter(name, limit)); |
||||||
|
|
||||||
|
return reinterpret_cast<jlong>(ptr); |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_rocksdb_ConcurrentTaskLimiterImpl |
||||||
|
* Method: name |
||||||
|
* Signature: (J)Ljava/lang/String; |
||||||
|
*/ |
||||||
|
jstring Java_org_rocksdb_ConcurrentTaskLimiterImpl_name(JNIEnv* env, jclass, |
||||||
|
jlong handle) { |
||||||
|
const auto& limiter = *reinterpret_cast< |
||||||
|
std::shared_ptr<ROCKSDB_NAMESPACE::ConcurrentTaskLimiter>*>(handle); |
||||||
|
return ROCKSDB_NAMESPACE::JniUtil::toJavaString(env, &limiter->GetName()); |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_rocksdb_ConcurrentTaskLimiterImpl |
||||||
|
* Method: setMaxOutstandingTask |
||||||
|
* Signature: (JI)V |
||||||
|
*/ |
||||||
|
void Java_org_rocksdb_ConcurrentTaskLimiterImpl_setMaxOutstandingTask( |
||||||
|
JNIEnv*, jclass, jlong handle, jint max_outstanding_task) { |
||||||
|
const auto& limiter = *reinterpret_cast< |
||||||
|
std::shared_ptr<ROCKSDB_NAMESPACE::ConcurrentTaskLimiter>*>(handle); |
||||||
|
limiter->SetMaxOutstandingTask(static_cast<int32_t>(max_outstanding_task)); |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_rocksdb_ConcurrentTaskLimiterImpl |
||||||
|
* Method: resetMaxOutstandingTask |
||||||
|
* Signature: (J)V |
||||||
|
*/ |
||||||
|
void Java_org_rocksdb_ConcurrentTaskLimiterImpl_resetMaxOutstandingTask( |
||||||
|
JNIEnv*, jclass, jlong handle) { |
||||||
|
const auto& limiter = *reinterpret_cast< |
||||||
|
std::shared_ptr<ROCKSDB_NAMESPACE::ConcurrentTaskLimiter>*>(handle); |
||||||
|
limiter->ResetMaxOutstandingTask(); |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_rocksdb_ConcurrentTaskLimiterImpl |
||||||
|
* Method: outstandingTask |
||||||
|
* Signature: (J)I |
||||||
|
*/ |
||||||
|
jint Java_org_rocksdb_ConcurrentTaskLimiterImpl_outstandingTask(JNIEnv*, jclass, |
||||||
|
jlong handle) { |
||||||
|
const auto& limiter = *reinterpret_cast< |
||||||
|
std::shared_ptr<ROCKSDB_NAMESPACE::ConcurrentTaskLimiter>*>(handle); |
||||||
|
return static_cast<jint>(limiter->GetOutstandingTask()); |
||||||
|
} |
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_rocksdb_ConcurrentTaskLimiterImpl |
||||||
|
* Method: disposeInternal |
||||||
|
* Signature: (J)V |
||||||
|
*/ |
||||||
|
void Java_org_rocksdb_ConcurrentTaskLimiterImpl_disposeInternal(JNIEnv*, |
||||||
|
jobject, |
||||||
|
jlong jhandle) { |
||||||
|
auto* ptr = reinterpret_cast< |
||||||
|
std::shared_ptr<ROCKSDB_NAMESPACE::ConcurrentTaskLimiter>*>(jhandle); |
||||||
|
delete ptr; // delete std::shared_ptr
|
||||||
|
} |
@ -0,0 +1,38 @@ |
|||||||
|
package org.rocksdb; |
||||||
|
|
||||||
|
public abstract class ConcurrentTaskLimiter extends RocksObject { |
||||||
|
protected ConcurrentTaskLimiter(final long nativeHandle) { |
||||||
|
super(nativeHandle); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a name that identifies this concurrent task limiter. |
||||||
|
* |
||||||
|
* @return Concurrent task limiter name. |
||||||
|
*/ |
||||||
|
public abstract String name(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Set max concurrent tasks.<br> |
||||||
|
* limit = 0 means no new task allowed.<br> |
||||||
|
* limit < 0 means no limitation. |
||||||
|
* |
||||||
|
* @param maxOutstandinsTask max concurrent tasks. |
||||||
|
* @return the reference to the current instance of ConcurrentTaskLimiter. |
||||||
|
*/ |
||||||
|
public abstract ConcurrentTaskLimiter setMaxOutstandingTask(final int maxOutstandinsTask); |
||||||
|
|
||||||
|
/** |
||||||
|
* Reset to unlimited max concurrent task. |
||||||
|
* |
||||||
|
* @return the reference to the current instance of ConcurrentTaskLimiter. |
||||||
|
*/ |
||||||
|
public abstract ConcurrentTaskLimiter resetMaxOutstandingTask(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns current outstanding task count. |
||||||
|
* |
||||||
|
* @return current outstanding task count. |
||||||
|
*/ |
||||||
|
public abstract int outstandingTask(); |
||||||
|
} |
@ -0,0 +1,42 @@ |
|||||||
|
package org.rocksdb; |
||||||
|
|
||||||
|
public class ConcurrentTaskLimiterImpl extends ConcurrentTaskLimiter { |
||||||
|
public ConcurrentTaskLimiterImpl(final String name, final int maxOutstandingTask) { |
||||||
|
super(newConcurrentTaskLimiterImpl0(name, maxOutstandingTask)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String name() { |
||||||
|
assert (isOwningHandle()); |
||||||
|
return name(nativeHandle_); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ConcurrentTaskLimiter setMaxOutstandingTask(final int maxOutstandingTask) { |
||||||
|
assert (isOwningHandle()); |
||||||
|
setMaxOutstandingTask(nativeHandle_, maxOutstandingTask); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ConcurrentTaskLimiter resetMaxOutstandingTask() { |
||||||
|
assert (isOwningHandle()); |
||||||
|
resetMaxOutstandingTask(nativeHandle_); |
||||||
|
return this; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int outstandingTask() { |
||||||
|
assert (isOwningHandle()); |
||||||
|
return outstandingTask(nativeHandle_); |
||||||
|
} |
||||||
|
|
||||||
|
private static native long newConcurrentTaskLimiterImpl0( |
||||||
|
final String name, final int maxOutstandingTask); |
||||||
|
private static native String name(final long handle); |
||||||
|
private static native void setMaxOutstandingTask(final long handle, final int limit); |
||||||
|
private static native void resetMaxOutstandingTask(final long handle); |
||||||
|
private static native int outstandingTask(final long handle); |
||||||
|
|
||||||
|
@Override protected final native void disposeInternal(final long handle); |
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
package org.rocksdb; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.ClassRule; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
public class ConcurrentTaskLimiterTest { |
||||||
|
@ClassRule |
||||||
|
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = |
||||||
|
new RocksNativeLibraryResource(); |
||||||
|
|
||||||
|
private static final String NAME = "name"; |
||||||
|
|
||||||
|
private ConcurrentTaskLimiter concurrentTaskLimiter; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void beforeTest() { |
||||||
|
concurrentTaskLimiter = new ConcurrentTaskLimiterImpl(NAME, 3); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void name() { |
||||||
|
assertEquals(NAME, concurrentTaskLimiter.name()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void outstandingTask() { |
||||||
|
assertEquals(0, concurrentTaskLimiter.outstandingTask()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void setMaxOutstandingTask() { |
||||||
|
assertEquals(concurrentTaskLimiter, concurrentTaskLimiter.setMaxOutstandingTask(4)); |
||||||
|
assertEquals(0, concurrentTaskLimiter.outstandingTask()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void resetMaxOutstandingTask() { |
||||||
|
assertEquals(concurrentTaskLimiter, concurrentTaskLimiter.resetMaxOutstandingTask()); |
||||||
|
assertEquals(0, concurrentTaskLimiter.outstandingTask()); |
||||||
|
} |
||||||
|
|
||||||
|
@After |
||||||
|
public void afterTest() { |
||||||
|
concurrentTaskLimiter.close(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue