use of com.alipay.sofa.jraft.Closure in project jdchain-core by blockchain-jd-com.
the class RaftNodeServerServiceImpl method handleSubmitTxRequest.
@Override
public void handleSubmitTxRequest(SubmitTxRequest submitTxRequest, Closure done) {
RpcResponseClosure responseClosure = (RpcResponseClosure) done;
if (submitTxRequest.getTx() == null || submitTxRequest.getTx().length == 0) {
done.run(new Status(RaftError.EREQUEST, "tx is empty"));
return;
}
applyRequest(submitTxRequest, responseClosure, (req, closure) -> {
int retryTimes = 0;
try {
final EventTranslator<SubmitTx> translator = (event, sequence) -> {
event.reset();
event.setDone(responseClosure);
event.setValues(submitTxRequest.getTx());
};
while (true) {
if (this.submitTxQueue.tryPublishEvent(translator)) {
break;
} else {
retryTimes++;
if (retryTimes > MAX_SUBMIT_RETRY_TIMES) {
LOGGER.error("node {} submit request is overload.", nodeServer.getNode().getNodeId());
responseClosure.run(new Status(RaftError.EBUSY, "node has too many tasks."));
return;
}
ThreadHelper.onSpinWait();
}
}
} catch (final Exception e) {
LoggerUtils.errorIfEnabled(LOGGER, "fail to publish request:{} error", req, e);
responseClosure.run(new Status(RaftError.EPERM, "publish tx error: %s", e.getMessage()));
}
});
}
use of com.alipay.sofa.jraft.Closure in project jdchain-core by blockchain-jd-com.
the class RaftNodeServerServiceImpl method proposalBlock.
private void proposalBlock(List<SubmitTx> submitTxList) {
if (!proposer.canPropose()) {
LoggerUtils.debugIfEnabled(LOGGER, "node {} can't propose block", nodeServer.getNode().getNodeId());
for (SubmitTx submitTx : submitTxList) {
submitTx.getDone().run(new Status(RaftError.ENEWLEADER, "node {} can't propose block", nodeServer.getNode().getNodeId()));
}
return;
}
LoggerUtils.debugIfEnabled(LOGGER, "node: {} begin proposal block, proposal tx size: {}", nodeServer.getNode().getNodeId(), submitTxList.size());
List<byte[]> txList = submitTxList.stream().map(SubmitTx::getValues).collect(Collectors.toList());
List<Closure> doneList = submitTxList.stream().map(SubmitTx::getDone).collect(Collectors.toList());
try {
final Task task = new Task();
Block block = proposer.proposeBlock(txList);
task.setData(ByteBuffer.wrap(serializer.serialize(block)));
task.setDone(new BlockClosure(block, doneList));
nodeServer.getNode().apply(task);
} catch (Exception e) {
LOGGER.error("proposal block error", e);
// todo retry ?
for (Closure closure : doneList) {
runSubmitTxClosure(closure, i -> RpcResponse.fail(RaftError.UNKNOWN.getNumber(), e.getMessage()), 0);
}
}
}
use of com.alipay.sofa.jraft.Closure in project jdchain-core by blockchain-jd-com.
the class BlockClosure method run.
@Override
public void run(Status status) {
int i = 0;
for (Closure closure : this.getDoneList()) {
int index = i;
RpcResponseClosure submitTxDone = (RpcResponseClosure) closure;
if (submitTxDone != null) {
Utils.runClosureInThread(s -> {
if (status.isOk()) {
AsyncFuture<byte[]> future = getFuture(index);
submitTxDone.setResponse(RpcResponse.success(future.get()));
} else {
submitTxDone.setResponse(RpcResponse.fail(status.getCode(), status.getErrorMsg()));
}
submitTxDone.run(s);
});
}
i++;
}
}
Aggregations