Search in sources :

Example 21 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry 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 22 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class Replicator method prepareEntry.

boolean prepareEntry(final long nextSendingIndex, final int offset, final EntryMetaBuilder emb, final RecyclableByteBufferList dateBuffer) {
    if (dateBuffer.getCapacity() >= this.raftOptions.getMaxBodySize()) {
        return false;
    }
    final long logIndex = nextSendingIndex + offset;
    final LogEntry entry = this.options.getLogManager().getEntry(logIndex);
    if (entry == null) {
        return false;
    }
    emb.term(entry.getId().getTerm());
    if (entry.hasChecksum())
        // since 1.2.6
        emb.checksum(entry.getChecksum());
    emb.type(entry.getType());
    if (entry.getPeers() != null) {
        Requires.requireTrue(!entry.getPeers().isEmpty(), "Empty peers at logIndex=%d", logIndex);
        fillMetaPeers(emb, entry);
    } else {
        Requires.requireTrue(entry.getType() != EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION, "Empty peers but is ENTRY_TYPE_CONFIGURATION type at logIndex=%d", logIndex);
    }
    final int remaining = entry.getData() != null ? entry.getData().remaining() : 0;
    emb.dataLen(remaining);
    if (entry.getData() != null) {
        // should slice entry data
        dateBuffer.add(entry.getData().slice());
    }
    return true;
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 23 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class BaseLogStorageTest method testAddManyEntries.

@Test
public void testAddManyEntries() {
    final List<LogEntry> entries = TestUtils.mockEntries();
    assertEquals(10, this.logStorage.appendEntries(entries));
    assertEquals(0, this.logStorage.getFirstLogIndex());
    assertEquals(9, this.logStorage.getLastLogIndex());
    for (int i = 0; i < 10; i++) {
        assertEquals(i, this.logStorage.getTerm(i));
        final LogEntry entry = this.logStorage.getEntry(i);
        assertNotNull(entry);
        assertEquals(entries.get(i), entry);
    }
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 24 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class BaseLogStorageTest method testAddOneEntryState.

@Test
public void testAddOneEntryState() {
    final LogEntry entry1 = TestUtils.mockEntry(100, 1);
    assertTrue(this.logStorage.appendEntry(entry1));
    assertEquals(100, this.logStorage.getFirstLogIndex());
    assertEquals(100, this.logStorage.getLastLogIndex());
    assertEquals(entry1, this.logStorage.getEntry(100));
    assertEquals(1, this.logStorage.getTerm(100));
    final LogEntry entry2 = TestUtils.mockEntry(200, 2);
    assertTrue(this.logStorage.appendEntry(entry2));
    assertEquals(100, this.logStorage.getFirstLogIndex());
    assertEquals(200, this.logStorage.getLastLogIndex());
    assertEquals(entry1, this.logStorage.getEntry(100));
    assertEquals(entry2, this.logStorage.getEntry(200));
    assertEquals(1, this.logStorage.getTerm(100));
    assertEquals(2, this.logStorage.getTerm(200));
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 25 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class BaseLogStorageTest method testAppendManyLargeEntries.

@Test
public void testAppendManyLargeEntries() {
    final long start = Utils.monotonicMs();
    final int totalLogs = 1000;
    final int logSize = 16 * 1024;
    final int batch = 100;
    appendLargeEntries(totalLogs, logSize, batch);
    System.out.println("Inserted " + totalLogs + " large logs, cost " + (Utils.monotonicMs() - start) + " ms.");
    for (int i = 0; i < totalLogs; i++) {
        final LogEntry log = this.logStorage.getEntry(i);
        assertNotNull(log);
        assertEquals(i, log.getId().getIndex());
        assertEquals(i, log.getId().getTerm());
        assertEquals(logSize, log.getData().remaining());
    }
    for (int i = 0; i < totalLogs; i++) {
        final LogEntry log = this.logStorage.getEntry(i);
        assertNotNull(log);
        assertEquals(i, log.getId().getIndex());
        assertEquals(i, log.getId().getTerm());
        assertEquals(logSize, log.getData().remaining());
    }
}
Also used : LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Aggregations

LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)48 LogId (org.apache.ignite.raft.jraft.entity.LogId)26 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)12 Status (org.apache.ignite.raft.jraft.Status)11 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)11 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 LogManager (org.apache.ignite.raft.jraft.storage.LogManager)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)6 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)6 ByteBuffer (java.nio.ByteBuffer)5 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)5 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)4 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)4 EventHandler (com.lmax.disruptor.EventHandler)3 EventTranslator (com.lmax.disruptor.EventTranslator)3 RingBuffer (com.lmax.disruptor.RingBuffer)3 List (java.util.List)3