Search in sources :

Example 1 with ItemState

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];
    });
}
Also used : BiDeserializer(net.sergeych.biserializer.BiDeserializer) HttpURLConnection(java.net.HttpURLConnection) NonNull(org.checkerframework.checker.nullness.qual.NonNull) java.util(java.util) URL(java.net.URL) Binder(net.sergeych.tools.Binder) ErrorRecord(com.icodici.universa.ErrorRecord) PublicKey(com.icodici.crypto.PublicKey) Contract(com.icodici.universa.contract.Contract) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncEvent(net.sergeych.tools.AsyncEvent) ExecutorService(java.util.concurrent.ExecutorService) ItemResult(com.icodici.universa.node.ItemResult) Reporter(net.sergeych.tools.Reporter) PrivateKey(com.icodici.crypto.PrivateKey) com.icodici.universa.node2(com.icodici.universa.node2) Do(net.sergeych.tools.Do) IOException(java.io.IOException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ItemState(com.icodici.universa.node.ItemState) Executors(java.util.concurrent.Executors) EncryptionError(com.icodici.crypto.EncryptionError) Approvable(com.icodici.universa.Approvable) Parcel(com.icodici.universa.contract.Parcel) HashId(com.icodici.universa.HashId) DefaultBiMapper(net.sergeych.biserializer.DefaultBiMapper) Boss(net.sergeych.boss.Boss) IOException(java.io.IOException) AsyncEvent(net.sergeych.tools.AsyncEvent) ItemResult(com.icodici.universa.node.ItemResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ItemState(com.icodici.universa.node.ItemState) ExecutorService(java.util.concurrent.ExecutorService) ErrorRecord(com.icodici.universa.ErrorRecord)

Example 2 with ItemState

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);
    }
}
Also used : HashId(com.icodici.universa.HashId) ItemState(com.icodici.universa.node.ItemState)

Aggregations

HashId (com.icodici.universa.HashId)2 ItemState (com.icodici.universa.node.ItemState)2 EncryptionError (com.icodici.crypto.EncryptionError)1 PrivateKey (com.icodici.crypto.PrivateKey)1 PublicKey (com.icodici.crypto.PublicKey)1 Approvable (com.icodici.universa.Approvable)1 ErrorRecord (com.icodici.universa.ErrorRecord)1 Contract (com.icodici.universa.contract.Contract)1 Parcel (com.icodici.universa.contract.Parcel)1 ItemResult (com.icodici.universa.node.ItemResult)1 com.icodici.universa.node2 (com.icodici.universa.node2)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Instant (java.time.Instant)1 java.util (java.util)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1