Search in sources :

Example 16 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class NodeImpl method executeApplyingTasks.

private void executeApplyingTasks(final List<LogEntryAndClosure> tasks) {
    this.writeLock.lock();
    try {
        final int size = tasks.size();
        if (this.state != State.STATE_LEADER) {
            final Status st = new Status();
            if (this.state != State.STATE_TRANSFERRING) {
                st.setError(RaftError.EPERM, "Is not leader.");
            } else {
                st.setError(RaftError.EBUSY, "Is transferring leadership.");
            }
            LOG.debug("Node {} can't apply, status={}.", getNodeId(), st);
            final List<Closure> dones = tasks.stream().map(ele -> ele.done).collect(Collectors.toList());
            Utils.runInThread(this.getOptions().getCommonExecutor(), () -> {
                for (final Closure done : dones) {
                    done.run(st);
                }
            });
            return;
        }
        final List<LogEntry> entries = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            final LogEntryAndClosure task = tasks.get(i);
            if (task.expectedTerm != -1 && task.expectedTerm != this.currTerm) {
                LOG.debug("Node {} can't apply task whose expectedTerm={} doesn't match currTerm={}.", getNodeId(), task.expectedTerm, this.currTerm);
                if (task.done != null) {
                    final Status st = new Status(RaftError.EPERM, "expected_term=%d doesn't match current_term=%d", task.expectedTerm, this.currTerm);
                    Utils.runClosureInThread(this.getOptions().getCommonExecutor(), task.done, st);
                }
                continue;
            }
            if (!this.ballotBox.appendPendingTask(this.conf.getConf(), this.conf.isStable() ? null : this.conf.getOldConf(), task.done)) {
                Utils.runClosureInThread(this.getOptions().getCommonExecutor(), task.done, new Status(RaftError.EINTERNAL, "Fail to append task."));
                continue;
            }
            // set task entry info before adding to list.
            task.entry.getId().setTerm(this.currTerm);
            task.entry.setType(EnumOutter.EntryType.ENTRY_TYPE_DATA);
            entries.add(task.entry);
        }
        this.logManager.appendEntries(entries, new LeaderStableClosure(entries));
        // update conf.first
        checkAndSetConfiguration(true);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) SnapshotExecutorImpl(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotExecutorImpl) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Set(java.util.Set) AppendEntriesResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.AppendEntriesResponse) ExecutorServiceHelper(org.apache.ignite.raft.jraft.util.ExecutorServiceHelper) AppendEntriesRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.AppendEntriesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) SnapshotExecutorOptions(org.apache.ignite.raft.jraft.option.SnapshotExecutorOptions) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RaftError(org.apache.ignite.raft.jraft.error.RaftError) LogManagerImpl(org.apache.ignite.raft.jraft.storage.impl.LogManagerImpl) StringUtils(org.apache.ignite.raft.jraft.util.StringUtils) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) RaftException(org.apache.ignite.raft.jraft.error.RaftException) RaftMetaStorageOptions(org.apache.ignite.raft.jraft.option.RaftMetaStorageOptions) ArrayList(java.util.ArrayList) Task(org.apache.ignite.raft.jraft.entity.Task) TimeoutStrategy(org.apache.ignite.raft.jraft.util.TimeoutStrategy) SnapshotExecutor(org.apache.ignite.raft.jraft.storage.SnapshotExecutor) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CatchUpClosure(org.apache.ignite.raft.jraft.closure.CatchUpClosure) BallotBoxOptions(org.apache.ignite.raft.jraft.option.BallotBoxOptions) EventHandler(com.lmax.disruptor.EventHandler) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ReadIndexResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexResponse) LinkedHashSet(java.util.LinkedHashSet) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) SystemPropertyUtil(org.apache.ignite.raft.jraft.util.SystemPropertyUtil) TimeoutNowResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.TimeoutNowResponse) Closure(org.apache.ignite.raft.jraft.Closure) ClosureQueue(org.apache.ignite.raft.jraft.closure.ClosureQueue) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) RaftRpcFactory(org.apache.ignite.raft.jraft.rpc.RaftRpcFactory) Lock(java.util.concurrent.locks.Lock) LogManagerOptions(org.apache.ignite.raft.jraft.option.LogManagerOptions) FSMCaller(org.apache.ignite.raft.jraft.FSMCaller) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) TimeoutNowRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.TimeoutNowRequest) LeaderChangeContext(org.apache.ignite.raft.jraft.entity.LeaderChangeContext) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RaftServerService(org.apache.ignite.raft.jraft.rpc.RaftServerService) LogManager(org.apache.ignite.raft.jraft.storage.LogManager) ScheduledFuture(java.util.concurrent.ScheduledFuture) EnumOutter(org.apache.ignite.raft.jraft.entity.EnumOutter) Requires(org.apache.ignite.raft.jraft.util.Requires) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ByteBuffer(java.nio.ByteBuffer) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) ReadIndexResponseBuilder(org.apache.ignite.raft.jraft.rpc.ReadIndexResponseBuilder) ReplicatorGroupOptions(org.apache.ignite.raft.jraft.option.ReplicatorGroupOptions) ReadOnlyOption(org.apache.ignite.raft.jraft.option.ReadOnlyOption) AppendEntriesResponseBuilder(org.apache.ignite.raft.jraft.rpc.AppendEntriesResponseBuilder) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) RpcResponseClosureAdapter(org.apache.ignite.raft.jraft.rpc.RpcResponseClosureAdapter) Node(org.apache.ignite.raft.jraft.Node) LogId(org.apache.ignite.raft.jraft.entity.LogId) ReadOnlyService(org.apache.ignite.raft.jraft.ReadOnlyService) Collection(java.util.Collection) Status(org.apache.ignite.raft.jraft.Status) GroupAware(org.apache.ignite.raft.jraft.disruptor.GroupAware) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) Collectors(java.util.stream.Collectors) OnlyForTest(org.apache.ignite.raft.jraft.util.OnlyForTest) Utils(org.apache.ignite.raft.jraft.util.Utils) List(java.util.List) LogStorage(org.apache.ignite.raft.jraft.storage.LogStorage) LongHeldDetectingReadWriteLock(org.apache.ignite.raft.jraft.util.concurrent.LongHeldDetectingReadWriteLock) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) RequestVoteRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.RequestVoteRequest) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) HashSet(java.util.HashSet) RepeatedTimer(org.apache.ignite.raft.jraft.util.RepeatedTimer) RequestVoteResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.RequestVoteResponse) JRaftServiceFactory(org.apache.ignite.raft.jraft.JRaftServiceFactory) Describer(org.apache.ignite.raft.jraft.util.Describer) RaftClientService(org.apache.ignite.raft.jraft.rpc.RaftClientService) ClosureQueueImpl(org.apache.ignite.raft.jraft.closure.ClosureQueueImpl) JRaftUtils(org.apache.ignite.raft.jraft.JRaftUtils) RingBuffer(com.lmax.disruptor.RingBuffer) FSMCallerOptions(org.apache.ignite.raft.jraft.option.FSMCallerOptions) RaftMetaStorage(org.apache.ignite.raft.jraft.storage.RaftMetaStorage) ThreadHelper(org.apache.ignite.raft.jraft.util.ThreadHelper) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) Ballot(org.apache.ignite.raft.jraft.entity.Ballot) InstallSnapshotRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotRequest) EventTranslator(com.lmax.disruptor.EventTranslator) UserLog(org.apache.ignite.raft.jraft.entity.UserLog) Message(org.apache.ignite.raft.jraft.rpc.Message) ThreadId(org.apache.ignite.raft.jraft.util.ThreadId) ReplicatorGroup(org.apache.ignite.raft.jraft.ReplicatorGroup) ByteString(org.apache.ignite.raft.jraft.util.ByteString) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) CatchUpClosure(org.apache.ignite.raft.jraft.closure.CatchUpClosure) Closure(org.apache.ignite.raft.jraft.Closure) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 17 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class NodeImpl method handleAppendEntriesRequest.

