Skip to content

Commit

Permalink
feat(db): optimize the auto-stop logic
Browse files Browse the repository at this point in the history
  1. print an error and exit when the parameter is illegal
  2. print an error and exit when more than one parameter is set
  • Loading branch information
halibobo1205 committed Dec 20, 2022
1 parent eb0065c commit 4aa1604
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
87 changes: 66 additions & 21 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.bouncycastle.util.encoders.Hex;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.api.GrpcAPI.TransactionInfoList;
Expand Down Expand Up @@ -537,28 +538,14 @@ public void init() {
//initActuatorCreator
ActuatorCreator.init();
TransactionRegister.registerActuator();

long exitHeight = CommonParameter.getInstance().getShutdownBlockHeight();
long exitCount = CommonParameter.getInstance().getShutdownBlockCount();

if (exitHeight > 0 && exitHeight < headNum) {
logger.info("ShutDownBlockHeight {} is less than headNum {}, reset it to -1.",
exitHeight, headNum);
CommonParameter.getInstance().setShutdownBlockHeight(-1);
exitHeight = -1;
}

if (exitCount > 0 && (exitHeight < 0 || exitHeight > headNum + exitCount)) {
CommonParameter.getInstance().setShutdownBlockHeight(headNum + exitCount);
// init auto-stop
try {
initAutoStop();
} catch (IllegalArgumentException e) {
logger.error("Auto-stop params error: {}", e.getMessage());
System.exit(1);
}

if (CommonParameter.getInstance().getShutdownBlockHeight() < headNum) {
logger.info("ShutDownBlockHeight {} is less than headNum {}, ignored.",
CommonParameter.getInstance().getShutdownBlockHeight(), headNum);
CommonParameter.getInstance().setShutdownBlockHeight(-1);
}
// init
latestSolidityNumShutDown = CommonParameter.getInstance().getShutdownBlockHeight();
maxFlushCount = CommonParameter.getInstance().getStorage().getMaxFlushCount();
}

Expand Down Expand Up @@ -684,6 +671,64 @@ private void initWitness() {
});
}

/**
* init auto-stop, check params
*/
private void initAutoStop() {
final long headNum = chainBaseManager.getHeadBlockNum();
final long headTime = chainBaseManager.getHeadBlockTimeStamp();
final long exitHeight = CommonParameter.getInstance().getShutdownBlockHeight();
final long exitCount = CommonParameter.getInstance().getShutdownBlockCount();
final CronExpression blockTime = Args.getInstance().getShutdownBlockTime();

if (exitHeight > 0 && exitHeight < headNum) {
throw new IllegalArgumentException(
String.format("shutDownBlockHeight %d is less than headNum %d", exitHeight, headNum));
}

if (exitCount == 0) {
throw new IllegalArgumentException(
String.format("shutDownBlockCount %d is less than 1", exitCount));
}

if (blockTime != null) {
if (blockTime.getNextValidTimeAfter(new Date(headTime)) == null) {
throw new IllegalArgumentException(
String.format("shutDownBlockTime %s is illegal", blockTime));
}
}

if (exitHeight > 0 && exitCount > 0) {
throw new IllegalArgumentException(
String.format("shutDownBlockHeight %d and shutDownBlockCount %d set both",
exitHeight, exitCount));
}

if (exitHeight > 0 && blockTime != null) {
throw new IllegalArgumentException(
String.format("shutDownBlockHeight %d and shutDownBlockTime %s set both",
exitHeight, blockTime));
}

if (exitCount > 0 && blockTime != null) {
throw new IllegalArgumentException(
String.format("shutDownBlockCount %d and shutDownBlockTime %s set both",
exitCount, blockTime));
}

if (exitHeight == headNum) {
logger.info("Auto-stop hit: shutDownBlockHeight: {}, currentHeaderNum: {}, exit now",
exitHeight, headNum);
System.exit(0);
}

if (exitCount > 0) {
CommonParameter.getInstance().setShutdownBlockHeight(headNum + exitCount);
}
// init
latestSolidityNumShutDown = CommonParameter.getInstance().getShutdownBlockHeight();
}

public AccountStore getAccountStore() {
return chainBaseManager.getAccountStore();
}
Expand Down Expand Up @@ -971,7 +1016,7 @@ private void applyBlock(BlockCapsule block, List<TransactionCapsule> txs)
revokingStore.setMaxFlushCount(maxFlushCount);
if (Args.getInstance().getShutdownBlockTime() != null
&& Args.getInstance().getShutdownBlockTime().getNextValidTimeAfter(
new Date(block.getTimeStamp() - maxFlushCount * 1000 * 3))
new Date(block.getTimeStamp() - maxFlushCount * 1000 * 3L))
.compareTo(new Date(block.getTimeStamp())) <= 0) {
revokingStore.setMaxFlushCount(SnapshotManager.DEFAULT_MIN_FLUSH_COUNT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public class TronNetDelegate {

private Thread hitThread;

// for Test
@Setter
private volatile boolean test = false;
private volatile boolean exit = true;

private Cache<BlockId, Long> freshBlockId = CacheBuilder.newBuilder()
.maximumSize(blockIdCacheSize).expireAfterWrite(1, TimeUnit.HOURS)
Expand All @@ -107,7 +106,7 @@ public void init() {
hitThread = new Thread(() -> {
LockSupport.park();
// to Guarantee Some other thread invokes unpark with the current thread as the target
if (hitDown && !test) {
if (hitDown && exit) {
System.exit(0);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class BlockHeightStopTest extends ConditionallyStopTest {

protected void initParameter(CommonParameter parameter) {
parameter.setShutdownBlockHeight(height);
// will ignore
parameter.setShutdownBlockCount(128);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class BlockSyncCountStopTest extends ConditionallyStopTest {

protected void initParameter(CommonParameter parameter) {
parameter.setShutdownBlockCount(sync);
// will ignore
parameter.setShutdownBlockHeight(1024);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public class BlockTimeStopTest extends ConditionallyStopTest {

protected void initParameter(CommonParameter parameter) {
parameter.setShutdownBlockTime(cronExpression);
// will ignore
parameter.setShutdownBlockHeight(48);
// will ignore
parameter.setShutdownBlockCount(32);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void init() throws Exception {
consensusService.start();
chainManager = dbManager.getChainBaseManager();
tronNetDelegate = context.getBean(TronNetDelegate.class);
tronNetDelegate.setTest(true);
tronNetDelegate.setExit(false);
currentHeader = dbManager.getDynamicPropertiesStore()
.getLatestBlockHeaderNumberFromDB();
}
Expand Down

0 comments on commit 4aa1604

Please sign in to comment.