use of org.apache.ignite.raft.jraft.entity.LogId 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.LogId in project ignite-3 by apache.
the class LogManagerImpl method setDiskId.
private void setDiskId(final LogId id) {
if (id == null) {
return;
}
LogId clearId;
this.writeLock.lock();
try {
if (id.compareTo(this.diskId) < 0) {
return;
}
this.diskId = id;
clearId = this.diskId.compareTo(this.appliedId) <= 0 ? this.diskId : this.appliedId;
} finally {
this.writeLock.unlock();
}
if (clearId != null) {
clearMemoryLogs(clearId);
}
}
use of org.apache.ignite.raft.jraft.entity.LogId in project ignite-3 by apache.
the class LogManagerImpl method init.
@Override
public boolean init(final LogManagerOptions opts) {
this.writeLock.lock();
try {
if (opts.getLogStorage() == null) {
LOG.error("Fail to init log manager, log storage is null");
return false;
}
this.raftOptions = opts.getRaftOptions();
this.nodeMetrics = opts.getNodeMetrics();
this.logStorage = opts.getLogStorage();
this.configManager = opts.getConfigurationManager();
this.nodeOptions = opts.getNode().getOptions();
this.groupId = opts.getGroupId();
LogStorageOptions lsOpts = new LogStorageOptions();
lsOpts.setConfigurationManager(this.configManager);
lsOpts.setLogEntryCodecFactory(opts.getLogEntryCodecFactory());
if (!this.logStorage.init(lsOpts)) {
LOG.error("Fail to init logStorage");
return false;
}
this.firstLogIndex = this.logStorage.getFirstLogIndex();
this.lastLogIndex = this.logStorage.getLastLogIndex();
this.diskId = new LogId(this.lastLogIndex, getTermFromLogStorage(this.lastLogIndex));
this.fsmCaller = opts.getFsmCaller();
this.disruptor = opts.getLogManagerDisruptor();
this.diskQueue = disruptor.subscribe(groupId, new StableClosureEventHandler(), (event, ex) -> reportError(-1, "LogManager handle event error"));
if (this.nodeMetrics.getMetricRegistry() != null) {
this.nodeMetrics.getMetricRegistry().register("jraft-log-manager-disruptor", new DisruptorMetricSet(this.diskQueue));
}
} finally {
this.writeLock.unlock();
}
return true;
}
use of org.apache.ignite.raft.jraft.entity.LogId in project ignite-3 by apache.
the class LogManagerImpl method setSnapshot.
@Override
public void setSnapshot(final SnapshotMeta meta) {
LOG.debug("set snapshot: {}.", meta);
this.writeLock.lock();
try {
if (meta.lastIncludedIndex() <= this.lastSnapshotId.getIndex()) {
return;
}
final Configuration conf = confFromMeta(meta);
final Configuration oldConf = oldConfFromMeta(meta);
final ConfigurationEntry entry = new ConfigurationEntry(new LogId(meta.lastIncludedIndex(), meta.lastIncludedTerm()), conf, oldConf);
this.configManager.setSnapshot(entry);
final long term = unsafeGetTerm(meta.lastIncludedIndex());
final long savedLastSnapshotIndex = this.lastSnapshotId.getIndex();
this.lastSnapshotId.setIndex(meta.lastIncludedIndex());
this.lastSnapshotId.setTerm(meta.lastIncludedTerm());
if (this.lastSnapshotId.compareTo(this.appliedId) > 0) {
this.appliedId = this.lastSnapshotId.copy();
}
if (term == 0) {
// last_included_index is larger than last_index
// FIXME: what if last_included_index is less than first_index?
truncatePrefix(meta.lastIncludedIndex() + 1);
} else if (term == meta.lastIncludedTerm()) {
// TODO if there are still be need? TODO asch
if (savedLastSnapshotIndex > 0) {
truncatePrefix(savedLastSnapshotIndex + 1);
}
} else {
if (!reset(meta.lastIncludedIndex() + 1)) {
LOG.warn("Reset log manager failed, nextLogIndex={}.", meta.lastIncludedIndex() + 1);
}
}
} finally {
this.writeLock.unlock();
}
}
use of org.apache.ignite.raft.jraft.entity.LogId 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();
}
}
Aggregations