Search in sources :

Example 11 with Closure

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);
    }
}
Also used : TaskClosure(org.apache.ignite.raft.jraft.closure.TaskClosure) SaveSnapshotClosure(org.apache.ignite.raft.jraft.closure.SaveSnapshotClosure) Closure(org.apache.ignite.raft.jraft.Closure) LoadSnapshotClosure(org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) TaskClosure(org.apache.ignite.raft.jraft.closure.TaskClosure)

Example 12 with Closure

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);
            }
        }
    });
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Closure(org.apache.ignite.raft.jraft.Closure)

Example 13 with Closure

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();
    }
}
Also used : Closure(org.apache.ignite.raft.jraft.Closure)

Example 14 with Closure

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();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Closure(org.apache.ignite.raft.jraft.Closure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 15 with Closure

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();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Closure(org.apache.ignite.raft.jraft.Closure) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

Closure (org.apache.ignite.raft.jraft.Closure)18 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)9 Status (org.apache.ignite.raft.jraft.Status)8 Test (org.junit.jupiter.api.Test)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ArrayList (java.util.ArrayList)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 CatchUpClosure (org.apache.ignite.raft.jraft.closure.CatchUpClosure)3 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)3 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)3 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)3 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)3 LogId (org.apache.ignite.raft.jraft.entity.LogId)3 NodeId (org.apache.ignite.raft.jraft.entity.NodeId)3 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)3 LearnersOpResponse (org.apache.ignite.raft.jraft.rpc.CliRequests.LearnersOpResponse)3 RpcRequestClosure (org.apache.ignite.raft.jraft.rpc.RpcRequestClosure)3 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)3 EventHandler (com.lmax.disruptor.EventHandler)2 EventTranslator (com.lmax.disruptor.EventTranslator)2