Search in sources :

Example 56 with Node

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

the class ItNodeTest method testReadIndexChaos.

@Test
public void testReadIndexChaos() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(3);
    cluster = new TestCluster("unittest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), false, 300, true));
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    CountDownLatch latch = new CountDownLatch(10);
    ExecutorService executor = Executors.newFixedThreadPool(10);
    executors.add(executor);
    for (int i = 0; i < 10; i++) {
        executor.submit(new Runnable() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 100; i++) {
                        try {
                            sendTestTaskAndWait(leader);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                        readIndexRandom(cluster);
                    }
                } finally {
                    latch.countDown();
                }
            }

            private void readIndexRandom(TestCluster cluster) {
                CountDownLatch readLatch = new CountDownLatch(1);
                byte[] requestContext = TestUtils.getRandomBytes();
                cluster.getNodes().get(ThreadLocalRandom.current().nextInt(3)).readIndex(requestContext, new ReadIndexClosure() {

                    @Override
                    public void run(Status status, long index, byte[] reqCtx) {
                        if (status.isOk()) {
                            assertTrue(status.isOk(), status.toString());
                            assertTrue(index > 0);
                            assertArrayEquals(requestContext, reqCtx);
                        }
                        readLatch.countDown();
                    }
                });
                try {
                    readLatch.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
    }
    latch.await();
    cluster.ensureSame();
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(10000, fsm.getLogs().size());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Node(org.apache.ignite.raft.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) ExecutorService(java.util.concurrent.ExecutorService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 57 with Node

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

the class ItNodeTest method testInstallLargeSnapshot.

@Test
public void testInstallLargeSnapshot() throws Exception {
    List<PeerId> peers = TestUtils.generatePeers(4);
    cluster = new TestCluster("unitest", dataPath, peers.subList(0, 3), testInfo);
    for (int i = 0; i < peers.size() - 1; i++) {
        PeerId peer = peers.get(i);
        boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
        assertTrue(started);
    }
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    cluster.ensureLeader(leader);
    // apply tasks to leader
    sendTestTaskAndWait(leader, 0, RaftError.SUCCESS);
    cluster.ensureSame();
    // apply something more
    for (int i = 1; i < 100; i++) sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
    Thread.sleep(1000);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader);
    // apply something more
    for (int i = 100; i < 200; i++) sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
    // trigger leader snapshot
    triggerLeaderSnapshot(cluster, leader, 2);
    // wait leader to compact logs
    Thread.sleep(1000);
    // add follower
    PeerId newPeer = peers.get(3);
    RaftOptions raftOptions = new RaftOptions();
    raftOptions.setMaxByteCountPerRpc(128);
    boolean started = cluster.start(newPeer.getEndpoint(), false, 300, false, null, raftOptions);
    assertTrue(started);
    CountDownLatch latch = new CountDownLatch(1);
    leader.addPeer(newPeer, status -> {
        assertTrue(status.isOk(), status.toString());
        latch.countDown();
    });
    waitLatch(latch);
    cluster.ensureSame();
    assertEquals(4, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(2000, fsm.getLogs().size());
}
Also used : RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) 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 58 with Node

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

the class ItNodeTest method testChangePeersChaosApplyTasks.

@Test
public void testChangePeersChaosApplyTasks() throws Exception {
    // start cluster
    List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT));
    cluster = new TestCluster("unittest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
    // start other peers
    for (int i = 1; i < 10; i++) {
        PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint(), false, 100000));
    }
    final int threads = 3;
    List<ChangeArg> args = new ArrayList<>();
    List<Future<?>> futures = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(threads);
    ExecutorService executor = Executors.newFixedThreadPool(threads);
    executors.add(executor);
    for (int t = 0; t < threads; t++) {
        ChangeArg arg = new ChangeArg(cluster, peers, false, true);
        args.add(arg);
        futures.add(startChangePeersThread(arg));
        Utils.runInThread(executor, () -> {
            try {
                for (int i = 0; i < 5000; ) {
                    cluster.waitLeader();
                    Node leader = cluster.getLeader();
                    if (leader == null)
                        continue;
                    SynchronizedClosure done = new SynchronizedClosure();
                    Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes(UTF_8)), done);
                    leader.apply(task);
                    Status status = done.await();
                    if (status.isOk()) {
                        if (++i % 100 == 0)
                            System.out.println("Progress:" + i);
                    } else
                        assertEquals(RaftError.EPERM, status.getRaftError());
                }
            } catch (Exception e) {
                LOG.error("Failed to run tasks", e);
            } finally {
                latch.countDown();
            }
        });
    }
    latch.await();
    for (ChangeArg arg : args) arg.stop = true;
    for (Future<?> future : futures) future.get();
    cluster.waitLeader();
    SynchronizedClosure done = new SynchronizedClosure();
    Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    assertTrue(done.await().isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) {
        int logSize = fsm.getLogs().size();
        assertTrue(logSize >= 5000 * threads, "logSize= " + logSize);
        assertTrue(logSize - 5000 * threads < 100, "logSize= " + logSize);
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) Task(org.apache.ignite.raft.jraft.entity.Task) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftException(org.apache.ignite.raft.jraft.error.RaftException) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 59 with Node

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

the class ItNodeTest method testRemoveFollower.

@Test
public void testRemoveFollower() 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);
    // apply tasks to leader
    sendTestTaskAndWait(leader);
    cluster.ensureSame();
    List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
    Endpoint followerAddr = followerPeer.getEndpoint();
    // stop and clean follower
    LOG.info("Stop and clean follower {}", followerPeer);
    assertTrue(cluster.stop(followerAddr));
    cluster.clean(followerAddr);
    // remove follower
    LOG.info("Remove follower {}", followerPeer);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    followers = cluster.getFollowers();
    assertEquals(1, followers.size());
    peers = TestUtils.generatePeers(3);
    assertTrue(peers.remove(followerPeer));
    // start follower
    LOG.info("Start and add follower {}", followerPeer);
    assertTrue(cluster.start(followerAddr));
    // re-add follower
    latch = new CountDownLatch(1);
    leader.addPeer(followerPeer, new ExpectClosure(latch));
    waitLatch(latch);
    followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (MockStateMachine fsm : cluster.getFsms()) assertEquals(20, fsm.getLogs().size());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Node(org.apache.ignite.raft.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 60 with Node

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

the class ItNodeTest method testNoLeaderWithZeroValPriorityElection.

@Test
public void testNoLeaderWithZeroValPriorityElection() throws Exception {
    List<Integer> priorities = new ArrayList<>();
    priorities.add(0);
    priorities.add(0);
    priorities.add(0);
    List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
    cluster = new TestCluster("unittest", dataPath, peers, testInfo);
    for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
    Thread.sleep(200);
    List<Node> followers = cluster.getFollowers();
    assertEquals(3, followers.size());
    for (Node follower : followers) assertEquals(0, follower.getNodeId().getPeerId().getPriority());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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)

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