Search in sources :

Example 26 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class NodeImpl method addPeer.

@Override
public void addPeer(final PeerId peer, final Closure done) {
    Requires.requireNonNull(peer, "Null peer");
    this.writeLock.lock();
    try {
        Requires.requireTrue(!this.conf.getConf().contains(peer), "Peer already exists in current configuration");
        final Configuration newConf = new Configuration(this.conf.getConf());
        newConf.addPeer(peer);
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 27 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class NodeImpl method removePeer.

@Override
public void removePeer(final PeerId peer, final Closure done) {
    Requires.requireNonNull(peer, "Null peer");
    this.writeLock.lock();
    try {
        Requires.requireTrue(this.conf.getConf().contains(peer), "Peer not found in current configuration");
        final Configuration newConf = new Configuration(this.conf.getConf());
        newConf.removePeer(peer);
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration)

Example 28 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class NodeImpl method checkDeadNodes.

private boolean checkDeadNodes(final Configuration conf, final long monotonicNowMs, final boolean stepDownOnCheckFail) {
    // Check learner replicators at first.
    for (final PeerId peer : conf.getLearners()) {
        checkReplicator(peer);
    }
    // Ensure quorum nodes alive.
    final List<PeerId> peers = conf.listPeers();
    final Configuration deadNodes = new Configuration();
    if (checkDeadNodes0(peers, monotonicNowMs, true, deadNodes)) {
        return true;
    }
    if (stepDownOnCheckFail) {
        LOG.warn("Node {} steps down when alive nodes don't satisfy quorum, term={}, deadNodes={}, conf={}.", getNodeId(), this.currTerm, deadNodes, conf);
        final Status status = new Status();
        status.setError(RaftError.ERAFTTIMEDOUT, "Majority of the group dies: %d/%d", deadNodes.size(), peers.size());
        stepDown(this.currTerm, false, status);
    }
    return false;
}
Also used : Status(com.alipay.sofa.jraft.Status) Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 29 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class NodeImpl method addLearners.

@Override
public void addLearners(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.addLearner(peer);
        }
        unsafeRegisterConfChange(this.conf.getConf(), newConf, done);
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 30 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

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 : LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Closure(com.alipay.sofa.jraft.Closure) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3