Improvements

- Massively reduced logging, especially at debug level
- Optimizations to reduce system load
- Use bootstrapping to find stable nodes
This commit is contained in:
2015-11-08 10:14:37 +01:00
parent 2a8834e3c6
commit 1f05a52f05
10 changed files with 88 additions and 94 deletions

View File

@ -33,8 +33,7 @@ import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import static ch.dissem.bitmessage.entity.Plaintext.Status.*;
import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST;
@ -201,13 +200,15 @@ public class BitmessageContext {
* @param wait waits for the synchronization thread to finish
*/
public void synchronize(InetAddress host, int port, long timeoutInSeconds, boolean wait) {
Thread t = ctx.getNetworkHandler().synchronize(host, port, networkListener, timeoutInSeconds);
Future<?> future = ctx.getNetworkHandler().synchronize(host, port, networkListener, timeoutInSeconds);
if (wait) {
try {
t.join();
future.get();
} catch (InterruptedException e) {
LOG.info("Thread was interrupted. Trying to shut down synchronization and returning.");
t.interrupt();
future.cancel(true);
} catch (CancellationException | ExecutionException e) {
LOG.debug(e.getMessage(), e);
}
}
}
@ -241,7 +242,7 @@ public class BitmessageContext {
ctx.getAddressRepo().save(address);
break;
} else {
LOG.debug("Found pubkey for " + address + " but signature is invalid");
LOG.info("Found pubkey for " + address + " but signature is invalid");
}
}
} else {

View File

@ -71,7 +71,8 @@ class DefaultMessageListener implements NetworkHandler.MessageListener {
protected void receive(ObjectMessage object, GetPubkey getPubkey) {
BitmessageAddress identity = ctx.getAddressRepo().findIdentity(getPubkey.getRipeTag());
if (identity != null && identity.getPrivateKey() != null) {
LOG.debug("Got pubkey request for identity " + identity);
LOG.info("Got pubkey request for identity " + identity);
// FIXME: only send pubkey if it wasn't sent in the last 28 days
ctx.sendPubkey(identity, object.getStream());
}
}
@ -90,10 +91,10 @@ class DefaultMessageListener implements NetworkHandler.MessageListener {
}
if (address != null) {
address.setPubkey(pubkey);
LOG.debug("Got pubkey for contact " + address);
LOG.info("Got pubkey for contact " + address);
ctx.getAddressRepo().save(address);
List<Plaintext> messages = ctx.getMessageRepository().findMessages(Plaintext.Status.PUBKEY_REQUESTED, address);
LOG.debug("Sending " + messages.size() + " messages for contact " + address);
LOG.info("Sending " + messages.size() + " messages for contact " + address);
for (Plaintext msg : messages) {
msg.setStatus(DOING_PROOF_OF_WORK);
ctx.getMessageRepository().save(msg);

View File

@ -157,7 +157,7 @@ public class CryptoBox implements Streamable {
}
public Builder curveType(int curveType) {
if (curveType != 0x2CA) LOG.debug("Unexpected curve type " + curveType);
if (curveType != 0x2CA) LOG.trace("Unexpected curve type " + curveType);
this.curveType = curveType;
return this;
}

View File

@ -123,7 +123,6 @@ public abstract class AbstractSecurity implements Security, InternalContext.Cont
private byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException {
BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now());
LOG.debug("TTL: " + TTL + "s");
BigInteger numerator = TWO.pow(64);
BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes);
BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte).multiply(powLength.add(powLength.multiply(TTL).divide(BigInteger.valueOf(2).pow(16))));

View File

@ -37,40 +37,42 @@ public class MemoryNodeRegistry implements NodeRegistry {
private final Map<Long, Set<NetworkAddress>> stableNodes = new ConcurrentHashMap<>();
private final Map<Long, Set<NetworkAddress>> knownNodes = new ConcurrentHashMap<>();
public MemoryNodeRegistry() {
new Thread(new Runnable() {
@Override
public void run() {
try (InputStream in = getClass().getClassLoader().getResourceAsStream("nodes.txt")) {
Scanner scanner = new Scanner(in);
long stream = 0;
Set<NetworkAddress> streamSet = null;
while (scanner.hasNext()) {
try {
String line = scanner.nextLine().trim();
if (line.startsWith("#") || line.isEmpty()) {
// Ignore
continue;
}
if (line.startsWith("[stream")) {
stream = Long.parseLong(line.substring(8, line.lastIndexOf(']')));
streamSet = new HashSet<>();
stableNodes.put(stream, streamSet);
} else if (streamSet != null) {
int portIndex = line.lastIndexOf(':');
InetAddress inetAddress = InetAddress.getByName(line.substring(0, portIndex));
int port = Integer.valueOf(line.substring(portIndex + 1));
streamSet.add(new NetworkAddress.Builder().ip(inetAddress).port(port).stream(stream).build());
}
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
private void loadStableNodes() {
try (InputStream in = getClass().getClassLoader().getResourceAsStream("nodes.txt")) {
Scanner scanner = new Scanner(in);
long stream = 0;
Set<NetworkAddress> streamSet = null;
while (scanner.hasNext()) {
try {
String line = scanner.nextLine().trim();
if (line.startsWith("#") || line.isEmpty()) {
// Ignore
continue;
}
if (line.startsWith("[stream")) {
stream = Long.parseLong(line.substring(8, line.lastIndexOf(']')));
streamSet = new HashSet<>();
stableNodes.put(stream, streamSet);
} else if (streamSet != null) {
int portIndex = line.lastIndexOf(':');
InetAddress[] inetAddresses = InetAddress.getAllByName(line.substring(0, portIndex));
int port = Integer.valueOf(line.substring(portIndex + 1));
for (InetAddress inetAddress : inetAddresses) {
streamSet.add(new NetworkAddress.Builder().ip(inetAddress).port(port).stream(stream).build());
}
}
} catch (IOException e) {
throw new RuntimeException(e);
LOG.warn(e.getMessage(), e);
}
}
}, "node registry initializer").start();
if (LOG.isDebugEnabled()) {
for (Map.Entry<Long, Set<NetworkAddress>> e : stableNodes.entrySet()) {
LOG.debug("Stream " + e.getKey() + ": loaded " + e.getValue().size() + " bootstrap nodes.");
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
@ -86,9 +88,16 @@ public class MemoryNodeRegistry implements NodeRegistry {
known.remove(node);
}
}
} else if (stableNodes.containsKey(stream)) {
// To reduce load on stable nodes, only return one
result.add(selectRandom(stableNodes.get(stream)));
} else {
Set<NetworkAddress> nodes = stableNodes.get(stream);
if (nodes == null || nodes.isEmpty()) {
loadStableNodes();
nodes = stableNodes.get(stream);
}
if (nodes != null && !nodes.isEmpty()) {
// To reduce load on stable nodes, only return one
result.add(selectRandom(nodes));
}
}
}
return selectRandom(limit, result);

View File

@ -22,6 +22,7 @@ import ch.dissem.bitmessage.utils.Property;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.Future;
/**
* Handles incoming messages
@ -33,7 +34,7 @@ public interface NetworkHandler {
* An implementation should disconnect if either the timeout is reached or the returned thread is interrupted.
* </p>
*/
Thread synchronize(InetAddress trustedHost, int port, MessageListener listener, long timeoutInSeconds);
Future<?> synchronize(InetAddress trustedHost, int port, MessageListener listener, long timeoutInSeconds);
/**
* Start a full network node, accepting incoming connections and relaying objects.

View File

@ -1,18 +1,8 @@
[stream 1]
5.45.99.75:8444
75.167.159.54:8444
95.165.168.168:8444
85.180.139.241:8444
178.62.12.187:8448
[2604:2000:1380:9f:82e:148b:2746:d0c7]:8080
158.222.211.81:8080
24.188.198.204:8111
109.147.204.113:1195
178.11.46.221:8444
dissem.ch:8444
bootstrap8080.bitmessage.org:8080
bootstrap8444.bitmessage.org:8444
[stream 2]
# none yet