Search in sources :

Example 26 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.

the class NodeTest method testBootStrapWithSnapshot.

@Test
public void testBootStrapWithSnapshot() throws Exception {
    final Endpoint addr = JRaftUtils.getEndPoint("127.0.0.1:5006");
    final MockStateMachine fsm = new MockStateMachine(addr);
    for (char ch = 'a'; ch <= 'z'; ch++) {
        fsm.getLogs().add(ByteBuffer.wrap(new byte[] { (byte) ch }));
    }
    final BootstrapOptions opts = new BootstrapOptions();
    opts.setLastLogIndex(fsm.getLogs().size());
    opts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    opts.setLogUri(this.dataPath + File.separator + "log");
    opts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
    opts.setFsm(fsm);
    NodeManager.getInstance().addAddress(addr);
    assertTrue(JRaftUtils.bootstrap(opts));
    final NodeOptions nodeOpts = createNodeOptionsWithSharedTimer();
    nodeOpts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOpts.setLogUri(this.dataPath + File.separator + "log");
    nodeOpts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOpts.setFsm(fsm);
    final NodeImpl node = new NodeImpl("test", new PeerId(addr, 0));
    assertTrue(node.init(nodeOpts));
    assertEquals(26, fsm.getLogs().size());
    for (int i = 0; i < 26; i++) {
        assertEquals('a' + i, fsm.getLogs().get(i).get());
    }
    while (!node.isLeader()) {
        Thread.sleep(20);
    }
    this.sendTestTaskAndWait(node);
    assertEquals(36, fsm.getLogs().size());
    node.shutdown();
    node.join();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) BootstrapOptions(com.alipay.sofa.jraft.option.BootstrapOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 27 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.

the class NodeTest method testBootStrapWithoutSnapshot.

@Test
public void testBootStrapWithoutSnapshot() throws Exception {
    final Endpoint addr = JRaftUtils.getEndPoint("127.0.0.1:5006");
    final MockStateMachine fsm = new MockStateMachine(addr);
    final BootstrapOptions opts = new BootstrapOptions();
    opts.setLastLogIndex(0);
    opts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    opts.setLogUri(this.dataPath + File.separator + "log");
    opts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
    opts.setFsm(fsm);
    NodeManager.getInstance().addAddress(addr);
    assertTrue(JRaftUtils.bootstrap(opts));
    final NodeOptions nodeOpts = createNodeOptionsWithSharedTimer();
    nodeOpts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOpts.setLogUri(this.dataPath + File.separator + "log");
    nodeOpts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOpts.setFsm(fsm);
    final NodeImpl node = new NodeImpl("test", new PeerId(addr, 0));
    assertTrue(node.init(nodeOpts));
    while (!node.isLeader()) {
        Thread.sleep(20);
    }
    this.sendTestTaskAndWait(node);
    assertEquals(10, fsm.getLogs().size());
    node.shutdown();
    node.join();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) BootstrapOptions(com.alipay.sofa.jraft.option.BootstrapOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 28 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.

the class NodeTest method testShutdownAndJoinWorkAfterInitFails.

@Test
public void testShutdownAndJoinWorkAfterInitFails() 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));
        Thread.sleep(1000);
        this.sendTestTaskAndWait(node);
        // save snapshot
        final CountDownLatch latch = new CountDownLatch(1);
        node.snapshot(new ExpectClosure(latch));
        waitLatch(latch);
        node.shutdown();
        node.join();
    }
    {
        final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
        final MockStateMachine fsm = new MockFSM1(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));
        assertFalse(node.init(nodeOptions));
        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) CountDownLatch(java.util.concurrent.CountDownLatch) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 29 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.

the class NodeTest method testAutoSnapshot.

@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);
}
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) Endpoint(com.alipay.sofa.jraft.util.Endpoint) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 30 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project sofa-jraft by sofastack.

the class NodeTest method testInitShutdown.

@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();
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)39 PeerId (com.alipay.sofa.jraft.entity.PeerId)24 Endpoint (com.alipay.sofa.jraft.util.Endpoint)22 Configuration (com.alipay.sofa.jraft.conf.Configuration)18 Test (org.junit.Test)16 Node (com.alipay.sofa.jraft.Node)13 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)9 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)9 File (java.io.File)8 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)7 Before (org.junit.Before)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 BootstrapOptions (com.alipay.sofa.jraft.option.BootstrapOptions)3 SnapshotCopierOptions (com.alipay.sofa.jraft.option.SnapshotCopierOptions)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Iterator (com.alipay.sofa.jraft.Iterator)2 StateMachine (com.alipay.sofa.jraft.StateMachine)2