use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class NodeImpl method executeApplyingTasks.
private void executeApplyingTasks(final List<LogEntryAndClosure> tasks) {
this.writeLock.lock();
try {
final int size = tasks.size();
if (this.state != State.STATE_LEADER) {
final Status st = new Status();
if (this.state != State.STATE_TRANSFERRING) {
st.setError(RaftError.EPERM, "Is not leader.");
} else {
st.setError(RaftError.EBUSY, "Is transferring leadership.");
}
LOG.debug("Node {} can't apply, status={}.", getNodeId(), st);
final List<Closure> dones = tasks.stream().map(ele -> ele.done).collect(Collectors.toList());
Utils.runInThread(() -> {
for (final Closure done : dones) {
done.run(st);
}
});
return;
}
final List<LogEntry> entries = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
final LogEntryAndClosure task = tasks.get(i);
if (task.expectedTerm != -1 && task.expectedTerm != this.currTerm) {
LOG.debug("Node {} can't apply task whose expectedTerm={} doesn't match currTerm={}.", getNodeId(), task.expectedTerm, this.currTerm);
if (task.done != null) {
final Status st = new Status(RaftError.EPERM, "expected_term=%d doesn't match current_term=%d", task.expectedTerm, this.currTerm);
Utils.runClosureInThread(task.done, st);
task.reset();
}
continue;
}
if (!this.ballotBox.appendPendingTask(this.conf.getConf(), this.conf.isStable() ? null : this.conf.getOldConf(), task.done)) {
Utils.runClosureInThread(task.done, new Status(RaftError.EINTERNAL, "Fail to append task."));
task.reset();
continue;
}
// set task entry info before adding to list.
task.entry.getId().setTerm(this.currTerm);
task.entry.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
entries.add(task.entry);
task.reset();
}
this.logManager.appendEntries(entries, new LeaderStableClosure(entries));
// update conf.first
checkAndSetConfiguration(true);
} finally {
this.writeLock.unlock();
}
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class CliServiceTest method sendTestTaskAndWait.
@SuppressWarnings("SameParameterValue")
private void sendTestTaskAndWait(final Node node, final int code) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
final Task task = new Task(data, new ExpectClosure(code, null, latch));
node.apply(task);
}
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
use of com.alipay.sofa.jraft.entity.Task in project incubator-hugegraph by apache.
the class RaftNode method submitCommand.
private void submitCommand(StoreCommand command, RaftStoreClosure future) {
// Wait leader elected
LeaderInfo leaderInfo = this.waitLeaderElected(RaftSharedContext.NO_TIMEOUT);
// If myself is not leader, forward to the leader
if (!leaderInfo.selfIsLeader) {
this.context.rpcForwarder().forwardToLeader(leaderInfo.leaderId, command, future);
return;
}
// Sleep a while when raft node is busy
this.waitIfBusy();
Task task = new Task();
// Compress data, note compress() will return a BytesBuffer
ByteBuffer buffer = LZ4Util.compress(command.data(), RaftSharedContext.BLOCK_SIZE).forReadWritten().asByteBuffer();
LOG.debug("Submit to raft node '{}', the compressed bytes of command " + "{} is {}", this.node, command.action(), buffer.limit());
task.setData(buffer);
task.setDone(future);
this.node.apply(task);
}
use of com.alipay.sofa.jraft.entity.Task in project mmqtt by MrHKing.
the class JRaftServer method applyOperation.
public void applyOperation(Node node, Message data, FailoverClosure closure) {
final Task task = new Task();
task.setDone(new MmqClosure(data, status -> {
MmqClosure.MmqStatus mmqStatus = (MmqClosure.MmqStatus) status;
closure.setThrowable(mmqStatus.getThrowable());
closure.setResponse(mmqStatus.getResponse());
closure.run(mmqStatus);
}));
task.setData(ByteBuffer.wrap(data.toByteArray()));
node.apply(task);
}
use of com.alipay.sofa.jraft.entity.Task 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);
}
Aggregations