Search in sources :

Example 1 with LogEntry

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

the class BaseLogStorageTest method testLoadWithConfigManager.

@Test
public void testLoadWithConfigManager() {
    assertTrue(this.confManager.getLastConfiguration().isEmpty());
    final LogEntry confEntry1 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry1.setId(new LogId(99, 1));
    confEntry1.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082").listPeers());
    final LogEntry confEntry2 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry2.setId(new LogId(100, 2));
    confEntry2.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083").listPeers());
    assertTrue(this.logStorage.appendEntry(confEntry1));
    assertEquals(1, this.logStorage.appendEntries(Arrays.asList(confEntry2)));
    // reload log storage.
    this.logStorage.shutdown();
    this.logStorage = newLogStorage();
    this.logStorage.init(newLogStorageOptions());
    ConfigurationEntry conf = this.confManager.getLastConfiguration();
    assertNotNull(conf);
    assertFalse(conf.isEmpty());
    assertEquals("localhost:8081,localhost:8082,localhost:8083", conf.getConf().toString());
    conf = this.confManager.get(99);
    assertNotNull(conf);
    assertFalse(conf.isEmpty());
    assertEquals("localhost:8081,localhost:8082", conf.getConf().toString());
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 2 with LogEntry

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

the class BaseLogStorageTest method testAddOneEntryState.

@Test
public void testAddOneEntryState() {
    final LogEntry entry1 = TestUtils.mockEntry(100, 1);
    assertTrue(this.logStorage.appendEntry(entry1));
    assertEquals(100, this.logStorage.getFirstLogIndex());
    assertEquals(100, this.logStorage.getLastLogIndex());
    Assert.assertEquals(entry1, this.logStorage.getEntry(100));
    assertEquals(1, this.logStorage.getTerm(100));
    final LogEntry entry2 = TestUtils.mockEntry(200, 2);
    assertTrue(this.logStorage.appendEntry(entry2));
    assertEquals(100, this.logStorage.getFirstLogIndex());
    assertEquals(200, this.logStorage.getLastLogIndex());
    Assert.assertEquals(entry1, this.logStorage.getEntry(100));
    Assert.assertEquals(entry2, this.logStorage.getEntry(200));
    assertEquals(1, this.logStorage.getTerm(100));
    assertEquals(2, this.logStorage.getTerm(200));
}
Also used : LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 3 with LogEntry

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

the class LogManagerTest method testAppendEntriesBeforeAppliedIndex.

@Test
public void testAppendEntriesBeforeAppliedIndex() 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());
    // waiting for setDiskId()
    Thread.sleep(200);
    this.logManager.setAppliedId(new LogId(9, 1));
    for (int i = 0; i < 10; i++) {
        assertNull(this.logManager.getEntryFromMemory(i));
    }
    // append 1-10 again, already applied, returns OK.
    final CountDownLatch latch2 = new CountDownLatch(1);
    mockEntries = TestUtils.mockEntries(10);
    mockEntries.remove(0);
    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(10, this.logManager.getLastLogIndex());
}
Also used : Status(com.alipay.sofa.jraft.Status) 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 4 with LogEntry

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

the class LogManagerTest method testGetConfiguration.

@Test
public void testGetConfiguration() throws Exception {
    assertTrue(this.logManager.getConfiguration(1).isEmpty());
    final List<LogEntry> entries = new ArrayList<>(2);
    final LogEntry confEntry1 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry1.setId(new LogId(0, 1));
    confEntry1.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082").listPeers());
    final LogEntry confEntry2 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    confEntry2.setId(new LogId(0, 2));
    confEntry2.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083").listPeers());
    confEntry2.setOldPeers(confEntry1.getPeers());
    entries.add(confEntry1);
    entries.add(confEntry2);
    final CountDownLatch latch = new CountDownLatch(1);
    this.logManager.appendEntries(new ArrayList<>(entries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    latch.await();
    ConfigurationEntry entry = this.logManager.getConfiguration(1);
    assertEquals("localhost:8081,localhost:8082", entry.getConf().toString());
    assertTrue(entry.getOldConf().isEmpty());
    entry = this.logManager.getConfiguration(2);
    assertEquals("localhost:8081,localhost:8082,localhost:8083", entry.getConf().toString());
    assertEquals("localhost:8081,localhost:8082", entry.getOldConf().toString());
}
Also used : Status(com.alipay.sofa.jraft.Status) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LogId(com.alipay.sofa.jraft.entity.LogId) LogManager(com.alipay.sofa.jraft.storage.LogManager) ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 5 with LogEntry

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

the class LogManagerTest method mockAddEntries.

private List<LogEntry> mockAddEntries() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final List<LogEntry> mockEntries = TestUtils.mockEntries(10);
    this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }
    });
    latch.await();
    return mockEntries;
}
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)

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