Search in sources :

Example 6 with LogEntry

use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.

the class LogManagerTest method testAppendEntresConflicts.

@Test
public void testAppendEntresConflicts() 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());
    // Append 11-20
    final CountDownLatch latch2 = new CountDownLatch(1);
    mockEntries = TestUtils.mockEntries(10);
    for (int i = 0; i < 10; i++) {
        mockEntries.get(i).getId().setIndex(11 + i);
        mockEntries.get(i).getId().setTerm(1);
    }
    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(20, this.logManager.getLastLogIndex());
    // Re-adds 11-30, but 15 has different term, it will truncate [14,lastIndex] logs
    mockEntries = TestUtils.mockEntries(20);
    for (int i = 0; i < 20; i++) {
        if (11 + i >= 15) {
            mockEntries.get(i).getId().setTerm(2);
        } else {
            mockEntries.get(i).getId().setTerm(1);
        }
        mockEntries.get(i).getId().setIndex(11 + i);
    }
    final CountDownLatch latch3 = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch3.countDown();
        }
    });
    latch3.await();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(30, this.logManager.getLastLogIndex());
    for (int i = 0; i < 30; i++) {
        final LogEntry entry = (this.logManager.getEntry(i + 1));
        assertEquals(i + 1, entry.getId().getIndex());
        if (i + 1 >= 15) {
            assertEquals(2, entry.getId().getTerm());
        } else {
            assertEquals(1, entry.getId().getTerm());
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(com.alipay.sofa.jraft.storage.LogManager) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 7 with LogEntry

use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.

the class TestUtils method mockEntries.

public static List<LogEntry> mockEntries(final int n) {
    List<LogEntry> entries = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        LogEntry entry = mockEntry(i, i);
        if (i > 0) {
            entry.setData(ByteBuffer.wrap(String.valueOf(i).getBytes()));
        }
        entries.add(entry);
    }
    return entries;
}
Also used : ArrayList(java.util.ArrayList) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Example 8 with LogEntry

use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.

the class ReplicatorTest method testContinueSendingEntries.

@Test
public void testContinueSendingEntries() throws Exception {
    testOnRpcReturnedWaitMoreEntries();
    final Replicator r = getReplicator();
    this.id.unlock();
    mockSendEmptyEntries();
    final Future<Message> rpcInFly = r.getRpcInFly();
    assertNotNull(rpcInFly);
    final RpcRequests.AppendEntriesRequest.Builder rb = // 
    RpcRequests.AppendEntriesRequest.newBuilder().setGroupId(// 
    "test").setServerId(// 
    new PeerId("localhost", 8082).toString()).setPeerId(// 
    this.peerId.toString()).setTerm(// 
    1).setPrevLogIndex(// 
    10).setPrevLogTerm(// 
    1).setCommittedIndex(0);
    int totalDataLen = 0;
    for (int i = 0; i < 10; i++) {
        totalDataLen += i;
        final LogEntry value = new LogEntry();
        value.setData(ByteBuffer.allocate(i));
        value.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
        value.setId(new LogId(11 + i, 1));
        Mockito.when(this.logManager.getEntry(11 + i)).thenReturn(value);
        rb.addEntries(RaftOutter.EntryMeta.newBuilder().setTerm(1).setType(EnumOutter.EntryType.ENTRY_TYPE_DATA).setDataLen(i));
    }
    rb.setData(ByteString.copyFrom(new byte[totalDataLen]));
    final RpcRequests.AppendEntriesRequest request = rb.build();
    Mockito.when(this.rpcService.appendEntries(eq(this.peerId.getEndpoint()), eq(request), eq(-1), Mockito.any())).thenReturn(new FutureImpl<>());
    assertEquals(11, r.statInfo.firstLogIndex);
    assertEquals(10, r.statInfo.lastLogIndex);
    Mockito.when(this.logManager.getTerm(20)).thenReturn(1L);
    assertTrue(Replicator.continueSending(this.id, 0));
    assertNotNull(r.getRpcInFly());
    assertNotSame(rpcInFly, r.getRpcInFly());
    assertEquals(11, r.statInfo.firstLogIndex);
    assertEquals(20, r.statInfo.lastLogIndex);
    assertEquals(0, r.getWaitId());
    assertEquals(r.statInfo.runningState, Replicator.RunningState.IDLE);
}
Also used : Message(com.google.protobuf.Message) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 9 with LogEntry

use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.

the class ReplicatorTest method createEntriesRequest.

private RpcRequests.AppendEntriesRequest createEntriesRequest(final int n) {
    final RpcRequests.AppendEntriesRequest.Builder rb = // 
    RpcRequests.AppendEntriesRequest.newBuilder().setGroupId(// 
    "test").setServerId(// 
    new PeerId("localhost", 8082).toString()).setPeerId(// 
    this.peerId.toString()).setTerm(// 
    1).setPrevLogIndex(// 
    10).setPrevLogTerm(// 
    1).setCommittedIndex(0);
    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);
        rb.addEntries(RaftOutter.EntryMeta.newBuilder().setDataLen(i).setTerm(1).setType(EnumOutter.EntryType.ENTRY_TYPE_DATA).build());
    }
    return rb.build();
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 10 with LogEntry

use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.

the class LogEntryV2CodecFactoryTest method testDecodeV1LogEntry.

@Test
public void testDecodeV1LogEntry() {
    ByteBuffer buf = ByteBuffer.wrap("hello".getBytes());
    LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
    entry.setId(new LogId(100, 3));
    entry.setData(buf);
    entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
    assertEquals(buf, entry.getData());
    byte[] content = V1Encoder.INSTANCE.encode(entry);
    assertNotNull(content);
    assertTrue(content.length > 0);
    // Decode by auto detect decoder
    LogEntry nentry = this.decoder.decode(content);
    assertNotNull(nentry);
    assertEquals(100, nentry.getId().getIndex());
    assertEquals(3, nentry.getId().getTerm());
    assertEquals(2, nentry.getPeers().size());
    assertEquals("localhost:99:1", nentry.getPeers().get(0).toString());
    assertEquals("localhost:100:2", nentry.getPeers().get(1).toString());
    assertEquals(buf, nentry.getData());
    assertEquals(0, nentry.getData().position());
    assertEquals(5, nentry.getData().remaining());
    assertNull(nentry.getOldPeers());
}
Also used : ByteBuffer(java.nio.ByteBuffer) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test) BaseLogEntryCodecFactoryTest(com.alipay.sofa.jraft.entity.codec.BaseLogEntryCodecFactoryTest)

Aggregations

LogEntry (com.alipay.sofa.jraft.entity.LogEntry)50 LogId (com.alipay.sofa.jraft.entity.LogId)27 Test (org.junit.Test)19 PeerId (com.alipay.sofa.jraft.entity.PeerId)13 Status (com.alipay.sofa.jraft.Status)11 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)11 ArrayList (java.util.ArrayList)10 LogManager (com.alipay.sofa.jraft.storage.LogManager)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)6 ByteBuffer (java.nio.ByteBuffer)6 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 Closure (com.alipay.sofa.jraft.Closure)3 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)3 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)3 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)3 UserLog (com.alipay.sofa.jraft.entity.UserLog)3 FSMCaller (com.alipay.sofa.jraft.FSMCaller)2 JRaftServiceFactory (com.alipay.sofa.jraft.JRaftServiceFactory)2