Skip to content

Commit

Permalink
feat(db): update leveldb and rocksdb for arm
Browse files Browse the repository at this point in the history
  • Loading branch information
halibobo1205 committed Oct 16, 2024
1 parent c136a26 commit 15036b0
Show file tree
Hide file tree
Showing 23 changed files with 493 additions and 254 deletions.
15 changes: 15 additions & 0 deletions chainbase/src/main/java/org/tron/common/storage/OptionsPicker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.tron.common.storage;

import org.tron.common.setting.RocksDbSettings;
import org.tron.common.utils.StorageUtils;

public class OptionsPicker {

protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) {
return StorageUtils.getOptionsByDbName(dbName);
}

protected org.rocksdb.Options getOptionsByDbNameForRocksDB(String dbName) {
return RocksDbSettings.getOptionsByDbName(dbName);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.common.storage.rocksdb;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;
import com.google.common.primitives.Bytes;
import java.io.File;
Expand All @@ -20,23 +21,18 @@
import java.util.stream.Collectors;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.rocksdb.BlockBasedTableConfig;
import org.rocksdb.BloomFilter;
import org.rocksdb.Checkpoint;
import org.rocksdb.DirectComparator;
import org.rocksdb.InfoLogLevel;
import org.rocksdb.Logger;
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.Status;
import org.rocksdb.WriteBatch;
import org.rocksdb.WriteOptions;
import org.slf4j.LoggerFactory;
import org.tron.common.setting.RocksDbSettings;
import org.tron.common.storage.WriteOptionsWrapper;
import org.tron.common.storage.metric.DbStat;
import org.tron.common.utils.FileUtil;
Expand All @@ -52,36 +48,28 @@
public class RocksDbDataSourceImpl extends DbStat implements DbSourceInter<byte[]>,
Iterable<Map.Entry<byte[], byte[]>>, Instance<RocksDbDataSourceImpl> {

ReadOptions readOpts;
private String dataBaseName;
private RocksDB database;
private Options options;
private volatile boolean alive;
private String parentPath;
private ReadWriteLock resetDbLock = new ReentrantReadWriteLock();
private static final String KEY_ENGINE = "ENGINE";
private static final String ROCKSDB = "ROCKSDB";
private DirectComparator comparator;
private static final org.slf4j.Logger rocksDbLogger = LoggerFactory.getLogger(ROCKSDB);

public RocksDbDataSourceImpl(String parentPath, String name, RocksDbSettings settings,
DirectComparator comparator) {
public RocksDbDataSourceImpl(String parentPath, String name, Options options) {
this.dataBaseName = name;
this.parentPath = parentPath;
this.comparator = comparator;
RocksDbSettings.setRocksDbSettings(settings);
initDB();
}

public RocksDbDataSourceImpl(String parentPath, String name, RocksDbSettings settings) {
this.dataBaseName = name;
this.parentPath = parentPath;
RocksDbSettings.setRocksDbSettings(settings);
this.options = options;
initDB();
}

@VisibleForTesting
public RocksDbDataSourceImpl(String parentPath, String name) {
this.parentPath = parentPath;
this.dataBaseName = name;
this.options = new Options().setCreateIfMissing(true);
}

public Path getDbPath() {
Expand Down Expand Up @@ -196,10 +184,6 @@ public void initDB() {
throw new RuntimeException(
String.format("failed to check database: %s, engine do not match", dataBaseName));
}
initDB(RocksDbSettings.getSettings());
}

public void initDB(RocksDbSettings settings) {
resetDbLock.writeLock().lock();
try {
if (isAlive()) {
Expand All @@ -208,81 +192,40 @@ public void initDB(RocksDbSettings settings) {
if (dataBaseName == null) {
throw new IllegalArgumentException("No name set to the dbStore");
}
options.setLogger(new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
rocksDbLogger.info("{} {}", dataBaseName, logMsg);
}
});

try (Options options = new Options()) {

// most of these options are suggested by https://github.com/facebook/rocksdb/wiki/Set-Up-Options
try {
logger.debug("Opening database {}.", dataBaseName);
final Path dbPath = getDbPath();

// general options
if (settings.isEnableStatistics()) {
options.setStatistics(new Statistics());
options.setStatsDumpPeriodSec(60);
}
options.setCreateIfMissing(true);
options.setIncreaseParallelism(1);
options.setLevelCompactionDynamicLevelBytes(true);
options.setMaxOpenFiles(settings.getMaxOpenFiles());

// general options supported user config
options.setNumLevels(settings.getLevelNumber());
options.setMaxBytesForLevelMultiplier(settings.getMaxBytesForLevelMultiplier());
options.setMaxBytesForLevelBase(settings.getMaxBytesForLevelBase());
options.setMaxBackgroundCompactions(settings.getCompactThreads());
options.setLevel0FileNumCompactionTrigger(settings.getLevel0FileNumCompactionTrigger());
options.setTargetFileSizeMultiplier(settings.getTargetFileSizeMultiplier());
options.setTargetFileSizeBase(settings.getTargetFileSizeBase());
if (comparator != null) {
options.setComparator(comparator);
if (!Files.isSymbolicLink(dbPath.getParent())) {
Files.createDirectories(dbPath.getParent());
}
options.setLogger(new Logger(options) {
@Override
protected void log(InfoLogLevel infoLogLevel, String logMsg) {
rocksDbLogger.info("{} {}", dataBaseName, logMsg);
}
});

// table options
final BlockBasedTableConfig tableCfg;
options.setTableFormatConfig(tableCfg = new BlockBasedTableConfig());
tableCfg.setBlockSize(settings.getBlockSize());
tableCfg.setBlockCache(RocksDbSettings.getCache());
tableCfg.setCacheIndexAndFilterBlocks(true);
tableCfg.setPinL0FilterAndIndexBlocksInCache(true);
tableCfg.setFilter(new BloomFilter(10, false));

// read options
readOpts = new ReadOptions();
readOpts = readOpts.setPrefixSameAsStart(true)
.setVerifyChecksums(false);

try {
logger.debug("Opening database {}.", dataBaseName);
final Path dbPath = getDbPath();

if (!Files.isSymbolicLink(dbPath.getParent())) {
Files.createDirectories(dbPath.getParent());
}

try {
database = RocksDB.open(options, dbPath.toString());
} catch (RocksDBException e) {
if (Objects.equals(e.getStatus().getCode(), Status.Code.Corruption)) {
logger.error("Database {} corrupted, please delete database directory({}) " +
"and restart.", dataBaseName, parentPath, e);
} else {
logger.error("Open Database {} failed", dataBaseName, e);
}
System.exit(1);
database = RocksDB.open(options, dbPath.toString());
} catch (RocksDBException e) {
if (Objects.equals(e.getStatus().getCode(), Status.Code.Corruption)) {
logger.error("Database {} corrupted, please delete database directory({}) "
+ "and restart.", dataBaseName, parentPath, e);
} else {
logger.error("Open Database {} failed", dataBaseName, e);
}

alive = true;
} catch (IOException ioe) {
throw new RuntimeException(
String.format("failed to init database: %s", dataBaseName), ioe);
System.exit(1);
}

logger.debug("Init DB {} done.", dataBaseName);
alive = true;
} catch (IOException ioe) {
throw new RuntimeException(
String.format("failed to init database: %s", dataBaseName), ioe);
}

logger.debug("Init DB {} done.", dataBaseName);
} finally {
resetDbLock.writeLock().unlock();
}
Expand Down Expand Up @@ -544,7 +487,8 @@ public boolean deleteDbBakPath(String dir) {

@Override
public RocksDbDataSourceImpl newInstance() {
return new RocksDbDataSourceImpl(parentPath, dataBaseName, RocksDbSettings.getSettings());
return new RocksDbDataSourceImpl(parentPath, dataBaseName,
this.options);
}


Expand Down
10 changes: 8 additions & 2 deletions chainbase/src/main/java/org/tron/common/utils/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ public static String getOutputDirectory() {
}

public static Options getOptionsByDbName(String dbName) {
Options options;
if (hasProperty(dbName)) {
return getProperty(dbName).getDbOptions();
options = getProperty(dbName).getDbOptions();
} else {
options = CommonParameter.getInstance().getStorage().newDefaultDbOptions(dbName);
}
return CommonParameter.getInstance().getStorage().newDefaultDbOptions(dbName);
if ("market_pair_price_to_order".equals(dbName)) {
options.comparator(new MarketOrderPriceComparatorForLevelDB());
}
return options;
}
}
15 changes: 3 additions & 12 deletions chainbase/src/main/java/org/tron/core/db/TronDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.iq80.leveldb.WriteOptions;
import org.rocksdb.DirectComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.storage.OptionsPicker;
import org.tron.common.storage.WriteOptionsWrapper;
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
import org.tron.common.storage.metric.DbStatService;
Expand All @@ -24,7 +24,7 @@
import org.tron.core.exception.ItemNotFoundException;

@Slf4j(topic = "DB")
public abstract class TronDatabase<T> implements ITronChainBase<T> {
public abstract class TronDatabase<T> extends OptionsPicker implements ITronChainBase<T> {

protected DbSourceInter<byte[]> dbSource;
@Getter
Expand All @@ -51,8 +51,7 @@ protected TronDatabase(String dbName) {
String parentName = Paths.get(StorageUtils.getOutputDirectoryByDbName(dbName),
CommonParameter.getInstance().getStorage().getDbDirectory()).toString();
dbSource =
new RocksDbDataSourceImpl(parentName, dbName, CommonParameter.getInstance()
.getRocksDBCustomSettings(), getDirectComparator());
new RocksDbDataSourceImpl(parentName, dbName, getOptionsByDbNameForRocksDB(dbName));
}

dbSource.initDB();
Expand All @@ -66,14 +65,6 @@ protected void init() {
protected TronDatabase() {
}

protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) {
return StorageUtils.getOptionsByDbName(dbName);
}

protected DirectComparator getDirectComparator() {
return null;
}

public DbSourceInter<byte[]> getDbSource() {
return dbSource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.iq80.leveldb.WriteOptions;
import org.rocksdb.DirectComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.storage.OptionsPicker;
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
import org.tron.common.storage.metric.DbStatService;
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
Expand All @@ -38,7 +38,7 @@


@Slf4j(topic = "DB")
public abstract class TronStoreWithRevoking<T extends ProtoCapsule> implements ITronChainBase<T> {
public abstract class TronStoreWithRevoking<T extends ProtoCapsule> extends OptionsPicker implements ITronChainBase<T> {

@Getter // only for unit test
protected IRevokingDB revokingDB;
Expand Down Expand Up @@ -69,22 +69,13 @@ protected TronStoreWithRevoking(String dbName) {
.getInstance().getStorage().getDbDirectory()).toString();
this.db = new RocksDB(
new RocksDbDataSourceImpl(parentPath,
dbName, CommonParameter.getInstance()
.getRocksDBCustomSettings(), getDirectComparator()));
dbName, getOptionsByDbNameForRocksDB(dbName)));
} else {
throw new RuntimeException(String.format("db engine %s is error", dbEngine));
}
this.revokingDB = new Chainbase(new SnapshotRoot(this.db));
}

protected org.iq80.leveldb.Options getOptionsByDbNameForLevelDB(String dbName) {
return StorageUtils.getOptionsByDbName(dbName);
}

protected DirectComparator getDirectComparator() {
return null;
}

protected TronStoreWithRevoking(DB<byte[], byte[]> db) {
this.db = db;
this.revokingDB = new Chainbase(new SnapshotRoot(db));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.tron.common.parameter.CommonParameter;
import org.tron.common.prometheus.MetricKeys;
import org.tron.common.prometheus.Metrics;
import org.tron.common.storage.OptionsPicker;
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
import org.tron.common.storage.rocksdb.RocksDbDataSourceImpl;
import org.tron.common.utils.ByteArray;
Expand All @@ -48,7 +49,7 @@
import org.tron.core.store.DynamicPropertiesStore;

@Slf4j(topic = "DB")
public class TxCacheDB implements DB<byte[], byte[]>, Flusher {
public class TxCacheDB extends OptionsPicker implements DB<byte[], byte[]>, Flusher {

// > 65_536(= 2^16) blocks, that is the number of the reference block
private static final long MAX_BLOCK_SIZE = 65536;
Expand Down Expand Up @@ -106,7 +107,7 @@ public TxCacheDB(String name, RecentTransactionStore recentTransactionStore,
if ("LEVELDB".equals(dbEngine.toUpperCase())) {
this.persistentStore = new LevelDB(
new LevelDbDataSourceImpl(StorageUtils.getOutputDirectoryByDbName(name),
name, StorageUtils.getOptionsByDbName(name),
name, getOptionsByDbNameForLevelDB(name),
new WriteOptions().sync(CommonParameter.getInstance()
.getStorage().isDbSync())));
} else if ("ROCKSDB".equals(dbEngine.toUpperCase())) {
Expand All @@ -115,9 +116,7 @@ public TxCacheDB(String name, RecentTransactionStore recentTransactionStore,
.getInstance().getStorage().getDbDirectory()).toString();

this.persistentStore = new RocksDB(
new RocksDbDataSourceImpl(parentPath,
name, CommonParameter.getInstance()
.getRocksDBCustomSettings()));
new RocksDbDataSourceImpl(parentPath, name, getOptionsByDbNameForRocksDB(name)));
} else {
throw new RuntimeException(String.format("db type: %s is not supported", dbEngine));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.iq80.leveldb.Options;
import org.rocksdb.ComparatorOptions;
import org.rocksdb.DirectComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.MarketOrderPriceComparatorForLevelDB;
import org.tron.common.utils.MarketOrderPriceComparatorForRockDB;
import org.tron.common.utils.StorageUtils;
import org.tron.core.capsule.MarketOrderIdListCapsule;
import org.tron.core.capsule.utils.MarketUtils;
import org.tron.core.db.TronStoreWithRevoking;
Expand All @@ -26,20 +20,6 @@ protected MarketPairPriceToOrderStore(@Value("market_pair_price_to_order") Strin
super(dbName);
}

@Override
protected Options getOptionsByDbNameForLevelDB(String dbName) {
Options options = StorageUtils.getOptionsByDbName(dbName);
options.comparator(new MarketOrderPriceComparatorForLevelDB());
return options;
}

//todo: to test later
@Override
protected DirectComparator getDirectComparator() {
ComparatorOptions comparatorOptions = new ComparatorOptions();
return new MarketOrderPriceComparatorForRockDB(comparatorOptions);
}

@Override
public MarketOrderIdListCapsule get(byte[] key) throws ItemNotFoundException {
byte[] value = revokingDB.get(key);
Expand Down
Loading

0 comments on commit 15036b0

Please sign in to comment.