Browse Source

Faster Synchronizer shutdown by checking Controller.isStopping()

pull/67/head
catbref 5 years ago
parent
commit
12f9ecaaca
  1. 8
      src/main/java/org/qortal/controller/Controller.java
  2. 18
      src/main/java/org/qortal/controller/Synchronizer.java

8
src/main/java/org/qortal/controller/Controller.java

@ -241,6 +241,10 @@ public class Controller extends Thread {
return this.savedArgs; return this.savedArgs;
} }
/* package */ static boolean isStopping() {
return isStopping;
}
// Entry point // Entry point
public static void main(String[] args) { public static void main(String[] args) {
@ -545,6 +549,10 @@ public class Controller extends Thread {
LOGGER.debug(() -> String.format("Failed to synchronize with peer %s (%s)", peer, syncResult.name())); LOGGER.debug(() -> String.format("Failed to synchronize with peer %s (%s)", peer, syncResult.name()));
break; break;
case SHUTTING_DOWN:
// Just quietly exit
break;
case OK: case OK:
requestSysTrayUpdate = true; requestSysTrayUpdate = true;
// fall-through... // fall-through...

18
src/main/java/org/qortal/controller/Synchronizer.java

@ -45,7 +45,7 @@ public class Synchronizer {
private static Synchronizer instance; private static Synchronizer instance;
public enum SynchronizationResult { public enum SynchronizationResult {
OK, NOTHING_TO_DO, GENESIS_ONLY, NO_COMMON_BLOCK, TOO_DIVERGENT, NO_REPLY, INFERIOR_CHAIN, INVALID_DATA, NO_BLOCKCHAIN_LOCK, REPOSITORY_ISSUE; OK, NOTHING_TO_DO, GENESIS_ONLY, NO_COMMON_BLOCK, TOO_DIVERGENT, NO_REPLY, INFERIOR_CHAIN, INVALID_DATA, NO_BLOCKCHAIN_LOCK, REPOSITORY_ISSUE, SHUTTING_DOWN;
} }
// Constructors // Constructors
@ -94,6 +94,9 @@ public class Synchronizer {
ourInitialHeight, Base58.encode(ourLastBlockSignature), ourLatestBlockData.getTimestamp())); ourInitialHeight, Base58.encode(ourLastBlockSignature), ourLatestBlockData.getTimestamp()));
List<BlockSummaryData> peerBlockSummaries = fetchSummariesFromCommonBlock(repository, peer, ourInitialHeight); List<BlockSummaryData> peerBlockSummaries = fetchSummariesFromCommonBlock(repository, peer, ourInitialHeight);
if (peerBlockSummaries == null && Controller.isStopping())
return SynchronizationResult.SHUTTING_DOWN;
if (peerBlockSummaries == null) { if (peerBlockSummaries == null) {
LOGGER.info(String.format("Error while trying to find common block with peer %s", peer)); LOGGER.info(String.format("Error while trying to find common block with peer %s", peer));
return SynchronizationResult.NO_REPLY; return SynchronizationResult.NO_REPLY;
@ -154,6 +157,9 @@ public class Synchronizer {
int peerBlockCount = peerHeight - commonBlockHeight; int peerBlockCount = peerHeight - commonBlockHeight;
while (peerBlockSummaries.size() < peerBlockCount) { while (peerBlockSummaries.size() < peerBlockCount) {
if (Controller.isStopping())
return SynchronizationResult.SHUTTING_DOWN;
int lastSummaryHeight = commonBlockHeight + peerBlockSummaries.size(); int lastSummaryHeight = commonBlockHeight + peerBlockSummaries.size();
byte[] previousSignature; byte[] previousSignature;
if (peerBlockSummaries.isEmpty()) if (peerBlockSummaries.isEmpty())
@ -212,6 +218,9 @@ public class Synchronizer {
LOGGER.debug(String.format("Orphaning blocks back to common block height %d, sig %.8s", commonBlockHeight, commonBlockSig58)); LOGGER.debug(String.format("Orphaning blocks back to common block height %d, sig %.8s", commonBlockHeight, commonBlockSig58));
while (ourHeight > commonBlockHeight) { while (ourHeight > commonBlockHeight) {
if (Controller.isStopping())
return SynchronizationResult.SHUTTING_DOWN;
BlockData blockData = repository.getBlockRepository().fromHeight(ourHeight); BlockData blockData = repository.getBlockRepository().fromHeight(ourHeight);
Block block = new Block(repository, blockData); Block block = new Block(repository, blockData);
block.orphan(); block.orphan();
@ -232,6 +241,9 @@ public class Synchronizer {
List<byte[]> peerBlockSignatures = peerBlockSummaries.stream().map(BlockSummaryData::getSignature).collect(Collectors.toList()); List<byte[]> peerBlockSignatures = peerBlockSummaries.stream().map(BlockSummaryData::getSignature).collect(Collectors.toList());
while (ourHeight < peerHeight && ourHeight < maxBatchHeight) { while (ourHeight < peerHeight && ourHeight < maxBatchHeight) {
if (Controller.isStopping())
return SynchronizationResult.SHUTTING_DOWN;
// Do we need more signatures? // Do we need more signatures?
if (peerBlockSignatures.isEmpty()) { if (peerBlockSignatures.isEmpty()) {
int numberRequested = maxBatchHeight - ourHeight; int numberRequested = maxBatchHeight - ourHeight;
@ -331,6 +343,10 @@ public class Synchronizer {
BlockData testBlockData = null; BlockData testBlockData = null;
while (testHeight >= 1) { while (testHeight >= 1) {
// Are we shutting down?
if (Controller.isStopping())
return null;
// Fetch our block signature at this height // Fetch our block signature at this height
testBlockData = repository.getBlockRepository().fromHeight(testHeight); testBlockData = repository.getBlockRepository().fromHeight(testHeight);
if (testBlockData == null) { if (testBlockData == null) {

Loading…
Cancel
Save