Search in sources :

Example 1 with LogStorage

use of org.apache.ignite.raft.jraft.storage.LogStorage 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 2 with LogStorage

use of org.apache.ignite.raft.jraft.storage.LogStorage in project ignite-3 by apache.

the class LogStorageBenchmark method main.

public static void main(final String[] args) {
    String testPath = Paths.get(SystemPropertyUtil.get("user.dir"), "log_storage").toString();
    System.out.println("Test log storage path: " + testPath);
    int batchSize = 100;
    int logSize = 16 * 1024;
    int totalLogs = 30 * 1024;
    LogStorage logStorage = new RocksDBLogStorage(testPath, new RaftOptions());
    // LogStorage logStorage = new LocalLogStorage(testPath, new RaftOptions());
    LogStorageOptions opts = new LogStorageOptions();
    opts.setConfigurationManager(new ConfigurationManager());
    opts.setLogEntryCodecFactory(LogEntryV1CodecFactory.getInstance());
    logStorage.init(opts);
    new LogStorageBenchmark(logStorage, logSize, totalLogs, batchSize).doTest();
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) LogStorageOptions(org.apache.ignite.raft.jraft.option.LogStorageOptions) LogStorage(org.apache.ignite.raft.jraft.storage.LogStorage) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager)

Example 3 with LogStorage

use of org.apache.ignite.raft.jraft.storage.LogStorage in project ignite-3 by apache.

the class LogManagerImpl method appendEntries.

@Override
public void appendEntries(final List<LogEntry> entries, final StableClosure done) {
    Requires.requireNonNull(done, "done");
    if (this.hasError) {
        entries.clear();
        Utils.runClosureInThread(nodeOptions.getCommonExecutor(), done, new Status(RaftError.EIO, "Corrupted LogStorage"));
        return;
    }
    boolean doUnlock = true;
    this.writeLock.lock();
    try {
        if (!entries.isEmpty() && !checkAndResolveConflict(entries, done)) {
            // If checkAndResolveConflict returns false, the done will be called in it.
            entries.clear();
            return;
        }
        for (int i = 0; i < entries.size(); i++) {
            final LogEntry entry = entries.get(i);
            // Set checksum after checkAndResolveConflict
            if (this.raftOptions.isEnableLogEntryChecksum()) {
                entry.setChecksum(entry.checksum());
            }
            if (entry.getType() == EntryType.ENTRY_TYPE_CONFIGURATION) {
                Configuration oldConf = new Configuration();
                if (entry.getOldPeers() != null) {
                    oldConf = new Configuration(entry.getOldPeers(), entry.getOldLearners());
                }
                final ConfigurationEntry conf = new ConfigurationEntry(entry.getId(), new Configuration(entry.getPeers(), entry.getLearners()), oldConf);
                this.configManager.add(conf);
            }
        }
        if (!entries.isEmpty()) {
            done.setFirstLogIndex(entries.get(0).getId().getIndex());
            this.logsInMemory.addAll(entries);
        }
        done.setEntries(entries);
        int retryTimes = 0;
        final EventTranslator<StableClosureEvent> translator = (event, sequence) -> {
            event.reset();
            event.groupId = groupId;
            event.type = EventType.OTHER;
            event.done = done;
        };
        while (true) {
            if (tryOfferEvent(done, translator)) {
                break;
            } else {
                retryTimes++;
                if (retryTimes > APPEND_LOG_RETRY_TIMES) {
                    reportError(RaftError.EBUSY.getNumber(), "LogManager is busy, disk queue overload.");
                    return;
                }
                ThreadHelper.onSpinWait();
            }
        }
        doUnlock = false;
        if (!wakeupAllWaiter(this.writeLock)) {
            notifyLastLogIndexListeners();
        }
    } finally {
        if (doUnlock) {
            this.writeLock.unlock();
        }
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) 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) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Aggregations

ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)3 LogStorageOptions (org.apache.ignite.raft.jraft.option.LogStorageOptions)3 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)3 LogStorage (org.apache.ignite.raft.jraft.storage.LogStorage)3 EventHandler (com.lmax.disruptor.EventHandler)2 EventTranslator (com.lmax.disruptor.EventTranslator)2 RingBuffer (com.lmax.disruptor.RingBuffer)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Lock (java.util.concurrent.locks.Lock)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 IgniteLogger (org.apache.ignite.lang.IgniteLogger)2 FSMCaller (org.apache.ignite.raft.jraft.FSMCaller)2 Status (org.apache.ignite.raft.jraft.Status)2 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)2