Search in sources :

Example 26 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class NodeImpl method resetLearners.

@Override
public void resetLearners(final List<PeerId> learners, final Closure done) {
    checkPeers(learners);
    this.writeLock.lock();
    try {
        final Configuration newConf = new Configuration(this.conf.getConf());
        newConf.setLearners(new LinkedHashSet<>(learners));
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Configuration(io.dingodb.raft.conf.Configuration)

Example 27 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class FSMCallerImpl method doCommitted.

private void doCommitted(final long committedIndex) {
    if (!this.error.getStatus().isOk()) {
        return;
    }
    final long lastAppliedIndex = this.lastAppliedIndex.get();
    // We can tolerate the disorder of committed_index
    if (lastAppliedIndex >= committedIndex) {
        return;
    }
    final long startMs = Utils.monotonicMs();
    try {
        final List<Closure> closures = new ArrayList<>();
        final List<TaskClosure> taskClosures = new ArrayList<>();
        final long firstClosureIndex = this.closureQueue.popClosureUntil(committedIndex, closures, taskClosures);
        // Calls TaskClosure#onCommitted if necessary
        onTaskCommitted(taskClosures);
        Requires.requireTrue(firstClosureIndex >= 0, "Invalid firstClosureIndex");
        final IteratorImpl iterImpl = new IteratorImpl(this.fsm, this.logManager, closures, firstClosureIndex, lastAppliedIndex, committedIndex, this.applyingIndex);
        while (iterImpl.isGood()) {
            final LogEntry logEntry = iterImpl.entry();
            if (logEntry.getType() != EnumOutter.EntryType.ENTRY_TYPE_DATA) {
                if (logEntry.getType() == EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION) {
                    if (logEntry.getOldPeers() != null && !logEntry.getOldPeers().isEmpty()) {
                        // Joint stage is not supposed to be noticeable by end users.
                        this.fsm.onConfigurationCommitted(new Configuration(iterImpl.entry().getPeers()));
                    }
                }
                if (iterImpl.done() != null) {
                    // For other entries, we have nothing to do besides flush the
                    // pending tasks and run this closure to notify the caller that the
                    // entries before this one were successfully committed and applied.
                    iterImpl.done().run(Status.OK());
                }
                iterImpl.next();
                continue;
            }
            // Apply data task to user state machine
            doApplyTasks(iterImpl);
        }
        if (iterImpl.hasError()) {
            setError(iterImpl.getError());
            iterImpl.runTheRestClosureWithError();
        }
        final long lastIndex = iterImpl.getIndex() - 1;
        final long lastTerm = this.logManager.getTerm(lastIndex);
        final LogId lastAppliedId = new LogId(lastIndex, lastTerm);
        this.lastAppliedIndex.set(lastIndex);
        this.lastAppliedTerm = lastTerm;
        this.logManager.setAppliedId(lastAppliedId);
        notifyLastAppliedIndexUpdated(lastIndex);
    } finally {
        this.nodeMetrics.recordLatency("fsm-commit", Utils.monotonicMs() - startMs);
    }
}
Also used : TaskClosure(io.dingodb.raft.closure.TaskClosure) LoadSnapshotClosure(io.dingodb.raft.closure.LoadSnapshotClosure) Closure(io.dingodb.raft.Closure) SaveSnapshotClosure(io.dingodb.raft.closure.SaveSnapshotClosure) Configuration(io.dingodb.raft.conf.Configuration) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LogId(io.dingodb.raft.entity.LogId) LogEntry(io.dingodb.raft.entity.LogEntry) TaskClosure(io.dingodb.raft.closure.TaskClosure)

Example 28 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class FSMCallerImpl method doSnapshotLoad.

private void doSnapshotLoad(final LoadSnapshotClosure done) {
    Requires.requireNonNull(done, "LoadSnapshotClosure is null");
    final SnapshotReader reader = done.start();
    if (reader == null) {
        done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
        return;
    }
    final RaftOutter.SnapshotMeta meta = reader.load();
    if (meta == null) {
        done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
        if (reader.getRaftError() == RaftError.EIO) {
            final RaftException err = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
            setError(err);
        }
        return;
    }
    final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
    final LogId snapshotId = new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm());
    if (lastAppliedId.compareTo(snapshotId) > 0) {
        done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d " + "last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
        return;
    }
    if (!this.fsm.onSnapshotLoad(reader)) {
        done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
        final RaftException e = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
        setError(e);
        return;
    }
    if (meta.getOldPeersCount() == 0) {
        // Joint stage is not supposed to be noticeable by end users.
        final Configuration conf = new Configuration();
        for (int i = 0, size = meta.getPeersCount(); i < size; i++) {
            final PeerId peer = new PeerId();
            Requires.requireTrue(peer.parse(meta.getPeers(i)), "Parse peer failed");
            conf.addPeer(peer);
        }
        this.fsm.onConfigurationCommitted(conf);
    }
    this.lastAppliedIndex.set(meta.getLastIncludedIndex());
    this.lastAppliedTerm = meta.getLastIncludedTerm();
    done.run(Status.OK());
}
Also used : Status(io.dingodb.raft.Status) RaftException(io.dingodb.raft.error.RaftException) Configuration(io.dingodb.raft.conf.Configuration) RaftOutter(io.dingodb.raft.entity.RaftOutter) SnapshotReader(io.dingodb.raft.storage.snapshot.SnapshotReader) LogId(io.dingodb.raft.entity.LogId) PeerId(io.dingodb.raft.entity.PeerId)

Example 29 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class NodeImpl method removeLearners.

@Override
public void removeLearners(final List<PeerId> learners, final Closure done) {
    checkPeers(learners);
    this.writeLock.lock();
    try {
        final Configuration newConf = new Configuration(this.conf.getConf());
        for (final PeerId peer : learners) {
            newConf.removeLearner(peer);
        }
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) PeerId(io.dingodb.raft.entity.PeerId)

Example 30 with Configuration

use of io.dingodb.raft.conf.Configuration in project dingo by dingodb.

the class CliServiceImpl method processLearnersOpResponse.

private Status processLearnersOpResponse(final String groupId, final Message result, final String fmt, final Object... formatArgs) {
    if (result instanceof CliRequests.LearnersOpResponse) {
        final CliRequests.LearnersOpResponse resp = (CliRequests.LearnersOpResponse) result;
        final Configuration oldConf = new Configuration();
        for (final String peerIdStr : resp.getOldLearnersList()) {
            final PeerId oldPeer = new PeerId();
            oldPeer.parse(peerIdStr);
            oldConf.addLearner(oldPeer);
        }
        final Configuration newConf = new Configuration();
        for (final String peerIdStr : resp.getNewLearnersList()) {
            final PeerId newPeer = new PeerId();
            newPeer.parse(peerIdStr);
            newConf.addLearner(newPeer);
        }
        LOG.info("Learners of replication group {} changed from {} to {} after {}.", groupId, oldConf, newConf, String.format(fmt, formatArgs));
        return Status.OK();
    } else {
        return statusFromResponse(result);
    }
}
Also used : Configuration(io.dingodb.raft.conf.Configuration) CliRequests(io.dingodb.raft.rpc.CliRequests) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

Configuration (io.dingodb.raft.conf.Configuration)31 PeerId (io.dingodb.raft.entity.PeerId)16 Status (io.dingodb.raft.Status)9 LogId (io.dingodb.raft.entity.LogId)5 ConfigurationEntry (io.dingodb.raft.conf.ConfigurationEntry)3 LogEntry (io.dingodb.raft.entity.LogEntry)3 CliRequests (io.dingodb.raft.rpc.CliRequests)3 TimeoutException (java.util.concurrent.TimeoutException)3 Message (com.google.protobuf.Message)2 RaftException (io.dingodb.raft.error.RaftException)2 NodeOptions (io.dingodb.raft.option.NodeOptions)2 RpcRequests (io.dingodb.raft.rpc.RpcRequests)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 com.lmax.disruptor (com.lmax.disruptor)1 Disruptor (com.lmax.disruptor.dsl.Disruptor)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 Closure (io.dingodb.raft.Closure)1