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