@Override
public Message handleAppendEntriesRequest(final AppendEntriesRequest request, final RpcRequestClosure done) {
    boolean doUnlock = true;
    final long startMs = Utils.monotonicMs();
    this.writeLock.lock();
    final int entriesCount = Utils.size(request.entriesList());
    try {
        if (!this.state.isActive()) {
            LOG.warn("Node {} is not in active state, currTerm={}.", getNodeId(), this.currTerm);
            return // 
            RaftRpcFactory.DEFAULT.newResponse(raftOptions.getRaftMessagesFactory(), RaftError.EINVAL, "Node %s is not in active state, state %s.", getNodeId(), this.state.name());
        }
        final PeerId serverId = new PeerId();
        if (!serverId.parse(request.serverId())) {
            LOG.warn("Node {} received AppendEntriesRequest from {} serverId bad format.", getNodeId(), request.serverId());
            return // 
            RaftRpcFactory.DEFAULT.newResponse(raftOptions.getRaftMessagesFactory(), RaftError.EINVAL, "Parse serverId failed: %s.", request.serverId());
        }
        // Check stale term
        if (request.term() < this.currTerm) {
            LOG.warn("Node {} ignore stale AppendEntriesRequest from {}, term={}, currTerm={}.", getNodeId(), request.serverId(), request.term(), this.currTerm);
            return raftOptions.getRaftMessagesFactory().appendEntriesResponse().success(false).term(this.currTerm).build();
        }
        // Check term and state to step down
        checkStepDown(request.term(), serverId);
        if (!serverId.equals(this.leaderId)) {
            LOG.error("Another peer {} declares that it is the leader at term {} which was occupied by leader {}.", serverId, this.currTerm, this.leaderId);
            // Increase the term by 1 and make both leaders step down to minimize the
            // loss of split brain
            stepDown(request.term() + 1, false, new Status(RaftError.ELEADERCONFLICT, "More than one leader in the same term."));
            return raftOptions.getRaftMessagesFactory().appendEntriesResponse().success(// 
            false).term(// 
            request.term() + 1).build();
        }
        updateLastLeaderTimestamp(Utils.monotonicMs());
        if (entriesCount > 0 && this.snapshotExecutor != null && this.snapshotExecutor.isInstallingSnapshot()) {
            LOG.warn("Node {} received AppendEntriesRequest while installing snapshot.", getNodeId());
            return // 
            RaftRpcFactory.DEFAULT.newResponse(raftOptions.getRaftMessagesFactory(), RaftError.EBUSY, "Node %s:%s is installing snapshot.", this.groupId, this.serverId);
        }
        final long prevLogIndex = request.prevLogIndex();
        final long prevLogTerm = request.prevLogTerm();
        final long localPrevLogTerm = this.logManager.getTerm(prevLogIndex);
        if (localPrevLogTerm != prevLogTerm) {
            final long lastLogIndex = this.logManager.getLastLogIndex();
            LOG.warn("Node {} reject term_unmatched AppendEntriesRequest from {}, term={}, prevLogIndex={}, " + "prevLogTerm={}, localPrevLogTerm={}, lastLogIndex={}, entriesSize={}.", getNodeId(), request.serverId(), request.term(), prevLogIndex, prevLogTerm, localPrevLogTerm, lastLogIndex, entriesCount);
            return raftOptions.getRaftMessagesFactory().appendEntriesResponse().success(false).term(this.currTerm).lastLogIndex(lastLogIndex).build();
        }
        if (entriesCount == 0) {
            // heartbeat or probe request
            final AppendEntriesResponseBuilder respBuilder = raftOptions.getRaftMessagesFactory().appendEntriesResponse().success(true).term(this.currTerm).lastLogIndex(this.logManager.getLastLogIndex());
            doUnlock = false;
            this.writeLock.unlock();
            // see the comments at FollowerStableClosure#run()
            this.ballotBox.setLastCommittedIndex(Math.min(request.committedIndex(), prevLogIndex));
            return respBuilder.build();
        }
        // Parse request
        long index = prevLogIndex;
        final List<LogEntry> entries = new ArrayList<>(entriesCount);
        ByteBuffer allData = request.data() != null ? request.data().asReadOnlyByteBuffer() : ByteString.EMPTY.asReadOnlyByteBuffer();
        final Collection<RaftOutter.EntryMeta> entriesList = request.entriesList();
        for (RaftOutter.EntryMeta entry : entriesList) {
            index++;
            final LogEntry logEntry = logEntryFromMeta(index, allData, entry);
            if (logEntry != null) {
                // Validate checksum
                if (this.raftOptions.isEnableLogEntryChecksum() && logEntry.isCorrupted()) {
                    long realChecksum = logEntry.checksum();
                    LOG.error("Corrupted log entry received from leader, index={}, term={}, expectedChecksum={}, realChecksum={}", logEntry.getId().getIndex(), logEntry.getId().getTerm(), logEntry.getChecksum(), realChecksum);
                    return // 
                    RaftRpcFactory.DEFAULT.newResponse(raftOptions.getRaftMessagesFactory(), RaftError.EINVAL, "The log entry is corrupted, index=%d, term=%d, expectedChecksum=%d, realChecksum=%d", logEntry.getId().getIndex(), logEntry.getId().getTerm(), logEntry.getChecksum(), realChecksum);
                }
                entries.add(logEntry);
            }
        }
        final FollowerStableClosure closure = new FollowerStableClosure(request, raftOptions.getRaftMessagesFactory().appendEntriesResponse().term(this.currTerm), this, done, this.currTerm);
        this.logManager.appendEntries(entries, closure);
        // update configuration after _log_manager updated its memory status
        checkAndSetConfiguration(true);
        return null;
    } finally {
        if (doUnlock) {
            this.writeLock.unlock();
        }
        this.metrics.recordLatency("handle-append-entries", Utils.monotonicMs() - startMs);
        this.metrics.recordSize("handle-append-entries-count", entriesCount);
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) AppendEntriesResponseBuilder(org.apache.ignite.raft.jraft.rpc.AppendEntriesResponseBuilder) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 18 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class NodeImpl method unsafeApplyConfiguration.

private void unsafeApplyConfiguration(final Configuration newConf, final Configuration oldConf, final boolean leaderStart) {
    Requires.requireTrue(this.confCtx.isBusy(), "ConfigurationContext is not busy");
    final LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    entry.setId(new LogId(0, this.currTerm));
    entry.setPeers(newConf.listPeers());
    entry.setLearners(newConf.listLearners());
    if (oldConf != null) {
        entry.setOldPeers(oldConf.listPeers());
        entry.setOldLearners(oldConf.listLearners());
    }
    final ConfigurationChangeDone configurationChangeDone = new ConfigurationChangeDone(this.currTerm, leaderStart);
    // Use the new_conf to deal the quorum of this very log
    if (!this.ballotBox.appendPendingTask(newConf, oldConf, configurationChangeDone)) {
        Utils.runClosureInThread(this.getOptions().getCommonExecutor(), configurationChangeDone, new Status(RaftError.EINTERNAL, "Fail to append task."));
        return;
    }
    final List<LogEntry> entries = new ArrayList<>();
    entries.add(entry);
    this.logManager.appendEntries(entries, new LeaderStableClosure(entries));
    checkAndSetConfiguration(false);
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) LogId(org.apache.ignite.raft.jraft.entity.LogId) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry)

