use of com.icodici.universa.node.ItemState in project universa by UniversaBlockchain.
the class Client method getState.
public ItemResult getState(HashId itemId, Reporter reporter) throws ClientError {
final ExecutorService pool = Executors.newCachedThreadPool();
final List<ErrorRecord> errors = new ArrayList<>();
final AsyncEvent<Void> consensusFound = new AsyncEvent<>();
final int checkConsensus = getNodes().size() / 3;
final AtomicInteger nodesLeft = new AtomicInteger(nodes.size());
return protect(() -> {
final Map<ItemState, List<ItemResult>> states = new HashMap<>();
for (int i = 0; i < nodes.size(); i++) {
final int nn = i;
pool.submit(() -> {
for (int retry = 0; retry < 5; retry++) {
// reporter.verbose("trying "+reporter+" for node "+nn);
try {
Client c = getClient(nn);
ItemResult r = c.command("getState", "itemId", itemId).getOrThrow("itemResult");
r.meta.put("url", c.getNodeNumber());
synchronized (states) {
List<ItemResult> list = states.get(r.state);
if (list == null) {
list = new ArrayList();
states.put(r.state, list);
}
list.add(r);
if (r.errors.size() > 0)
reporter.warning("errors from " + c.getNodeNumber() + ": " + r.errors);
break;
}
} catch (IOException e) {
// reporter.warning("can't get answer from node " + nn + ", retry #" + retry + ": " + e);
}
}
// Now we should check the consensus
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
if (itemResults.size() >= checkConsensus) {
consensusFound.fire();
return;
}
});
if (nodesLeft.decrementAndGet() < 1)
consensusFound.fire();
});
}
consensusFound.await(5000);
pool.shutdownNow();
final ItemResult[] consensus = new ItemResult[1];
states.forEach((itemState, itemResults) -> {
if (itemResults.size() >= checkConsensus)
consensus[0] = itemResults.get(0);
});
if (consensus[0] != null)
reporter.message("State consensus found:" + consensus[0]);
else {
reporter.warning("no consensus found " + states.size());
}
if (states.size() > 1) {
states.entrySet().stream().sorted(Comparator.comparingInt(o -> o.getValue().size())).forEach(kv -> {
List<ItemResult> itemResults = kv.getValue();
reporter.message("" + kv.getKey() + ": " + itemResults.size() + ": " + itemResults.stream().map(x -> x.meta.getStringOrThrow("url")).collect(Collectors.toSet()));
});
}
return consensus[0];
});
}
use of com.icodici.universa.node.ItemState in project universa by UniversaBlockchain.
the class ItemResyncNotification method readFrom.
@Override
protected void readFrom(Boss.Reader br) throws IOException {
super.readFrom(br);
Map<String, Object> packingMap = br.readMap();
for (String s : packingMap.keySet()) {
HashId hid = HashId.withDigest(s);
ItemState state = ItemState.values()[(int) packingMap.get(s)];
itemsToResync.put(hid, state);
}
}
Aggregations