Java Code Examples for com.alipay.sofa.jraft.option.NodeOptions#setElectionTimeoutMs()

The following examples show how to use com.alipay.sofa.jraft.option.NodeOptions#setElectionTimeoutMs() . 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: AtomicRangeGroup.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
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;
    }
 
Example 2
Source File: CounterServer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws IOException {
    if (args.length != 4) {
        System.out
            .println("Useage : java com.alipay.sofa.jraft.example.counter.CounterServer {dataPath} {groupId} {serverId} {initConf}");
        System.out
            .println("Example: java com.alipay.sofa.jraft.example.counter.CounterServer /tmp/server1 counter 127.0.0.1:8081 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
        System.exit(1);
    }
    final String dataPath = args[0];
    final String groupId = args[1];
    final String serverIdStr = args[2];
    final String initConfStr = args[3];

    final NodeOptions nodeOptions = new NodeOptions();
    // 为了测试,调整 snapshot 间隔等参数
    // 设置选举超时时间为 1 秒
    nodeOptions.setElectionTimeoutMs(1000);
    // 关闭 CLI 服务。
    nodeOptions.setDisableCli(false);
    // 每隔30秒做一次 snapshot
    nodeOptions.setSnapshotIntervalSecs(30);
    // 解析参数
    final PeerId serverId = new PeerId();
    if (!serverId.parse(serverIdStr)) {
        throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
    }
    final Configuration initConf = new Configuration();
    if (!initConf.parse(initConfStr)) {
        throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
    }
    // 设置初始集群配置
    nodeOptions.setInitialConf(initConf);

    // 启动
    final CounterServer counterServer = new CounterServer(dataPath, groupId, serverId, nodeOptions);
    System.out.println("Started counter server at port:"
                       + counterServer.getNode().getNodeId().getPeerId().getPort());
}
 
Example 3
Source File: DmetaServer.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(final String[] args) throws IOException {
  if (args.length != 4) {
    System.out
        .println("Useage :  {dataPath} {groupId} {serverId} {initConf}");
    System.out
        .println("Example:  /tmp/server1 counter 127.0.0.1:8081 " +
            "127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
    System.exit(1);
  }
  final String dataPath = args[0];
  final String groupId = args[1];
  final String serverIdStr = args[2];
  final String initConfStr = args[3];

  final NodeOptions nodeOptions = new NodeOptions();
  // Set the election timeout to 1 second.
  nodeOptions.setElectionTimeoutMs(1000);
  // close CLI service.
  nodeOptions.setDisableCli(false);
  //30s snapshot
  nodeOptions.setSnapshotIntervalSecs(30);
  // parser
  final PeerId serverId = new PeerId();
  if (!serverId.parse(serverIdStr)) {
    throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
  }
  final Configuration initConf = new Configuration();
  if (!initConf.parse(initConfStr)) {
    throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
  }
  // set origin conf
  nodeOptions.setInitialConf(initConf);

  // start
  final DmetaServer counterServer = new DmetaServer(dataPath, groupId, serverId, nodeOptions);
  System.out.println("Started DMeta server at port:"
      + counterServer.getNode().getNodeId().getPeerId().getPort());
}
 
Example 4
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public boolean start(final Endpoint listenAddr, final boolean emptyPeers, final int snapshotIntervalSecs,
                     final boolean enableMetrics, final SnapshotThrottle snapshotThrottle,
                     final RaftOptions raftOptions, final int priority) throws IOException {

    if (this.serverMap.get(listenAddr.toString()) != null) {
        return true;
    }

    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
    nodeOptions.setEnableMetrics(enableMetrics);
    nodeOptions.setSnapshotThrottle(snapshotThrottle);
    nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
    if (raftOptions != null) {
        nodeOptions.setRaftOptions(raftOptions);
    }
    final String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
    FileUtils.forceMkdir(new File(serverDataPath));
    nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
    nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
    nodeOptions.setElectionPriority(priority);

    final MockStateMachine fsm = new MockStateMachine(listenAddr);
    nodeOptions.setFsm(fsm);

    if (!emptyPeers) {
        nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
    }

    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(listenAddr);
    final RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0, priority),
        nodeOptions, rpcServer);

    this.lock.lock();
    try {
        if (this.serverMap.put(listenAddr.toString(), server) == null) {
            final Node node = server.start();

            this.fsms.put(new PeerId(listenAddr, 0), fsm);
            this.nodes.add((NodeImpl) node);
            return true;
        }
    } finally {
        this.lock.unlock();
    }
    return false;
}
 
