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();
}
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();
}
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();
}
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;
}
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);
}
}
Aggregations