use of com.alipay.sofa.jraft.entity.LogEntry 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());
}
use of com.alipay.sofa.jraft.entity.LogEntry 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));
}
}
use of com.alipay.sofa.jraft.entity.LogEntry 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());
}
use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class LogStorageBenchmark method read.
private void read(final int logSize, final int totalLogs) {
for (int i = 0; i < totalLogs; i++) {
LogEntry log = this.logStorage.getEntry(i);
assertNotNull(log);
assertEquals(i, log.getId().getIndex());
assertEquals(i, log.getId().getTerm());
assertEquals(logSize, log.getData().remaining());
}
}
use of com.alipay.sofa.jraft.entity.LogEntry in project sofa-jraft by sofastack.
the class LogManagerImpl method getEntry.
@Override
public LogEntry getEntry(final long index) {
this.readLock.lock();
try {
if (index > this.lastLogIndex || index < this.firstLogIndex) {
return null;
}
final LogEntry entry = getEntryFromMemory(index);
if (entry != null) {
return entry;
}
} finally {
this.readLock.unlock();
}
final LogEntry entry = this.logStorage.getEntry(index);
if (entry == null) {
reportError(RaftError.EIO.getNumber(), "Corrupted entry at index=%d, not found", index);
}
// Validate checksum
if (entry != null && this.raftOptions.isEnableLogEntryChecksum() && entry.isCorrupted()) {
String msg = String.format("Corrupted entry at index=%d, term=%d, expectedChecksum=%d, realChecksum=%d", index, entry.getId().getTerm(), entry.getChecksum(), entry.checksum());
// Report error to node and throw exception.
reportError(RaftError.EIO.getNumber(), msg);
throw new LogEntryCorruptedException(msg);
}
return entry;
}
Aggregations