Search in sources :

Example 26 with PeerId

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

the class NodeTest method testLeaderTransferBeforeLogIsCompleted.

@Test
public void testLeaderTransferBeforeLogIsCompleted() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 300);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), false, 1));
    }
    cluster.waitLeader();
    Node leader = cluster.getLeader();
    assertNotNull(leader);
    Thread.sleep(100);
    final List<Node> followers = cluster.getFollowers();
    assertEquals(2, followers.size());
    final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
    assertTrue(cluster.stop(targetPeer.getEndpoint()));
    this.sendTestTaskAndWait(leader);
    LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
    assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
    final CountDownLatch latch = new CountDownLatch(1);
    final Task task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(RaftError.EBUSY, latch));
    leader.apply(task);
    waitLatch(latch);
    assertTrue(cluster.start(targetPeer.getEndpoint()));
    Thread.sleep(5000);
    cluster.waitLeader();
    leader = cluster.getLeader();
    Assert.assertEquals(targetPeer, leader.getNodeId().getPeerId());
    assertTrue(cluster.ensureSame(5));
    cluster.stopAll();
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 27 with PeerId

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

the class NodeTest method testInstallLargeSnapshotWithThrottle.

@Test
public void testInstallLargeSnapshotWithThrottle() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(4);
    final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers.subList(0, 3));
    for (int i = 0; i < peers.size() - 1; i++) {
        final PeerId peer = peers.get(i);
        final boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
        assertTrue(started);
    }
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(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
    final PeerId newPeer = peers.get(3);
    final SnapshotThrottle snapshotThrottle = new ThroughputSnapshotThrottle(128, 1);
    final boolean started = cluster.start(newPeer.getEndpoint(), true, 300, false, snapshotThrottle);
    assertTrue(started);
    final CountDownLatch latch = new CountDownLatch(1);
    leader.addPeer(newPeer, status -> {
        assertTrue(status.toString(), status.isOk());
        latch.countDown();
    });
    waitLatch(latch);
    cluster.ensureSame();
    assertEquals(4, cluster.getFsms().size());
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(2000, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) Node(com.alipay.sofa.jraft.Node) SnapshotThrottle(com.alipay.sofa.jraft.storage.SnapshotThrottle) ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 28 with PeerId

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

the class NodeTest method testNodeTaskOverload.

@Test
public void testNodeTaskOverload() 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 RaftOptions raftOptions = new RaftOptions();
    raftOptions.setDisruptorBufferSize(2);
    nodeOptions.setRaftOptions(raftOptions);
    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()) {
        ;
    }
    final List<Task> tasks = new ArrayList<>();
    final AtomicInteger c = new AtomicInteger(0);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new JoinableClosure(status -> {
            System.out.println(status);
            if (!status.isOk()) {
                assertTrue(status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
            }
            c.incrementAndGet();
        }));
        node.apply(task);
        tasks.add(task);
    }
    try {
        Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
        assertEquals(10, c.get());
    } finally {
        node.shutdown();
        node.join();
    }
}
Also used : Arrays(java.util.Arrays) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Assert.assertNotSame(org.junit.Assert.assertNotSame) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) Future(java.util.concurrent.Future) Vector(java.util.Vector) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) TestUtils(com.alipay.sofa.jraft.test.TestUtils) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Assert.fail(org.junit.Assert.fail) AfterClass(org.junit.AfterClass) PeerId(com.alipay.sofa.jraft.entity.PeerId) Configuration(com.alipay.sofa.jraft.conf.Configuration) StateMachine(com.alipay.sofa.jraft.StateMachine) SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) BootstrapOptions(com.alipay.sofa.jraft.option.BootstrapOptions) Set(java.util.Set) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) SnapshotThrottle(com.alipay.sofa.jraft.storage.SnapshotThrottle) JRaftUtils(com.alipay.sofa.jraft.JRaftUtils) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Assert.assertFalse(org.junit.Assert.assertFalse) RaftException(com.alipay.sofa.jraft.error.RaftException) LogIndexOutOfBoundsException(com.alipay.sofa.jraft.error.LogIndexOutOfBoundsException) ThroughputSnapshotThrottle(com.alipay.sofa.jraft.storage.snapshot.ThroughputSnapshotThrottle) UserLog(com.alipay.sofa.jraft.entity.UserLog) BeforeClass(org.junit.BeforeClass) JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure) Bits(com.alipay.sofa.jraft.util.Bits) LogNotFoundException(com.alipay.sofa.jraft.error.LogNotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Utils(com.alipay.sofa.jraft.util.Utils) EnumOutter(com.alipay.sofa.jraft.entity.EnumOutter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) SizeUnit(org.rocksdb.util.SizeUnit) StorageOptionsFactory(com.alipay.sofa.jraft.util.StorageOptionsFactory) NodeManager(com.alipay.sofa.jraft.NodeManager) TestName(org.junit.rules.TestName) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) RaftError(com.alipay.sofa.jraft.error.RaftError) TaskClosure(com.alipay.sofa.jraft.closure.TaskClosure) Iterator(com.alipay.sofa.jraft.Iterator) LinkedHashSet(java.util.LinkedHashSet) Before(org.junit.Before) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) RaftRpcServerFactory(com.alipay.sofa.jraft.rpc.RaftRpcServerFactory) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Task(com.alipay.sofa.jraft.entity.Task) Assert.assertNull(org.junit.Assert.assertNull) Node(com.alipay.sofa.jraft.Node) ConsoleReporter(com.codahale.metrics.ConsoleReporter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Assert(org.junit.Assert) RocksDBLogStorage(com.alipay.sofa.jraft.storage.impl.RocksDBLogStorage) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) Node(com.alipay.sofa.jraft.Node) ArrayList(java.util.ArrayList) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint) JoinableClosure(com.alipay.sofa.jraft.closure.JoinableClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 29 with PeerId

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

the class NodeTest method testReadIndexFromLearner.

@Test
public void testReadIndexFromLearner() throws Exception {
    final List<PeerId> peers = TestUtils.generatePeers(3);
    final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
    for (final PeerId peer : peers) {
        assertTrue(cluster.start(peer.getEndpoint(), false, 300, true));
    }
    // elect leader
    cluster.waitLeader();
    // get leader
    final Node leader = cluster.getLeader();
    assertNotNull(leader);
    assertEquals(3, leader.listPeers().size());
    // apply tasks to leader
    this.sendTestTaskAndWait(leader);
    {
        // Adds a learner
        SynchronizedClosure done = new SynchronizedClosure();
        PeerId learnerPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
        // Start learner
        assertTrue(cluster.startLearner(learnerPeer));
        leader.addLearners(Arrays.asList(learnerPeer), done);
        assertTrue(done.await().isOk());
        assertEquals(1, leader.listAliveLearners().size());
        assertEquals(1, leader.listLearners().size());
    }
    Thread.sleep(100);
    // read from learner
    Node learner = cluster.getNodes().get(3);
    assertNotNull(leader);
    assertReadIndex(learner, 12);
    assertReadIndex(learner, 12);
    cluster.stopAll();
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) Node(com.alipay.sofa.jraft.Node) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 30 with PeerId

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

the class NodeTest method testReadIndexChaos.

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

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

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

                    @Override
                    public void run(final Status status, final long index, final byte[] reqCtx) {
                        if (status.isOk()) {
                            assertTrue(status.toString(), status.isOk());
                            assertTrue(index > 0);
                            assertArrayEquals(requestContext, reqCtx);
                        }
                        readLatch.countDown();
                    }
                });
                try {
                    readLatch.await();
                } catch (final InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }.start();
    }
    latch.await();
    cluster.ensureSame();
    for (final MockStateMachine fsm : cluster.getFsms()) {
        assertEquals(10000, fsm.getLogs().size());
    }
    cluster.stopAll();
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Node(com.alipay.sofa.jraft.Node) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) 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