Search in sources :

Example 6 with EventTranslator

use of com.lmax.disruptor.EventTranslator 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)

Example 7 with EventTranslator

use of com.lmax.disruptor.EventTranslator in project dingo by dingodb.

the class NodeImpl method apply.

@Override
public void apply(final Task task) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(task.getDone(), new Status(RaftError.ENODESHUTDOWN, "Node is shutting down."));
        throw new IllegalStateException("Node is shutting down");
    }
    Requires.requireNonNull(task, "Null task");
    final LogEntry entry = new LogEntry();
    entry.setData(task.getData());
    int retryTimes = 0;
    try {
        final EventTranslator<LogEntryAndClosure> translator = (event, sequence) -> {
            event.reset();
            event.done = task.getDone();
            event.entry = entry;
            event.expectedTerm = task.getExpectedTerm();
        };
        this.applyQueue.publishEvent(translator);
    /*
            while (true) {
                if (this.applyQueue.tryPublishEvent(translator)) {
                    break;
                } else {
                    retryTimes++;
                    if (retryTimes > MAX_APPLY_RETRY_TIMES) {
                        Utils.runClosureInThread(task.getDone(),
                            new Status(RaftError.EBUSY, "Node is busy, has too many tasks."));
                        LOG.warn("Node {} applyQueue is overload.", getNodeId());
                        this.metrics.recordTimes("apply-task-overload-times", 1);
                        return;
                    }
                    ThreadHelper.onSpinWait();
                }
            }
             */
    } catch (final Exception e) {
        LOG.error("Fail to apply task.", e);
        Utils.runClosureInThread(task.getDone(), new Status(RaftError.EPERM, "Node is down."));
    }
}
Also used : Status(io.dingodb.raft.Status) FSMCallerOptions(io.dingodb.raft.option.FSMCallerOptions) RpcRequestClosure(io.dingodb.raft.rpc.RpcRequestClosure) StringUtils(org.apache.commons.lang.StringUtils) Describer(io.dingodb.raft.util.Describer) LongHeldDetectingReadWriteLock(io.dingodb.raft.util.concurrent.LongHeldDetectingReadWriteLock) ThreadHelper(io.dingodb.raft.util.ThreadHelper) LogManager(io.dingodb.raft.storage.LogManager) RaftClientService(io.dingodb.raft.rpc.RaftClientService) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) SynchronizedClosure(io.dingodb.raft.closure.SynchronizedClosure) LogManagerOptions(io.dingodb.raft.option.LogManagerOptions) Set(java.util.Set) Configuration(io.dingodb.raft.conf.Configuration) Utils(io.dingodb.raft.util.Utils) RaftOutter(io.dingodb.raft.entity.RaftOutter) EnumOutter(io.dingodb.raft.entity.EnumOutter) CountDownLatch(java.util.concurrent.CountDownLatch) RaftTimerFactory(io.dingodb.raft.util.timer.RaftTimerFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LogId(io.dingodb.raft.entity.LogId) JRaftServiceLoader(io.dingodb.raft.util.JRaftServiceLoader) SystemPropertyUtil(io.dingodb.raft.util.SystemPropertyUtil) JRaftUtils(io.dingodb.raft.JRaftUtils) LogExceptionHandler(io.dingodb.raft.util.LogExceptionHandler) ArrayList(java.util.ArrayList) JRaftServiceFactory(io.dingodb.raft.JRaftServiceFactory) ReadOnlyOption(io.dingodb.raft.option.ReadOnlyOption) BootstrapOptions(io.dingodb.raft.option.BootstrapOptions) LogStorage(io.dingodb.raft.storage.LogStorage) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) EventHandler(com.lmax.disruptor.EventHandler) LinkedHashSet(java.util.LinkedHashSet) DisruptorMetricSet(io.dingodb.raft.util.DisruptorMetricSet) DefaultRaftClientService(io.dingodb.raft.rpc.impl.core.DefaultRaftClientService) SnapshotExecutorImpl(io.dingodb.raft.storage.snapshot.SnapshotExecutorImpl) NodeManager(io.dingodb.raft.NodeManager) BallotBoxOptions(io.dingodb.raft.option.BallotBoxOptions) Lock(java.util.concurrent.locks.Lock) Ballot(io.dingodb.raft.entity.Ballot) PeerId(io.dingodb.raft.entity.PeerId) ReadOnlyServiceOptions(io.dingodb.raft.option.ReadOnlyServiceOptions) ConfigurationManager(io.dingodb.raft.conf.ConfigurationManager) SnapshotExecutor(io.dingodb.raft.storage.SnapshotExecutor) Disruptor(com.lmax.disruptor.dsl.Disruptor) RaftServerService(io.dingodb.raft.rpc.RaftServerService) ThreadId(io.dingodb.raft.util.ThreadId) ScheduledFuture(java.util.concurrent.ScheduledFuture) LoggerFactory(org.slf4j.LoggerFactory) ClosureQueueImpl(io.dingodb.raft.closure.ClosureQueueImpl) ByteBuffer(java.nio.ByteBuffer) RaftMetaStorageOptions(io.dingodb.raft.option.RaftMetaStorageOptions) SignalHelper(io.dingodb.raft.util.SignalHelper) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CatchUpClosure(io.dingodb.raft.closure.CatchUpClosure) Task(io.dingodb.raft.entity.Task) RaftOptions(io.dingodb.raft.option.RaftOptions) Collection(java.util.Collection) Status(io.dingodb.raft.Status) ReplicatorGroupOptions(io.dingodb.raft.option.ReplicatorGroupOptions) Collectors(java.util.stream.Collectors) FSMCaller(io.dingodb.raft.FSMCaller) DisruptorBuilder(io.dingodb.raft.util.DisruptorBuilder) RpcResponseClosureAdapter(io.dingodb.raft.rpc.RpcResponseClosureAdapter) List(java.util.List) NodeId(io.dingodb.raft.entity.NodeId) RpcResponseClosure(io.dingodb.raft.rpc.RpcResponseClosure) RaftException(io.dingodb.raft.error.RaftException) Requires(io.dingodb.raft.util.Requires) RaftError(io.dingodb.raft.error.RaftError) UserLog(io.dingodb.raft.entity.UserLog) LogEntry(io.dingodb.raft.entity.LogEntry) HashSet(java.util.HashSet) RaftMetaStorage(io.dingodb.raft.storage.RaftMetaStorage) SnapshotExecutorOptions(io.dingodb.raft.option.SnapshotExecutorOptions) ConfigurationEntry(io.dingodb.raft.conf.ConfigurationEntry) ReadOnlyService(io.dingodb.raft.ReadOnlyService) OnlyForTest(io.dingodb.raft.util.OnlyForTest) ReadIndexClosure(io.dingodb.raft.closure.ReadIndexClosure) LogIndexOutOfBoundsException(io.dingodb.raft.error.LogIndexOutOfBoundsException) Logger(org.slf4j.Logger) NamedThreadFactory(io.dingodb.raft.util.NamedThreadFactory) ClosureQueue(io.dingodb.raft.closure.ClosureQueue) RingBuffer(com.lmax.disruptor.RingBuffer) Closure(io.dingodb.raft.Closure) ProducerType(com.lmax.disruptor.dsl.ProducerType) Node(io.dingodb.raft.Node) Platform(io.dingodb.raft.util.Platform) NodeOptions(io.dingodb.raft.option.NodeOptions) LogNotFoundException(io.dingodb.raft.error.LogNotFoundException) ReplicatorGroup(io.dingodb.raft.ReplicatorGroup) TimeUnit(java.util.concurrent.TimeUnit) JRaftSignalHandler(io.dingodb.raft.util.JRaftSignalHandler) RpcFactoryHelper(io.dingodb.raft.util.RpcFactoryHelper) EventTranslator(com.lmax.disruptor.EventTranslator) LogManagerImpl(io.dingodb.raft.storage.impl.LogManagerImpl) RepeatedTimer(io.dingodb.raft.util.RepeatedTimer) Message(com.google.protobuf.Message) RpcRequests(io.dingodb.raft.rpc.RpcRequests) LeaderChangeContext(io.dingodb.raft.entity.LeaderChangeContext) EventFactory(com.lmax.disruptor.EventFactory) LogEntry(io.dingodb.raft.entity.LogEntry) RaftException(io.dingodb.raft.error.RaftException) LogIndexOutOfBoundsException(io.dingodb.raft.error.LogIndexOutOfBoundsException) LogNotFoundException(io.dingodb.raft.error.LogNotFoundException)

Aggregations

EventHandler (com.lmax.disruptor.EventHandler)7 EventTranslator (com.lmax.disruptor.EventTranslator)7 RingBuffer (com.lmax.disruptor.RingBuffer)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 Lock (java.util.concurrent.locks.Lock)7 TimeUnit (java.util.concurrent.TimeUnit)6 EventFactory (com.lmax.disruptor.EventFactory)5 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)4 Disruptor (com.lmax.disruptor.dsl.Disruptor)4 ProducerType (com.lmax.disruptor.dsl.ProducerType)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)4 ByteBuffer (java.nio.ByteBuffer)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)3