Search in sources :

Example 11 with LogId

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

the class V1Decoder method decode.

public void decode(final LogEntry log, final byte[] content) {
    // 1-5 type
    final int iType = Bits.getInt(content, 1);
    log.setType(EnumOutter.EntryType.forNumber(iType));
    // 5-13 index
    // 13-21 term
    final long index = Bits.getLong(content, 5);
    final long term = Bits.getLong(content, 13);
    log.setId(new LogId(index, term));
    // 21-29 checksum
    log.setChecksum(Bits.getLong(content, 21));
    // 29-33 peer count
    int peerCount = Bits.getInt(content, 29);
    // peers
    int pos = 33;
    if (peerCount > 0) {
        List<PeerId> peers = new ArrayList<>(peerCount);
        while (peerCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            peers.add(peer);
        }
        log.setPeers(peers);
    }
    // old peers
    int oldPeerCount = Bits.getInt(content, pos);
    pos += 4;
    if (oldPeerCount > 0) {
        List<PeerId> oldPeers = new ArrayList<>(oldPeerCount);
        while (oldPeerCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            oldPeers.add(peer);
        }
        log.setOldPeers(oldPeers);
    }
    // learners
    int learnersCount = Bits.getInt(content, pos);
    pos += 4;
    if (learnersCount > 0) {
        List<PeerId> learners = new ArrayList<>(learnersCount);
        while (learnersCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            learners.add(peer);
        }
        log.setLearners(learners);
    }
    // old learners
    int oldLearnersCount = Bits.getInt(content, pos);
    pos += 4;
    if (oldLearnersCount > 0) {
        List<PeerId> oldLearners = new ArrayList<>(oldLearnersCount);
        while (oldLearnersCount-- > 0) {
            final short len = Bits.getShort(content, pos);
            final byte[] bs = new byte[len];
            System.arraycopy(content, pos + 2, bs, 0, len);
            // peer len (short in 2 bytes)
            // peer str
            pos += 2 + len;
            final PeerId peer = new PeerId();
            peer.parse(AsciiStringUtil.unsafeDecode(bs));
            oldLearners.add(peer);
        }
        log.setOldLearners(oldLearners);
    }
    // data
    if (content.length > pos) {
        final int len = content.length - pos;
        ByteBuffer data = ByteBuffer.allocate(len);
        data.put(content, pos, len);
        data.flip();
        log.setData(data);
    }
}
Also used : ArrayList(java.util.ArrayList) LogId(org.apache.ignite.raft.jraft.entity.LogId) ByteBuffer(java.nio.ByteBuffer) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 12 with LogId

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

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

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

the class FSMCallerTest method testOnCommittedError.

@Test
public void testOnCommittedError() throws Exception {
    Mockito.when(this.logManager.getTerm(10)).thenReturn(1L);
    Mockito.when(this.logManager.getEntry(11)).thenReturn(null);
    assertTrue(this.fsmCaller.onCommitted(11));
    this.fsmCaller.flush();
    assertEquals(10, this.fsmCaller.getLastAppliedIndex());
    Mockito.verify(this.logManager).setAppliedId(new LogId(10, 1));
    assertFalse(this.fsmCaller.getError().getStatus().isOk());
    assertEquals("Fail to get entry at index=11 while committed_index=11", this.fsmCaller.getError().getStatus().getErrorMsg());
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId) Test(org.junit.jupiter.api.Test)

Example 15 with LogId

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

the class ConfigurationManagerTest method testGetStuff.

@Test
public void testGetStuff() {
    ConfigurationEntry lastConf = this.confManager.getLastConfiguration();
    ConfigurationEntry snapshot = this.confManager.getSnapshot();
    assertSame(snapshot, lastConf);
    assertSame(snapshot, this.confManager.get(0));
    ConfigurationEntry confEntry1 = TestUtils.getConfEntry("localhost:8080", null);
    confEntry1.setId(new LogId(0, 0));
    assertTrue(this.confManager.add(confEntry1));
    lastConf = this.confManager.getLastConfiguration();
    assertNotSame(snapshot, lastConf);
    assertSame(confEntry1, lastConf);
    assertSame(confEntry1, this.confManager.get(0));
    assertSame(confEntry1, this.confManager.get(1));
    assertSame(confEntry1, this.confManager.get(2));
    ConfigurationEntry confEntry2 = TestUtils.getConfEntry("localhost:8080,localhost:8081", "localhost:8080");
    confEntry2.setId(new LogId(1, 1));
    assertTrue(this.confManager.add(confEntry2));
    lastConf = this.confManager.getLastConfiguration();
    assertNotSame(snapshot, lastConf);
    assertSame(confEntry2, lastConf);
    assertSame(confEntry1, this.confManager.get(0));
    assertSame(confEntry2, this.confManager.get(1));
    assertSame(confEntry2, this.confManager.get(2));
    ConfigurationEntry confEntry3 = TestUtils.getConfEntry("localhost:8080,localhost:8081,localhost:8082", "localhost:8080,localhost:8081");
    confEntry3.setId(new LogId(2, 1));
    assertTrue(this.confManager.add(confEntry3));
    lastConf = this.confManager.getLastConfiguration();
    assertNotSame(snapshot, lastConf);
    assertSame(confEntry3, lastConf);
    assertSame(confEntry1, this.confManager.get(0));
    assertSame(confEntry2, this.confManager.get(1));
    assertSame(confEntry3, this.confManager.get(2));
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId) 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