Skip to content

Commit

Permalink
(feat) add rocksdb table_format_config and statistics (sofastack#295)
Browse files Browse the repository at this point in the history
* (feat) add rocksdb table_formatting_config and statistics

* (feat) add rocksdb table_formatting_config and statistics

* (feat) add open_statistics options
  • Loading branch information
fengjiachun authored and killme2008 committed Oct 15, 2019
1 parent 7fcfdb1 commit a503b5b
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void handle(final String signalName) {

try (final PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true),
StandardCharsets.UTF_8))) {
final Describer.Printer printer = new DefaultPrinter(out);
final Describer.Printer printer = new Describer.DefaultPrinter(out);
for (final Node node : nodes) {
node.describe(printer);
}
Expand All @@ -65,25 +65,4 @@ public void handle(final String signalName) {
LOG.error("Fail to describe nodes: {}.", nodes, e);
}
}

private static class DefaultPrinter implements Describer.Printer {

private final PrintWriter out;

private DefaultPrinter(PrintWriter out) {
this.out = out;
}

@Override
public Describer.Printer print(final Object x) {
this.out.print(x);
return this;
}

@Override
public Describer.Printer println(final Object x) {
this.out.println(x);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import com.alipay.sofa.jraft.storage.SnapshotExecutor;
import com.alipay.sofa.jraft.storage.impl.LogManagerImpl;
import com.alipay.sofa.jraft.storage.snapshot.SnapshotExecutorImpl;
import com.alipay.sofa.jraft.util.Describer;
import com.alipay.sofa.jraft.util.DisruptorBuilder;
import com.alipay.sofa.jraft.util.DisruptorMetricSet;
import com.alipay.sofa.jraft.util.JRaftServiceLoader;
Expand Down Expand Up @@ -2971,6 +2972,12 @@ public void describe(final Printer out) {
// replicators
out.println("replicatorGroup: ");
this.replicatorGroup.describe(out);

// log storage
if (this.logStorage instanceof Describer) {
out.println("logStorage: ");
((Describer) this.logStorage).describe(out);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class RaftOptions implements Copiable<RaftOptions> {
private boolean sync = true;
/** Sync log meta, snapshot meta and raft meta */
private boolean syncMeta = false;
/** Statistics to analyze the performance of db */
private boolean openStatistics = true;
/** Whether to enable replicator pipeline. */
private boolean replicatorPipeline = true;
/** The maximum replicator pipeline in-flight requests/responses, only valid when enable replicator pipeline. */
Expand Down Expand Up @@ -207,6 +209,14 @@ public void setSyncMeta(final boolean syncMeta) {
this.syncMeta = syncMeta;
}

public boolean isOpenStatistics() {
return openStatistics;
}

public void setOpenStatistics(boolean openStatistics) {
this.openStatistics = openStatistics;
}

@Override
public RaftOptions copy() {
final RaftOptions raftOptions = new RaftOptions();
Expand All @@ -220,6 +230,7 @@ public RaftOptions copy() {
raftOptions.setApplyBatch(this.applyBatch);
raftOptions.setSync(this.sync);
raftOptions.setSyncMeta(this.syncMeta);
raftOptions.setOpenStatistics(this.openStatistics);
raftOptions.setReplicatorPipeline(this.replicatorPipeline);
raftOptions.setMaxReplicatorInflightMsgs(this.maxReplicatorInflightMsgs);
raftOptions.setDisruptorBufferSize(this.disruptorBufferSize);
Expand All @@ -229,13 +240,13 @@ public RaftOptions copy() {

@Override
public String toString() {
return "RaftOptions{" + "maxByteCountPerRpc=" + this.maxByteCountPerRpc + ", fileCheckHole="
+ this.fileCheckHole + ", maxEntriesSize=" + this.maxEntriesSize + ", maxBodySize=" + this.maxBodySize
+ ", maxAppendBufferSize=" + this.maxAppendBufferSize + ", maxElectionDelayMs="
+ this.maxElectionDelayMs + ", electionHeartbeatFactor=" + this.electionHeartbeatFactor
+ ", applyBatch=" + this.applyBatch + ", sync=" + this.sync + ", syncMeta=" + this.syncMeta
+ ", replicatorPipeline=" + this.replicatorPipeline + ", maxReplicatorInflightMsgs="
+ this.maxReplicatorInflightMsgs + ", disruptorBufferSize=" + this.disruptorBufferSize
+ ", readOnlyOptions=" + this.readOnlyOptions + '}';
return "RaftOptions{" + "maxByteCountPerRpc=" + maxByteCountPerRpc + ", fileCheckHole=" + fileCheckHole
+ ", maxEntriesSize=" + maxEntriesSize + ", maxBodySize=" + maxBodySize + ", maxAppendBufferSize="
+ maxAppendBufferSize + ", maxElectionDelayMs=" + maxElectionDelayMs + ", electionHeartbeatFactor="
+ electionHeartbeatFactor + ", applyBatch=" + applyBatch + ", sync=" + sync + ", syncMeta=" + syncMeta
+ ", openStatistics=" + openStatistics + ", replicatorPipeline=" + replicatorPipeline
+ ", maxReplicatorInflightMsgs=" + maxReplicatorInflightMsgs + ", disruptorBufferSize="
+ disruptorBufferSize + ", disruptorPublishEventWaitTimeoutSecs=" + disruptorPublishEventWaitTimeoutSecs
+ ", enableLogEntryChecksum=" + enableLogEntryChecksum + ", readOnlyOptions=" + readOnlyOptions + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.rocksdb.BlockBasedTableConfig;
import org.rocksdb.BloomFilter;
import org.rocksdb.ColumnFamilyDescriptor;
import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.ColumnFamilyOptions;
import org.rocksdb.DBOptions;
import org.rocksdb.IndexType;
import org.rocksdb.Options;
import org.rocksdb.ReadOptions;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.rocksdb.RocksIterator;
import org.rocksdb.Statistics;
import org.rocksdb.StringAppendOperator;
import org.rocksdb.WriteBatch;
import org.rocksdb.WriteOptions;
import org.rocksdb.util.SizeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -57,6 +55,7 @@
import com.alipay.sofa.jraft.storage.LogStorage;
import com.alipay.sofa.jraft.util.Bits;
import com.alipay.sofa.jraft.util.BytesUtil;
import com.alipay.sofa.jraft.util.Describer;
import com.alipay.sofa.jraft.util.Requires;
import com.alipay.sofa.jraft.util.StorageOptionsFactory;
import com.alipay.sofa.jraft.util.Utils;
Expand All @@ -68,7 +67,7 @@
*
* 2018-Apr-06 7:27:47 AM
*/
public class RocksDBLogStorage implements LogStorage {
public class RocksDBLogStorage implements LogStorage, Describer {

private static final Logger LOG = LoggerFactory.getLogger(RocksDBLogStorage.class);

Expand All @@ -90,13 +89,15 @@ private interface WriteBatchTemplate {

private final String path;
private final boolean sync;
private final boolean openStatistics;
private RocksDB db;
private DBOptions dbOptions;
private WriteOptions writeOptions;
private final List<ColumnFamilyOptions> cfOptions = new ArrayList<>();
private ColumnFamilyHandle defaultHandle;
private ColumnFamilyHandle confHandle;
private ReadOptions totalOrderReadOptions;
private Statistics statistics;
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final Lock readLock = this.readWriteLock.readLock();
private final Lock writeLock = this.readWriteLock.writeLock();
Expand All @@ -112,31 +113,16 @@ public RocksDBLogStorage(final String path, final RaftOptions raftOptions) {
super();
this.path = path;
this.sync = raftOptions.isSync();
}

private static BlockBasedTableConfig createTableConfig() {
return new BlockBasedTableConfig() //
// Begin to use partitioned index filters
// https://github.com/facebook/rocksdb/wiki/Partitioned-Index-Filters#how-to-use-it
.setIndexType(IndexType.kTwoLevelIndexSearch) //
.setFilter(new BloomFilter(16, false)) //
.setPartitionFilters(true) //
.setMetadataBlockSize(8 * SizeUnit.KB) //
.setCacheIndexAndFilterBlocks(false) //
.setCacheIndexAndFilterBlocksWithHighPriority(true) //
.setPinL0FilterAndIndexBlocksInCache(true) //
// End of partitioned index filters settings.
.setBlockSize(4 * SizeUnit.KB)//
.setBlockCacheSize(512 * SizeUnit.MB) //
.setCacheNumShardBits(8);
this.openStatistics = raftOptions.isOpenStatistics();
}

public static DBOptions createDBOptions() {
return StorageOptionsFactory.getRocksDBOptions(RocksDBLogStorage.class);
}

public static ColumnFamilyOptions createColumnFamilyOptions() {
final BlockBasedTableConfig tConfig = createTableConfig();
final BlockBasedTableConfig tConfig = StorageOptionsFactory
.getRocksDBTableFormatConfig(RocksDBLogStorage.class);
return StorageOptionsFactory.getRocksDBColumnFamilyOptions(RocksDBLogStorage.class) //
.useFixedLengthPrefixExtractor(8) //
.setTableFormatConfig(tConfig) //
Expand All @@ -158,6 +144,10 @@ public boolean init(final LogStorageOptions opts) {
Requires.requireNonNull(this.logEntryDecoder, "Null log entry decoder");
Requires.requireNonNull(this.logEntryEncoder, "Null log entry encoder");
this.dbOptions = createDBOptions();
if (this.openStatistics) {
this.statistics = new Statistics();
this.dbOptions.setStatistics(this.statistics);
}

this.writeOptions = new WriteOptions();
this.writeOptions.setSync(this.sync);
Expand Down Expand Up @@ -318,15 +308,21 @@ public void shutdown() {
opt.close();
}
// 3. close options
this.dbOptions.close();
if (this.statistics != null) {
this.statistics.close();
}
this.writeOptions.close();
this.totalOrderReadOptions.close();
// 4. help gc.
this.cfOptions.clear();
this.db = null;
this.totalOrderReadOptions = null;
this.dbOptions = null;
this.statistics = null;
this.writeOptions = null;
this.totalOrderReadOptions = null;
this.defaultHandle = null;
this.confHandle = null;
this.db = null;
LOG.info("DB destroyed, the db path is: {}.", this.path);
} finally {
this.writeLock.unlock();
Expand Down Expand Up @@ -656,4 +652,22 @@ protected byte[] onDataAppend(final long logIndex, final byte[] value) throws IO
protected byte[] onDataGet(final long logIndex, final byte[] value) throws IOException {
return value;
}

@Override
public void describe(final Printer out) {
this.readLock.lock();
try {
if (this.db != null) {
out.println(this.db.getProperty("rocksdb.stats"));
}
out.println("");
if (this.statistics != null) {
out.println(this.statistics.toString());
}
} catch (final RocksDBException e) {
out.println(e);
} finally {
this.readLock.unlock();
}
}
}
23 changes: 23 additions & 0 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/util/Describer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.alipay.sofa.jraft.util;

import java.io.PrintWriter;

/**
* Components that implement this interface need to be able to describe
* their own state and output state information via the {@code describe} method.
Expand Down Expand Up @@ -44,4 +46,25 @@ interface Printer {
*/
Printer println(final Object x);
}

class DefaultPrinter implements Describer.Printer {

private final PrintWriter out;

public DefaultPrinter(PrintWriter out) {
this.out = out;
}

@Override
public Describer.Printer print(final Object x) {
this.out.print(x);
return this;
}

@Override
public Describer.Printer println(final Object x) {
this.out.println(x);
return this;
}
}
}
Loading

0 comments on commit a503b5b

Please sign in to comment.