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

Cleanup monitor module #4695

Merged
merged 3 commits into from
Oct 27, 2020
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
1 change: 1 addition & 0 deletions monitor/src/main/java/bisq/monitor/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private void start() throws Throwable {
// start Tor
Tor.setDefault(new NativeTor(TOR_WORKING_DIR, null, null, false));

//noinspection deprecation,deprecation,deprecation,deprecation,deprecation,deprecation,deprecation,deprecation
Capabilities.app.addAll(Capability.TRADE_STATISTICS,
Capability.TRADE_STATISTICS_2,
Capability.ACCOUNT_AGE_WITNESS,
Expand Down
4 changes: 2 additions & 2 deletions monitor/src/main/java/bisq/monitor/StatisticsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package bisq.monitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Calculates average, max, min, p25, p50, p75 off of a list of samples and
Expand All @@ -35,7 +35,7 @@ public class StatisticsHelper {

public static Map<String, String> process(Collection<Long> input) {

List<Long> samples = input.stream().collect(Collectors.toList());
List<Long> samples = new ArrayList<>(input);

// aftermath
Collections.sort(samples);
Expand Down
2 changes: 1 addition & 1 deletion monitor/src/main/java/bisq/monitor/ThreadGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public synchronized void await() {
while (lock.getCount() > 0)
try {
if (!lock.await(60, TimeUnit.SECONDS)) {
log.warn("timeout occured!");
log.warn("timeout occurred!");
break; // break the loop
}
} catch (InterruptedException ignore) {
Expand Down
7 changes: 3 additions & 4 deletions monitor/src/main/java/bisq/monitor/metric/MarketStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@
*/
@Slf4j
public class MarketStats extends Metric {

private static final String MARKETS_BISQ_NETWORK = "https://markets.bisq.network";
// poor mans JSON parser
private final Pattern marketPattern = Pattern.compile("\"market\" ?: ?\"([a-z_]+)\"");
private final Pattern amountPattern = Pattern.compile("\"amount\" ?: ?\"([\\d\\.]+)\"");
private final Pattern volumePattern = Pattern.compile("\"volume\" ?: ?\"([\\d\\.]+)\"");
private final Pattern timestampPattern = Pattern.compile("\"trade_date\" ?: ?([\\d]+)");

private final String marketApi = "https://markets.bisq.network";
private Long lastRun = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(15));

public MarketStats(Reporter reporter) {
Expand All @@ -65,12 +64,12 @@ protected void execute() {
Map<String, String> result = new HashMap<>();

// assemble query
Long now = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
long now = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
String query = "/api/trades?format=json&market=all&timestamp_from=" + lastRun + "&timestamp_to=" + now;
lastRun = now; // thought about adding 1 second but what if a trade is done exactly in this one second?

// connect
URLConnection connection = new URL(marketApi + query).openConnection();
URLConnection connection = new URL(MARKETS_BISQ_NETWORK + query).openConnection();

// prepare to receive data
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Expand Down
15 changes: 9 additions & 6 deletions monitor/src/main/java/bisq/monitor/metric/P2PMarketStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -59,7 +60,7 @@ public class P2PMarketStats extends P2PSeedNodeSnapshotBase {
/**
* Efficient way to aggregate numbers.
*/
private class Aggregator {
private static class Aggregator {
private long value = 0;

synchronized long value() {
Expand All @@ -75,7 +76,7 @@ synchronized void add(long amount) {
}
}

private abstract class OfferStatistics<T> extends Statistics<T> {
private abstract static class OfferStatistics<T> extends Statistics<T> {
@Override
public synchronized void log(Object message) {
if (message instanceof OfferPayload) {
Expand Down Expand Up @@ -171,14 +172,14 @@ protected void createHistogram(List<Long> input, String market, Map<String, Stri
int numberOfBins = 5;

// - get biggest offer
double max = input.stream().max(Long::compareTo).get() * 1.01;
double max = input.stream().max(Long::compareTo).map(value -> value * 1.01).orElse(0.0);

// - create histogram
input.stream().collect(
Collectors.groupingBy(aLong -> aLong == max ? (int) numberOfBins - 1 : (int) Math.floor(aLong / (max / numberOfBins)), Collectors.counting())).
Collectors.groupingBy(aLong -> aLong == max ? numberOfBins - 1 : (int) Math.floor(aLong / (max / numberOfBins)), Collectors.counting())).
forEach((integer, integer2) -> report.put(market + ".bin_" + integer, String.valueOf(integer2)));

report.put(market + ".number_of_bins", String.valueOf((int) numberOfBins));
report.put(market + ".number_of_bins", String.valueOf(numberOfBins));
report.put(market + ".max", String.valueOf((int) max));
}

Expand Down Expand Up @@ -223,7 +224,9 @@ protected void report() {

// do version statistics
report.clear();
versionBucketsPerHost.values().stream().findAny().get().values().forEach((version, numberOfOccurrences) -> report.put(version, String.valueOf(numberOfOccurrences.value())));
Optional<Statistics<Aggregator>> optionalStatistics = versionBucketsPerHost.values().stream().findAny();
optionalStatistics.ifPresent(aggregatorStatistics -> aggregatorStatistics.values()
.forEach((version, numberOfOccurrences) -> report.put(version, String.valueOf(numberOfOccurrences.value()))));
reporter.report(report, "versions");
}

Expand Down
7 changes: 3 additions & 4 deletions monitor/src/main/java/bisq/monitor/metric/P2PNetworkLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class P2PNetworkLoad extends Metric implements MessageListener, SetupList
* History implementation using a {@link LinkedHashMap} and its
* {@link LinkedHashMap#removeEldestEntry(Map.Entry)} option.
*/
private class FixedSizeHistoryTracker<K, V> extends LinkedHashMap<K, V> {
private static class FixedSizeHistoryTracker<K, V> extends LinkedHashMap<K, V> {
final int historySize;

FixedSizeHistoryTracker(int historySize) {
Expand Down Expand Up @@ -123,7 +123,6 @@ protected void execute() {
hsReady.await();

// boot up P2P node
File storageDir = torHiddenServiceDir;
try {
Config config = new Config();
CorruptedStorageFileHandler corruptedStorageFileHandler = new CorruptedStorageFileHandler();
Expand All @@ -133,7 +132,7 @@ protected void execute() {
networkProtoResolver);
DefaultSeedNodeRepository seedNodeRepository = new DefaultSeedNodeRepository(config);
PeerManager peerManager = new PeerManager(networkNode, seedNodeRepository, new ClockWatcher(),
new PersistenceManager<>(storageDir, persistenceProtoResolver, corruptedStorageFileHandler), maxConnections);
new PersistenceManager<>(torHiddenServiceDir, persistenceProtoResolver, corruptedStorageFileHandler), maxConnections);

// init file storage
peerManager.readPersisted();
Expand Down Expand Up @@ -194,7 +193,7 @@ public void configure(Properties properties) {
/**
* Efficient way to count message occurrences.
*/
private class Counter {
private static class Counter {
private int value = 1;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class P2PRoundTripTime extends P2PSeedNodeSnapshotBase {

private static final String SAMPLE_SIZE = "run.sampleSize";
private Map<Integer, Long> sentAt = new HashMap<>();
private final Map<Integer, Long> sentAt = new HashMap<>();
private Map<NodeAddress, Statistics> measurements = new HashMap<>();

public P2PRoundTripTime(Reporter reporter) {
Expand Down
27 changes: 17 additions & 10 deletions monitor/src/main/java/bisq/monitor/metric/P2PSeedNodeSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -78,7 +77,7 @@ public class P2PSeedNodeSnapshot extends P2PSeedNodeSnapshotBase {
/**
* Use a counter to do statistics.
*/
private class MyStatistics extends Statistics<Set<Integer>> {
private static class MyStatistics extends Statistics<Set<Integer>> {

@Override
public synchronized void log(Object message) {
Expand Down Expand Up @@ -141,8 +140,8 @@ void report() {
try {
report.put(OnionParser.prettyPrint(host) + ".relativeNumberOfMessages." + messageType,
String.valueOf(set.size() - referenceValues.get(messageType).size()));
} catch (MalformedURLException | NullPointerException ignore) {
log.error("we should never have gotten here", ignore);
} catch (MalformedURLException | NullPointerException e) {
log.error("we should never have gotten here", e);
}
});
try {
Expand Down Expand Up @@ -175,8 +174,12 @@ void report() {
// - process dao data
perType.forEach((type, nodeAddressTupleMap) -> {
// - find head
int head = (int) nodeAddressTupleMap.values().stream().max(Comparator.comparingLong(Tuple::getHeight)).get().height;
int oldest = (int) nodeAddressTupleMap.values().stream().min(Comparator.comparingLong(Tuple::getHeight)).get().height;
int head = nodeAddressTupleMap.values().stream().max(Comparator.comparingLong(Tuple::getHeight))
.map(value -> (int) value.height)
.orElse(0);
int oldest = nodeAddressTupleMap.values().stream().min(Comparator.comparingLong(Tuple::getHeight))
.map(value -> (int) value.height)
.orElse(0);

// - update queried height
if (type.contains("DaoState"))
Expand All @@ -199,10 +202,14 @@ else if (type.contains("Proposal"))
hitcount.put(hash, 1);
});

List<ByteBuffer> states = hitcount.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).map(byteBufferIntegerEntry -> byteBufferIntegerEntry.getKey()).collect(Collectors.toList());
hitcount.clear();

nodeAddressTupleMap.forEach((nodeAddress, tuple) -> daoreport.put(type + "." + OnionParser.prettyPrint(nodeAddress) + ".hash", Integer.toString(Arrays.asList(states.toArray()).indexOf(ByteBuffer.wrap(tuple.hash)))));
nodeAddressTupleMap.forEach((nodeAddress, tuple) ->
daoreport.put(type + "." + OnionParser.prettyPrint(nodeAddress) + ".hash",
Integer.toString(Arrays.asList(hitcount.entrySet().stream()
.sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue()))
.map(Map.Entry::getKey).toArray()).indexOf(ByteBuffer
.wrap(tuple.hash)))));

// - report reference head
daoreport.put(type + ".referenceHead", Integer.toString(head));
Expand All @@ -214,7 +221,7 @@ else if (type.contains("Proposal"))
reporter.report(daoreport, "DaoStateSnapshot");
}

private class Tuple {
private static class Tuple {
@Getter
private final long height;
private final byte[] hash;
Expand All @@ -239,7 +246,7 @@ public void log(Object message) {
}
}

private Map<NodeAddress, Statistics<Tuple>> daoData = new ConcurrentHashMap<>();
private final Map<NodeAddress, Statistics<Tuple>> daoData = new ConcurrentHashMap<>();

protected boolean treatMessage(NetworkEnvelope networkEnvelope, Connection connection) {
checkNotNull(connection.getPeersNodeAddressProperty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,13 @@ public void configure(Properties properties) {
try {
CorePersistenceProtoResolver persistenceProtoResolver = new CorePersistenceProtoResolver(null, null);

//TODO will not work with historical data... should be refactored to re-use code for reading resource files
TradeStatistics3Store tradeStatistics3Store = new TradeStatistics3Store();
PersistenceManager<TradeStatistics3Store> tradeStatistics3PersistenceManager = new PersistenceManager<>(dir,
persistenceProtoResolver, null);
tradeStatistics3PersistenceManager.initialize(tradeStatistics3Store, PersistenceManager.Source.NETWORK);
tradeStatistics3PersistenceManager.initialize(tradeStatistics3Store,
tradeStatistics3Store.getDefaultStorageFileName() + networkPostfix,
PersistenceManager.Source.NETWORK);
TradeStatistics3Store persistedTradeStatistics3Store = tradeStatistics3PersistenceManager.getPersisted();
if (persistedTradeStatistics3Store != null) {
tradeStatistics3Store.getMap().putAll(persistedTradeStatistics3Store.getMap());
Expand All @@ -131,7 +134,9 @@ public void configure(Properties properties) {
AccountAgeWitnessStore accountAgeWitnessStore = new AccountAgeWitnessStore();
PersistenceManager<AccountAgeWitnessStore> accountAgeWitnessPersistenceManager = new PersistenceManager<>(dir,
persistenceProtoResolver, null);
accountAgeWitnessPersistenceManager.initialize(accountAgeWitnessStore, PersistenceManager.Source.NETWORK);
accountAgeWitnessPersistenceManager.initialize(accountAgeWitnessStore,
accountAgeWitnessStore.getDefaultStorageFileName() + networkPostfix,
PersistenceManager.Source.NETWORK);
AccountAgeWitnessStore persistedAccountAgeWitnessStore = accountAgeWitnessPersistenceManager.getPersisted();
if (persistedAccountAgeWitnessStore != null) {
accountAgeWitnessStore.getMap().putAll(persistedAccountAgeWitnessStore.getMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void report(String key, String value, String timeInMilliseconds, String p
String report = "bisq" + (Version.getBaseCurrencyNetwork() != 0 ? "-" + BaseCurrencyNetwork.values()[Version.getBaseCurrencyNetwork()].getNetwork() : "")
+ (prefix.isEmpty() ? "" : "." + prefix)
+ (key.isEmpty() ? "" : "." + key)
+ " " + value + " " + Long.valueOf(timeInMilliseconds) / 1000 + "\n";
+ " " + value + " " + Long.parseLong(timeInMilliseconds) / 1000 + "\n";

try {
NodeAddress nodeAddress = OnionParser.getNodeAddress(configuration.getProperty("serviceUrl"));
Expand Down