Search in sources :

Example 46 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testSetPeer2.

@Test
public void testSetPeer2() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId followerPeer1 = followers.get(0).getNodeId().getPeerId();
    final Endpoint followerAddr1 = followerPeer1.getEndpoint();
    final PeerId followerPeer2 = followers.get(1).getNodeId().getPeerId();
    final Endpoint followerAddr2 = followerPeer2.getEndpoint();
    LOG.info("Stop and clean follower {}", followerPeer1);
    assertTrue(cluster.stop(followerAddr1));
    cluster.clean(followerAddr1);
    // apply tasks to leader again
    this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
    // set peer when no quorum die
    final Endpoint leaderAddr = leader.getLeaderId().getEndpoint().copy();
    LOG.info("Set peers to {}", leaderAddr);
    final List<PeerId> newPeers = TestUtils.generatePeers(3);
    assertTrue(newPeers.remove(followerPeer1));
    LOG.info("Stop and clean follower {}", followerPeer2);
    assertTrue(cluster.stop(followerAddr2));
    cluster.clean(followerAddr2);
    // leader will step-down, become follower
    Thread.sleep(2000);
    newPeers.clear();
    newPeers.add(new PeerId(leaderAddr, 0));
    // new peers equal to current conf
    assertTrue(leader.resetPeers(new Configuration(peers)).isOk());
    // set peer when quorum die
    LOG.warn("Set peers to {}", leaderAddr);
    assertTrue(leader.resetPeers(new Configuration(newPeers)).isOk());
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertNotNull(leader);
    Assert.assertEquals(leaderAddr, leader.getNodeId().getPeerId().getEndpoint());
    LOG.info("start follower {}", followerAddr1);
    assertTrue(cluster.start(followerAddr1, true, 300));
    LOG.info("start follower {}", followerAddr2);
    assertTrue(cluster.start(followerAddr2, true, 300));
    CountDownLatch latch = new CountDownLatch(1);
    LOG.info("Add old follower {}", followerAddr1);
    leader.addPeer(followerPeer1, new ExpectClosure(latch));
    waitLatch(latch);
    latch = new CountDownLatch(1);
    LOG.info("Add old follower {}", followerAddr2);
    leader.addPeer(followerPeer2, new ExpectClosure(latch));
    waitLatch(latch);
    newPeers.add(followerPeer1);
    newPeers.add(followerPeer2);
    cluster.ensureSame();
    assertEquals(3, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(20, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 47 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testTripleNodesV1V2Codec.

@Test
public void testTripleNodesV1V2Codec() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (int i = 0; i < peers.size(); i++) {
        // Peer3 use codec v1
        if (i == 2) {
            cluster.setRaftServiceFactory(new V1JRaftServiceFactory());
        }
        assertTrue(cluster.start(peers.get(i).getEndpoint()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    {
        final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes());
        final Task task = new Task(data, null);
        leader.apply(task);
    }
    {
        // task with TaskClosure
        final ByteBuffer data = ByteBuffer.wrap("task closure".getBytes());
        final Vector<String> cbs = new Vector<>();
        final CountDownLatch latch = new CountDownLatch(1);
        final Task task = new Task(data, new TaskClosure() {

            @Override
            public void run(final Status status) {
                cbs.add("apply");
                latch.countDown();
            }

            @Override
            public void onCommitted() {
                cbs.add("commit");
            }
        });
        leader.apply(task);
        latch.await();
        assertEquals(2, cbs.size());
        assertEquals("commit", cbs.get(0));
        assertEquals("apply", cbs.get(1));
    }
    cluster.ensureSame(-1);
    assertEquals(2, cluster.getFollowers().size());
    // transfer the leader to v1 codec peer
    assertTrue(leader.transferLeadershipTo(peers.get(2)).isOk());
    cluster.waitLeader();
    leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(leader.getLeaderId(), peers.get(2));
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    cluster.ensureSame();
    cluster.stopAll();
    // start the cluster with v2 codec, should work
    final TestCluster newCluster = new TestCluster("unittest", this.dataPath, peers);
    for (int i = 0; i < peers.size(); i++) {
        assertTrue(newCluster.start(peers.get(i).getEndpoint()));
    }
    // elect leader
    newCluster.waitLeader();
    newCluster.ensureSame();
    leader = newCluster.getLeader();
    assertNotNull(leader);
    // apply new tasks
    this.sendTestTaskAndWait(leader);
    newCluster.ensureSame();
    newCluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Vector(java.util.Vector) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 48 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testNoSnapshot.

@Test
public void testNoSnapshot() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));
    // wait node elect self as leader
    Thread.sleep(2000);
    this.sendTestTaskAndWait(node);
    assertEquals(0, fsm.getSaveSnapshotTimes());
    // do snapshot but returns error
    CountDownLatch latch = new CountDownLatch(1);
    node.snapshot(new ExpectClosure(RaftError.EINVAL, "Snapshot is not supported", latch));
    waitLatch(latch);
    assertEquals(0, fsm.getSaveSnapshotTimes());
    latch = new CountDownLatch(1);
    node.shutdown(new ExpectClosure(latch));
    node.join();
    waitLatch(latch);
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 49 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testRemoveLeaderWithPriority.

@Test
public void testRemoveLeaderWithPriority() throws Exception {
    final List<Integer> priorities = new ArrayList<Integer>();
    priorities.add(100);
    priorities.add(60);
    priorities.add(10);
    final List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(100, leader.getNodeTargetPriority());
    assertEquals(100, leader.getNodeId().getPeerId().getPriority());
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId oldLeader = leader.getNodeId().getPeerId().copy();
    final Endpoint oldLeaderAddr = oldLeader.getEndpoint();
    // remove old leader
    LOG.info("Remove old leader {}", oldLeader);
    CountDownLatch latch = new CountDownLatch(1);
    leader.removePeer(oldLeader, new ExpectClosure(latch));
    waitLatch(latch);
    assertEquals(60, leader.getNodeTargetPriority());
    // stop and clean old leader
    LOG.info("Stop and clean old leader {}", oldLeader);
    assertTrue(cluster.stop(oldLeaderAddr));
    cluster.clean(oldLeaderAddr);
    // elect new leader
    cluster.waitLeader();
    leader = cluster.getLeader();
    LOG.info("New leader is {}", leader);
    assertNotNull(leader);
    assertNotSame(leader, oldLeader);
    cluster.stopAll();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 50 with PeerId

use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.

the class NodeTest method testSingleNodeWithLearner.

@Test
public void testSingleNodeWithLearner() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    final Endpoint learnerAddr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
    final PeerId learnerPeer = new PeerId(learnerAddr, 0);
    NodeManager.getInstance().addAddress(addr);
    NodeManager.getInstance().addAddress(learnerAddr);
    MockStateMachine learnerFsm = null;
    Node learner = null;
    RaftGroupService learnerServer = null;
    {
        // Start learner
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        learnerFsm = new MockStateMachine(learnerAddr);
        nodeOptions.setFsm(learnerFsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log1");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta1");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot1");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(learnerAddr);
        learnerServer = new RaftGroupService("unittest", new PeerId(learnerAddr, 0), nodeOptions, rpcServer);
        learner = learnerServer.start();
    }
    {
        // Start leader
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        final MockStateMachine fsm = new MockStateMachine(addr);
        nodeOptions.setFsm(fsm);
        nodeOptions.setLogUri(this.dataPath + File.separator + "log");
        nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
        nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
        nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
        final Node node = new NodeImpl("unittest", peer);
        assertTrue(node.init(nodeOptions));
        assertEquals(1, node.listPeers().size());
        assertTrue(node.listPeers().contains(peer));
        while (!node.isLeader()) {
            ;
        }
        sendTestTaskAndWait(node);
        assertEquals(10, fsm.getLogs().size());
        int i = 0;
        for (final ByteBuffer data : fsm.getLogs()) {
            assertEquals("hello" + i++, new String(data.array()));
        }
        // wait for entries to be replicated to learner.
        Thread.sleep(1000);
        node.shutdown();
        node.join();
    }
    {
        // assert learner fsm
        assertEquals(10, learnerFsm.getLogs().size());
        int i = 0;
        for (final ByteBuffer data : learnerFsm.getLogs()) {
            assertEquals("hello" + i++, new String(data.array()));
        }
        learnerServer.shutdown();
        learnerServer.join();
    }
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

PeerId (com.alipay.sofa.jraft.entity.PeerId)236 Test (org.junit.Test)107 Node (com.alipay.sofa.jraft.Node)70 Configuration (com.alipay.sofa.jraft.conf.Configuration)54 Status (com.alipay.sofa.jraft.Status)49 Endpoint (com.alipay.sofa.jraft.util.Endpoint)43 ArrayList (java.util.ArrayList)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)24 LogId (com.alipay.sofa.jraft.entity.LogId)17 Message (com.google.protobuf.Message)15 ByteBuffer (java.nio.ByteBuffer)15 Task (com.alipay.sofa.jraft.entity.Task)13 File (java.io.File)12 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)11 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)11 JRaftException (com.alipay.sofa.jraft.error.JRaftException)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)9 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)8