From cae360b6c59e28db07e45d91facba3a75da20697 Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 30 Aug 2024 12:12:01 -0400 Subject: [PATCH] throttle warnings when monerod not synced within tolerance --- .../java/haveno/core/api/XmrConnectionService.java | 14 ++++++++++---- .../main/java/haveno/core/trade/HavenoUtils.java | 1 + .../haveno/core/xmr/wallet/XmrWalletService.java | 9 ++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index 48538ef4..4465fd2a 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -99,9 +99,10 @@ public final class XmrConnectionService { @Getter private MoneroDaemonInfo lastInfo; private Long lastLogPollErrorTimestamp; - private Long syncStartHeight = null; + private long lastLogDaemonNotSyncedTimestamp; + private Long syncStartHeight; private TaskLooper daemonPollLooper; - private long lastRefreshPeriodMs = 0; + private long lastRefreshPeriodMs; @Getter private boolean isShutDownStarted; private List listeners = new ArrayList<>(); @@ -371,7 +372,6 @@ public final class XmrConnectionService { Long targetHeight = getTargetHeight(); if (targetHeight == null) return false; if (targetHeight - chainHeight.get() <= 3) return true; // synced if within 3 blocks of target height - log.warn("Our chain height: {} is out of sync with peer nodes chain height: {}", chainHeight.get(), targetHeight); return false; } @@ -720,7 +720,7 @@ public final class XmrConnectionService { } // log error message periodically - if ((lastLogPollErrorTimestamp == null || System.currentTimeMillis() - lastLogPollErrorTimestamp > HavenoUtils.LOG_POLL_ERROR_PERIOD_MS)) { + if (lastLogPollErrorTimestamp == null || System.currentTimeMillis() - lastLogPollErrorTimestamp > HavenoUtils.LOG_POLL_ERROR_PERIOD_MS) { log.warn("Failed to fetch daemon info, trying to switch to best connection: " + e.getMessage()); if (DevEnv.isDevMode()) e.printStackTrace(); lastLogPollErrorTimestamp = System.currentTimeMillis(); @@ -734,6 +734,12 @@ public final class XmrConnectionService { // connected to daemon isConnected = true; + // throttle warnings if daemon not synced + if (!isSyncedWithinTolerance() && System.currentTimeMillis() - lastLogDaemonNotSyncedTimestamp > HavenoUtils.LOG_DAEMON_NOT_SYNCED_WARN_PERIOD_MS) { + log.warn("Our chain height: {} is out of sync with peer nodes chain height: {}", chainHeight.get(), getTargetHeight()); + lastLogDaemonNotSyncedTimestamp = System.currentTimeMillis(); + } + // announce connection change if refresh period changes if (getRefreshPeriodMs() != lastRefreshPeriodMs) { lastRefreshPeriodMs = getRefreshPeriodMs(); diff --git a/core/src/main/java/haveno/core/trade/HavenoUtils.java b/core/src/main/java/haveno/core/trade/HavenoUtils.java index 994753e9..5b9386af 100644 --- a/core/src/main/java/haveno/core/trade/HavenoUtils.java +++ b/core/src/main/java/haveno/core/trade/HavenoUtils.java @@ -81,6 +81,7 @@ public class HavenoUtils { // other configuration public static final long LOG_POLL_ERROR_PERIOD_MS = 1000 * 60 * 4; // log poll errors up to once every 4 minutes + public static final long LOG_DAEMON_NOT_SYNCED_WARN_PERIOD_MS = 1000 * 30; // log warnings when daemon not synced once every 30s // synchronize requests to the daemon private static boolean SYNC_DAEMON_REQUESTS = false; // sync long requests to daemon (e.g. refresh, update pool) // TODO: performance suffers by syncing daemon requests, but otherwise we sometimes get sporadic errors? diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index ea0d77fb..d3402061 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -151,6 +151,7 @@ public class XmrWalletService extends XmrWalletBase { private TaskLooper pollLooper; private boolean pollInProgress; private Long pollPeriodMs; + private long lastLogDaemonNotSyncedTimestamp; private long lastLogPollErrorTimestamp; private long lastPollTxsTimestamp; private final Object pollLock = new Object(); @@ -1767,9 +1768,15 @@ public class XmrWalletService extends XmrWalletBase { return; } if (!xmrConnectionService.isSyncedWithinTolerance()) { - log.warn("Monero daemon is not synced within tolerance, height={}, targetHeight={}", xmrConnectionService.chainHeightProperty().get(), xmrConnectionService.getTargetHeight()); + + // throttle warnings + if (System.currentTimeMillis() - lastLogDaemonNotSyncedTimestamp > HavenoUtils.LOG_DAEMON_NOT_SYNCED_WARN_PERIOD_MS) { + log.warn("Monero daemon is not synced within tolerance, height={}, targetHeight={}, monerod={}", xmrConnectionService.chainHeightProperty().get(), xmrConnectionService.getTargetHeight(), xmrConnectionService.getConnection() == null ? null : xmrConnectionService.getConnection().getUri()); + lastLogDaemonNotSyncedTimestamp = System.currentTimeMillis(); + } return; } + // sync wallet if behind daemon if (walletHeight.get() < xmrConnectionService.getTargetHeight()) { synchronized (walletLock) { // avoid long sync from blocking other operations