Search in sources :

Example 1 with RaftClosure

use of com.baidu.hugegraph.backend.store.raft.RaftClosure in project incubator-hugegraph by apache.

the class RpcForwarder method forwardToLeader.

public <T extends Message> RaftClosure<T> forwardToLeader(PeerId leaderId, Message request) {
    E.checkNotNull(leaderId, "leader id");
    E.checkState(!leaderId.equals(this.nodeId), "Invalid state: current node is the leader, there is " + "no need to forward the request");
    LOG.debug("The node '{}' forward request to leader '{}'", this.nodeId, leaderId);
    RaftClosure<T> future = new RaftClosure<>();
    RpcResponseClosure<T> responseDone = new RpcResponseClosure<T>() {

        @Override
        public void setResponse(T response) {
            FieldDescriptor fd = response.getDescriptorForType().findFieldByName("common");
            Object object = response.getField(fd);
            E.checkState(object instanceof CommonResponse, "The common field must be instance of " + "CommonResponse, actual is '%s'", object != null ? object.getClass() : null);
            CommonResponse commonResponse = (CommonResponse) object;
            if (commonResponse.getStatus()) {
                future.complete(Status.OK(), () -> response);
            } else {
                Status status = new Status(RaftError.UNKNOWN, "fowared request failed");
                BackendException e = new BackendException("Current node isn't leader, leader " + "is [%s], failed to forward request " + "to leader: %s", leaderId, commonResponse.getMessage());
                future.failure(status, e);
            }
        }

        @Override
        public void run(Status status) {
            future.run(status);
        }
    };
    this.waitRpc(leaderId.getEndpoint(), request, responseDone);
    return future;
}
Also used : Status(com.alipay.sofa.jraft.Status) WAIT_RPC_TIMEOUT(com.baidu.hugegraph.backend.store.raft.RaftSharedContext.WAIT_RPC_TIMEOUT) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) CommonResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse) RaftClosure(com.baidu.hugegraph.backend.store.raft.RaftClosure) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)1 BackendException (com.baidu.hugegraph.backend.BackendException)1 RaftClosure (com.baidu.hugegraph.backend.store.raft.RaftClosure)1 WAIT_RPC_TIMEOUT (com.baidu.hugegraph.backend.store.raft.RaftSharedContext.WAIT_RPC_TIMEOUT)1 CommonResponse (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1