Example 19 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class NodeImpl method apply.

@Override
public void apply(final Task task) {
    if (this.shutdownLatch != null) {
        Utils.runClosureInThread(this.getOptions().getCommonExecutor(), 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.groupId = groupId;
            event.done = task.getDone();
            event.entry = entry;
            event.expectedTerm = task.getExpectedTerm();
        };
        while (true) {
            if (this.applyQueue.tryPublishEvent(translator)) {
                break;
            } else {
                retryTimes++;
                if (retryTimes > MAX_APPLY_RETRY_TIMES) {
                    Utils.runClosureInThread(this.getOptions().getCommonExecutor(), 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(this.getOptions().getCommonExecutor(), task.getDone(), new Status(RaftError.EPERM, "Node is down."));
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) SnapshotExecutorImpl(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotExecutorImpl) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Set(java.util.Set) AppendEntriesResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.AppendEntriesResponse) ExecutorServiceHelper(org.apache.ignite.raft.jraft.util.ExecutorServiceHelper) AppendEntriesRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.AppendEntriesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationEntry(org.apache.ignite.raft.jraft.conf.ConfigurationEntry) SnapshotExecutorOptions(org.apache.ignite.raft.jraft.option.SnapshotExecutorOptions) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RaftError(org.apache.ignite.raft.jraft.error.RaftError) LogManagerImpl(org.apache.ignite.raft.jraft.storage.impl.LogManagerImpl) StringUtils(org.apache.ignite.raft.jraft.util.StringUtils) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) RaftException(org.apache.ignite.raft.jraft.error.RaftException) RaftMetaStorageOptions(org.apache.ignite.raft.jraft.option.RaftMetaStorageOptions) ArrayList(java.util.ArrayList) Task(org.apache.ignite.raft.jraft.entity.Task) TimeoutStrategy(org.apache.ignite.raft.jraft.util.TimeoutStrategy) SnapshotExecutor(org.apache.ignite.raft.jraft.storage.SnapshotExecutor) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) CatchUpClosure(org.apache.ignite.raft.jraft.closure.CatchUpClosure) BallotBoxOptions(org.apache.ignite.raft.jraft.option.BallotBoxOptions) EventHandler(com.lmax.disruptor.EventHandler) ReadOnlyServiceOptions(org.apache.ignite.raft.jraft.option.ReadOnlyServiceOptions) ReadIndexResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexResponse) LinkedHashSet(java.util.LinkedHashSet) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) SystemPropertyUtil(org.apache.ignite.raft.jraft.util.SystemPropertyUtil) TimeoutNowResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.TimeoutNowResponse) Closure(org.apache.ignite.raft.jraft.Closure) ClosureQueue(org.apache.ignite.raft.jraft.closure.ClosureQueue) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) RaftRpcFactory(org.apache.ignite.raft.jraft.rpc.RaftRpcFactory) Lock(java.util.concurrent.locks.Lock) LogManagerOptions(org.apache.ignite.raft.jraft.option.LogManagerOptions) FSMCaller(org.apache.ignite.raft.jraft.FSMCaller) ReadIndexRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.ReadIndexRequest) TimeoutNowRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.TimeoutNowRequest) LeaderChangeContext(org.apache.ignite.raft.jraft.entity.LeaderChangeContext) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) RaftServerService(org.apache.ignite.raft.jraft.rpc.RaftServerService) LogManager(org.apache.ignite.raft.jraft.storage.LogManager) ScheduledFuture(java.util.concurrent.ScheduledFuture) EnumOutter(org.apache.ignite.raft.jraft.entity.EnumOutter) Requires(org.apache.ignite.raft.jraft.util.Requires) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ByteBuffer(java.nio.ByteBuffer) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) ReadIndexResponseBuilder(org.apache.ignite.raft.jraft.rpc.ReadIndexResponseBuilder) ReplicatorGroupOptions(org.apache.ignite.raft.jraft.option.ReplicatorGroupOptions) ReadOnlyOption(org.apache.ignite.raft.jraft.option.ReadOnlyOption) AppendEntriesResponseBuilder(org.apache.ignite.raft.jraft.rpc.AppendEntriesResponseBuilder) StripedDisruptor(org.apache.ignite.raft.jraft.disruptor.StripedDisruptor) RpcResponseClosureAdapter(org.apache.ignite.raft.jraft.rpc.RpcResponseClosureAdapter) Node(org.apache.ignite.raft.jraft.Node) LogId(org.apache.ignite.raft.jraft.entity.LogId) ReadOnlyService(org.apache.ignite.raft.jraft.ReadOnlyService) Collection(java.util.Collection) Status(org.apache.ignite.raft.jraft.Status) GroupAware(org.apache.ignite.raft.jraft.disruptor.GroupAware) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) Collectors(java.util.stream.Collectors) OnlyForTest(org.apache.ignite.raft.jraft.util.OnlyForTest) Utils(org.apache.ignite.raft.jraft.util.Utils) List(java.util.List) LogStorage(org.apache.ignite.raft.jraft.storage.LogStorage) LongHeldDetectingReadWriteLock(org.apache.ignite.raft.jraft.util.concurrent.LongHeldDetectingReadWriteLock) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) DisruptorMetricSet(org.apache.ignite.raft.jraft.util.DisruptorMetricSet) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) RequestVoteRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.RequestVoteRequest) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) HashSet(java.util.HashSet) RepeatedTimer(org.apache.ignite.raft.jraft.util.RepeatedTimer) RequestVoteResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.RequestVoteResponse) JRaftServiceFactory(org.apache.ignite.raft.jraft.JRaftServiceFactory) Describer(org.apache.ignite.raft.jraft.util.Describer) RaftClientService(org.apache.ignite.raft.jraft.rpc.RaftClientService) ClosureQueueImpl(org.apache.ignite.raft.jraft.closure.ClosureQueueImpl) JRaftUtils(org.apache.ignite.raft.jraft.JRaftUtils) RingBuffer(com.lmax.disruptor.RingBuffer) FSMCallerOptions(org.apache.ignite.raft.jraft.option.FSMCallerOptions) RaftMetaStorage(org.apache.ignite.raft.jraft.storage.RaftMetaStorage) ThreadHelper(org.apache.ignite.raft.jraft.util.ThreadHelper) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) Ballot(org.apache.ignite.raft.jraft.entity.Ballot) InstallSnapshotRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.InstallSnapshotRequest) EventTranslator(com.lmax.disruptor.EventTranslator) UserLog(org.apache.ignite.raft.jraft.entity.UserLog) Message(org.apache.ignite.raft.jraft.rpc.Message) ThreadId(org.apache.ignite.raft.jraft.util.ThreadId) ReplicatorGroup(org.apache.ignite.raft.jraft.ReplicatorGroup) ByteString(org.apache.ignite.raft.jraft.util.ByteString) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) RaftException(org.apache.ignite.raft.jraft.error.RaftException) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException)

