Search in sources :

Example 1 with StoreCommandRequest

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandRequest in project incubator-hugegraph by apache.

the class RpcForwarder method forwardToLeader.

public void forwardToLeader(PeerId leaderId, StoreCommand command, RaftStoreClosure future) {
    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);
    StoreCommandRequest.Builder builder = StoreCommandRequest.newBuilder();
    builder.setType(command.type());
    builder.setAction(command.action());
    builder.setData(ZeroByteStringHelper.wrap(command.data()));
    StoreCommandRequest request = builder.build();
    RpcResponseClosure<StoreCommandResponse> responseClosure;
    responseClosure = new RpcResponseClosure<StoreCommandResponse>() {

        @Override
        public void setResponse(StoreCommandResponse response) {
            if (response.getStatus()) {
                LOG.debug("StoreCommandResponse status ok");
                future.complete(Status.OK(), () -> null);
            } else {
                LOG.debug("StoreCommandResponse status error");
                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, response.getMessage());
                future.failure(status, e);
            }
        }

        @Override
        public void run(Status status) {
            future.run(status);
        }
    };
    this.waitRpc(leaderId.getEndpoint(), request, responseClosure);
}
Also used : Status(com.alipay.sofa.jraft.Status) StoreCommandRequest(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandRequest) StoreCommandResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandResponse) BackendException(com.baidu.hugegraph.backend.BackendException)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 BackendException (com.baidu.hugegraph.backend.BackendException)1 StoreCommandRequest (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandRequest)1 StoreCommandResponse (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.StoreCommandResponse)1