Example 5
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public boolean start(final Endpoint listenAddr, final boolean emptyPeers, final int snapshotIntervalSecs,
                     final boolean enableMetrics, final SnapshotThrottle snapshotThrottle,
                     final RaftOptions raftOptions) throws IOException {

    if (this.serverMap.get(listenAddr.toString()) != null) {
        return true;
    }

    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
    nodeOptions.setEnableMetrics(enableMetrics);
    nodeOptions.setSnapshotThrottle(snapshotThrottle);
    nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
    if (raftOptions != null) {
        nodeOptions.setRaftOptions(raftOptions);
    }
    final String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
    FileUtils.forceMkdir(new File(serverDataPath));
    nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
    nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
    final MockStateMachine fsm = new MockStateMachine(listenAddr);
    nodeOptions.setFsm(fsm);

    if (!emptyPeers) {
        nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
    }

    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(listenAddr);
    final RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0), nodeOptions,
        rpcServer);

    this.lock.lock();
    try {
        if (this.serverMap.put(listenAddr.toString(), server) == null) {
            final Node node = server.start();

            this.fsms.put(new PeerId(listenAddr, 0), fsm);
            this.nodes.add((NodeImpl) node);
            return true;
        }
    } finally {
        this.lock.unlock();
    }
    return false;
}
 
Example 6
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public boolean start(final Endpoint listenAddr, final boolean emptyPeers, final int snapshotIntervalSecs,
                     final boolean enableMetrics, final SnapshotThrottle snapshotThrottle,
                     final RaftOptions raftOptions, final int priority) throws IOException {

    if (this.serverMap.get(listenAddr.toString()) != null) {
        return true;
    }

    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
    nodeOptions.setEnableMetrics(enableMetrics);
    nodeOptions.setSnapshotThrottle(snapshotThrottle);
    nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
    nodeOptions.setServiceFactory(this.raftServiceFactory);
    if (raftOptions != null) {
        nodeOptions.setRaftOptions(raftOptions);
    }
    final String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
    FileUtils.forceMkdir(new File(serverDataPath));
    nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
    nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
    nodeOptions.setElectionPriority(priority);

    final MockStateMachine fsm = new MockStateMachine(listenAddr);
    nodeOptions.setFsm(fsm);

    if (!emptyPeers) {
        nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
    }

    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(listenAddr);
    final RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0, priority),
        nodeOptions, rpcServer);

    this.lock.lock();
    try {
        if (this.serverMap.put(listenAddr.toString(), server) == null) {
            final Node node = server.start();

            this.fsms.put(new PeerId(listenAddr, 0), fsm);
            this.nodes.add((NodeImpl) node);
            return true;
        }
    } finally {
        this.lock.unlock();
    }
    return false;
}
 
