Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(db): node conditionalized stop #4325

Merged
6 changes: 6 additions & 0 deletions chainbase/src/main/java/org/tron/core/db/TronDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public void close() {
public abstract T get(byte[] key)
throws InvalidProtocolBufferException, ItemNotFoundException, BadItemException;

@Override
public T getFromRoot(byte[] key)
throws InvalidProtocolBufferException, BadItemException, ItemNotFoundException {
return get(key);
}

public T getUnchecked(byte[] key) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public T getUnchecked(byte[] key) {
}
}

@Override
public T getFromRoot(byte[] key) throws ItemNotFoundException, BadItemException{
return of(revokingDB.getFromRoot(key)) ;

}

public T of(byte[] value) throws BadItemException {
try {
Constructor constructor = token.getRawType().getConstructor(byte[].class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface IRevokingDB extends Iterable<Map.Entry<byte[], byte[]>> {

byte[] get(byte[] key) throws ItemNotFoundException;

byte[] getFromRoot(byte[] key) throws ItemNotFoundException;

byte[] getUnchecked(byte[] key);

void close();
Expand Down
9 changes: 9 additions & 0 deletions chainbase/src/main/java/org/tron/core/db2/core/Chainbase.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ public byte[] get(byte[] key) throws ItemNotFoundException {
return value;
}

@Override
public byte[] getFromRoot(byte[] key) throws ItemNotFoundException {
byte[] value = head().getRoot().get(key);
if (value == null) {
throw new ItemNotFoundException();
}
return value;
}

@Override
public byte[] getUnchecked(byte[] key) {
return head().get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public interface ITronChainBase<T> extends Iterable<Entry<byte[], T>>, Quitable

T get(byte[] key) throws InvalidProtocolBufferException, ItemNotFoundException, BadItemException;

T getFromRoot(byte[] key) throws InvalidProtocolBufferException, ItemNotFoundException,
BadItemException;

T getUnchecked(byte[] key);

boolean has(byte[] key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public byte[] get(byte[] key) throws ItemNotFoundException {
return value;
}

@Override
public byte[] getFromRoot(byte[] key) throws ItemNotFoundException {
return get(key);
}

@Override
public byte[] getUnchecked(byte[] key) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.tron.core.config.Parameter;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.db.TronStoreWithRevoking;
import org.tron.core.exception.BadItemException;
import org.tron.core.exception.ItemNotFoundException;

@Slf4j(topic = "DB")
@Component
Expand Down Expand Up @@ -1983,6 +1985,19 @@ public long getLatestBlockHeaderNumber() {
() -> new IllegalArgumentException("not found latest block header number"));
}

public long getLatestBlockHeaderNumberFromDB() {
try {
return Optional.ofNullable(getFromRoot(LATEST_BLOCK_HEADER_NUMBER))
.map(BytesCapsule::getData)
.map(ByteArray::toLong)
.orElseThrow(
() -> new IllegalArgumentException("not found latest block header number"));
} catch (ItemNotFoundException | BadItemException e) {
logger.error("{}", e);
}
return -1;
}

public int getStateFlag() {
return Optional.ofNullable(getUnchecked(STATE_FLAG))
.map(BytesCapsule::getData)
Expand Down
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies {
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
compile group: leveldbGroup, name: leveldbName, version: leveldbVersion
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10'
// https://mvnrepository.com/artifact/org.quartz-scheduler/quartz
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.2'
compile project(":protocol")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import org.quartz.CronExpression;
import org.tron.common.args.GenesisBlock;
import org.tron.common.config.DbBackupConfig;
import org.tron.common.logsfilter.EventPluginConfig;
Expand Down Expand Up @@ -512,6 +513,18 @@ public class CommonParameter {
@Setter
public List<String> disabledApiList;

@Getter
@Setter
public CronExpression shutdownBlockTime = null;

@Getter
@Setter
public long shutdownBlockHeight = -1;

@Getter
@Setter
public long shutdownBlockCount = -1;

private static double calcMaxTimeRatio() {
//return max(2.0, min(5.0, 5 * 4.0 / max(Runtime.getRuntime().availableProcessors(), 1)));
return 5.0;
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/java/org/tron/core/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,8 @@ public class Constant {
public static final String ALLOW_ACCOUNT_ASSET_OPTIMIZATION = "committee.allowAccountAssetOptimization";

public static final String LOCAL_HOST = "127.0.0.1";

public static final String NODE_SHUTDOWN_BLOCK_TIME = "node.shutdown.BlockTime";
public static final String NODE_SHUTDOWN_BLOCK_HEIGHT = "node.shutdown.BlockHeight";
public static final String NODE_SHUTDOWN_BLOCK_COUNT = "node.shutdown.BlockCount";
}
92 changes: 60 additions & 32 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.Socket;
import java.net.URL;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -34,6 +35,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.common.args.Account;
Expand Down Expand Up @@ -77,14 +79,15 @@ public class Args extends CommonParameter {

@Autowired(required = false)
@Getter
private static ConcurrentHashMap<Long, BlockingQueue<ContractLogTrigger>>
private static final ConcurrentHashMap<Long, BlockingQueue<ContractLogTrigger>>
solidityContractLogTriggerMap = new ConcurrentHashMap<>();

@Autowired(required = false)
@Getter
private static ConcurrentHashMap<Long, BlockingQueue<ContractEventTrigger>>
private static final ConcurrentHashMap<Long, BlockingQueue<ContractEventTrigger>>
solidityContractEventTriggerMap = new ConcurrentHashMap<>();


public static void clearParam() {
PARAMETER.outputDirectory = "output-directory";
PARAMETER.help = false;
Expand Down Expand Up @@ -198,6 +201,10 @@ public static void clearParam() {
PARAMETER.openTransactionSort = false;
PARAMETER.allowAccountAssetOptimization = 0;
PARAMETER.disabledApiList = Collections.emptyList();
PARAMETER.shutdownBlockTime = null;
PARAMETER.shutdownBlockHeight = -1;
PARAMETER.shutdownBlockCount = -1;

}

/**
Expand Down Expand Up @@ -341,7 +348,7 @@ public static void setParam(final String[] args, final String confFileName) {
.filter(StringUtils::isNotEmpty)
.orElse(Storage.getDbEngineFromConfig(config)));

if (Constant.ROCKSDB.equals(PARAMETER.storage.getDbEngine().toUpperCase())
if (Constant.ROCKSDB.equalsIgnoreCase(PARAMETER.storage.getDbEngine())
&& PARAMETER.storage.getDbVersion() == 1) {
throw new RuntimeException("db.version = 1 is not supported by ROCKSDB engine.");
}
Expand Down Expand Up @@ -637,9 +644,8 @@ public static void setParam(final String[] args, final String confFileName) {
? config.getLong(Constant.NODE_PENDING_TRANSACTION_TIMEOUT) : 60_000;

PARAMETER.needToUpdateAsset =
config.hasPath(Constant.STORAGE_NEEDTO_UPDATE_ASSET) ? config
.getBoolean(Constant.STORAGE_NEEDTO_UPDATE_ASSET)
: true;
!config.hasPath(Constant.STORAGE_NEEDTO_UPDATE_ASSET) || config
.getBoolean(Constant.STORAGE_NEEDTO_UPDATE_ASSET);
PARAMETER.trxReferenceBlock = config.hasPath(Constant.TRX_REFERENCE_BLOCK)
? config.getString(Constant.TRX_REFERENCE_BLOCK) : "head";

Expand All @@ -653,7 +659,7 @@ public static void setParam(final String[] args, final String confFileName) {
? config.getInt(Constant.NODE_RPC_MIN_EFFECTIVE_CONNECTION) : 1;

PARAMETER.trxCacheEnable = config.hasPath(Constant.NODE_RPC_TRX_CACHE_ENABLE)
&& config.getBoolean(Constant.NODE_RPC_TRX_CACHE_ENABLE);
&& config.getBoolean(Constant.NODE_RPC_TRX_CACHE_ENABLE);

PARAMETER.blockNumForEnergyLimit = config.hasPath(Constant.ENERGY_LIMIT_BLOCK_NUM)
? config.getInt(Constant.ENERGY_LIMIT_BLOCK_NUM) : 4727890L;
Expand Down Expand Up @@ -755,8 +761,8 @@ public static void setParam(final String[] args, final String confFileName) {
}

PARAMETER.allowTvmFreeze =
config.hasPath(Constant.COMMITTEE_ALLOW_TVM_FREEZE) ? config
.getInt(Constant.COMMITTEE_ALLOW_TVM_FREEZE) : 0;
config.hasPath(Constant.COMMITTEE_ALLOW_TVM_FREEZE) ? config
.getInt(Constant.COMMITTEE_ALLOW_TVM_FREEZE) : 0;

PARAMETER.allowTvmVote =
config.hasPath(Constant.COMMITTEE_ALLOW_TVM_VOTE) ? config
Expand All @@ -771,8 +777,8 @@ public static void setParam(final String[] args, final String confFileName) {
.getInt(Constant.COMMITTEE_ALLOW_TVM_COMPATIBLE_EVM) : 0;

initBackupProperty(config);
if (Constant.ROCKSDB.equals(CommonParameter
.getInstance().getStorage().getDbEngine().toUpperCase())) {
if (Constant.ROCKSDB.equalsIgnoreCase(CommonParameter
.getInstance().getStorage().getDbEngine())) {
initRocksDbBackupProperty(config);
initRocksDbSettings(config);
}
Expand All @@ -787,21 +793,21 @@ public static void setParam(final String[] args, final String confFileName) {
}

PARAMETER.metricsStorageEnable = config.hasPath(Constant.METRICS_STORAGE_ENABLE) && config
.getBoolean(Constant.METRICS_STORAGE_ENABLE);
.getBoolean(Constant.METRICS_STORAGE_ENABLE);
PARAMETER.influxDbIp = config.hasPath(Constant.METRICS_INFLUXDB_IP) ? config
.getString(Constant.METRICS_INFLUXDB_IP) : Constant.LOCAL_HOST;
.getString(Constant.METRICS_INFLUXDB_IP) : Constant.LOCAL_HOST;
PARAMETER.influxDbPort = config.hasPath(Constant.METRICS_INFLUXDB_PORT) ? config
.getInt(Constant.METRICS_INFLUXDB_PORT) : 8086;
.getInt(Constant.METRICS_INFLUXDB_PORT) : 8086;
PARAMETER.influxDbDatabase = config.hasPath(Constant.METRICS_INFLUXDB_DATABASE) ? config
.getString(Constant.METRICS_INFLUXDB_DATABASE) : "metrics";
.getString(Constant.METRICS_INFLUXDB_DATABASE) : "metrics";
PARAMETER.metricsReportInterval = config.hasPath(Constant.METRICS_REPORT_INTERVAL) ? config
.getInt(Constant.METRICS_REPORT_INTERVAL) : 10;
.getInt(Constant.METRICS_REPORT_INTERVAL) : 10;

// lite fullnode params
PARAMETER.setLiteFullNode(checkIsLiteFullNode());
PARAMETER.setOpenHistoryQueryWhenLiteFN(
config.hasPath(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN)
&& config.getBoolean(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN));
config.hasPath(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN)
&& config.getBoolean(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN));

PARAMETER.historyBalanceLookup = config.hasPath(Constant.HISTORY_BALANCE_LOOKUP) && config
.getBoolean(Constant.HISTORY_BALANCE_LOOKUP);
Expand All @@ -814,15 +820,32 @@ public static void setParam(final String[] args, final String confFileName) {
.getBoolean(Constant.OPEN_TRANSACTION_SORT);

PARAMETER.allowAccountAssetOptimization = config
.hasPath(Constant.ALLOW_ACCOUNT_ASSET_OPTIMIZATION) ? config
.getInt(Constant.ALLOW_ACCOUNT_ASSET_OPTIMIZATION) : 0;
.hasPath(Constant.ALLOW_ACCOUNT_ASSET_OPTIMIZATION) ? config
.getInt(Constant.ALLOW_ACCOUNT_ASSET_OPTIMIZATION) : 0;

PARAMETER.disabledApiList =
config.hasPath(Constant.NODE_DISABLED_API_LIST)
? config.getStringList(Constant.NODE_DISABLED_API_LIST)
.stream().map(String::toLowerCase).collect(Collectors.toList())
: Collections.emptyList();

if (config.hasPath(Constant.NODE_SHUTDOWN_BLOCK_TIME)) {
try {
PARAMETER.shutdownBlockTime = new CronExpression(config.getString(
Constant.NODE_SHUTDOWN_BLOCK_TIME));
} catch (ParseException e) {
logger.error(e.getMessage(), e);
}
}

if (config.hasPath(Constant.NODE_SHUTDOWN_BLOCK_HEIGHT)) {
PARAMETER.shutdownBlockHeight = config.getLong(Constant.NODE_SHUTDOWN_BLOCK_HEIGHT);
}

if (config.hasPath(Constant.NODE_SHUTDOWN_BLOCK_COUNT)) {
PARAMETER.shutdownBlockCount = config.getLong(Constant.NODE_SHUTDOWN_BLOCK_COUNT);
}

logConfig();
}

Expand Down Expand Up @@ -1167,6 +1190,11 @@ public static void logConfig() {
logger.info("DB version : {}", parameter.getStorage().getDbVersion());
logger.info("DB engine : {}", parameter.getStorage().getDbEngine());
logger.info("***************************************************************");
logger.info("************************ shutDown config *************************");
logger.info("ShutDown blockTime : {}", parameter.getShutdownBlockTime());
logger.info("ShutDown blockHeight : {}", parameter.getShutdownBlockHeight());
logger.info("ShutDown blockCount : {}", parameter.getShutdownBlockCount());
logger.info("***************************************************************");
logger.info("\n");
}

Expand All @@ -1179,28 +1207,18 @@ public static void setFullNodeAllowShieldedTransaction(boolean fullNodeAllowShie
*/
public static boolean checkIsLiteFullNode() {
String infoFile = Paths.get(PARAMETER.outputDirectory,
PARAMETER.storage.getDbDirectory(), Constant.INFO_FILE_NAME).toString();
PARAMETER.storage.getDbDirectory(), Constant.INFO_FILE_NAME).toString();
if (FileUtil.isExists(infoFile)) {
String value = PropUtil.readProperty(infoFile, Constant.SPLIT_BLOCK_NUM);
return !"".equals(value) && Long.parseLong(value) > 0;
}
return false;
}

/**
* get output directory.
*/
public String getOutputDirectory() {
if (!this.outputDirectory.equals("") && !this.outputDirectory.endsWith(File.separator)) {
return this.outputDirectory + File.separator;
}
return this.outputDirectory;
}

private static void witnessAddressCheck(Config config) {
if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) {
byte[] bytes = Commons
.decodeFromBase58Check(config.getString(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS));
.decodeFromBase58Check(config.getString(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS));
if (bytes != null) {
localWitnesses.setWitnessAccountAddress(bytes);
logger.debug("Got localWitnessAccountAddress from config.conf");
Expand All @@ -1209,5 +1227,15 @@ private static void witnessAddressCheck(Config config) {
}
}
}

/**
* get output directory.
*/
public String getOutputDirectory() {
if (!this.outputDirectory.equals("") && !this.outputDirectory.endsWith(File.separator)) {
return this.outputDirectory + File.separator;
}
return this.outputDirectory;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public void start() {
}

public void stop() {
logger.info("consensus service closed start.");
consensus.stop();
logger.info("consensus service closed successfully.");
}

}
Loading