Search in sources :

Example 21 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration in project ignite-3 by apache.

the class ItNodeTest method testChangePeers.

@Test
public void testChangePeers() throws Exception {
    PeerId peer0 = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    cluster = new TestCluster("testChangePeers", dataPath, Collections.singletonList(peer0), testInfo);
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    sendTestTaskAndWait(leader);
    for (int i = 1; i < 10; i++) {
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
        assertTrue(cluster.start(peer.getEndpoint(), false, 300));
    }
    for (int i = 0; i < 9; i++) {
        cluster.waitLeader();
        leader = cluster.getLeader();
        assertNotNull(leader);
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), peer0.getEndpoint().getPort() + i);
        assertEquals(peer, leader.getNodeId().getPeerId());
        peer = new PeerId(TestUtils.getLocalAddress(), peer0.getEndpoint().getPort() + i + 1);
        SynchronizedClosure done = new SynchronizedClosure();
        leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
        Status status = done.await();
        assertTrue(status.isOk(), status.getRaftError().toString());
    }
    cluster.waitLeader();
    for (MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(10, fsm.getLogs().size());
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 22 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration in project ignite-3 by apache.

the class ItNodeTest method testChangePeersStepsDownInJointConsensus.

@Test
public void testChangePeersStepsDownInJointConsensus() throws Exception {
    List<PeerId> peers = new ArrayList<>();
    PeerId peer0 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5006");
    PeerId peer1 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5007");
    PeerId peer2 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5008");
    PeerId peer3 = JRaftUtils.getPeerId(TestUtils.getLocalAddress() + ":5009");
    // start single cluster
    peers.add(peer0);
    cluster = new TestCluster("testChangePeersStepsDownInJointConsensus", dataPath, peers, testInfo);
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    sendTestTaskAndWait(leader);
    // start peer1-3
    assertTrue(cluster.start(peer1.getEndpoint()));
    assertTrue(cluster.start(peer2.getEndpoint()));
    assertTrue(cluster.start(peer3.getEndpoint()));
    // Make sure the topology is ready before adding peers.
    assertTrue(waitForTopology(cluster, leader.getNodeId().getPeerId().getEndpoint(), 4, 3_000));
    Configuration conf = new Configuration();
    conf.addPeer(peer0);
    conf.addPeer(peer1);
    conf.addPeer(peer2);
    conf.addPeer(peer3);
    // change peers
    SynchronizedClosure done = new SynchronizedClosure();
    leader.changePeers(conf, done);
    assertTrue(done.await().isOk());
    // stop peer3
    assertTrue(cluster.stop(peer3.getEndpoint()));
    conf.removePeer(peer0);
    conf.removePeer(peer1);
    // Change peers to [peer2, peer3], which must fail since peer3 is stopped
    done.reset();
    leader.changePeers(conf, done);
    assertEquals(RaftError.EPERM, done.await().getRaftError());
    LOG.info(done.getStatus().toString());
    assertFalse(((NodeImpl) leader).getConf().isStable());
    leader = cluster.getLeader();
    assertNull(leader);
    assertTrue(cluster.start(peer3.getEndpoint()));
    Thread.sleep(1000);
    cluster.waitLeader();
    leader = cluster.getLeader();
    List<PeerId> thePeers = leader.listPeers();
    assertTrue(!thePeers.isEmpty());
    assertEquals(conf.getPeerSet(), new HashSet<>(thePeers));
}
Also used : SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 23 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration in project ignite-3 by apache.

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(org.apache.ignite.raft.jraft.conf.Configuration) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 24 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration in project ignite-3 by apache.

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(org.apache.ignite.raft.jraft.conf.Configuration)

Example 25 with Configuration

use of org.apache.ignite.raft.jraft.conf.Configuration in project ignite-3 by apache.

the class NodeImpl method resetPeers.

@Override
public Status resetPeers(final Configuration newPeers) {
    Requires.requireNonNull(newPeers, "Null new peers");
    Requires.requireTrue(!newPeers.isEmpty(), "Empty new peers");
    Requires.requireTrue(newPeers.isValid(), "Invalid new peers: %s", newPeers);
    this.writeLock.lock();
    try {
        if (newPeers.isEmpty()) {
            LOG.warn("Node {} set empty peers.", getNodeId());
            return new Status(RaftError.EINVAL, "newPeers is empty");
        }
        if (!this.state.isActive()) {
            LOG.warn("Node {} is in state {}, can't set peers.", getNodeId(), this.state);
            return new Status(RaftError.EPERM, "Bad state: %s", this.state);
        }
        // bootstrap?
        if (this.conf.getConf().isEmpty()) {
            LOG.info("Node {} set peers to {} from empty.", getNodeId(), newPeers);
            this.conf.setConf(newPeers);
            stepDown(this.currTerm + 1, false, new Status(RaftError.ESETPEER, "Set peer from empty configuration"));
            return Status.OK();
        }
        if (this.state == State.STATE_LEADER && this.confCtx.isBusy()) {
            LOG.warn("Node {} set peers need wait current conf changing.", getNodeId());
            return new Status(RaftError.EBUSY, "Changing to another configuration");
        }
        // check equal, maybe retry direct return
        if (this.conf.getConf().equals(newPeers)) {
            return Status.OK();
        }
        final Configuration newConf = new Configuration(newPeers);
        LOG.info("Node {} set peers from {} to {}.", getNodeId(), this.conf.getConf(), newPeers);
        this.conf.setConf(newConf);
        this.conf.getOldConf().reset();
        stepDown(this.currTerm + 1, false, new Status(RaftError.ESETPEER, "Raft node set peer normally"));
        return Status.OK();
    } finally {
        this.writeLock.unlock();
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Configuration(org.apache.ignite.raft.jraft.conf.Configuration)

Aggregations

Configuration (org.apache.ignite.raft.jraft.conf.Configuration)45 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)37 Test (org.junit.jupiter.api.Test)20 Node (org.apache.ignite.raft.jraft.Node)17 Status (org.apache.ignite.raft.jraft.Status)16 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)13 ArrayList (java.util.ArrayList)9 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)9 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)9 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)8 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutorService (java.util.concurrent.ExecutorService)5 NetworkAddress (org.apache.ignite.network.NetworkAddress)5 ConfigurationEntry (org.apache.ignite.raft.jraft.conf.ConfigurationEntry)5 LogId (org.apache.ignite.raft.jraft.entity.LogId)5 Task (org.apache.ignite.raft.jraft.entity.Task)5 IgniteRpcClient (org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient)5 HashMap (java.util.HashMap)4 ClusterService (org.apache.ignite.network.ClusterService)4 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)4