Search in sources :

Example 16 with BackendException

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

the class RaftGroupManagerImpl method transferLeaderTo.

@Override
public String transferLeaderTo(String endpoint) {
    PeerId peerId = PeerId.parsePeer(endpoint);
    Status status = this.raftNode.node().transferLeadershipTo(peerId);
    if (!status.isOk()) {
        throw new BackendException("Failed to transfer leader to '%s', raft error: %s", endpoint, status.getErrorMsg());
    }
    return peerId.toString();
}
Also used : Status(com.alipay.sofa.jraft.Status) PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 17 with BackendException

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

the class RaftGroupManagerImpl method listPeers.

@Override
public List<String> listPeers() {
    if (this.raftNode.selfIsLeader()) {
        List<PeerId> peerIds = this.raftNode.node().listPeers();
        return peerIds.stream().map(PeerId::toString).collect(Collectors.toList());
    }
    // If current node is not leader, forward request to leader
    ListPeersRequest request = ListPeersRequest.getDefaultInstance();
    try {
        RaftClosure<ListPeersResponse> future;
        future = this.forwardToLeader(request);
        ListPeersResponse response = future.waitFinished();
        return response.getEndpointsList();
    } catch (Throwable e) {
        throw new BackendException("Failed to list peers", e);
    }
}
Also used : ListPeersRequest(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.ListPeersRequest) ListPeersResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.ListPeersResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 18 with BackendException

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

the class BinarySerializer method parseProperty.

protected void parseProperty(Id pkeyId, BytesBuffer buffer, HugeElement owner) {
    PropertyKey pkey = owner.graph().propertyKey(pkeyId);
    // Parse value
    Object value = buffer.readProperty(pkey);
    // Set properties of vertex/edge
    if (pkey.cardinality() == Cardinality.SINGLE) {
        owner.addProperty(pkey, value);
    } else {
        if (!(value instanceof Collection)) {
            throw new BackendException("Invalid value of non-single property: %s", value);
        }
        owner.addProperty(pkey, value);
    }
}
Also used : Collection(java.util.Collection) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 19 with BackendException

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

the class InMemoryDBTable method matchCondition.

private static boolean matchCondition(BackendEntry item, Condition c) {
    // TODO: Compatible with BackendEntry
    TextBackendEntry entry = (TextBackendEntry) item;
    // Not supported by memory
    if (!(c instanceof Condition.Relation)) {
        throw new BackendException("Unsupported condition: " + c);
    }
    Condition.Relation r = (Condition.Relation) c;
    String key = r.serialKey().toString();
    // TODO: deal with others Relation like: <, >=, ...
    if (r.relation() == Condition.RelationType.CONTAINS_KEY) {
        return entry.contains(r.serialValue().toString());
    } else if (r.relation() == Condition.RelationType.CONTAINS_VALUE) {
        return entry.containsValue(r.serialValue().toString());
    } else if (r.relation() == Condition.RelationType.EQ) {
        return entry.contains(key, r.serialValue().toString());
    } else if (entry.contains(key)) {
        return r.test(entry.column(key));
    }
    return false;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) TextBackendEntry(com.baidu.hugegraph.backend.serializer.TextBackendEntry) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 20 with BackendException

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

the class RaftBackendStore method queryByRaft.

private Object queryByRaft(Object query, boolean safeRead, Function<Object, Object> func) {
    if (!safeRead) {
        return func.apply(query);
    }
    RaftClosure<Object> future = new RaftClosure<>();
    ReadIndexClosure readIndexClosure = new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            if (status.isOk()) {
                future.complete(status, () -> func.apply(query));
            } else {
                future.failure(status, new BackendException("Failed to do raft read-index: %s", status));
            }
        }
    };
    this.node().readIndex(BytesUtil.EMPTY_BYTES, readIndexClosure);
    try {
        return future.waitFinished();
    } catch (Throwable e) {
        LOG.warn("Failed to execute query '{}': {}", query, future.status(), e);
        throw new BackendException("Failed to execute query: %s", e, query);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) 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