use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class LogManagerImpl method appendToStorage.
private LogId appendToStorage(final List<LogEntry> toAppend) {
LogId lastId = null;
if (!this.hasError) {
final long startMs = Utils.monotonicMs();
final int entriesCount = toAppend.size();
this.nodeMetrics.recordSize("append-logs-count", entriesCount);
try {
int writtenSize = 0;
for (int i = 0; i < entriesCount; i++) {
final LogEntry entry = toAppend.get(i);
writtenSize += entry.getData() != null ? entry.getData().remaining() : 0;
}
this.nodeMetrics.recordSize("append-logs-bytes", writtenSize);
final int nAppent = this.logStorage.appendEntries(toAppend);
if (nAppent != entriesCount) {
LOG.error("**Critical error**, fail to appendEntries, nAppent={}, toAppend={}", nAppent, toAppend.size());
reportError(RaftError.EIO.getNumber(), "Fail to append log entries");
}
if (nAppent > 0) {
lastId = toAppend.get(nAppent - 1).getId();
}
toAppend.clear();
} finally {
this.nodeMetrics.recordLatency("append-logs", Utils.monotonicMs() - startMs);
}
}
return lastId;
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class LogManagerImpl method getTermFromLogStorage.
private long getTermFromLogStorage(final long index) {
final LogEntry entry = this.logStorage.getEntry(index);
if (entry != null) {
if (this.raftOptions.isEnableLogEntryChecksum() && entry.isCorrupted()) {
// Report error to node and throw exception.
final String msg = String.format("The log entry is corrupted, index=%d, term=%d, expectedChecksum=%d, realChecksum=%d", entry.getId().getIndex(), entry.getId().getTerm(), entry.getChecksum(), entry.checksum());
reportError(RaftError.EIO.getNumber(), msg);
throw new LogEntryCorruptedException(msg);
}
return entry.getId().getTerm();
}
return 0;
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class LogManagerImpl method descLogsInMemory.
private String descLogsInMemory() {
final StringBuilder sb = new StringBuilder();
boolean wasFirst = true;
for (int i = 0; i < this.logsInMemory.size(); i++) {
LogEntry logEntry = this.logsInMemory.get(i);
if (!wasFirst) {
sb.append(",");
} else {
wasFirst = false;
}
sb.append("<id:(").append(logEntry.getId().getTerm()).append(",").append(logEntry.getId().getIndex()).append("),type:").append(logEntry.getType()).append(">");
}
return sb.toString();
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class RocksDBLogStorage method reset.
@Override
public boolean reset(final long nextLogIndex) {
if (nextLogIndex <= 0) {
throw new IllegalArgumentException("Invalid next log index.");
}
this.writeLock.lock();
try (final Options opt = new Options()) {
LogEntry entry = getEntry(nextLogIndex);
closeDB();
try {
RocksDB.destroyDB(this.path, opt);
onReset(nextLogIndex);
if (initAndLoad(null)) {
if (entry == null) {
entry = new LogEntry();
entry.setType(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
entry.setId(new LogId(nextLogIndex, 0));
LOG.warn("Entry not found for nextLogIndex {} when reset.", nextLogIndex);
}
return appendEntry(entry);
} else {
return false;
}
} catch (final RocksDBException e) {
LOG.error("Fail to reset next log index.", e);
return false;
}
} finally {
this.writeLock.unlock();
}
}
use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.
the class LocalLogStorage method appendEntries.
@Override
public int appendEntries(final List<LogEntry> entries) {
if (entries == null || entries.isEmpty()) {
return 0;
}
final int entriesCount = entries.size();
this.readLock.lock();
try {
if (!initialized) {
LOG.warn("DB not initialized or destroyed.");
return 0;
}
for (LogEntry logEntry : entries) {
log.put(logEntry.getId().getIndex(), logEntry);
}
lastLogIndex = log.lastKey();
firstLogIndex = log.firstKey();
return entriesCount;
} catch (Exception e) {
LOG.error("Fail to append entry.", e);
return 0;
} finally {
this.readLock.unlock();
}
}
Aggregations