use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class FSMCallerImpl method doCommitted.
private void doCommitted(final long committedIndex) {
if (!this.error.getStatus().isOk()) {
return;
}
final long lastAppliedIndex = this.lastAppliedIndex.get();
// We can tolerate the disorder of committed_index
if (lastAppliedIndex >= committedIndex) {
return;
}
final long startMs = Utils.monotonicMs();
try {
final List<Closure> closures = new ArrayList<>();
final List<TaskClosure> taskClosures = new ArrayList<>();
final long firstClosureIndex = this.closureQueue.popClosureUntil(committedIndex, closures, taskClosures);
// Calls TaskClosure#onCommitted if necessary
onTaskCommitted(taskClosures);
Requires.requireTrue(firstClosureIndex >= 0, "Invalid firstClosureIndex");
final IteratorImpl iterImpl = new IteratorImpl(this.fsm, this.logManager, closures, firstClosureIndex, lastAppliedIndex, committedIndex, this.applyingIndex, this.node.getOptions());
while (iterImpl.isGood()) {
final LogEntry logEntry = iterImpl.entry();
if (logEntry.getType() != EnumOutter.EntryType.ENTRY_TYPE_DATA) {
if (logEntry.getType() == EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION) {
if (logEntry.getOldPeers() != null && !logEntry.getOldPeers().isEmpty()) {
// Joint stage is not supposed to be noticeable by end users.
this.fsm.onConfigurationCommitted(new Configuration(iterImpl.entry().getPeers()));
}
}
if (iterImpl.done() != null) {
// For other entries, we have nothing to do besides flush the
// pending tasks and run this closure to notify the caller that the
// entries before this one were successfully committed and applied.
iterImpl.done().run(Status.OK());
}
iterImpl.next();
continue;
}
// Apply data task to user state machine
doApplyTasks(iterImpl);
}
if (iterImpl.hasError()) {
setError(iterImpl.getError());
iterImpl.runTheRestClosureWithError();
}
final long lastIndex = iterImpl.getIndex() - 1;
final long lastTerm = this.logManager.getTerm(lastIndex);
final LogId lastAppliedId = new LogId(lastIndex, lastTerm);
this.lastAppliedIndex.set(lastIndex);
this.lastAppliedTerm = lastTerm;
this.logManager.setAppliedId(lastAppliedId);
notifyLastAppliedIndexUpdated(lastIndex);
} finally {
this.nodeMetrics.recordLatency("fsm-commit", Utils.monotonicMs() - startMs);
}
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class ClosureQueueImpl method clear.
@Override
public void clear() {
List<Closure> savedQueue;
this.lock.lock();
try {
this.firstIndex = 0;
savedQueue = this.queue;
this.queue = new LinkedList<>();
} finally {
this.lock.unlock();
}
final Status status = new Status(RaftError.EPERM, "Leader stepped down");
Utils.runInThread(options.getCommonExecutor(), () -> {
for (final Closure done : savedQueue) {
if (done != null) {
done.run(status);
}
}
});
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class ClosureQueueImpl method popClosureUntil.
@Override
public long popClosureUntil(final long endIndex, final List<Closure> closures, final List<TaskClosure> taskClosures) {
closures.clear();
if (taskClosures != null) {
taskClosures.clear();
}
this.lock.lock();
try {
final int queueSize = this.queue.size();
if (queueSize == 0 || endIndex < this.firstIndex) {
return endIndex + 1;
}
if (endIndex > this.firstIndex + queueSize - 1) {
LOG.error("Invalid endIndex={}, firstIndex={}, closureQueueSize={}", endIndex, this.firstIndex, queueSize);
return -1;
}
final long outFirstIndex = this.firstIndex;
for (long i = outFirstIndex; i <= endIndex; i++) {
final Closure closure = this.queue.pollFirst();
if (taskClosures != null && closure instanceof TaskClosure) {
taskClosures.add((TaskClosure) closure);
}
closures.add(closure);
}
this.firstIndex = endIndex + 1;
return outFirstIndex;
} finally {
this.lock.unlock();
}
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class UtilsTest method testRunClosure.
@Test
public void testRunClosure() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Utils.runClosureInExecutor(executor, new Closure() {
@Override
public void run(Status status) {
assertTrue(status.isOk());
latch.countDown();
}
});
latch.await();
}
use of org.apache.ignite.raft.jraft.Closure in project ignite-3 by apache.
the class UtilsTest method testRunClosureWithStatus.
@Test
public void testRunClosureWithStatus() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
Utils.runClosureInExecutor(executor, new Closure() {
@Override
public void run(Status status) {
assertFalse(status.isOk());
assertEquals(RaftError.EACCES.getNumber(), status.getCode());
assertEquals("test 99", status.getErrorMsg());
latch.countDown();
}
}, new Status(RaftError.EACCES, "test %d", 99));
latch.await();
}
Aggregations