From 85910573a367e7ea40d2df34a68eef365f648390 Mon Sep 17 00:00:00 2001 From: kennycud Date: Tue, 22 Oct 2024 11:30:39 -0700 Subject: [PATCH] broader exception handling and added verbosity to the logging --- .../controller/repository/AtStatesPruner.java | 104 +++++++++--------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/src/main/java/org/qortal/controller/repository/AtStatesPruner.java b/src/main/java/org/qortal/controller/repository/AtStatesPruner.java index f06efdb8..19923d68 100644 --- a/src/main/java/org/qortal/controller/repository/AtStatesPruner.java +++ b/src/main/java/org/qortal/controller/repository/AtStatesPruner.java @@ -46,72 +46,74 @@ public class AtStatesPruner implements Runnable { repository.saveChanges(); while (!Controller.isStopping()) { - repository.discardChanges(); + try { + repository.discardChanges(); - Thread.sleep(Settings.getInstance().getAtStatesPruneInterval()); + Thread.sleep(Settings.getInstance().getAtStatesPruneInterval()); - BlockData chainTip = Controller.getInstance().getChainTip(); - if (chainTip == null || NTP.getTime() == null) - continue; + BlockData chainTip = Controller.getInstance().getChainTip(); + if (chainTip == null || NTP.getTime() == null) + continue; - // Don't even attempt if we're mid-sync as our repository requests will be delayed for ages - if (Synchronizer.getInstance().isSynchronizing()) - continue; + // Don't even attempt if we're mid-sync as our repository requests will be delayed for ages + if (Synchronizer.getInstance().isSynchronizing()) + continue; - // Prune AT states for all blocks up until our latest minus pruneBlockLimit - final int ourLatestHeight = chainTip.getHeight(); - int upperPrunableHeight = ourLatestHeight - Settings.getInstance().getPruneBlockLimit(); + // Prune AT states for all blocks up until our latest minus pruneBlockLimit + final int ourLatestHeight = chainTip.getHeight(); + int upperPrunableHeight = ourLatestHeight - Settings.getInstance().getPruneBlockLimit(); - // In archive mode we are only allowed to trim blocks that have already been archived - if (archiveMode) { - upperPrunableHeight = repository.getBlockArchiveRepository().getBlockArchiveHeight() - 1; + // In archive mode we are only allowed to trim blocks that have already been archived + if (archiveMode) { + upperPrunableHeight = repository.getBlockArchiveRepository().getBlockArchiveHeight() - 1; - // TODO: validate that the actual archived data exists before pruning it? - } + // TODO: validate that the actual archived data exists before pruning it? + } - int upperBatchHeight = pruneStartHeight + Settings.getInstance().getAtStatesPruneBatchSize(); - int upperPruneHeight = Math.min(upperBatchHeight, upperPrunableHeight); + int upperBatchHeight = pruneStartHeight + Settings.getInstance().getAtStatesPruneBatchSize(); + int upperPruneHeight = Math.min(upperBatchHeight, upperPrunableHeight); - if (pruneStartHeight >= upperPruneHeight) - continue; + if (pruneStartHeight >= upperPruneHeight) + continue; - LOGGER.debug(String.format("Pruning AT states between blocks %d and %d...", pruneStartHeight, upperPruneHeight)); + LOGGER.info(String.format("Pruning AT states between blocks %d and %d...", pruneStartHeight, upperPruneHeight)); - int numAtStatesPruned = repository.getATRepository().pruneAtStates(pruneStartHeight, upperPruneHeight); - repository.saveChanges(); - int numAtStateDataRowsTrimmed = repository.getATRepository().trimAtStates( - pruneStartHeight, upperPruneHeight, Settings.getInstance().getAtStatesTrimLimit()); - repository.saveChanges(); - - if (numAtStatesPruned > 0 || numAtStateDataRowsTrimmed > 0) { - final int finalPruneStartHeight = pruneStartHeight; - LOGGER.debug(() -> String.format("Pruned %d AT state%s between blocks %d and %d", - numAtStatesPruned, (numAtStatesPruned != 1 ? "s" : ""), - finalPruneStartHeight, upperPruneHeight)); - } else { - // Can we move onto next batch? - if (upperPrunableHeight > upperBatchHeight) { - pruneStartHeight = upperBatchHeight; - repository.getATRepository().setAtPruneHeight(pruneStartHeight); - maxLatestAtStatesHeight = PruneManager.getMaxHeightForLatestAtStates(repository); - repository.getATRepository().rebuildLatestAtStates(maxLatestAtStatesHeight); - repository.saveChanges(); + int numAtStatesPruned = repository.getATRepository().pruneAtStates(pruneStartHeight, upperPruneHeight); + repository.saveChanges(); + int numAtStateDataRowsTrimmed = repository.getATRepository().trimAtStates( + pruneStartHeight, upperPruneHeight, Settings.getInstance().getAtStatesTrimLimit()); + repository.saveChanges(); + if (numAtStatesPruned > 0 || numAtStateDataRowsTrimmed > 0) { final int finalPruneStartHeight = pruneStartHeight; - LOGGER.debug(() -> String.format("Bumping AT state base prune height to %d", finalPruneStartHeight)); - } - else { - // We've pruned up to the upper prunable height - // Back off for a while to save CPU for syncing - repository.discardChanges(); - Thread.sleep(5*60*1000L); + LOGGER.info(() -> String.format("Pruned %d AT state%s between blocks %d and %d", + numAtStatesPruned, (numAtStatesPruned != 1 ? "s" : ""), + finalPruneStartHeight, upperPruneHeight)); + } else { + // Can we move onto next batch? + if (upperPrunableHeight > upperBatchHeight) { + pruneStartHeight = upperBatchHeight; + repository.getATRepository().setAtPruneHeight(pruneStartHeight); + maxLatestAtStatesHeight = PruneManager.getMaxHeightForLatestAtStates(repository); + repository.getATRepository().rebuildLatestAtStates(maxLatestAtStatesHeight); + repository.saveChanges(); + + final int finalPruneStartHeight = pruneStartHeight; + LOGGER.info(() -> String.format("Bumping AT state base prune height to %d", finalPruneStartHeight)); + } + else { + // We've pruned up to the upper prunable height + // Back off for a while to save CPU for syncing + repository.discardChanges(); + Thread.sleep(5*60*1000L); + } } + } catch (Exception e) { + LOGGER.warn("Pruning stopped working. Trying again. Report this error immediately to the developers.", e); } } - } catch (DataException e) { - LOGGER.warn(String.format("Repository issue trying to prune AT states: %s", e.getMessage())); - } catch (InterruptedException e) { - // Time to exit + } catch (Exception e) { + LOGGER.error("Pruning is not working! Not trying again. Restart ASAP. Report this error immediately to the developers.", e); } }