Search in sources :

Example 16 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration 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 17 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration 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)

Example 18 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration 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 19 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration 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)

Example 20 with Configuration

use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.

the class AtomicRangeGroup method start.

public static AtomicRangeGroup start(StartupConf conf, RpcServer rpcServer) throws IOException {
    final NodeOptions nodeOptions = new NodeOptions();
    // Set election timeout to 1 second
    nodeOptions.setElectionTimeoutMs(1000);
    // Close cli service
    nodeOptions.setDisableCli(false);
    // A snapshot saving would be triggered every 30 seconds
    // nodeOptions.setSnapshotIntervalSecs(30);
    // Parsing Options
    final PeerId serverId = new PeerId();
    if (!serverId.parse(conf.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
    }
    final Configuration initConf = new Configuration();
    if (!initConf.parse(conf.getConf())) {
        throw new IllegalArgumentException("Fail to parse initConf:" + conf.getConf());
    }
    // Set the initial cluster configuration
    nodeOptions.setInitialConf(initConf);
    // Startup node
    final AtomicRangeGroup node = new AtomicRangeGroup(conf.getDataPath(), conf.getGroupId(), serverId, conf.getMinSlot(), conf.getMaxSlot(), nodeOptions, rpcServer);
    LOG.info("Started range node[{}-{}] at port:{}", conf.getMinSlot(), conf.getMaxSlot(), node.getNode().getNodeId().getPeerId().getPort());
    return node;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Configuration (com.alipay.sofa.jraft.conf.Configuration)81 PeerId (com.alipay.sofa.jraft.entity.PeerId)54 Test (org.junit.Test)28 Node (com.alipay.sofa.jraft.Node)20 Endpoint (com.alipay.sofa.jraft.util.Endpoint)20 Status (com.alipay.sofa.jraft.Status)18 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)8 File (java.io.File)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 ConfigurationEntry (com.alipay.sofa.jraft.conf.ConfigurationEntry)5 Task (com.alipay.sofa.jraft.entity.Task)5 CliOptions (com.alipay.sofa.jraft.option.CliOptions)5 LogId (com.alipay.sofa.jraft.entity.LogId)4 RheaKVStore (com.alipay.sofa.jraft.rhea.client.RheaKVStore)4 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3