Retry the entire bootstrap import process on failure, rather than just the download.

This commit is contained in:
CalDescent 2021-10-02 14:43:26 +01:00
parent adeb654248
commit 47b1b6daba
2 changed files with 30 additions and 24 deletions

View File

@ -517,7 +517,12 @@ public class BlockChain {
} else { } else {
// Check first block is Genesis Block // Check first block is Genesis Block
if (!isGenesisBlockValid()) { if (!isGenesisBlockValid()) {
rebuildBlockchain(); try {
rebuildBlockchain();
} catch (InterruptedException e) {
throw new DataException(String.format("Interrupted when trying to rebuild blockchain: %s", e.getMessage()));
}
} }
} }
@ -600,7 +605,7 @@ public class BlockChain {
} }
} }
private static void rebuildBlockchain() throws DataException { private static void rebuildBlockchain() throws DataException, InterruptedException {
boolean shouldBootstrap = Settings.getInstance().getBootstrap(); boolean shouldBootstrap = Settings.getInstance().getBootstrap();
if (shouldBootstrap) { if (shouldBootstrap) {
// Settings indicate that we should apply a bootstrap rather than rebuilding and syncing from genesis // Settings indicate that we should apply a bootstrap rather than rebuilding and syncing from genesis

View File

@ -27,7 +27,7 @@ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
public class Bootstrap { public class Bootstrap {
private Repository repository; private final Repository repository;
private static final Logger LOGGER = LogManager.getLogger(Bootstrap.class); private static final Logger LOGGER = LogManager.getLogger(Bootstrap.class);
@ -286,7 +286,22 @@ public class Bootstrap {
} }
} }
public void startImport() throws DataException { public void startImport() throws InterruptedException {
while (!Controller.isStopping()) {
try {
LOGGER.info("Starting import of bootstrap...");
this.doImport();
} catch (DataException e) {
LOGGER.info("Bootstrap import failed: {}", e.getMessage());
LOGGER.info("Retrying in 5 minutes");
Thread.sleep(5 * 60 * 1000L);
}
}
}
private void doImport() throws DataException {
Path path = null; Path path = null;
try { try {
Path tempDir = Files.createTempDirectory("qortal-bootstrap"); Path tempDir = Files.createTempDirectory("qortal-bootstrap");
@ -313,28 +328,14 @@ public class Bootstrap {
private void downloadToPath(Path path) throws DataException { private void downloadToPath(Path path) throws DataException {
String bootstrapUrl = "http://bootstrap.qortal.org/bootstrap.7z"; String bootstrapUrl = "http://bootstrap.qortal.org/bootstrap.7z";
while (!Controller.isStopping()) { try {
try { LOGGER.info("Downloading bootstrap...");
LOGGER.info("Downloading bootstrap..."); InputStream in = new URL(bootstrapUrl).openStream();
InputStream in = new URL(bootstrapUrl).openStream(); Files.copy(in, path, REPLACE_EXISTING);
Files.copy(in, path, REPLACE_EXISTING);
break;
} catch (IOException e) { } catch (IOException e) {
LOGGER.info("Unable to download bootstrap: {}", e.getMessage()); throw new DataException(String.format("Unable to download bootstrap: {}", e.getMessage()));
LOGGER.info("Retrying in 5 minutes");
try {
repository.discardChanges();
Thread.sleep(5 * 60 * 1000L);
} catch (InterruptedException e2) {
break;
}
}
} }
// It's best to throw an exception on all failures, even though we're most likely just stopping
throw new DataException("Unable to download bootstrap");
} }
public void importFromPath(Path path) throws InterruptedException, DataException, IOException { public void importFromPath(Path path) throws InterruptedException, DataException, IOException {