Skip to content
Snippets Groups Projects
Commit 2b595812 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

[fix] clean code on peers async refreshing

parent 3c9cebfe
No related branches found
No related tags found
No related merge requests found
Pipeline #4432 passed
...@@ -176,14 +176,9 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network ...@@ -176,14 +176,9 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
peer = mainPeer; peer = mainPeer;
} }
// Exclude peer with only a local IPv4 address // Exclude peer with only a local IPv4 address (or localhost)
else if (InetAddressUtils.isLocalIPv4Address(peer.getHost())) { else if (InetAddressUtils.isLocalAddress(peer.getHost())) {
return null; return CompletableFuture.<Peer>completedFuture(null);
}
// Exclude localhost address
else if ("localhost".equalsIgnoreCase(peer.getHost())) {
return null;
} }
return asyncRefreshPeer(peer, memberUids, pool); return asyncRefreshPeer(peer, memberUids, pool);
...@@ -195,7 +190,8 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network ...@@ -195,7 +190,8 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
public CompletableFuture<Peer> asyncRefreshPeer(final Peer peer, final Map<String, String> memberUids, final ExecutorService pool) { public CompletableFuture<Peer> asyncRefreshPeer(final Peer peer, final Map<String, String> memberUids, final ExecutorService pool) {
System.out.println("Refreshing peer: " + peer.toString()); if (log.isDebugEnabled()) log.debug(String.format("[%s] Refreshing peer status", peer.toString()));
return CompletableFuture.supplyAsync(() -> fillNodeSummary(peer), pool) return CompletableFuture.supplyAsync(() -> fillNodeSummary(peer), pool)
.thenApplyAsync(this::fillCurrentBlock) .thenApplyAsync(this::fillCurrentBlock)
.exceptionally(throwable -> { .exceptionally(throwable -> {
...@@ -615,15 +611,20 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network ...@@ -615,15 +611,20 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
} }
protected Peer fillNodeSummary(final Peer peer) { protected Peer fillNodeSummary(final Peer peer) {
// Skip if no BMA, BMAS or ES_CORE_API
if (!Peers.hasBmaEndpoint(peer) && !Peers.hasEsCoreEndpoint(peer)) return peer; if (!Peers.hasBmaEndpoint(peer) && !Peers.hasEsCoreEndpoint(peer)) return peer;
JsonNode summary = getNodeSummary(peer); JsonNode summary = getNodeSummary(peer);
peer.getStats().setVersion(getVersion(summary)); peer.getStats().setVersion(getVersion(summary));
peer.getStats().setSoftware(getSoftware(summary)); peer.getStats().setSoftware(getSoftware(summary));
return peer; return peer;
} }
protected Peer fillCurrentBlock(final Peer peer) { protected Peer fillCurrentBlock(final Peer peer) {
if (Peers.hasBmaEndpoint(peer) || Peers.hasEsCoreEndpoint(peer)) { // Skip if no BMA, BMAS or ES_CORE_API
if (!Peers.hasBmaEndpoint(peer) && !Peers.hasEsCoreEndpoint(peer)) return peer;
JsonNode json = get(peer, BMA_URL_BLOCKCHAIN_CURRENT); JsonNode json = get(peer, BMA_URL_BLOCKCHAIN_CURRENT);
String currency = json.has("currency") ? json.get("currency").asText() : null; String currency = json.has("currency") ? json.get("currency").asText() : null;
...@@ -641,7 +642,6 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network ...@@ -641,7 +642,6 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
if (log.isTraceEnabled()) { if (log.isTraceEnabled()) {
log.trace(String.format("[%s] current block [%s-%s]", peer.toString(), number, hash)); log.trace(String.format("[%s] current block [%s-%s]", peer.toString(), number, hash));
} }
}
return peer; return peer;
} }
...@@ -675,17 +675,16 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network ...@@ -675,17 +675,16 @@ public class NetworkServiceImpl extends BaseRemoteServiceImpl implements Network
peers.forEach(peerFound -> { peers.forEach(peerFound -> {
if (peerFound.getStats().getStatus() == Peer.PeerStatus.DOWN) { if (peerFound.getStats().getStatus() == Peer.PeerStatus.DOWN) {
String error = peerFound.getStats().getError(); String error = peerFound.getStats().getError();
log.trace(String.format(" peer [%s] [%s] %s", log.trace(String.format(" [%s] status is %s %s",
peerFound.toString(), peerFound.toString(),
peerFound.getStats().getStatus().name(), Peer.PeerStatus.DOWN.name(),
error != null ? error : "")); error != null ? (":" + error) : ""));
} else { } else {
log.trace(String.format(" peer [%s] [%s] [v%s] block [%s]", peerFound.toString(), log.trace(String.format(" [%s] status %s: [v%s] block [%s]", peerFound.toString(),
peerFound.getStats().getStatus().name(), peerFound.getStats().getStatus().name(),
peerFound.getStats().getVersion(), peerFound.getStats().getVersion(),
peerFound.getStats().getBlockNumber() peerFound.getStats().getBlockNumber()
)); ));
} }
}); });
} }
......
...@@ -22,8 +22,6 @@ package org.duniter.core.util.concurrent; ...@@ -22,8 +22,6 @@ package org.duniter.core.util.concurrent;
* #L% * #L%
*/ */
import org.apache.commons.collections4.CollectionUtils;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
...@@ -40,7 +38,6 @@ public class CompletableFutures { ...@@ -40,7 +38,6 @@ public class CompletableFutures {
} }
public static <T> CompletableFuture<List<T>> allOfToList(List<CompletableFuture<T>> futures) { public static <T> CompletableFuture<List<T>> allOfToList(List<CompletableFuture<T>> futures) {
CollectionUtils.filter(futures, Objects::nonNull);
CompletableFuture<Void> allDoneFuture = CompletableFuture<Void> allDoneFuture =
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return allDoneFuture.thenApply(v -> return allDoneFuture.thenApply(v ->
...@@ -57,8 +54,10 @@ public class CompletableFutures { ...@@ -57,8 +54,10 @@ public class CompletableFutures {
return allDoneFuture.thenApply(v -> return allDoneFuture.thenApply(v ->
futures.stream() futures.stream()
.map(future -> future.join()) .map(future -> future.join())
.filter(Objects::nonNull) // skip empty result
.filter(filter) .filter(filter)
.collect(Collectors.toList()) .collect(Collectors.toList())
); );
} }
} }
...@@ -51,4 +51,12 @@ public class InetAddressUtils { ...@@ -51,4 +51,12 @@ public class InetAddressUtils {
public static boolean isIPv6Address(String input) { public static boolean isIPv6Address(String input) {
return org.apache.http.conn.util.InetAddressUtils.isIPv6Address(input); return org.apache.http.conn.util.InetAddressUtils.isIPv6Address(input);
} }
public static boolean isLocalAddress(String input) {
return isLocalIPv4Address(input) || "localhost".equalsIgnoreCase(input);
}
public static boolean isNotLocalAddress(String input) {
return !isLocalAddress(input);
}
} }
...@@ -44,6 +44,15 @@ public class InetAddressUtilsTest { ...@@ -44,6 +44,15 @@ public class InetAddressUtilsTest {
boolean check = InetAddressUtils.isLocalIPv4Address("192.168.1.11"); boolean check = InetAddressUtils.isLocalIPv4Address("192.168.1.11");
Assert.assertTrue(check); Assert.assertTrue(check);
check = InetAddressUtils.isLocalIPv4Address("127.0.0.1");
Assert.assertTrue(check);
} }
@Test
public void isLocalAddress() {
boolean check = InetAddressUtils.isLocalAddress("localhost");
Assert.assertTrue(check);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment