Summary: Closes https://github.com/facebook/rocksdb/pull/2551 Differential Revision: D5399288 Pulled By: sagar0 fbshipit-source-id: dd3df2ed6cc5ae612db0998ea746cc29fccf568emain
parent
269d383d5d
commit
000bf0af38
@ -0,0 +1,33 @@ |
|||||||
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
//
|
||||||
|
// This file implements the callback "bridge" between Java and C++ for
|
||||||
|
// rocksdb::Statistics
|
||||||
|
|
||||||
|
#include "rocksjni/statisticsjni.h" |
||||||
|
|
||||||
|
namespace rocksdb { |
||||||
|
|
||||||
|
StatisticsJni::StatisticsJni(std::shared_ptr<Statistics> stats) |
||||||
|
: StatisticsImpl(stats, false), m_ignore_histograms() { |
||||||
|
} |
||||||
|
|
||||||
|
StatisticsJni::StatisticsJni(std::shared_ptr<Statistics> stats, |
||||||
|
const std::set<uint32_t> ignore_histograms) : StatisticsImpl(stats, false), |
||||||
|
m_ignore_histograms(ignore_histograms) { |
||||||
|
} |
||||||
|
|
||||||
|
bool StatisticsJni::HistEnabledForType(uint32_t type) const { |
||||||
|
if (type >= HISTOGRAM_ENUM_MAX) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (m_ignore_histograms.count(type) > 0) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
}; |
@ -0,0 +1,33 @@ |
|||||||
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
//
|
||||||
|
// This file implements the callback "bridge" between Java and C++ for
|
||||||
|
// rocksdb::Statistics
|
||||||
|
|
||||||
|
#ifndef JAVA_ROCKSJNI_STATISTICSJNI_H_ |
||||||
|
#define JAVA_ROCKSJNI_STATISTICSJNI_H_ |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <set> |
||||||
|
#include <string> |
||||||
|
#include "rocksdb/statistics.h" |
||||||
|
#include "monitoring/statistics.h" |
||||||
|
|
||||||
|
namespace rocksdb { |
||||||
|
|
||||||
|
class StatisticsJni : public StatisticsImpl { |
||||||
|
public: |
||||||
|
StatisticsJni(std::shared_ptr<Statistics> stats); |
||||||
|
StatisticsJni(std::shared_ptr<Statistics> stats, |
||||||
|
const std::set<uint32_t> ignore_histograms); |
||||||
|
virtual bool HistEnabledForType(uint32_t type) const override; |
||||||
|
|
||||||
|
private: |
||||||
|
const std::set<uint32_t> m_ignore_histograms; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace rocksdb
|
||||||
|
|
||||||
|
#endif // JAVA_ROCKSJNI_STATISTICSJNI_H_
|
@ -0,0 +1,65 @@ |
|||||||
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
|
||||||
|
package org.rocksdb; |
||||||
|
|
||||||
|
/** |
||||||
|
* The level of Statistics to report. |
||||||
|
*/ |
||||||
|
public enum StatsLevel { |
||||||
|
/** |
||||||
|
* Collect all stats except time inside mutex lock AND time spent on |
||||||
|
* compression. |
||||||
|
*/ |
||||||
|
EXCEPT_DETAILED_TIMERS((byte) 0x0), |
||||||
|
|
||||||
|
/** |
||||||
|
* Collect all stats except the counters requiring to get time inside the |
||||||
|
* mutex lock. |
||||||
|
*/ |
||||||
|
EXCEPT_TIME_FOR_MUTEX((byte) 0x1), |
||||||
|
|
||||||
|
/** |
||||||
|
* Collect all stats, including measuring duration of mutex operations. |
||||||
|
* |
||||||
|
* If getting time is expensive on the platform to run, it can |
||||||
|
* reduce scalability to more threads, especially for writes. |
||||||
|
*/ |
||||||
|
ALL((byte) 0x2); |
||||||
|
|
||||||
|
private final byte value; |
||||||
|
|
||||||
|
StatsLevel(final byte value) { |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* <p>Returns the byte value of the enumerations value.</p> |
||||||
|
* |
||||||
|
* @return byte representation |
||||||
|
*/ |
||||||
|
public byte getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get StatsLevel by byte value. |
||||||
|
* |
||||||
|
* @param value byte representation of StatsLevel. |
||||||
|
* |
||||||
|
* @return {@link org.rocksdb.StatsLevel} instance. |
||||||
|
* @throws java.lang.IllegalArgumentException if an invalid |
||||||
|
* value is provided. |
||||||
|
*/ |
||||||
|
public static StatsLevel getStatsLevel(final byte value) { |
||||||
|
for (final StatsLevel statsLevel : StatsLevel.values()) { |
||||||
|
if (statsLevel.getValue() == value){ |
||||||
|
return statsLevel; |
||||||
|
} |
||||||
|
} |
||||||
|
throw new IllegalArgumentException( |
||||||
|
"Illegal value provided for InfoLogLevel."); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,160 @@ |
|||||||
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||||||
|
// This source code is licensed under the BSD-style license found in the
|
||||||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
|
||||||
|
package org.rocksdb; |
||||||
|
|
||||||
|
import org.junit.ClassRule; |
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.rules.TemporaryFolder; |
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
public class StatisticsTest { |
||||||
|
|
||||||
|
@ClassRule |
||||||
|
public static final RocksMemoryResource rocksMemoryResource = |
||||||
|
new RocksMemoryResource(); |
||||||
|
|
||||||
|
@Rule |
||||||
|
public TemporaryFolder dbFolder = new TemporaryFolder(); |
||||||
|
|
||||||
|
@Test |
||||||
|
public void statsLevel() throws RocksDBException { |
||||||
|
final Statistics statistics = new Statistics(); |
||||||
|
statistics.setStatsLevel(StatsLevel.ALL); |
||||||
|
assertThat(statistics.statsLevel()).isEqualTo(StatsLevel.ALL); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getTickerCount() throws RocksDBException { |
||||||
|
try (final Statistics statistics = new Statistics(); |
||||||
|
final Options opt = new Options() |
||||||
|
.setStatistics(statistics) |
||||||
|
.setCreateIfMissing(true); |
||||||
|
final RocksDB db = RocksDB.open(opt, |
||||||
|
dbFolder.getRoot().getAbsolutePath())) { |
||||||
|
|
||||||
|
final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); |
||||||
|
final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); |
||||||
|
|
||||||
|
db.put(key, value); |
||||||
|
for(int i = 0; i < 10; i++) { |
||||||
|
db.get(key); |
||||||
|
} |
||||||
|
|
||||||
|
assertThat(statistics.getTickerCount(TickerType.BYTES_READ)).isGreaterThan(0); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getAndResetTickerCount() throws RocksDBException { |
||||||
|
try (final Statistics statistics = new Statistics(); |
||||||
|
final Options opt = new Options() |
||||||
|
.setStatistics(statistics) |
||||||
|
.setCreateIfMissing(true); |
||||||
|
final RocksDB db = RocksDB.open(opt, |
||||||
|
dbFolder.getRoot().getAbsolutePath())) { |
||||||
|
|
||||||
|
final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); |
||||||
|
final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); |
||||||
|
|
||||||
|
db.put(key, value); |
||||||
|
for(int i = 0; i < 10; i++) { |
||||||
|
db.get(key); |
||||||
|
} |
||||||
|
|
||||||
|
final long read = statistics.getAndResetTickerCount(TickerType.BYTES_READ); |
||||||
|
assertThat(read).isGreaterThan(0); |
||||||
|
|
||||||
|
final long readAfterReset = statistics.getTickerCount(TickerType.BYTES_READ); |
||||||
|
assertThat(readAfterReset).isLessThan(read); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getHistogramData() throws RocksDBException { |
||||||
|
try (final Statistics statistics = new Statistics(); |
||||||
|
final Options opt = new Options() |
||||||
|
.setStatistics(statistics) |
||||||
|
.setCreateIfMissing(true); |
||||||
|
final RocksDB db = RocksDB.open(opt, |
||||||
|
dbFolder.getRoot().getAbsolutePath())) { |
||||||
|
|
||||||
|
final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); |
||||||
|
final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); |
||||||
|
|
||||||
|
db.put(key, value); |
||||||
|
for(int i = 0; i < 10; i++) { |
||||||
|
db.get(key); |
||||||
|
} |
||||||
|
|
||||||
|
final HistogramData histogramData = statistics.getHistogramData(HistogramType.BYTES_PER_READ); |
||||||
|
assertThat(histogramData).isNotNull(); |
||||||
|
assertThat(histogramData.getAverage()).isGreaterThan(0); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void getHistogramString() throws RocksDBException { |
||||||
|
try (final Statistics statistics = new Statistics(); |
||||||
|
final Options opt = new Options() |
||||||
|
.setStatistics(statistics) |
||||||
|
.setCreateIfMissing(true); |
||||||
|
final RocksDB db = RocksDB.open(opt, |
||||||
|
dbFolder.getRoot().getAbsolutePath())) { |
||||||
|
|
||||||
|
final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); |
||||||
|
final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); |
||||||
|
|
||||||
|
for(int i = 0; i < 10; i++) { |
||||||
|
db.put(key, value); |
||||||
|
} |
||||||
|
|
||||||
|
assertThat(statistics.getHistogramString(HistogramType.BYTES_PER_WRITE)).isNotNull(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void reset() throws RocksDBException { |
||||||
|
try (final Statistics statistics = new Statistics(); |
||||||
|
final Options opt = new Options() |
||||||
|
.setStatistics(statistics) |
||||||
|
.setCreateIfMissing(true); |
||||||
|
final RocksDB db = RocksDB.open(opt, |
||||||
|
dbFolder.getRoot().getAbsolutePath())) { |
||||||
|
|
||||||
|
final byte[] key = "some-key".getBytes(StandardCharsets.UTF_8); |
||||||
|
final byte[] value = "some-value".getBytes(StandardCharsets.UTF_8); |
||||||
|
|
||||||
|
db.put(key, value); |
||||||
|
for(int i = 0; i < 10; i++) { |
||||||
|
db.get(key); |
||||||
|
} |
||||||
|
|
||||||
|
final long read = statistics.getTickerCount(TickerType.BYTES_READ); |
||||||
|
assertThat(read).isGreaterThan(0); |
||||||
|
|
||||||
|
statistics.reset(); |
||||||
|
|
||||||
|
final long readAfterReset = statistics.getTickerCount(TickerType.BYTES_READ); |
||||||
|
assertThat(readAfterReset).isLessThan(read); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void ToString() throws RocksDBException { |
||||||
|
try (final Statistics statistics = new Statistics(); |
||||||
|
final Options opt = new Options() |
||||||
|
.setStatistics(statistics) |
||||||
|
.setCreateIfMissing(true); |
||||||
|
final RocksDB db = RocksDB.open(opt, |
||||||
|
dbFolder.getRoot().getAbsolutePath())) { |
||||||
|
assertThat(statistics.toString()).isNotNull(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue