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());
}
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));
}
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());
}
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());
}
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;
}
Aggregations