Example 20 with LogEntry

use of org.apache.ignite.raft.jraft.entity.LogEntry in project ignite-3 by apache.

the class NodeImpl method bootstrap.

public boolean bootstrap(final BootstrapOptions opts) throws InterruptedException {
    if (opts.getLastLogIndex() > 0 && (opts.getGroupConf().isEmpty() || opts.getFsm() == null)) {
        LOG.error("Invalid arguments for bootstrap, groupConf={}, fsm={}, lastLogIndex={}.", opts.getGroupConf(), opts.getFsm(), opts.getLastLogIndex());
        return false;
    }
    if (opts.getGroupConf().isEmpty()) {
        LOG.error("Bootstrapping an empty node makes no sense.");
        return false;
    }
    Requires.requireNonNull(opts.getServiceFactory(), "Null jraft service factory");
    this.serviceFactory = opts.getServiceFactory();
    // Term is not an option since changing it is very dangerous
    final long bootstrapLogTerm = opts.getLastLogIndex() > 0 ? 1 : 0;
    final LogId bootstrapId = new LogId(opts.getLastLogIndex(), bootstrapLogTerm);
    this.options = opts.getNodeOptions() == null ? new NodeOptions() : opts.getNodeOptions();
    this.raftOptions = this.options.getRaftOptions();
    this.metrics = new NodeMetrics(opts.isEnableMetrics());
    this.options.setFsm(opts.getFsm());
    this.options.setLogUri(opts.getLogUri());
    this.options.setRaftMetaUri(opts.getRaftMetaUri());
    this.options.setSnapshotUri(opts.getSnapshotUri());
    this.configManager = new ConfigurationManager();
    // Create fsmCaller at first as logManager needs it to report error
    this.fsmCaller = new FSMCallerImpl();
    initPools(opts.getNodeOptions());
    if (!initLogStorage()) {
        LOG.error("Fail to init log storage.");
        return false;
    }
    if (!initMetaStorage()) {
        LOG.error("Fail to init meta storage.");
        return false;
    }
    if (this.currTerm == 0) {
        this.currTerm = 1;
        if (!this.metaStorage.setTermAndVotedFor(1, new PeerId())) {
            LOG.error("Fail to set term.");
            return false;
        }
    }
    if (opts.getFsm() != null && !initFSMCaller(bootstrapId)) {
        LOG.error("Fail to init fsm caller.");
        return false;
    }
    final LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
    entry.getId().setTerm(this.currTerm);
    entry.setPeers(opts.getGroupConf().listPeers());
    entry.setLearners(opts.getGroupConf().listLearners());
    final List<LogEntry> entries = new ArrayList<>();
    entries.add(entry);
    final BootstrapStableClosure bootstrapDone = new BootstrapStableClosure();
    this.logManager.appendEntries(entries, bootstrapDone);
    if (!bootstrapDone.await().isOk()) {
        LOG.error("Fail to append configuration.");
        return false;
    }
    if (opts.getLastLogIndex() > 0) {
        if (!initSnapshotStorage()) {
            LOG.error("Fail to init snapshot storage.");
            return false;
        }
        final SynchronizedClosure snapshotDone = new SynchronizedClosure();
        this.snapshotExecutor.doSnapshot(snapshotDone);
        if (!snapshotDone.await().isOk()) {
            LOG.error("Fail to save snapshot, status={}.", snapshotDone.getStatus());
            return false;
        }
    }
    if (this.logManager.getFirstLogIndex() != opts.getLastLogIndex() + 1) {
        throw new IllegalStateException("First and last log index mismatch");
    }
    if (opts.getLastLogIndex() > 0) {
        if (this.logManager.getLastLogIndex() != opts.getLastLogIndex()) {
            throw new IllegalStateException("Last log index mismatch");
        }
    } else {
        if (this.logManager.getLastLogIndex() != opts.getLastLogIndex() + 1) {
            throw new IllegalStateException("Last log index mismatch");
        }
    }
    return true;
}
Also used : SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) LogId(org.apache.ignite.raft.jraft.entity.LogId) ConfigurationManager(org.apache.ignite.raft.jraft.conf.ConfigurationManager) LogEntry(org.apache.ignite.raft.jraft.entity.LogEntry) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

LogEntry (org.apache.ignite.raft.jraft.entity.LogEntry)48 LogId (org.apache.ignite.raft.jraft.entity.LogId)26 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)12 Status (org.apache.ignite.raft.jraft.Status)11 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)11 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 LogManager (org.apache.ignite.raft.jraft.storage.LogManager)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)6 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)6 ByteBuffer (java.nio.ByteBuffer)5 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)5 ConfigurationManager (org.apache.ignite.raft.jraft.conf.ConfigurationManager)4 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)4 EventHandler (com.lmax.disruptor.EventHandler)3 EventTranslator (com.lmax.disruptor.EventTranslator)3 RingBuffer (com.lmax.disruptor.RingBuffer)3 List (java.util.List)3