Search in sources :

Example 46 with Node

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

the class ItNodeTest method testVoteTimedoutStepDown.

// TODO asch Broken then using volatile log. A follower with empty log can become a leader IGNITE-14832.
@Test
@Disabled
public void testVoteTimedoutStepDown() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unittest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    // Stop all followers
    List<Node> followers = cluster.getFollowers();
    assertFalse(followers.isEmpty());
    for (Node node : followers) assertTrue(cluster.stop(node.getNodeId().getPeerId().getEndpoint()));
    // Wait leader to step down.
    while (leader.isLeader()) Thread.sleep(10);
    // old leader try to elect self, it should fail.
    ((NodeImpl) leader).tryElectSelf();
    Thread.sleep(1500);
    assertNull(cluster.getLeader());
    // Start followers
    for (Node node : followers) assertTrue(cluster.start(node.getNodeId().getPeerId().getEndpoint()));
    cluster.ensureSame();
}
Also used : Node(org.apache.ignite.raft.jraft.Node) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 47 with Node

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

the class ItNodeTest method testRollbackStateMachineWithReadIndex_Issue317.

/**
 * Test rollback stateMachine with readIndex for issue 317: https://github.com/sofastack/sofa-jraft/issues/317
 */
@Test
public void testRollbackStateMachineWithReadIndex_Issue317() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    PeerId peer = new PeerId(addr, 0);
    NodeOptions nodeOptions = createNodeOptions();
    CountDownLatch applyCompleteLatch = new CountDownLatch(1);
    CountDownLatch applyLatch = new CountDownLatch(1);
    CountDownLatch readIndexLatch = new CountDownLatch(1);
    AtomicInteger currentValue = new AtomicInteger(-1);
    String errorMsg = testInfo.getDisplayName();
    StateMachine fsm = new StateMachineAdapter() {

        @Override
        public void onApply(Iterator iter) {
            // Notify that the #onApply is preparing to go.
            readIndexLatch.countDown();
            // Wait for submitting a read-index request
            try {
                applyLatch.await();
            } catch (InterruptedException e) {
                fail();
            }
            int i = 0;
            while (iter.hasNext()) {
                byte[] data = iter.next().array();
                int v = Bits.getInt(data, 0);
                assertEquals(i++, v);
                currentValue.set(v);
            }
            if (i > 0) {
                // rollback
                currentValue.set(i - 1);
                iter.setErrorAndRollback(1, new Status(-1, errorMsg));
                applyCompleteLatch.countDown();
            }
        }
    };
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    RaftGroupService service = createService("unittest", peer, nodeOptions);
    Node node = service.start();
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) ;
    int n = 5;
    {
        // apply tasks
        for (int i = 0; i < n; i++) {
            byte[] b = new byte[4];
            Bits.putInt(b, 0, i);
            node.apply(new Task(ByteBuffer.wrap(b), null));
        }
    }
    AtomicInteger readIndexSuccesses = new AtomicInteger(0);
    {
        // Submit a read-index, wait for #onApply
        readIndexLatch.await();
        CountDownLatch latch = new CountDownLatch(1);
        node.readIndex(null, new ReadIndexClosure() {

            @Override
            public void run(Status status, long index, byte[] reqCtx) {
                try {
                    if (status.isOk())
                        readIndexSuccesses.incrementAndGet();
                    else {
                        assertTrue(status.getErrorMsg().contains(errorMsg) || status.getRaftError() == RaftError.ETIMEDOUT || status.getErrorMsg().contains("Invalid state for readIndex: STATE_ERROR"), "Unexpected status: " + status);
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
        // We have already submit a read-index request,
        // notify #onApply can go right now
        applyLatch.countDown();
        // The state machine is in error state, the node should step down.
        waitForCondition(() -> !node.isLeader(), 5_000);
        latch.await();
        applyCompleteLatch.await();
    }
    // No read-index request succeed.
    assertEquals(0, readIndexSuccesses.get());
    assertTrue(n - 1 >= currentValue.get());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Task(org.apache.ignite.raft.jraft.entity.Task) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StateMachine(org.apache.ignite.raft.jraft.StateMachine) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(org.apache.ignite.raft.jraft.Iterator) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 48 with Node

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

the class ItNodeTest method testChangePeersAddMultiNodes.

@Test
public void testChangePeersAddMultiNodes() 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);
    Configuration conf = new Configuration();
    for (int i = 0; i < 3; i++) {
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
        conf.addPeer(peer);
    }
    PeerId peer = new PeerId(TestUtils.getLocalAddress(), peer0.getEndpoint().getPort() + 1);
    // fail, because the peers are not started.
    SynchronizedClosure done = new SynchronizedClosure();
    leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
    assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
    // start peer1
    assertTrue(cluster.start(peer.getEndpoint()));
    // still fail, because peer2 is not started
    done.reset();
    leader.changePeers(conf, done);
    assertEquals(RaftError.ECATCHUP, done.await().getRaftError());
    // start peer2
    peer = new PeerId(TestUtils.getLocalAddress(), peer0.getEndpoint().getPort() + 2);
    assertTrue(cluster.start(peer.getEndpoint()));
    done.reset();
    // works
    leader.changePeers(conf, done);
    Status await = done.await();
    assertTrue(await.isOk(), await.getErrorMsg());
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    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 49 with Node

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

the class ItNodeTest method testTransferShouldWorkAfterInstallSnapshot.

@Test
public void testTransferShouldWorkAfterInstallSnapshot() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
    for (int i = 0; i < peers.size() - 1; i++) assertTrue(cluster.start(peers.get(i).getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    sendTestTaskAndWait(leader);
    List<Node> followers = cluster.getFollowers();
    assertEquals(1, followers.size());
    PeerId follower = followers.get(0).getNodeId().getPeerId();
    assertTrue(leader.transferLeadershipTo(follower).isOk());
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertEquals(follower, leader.getNodeId().getPeerId());
    CountDownLatch latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    latch = new CountDownLatch(1);
    leader.snapshot(new ExpectClosure(latch));
    waitLatch(latch);
    // start the last peer which should be recover with snapshot.
    PeerId lastPeer = peers.get(2);
    assertTrue(cluster.start(lastPeer.getEndpoint()));
    Thread.sleep(5000);
    assertTrue(leader.transferLeadershipTo(lastPeer).isOk());
    Thread.sleep(2000);
    leader = cluster.getLeader();
    assertEquals(lastPeer, leader.getNodeId().getPeerId());
    assertEquals(3, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(10, fsm.getLogs().size());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 50 with Node

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

the class ItNodeTest method testRecoverFollower.

@Test
public void testRecoverFollower() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unitest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint().copy();
    assertTrue(cluster.stop(followerAddr));
    sendTestTaskAndWait(leader);
    for (int i = 10; i < 30; i++) {
        ByteBuffer data = ByteBuffer.wrap(("no cluster" + i).getBytes(UTF_8));
        Task task = new Task(data, null);
        leader.apply(task);
    }
    // wait leader to compact logs
    Thread.sleep(5000);
    // restart follower
    assertTrue(cluster.start(followerAddr));
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(30, fsm.getLogs().size());
}
Also used : Task(org.apache.ignite.raft.jraft.entity.Task) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Node(org.apache.ignite.raft.jraft.Node) ByteBuffer(java.nio.ByteBuffer) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Aggregations

Node (org.apache.ignite.raft.jraft.Node)84 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)75 Test (org.junit.jupiter.api.Test)66 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)38 CountDownLatch (java.util.concurrent.CountDownLatch)30 ArrayList (java.util.ArrayList)26 Status (org.apache.ignite.raft.jraft.Status)23 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)23 Task (org.apache.ignite.raft.jraft.entity.Task)18 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)17 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)16 ByteBuffer (java.nio.ByteBuffer)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)12 RaftException (org.apache.ignite.raft.jraft.error.RaftException)11 LinkedHashSet (java.util.LinkedHashSet)10 ReadIndexClosure (org.apache.ignite.raft.jraft.closure.ReadIndexClosure)10 List (java.util.List)9 LogIndexOutOfBoundsException (org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException)9 LogNotFoundException (org.apache.ignite.raft.jraft.error.LogNotFoundException)9