Search in sources :

Example 56 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class RaftGroupManagerImpl method setLeader.

@Override
public String setLeader(String endpoint) {
    PeerId newLeaderId = PeerId.parsePeer(endpoint);
    Node node = this.raftNode.node();
    // If expected endpoint has already been raft leader
    if (node.getLeaderId().equals(newLeaderId)) {
        return newLeaderId.toString();
    }
    if (this.raftNode.selfIsLeader()) {
        // If current node is the leader, transfer directly
        this.transferLeaderTo(endpoint);
    } else {
        // If current node is not leader, forward request to leader
        SetLeaderRequest request = SetLeaderRequest.newBuilder().setEndpoint(endpoint).build();
        try {
            RaftClosure<SetLeaderResponse> future;
            future = this.forwardToLeader(request);
            future.waitFinished();
        } catch (Throwable e) {
            throw new BackendException("Failed to set leader to '%s'", e, endpoint);
        }
    }
    return newLeaderId.toString();
}
Also used : SetLeaderResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderResponse) Node(com.alipay.sofa.jraft.Node) SetLeaderRequest(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.SetLeaderRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 57 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class RaftGroupManagerImpl method addPeer.

@Override
public String addPeer(String endpoint) {
    E.checkArgument(this.raftNode.selfIsLeader(), "Operation add_peer can only be executed on leader");
    PeerId peerId = PeerId.parsePeer(endpoint);
    RaftClosure<?> future = new RaftClosure<>();
    try {
        this.raftNode.node().addPeer(peerId, future);
        future.waitFinished();
    } catch (Throwable e) {
        throw new BackendException("Failed to add peer '%s'", e, endpoint);
    }
    return peerId.toString();
}
Also used : PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 58 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class Example1 method loadData.

public static void loadData(final HugeGraph graph) {
    // will auto open tx (would not auto commit)
    graph.addVertex(T.label, "book", "name", "java-3");
    graph.addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
    graph.addVertex(T.label, "person", "name", "James", "city", "Beijing", "age", 19);
    graph.addVertex(T.label, "person", "name", "Tom Cat", "city", "Beijing", "age", 20);
    graph.addVertex(T.label, "person", "name", "Lisa", "city", "Beijing", "age", 20);
    graph.addVertex(T.label, "person", "name", "Hebe", "city", "Taipei", "age", 21);
    graph.tx().commit();
    // must commit manually with new backend tx (independent of tinkerpop)
    GraphTransaction tx = Whitebox.invoke(graph.getClass(), "openGraphTransaction", graph);
    LOG.info("===============  addVertex  ================");
    Vertex james = tx.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "San Francisco Bay Area");
    Vertex java = tx.addVertex(T.label, "language", "name", "java", "versions", Arrays.asList(6, 7, 8));
    Vertex book1 = tx.addVertex(T.label, "book", "name", "java-1");
    Vertex book2 = tx.addVertex(T.label, "book", "name", "java-2");
    Vertex book3 = tx.addVertex(T.label, "book", "name", "java-3");
    james.addEdge("created", java);
    james.addEdge("authored", book1, "contribution", "1990-1-1", "comment", "it's a good book", "comment", "it's a good book", "comment", "it's a good book too");
    james.addEdge("authored", book2, "contribution", "2017-4-28");
    james.addEdge("write", book2, "time", "2017-4-28");
    james.addEdge("write", book3, "time", "2016-1-1");
    james.addEdge("write", book3, "time", "2017-4-28");
    // commit data changes
    try {
        tx.commit();
    } catch (BackendException e) {
        e.printStackTrace();
        try {
            tx.rollback();
        } catch (BackendException e2) {
            e2.printStackTrace();
        }
    } finally {
        tx.close();
    }
    // use the manually open transaction (tinkerpop tx)
    graph.tx().open();
    graph.addVertex(T.label, "book", "name", "java-3");
    graph.addVertex(T.label, "book", "name", "java-4");
    graph.addVertex(T.label, "book", "name", "java-5");
    graph.tx().commit();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 59 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class GZipUtil method decompress.

public static BytesBuffer decompress(byte[] data) {
    int estimateSize = data.length << 3;
    BytesBuffer output = BytesBuffer.allocate(estimateSize);
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    byte[] buffer = new byte[BUF_SIZE];
    while (!inflater.finished()) {
        try {
            int count = inflater.inflate(buffer);
            output.write(buffer, 0, count);
        } catch (DataFormatException e) {
            throw new BackendException("Failed to decompress", e);
        }
    }
    output.forReadWritten();
    return output;
}
Also used : DataFormatException(java.util.zip.DataFormatException) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Inflater(java.util.zip.Inflater) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 60 with BackendException

use of com.baidu.hugegraph.backend.BackendException in project incubator-hugegraph by apache.

the class KryoUtil method toKryoWithType.

public static byte[] toKryoWithType(Object value) {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Output output = new Output(bos, 256)) {
        kryo().writeClassAndObject(output, value);
        output.flush();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new BackendException("Failed to serialize: %s", e, value);
    }
}
Also used : Output(org.apache.tinkerpop.shaded.kryo.io.Output) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

BackendException (com.baidu.hugegraph.backend.BackendException)79 SQLException (java.sql.SQLException)15 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)6 Status (com.alipay.sofa.jraft.Status)5 PeerId (com.alipay.sofa.jraft.entity.PeerId)5 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)5 BytesBuffer (com.baidu.hugegraph.backend.serializer.BytesBuffer)4 Path (java.nio.file.Path)4 Random (java.util.Random)4 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 RocksDBException (org.rocksdb.RocksDBException)4 HugeGraph (com.baidu.hugegraph.HugeGraph)3 Id (com.baidu.hugegraph.backend.id.Id)3 Query (com.baidu.hugegraph.backend.query.Query)3 HugeConfig (com.baidu.hugegraph.config.HugeConfig)3 Map (java.util.Map)3 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)2 Condition (com.baidu.hugegraph.backend.query.Condition)2 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)2