Search in sources :

Example 6 with ComputerException

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

the class FileGraphPartition method output.

protected PartitionStat output() {
    ComputerOutput output = this.context.config().createObject(ComputerOptions.OUTPUT_CLASS);
    output.init(this.context.config(), this.partition);
    try {
        this.beforeOutput();
    } catch (IOException e) {
        throw new ComputerException("Error occurred when beforeOutput", e);
    }
    Value result = this.context.config().createObject(ComputerOptions.ALGORITHM_RESULT_CLASS);
    while (this.vertexInput.hasNext()) {
        Vertex vertex = this.vertexInput.next();
        this.readVertexStatusAndValue(vertex, result);
        Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
        vertex.edges(edges);
        output.write(vertex);
    }
    try {
        this.afterOutput();
    } catch (IOException e) {
        throw new ComputerException("Error occurred when afterOutput", e);
    }
    output.close();
    return new PartitionStat(this.partition, this.vertexCount, this.edgeCount, 0L);
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) PartitionStat(com.baidu.hugegraph.computer.core.graph.partition.PartitionStat) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IOException(java.io.IOException) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) ComputerOutput(com.baidu.hugegraph.computer.core.output.ComputerOutput)

Example 7 with ComputerException

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

the class EdgesInput method readEdges.

// TODO: use one reused Edges instance to read batches for each vertex.
private Edges readEdges(RandomAccessInput in) {
    try {
        int count = in.readFixedInt();
        Edges edges = this.graphFactory.createEdges(count);
        if (this.frequency == EdgeFrequency.SINGLE) {
            for (int i = 0; i < count; i++) {
                Edge edge = this.graphFactory.createEdge();
                // Only use targetId as subKey, use props as subValue
                edge.targetId(StreamGraphInput.readId(in));
                // Read subValue
                Properties props = this.graphFactory.createProperties();
                props.read(in);
                edge.properties(props);
                edges.add(edge);
            }
        } else if (this.frequency == EdgeFrequency.SINGLE_PER_LABEL) {
            for (int i = 0; i < count; i++) {
                Edge edge = this.graphFactory.createEdge();
                // Use label + targetId as subKey, use props as subValue
                edge.label(StreamGraphInput.readLabel(in));
                edge.targetId(StreamGraphInput.readId(in));
                // Read subValue
                Properties props = this.graphFactory.createProperties();
                props.read(in);
                edge.properties(props);
                edges.add(edge);
            }
        } else {
            assert this.frequency == EdgeFrequency.MULTIPLE;
            for (int i = 0; i < count; i++) {
                Edge edge = this.graphFactory.createEdge();
                /*
                     * Use label + sortValues + targetId as subKey,
                     * use properties as subValue
                     */
                edge.label(StreamGraphInput.readLabel(in));
                edge.name(StreamGraphInput.readLabel(in));
                edge.targetId(StreamGraphInput.readId(in));
                // Read subValue
                Properties props = this.graphFactory.createProperties();
                props.read(in);
                edge.properties(props);
                edges.add(edge);
            }
        }
        return edges;
    } catch (IOException e) {
        throw new ComputerException("Failed to read edges from input '%s'", e, this.edgeFile.getAbsoluteFile());
    }
}
Also used : IOException(java.io.IOException) Properties(com.baidu.hugegraph.computer.core.graph.properties.Properties) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) Edge(com.baidu.hugegraph.computer.core.graph.edge.Edge) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 8 with ComputerException

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

the class FileGraphPartition method compute1.

private long compute1(ComputationContext context) {
    Value result = this.context.config().createObject(ComputerOptions.ALGORITHM_RESULT_CLASS);
    long activeVertexCount = 0L;
    while (this.vertexInput.hasNext()) {
        Vertex vertex = this.vertexInput.next();
        this.readVertexStatusAndValue(vertex, result);
        Iterator<Value> messageIter = this.messageInput.iterator(this.vertexInput.idPointer());
        if (messageIter.hasNext()) {
            vertex.reactivate();
        }
        /*
             * If the vertex is inactive, it's edges will be skipped
             * automatically at the next vertex.
             */
        if (vertex.active()) {
            Edges edges = this.edgesInput.edges(this.vertexInput.idPointer());
            vertex.edges(edges);
            this.computation.compute(context, vertex, messageIter);
        }
        // The vertex status may be changed after computation
        if (vertex.active()) {
            activeVertexCount++;
        }
        try {
            this.saveVertexStatusAndValue(vertex);
        } catch (IOException e) {
            throw new ComputerException("Error occurred when saveVertex", e);
        }
    }
    return activeVertexCount;
}
Also used : Vertex(com.baidu.hugegraph.computer.core.graph.vertex.Vertex) Value(com.baidu.hugegraph.computer.core.graph.value.Value) IOException(java.io.IOException) Edges(com.baidu.hugegraph.computer.core.graph.edge.Edges) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 9 with ComputerException

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

the class VertexValueCombiner method combine.

@Override
public Pointer combine(Pointer v1, Pointer v2) {
    try {
        RandomAccessInput input1 = v1.input();
        RandomAccessInput input2 = v2.input();
        input1.seek(v1.offset());
        input2.seek(v2.offset());
        String label1 = StreamGraphInput.readLabel(input1);
        String label2 = StreamGraphInput.readLabel(input2);
        assert label1.equals(label2);
        this.v1.read(input1);
        this.v2.read(input2);
        this.combiner.combine(this.v1, this.v2, this.result);
        this.output.seek(0L);
        this.output.writeUTF(label1);
        this.result.write(this.output);
        return new InlinePointer(this.output.buffer(), this.output.position());
    } catch (Exception e) {
        throw new ComputerException("Failed to combine pointer1(offset=%s, length=%s) and " + "pointer2(offset=%s, length=%s)'", e, v1.offset(), v1.length(), v2.offset(), v2.length());
    }
}
Also used : RandomAccessInput(com.baidu.hugegraph.computer.core.io.RandomAccessInput) InlinePointer(com.baidu.hugegraph.computer.core.store.entry.InlinePointer) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

Example 10 with ComputerException

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

the class EtcdClient method getWithPrefix.

/**
 * Get the values of keys with the specified prefix.
 * If no key found, return empty list.
 */
public List<byte[]> getWithPrefix(String prefix) {
    E.checkArgumentNotNull(prefix, "The prefix can't be null");
    try {
        ByteSequence prefixSeq = ByteSequence.from(prefix, ENCODING);
        GetOption getOption = GetOption.newBuilder().withPrefix(prefixSeq).withSortOrder(SortOrder.ASCEND).build();
        GetResponse response = this.kv.get(prefixSeq, getOption).get();
        if (response.getCount() > 0) {
            return getResponseValues(response);
        } else {
            return Collections.emptyList();
        }
    } catch (InterruptedException e) {
        throw new ComputerException("Interrupted while getting with prefix='%s'", e, prefix);
    } catch (ExecutionException e) {
        throw new ComputerException("Error while getting with prefix='%s'", e, prefix);
    }
}
Also used : GetOption(io.etcd.jetcd.options.GetOption) ExecutionException(java.util.concurrent.ExecutionException) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) ComputerException(com.baidu.hugegraph.computer.core.common.exception.ComputerException)

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