Search in sources :

Example 16 with LogId

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;
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 17 with LogId

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);
    }
}
Also used : LogId(org.apache.ignite.raft.jraft.entity.LogId)

Example 18 with LogId

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;
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) NodeMetrics(org.apache.ignite.raft.jraft.core.NodeMetrics) RaftException(org.apache.ignite.raft.jraft.error.RaftException) LogEntryCorruptedException(org.apache.ignite.raft.jraft.error.LogEntryCorruptedException) Requires(org.apache.ignite.raft.jraft.util.Requires) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) SegmentList(org.apache.ignite.raft.jraft.util.SegmentList) ArrayList(java.util.ArrayList) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) Map(java.util.Map) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogStorageOptions(org.apache.ignite.raft.jraft.option.LogStorageOptions) EventHandler(com.lmax.disruptor.EventHandler) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ErrorType(org.apache.ignite.raft.jraft.entity.EnumOutter.ErrorType) SnapshotMeta(org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta) RingBuffer(com.lmax.disruptor.RingBuffer) Status(org.apache.ignite.raft.jraft.Status) ThreadHelper(org.apache.ignite.raft.jraft.util.ThreadHelper) GroupAware(org.apache.ignite.raft.jraft.disruptor.GroupAware) EntryType(org.apache.ignite.raft.jraft.entity.EnumOutter.EntryType) Utils(org.apache.ignite.raft.jraft.util.Utils) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) List(java.util.List) Lock(java.util.concurrent.locks.Lock) LogManagerOptions(org.apache.ignite.raft.jraft.option.LogManagerOptions) EventTranslator(com.lmax.disruptor.EventTranslator) LogStorage(org.apache.ignite.raft.jraft.storage.LogStorage) FSMCaller(org.apache.ignite.raft.jraft.FSMCaller) ArrayDeque(org.apache.ignite.raft.jraft.util.ArrayDeque) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RaftError(org.apache.ignite.raft.jraft.error.RaftError) LogManager(org.apache.ignite.raft.jraft.storage.LogManager) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) LogStorageOptions(org.apache.ignite.raft.jraft.option.LogStorageOptions) LogId(org.apache.ignite.raft.jraft.entity.LogId)

Example 19 with LogId

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();
    }
}
Also used : Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) LogId(org.apache.ignite.raft.jraft.entity.LogId)

Example 20 with LogId

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();
    }
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) LogStorageOptions(org.apache.ignite.raft.jraft.option.LogStorageOptions) ReadOptions(org.rocksdb.ReadOptions) DBOptions(org.rocksdb.DBOptions) WriteOptions(org.rocksdb.WriteOptions) Options(org.rocksdb.Options) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RocksDBException(org.rocksdb.RocksDBException) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Aggregations

LogId (org.apache.ignite.raft.jraft.entity.LogId)41 LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)24 Test (org.junit.jupiter.api.Test)15 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)14 ArrayList (java.util.ArrayList)10 Status (org.apache.ignite.raft.jraft.Status)10 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)9 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)7 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)5 ByteBuffer (java.nio.ByteBuffer)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 LogManager (org.apache.ignite.raft.jraft.storage.LogManager)4 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)3 StripedDisruptor (org.apache.ignite.raft.jraft.disruptor.StripedDisruptor)2 EntryType (org.apache.ignite.raft.jraft.entity.EnumOutter.EntryType)2 RaftException (org.apache.ignite.raft.jraft.error.RaftException)2 LogStorageOptions (org.apache.ignite.raft.jraft.option.LogStorageOptions)2 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)2 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)2