Search in sources :

Example 1 with RaftError

use of com.alipay.sofa.jraft.error.RaftError in project sofa-jraft by sofastack.

the class NodeTest method startChangePeersThread.

private Future<?> startChangePeersThread(final ChangeArg arg) {
    final Set<RaftError> expectedErrors = new HashSet<>();
    expectedErrors.add(RaftError.EBUSY);
    expectedErrors.add(RaftError.EPERM);
    expectedErrors.add(RaftError.ECATCHUP);
    return Utils.runInThread(() -> {
        try {
            while (!arg.stop) {
                arg.c.waitLeader();
                final Node leader = arg.c.getLeader();
                if (leader == null) {
                    continue;
                }
                // select peers in random
                final Configuration conf = new Configuration();
                if (arg.dontRemoveFirstPeer) {
                    conf.addPeer(arg.peers.get(0));
                }
                for (int i = 0; i < arg.peers.size(); i++) {
                    final boolean select = ThreadLocalRandom.current().nextInt(64) < 32;
                    if (select && !conf.contains(arg.peers.get(i))) {
                        conf.addPeer(arg.peers.get(i));
                    }
                }
                if (conf.isEmpty()) {
                    LOG.warn("No peer has been selected");
                    continue;
                }
                final SynchronizedClosure done = new SynchronizedClosure();
                leader.changePeers(conf, done);
                done.await();
                assertTrue(done.getStatus().toString(), done.getStatus().isOk() || expectedErrors.contains(done.getStatus().getRaftError()));
            }
        } catch (final InterruptedException e) {
            LOG.error("ChangePeersThread is interrupted", e);
        }
    });
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftError(com.alipay.sofa.jraft.error.RaftError) Node(com.alipay.sofa.jraft.Node) Endpoint(com.alipay.sofa.jraft.util.Endpoint) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Node (com.alipay.sofa.jraft.Node)1 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 RaftError (com.alipay.sofa.jraft.error.RaftError)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1