Skip to content

Commit 7a1bd05

Browse files
committed
Merge pull request XRPLF#302 from ankgup87/master
[Java] Add rate limiter
2 parents 32f2532 + 423e52c commit 7a1bd05

7 files changed

+112
-1
lines changed

java/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.RocksIterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.BlockBasedTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter org.rocksdb.BloomFilter org.rocksdb.RestoreOptions org.rocksdb.RestoreBackupableDB org.rocksdb.RocksEnv
1+
NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.RocksIterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.BlockBasedTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter org.rocksdb.BloomFilter org.rocksdb.RestoreOptions org.rocksdb.RestoreBackupableDB org.rocksdb.RocksEnv org.rocksdb.GenericRateLimiterConfig
22

33
NATIVE_INCLUDE = ./include
44
ROCKSDB_JAR = rocksdbjni.jar

java/RocksDBSample.java

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public static void main(String[] args) {
7575
// Plain-Table requires mmap read
7676
options.setAllowMmapReads(true);
7777
assert(options.tableFactoryName().equals("PlainTable"));
78+
79+
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000,
80+
10000, 10));
81+
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000));
7882

7983
BlockBasedTableConfig table_options = new BlockBasedTableConfig();
8084
table_options.setBlockCacheSize(64 * SizeUnit.KB)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
package org.rocksdb;
6+
7+
/**
8+
* Config for rate limiter, which is used to control write rate of flush and
9+
* compaction.
10+
*/
11+
public class GenericRateLimiterConfig extends RateLimiterConfig {
12+
private static final long DEFAULT_REFILL_PERIOD_MICROS = (100 * 1000);
13+
private static final int DEFAULT_FAIRNESS = 10;
14+
15+
public GenericRateLimiterConfig(long rateBytesPerSecond,
16+
long refillPeriodMicros, int fairness) {
17+
rateBytesPerSecond_ = rateBytesPerSecond;
18+
refillPeriodMicros_ = refillPeriodMicros;
19+
fairness_ = fairness;
20+
}
21+
22+
public GenericRateLimiterConfig(long rateBytesPerSecond) {
23+
this(rateBytesPerSecond, DEFAULT_REFILL_PERIOD_MICROS, DEFAULT_FAIRNESS);
24+
}
25+
26+
@Override protected long newRateLimiterHandle() {
27+
return newRateLimiterHandle(rateBytesPerSecond_, refillPeriodMicros_,
28+
fairness_);
29+
}
30+
31+
private native long newRateLimiterHandle(long rateBytesPerSecond,
32+
long refillPeriodMicros, int fairness);
33+
private final long rateBytesPerSecond_;
34+
private final long refillPeriodMicros_;
35+
private final int fairness_;
36+
}

java/org/rocksdb/Options.java

+15
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,19 @@ public Options setMemTableConfig(MemTableConfig config) {
11041104
setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle());
11051105
return this;
11061106
}
1107+
1108+
/**
1109+
* Use to control write rate of flush and compaction. Flush has higher
1110+
* priority than compaction. Rate limiting is disabled if nullptr.
1111+
* Default: nullptr
1112+
*
1113+
* @param config rate limiter config.
1114+
* @return the instance of the current Options.
1115+
*/
1116+
public Options setRateLimiterConfig(RateLimiterConfig config) {
1117+
setRateLimiter(nativeHandle_, config.newRateLimiterHandle());
1118+
return this;
1119+
}
11071120

11081121
/**
11091122
* Returns the name of the current mem table representation.
@@ -2192,6 +2205,8 @@ private native void setMaxBackgroundCompactions(
21922205
private native long statisticsPtr(long optHandle);
21932206

21942207
private native void setMemTableFactory(long handle, long factoryHandle);
2208+
private native void setRateLimiter(long handle,
2209+
long rateLimiterHandle);
21952210
private native String memTableFactoryName(long handle);
21962211

21972212
private native void setTableFactory(long handle, long factoryHandle);
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
package org.rocksdb;
6+
7+
/**
8+
* Config for rate limiter, which is used to control write rate of flush and
9+
* compaction.
10+
*/
11+
public abstract class RateLimiterConfig {
12+
/**
13+
* This function should only be called by Options.setRateLimiter(),
14+
* which will create a c++ shared-pointer to the c++ RateLimiter
15+
* that is associated with the Java RateLimtierConifg.
16+
*
17+
* @see Options.setRateLimiter()
18+
*/
19+
abstract protected long newRateLimiterHandle();
20+
}

java/rocksjni/options.cc

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rocksdb/memtablerep.h"
2222
#include "rocksdb/table.h"
2323
#include "rocksdb/slice_transform.h"
24+
#include "rocksdb/rate_limiter.h"
2425

2526
/*
2627
* Class: org_rocksdb_Options
@@ -459,6 +460,17 @@ void Java_org_rocksdb_Options_setMemTableFactory(
459460
reinterpret_cast<rocksdb::MemTableRepFactory*>(jfactory_handle));
460461
}
461462

463+
/*
464+
* Class: org_rocksdb_Options
465+
* Method: setRateLimiter
466+
* Signature: (JJ)V
467+
*/
468+
void Java_org_rocksdb_Options_setRateLimiter(
469+
JNIEnv* env, jobject jobj, jlong jhandle, jlong jrate_limiter_handle) {
470+
reinterpret_cast<rocksdb::Options*>(jhandle)->rate_limiter.reset(
471+
reinterpret_cast<rocksdb::RateLimiter*>(jrate_limiter_handle));
472+
}
473+
462474
/*
463475
* Class: org_rocksdb_Options
464476
* Method: tableCacheNumshardbits

java/rocksjni/ratelimiterjni.cc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
//
6+
// This file implements the "bridge" between Java and C++ for RateLimiter.
7+
8+
#include "rocksjni/portal.h"
9+
#include "include/org_rocksdb_GenericRateLimiterConfig.h"
10+
#include "rocksdb/rate_limiter.h"
11+
12+
/*
13+
* Class: org_rocksdb_GenericRateLimiterConfig
14+
* Method: newRateLimiterHandle
15+
* Signature: (JJI)J
16+
*/
17+
jlong Java_org_rocksdb_GenericRateLimiterConfig_newRateLimiterHandle(
18+
JNIEnv* env, jobject jobj, jlong jrate_bytes_per_second,
19+
jlong jrefill_period_micros, jint jfairness) {
20+
return reinterpret_cast<jlong>(rocksdb::NewGenericRateLimiter(
21+
rocksdb::jlong_to_size_t(jrate_bytes_per_second),
22+
rocksdb::jlong_to_size_t(jrefill_period_micros),
23+
static_cast<int32_t>(jfairness)));
24+
}

0 commit comments

Comments
 (0)