Search in sources :

Example 16 with NodeOptions

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

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

Example 18 with NodeOptions

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

the class ElectionNode method init.

@Override
public boolean init(final ElectionNodeOptions opts) {
    if (this.started) {
        LOG.info("[ElectionNode: {}] already started.", opts.getServerAddress());
        return true;
    }
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    this.fsm = new ElectionOnlyStateMachine(this.listeners);
    nodeOpts.setFsm(this.fsm);
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerAddressList())) {
        throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
    }
    // Set the initial cluster configuration
    nodeOpts.setInitialConf(initialConf);
    final String dataPath = opts.getDataPath();
    try {
        FileUtils.forceMkdir(new File(dataPath));
    } catch (final IOException e) {
        LOG.error("Fail to make dir for dataPath {}.", dataPath);
        return false;
    }
    // Set the data path
    // Log, required
    nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
    // Metadata, required
    nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
    // nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
    final String groupId = opts.getGroupId();
    final PeerId serverId = new PeerId();
    if (!serverId.parse(opts.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
    }
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
    this.node = this.raftGroupService.start();
    if (this.node != null) {
        this.started = true;
    }
    return this.started;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 19 with NodeOptions

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

the class PriorityElectionNode method init.

@Override
public boolean init(final PriorityElectionNodeOptions opts) {
    if (this.started) {
        LOG.info("[PriorityElectionNode: {}] already started.", opts.getServerAddress());
        return true;
    }
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    this.fsm = new PriorityElectionOnlyStateMachine(this.listeners);
    // Set the initial PriorityElectionOnlyStateMachine
    nodeOpts.setFsm(this.fsm);
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerAddressList())) {
        throw new IllegalArgumentException("Fail to parse initConf: " + opts.getInitialServerAddressList());
    }
    // Set the initial cluster configuration
    nodeOpts.setInitialConf(initialConf);
    final String dataPath = opts.getDataPath();
    try {
        FileUtils.forceMkdir(new File(dataPath));
    } catch (final IOException e) {
        LOG.error("Fail to make dir for dataPath {}.", dataPath);
        return false;
    }
    // Set the data path
    // Log, required
    nodeOpts.setLogUri(Paths.get(dataPath, "log").toString());
    // Metadata, required
    nodeOpts.setRaftMetaUri(Paths.get(dataPath, "meta").toString());
    // nodeOpts.setSnapshotUri(Paths.get(dataPath, "snapshot").toString());
    final String groupId = opts.getGroupId();
    final PeerId serverId = new PeerId();
    if (!serverId.parse(opts.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId: " + opts.getServerAddress());
    }
    /**
     * Set priority value, required for priority-based election, it must be a positive value when
     * enable the feature, some special value meaning:
     * <ul>
     * <li>-1 : disable priority-based election.</li>
     * <li>0: will never participate in election.</li>
     * <li>1: minimum value</li>
     * </ul>
     * value.
     */
    nodeOpts.setElectionPriority(serverId.getPriority());
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOpts, rpcServer);
    this.node = this.raftGroupService.start();
    if (this.node != null) {
        this.started = true;
    }
    return this.started;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) IOException(java.io.IOException) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 20 with NodeOptions

use of com.alipay.sofa.jraft.option.NodeOptions in project jdchain-core by blockchain-jd-com.

the class RaftNodeServer method initNodeOptions.

private NodeOptions initNodeOptions(RaftServerSettings config) {
    NodeOptions options = new NodeOptions();
    options.setElectionTimeoutMs(config.getElectionTimeoutMs());
    options.setSnapshotIntervalSecs(config.getSnapshotIntervalSec());
    mkdirRaftDirs(serverSettings.getExtraProperties().getProperty(RAFT_PATH_KEY), options);
    options.setSharedElectionTimer(true);
    options.setSharedVoteTimer(true);
    options.setSharedStepDownTimer(true);
    options.setSharedSnapshotTimer(true);
    options.setRaftOptions(RaftConfig.buildRaftOptions(config.getRaftSettings()));
    return options;
}
Also used : NodeOptions(com.alipay.sofa.jraft.option.NodeOptions)

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