Search in sources :

Example 6 with LogEntry

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

the class LogManagerTest method testAppendEntriesBeforeAppliedIndex.

@Test
public void testAppendEntriesBeforeAppliedIndex() throws Exception {
    // Append 0-10
    List<LogEntry> mockEntries = TestUtils.mockEntries(10);
    for (int i = 0; i < 10; i++) {
        mockEntries.get(i).getId().setTerm(1);
    }
    final CountDownLatch latch1 = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch1.countDown();
        }
    });
    latch1.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(10, this.logManager.getLastLogIndex());
    // waiting for setDiskId()
    Thread.sleep(200);
    this.logManager.setAppliedId(new LogId(9, 1));
    for (int i = 0; i < 10; i++) {
        assertNull(this.logManager.getEntryFromMemory(i));
    }
    // append 1-10 again, already applied, returns OK.
    final CountDownLatch latch2 = new CountDownLatch(1);
    mockEntries = TestUtils.mockEntries(10);
    mockEntries.remove(0);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch2.countDown();
        }
    });
    latch2.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(10, this.logManager.getLastLogIndex());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) 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 7 with LogEntry

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

the class NodeImpl method readCommittedUserLog.

@Override
public UserLog readCommittedUserLog(final long index) {
    if (index <= 0) {
        throw new LogIndexOutOfBoundsException("Request index is invalid: " + index);
    }
    final long savedLastAppliedIndex = this.fsmCaller.getLastAppliedIndex();
    if (index > savedLastAppliedIndex) {
        throw new LogIndexOutOfBoundsException("Request index " + index + " is greater than lastAppliedIndex: " + savedLastAppliedIndex);
    }
    long curIndex = index;
    LogEntry entry = this.logManager.getEntry(curIndex);
    if (entry == null) {
        throw new LogNotFoundException("User log is deleted at index: " + index);
    }
    do {
        if (entry.getType() == EnumOutter.EntryType.ENTRY_TYPE_DATA) {
            return new UserLog(curIndex, entry.getData());
        } else {
            curIndex++;
        }
        if (curIndex > savedLastAppliedIndex) {
            throw new IllegalStateException("No user log between index:" + index + " and last_applied_index:" + savedLastAppliedIndex);
        }
        entry = this.logManager.getEntry(curIndex);
    } while (entry != null);
    throw new LogNotFoundException("User log is deleted at index: " + curIndex);
}
Also used : UserLog(org.apache.ignite.raft.jraft.entity.UserLog) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 8 with LogEntry

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

the class ReplicatorTest method createEntriesRequest.

private RpcRequests.AppendEntriesRequest createEntriesRequest(final int n) {
    final AppendEntriesRequestBuilder rb = raftOptions.getRaftMessagesFactory().appendEntriesRequest().groupId("test").serverId(new PeerId("localhost", 8082).toString()).peerId(this.peerId.toString()).term(1).prevLogIndex(10).prevLogTerm(1).committedIndex(0);
    List<RaftOutter.EntryMeta> entries = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        final LogEntry log = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_DATA);
        log.setData(ByteBuffer.wrap(new byte[i]));
        log.setId(new LogId(i + 11, 1));
        Mockito.when(this.logManager.getEntry(i + 11)).thenReturn(log);
        Mockito.when(this.logManager.getTerm(i + 11)).thenReturn(1L);
        entries.add(raftOptions.getRaftMessagesFactory().entryMeta().dataLen(i).term(1).type(EnumOutter.EntryType.ENTRY_TYPE_DATA).build());
    }
    rb.entriesList(entries);
    return rb.build();
}
Also used : AppendEntriesRequestBuilder(org.apache.ignite.raft.jraft.rpc.AppendEntriesRequestBuilder) ArrayList(java.util.ArrayList) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 9 with LogEntry

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

the class FSMCallerTest method testOnCommitted.

@Test
public void testOnCommitted() throws Exception {
    final LogEntry log = new LogEntry(EntryType.ENTRY_TYPE_DATA);
    log.getId().setIndex(11);
    log.getId().setTerm(1);
    Mockito.when(this.logManager.getTerm(11)).thenReturn(1L);
    Mockito.when(this.logManager.getEntry(11)).thenReturn(log);
    final ArgumentCaptor<Iterator> itArg = ArgumentCaptor.forClass(Iterator.class);
    assertTrue(this.fsmCaller.onCommitted(11));
    this.fsmCaller.flush();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 11);
    Mockito.verify(this.fsm).onApply(itArg.capture());
    final Iterator it = itArg.getValue();
    assertFalse(it.hasNext());
    assertEquals(it.getIndex(), 12);
    Mockito.verify(this.logManager).setAppliedId(new LogId(11, 1));
    assertTrue(this.fsmCaller.getError().getStatus().isOk());
}
Also used : Iterator(org.apache.ignite.raft.jraft.Iterator) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) Test(org.junit.jupiter.api.Test)

Example 10 with LogEntry

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

the class IteratorImplTest method setup.

@BeforeEach
public void setup() {
    this.applyingIndex = new AtomicLong(0);
    this.closures = new ArrayList<>();
    for (int i = 0; i < 11; i++) {
        this.closures.add(new MockClosure());
        final LogEntry log = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
        log.getId().setIndex(i);
        log.getId().setTerm(1);
        Mockito.when(this.logManager.getEntry(i)).thenReturn(log);
    }
    NodeOptions nodeOptions = new NodeOptions();
    executor = JRaftUtils.createExecutor("test-executor", Utils.cpus());
    nodeOptions.setCommonExecutor(executor);
    this.iter = new IteratorImpl(fsm, logManager, closures, 0L, 0L, 10L, applyingIndex, nodeOptions);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) BeforeEach(org.junit.jupiter.api.BeforeEach)

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