Search in sources :

Example 11 with Configuration

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

the class NodeTest method testSingleNode.

@Test
public void testSingleNode() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    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.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    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()));
    }
    node.shutdown();
    node.join();
}
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) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 12 with Configuration

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

the class NodeTest method testChangePeersChaosWithSnapshot.

@Test
public void testChangePeersChaosWithSnapshot() throws Exception {
    // start cluster
    final List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 2));
    // start other peers
    for (int i = 1; i < 10; i++) {
        final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint()));
    }
    final ChangeArg arg = new ChangeArg(cluster, peers, false, false);
    final Future<?> future = startChangePeersThread(arg);
    for (int i = 0; i < 5000; ) {
        cluster.waitLeader();
        final Node leader = cluster.getLeader();
        if (leader == null) {
            continue;
        }
        final SynchronizedClosure done = new SynchronizedClosure();
        final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
        leader.apply(task);
        final Status status = done.await();
        if (status.isOk()) {
            if (++i % 100 == 0) {
                System.out.println("Progress:" + i);
            }
        } else {
            assertEquals(RaftError.EPERM, status.getRaftError());
        }
    }
    arg.stop = true;
    future.get();
    cluster.waitLeader();
    final SynchronizedClosure done = new SynchronizedClosure();
    final Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    final Status st = done.await();
    assertTrue(st.getErrorMsg(), st.isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertTrue(fsm.getLogs().size() >= 5000);
    }
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 13 with Configuration

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

the class NodeTest method testChangePeersChaosApplyTasks.

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

Example 14 with Configuration

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

the class NodeTest method testChangePeersChaosWithoutSnapshot.

@Test
public void testChangePeersChaosWithoutSnapshot() throws Exception {
    // start cluster
    final List<PeerId> peers = new ArrayList<>();
    peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
    assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
    // start other peers
    for (int i = 1; i < 10; i++) {
        final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
        peers.add(peer);
        assertTrue(cluster.start(peer.getEndpoint(), true, 10000));
    }
    final ChangeArg arg = new ChangeArg(cluster, peers, false, true);
    final Future<?> future = startChangePeersThread(arg);
    final int tasks = 5000;
    for (int i = 0; i < tasks; ) {
        cluster.waitLeader();
        final Node leader = cluster.getLeader();
        if (leader == null) {
            continue;
        }
        final SynchronizedClosure done = new SynchronizedClosure();
        final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
        leader.apply(task);
        final Status status = done.await();
        if (status.isOk()) {
            if (++i % 100 == 0) {
                System.out.println("Progress:" + i);
            }
        } else {
            assertEquals(RaftError.EPERM, status.getRaftError());
        }
    }
    arg.stop = true;
    future.get();
    cluster.waitLeader();
    final SynchronizedClosure done = new SynchronizedClosure();
    final Node leader = cluster.getLeader();
    leader.changePeers(new Configuration(peers), done);
    assertTrue(done.await().isOk());
    cluster.ensureSame();
    assertEquals(10, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertTrue(fsm.getLogs().size() >= tasks);
        assertTrue(fsm.getLogs().size() - tasks < 100);
    }
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 15 with Configuration

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

the class NodeTest method testChangePeersStepsDownInJointConsensus.

@Test
public void testChangePeersStepsDownInJointConsensus() throws Exception {
    final List<PeerId> peers = new ArrayList<>();
    final PeerId peer0 = JRaftUtils.getPeerId("127.0.0.1:5006");
    final PeerId peer1 = JRaftUtils.getPeerId("127.0.0.1:5007");
    final PeerId peer2 = JRaftUtils.getPeerId("127.0.0.1:5008");
    final PeerId peer3 = JRaftUtils.getPeerId("127.0.0.1:5009");
    // start single cluster
    peers.add(peer0);
    final TestCluster cluster = new TestCluster("testChangePeersStepsDownInJointConsensus", this.dataPath, peers);
    assertTrue(cluster.start(peer0.getEndpoint()));
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    this.sendTestTaskAndWait(leader);
    // start peer1-3
    assertTrue(cluster.start(peer1.getEndpoint()));
    assertTrue(cluster.start(peer2.getEndpoint()));
    assertTrue(cluster.start(peer3.getEndpoint()));
    final Configuration conf = new Configuration();
    conf.addPeer(peer0);
    conf.addPeer(peer1);
    conf.addPeer(peer2);
    conf.addPeer(peer3);
    // change peers
    final 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);
    Assert.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();
    final List<PeerId> thePeers = leader.listPeers();
    assertTrue(thePeers.size() > 0);
    assertEquals(conf.getPeerSet(), new HashSet<>(thePeers));
    cluster.stopAll();
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

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