Java Code Examples for com.alipay.sofa.jraft.Node#shutdown()

The following examples show how to use com.alipay.sofa.jraft.Node#shutdown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitShutdown() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setFsm(new MockStateMachine(addr));
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");

    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));

    node.shutdown();
    node.join();
}
 
Example 2
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public boolean stop(final Endpoint listenAddr) throws InterruptedException {
    final Node node = removeNode(listenAddr);
    final CountDownLatch latch = new CountDownLatch(1);
    if (node != null) {
        node.shutdown(new ExpectClosure(latch));
        node.join();
        latch.await();
    }
    final RaftGroupService raftGroupService = this.serverMap.remove(listenAddr.toString());
    raftGroupService.shutdown();
    raftGroupService.join();
    return node != null;
}
 
Example 3
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 4
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoSnapshot() 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.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotIntervalSecs(10);
    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);

    sendTestTaskAndWait(node);

    // wait for auto snapshot
    Thread.sleep(10000);
    // first snapshot will be triggered randomly
    final int times = fsm.getSaveSnapshotTimes();
    assertTrue("snapshotTimes=" + times, times >= 1);
    assertTrue(fsm.getSnapshotIndex() > 0);

    final CountDownLatch latch = new CountDownLatch(1);
    node.shutdown(new ExpectClosure(latch));
    node.join();
    waitLatch(latch);
}
 
Example 5
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testShuttingDownLeaderTriggerTimeoutNow() 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()));
    }

    cluster.waitLeader();

    Node leader = cluster.getLeader();
    assertNotNull(leader);
    final Node oldLeader = leader;

    LOG.info("Shutdown leader {}", leader);
    leader.shutdown();
    leader.join();

    Thread.sleep(100);
    leader = cluster.getLeader();
    assertNotNull(leader);
    assertNotSame(leader, oldLeader);

    cluster.stopAll();
}
 
Example 6
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public boolean stop(final Endpoint listenAddr) throws InterruptedException {
    final Node node = removeNode(listenAddr);
    final CountDownLatch latch = new CountDownLatch(1);
    if (node != null) {
        node.shutdown(new ExpectClosure(latch));
        node.join();
        latch.await();
    }
    final RaftGroupService raftGroupService = this.serverMap.remove(listenAddr.toString());
    raftGroupService.shutdown();
    raftGroupService.join();
    return node != null;
}
 
Example 7
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@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();
    }
}