Search in sources :

Example 1 with CommonResponse

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

the class SetLeaderProcessor method processRequest.

@Override
public Message processRequest(SetLeaderRequest request, RpcRequestClosure done) {
    LOG.debug("Processing SetLeaderRequest {}", request.getClass());
    RaftGroupManager nodeManager = this.context.raftNodeManager(RaftSharedContext.DEFAULT_GROUP);
    try {
        nodeManager.setLeader(request.getEndpoint());
        CommonResponse common = CommonResponse.newBuilder().setStatus(true).build();
        return SetLeaderResponse.newBuilder().setCommon(common).build();
    } catch (Throwable e) {
        CommonResponse common = CommonResponse.newBuilder().setStatus(false).setMessage(e.toString()).build();
        return SetLeaderResponse.newBuilder().setCommon(common).build();
    }
}
Also used : RaftGroupManager(com.baidu.hugegraph.backend.store.raft.RaftGroupManager) CommonResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse)

Example 2 with CommonResponse

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

the class ListPeersProcessor method processRequest.

@Override
public Message processRequest(ListPeersRequest request, RpcRequestClosure done) {
    LOG.debug("Processing ListPeersRequest {}", request.getClass());
    RaftGroupManager nodeManager = this.context.raftNodeManager(RaftSharedContext.DEFAULT_GROUP);
    try {
        CommonResponse common = CommonResponse.newBuilder().setStatus(true).build();
        return ListPeersResponse.newBuilder().setCommon(common).addAllEndpoints(nodeManager.listPeers()).build();
    } catch (Throwable e) {
        CommonResponse common = CommonResponse.newBuilder().setStatus(false).setMessage(e.toString()).build();
        return ListPeersResponse.newBuilder().setCommon(common).addAllEndpoints(ImmutableList.of()).build();
    }
}
Also used : RaftGroupManager(com.baidu.hugegraph.backend.store.raft.RaftGroupManager) CommonResponse(com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse)

Example 3 with CommonResponse

use of com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse 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

CommonResponse (com.baidu.hugegraph.backend.store.raft.rpc.RaftRequests.CommonResponse)3 RaftGroupManager (com.baidu.hugegraph.backend.store.raft.RaftGroupManager)2 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 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1