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