Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'No sync target' logging improvements #4853

Merged
merged 11 commits into from
Jan 16, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.util;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class LogUtil {
static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

public static void throttledLog(
final Consumer<String> logger,
final String logMessage,
final AtomicBoolean shouldLog,
final int logRepeatDelay) {

if (shouldLog.compareAndSet(true, false)) {
logger.accept(logMessage);

final Runnable runnable = () -> shouldLog.set(true);
executor.schedule(runnable, logRepeatDelay, TimeUnit.SECONDS);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright ConsenSys AG.
* Copyright Hyperledger Besu Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -16,6 +16,7 @@

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.hyperledger.besu.ethereum.eth.sync.fastsync.PivotBlockRetriever.MAX_QUERY_RETRIES_PER_PEER;
import static org.hyperledger.besu.ethereum.util.LogUtil.throttledLog;

import org.hyperledger.besu.ethereum.ProtocolContext;
import org.hyperledger.besu.ethereum.core.BlockHeader;
Expand All @@ -33,6 +34,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -46,6 +48,10 @@ public class FastSyncTargetManager extends SyncTargetManager {
private final EthContext ethContext;
private final MetricsSystem metricsSystem;
private final FastSyncState fastSyncState;
private final AtomicBoolean logDebug = new AtomicBoolean(true);
private final AtomicBoolean logInfo = new AtomicBoolean(true);
private final int logDebugRepeatDelay = 15;
private final int logInfoRepeatDelay = 120;

public FastSyncTargetManager(
final SynchronizerConfiguration config,
Expand All @@ -69,10 +75,21 @@ protected CompletableFuture<Optional<EthPeer>> selectBestAvailableSyncTarget() {
final BlockHeader pivotBlockHeader = fastSyncState.getPivotBlockHeader().get();
final EthPeers ethPeers = ethContext.getEthPeers();
final Optional<EthPeer> maybeBestPeer = ethPeers.bestPeerWithHeightEstimate();
if (!maybeBestPeer.isPresent()) {
LOG.debug(
"No sync target, checking current peers for usefulness: {}",
ethContext.getEthPeers().peerCount());
if (maybeBestPeer.isEmpty()) {
throttledLog(
LOG::debug,
String.format(
"Unable to find sync target. Currently checking %d peers for usefulness. Pivot block: %d",
ethContext.getEthPeers().peerCount(), pivotBlockHeader.getNumber()),
logDebug,
logDebugRepeatDelay);
throttledLog(
LOG::info,
String.format(
"Unable to find sync target. Currently checking %d peers for usefulness.",
ethContext.getEthPeers().peerCount()),
logInfo,
logInfoRepeatDelay);
return completedFuture(Optional.empty());
} else {
final EthPeer bestPeer = maybeBestPeer.get();
Expand Down