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