use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class FSMCallerImpl method doCommitted.
private void doCommitted(final long committedIndex) {
if (!this.error.getStatus().isOk()) {
return;
}
final long lastAppliedIndex = this.lastAppliedIndex.get();
// We can tolerate the disorder of committed_index
if (lastAppliedIndex >= committedIndex) {
return;
}
final long startMs = Utils.monotonicMs();
try {
final List<Closure> closures = new ArrayList<>();
final List<TaskClosure> taskClosures = new ArrayList<>();
final long firstClosureIndex = this.closureQueue.popClosureUntil(committedIndex, closures, taskClosures);
// Calls TaskClosure#onCommitted if necessary
onTaskCommitted(taskClosures);
Requires.requireTrue(firstClosureIndex >= 0, "Invalid firstClosureIndex");
final IteratorImpl iterImpl = new IteratorImpl(this.fsm, this.logManager, closures, firstClosureIndex, lastAppliedIndex, committedIndex, this.applyingIndex, this.node.getOptions());
while (iterImpl.isGood()) {
final LogEntry logEntry = iterImpl.entry();
if (logEntry.getType() != EnumOutter.EntryType.ENTRY_TYPE_DATA) {
if (logEntry.getType() == EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION) {
if (logEntry.getOldPeers() != null && !logEntry.getOldPeers().isEmpty()) {
// Joint stage is not supposed to be noticeable by end users.
this.fsm.onConfigurationCommitted(new Configuration(iterImpl.entry().getPeers()));
}
}
if (iterImpl.done() != null) {
// For other entries, we have nothing to do besides flush the
// pending tasks and run this closure to notify the caller that the
// entries before this one were successfully committed and applied.
iterImpl.done().run(Status.OK());
}
iterImpl.next();
continue;
}
// Apply data task to user state machine
doApplyTasks(iterImpl);
}
if (iterImpl.hasError()) {
setError(iterImpl.getError());
iterImpl.runTheRestClosureWithError();
}
final long lastIndex = iterImpl.getIndex() - 1;
final long lastTerm = this.logManager.getTerm(lastIndex);
final LogId lastAppliedId = new LogId(lastIndex, lastTerm);
this.lastAppliedIndex.set(lastIndex);
this.lastAppliedTerm = lastTerm;
this.logManager.setAppliedId(lastAppliedId);
notifyLastAppliedIndexUpdated(lastIndex);
} finally {
this.nodeMetrics.recordLatency("fsm-commit", Utils.monotonicMs() - startMs);
}
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class Replicator method prepareEntry.
boolean prepareEntry(final long nextSendingIndex, final int offset, final EntryMetaBuilder emb, final RecyclableByteBufferList dateBuffer) {
if (dateBuffer.getCapacity() >= this.raftOptions.getMaxBodySize()) {
return false;
}
final long logIndex = nextSendingIndex + offset;
final LogEntry entry = this.options.getLogManager().getEntry(logIndex);
if (entry == null) {
return false;
}
emb.term(entry.getId().getTerm());
if (entry.hasChecksum())
// since 1.2.6
emb.checksum(entry.getChecksum());
emb.type(entry.getType());
if (entry.getPeers() != null) {
Requires.requireTrue(!entry.getPeers().isEmpty(), "Empty peers at logIndex=%d", logIndex);
fillMetaPeers(emb, entry);
} else {
Requires.requireTrue(entry.getType() != EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION, "Empty peers but is ENTRY_TYPE_CONFIGURATION type at logIndex=%d", logIndex);
}
final int remaining = entry.getData() != null ? entry.getData().remaining() : 0;
emb.dataLen(remaining);
if (entry.getData() != null) {
// should slice entry data
dateBuffer.add(entry.getData().slice());
}
return true;
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class BaseLogStorageTest method testAddManyEntries.
@Test
public void testAddManyEntries() {
final List<LogEntry> entries = TestUtils.mockEntries();
assertEquals(10, this.logStorage.appendEntries(entries));
assertEquals(0, this.logStorage.getFirstLogIndex());
assertEquals(9, this.logStorage.getLastLogIndex());
for (int i = 0; i < 10; i++) {
assertEquals(i, this.logStorage.getTerm(i));
final LogEntry entry = this.logStorage.getEntry(i);
assertNotNull(entry);
assertEquals(entries.get(i), entry);
}
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
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());
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());
assertEquals(entry1, this.logStorage.getEntry(100));
assertEquals(entry2, this.logStorage.getEntry(200));
assertEquals(1, this.logStorage.getTerm(100));
assertEquals(2, this.logStorage.getTerm(200));
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class BaseLogStorageTest method testAppendManyLargeEntries.
@Test
public void testAppendManyLargeEntries() {
final long start = Utils.monotonicMs();
final int totalLogs = 1000;
final int logSize = 16 * 1024;
final int batch = 100;
appendLargeEntries(totalLogs, logSize, batch);
System.out.println("Inserted " + totalLogs + " large logs, cost " + (Utils.monotonicMs() - start) + " ms.");
for (int i = 0; i < totalLogs; i++) {
final LogEntry log = this.logStorage.getEntry(i);
assertNotNull(log);
assertEquals(i, log.getId().getIndex());
assertEquals(i, log.getId().getTerm());
assertEquals(logSize, log.getData().remaining());
}
for (int i = 0; i < totalLogs; i++) {
final LogEntry log = this.logStorage.getEntry(i);
assertNotNull(log);
assertEquals(i, log.getId().getIndex());
assertEquals(i, log.getId().getTerm());
assertEquals(logSize, log.getData().remaining());
}
}
Aggregations