Example 7
Source File: TestCluster.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public boolean start(final Endpoint listenAddr, final boolean emptyPeers, final int snapshotIntervalSecs,
                     final boolean enableMetrics, final SnapshotThrottle snapshotThrottle,
                     final RaftOptions raftOptions) throws IOException {

    if (this.serverMap.get(listenAddr.toString()) != null) {
        return true;
    }

    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setElectionTimeoutMs(this.electionTimeoutMs);
    nodeOptions.setEnableMetrics(enableMetrics);
    nodeOptions.setSnapshotThrottle(snapshotThrottle);
    nodeOptions.setSnapshotIntervalSecs(snapshotIntervalSecs);
    nodeOptions.setServiceFactory(this.raftServiceFactory);
    if (raftOptions != null) {
        nodeOptions.setRaftOptions(raftOptions);
    }
    final String serverDataPath = this.dataPath + File.separator + listenAddr.toString().replace(':', '_');
    FileUtils.forceMkdir(new File(serverDataPath));
    nodeOptions.setLogUri(serverDataPath + File.separator + "logs");
    nodeOptions.setRaftMetaUri(serverDataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(serverDataPath + File.separator + "snapshot");
    final MockStateMachine fsm = new MockStateMachine(listenAddr);
    nodeOptions.setFsm(fsm);

    if (!emptyPeers) {
        nodeOptions.setInitialConf(new Configuration(this.peers, this.learners));
    }

    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(listenAddr);
    final RaftGroupService server = new RaftGroupService(this.name, new PeerId(listenAddr, 0), nodeOptions,
        rpcServer);

    this.lock.lock();
    try {
        if (this.serverMap.put(listenAddr.toString(), server) == null) {
            final Node node = server.start();

            this.fsms.put(new PeerId(listenAddr, 0), fsm);
            this.nodes.add((NodeImpl) node);
            return true;
        }
    } finally {
        this.lock.unlock();
    }
    return false;
}
 
Example 8
Source File: RaftServer.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
private NodeOptions initNodeOptions(RaftServerConfig raftServerConfig) {

        NodeOptions nodeOptions = new NodeOptions();

        nodeOptions.setElectionTimeoutMs(raftServerConfig.getElectionTimeoutMs());

        nodeOptions.setDisableCli(false);

        nodeOptions.setSnapshotIntervalSecs(raftServerConfig.getSnapshotIntervalSecs());

        nodeOptions.setInitialConf(initConf);

        nodeOptions.setFsm(this.fsm);

        nodeOptions.setLogUri(dataPath + File.separator + "log");
        nodeOptions.setRaftMetaUri(dataPath + File.separator + "raft_meta");
        nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");

        if (raftServerConfig.isEnableMetrics()) {
            nodeOptions.setEnableMetrics(raftServerConfig.isEnableMetrics());
        }

        // See https://github.com/sofastack/sofa-jraft/pull/156
        final BlockBasedTableConfig conf = new BlockBasedTableConfig() //
            // Begin to use partitioned index filters
            // https://github.com/facebook/rocksdb/wiki/Partitioned-Index-Filters#how-to-use-it
            .setIndexType(IndexType.kTwoLevelIndexSearch) //
            .setFilter(new BloomFilter(16, false)) //
            .setPartitionFilters(true) //
            .setMetadataBlockSize(8 * SizeUnit.KB) //
            .setCacheIndexAndFilterBlocks(false) //
            .setCacheIndexAndFilterBlocksWithHighPriority(true) //
            .setPinL0FilterAndIndexBlocksInCache(true) //
            // End of partitioned index filters settings.
            .setBlockSize(4 * SizeUnit.KB)//
            .setBlockCacheSize(raftServerConfig.getRockDBCacheSize() * SizeUnit.MB) //
            .setCacheNumShardBits(8);

        StorageOptionsFactory.registerRocksDBTableFormatConfig(RocksDBLogStorage.class, conf);

        return nodeOptions;
    }
 
Example 9
Source File: KitRaft.java    From KitDB with Apache License 2.0 4 votes vote down vote up
public KitRaft(GroupConfig groupConfig, NodeConfig nodeConfig, DB db) throws IOException {

        NodeOptions nodeOptions = new NodeOptions();

        RaftOptions raftOptions = new RaftOptions();
        raftOptions.setDisruptorBufferSize(16 * 16384);
        raftOptions.setApplyBatch(128);
        raftOptions.setSync(false);
        nodeOptions.setRaftOptions(raftOptions);

        nodeOptions.setElectionTimeoutMs(groupConfig.getElectionTimeoutMs());
        nodeOptions.setDisableCli(true);
        nodeOptions.setSnapshotIntervalSecs(groupConfig.getSnapshotIntervalSecs());

        PeerId serverId = new PeerId();
        if (!serverId.parse(nodeConfig.getNode())) {
            throw new IllegalArgumentException("Fail to parse serverId:" + nodeConfig.getNode());
        }

        Configuration initConf = new Configuration();
        if (!initConf.parse(groupConfig.getInitNodes())) {
            throw new IllegalArgumentException("Fail to parse initConf:" + groupConfig.getInitNodes());
        }

        nodeOptions.setInitialConf(initConf);

        String raftDir = nodeConfig.getRaftDir();
        FileUtils.forceMkdir(new File(raftDir));

        RpcServer rpcServer = new RpcServer(serverId.getPort());
        RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);

        this.dbsm = new DBStateMachine();
        dbsm.setDbRequestProcessor(new DBRequestProcessor(this));
        dbsm.setDB(db);
        nodeOptions.setFsm(this.dbsm);

        nodeOptions.setLogUri(raftDir + File.separator + "log");
        nodeOptions.setRaftMetaUri(raftDir + File.separator + "raft_meta");
        nodeOptions.setSnapshotUri(raftDir + File.separator + "snapshot");

        this.raftGroupService = new RaftGroupService(groupConfig.getGroup(), serverId, nodeOptions, rpcServer);
        // 启动
        this.node = this.raftGroupService.start();
    }