use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class NodeImpl method apply.
@Override
public void apply(final Task task) {
if (this.shutdownLatch != null) {
Utils.runClosureInThread(task.getDone(), new Status(RaftError.ENODESHUTDOWN, "Node is shutting down."));
throw new IllegalStateException("Node is shutting down");
}
Requires.requireNonNull(task, "Null task");
final LogEntry entry = new LogEntry();
entry.setData(task.getData());
final EventTranslator<LogEntryAndClosure> translator = (event, sequence) -> {
event.reset();
event.done = task.getDone();
event.entry = entry;
event.expectedTerm = task.getExpectedTerm();
};
switch(this.options.getApplyTaskMode()) {
case Blocking:
this.applyQueue.publishEvent(translator);
break;
case NonBlocking:
default:
if (!this.applyQueue.tryPublishEvent(translator)) {
String errorMsg = "Node is busy, has too many tasks, queue is full and bufferSize=" + this.applyQueue.getBufferSize();
Utils.runClosureInThread(task.getDone(), new Status(RaftError.EBUSY, errorMsg));
LOG.warn("Node {} applyQueue is overload.", getNodeId());
this.metrics.recordTimes("apply-task-overload-times", 1);
if (task.getDone() == null) {
throw new OverloadException(errorMsg);
}
}
break;
}
}
use of com.alipay.sofa.jraft.entity.Task 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);
}
}
}
Aggregations