Search in sources :

Example 1 with LogStorage

use of io.dingodb.raft.storage.LogStorage in project dingo by dingodb.

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();
        LogStorageOptions lsOpts = new LogStorageOptions();
        lsOpts.setConfigurationManager(this.configManager);
        lsOpts.setLogEntryCodecFactory(opts.getLogEntryCodecFactory());
        lsOpts.setRaftLogStorageOptions(opts.getRaftLogStorageOptions());
        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 = // 
        DisruptorBuilder.<StableClosureEvent>newInstance().setEventFactory(// 
        new StableClosureEventFactory()).setRingBufferSize(// 
        opts.getDisruptorBufferSize()).setThreadFactory(// 
        new NamedThreadFactory("JRaft-LogManager-Disruptor-", true)).setProducerType(// 
        ProducerType.MULTI).setWaitStrategy(new BlockingWaitStrategy()).build();
        this.disruptor.handleEventsWith(new StableClosureEventHandler());
        this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(this.getClass().getSimpleName(), (event, ex) -> reportError(-1, "LogManager handle event error")));
        this.diskQueue = this.disruptor.start();
        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 : RaftException(io.dingodb.raft.error.RaftException) Requires(io.dingodb.raft.util.Requires) RaftError(io.dingodb.raft.error.RaftError) LogId(io.dingodb.raft.entity.LogId) LoggerFactory(org.slf4j.LoggerFactory) LogEntryCorruptedException(io.dingodb.raft.error.LogEntryCorruptedException) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NodeMetrics(io.dingodb.raft.core.NodeMetrics) LogEntry(io.dingodb.raft.entity.LogEntry) LogExceptionHandler(io.dingodb.raft.util.LogExceptionHandler) ArrayList(java.util.ArrayList) com.lmax.disruptor(com.lmax.disruptor) SnapshotMeta(io.dingodb.raft.entity.RaftOutter.SnapshotMeta) LogStorage(io.dingodb.raft.storage.LogStorage) Map(java.util.Map) ConfigurationEntry(io.dingodb.raft.conf.ConfigurationEntry) EntryType(io.dingodb.raft.entity.EnumOutter.EntryType) ThreadHelper(io.dingodb.raft.util.ThreadHelper) LogManager(io.dingodb.raft.storage.LogManager) RaftOptions(io.dingodb.raft.option.RaftOptions) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet) Logger(org.slf4j.Logger) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) ArrayDeque(io.dingodb.raft.util.ArrayDeque) LogManagerOptions(io.dingodb.raft.option.LogManagerOptions) ProducerType(com.lmax.disruptor.dsl.ProducerType) ErrorType(io.dingodb.raft.entity.EnumOutter.ErrorType) Status(io.dingodb.raft.Status) Configuration(io.dingodb.raft.conf.Configuration) Utils(io.dingodb.raft.util.Utils) FSMCaller(io.dingodb.raft.FSMCaller) DisruptorBuilder(io.dingodb.raft.util.DisruptorBuilder) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(java.util.concurrent.locks.Lock) PeerId(io.dingodb.raft.entity.PeerId) ConfigurationManager(io.dingodb.raft.conf.ConfigurationManager) LogStorageOptions(io.dingodb.raft.option.LogStorageOptions) SegmentList(io.dingodb.raft.util.SegmentList) Disruptor(com.lmax.disruptor.dsl.Disruptor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet) LogStorageOptions(io.dingodb.raft.option.LogStorageOptions) LogId(io.dingodb.raft.entity.LogId)

Example 2 with LogStorage

