Search in sources :

Example 31 with LogId

use of com.alipay.sofa.jraft.entity.LogId 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 LogId

use of com.alipay.sofa.jraft.entity.LogId 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 LogId

use of com.alipay.sofa.jraft.entity.LogId 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 LogId

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

the class LogManagerImpl method setDiskId.

private void setDiskId(final LogId id) {
    if (id == null) {
        return;
    }
    LogId clearId;
    this.writeLock.lock();
    try {
        if (id.compareTo(this.diskId) < 0) {
            return;
        }
        this.diskId = id;
        clearId = this.diskId.compareTo(this.appliedId) <= 0 ? this.diskId : this.appliedId;
    } finally {
        this.writeLock.unlock();
    }
    if (clearId != null) {
        clearMemoryLogs(clearId);
    }
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId)

Example 35 with LogId

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

the class LogManagerImpl method setAppliedId.

@Override
public void setAppliedId(final LogId appliedId) {
    LogId clearId;
    this.writeLock.lock();
    try {
        if (appliedId.compareTo(this.appliedId) < 0) {
            return;
        }
        this.appliedId = appliedId.copy();
        clearId = this.diskId.compareTo(this.appliedId) <= 0 ? this.diskId : this.appliedId;
    } finally {
        this.writeLock.unlock();
    }
    if (clearId != null) {
        clearMemoryLogs(clearId);
    }
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId)

Aggregations

LogId (com.alipay.sofa.jraft.entity.LogId)44 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)26 PeerId (com.alipay.sofa.jraft.entity.PeerId)18 Test (org.junit.Test)18 Status (com.alipay.sofa.jraft.Status)10 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)9 ArrayList (java.util.ArrayList)8 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)5 ByteBuffer (java.nio.ByteBuffer)5 LogManager (com.alipay.sofa.jraft.storage.LogManager)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ConfigurationManager (com.alipay.sofa.jraft.conf.ConfigurationManager)3 EntryType (com.alipay.sofa.jraft.entity.EnumOutter.EntryType)2 BaseLogEntryCodecFactoryTest (com.alipay.sofa.jraft.entity.codec.BaseLogEntryCodecFactoryTest)2 RaftException (com.alipay.sofa.jraft.error.RaftException)2 LogStorageOptions (com.alipay.sofa.jraft.option.LogStorageOptions)2 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)2 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)2