Search in sources :

Example 26 with LogEntry

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

the class LogEntryCodecPerfTest method testEncodeDecode.

private void testEncodeDecode(final LogEntryEncoder encoder, final LogEntryDecoder decoder, final CyclicBarrier barrier) throws Exception {
    ByteBuffer buf = ByteBuffer.wrap(DATA);
    LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
    entry.setData(buf);
    entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
    if (barrier != null) {
        barrier.await();
    }
    for (int i = 0; i < TIMES; i++) {
        entry.setId(new LogId(i, i));
        byte[] content = encoder.encode(entry);
        assert (content.length > 0);
        this.logSize.addAndGet(content.length);
        LogEntry nLog = decoder.decode(content);
        assertEquals(2, nLog.getPeers().size());
        assertArrayEquals(DATA, nLog.getData().array());
        assertEquals(i, nLog.getId().getIndex());
        assertEquals(i, nLog.getId().getTerm());
    }
    if (barrier != null) {
        barrier.await();
    }
}
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)

Example 27 with LogEntry

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

the class LogEntryV2CodecFactoryTest method testEncodeDecodeWithLearners.

@Test
public void testEncodeDecodeWithLearners() {
    LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
    entry.setId(new LogId(100, 3));
    entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
    List<PeerId> theLearners = createLearners("192.168.1.1:8081", "192.168.1.2:8081");
    entry.setLearners(theLearners);
    assertSame(entry.getData(), LogEntry.EMPTY_DATA);
    assertNull(entry.getOldPeers());
    byte[] content = this.encoder.encode(entry);
    assertNotNull(content);
    assertTrue(content.length > 0);
    LogEntry nentry = this.decoder.decode(content);
    assertNotNull(nentry);
    assertNull(nentry.getOldLearners());
    assertPeersAndLearners(theLearners, nentry);
    // test old learners
    List<PeerId> theOldLearners = createLearners("192.168.1.1:8081");
    entry.setOldLearners(theOldLearners);
    content = this.encoder.encode(entry);
    assertNotNull(content);
    assertTrue(content.length > 0);
    nentry = this.decoder.decode(content);
    assertNotNull(nentry);
    assertPeersAndLearners(theLearners, nentry);
    List<PeerId> oldLearners = nentry.getOldLearners();
    assertNotNull(oldLearners);
    assertEquals(1, oldLearners.size());
    assertEquals(oldLearners, theOldLearners);
}
Also used : 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)

Example 28 with LogEntry

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

the class BaseLogStorageTest method testAppendMantyLargeEntries.

@Test
public void testAppendMantyLargeEntries() {
    final long start = Utils.monotonicMs();
    final int totalLogs = 100000;
    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());
    }
    this.logStorage.shutdown();
    this.logStorage.init(newLogStorageOptions());
    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(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 29 with LogEntry

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

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(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 30 with LogEntry

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

the class LogManagerTest method testSetAppliedId.

@Test
public void testSetAppliedId() throws Exception {
    final List<LogEntry> mockEntries = mockAddEntries();
    for (int i = 0; i < 10; i++) {
        // it's in memory
        Assert.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));
        Assert.assertEquals(mockEntries.get(i), this.logManager.getEntry(i + 1));
    }
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

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