use of io.dingodb.raft.storage.LogStorage in project dingo by dingodb.

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(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);
        // 1. release the lock.
        doUnlock = false;
        if (!wakeupAllWaiter(this.writeLock)) {
            notifyLastLogIndexListeners();
        }
        // 2. Publish Event to Disruptor Queue.
        final EventTranslator<StableClosureEvent> translator = (event, sequence) -> {
            event.reset();
            event.type = EventType.OTHER;
            event.done = done;
        };
        doPublish(done, translator);
    } finally {
        if (doUnlock) {
            this.writeLock.unlock();
        }
    }
}
Also used : Status(io.dingodb.raft.Status) RaftException(io.dingodb.raft.error.RaftException) Requires(io.dingodb.raft.util.Requires) RaftError(io.dingodb.raft.error.RaftError) LogId(io.dingodb.raft.entity.LogId) LoggerFactory(org.slf4j.LoggerFactory) LogEntryCorruptedException(io.dingodb.raft.error.LogEntryCorruptedException) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NodeMetrics(io.dingodb.raft.core.NodeMetrics) LogEntry(io.dingodb.raft.entity.LogEntry) LogExceptionHandler(io.dingodb.raft.util.LogExceptionHandler) ArrayList(java.util.ArrayList) com.lmax.disruptor(com.lmax.disruptor) SnapshotMeta(io.dingodb.raft.entity.RaftOutter.SnapshotMeta) LogStorage(io.dingodb.raft.storage.LogStorage) Map(java.util.Map) ConfigurationEntry(io.dingodb.raft.conf.ConfigurationEntry) EntryType(io.dingodb.raft.entity.EnumOutter.EntryType) ThreadHelper(io.dingodb.raft.util.ThreadHelper) LogManager(io.dingodb.raft.storage.LogManager) RaftOptions(io.dingodb.raft.option.RaftOptions) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet) Logger(org.slf4j.Logger) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) ArrayDeque(io.dingodb.raft.util.ArrayDeque) LogManagerOptions(io.dingodb.raft.option.LogManagerOptions) ProducerType(com.lmax.disruptor.dsl.ProducerType) ErrorType(io.dingodb.raft.entity.EnumOutter.ErrorType) Status(io.dingodb.raft.Status) Configuration(io.dingodb.raft.conf.Configuration) Utils(io.dingodb.raft.util.Utils) FSMCaller(io.dingodb.raft.FSMCaller) DisruptorBuilder(io.dingodb.raft.util.DisruptorBuilder) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(java.util.concurrent.locks.Lock) PeerId(io.dingodb.raft.entity.PeerId) ConfigurationManager(io.dingodb.raft.conf.ConfigurationManager) LogStorageOptions(io.dingodb.raft.option.LogStorageOptions) SegmentList(io.dingodb.raft.util.SegmentList) Disruptor(com.lmax.disruptor.dsl.Disruptor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Configuration(io.dingodb.raft.conf.Configuration) ConfigurationEntry(io.dingodb.raft.conf.ConfigurationEntry) LogEntry(io.dingodb.raft.entity.LogEntry)

Aggregations

com.lmax.disruptor (com.lmax.disruptor)2 Disruptor (com.lmax.disruptor.dsl.Disruptor)2 ProducerType (com.lmax.disruptor.dsl.ProducerType)2 FSMCaller (io.dingodb.raft.FSMCaller)2 Status (io.dingodb.raft.Status)2 Configuration (io.dingodb.raft.conf.Configuration)2 ConfigurationEntry (io.dingodb.raft.conf.ConfigurationEntry)2 ConfigurationManager (io.dingodb.raft.conf.ConfigurationManager)2 NodeMetrics (io.dingodb.raft.core.NodeMetrics)2 EntryType (io.dingodb.raft.entity.EnumOutter.EntryType)2 ErrorType (io.dingodb.raft.entity.EnumOutter.ErrorType)2 LogEntry (io.dingodb.raft.entity.LogEntry)2 LogId (io.dingodb.raft.entity.LogId)2 PeerId (io.dingodb.raft.entity.PeerId)2 SnapshotMeta (io.dingodb.raft.entity.RaftOutter.SnapshotMeta)2 LogEntryCorruptedException (io.dingodb.raft.error.LogEntryCorruptedException)2 RaftError (io.dingodb.raft.error.RaftError)2 RaftException (io.dingodb.raft.error.RaftException)2 LogManagerOptions (io.dingodb.raft.option.LogManagerOptions)2 LogStorageOptions (io.dingodb.raft.option.LogStorageOptions)2