Search in sources :

Example 41 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class ComputeManager method input.

public WorkerStat input() {
    WorkerStat workerStat = new WorkerStat();
    this.recvManager.waitReceivedAllMessages();
    Map<Integer, PeekableIterator<KvEntry>> vertices = this.recvManager.vertexPartitions();
    Map<Integer, PeekableIterator<KvEntry>> edges = this.recvManager.edgePartitions();
    // TODO: parallel input process
    for (Map.Entry<Integer, PeekableIterator<KvEntry>> entry : vertices.entrySet()) {
        int partition = entry.getKey();
        PeekableIterator<KvEntry> vertexIter = entry.getValue();
        PeekableIterator<KvEntry> edgesIter = edges.getOrDefault(partition, PeekableIterator.emptyIterator());
        FileGraphPartition part = new FileGraphPartition(this.context, this.managers, partition);
        PartitionStat partitionStat = null;
        ComputerException inputException = null;
        try {
            partitionStat = part.input(vertexIter, edgesIter);
        } catch (ComputerException e) {
            inputException = e;
        } finally {
            try {
                vertexIter.close();
                edgesIter.close();
            } catch (Exception e) {
                String message = "Failed to close vertex or edge file " + "iterator";
                ComputerException closeException = new ComputerException(message, e);
                if (inputException != null) {
                    inputException.addSuppressed(closeException);
                } else {
                    throw closeException;
                }
            }
            if (inputException != null) {
                throw inputException;
            }
        }
        workerStat.add(partitionStat);
        this.partitions.put(partition, part);
    }
    return workerStat;
}
Also used : KvEntry(com.baidu.hugegraph.computer.core.store.entry.KvEntry) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) PartitionStat(com.baidu.hugegraph.computer.core.graph.partition.PartitionStat) WorkerStat(com.baidu.hugegraph.computer.core.worker.WorkerStat) PeekableIterator(com.baidu.hugegraph.computer.core.sort.flusher.PeekableIterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 42 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class FileGraphPartition method compute.

protected PartitionStat compute(WorkerContext context, int superstep) {
    LOG.info("Partition {} begin compute in superstep {}", this.partition, superstep);
    try {
        this.beforeCompute(superstep);
    } catch (IOException e) {
        throw new ComputerException("Error occurred when beforeCompute at superstep %s", e, superstep);
    }
    long activeVertexCount;
    try {
        this.computation.beforeSuperstep(context);
        activeVertexCount = superstep == 0 ? this.compute0(context) : this.compute1(context);
        this.computation.afterSuperstep(context);
    } catch (Exception e) {
        throw new ComputerException("Error occurred when compute at superstep %s", e, superstep);
    }
    try {
        this.afterCompute(superstep);
    } catch (Exception e) {
        throw new ComputerException("Error occurred when afterCompute at superstep %s", e, superstep);
    }
    LOG.info("Partition {} finish compute in superstep {}", this.partition, superstep);
    return new PartitionStat(this.partition, this.vertexCount, this.edgeCount, this.vertexCount - activeVertexCount);
}
Also used : PartitionStat(com.baidu.hugegraph.computer.core.graph.partition.PartitionStat) IOException(java.io.IOException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) IOException(java.io.IOException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 43 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class FileGraphPartition method readVertexStatusAndValue.

private void readVertexStatusAndValue(Vertex vertex, Value result) {
    try {
        boolean activate = this.preStatusInput.readBoolean();
        if (activate) {
            vertex.reactivate();
        } else {
            vertex.inactivate();
        }
    } catch (IOException e) {
        throw new ComputerException("Failed to read status of vertex '%s'", e, vertex);
    }
    try {
        result.read(this.preValueInput);
        vertex.value(result);
    } catch (IOException e) {
        throw new ComputerException("Failed to read value of vertex '%s'", e, vertex);
    }
}
Also used : IOException(java.io.IOException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 44 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class FileGraphPartition method compute0.

private long compute0(ComputationContext context) {
    long activeVertexCount = 0L;
    while (this.vertexInput.hasNext()) {
        Vertex vertex = this.vertexInput.next();
        vertex.reactivate();
        Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
        vertex.edges(edges);
        this.computation.compute0(context, vertex);
        if (vertex.active()) {
            activeVertexCount++;
        }
        try {
            this.saveVertexStatusAndValue(vertex);
        } catch (IOException e) {
            throw new ComputerException("Error occurred when saveVertex: %s", e, vertex);
        }
    }
    return activeVertexCount;
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) IOException(java.io.IOException) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 45 with ComputerException

use of com.baidu.hugegraph.computer.core.common.exception.ComputerException in project hugegraph-computer by hugegraph.

the class EtcdClient method waitAndPrefixGetFromPutEvent.

/**
 * Wait at most expected eventCount events triggered in timeout ms.
 * This method wait at most timeout ms regardless whether expected
 * eventCount events triggered.
 * @param existedKeyValues readonly
 */
private List<byte[]> waitAndPrefixGetFromPutEvent(ByteSequence prefixSeq, int count, List<KeyValue> existedKeyValues, long revision, long timeout, long logInterval) throws InterruptedException {
    Map<ByteSequence, ByteSequence> keyValues = new ConcurrentHashMap<>();
    for (KeyValue kv : existedKeyValues) {
        keyValues.put(kv.getKey(), kv.getValue());
    }
    WaitEvent<List<byte[]>> barrierEvent = new WaitEvent<>();
    Consumer<WatchResponse> consumer = watchResponse -> {
        List<WatchEvent> events = watchResponse.getEvents();
        for (WatchEvent event : events) {
            if (EventType.PUT.equals(event.getEventType())) {
                KeyValue keyValue = event.getKeyValue();
                keyValues.put(keyValue.getKey(), keyValue.getValue());
                if (keyValues.size() == count) {
                    List<byte[]> result = new ArrayList<>(count);
                    for (ByteSequence byteSequence : keyValues.values()) {
                        result.add(byteSequence.getBytes());
                    }
                    barrierEvent.signalAll(result);
                }
            } else if (EventType.DELETE.equals(event.getEventType())) {
                keyValues.remove(event.getKeyValue().getKey());
            } else {
                throw new ComputerException("Unexpected event type '%s'", event.getEventType());
            }
        }
    };
    WatchOption watchOption = WatchOption.newBuilder().withPrefix(prefixSeq).withRevision(revision).build();
    try (Watch.Watcher watcher = this.watch.watch(prefixSeq, watchOption, consumer)) {
        return barrierEvent.await(timeout, logInterval, () -> {
            LOG.info("Wait for keys with prefix '{}' and timeout {}ms, " + "expect {} keys but actual got {} keys", prefixSeq.toString(ENCODING), timeout, count, keyValues.size());
        });
    }
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) Charset(java.nio.charset.Charset) ByteSequence(io.etcd.jetcd.ByteSequence) Map(java.util.Map) GetResponse(io.etcd.jetcd.kv.GetResponse) E(com.baidu.hugegraph.util.E) GetOption(io.etcd.jetcd.options.GetOption) KV(io.etcd.jetcd.KV) Logger(org.slf4j.Logger) EventType(io.etcd.jetcd.watch.WatchEvent.EventType) SortOrder(io.etcd.jetcd.options.GetOption.SortOrder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) KeyValue(io.etcd.jetcd.KeyValue) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) Log(com.baidu.hugegraph.util.Log) WatchResponse(io.etcd.jetcd.watch.WatchResponse) WatchOption(io.etcd.jetcd.options.WatchOption) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) KeyValue(io.etcd.jetcd.KeyValue) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) Watch(io.etcd.jetcd.Watch) ArrayList(java.util.ArrayList) List(java.util.List) WatchEvent(io.etcd.jetcd.watch.WatchEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ByteSequence(io.etcd.jetcd.ByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption)

Aggregations

ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)61 IOException (java.io.IOException)27 ExecutionException (java.util.concurrent.ExecutionException)13 Edges (com.baidu.hugegraph.computer.core.graph.edge.Edges)10 ByteSequence (io.etcd.jetcd.ByteSequence)9 ArrayList (java.util.ArrayList)8 GetResponse (io.etcd.jetcd.kv.GetResponse)7 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)6 RandomAccessInput (com.baidu.hugegraph.computer.core.io.RandomAccessInput)6 PartitionStat (com.baidu.hugegraph.computer.core.graph.partition.PartitionStat)5 GetOption (io.etcd.jetcd.options.GetOption)5 Map (java.util.Map)5 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)4 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)3 Value (com.baidu.hugegraph.computer.core.graph.value.Value)3 MessageStat (com.baidu.hugegraph.computer.core.receiver.MessageStat)3 KeyValue (io.etcd.jetcd.KeyValue)3 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)3