From 73a382c18c3d2951895b3b3dd5defb51523099c3 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Tue, 12 May 2020 01:20:24 +0200 Subject: [PATCH] Added fix for syncingon xisting db --- .../impl/MilestoneSolidifierImpl.java | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/iota/iri/service/milestone/impl/MilestoneSolidifierImpl.java b/src/main/java/com/iota/iri/service/milestone/impl/MilestoneSolidifierImpl.java index 76e4edf90c..d938912963 100644 --- a/src/main/java/com/iota/iri/service/milestone/impl/MilestoneSolidifierImpl.java +++ b/src/main/java/com/iota/iri/service/milestone/impl/MilestoneSolidifierImpl.java @@ -113,7 +113,6 @@ public void start() { setLatestMilestone(latestSnapshot.getHash(), latestSnapshot.getIndex()); logChange(snapshotProvider.getInitialSnapshot().getIndex()); - syncProgressInfo.setSyncMilestoneStartIndex(snapshotProvider.getInitialSnapshot().getIndex()); milestoneSolidifier.start(); } catch (Exception e) { @@ -222,6 +221,8 @@ private void processSolidifyQueue() throws Exception { int lowest = oldestMilestoneInQueue == null ? -1 : oldestMilestoneInQueue.getValue(); scanMilestonesInQueue(); if (oldestMilestoneInQueue != null && lowest > oldestMilestoneInQueue.getValue()) { + // Going down or going up doesnt matter to the calculation + syncProgressInfo.addMilestoneApplicationTime(); logChange(-1); } } @@ -477,8 +478,7 @@ private void logChange(int prevSolidMilestoneIndex) { } // only print more sophisticated progress if we are coming from a more unsynced state - if (oldestMilestoneInQueue == null || - (prevSolidMilestoneIndex != -1 && getLatestMilestoneIndex() - nextLatestSolidMilestone < 1)) { + if (prevSolidMilestoneIndex != -1 && getLatestMilestoneIndex() - nextLatestSolidMilestone < 1) { syncProgressInfo.setSyncMilestoneStartIndex(nextLatestSolidMilestone); syncProgressInfo.resetMilestoneApplicationTimes(); return; @@ -486,12 +486,14 @@ private void logChange(int prevSolidMilestoneIndex) { int estSecondsToBeSynced = syncProgressInfo.computeEstimatedTimeToSyncUpSeconds(getLatestMilestoneIndex(), nextLatestSolidMilestone); - StringBuilder progressSB = new StringBuilder(); - - double percentPreferDown = 95; - double percentageSynced = ((100d - oldestMilestoneInQueue.getValue() / latestMilestoneIndex.doubleValue() / 0.01d) / 100d * percentPreferDown) - + ((latestSolidMilestone.doubleValue() / latestMilestoneIndex.doubleValue() / 0.01d) / 100d * (100d - percentPreferDown)); + + // oldestMilestoneInQueue can be null if they are processed faster than coming in. + // Unlikely to happen on mainnet though. + double percentageSynced = syncProgressInfo.calculatePercentageSynced(getLatestMilestoneIndex(), + getLatestSolidMilestoneIndex(), + oldestMilestoneInQueue == null ? getLatestSolidMilestoneIndex() : oldestMilestoneInQueue.getValue()); + StringBuilder progressSB = new StringBuilder(); // add progress bar progressSB.append(ASCIIProgressBar.getProgressBarString(0, 100, (int)Math.round(percentageSynced))); // add lsm to lm @@ -509,6 +511,9 @@ private void logChange(int prevSolidMilestoneIndex) { * Holds variables containing information needed for sync progress calculation. */ private static class SyncProgressInfo { + + static final double PERCENT_WEIGHT_DOWN = 95; + /** * The actual start milestone index from which the node started from when syncing up. */ @@ -579,6 +584,21 @@ int computeEstimatedTimeToSyncUpSeconds(int latestMilestoneIndex, int latestSoli return (int) ((avgMilestoneApplyMillisec / 1000) * (latestMilestoneIndex - latestSolidMilestoneIndex)); } + + /** + * + * @param latest + * @param latestSolid + * @return + */ + double calculatePercentageSynced(int latest, int latestSolid, int oldestInQueue) { + double currentD = oldestInQueue - latestSolid; + double targetD = latest - latestSolid; + double processPercentage = 100 - currentD / targetD / 0.01d / 100d * PERCENT_WEIGHT_DOWN; + + double percentageSynced = processPercentage + ((latestSolid / latest / 0.01d) / 100d * (100d - PERCENT_WEIGHT_DOWN)); + return percentageSynced; + } } }