Search in sources :

Example 1 with LogId

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

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

the class LogManagerTest method testEmptyState.

@Test
public void testEmptyState() {
    assertEquals(1, this.logManager.getFirstLogIndex());
    assertEquals(0, this.logManager.getLastLogIndex());
    assertNull(this.logManager.getEntry(1));
    assertEquals(0, this.logManager.getLastLogIndex(true));
    LogId lastLogId = this.logManager.getLastLogId(true);
    assertEquals(0, lastLogId.getIndex());
    lastLogId = this.logManager.getLastLogId(false);
    assertEquals(0, lastLogId.getIndex());
    assertTrue(this.logManager.checkConsistency().isOk());
}
Also used : LogId(com.alipay.sofa.jraft.entity.LogId) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 3 with LogId

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

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

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

the class LogManagerTest method testCheckAndSetConfiguration.

@Test
public void testCheckAndSetConfiguration() throws Exception {
    assertNull(this.logManager.checkAndSetConfiguration(null));
    final ConfigurationEntry entry = new ConfigurationEntry();
    entry.setId(new LogId(0, 1));
    entry.setConf(JRaftUtils.getConfiguration("localhost:8081,localhost:8082"));
    assertSame(entry, this.logManager.checkAndSetConfiguration(entry));
    testGetConfiguration();
    final ConfigurationEntry lastEntry = this.logManager.checkAndSetConfiguration(entry);
    assertNotSame(entry, lastEntry);
    assertEquals("localhost:8081,localhost:8082,localhost:8083", lastEntry.getConf().toString());
    assertEquals("localhost:8081,localhost:8082", lastEntry.getOldConf().toString());
}
Also used : ConfigurationEntry(com.alipay.sofa.jraft.conf.ConfigurationEntry) LogId(com.alipay.sofa.jraft.entity.LogId) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

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