Commit 81e0b7b8 authored by Benoit Lavenier's avatar Benoit Lavenier

[fix] Better conversion of peer into BMA Peers (now use the raw document)

parent d3b50100
Pipeline #4606 passed with stage
in 31 seconds
......@@ -34,18 +34,18 @@ import java.util.Set;
*/
public interface CurrencyDao extends Bean, EntityDao<String, Currency> {
Currency create(final Currency currency);
Currency update(final Currency currency);
void remove(final Currency currency);
Set<String> getAllIds();
List<Currency> getAll();
List<Currency> getAllByAccount(long accountId);
Currency create(final Currency currency);
Currency update(final Currency currency);
void remove(final Currency currency);
/**
* Return the value of the last universal dividend
* @param currencyId
......@@ -62,4 +62,6 @@ public interface CurrencyDao extends Bean, EntityDao<String, Currency> {
void insertUDs(String currencyId, Map<Integer, Long> newUDs);
boolean isExists(String currencyId);
}
......@@ -25,6 +25,7 @@ package org.duniter.core.client.dao.mem;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.duniter.core.client.dao.CurrencyDao;
import org.duniter.core.client.model.local.Currency;
import java.util.*;
......@@ -43,7 +44,7 @@ public class MemoryCurrencyDaoImpl implements CurrencyDao {
}
@Override
public org.duniter.core.client.model.local.Currency create(final org.duniter.core.client.model.local.Currency entity) {
public Currency create(final org.duniter.core.client.model.local.Currency entity) {
currencies.put(entity.getId(), entity);
......@@ -51,13 +52,13 @@ public class MemoryCurrencyDaoImpl implements CurrencyDao {
}
@Override
public org.duniter.core.client.model.local.Currency update(final org.duniter.core.client.model.local.Currency currency) {
public Currency update(final Currency currency) {
currencies.put(currency.getId(), currency);
return currency;
}
@Override
public void remove(final org.duniter.core.client.model.local.Currency currency) {
public void remove(final Currency currency) {
currencies.remove(currency.getId());
}
......@@ -67,23 +68,23 @@ public class MemoryCurrencyDaoImpl implements CurrencyDao {
}
@Override
public List<org.duniter.core.client.model.local.Currency> getAll() {
public List<Currency> getAll() {
return ImmutableList.copyOf(currencies.values());
}
@Override
public List<org.duniter.core.client.model.local.Currency> getAllByAccount(long accountId) {
public List<Currency> getAllByAccount(long accountId) {
return ImmutableList.copyOf(currencies.values());
}
@Override
public org.duniter.core.client.model.local.Currency getById(String id) {
public Currency getById(String id) {
return currencies.get(id);
}
@Override
public long getLastUD(String id) {
org.duniter.core.client.model.local.Currency currency = getById(id);
Currency currency = getById(id);
if (currency == null) {
return -1;
}
......
......@@ -160,14 +160,22 @@ public class MemoryPeerDaoImpl implements PeerDao {
@Override
public void updatePeersAsDown(String currencyId, long upTimeLimitInSec, Collection<String> endpointApis) {
long firstDownTime = System.currentTimeMillis();
getPeersByCurrencyId(currencyId).stream()
.filter(peer ->
peer.getStats() != null
&& peer.getStats().getLastUpTime() < upTimeLimitInSec
&& peer.getStats().isReacheable()
&& (
peer.getStats().getLastUpTime() == null
|| peer.getStats().getLastUpTime() < upTimeLimitInSec
)
&& (endpointApis == null || endpointApis.contains(peer.getApi()))
)
.forEach(peer -> peer.getStats().setStatus(Peer.PeerStatus.DOWN));
.forEach(peer -> {
peer.getStats().setStatus(Peer.PeerStatus.DOWN);
peer.getStats().setFirstDownTime(firstDownTime);
});
}
@Override
......
......@@ -27,6 +27,7 @@ import org.duniter.core.util.CollectionUtils;
import org.duniter.core.util.StringUtils;
import java.io.Serializable;
import java.util.Objects;
/**
* Created by eis on 05/02/15.
......@@ -91,7 +92,6 @@ public class NetworkPeering implements Serializable {
this.status = status;
}
@JsonIgnore
public String getRaw() {
return raw;
}
......@@ -231,6 +231,20 @@ public class NetworkPeering implements Serializable {
}
return sb.toString();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Endpoint) {
return Objects.equals(((Endpoint) obj).api, api)
&& Objects.equals(((Endpoint) obj).id, id)
&& Objects.equals(((Endpoint) obj).dns, dns)
&& Objects.equals(((Endpoint) obj).ipv4, ipv4)
&& Objects.equals(((Endpoint) obj).ipv6, ipv6)
&& Objects.equals(((Endpoint) obj).port, port)
&& Objects.equals(((Endpoint) obj).path, path);
}
return super.equals(obj);
}
}
......
......@@ -41,12 +41,15 @@ public class NetworkPeerings {
}
public static NetworkPeering parse(String document) throws IOException {
NetworkPeering result = new NetworkPeering();
return parse(document, result);
}
public static NetworkPeering parse(String document, NetworkPeering result) throws IOException {
Preconditions.checkNotNull(document);
try {
NetworkPeering result = new NetworkPeering();
String[] lines = document.trim().split("\n");
Preconditions.checkArgument(lines.length >= 7, "Invalid document");
......@@ -89,24 +92,4 @@ public class NetworkPeerings {
throw new IOException(e.getMessage(), e);
}
}
public static void parseDefaultFormatEndPoint(Matcher matcher, NetworkPeering.Endpoint endpoint, int startGroup) {
for(int i=startGroup; i<=matcher.groupCount(); i++) {
String word = matcher.group(i);
if (StringUtils.isNotBlank(word)) {
if (InetAddressUtils.isIPv4Address(word)) {
endpoint.ipv4 = word;
} else if (InetAddressUtils.isIPv6Address(word)) {
endpoint.ipv6 = word;
} else if (i == matcher.groupCount() || (i == matcher.groupCount() -1) && word.matches("\\d+")){
endpoint.port = Integer.parseInt(word);
} else if (word.startsWith("/")) {
endpoint.path = word;
} else {
endpoint.dns = word;
}
}
}
}
}
\ No newline at end of file
......@@ -32,24 +32,22 @@ import org.duniter.core.client.model.bma.BlockchainParameters;
*/
public class Currency implements LocalEntity<String>, Serializable {
public static final String PROPERTY_FIRST_BLOCK_SIGNATURE = "firstBlockSignature";
public static final String PROPERTY_MEMBER_COUNT = "membersCount";
public static final String PROPERTY_LAST_UD = "lastUD";
public static final String PROPERTY_PARAMETERS = "parameters";
public static final String PROPERTY_UNITBASE = "unitbase";
private String id;
private Integer membersCount;
private BlockchainParameters parameters;
private String firstBlockSignature;
private Integer membersCount;
private Long lastUD;
private BlockchainParameters parameters;
private Integer unitbase;
public Currency() {
}
public Currency(String id,
String firstBlockSignature,
int membersCount,
BlockchainParameters parameters) {
this.id = id;
this.firstBlockSignature = firstBlockSignature;
this.membersCount = membersCount;
this.parameters = parameters;
}
@JsonIgnore
public String getId() {
......@@ -68,7 +66,6 @@ public class Currency implements LocalEntity<String>, Serializable {
return firstBlockSignature;
}
public void setMembersCount(Integer membersCount) {
this.membersCount = membersCount;
}
......@@ -85,6 +82,14 @@ public class Currency implements LocalEntity<String>, Serializable {
this.lastUD = lastUD;
}
public Integer getUnitbase() {
return unitbase;
}
public void setUnitbase(Integer unitbase) {
this.unitbase = unitbase;
}
public BlockchainParameters getParameters() {
return parameters;
}
......
......@@ -27,7 +27,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Joiner;
import org.duniter.core.client.model.bma.EndpointApi;
import org.duniter.core.client.model.bma.NetworkPeering;
import org.duniter.core.client.model.bma.NetworkPeers;
import org.duniter.core.client.model.bma.NetworkPeerings;
import org.duniter.core.util.Preconditions;
import org.duniter.core.util.StringUtils;
import org.duniter.core.util.http.InetAddressUtils;
......@@ -56,6 +56,7 @@ public class Peer implements LocalEntity<String>, Serializable {
private String path;
private Peering peering;
private Stats stats;
public Builder() {
......@@ -160,6 +161,12 @@ public class Peer implements LocalEntity<String>, Serializable {
this.peering.setVersion(remotePeering.getVersion());
this.peering.setSignature(remotePeering.getSignature());
String raw = remotePeering.getRaw();
if (StringUtils.isBlank(raw)) {
raw = remotePeering.toString();
}
this.peering.setRaw(raw);
// Block number+hash
if (remotePeering.getBlock() != null) {
String[] blockParts = remotePeering.getBlock().split("-");
......@@ -172,6 +179,34 @@ public class Peer implements LocalEntity<String>, Serializable {
return this;
}
public Builder setStats(NetworkPeering remotePeering) {
this.stats = this.stats != null ? this.stats : new Stats();
// Block number+hash
if (remotePeering.getBlock() != null) {
String[] blockParts = remotePeering.getBlock().split("-");
if (blockParts.length == 2) {
this.stats.setBlockNumber(Integer.parseInt(blockParts[0]));
this.stats.setBlockHash(blockParts[1]);
}
}
// Update peer status UP/DOWN
if ("UP".equalsIgnoreCase(remotePeering.getStatus())) {
stats.setStatus(Peer.PeerStatus.UP);
// FIXME: Duniter 1.7 return lastUpTime in ms. Check if this a bug or not
stats.setLastUpTime(System.currentTimeMillis());
//stats.setLastUpTime((long)Math.round(System.currentTimeMillis() / 1000));
}
else {
stats.setStatus(Peer.PeerStatus.DOWN);
}
return this;
}
public void setPath(String path) {
this.path = path;
}
......@@ -201,6 +236,10 @@ public class Peer implements LocalEntity<String>, Serializable {
if (this.peering != null) {
ep.setPeering(this.peering);
}
// Stats
if (this.stats != null) {
ep.setStats(this.stats);
}
return ep;
}
......@@ -518,8 +557,9 @@ public class Peer implements LocalEntity<String>, Serializable {
public static final String PROPERTY_SOFTWARE = "software";
public static final String PROPERTY_VERSION = "version";
public static final String PROPERTY_STATUS = "status";
public static final String PROPERTY_LAST_UP_TIME = "lastUpTime";
public static final String PROPERTY_UID = "uid";
public static final String PROPERTY_LAST_UP_TIME = "lastUpTime";
public static final String PROPERTY_FIRST_DOWN_TIME = "firstDownTime";
private String software;
private String version;
......@@ -534,6 +574,7 @@ public class Peer implements LocalEntity<String>, Serializable {
private Double consensusPct = 0d;
private String uid;
private Long lastUpTime;
private Long firstDownTime;
public Stats() {
......@@ -647,5 +688,13 @@ public class Peer implements LocalEntity<String>, Serializable {
public void setLastUpTime(Long lastUpTime) {
this.lastUpTime = lastUpTime;
}
public Long getFirstDownTime() {
return firstDownTime;
}
public void setFirstDownTime(Long firstDownTime) {
this.firstDownTime = firstDownTime;
}
}
}
......@@ -191,12 +191,15 @@ public class PeerServiceImpl implements PeerService, InitializingBean {
log.debug(String.format("[%s] Updating peers (%s endpoints found)", currencyId, peers.size()));
}
final long upTime = System.currentTimeMillis() / 1000;
// FIXME: Duniter 1.7 return lastUpTime in ms. Check if this a bug or not
final long upTime = System.currentTimeMillis();
//final long upTime = System.currentTimeMillis() / 1000;
peers.forEach(peer -> {
// On each UP peers: set last UP time
if (peer.getStats() != null && peer.getStats().isReacheable()) {
peer.getStats().setLastUpTime(upTime);
peer.getStats().setFirstDownTime(null);
}
// Save
save(peer);
......@@ -209,7 +212,6 @@ public class PeerServiceImpl implements PeerService, InitializingBean {
return peerDao.isExists(currencyId, peerId);
}
@Override
public void updatePeersAsDown(String currencyId, Collection<String> filterApis) {
int peerDownTimeoutMs = config.getPeerUpMaxAge();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment