Search in sources :

Example 26 with LogId

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

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

the class BaseLogStorageTest method testLoadWithConfigManager.

@Test
public void testLoadWithConfigManager() {
    assertTrue(this.confManager.getLastConfiguration().isEmpty());
    final LogEntry confEntry1 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry1.setId(new LogId(99, 1));
    confEntry1.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082").listPeers());
    final LogEntry confEntry2 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry2.setId(new LogId(100, 2));
    confEntry2.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083").listPeers());
    assertTrue(this.logStorage.appendEntry(confEntry1));
    assertEquals(1, this.logStorage.appendEntries(Arrays.asList(confEntry2)));
    // reload log storage.
    if (this.logStorage instanceof RocksDBLogStorage) {
        this.logStorage.shutdown();
        this.logStorage = newLogStorage();
        this.logStorage.init(newLogStorageOptions());
        ConfigurationEntry conf = this.confManager.getLastConfiguration();
        assertNotNull(conf);
        assertFalse(conf.isEmpty());
        assertEquals("localhost:8081,localhost:8082,localhost:8083", conf.getConf().toString());
        conf = this.confManager.get(99);
        assertNotNull(conf);
        assertFalse(conf.isEmpty());
        assertEquals("localhost:8081,localhost:8082", conf.getConf().toString());
    }
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 28 with LogId

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

the class LogManagerTest method testAppendOneEntry.

@Test
public void testAppendOneEntry() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final LogEntry entry = TestUtils.mockEntry(1, 1);
    final List<LogEntry> entries = new ArrayList<>();
    entries.add(entry);
    this.logManager.appendEntries(entries, new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    latch.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(1, this.logManager.getLastLogIndex());
    assertEquals(entry, this.logManager.getEntry(1));
    assertEquals(1, this.logManager.getLastLogIndex(true));
    LogId lastLogId = this.logManager.getLastLogId(true);
    assertEquals(1, lastLogId.getIndex());
    lastLogId = this.logManager.getLastLogId(false);
    assertEquals(1, lastLogId.getIndex());
    assertTrue(this.logManager.checkConsistency().isOk());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(org.apache.ignite.raft.jraft.storage.LogManager) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 29 with LogId

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

the class LogManagerTest method testCheckAndSetConfiguration.

@Test
public void testCheckAndSetConfiguration() throws Exception {
    assertNull(this.logManager.checkAndSetConfiguration(null));
    final ConfigurationEntry entry = new ConfigurationEntry();
    entry.setId(new LogId(0, 1));
    entry.setConf(JRaftUtils.getConfiguration("localhost:8081,localhost:8082"));
    assertSame(entry, this.logManager.checkAndSetConfiguration(entry));
    testGetConfiguration();
    final ConfigurationEntry lastEntry = this.logManager.checkAndSetConfiguration(entry);
    assertNotSame(entry, lastEntry);
    assertEquals("localhost:8081,localhost:8082,localhost:8083", lastEntry.getConf().toString());
    assertEquals("localhost:8081,localhost:8082", lastEntry.getOldConf().toString());
}
Also used : ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) LogId(org.apache.ignite.raft.jraft.entity.LogId) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 30 with LogId

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

the class LogManagerTest method testSetAppliedId2.

@Test
public void testSetAppliedId2() throws Exception {
    final List<LogEntry> mockEntries = mockAddEntries();
    for (int i = 0; i < 10; i++) {
        // it's in memory
        assertEquals(mockEntries.get(i), this.logManager.getEntryFromMemory(i + 1));
    }
    // waiting for setDiskId()
    Thread.sleep(200);
    this.logManager.setAppliedId(new LogId(10, 10));
    for (int i = 0; i < 10; i++) {
        assertNull(this.logManager.getEntryFromMemory(i + 1));
        assertEquals(mockEntries.get(i), this.logManager.getEntry(i + 1));
    }
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Aggregations

LogId (org.apache.ignite.raft.jraft.entity.LogId)41 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)24 Test (org.junit.jupiter.api.Test)15 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)14 ArrayList (java.util.ArrayList)10 Status (org.apache.ignite.raft.jraft.Status)10 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)9 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)7 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)5 ByteBuffer (java.nio.ByteBuffer)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 LogManager (org.apache.ignite.raft.jraft.storage.LogManager)4 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)3 StripedDisruptor (org.apache.ignite.raft.jraft.disruptor.StripedDisruptor)2 EntryType (org.apache.ignite.raft.jraft.entity.EnumOutter.EntryType)2 RaftException (org.apache.ignite.raft.jraft.error.RaftException)2 LogStorageOptions (org.apache.ignite.raft.jraft.option.LogStorageOptions)2 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)2 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)2