Search in sources :

Example 31 with LogEntry

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

the class LogManagerTest method testAppendEntries.

@Test
public void testAppendEntries() throws Exception {
    final List<LogEntry> mockEntries = mockAddEntries();
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(10, this.logManager.getLastLogIndex());
    for (int i = 0; i < 10; i++) {
        Assert.assertEquals(mockEntries.get(i), this.logManager.getEntry(i + 1));
    }
    assertEquals(10, this.logManager.getLastLogIndex(true));
    LogId lastLogId = this.logManager.getLastLogId(true);
    assertEquals(10, lastLogId.getIndex());
    lastLogId = this.logManager.getLastLogId(false);
    assertEquals(10, lastLogId.getIndex());
    assertTrue(this.logManager.checkConsistency().isOk());
}
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)

Example 32 with LogEntry

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

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
        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)

Example 33 with LogEntry

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

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());
    Assert.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(com.alipay.sofa.jraft.Status) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LogManager(com.alipay.sofa.jraft.storage.LogManager) 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)

Example 34 with LogEntry

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

the class LogStorageBenchmark method read.

private void read(final int logSize, final int totalLogs) {
    for (int i = 0; i < totalLogs; i++) {
        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)

Example 35 with LogEntry

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

the class LogManagerImpl method getEntry.

@Override
public LogEntry getEntry(final long index) {
    this.readLock.lock();
    try {
        if (index > this.lastLogIndex || index < this.firstLogIndex) {
            return null;
        }
        final LogEntry entry = getEntryFromMemory(index);
        if (entry != null) {
            return entry;
        }
    } finally {
        this.readLock.unlock();
    }
    final LogEntry entry = this.logStorage.getEntry(index);
    if (entry == null) {
        reportError(RaftError.EIO.getNumber(), "Corrupted entry at index=%d, not found", index);
    }
    // Validate checksum
    if (entry != null && this.raftOptions.isEnableLogEntryChecksum() && entry.isCorrupted()) {
        String msg = String.format("Corrupted entry at index=%d, term=%d, expectedChecksum=%d, realChecksum=%d", index, entry.getId().getTerm(), entry.getChecksum(), entry.checksum());
        // Report error to node and throw exception.
        reportError(RaftError.EIO.getNumber(), msg);
        throw new LogEntryCorruptedException(msg);
    }
    return entry;
}
Also used : LogEntry(com.alipay.sofa.jraft.entity.LogEntry) LogEntryCorruptedException(com.alipay.sofa.jraft.error.LogEntryCorruptedException)

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