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

Use the index of a seed node address in the list of nodes at the #4445

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 65 additions & 14 deletions core/src/main/java/bisq/core/app/misc/ExecutableForAppWithP2p.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import bisq.core.offer.OpenOfferManager;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;

import bisq.network.p2p.NodeAddress;
import bisq.network.p2p.P2PService;
import bisq.network.p2p.seed.SeedNodeRepository;

import bisq.common.UserThread;
import bisq.common.config.Config;
Expand All @@ -36,8 +38,15 @@

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -106,6 +115,62 @@ public void gracefulShutDown(ResultHandler resultHandler) {
}
}

public void startShutDownInterval(GracefulShutDownHandler gracefulShutDownHandler) {
List<NodeAddress> seedNodeAddresses = new ArrayList<>(injector.getInstance(SeedNodeRepository.class).getSeedNodeAddresses());
seedNodeAddresses.sort(Comparator.comparing(NodeAddress::getFullAddress));

NodeAddress myAddress = injector.getInstance(P2PService.class).getNetworkNode().getNodeAddress();
int myIndex = -1;
for (int i = 0; i < seedNodeAddresses.size(); i++) {
if (seedNodeAddresses.get(i).equals(myAddress)) {
myIndex = i;
break;
}
}

if (myIndex == -1) {
log.warn("We did not find our node address in the seed nodes repository. " +
"We use a 24 hour delay after startup as shut down strategy." +
"myAddress={}, seedNodeAddresses={}",
myAddress, seedNodeAddresses);

UserThread.runPeriodically(() -> {
if (System.currentTimeMillis() - startTime > SHUTDOWN_INTERVAL) {
log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"Shut down as node was running longer as {} hours" +
"\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n",
SHUTDOWN_INTERVAL / 3600000);

shutDown(gracefulShutDownHandler);
}

}, CHECK_SHUTDOWN_SEC);
return;
}

// We interpret the value of myIndex as hour of day (0-23). That way we avoid the risk of a restart of
// multiple nodes around the same time in case it would be not deterministic.

// We wrap our periodic check in a delay of 2 hours to avoid that we get
// triggered multiple times after a restart while being in the same hour. It can be that we miss our target
// hour during that delay but that is not considered problematic, the seed would just restart a bit longer than
// 24 hours.
int target = myIndex;
UserThread.runAfter(() -> {
// We check every hour if we are in the target hour.
UserThread.runPeriodically(() -> {
int currentHour = ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("GMT0")).getHour();
if (currentHour == target) {
log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"Shut down node at hour {}" +
"\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n", target);
shutDown(gracefulShutDownHandler);
}
}, TimeUnit.MINUTES.toSeconds(10));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use CHECK_SHUTDOWN_SEC here?

Copy link
Contributor Author

@chimp1984 chimp1984 Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHECK_SHUTDOWN_SEC is 1 hour. That has risk that we miss the hour we are targeting. If one timer event is triggered just 1 sec. before our target hour and then due cpu load the next 1 hour scheduled call takes longer we would miss our target hour. E.g. once call at 13:59, next call at 15:01, 14:00 would be our target.

With 10 min. Its safe enough and does not cause performance costs. We could maybe go higher like 30 min as well...but prefer to keep it on the safe side.

}, TimeUnit.HOURS.toSeconds(2));
}


///////////////////////////////////////////////////////////////////////////////////////////
// UncaughtExceptionHandler implementation
///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -128,20 +193,6 @@ protected void keepRunning() {
}
}

protected void startShutDownInterval(GracefulShutDownHandler gracefulShutDownHandler) {
UserThread.runPeriodically(() -> {
if (System.currentTimeMillis() - startTime > SHUTDOWN_INTERVAL) {
log.warn("\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" +
"Shut down as node was running longer as {} hours" +
"\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n",
SHUTDOWN_INTERVAL / 3600000);

shutDown(gracefulShutDownHandler);
}

}, CHECK_SHUTDOWN_SEC);
}

protected void checkMemory(Config config, GracefulShutDownHandler gracefulShutDownHandler) {
int maxMemory = config.maxMemory;
UserThread.runPeriodically(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RequestDataHandler implements MessageListener {
private static final long TIMEOUT = 180;

private NodeAddress peersNodeAddress;
private String getDataRequestType;
/*
*/

Expand Down Expand Up @@ -138,7 +139,8 @@ void requestData(NodeAddress nodeAddress, boolean isPreliminaryDataRequest) {
TIMEOUT);
}

log.info("We send a {} to peer {}. ", getDataRequest.getClass().getSimpleName(), nodeAddress);
getDataRequestType = getDataRequest.getClass().getSimpleName();
log.info("We send a {} to peer {}. ", getDataRequestType, nodeAddress);
networkNode.addMessageListener(this);
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, getDataRequest);
//noinspection UnstableApiUsage
Expand Down Expand Up @@ -259,8 +261,9 @@ private void logContents(NetworkEnvelope networkEnvelope,
StringBuilder sb = new StringBuilder();
sb.append("\n#################################################################\n");
sb.append("Connected to node: " + peersNodeAddress.getFullAddress() + "\n");
final int items = dataSet.size() + persistableNetworkPayloadSet.size();
sb.append("Received ").append(items).append(" instances\n");
int items = dataSet.size() + persistableNetworkPayloadSet.size();
sb.append("Received ").append(items).append(" instances from a ")
.append(getDataRequestType).append("\n");
payloadByClassName.forEach((key, value) -> sb.append(key)
.append(": ")
.append(value.size())
Expand Down
46 changes: 45 additions & 1 deletion seednode/src/main/java/bisq/seednode/SeedNodeMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import bisq.core.app.misc.ExecutableForAppWithP2p;
import bisq.core.app.misc.ModuleForAppWithP2p;

import bisq.network.p2p.P2PService;
import bisq.network.p2p.P2PServiceListener;

import bisq.common.UserThread;
import bisq.common.app.AppModule;
import bisq.common.app.Capabilities;
Expand Down Expand Up @@ -47,7 +50,6 @@ protected void doExecute() {
super.doExecute();

checkMemory(config, this);
startShutDownInterval(this);
CommonSetup.setup(this);

keepRunning();
Expand Down Expand Up @@ -95,5 +97,47 @@ protected void applyInjector() {
@Override
protected void startApplication() {
seedNode.startApplication();

injector.getInstance(P2PService.class).addP2PServiceListener(new P2PServiceListener() {
@Override
public void onDataReceived() {
// Do nothing
}

@Override
public void onNoSeedNodeAvailable() {
// Do nothing
}

@Override
public void onNoPeersAvailable() {
// Do nothing
}

@Override
public void onUpdatedDataReceived() {
// Do nothing
}

@Override
public void onTorNodeReady() {
// Do nothing
}

@Override
public void onHiddenServicePublished() {
startShutDownInterval(SeedNodeMain.this);
}

@Override
public void onSetupFailed(Throwable throwable) {
// Do nothing
}

@Override
public void onRequestCustomBridges() {
// Do nothing
}
});
}
}