[Java] Add Java support for cache sharding.

Summary:
Add setCacheNumShardBits() and cacheNumShardBits() to Options.  This allows
developers to control the number of shards for the block cache.

Test Plan:
make rocksdbjava
cd java
make db_bench
./jdb_bench.sh --cache_size=1048576 --cache_numshardbits=6

Reviewers: sdong, ljin, ankgup87

Reviewed By: ankgup87

Subscribers: leveldb

Differential Revision: https://reviews.facebook.net/D19347
main
Yueh-Hsuan Chiang 11 years ago
parent ae7743f226
commit d19aa25594
  1. 2
      java/RocksDBSample.java
  2. 35
      java/org/rocksdb/Options.java
  3. 6
      java/org/rocksdb/RocksDB.java
  4. 3
      java/org/rocksdb/benchmark/DbBenchmark.java
  5. 8
      java/rocksjni/rocksjni.cc

@ -44,6 +44,7 @@ public class RocksDBSample {
.setBlockSize(64 * SizeUnit.KB) .setBlockSize(64 * SizeUnit.KB)
.setMaxBackgroundCompactions(10) .setMaxBackgroundCompactions(10)
.setFilter(filter) .setFilter(filter)
.setCacheNumShardBits(6)
.setCompressionType(CompressionType.SNAPPY_COMPRESSION); .setCompressionType(CompressionType.SNAPPY_COMPRESSION);
Statistics stats = options.statisticsPtr(); Statistics stats = options.statisticsPtr();
@ -53,6 +54,7 @@ public class RocksDBSample {
assert(options.disableSeekCompaction() == true); assert(options.disableSeekCompaction() == true);
assert(options.blockSize() == 64 * SizeUnit.KB); assert(options.blockSize() == 64 * SizeUnit.KB);
assert(options.maxBackgroundCompactions() == 10); assert(options.maxBackgroundCompactions() == 10);
assert(options.cacheNumShardBits() == 6);
assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION); assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION);
assert(options.memTableFactoryName().equals("SkipListFactory")); assert(options.memTableFactoryName().equals("SkipListFactory"));

@ -14,6 +14,7 @@ package org.rocksdb;
*/ */
public class Options extends RocksObject { public class Options extends RocksObject {
static final long DEFAULT_CACHE_SIZE = 8 << 20; static final long DEFAULT_CACHE_SIZE = 8 << 20;
static final int DEFAULT_NUM_SHARD_BITS = -1;
/** /**
* Construct options for opening a RocksDB. * Construct options for opening a RocksDB.
* *
@ -23,6 +24,7 @@ public class Options extends RocksObject {
public Options() { public Options() {
super(); super();
cacheSize_ = DEFAULT_CACHE_SIZE; cacheSize_ = DEFAULT_CACHE_SIZE;
numShardBits_ = DEFAULT_NUM_SHARD_BITS;
newOptions(); newOptions();
env_ = RocksEnv.getDefault(); env_ = RocksEnv.getDefault();
} }
@ -215,6 +217,7 @@ public class Options extends RocksObject {
* If cacheSize is non-positive, then cache will not be used. * If cacheSize is non-positive, then cache will not be used.
* *
* DEFAULT: 8M * DEFAULT: 8M
* @see setCacheNumShardBits()
*/ */
public Options setCacheSize(long cacheSize) { public Options setCacheSize(long cacheSize) {
cacheSize_ = cacheSize; cacheSize_ = cacheSize;
@ -223,11 +226,42 @@ public class Options extends RocksObject {
/** /**
* @return the amount of cache in bytes that will be used by RocksDB. * @return the amount of cache in bytes that will be used by RocksDB.
*
* @see cacheNumShardBits()
*/ */
public long cacheSize() { public long cacheSize() {
return cacheSize_; return cacheSize_;
} }
/**
* Controls the number of shards for the block cache.
* This is applied only if cacheSize is set to non-negative.
*
* @param numShardBits the number of shard bits. The resulting
* number of shards would be 2 ^ numShardBits. Any negative
* number means use default settings."
* @return the reference to the current option.
*
* @see setCacheSize()
*/
public Options setCacheNumShardBits(int numShardBits) {
numShardBits_ = numShardBits;
return this;
}
/**
* Returns the number of shard bits used in the block cache.
* The resulting number of shards would be 2 ^ (returned value).
* Any negative number means use default settings.
*
* @return the number of shard bits used in the block cache.
*
* @see cacheSize()
*/
public int cacheNumShardBits() {
return numShardBits_;
}
/** /**
* If true, an error will be thrown during RocksDB.open() if the * If true, an error will be thrown during RocksDB.open() if the
* database already exists. * database already exists.
@ -2397,6 +2431,7 @@ public class Options extends RocksObject {
long handle, int prefixLength); long handle, int prefixLength);
long cacheSize_; long cacheSize_;
int numShardBits_;
Filter filter_; Filter filter_;
RocksEnv env_; RocksEnv env_;
} }

@ -106,7 +106,8 @@ public class RocksDB extends RocksObject {
// in RocksDB can prevent Java to GC during the life-time of // in RocksDB can prevent Java to GC during the life-time of
// the currently-created RocksDB. // the currently-created RocksDB.
RocksDB db = new RocksDB(); RocksDB db = new RocksDB();
db.open(options.nativeHandle_, options.cacheSize_, path); db.open(options.nativeHandle_, options.cacheSize_,
options.numShardBits_, path);
db.transferCppRawPointersOwnershipFrom(options); db.transferCppRawPointersOwnershipFrom(options);
return db; return db;
} }
@ -330,7 +331,8 @@ public class RocksDB extends RocksObject {
// native methods // native methods
protected native void open( protected native void open(
long optionsHandle, long cacheSize, String path) throws RocksDBException; long optionsHandle, long cacheSize, int numShardBits,
String path) throws RocksDBException;
protected native void put( protected native void put(
long handle, byte[] key, int keyLen, long handle, byte[] key, int keyLen,
byte[] value, int valueLen) throws RocksDBException; byte[] value, int valueLen) throws RocksDBException;

@ -534,6 +534,8 @@ public class DbBenchmark {
(Integer)flags_.get(Flag.max_background_flushes)); (Integer)flags_.get(Flag.max_background_flushes));
options.setCacheSize( options.setCacheSize(
(Long)flags_.get(Flag.cache_size)); (Long)flags_.get(Flag.cache_size));
options.setCacheNumShardBits(
(Integer)flags_.get(Flag.cache_numshardbits));
options.setBlockSize( options.setBlockSize(
(Long)flags_.get(Flag.block_size)); (Long)flags_.get(Flag.block_size));
options.setMaxOpenFiles( options.setMaxOpenFiles(
@ -616,7 +618,6 @@ public class DbBenchmark {
options.setCompressionLevel((Integer)flags_.get(Flag.compression_level)); options.setCompressionLevel((Integer)flags_.get(Flag.compression_level));
options.setMinLevelToCompress((Integer)flags_.get(Flag.min_level_to_compress)); options.setMinLevelToCompress((Integer)flags_.get(Flag.min_level_to_compress));
options.setHdfs((String)flags_.get(Flag.hdfs)); // env options.setHdfs((String)flags_.get(Flag.hdfs)); // env
options.setCacheNumshardbits((Integer)flags_.get(Flag.cache_numshardbits));
options.setStatistics((Boolean)flags_.get(Flag.statistics)); options.setStatistics((Boolean)flags_.get(Flag.statistics));
options.setUniversalSizeRatio( options.setUniversalSizeRatio(
(Integer)flags_.get(Flag.universal_size_ratio)); (Integer)flags_.get(Flag.universal_size_ratio));

@ -27,11 +27,15 @@
*/ */
void Java_org_rocksdb_RocksDB_open( void Java_org_rocksdb_RocksDB_open(
JNIEnv* env, jobject jdb, jlong jopt_handle, JNIEnv* env, jobject jdb, jlong jopt_handle,
jlong jcache_size, jstring jdb_path) { jlong jcache_size, jint jnum_shardbits, jstring jdb_path) {
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle); auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
if (jcache_size > 0) { if (jcache_size > 0) {
opt->no_block_cache = false; opt->no_block_cache = false;
opt->block_cache = rocksdb::NewLRUCache(jcache_size); if (jnum_shardbits >= 1) {
opt->block_cache = rocksdb::NewLRUCache(jcache_size, jnum_shardbits);
} else {
opt->block_cache = rocksdb::NewLRUCache(jcache_size);
}
} else { } else {
opt->no_block_cache = true; opt->no_block_cache = true;
opt->block_cache = nullptr; opt->block_cache = nullptr;

Loading…
Cancel
Save