use of com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure in project nacos by alibaba.
the class JRaftServer method applyOperation.
public void applyOperation(Node node, Message data, FailoverClosure closure) {
final Task task = new Task();
task.setDone(new NacosClosure(data, status -> {
NacosClosure.NacosStatus nacosStatus = (NacosClosure.NacosStatus) status;
closure.setThrowable(nacosStatus.getThrowable());
closure.setResponse(nacosStatus.getResponse());
closure.run(nacosStatus);
}));
// add request type field at the head of task data.
byte[] requestTypeFieldBytes = new byte[2];
requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
if (data instanceof ReadRequest) {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
} else {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
}
byte[] dataBytes = data.toByteArray();
task.setData((ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes).put(dataBytes).position(0));
node.apply(task);
}
use of com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure in project nacos by alibaba.
the class AbstractProcessor method execute.
protected void execute(JRaftServer server, final RpcContext asyncCtx, final Message message, final JRaftServer.RaftGroupTuple tuple) {
FailoverClosure closure = new FailoverClosure() {
Response data;
Throwable ex;
@Override
public void setResponse(Response data) {
this.data = data;
}
@Override
public void setThrowable(Throwable throwable) {
this.ex = throwable;
}
@Override
public void run(Status status) {
if (Objects.nonNull(ex)) {
Loggers.RAFT.error("execute has error : ", ex);
asyncCtx.sendResponse(Response.newBuilder().setErrMsg(ex.toString()).setSuccess(false).build());
} else {
asyncCtx.sendResponse(data);
}
}
};
server.applyOperation(tuple.getNode(), message, closure);
}
Aggregations