Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Commit

Permalink
[PAN-2853] Create a metric tracking DB size (#1662)
Browse files Browse the repository at this point in the history
* [PAN-2853] Create a metric tracking DB size

- create a Prometheus long gauge to track the database size
- `rocksdb.live-sst-files-size` : https://github.com/facebook/rocksdb/blob/872a261ffc2a440dfe9e60d99e421e42f5f2cf5e/include/rocksdb/db.h#L685
- use `rocksdb.live-sst-files-size` property rather than  `rocksdb.total-sst-files-size` (performance purpose: see warning https://github.com/facebook/rocksdb/blob/872a261ffc2a440dfe9e60d99e421e42f5f2cf5e/include/rocksdb/db.h#L680)
- add tests to check creation of metrics

* fix wildcard import
  • Loading branch information
AbdelStark authored Jul 9, 2019
1 parent d158c04 commit 44e405c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 6 deletions.
2 changes: 2 additions & 0 deletions services/kvstore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ dependencies {
runtime 'org.apache.logging.log4j:log4j-core'

testImplementation 'junit:junit'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.assertj:assertj-core'
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ private RocksDbKeyValueStorage(
}
});

metricsSystem.createLongGauge(
PantheonMetricCategory.KVSTORE_ROCKSDB,
"rocks_db_files_size_bytes",
"Estimated database size in bytes",
() -> {
try {
return db.getLongProperty("rocksdb.live-sst-files-size");
} catch (final RocksDBException e) {
LOG.debug("Failed to get RocksDB metric", e);
return 0L;
}
});

rollbackCount =
metricsSystem
.createLabelledCounter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,118 @@
*/
package tech.pegasys.pantheon.services.kvstore;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.metrics.Counter;
import tech.pegasys.pantheon.metrics.LabelledMetric;
import tech.pegasys.pantheon.metrics.MetricsSystem;
import tech.pegasys.pantheon.metrics.OperationTimer;
import tech.pegasys.pantheon.metrics.PantheonMetricCategory;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.util.function.LongSupplier;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class RocksDbKeyValueStorageTest extends AbstractKeyValueStorageTest {

@Mock private MetricsSystem metricsSystemMock;
@Mock private LabelledMetric<OperationTimer> labelledMetricOperationTimerMock;
@Mock private LabelledMetric<Counter> labelledMetricCounterMock;
@Mock private OperationTimer operationTimerMock;
@Rule public final TemporaryFolder folder = new TemporaryFolder();

@Override
protected KeyValueStorage createStore() throws Exception {
return RocksDbKeyValueStorage.create(
RocksDbConfiguration.builder()
.databaseDir(folder.newFolder().toPath())
.useColumns(false)
.build(),
new NoOpMetricsSystem());
return RocksDbKeyValueStorage.create(config(), new NoOpMetricsSystem());
}

@Test
public void createStoreMustCreateMetrics() throws Exception {
// Prepare mocks
when(labelledMetricOperationTimerMock.labels(any())).thenReturn(operationTimerMock);
when(metricsSystemMock.createLabelledTimer(
eq(PantheonMetricCategory.KVSTORE_ROCKSDB), anyString(), anyString(), any()))
.thenReturn(labelledMetricOperationTimerMock);
when(metricsSystemMock.createLabelledCounter(
eq(PantheonMetricCategory.KVSTORE_ROCKSDB), anyString(), anyString(), any()))
.thenReturn(labelledMetricCounterMock);
// Prepare argument captors
final ArgumentCaptor<String> labelledTimersMetricsNameArgs =
ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> labelledTimersHelpArgs = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> labelledCountersMetricsNameArgs =
ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> labelledCountersHelpArgs = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> longGaugesMetricsNameArgs = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> longGaugesHelpArgs = ArgumentCaptor.forClass(String.class);

// Actual call
final KeyValueStorage keyValueStorage =
RocksDbKeyValueStorage.create(config(), metricsSystemMock);

// Assertions
assertThat(keyValueStorage).isNotNull();
verify(metricsSystemMock, times(4))
.createLabelledTimer(
eq(PantheonMetricCategory.KVSTORE_ROCKSDB),
labelledTimersMetricsNameArgs.capture(),
labelledTimersHelpArgs.capture(),
any());
assertThat(labelledTimersMetricsNameArgs.getAllValues())
.containsExactly(
"read_latency_seconds",
"remove_latency_seconds",
"write_latency_seconds",
"commit_latency_seconds");
assertThat(labelledTimersHelpArgs.getAllValues())
.containsExactly(
"Latency for read from RocksDB.",
"Latency of remove requests from RocksDB.",
"Latency for write to RocksDB.",
"Latency for commits to RocksDB.");

verify(metricsSystemMock, times(2))
.createLongGauge(
eq(PantheonMetricCategory.KVSTORE_ROCKSDB),
longGaugesMetricsNameArgs.capture(),
longGaugesHelpArgs.capture(),
any(LongSupplier.class));
assertThat(longGaugesMetricsNameArgs.getAllValues())
.containsExactly("rocks_db_table_readers_memory_bytes", "rocks_db_files_size_bytes");
assertThat(longGaugesHelpArgs.getAllValues())
.containsExactly(
"Estimated memory used for RocksDB index and filter blocks in bytes",
"Estimated database size in bytes");

verify(metricsSystemMock)
.createLabelledCounter(
eq(PantheonMetricCategory.KVSTORE_ROCKSDB),
labelledCountersMetricsNameArgs.capture(),
labelledCountersHelpArgs.capture(),
any());
assertThat(labelledCountersMetricsNameArgs.getValue()).isEqualTo("rollback_count");
assertThat(labelledCountersHelpArgs.getValue())
.isEqualTo("Number of RocksDB transactions rolled back.");
}

private RocksDbConfiguration config() throws Exception {
return RocksDbConfiguration.builder()
.databaseDir(folder.newFolder().toPath())
.useColumns(false)
.build();
}
}

0 comments on commit 44e405c

Please sign in to comment.