Java Code Examples for com.alipay.sofa.jraft.conf.Configuration#parse()

The following examples show how to use com.alipay.sofa.jraft.conf.Configuration#parse() . 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: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
protected void initRouteTableByRegion(final RegionRouteTableOptions opts) {
    final long regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
    final byte[] startKey = opts.getStartKeyBytes();
    final byte[] endKey = opts.getEndKeyBytes();
    final String initialServerList = opts.getInitialServerList();
    final Region region = new Region();
    final Configuration conf = new Configuration();
    // region
    region.setId(regionId);
    region.setStartKey(startKey);
    region.setEndKey(endKey);
    region.setRegionEpoch(new RegionEpoch(-1, -1));
    // peers
    Requires.requireTrue(Strings.isNotBlank(initialServerList), "opts.initialServerList is blank");
    conf.parse(initialServerList);
    region.setPeers(JRaftHelper.toPeerList(conf.listPeers()));
    // update raft route table
    RouteTable.getInstance().updateConfiguration(JRaftHelper.getJRaftGroupId(clusterName, regionId), conf);
    this.regionRouteTable.addOrUpdateRegion(region);
}
 
Example 2
Source File: AbstractPlacementDriverClient.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
protected Region getLocalRegionMetadata(final RegionEngineOptions opts) {
    final long regionId = Requires.requireNonNull(opts.getRegionId(), "opts.regionId");
    Requires.requireTrue(regionId >= Region.MIN_ID_WITH_MANUAL_CONF, "opts.regionId must >= "
                                                                     + Region.MIN_ID_WITH_MANUAL_CONF);
    Requires.requireTrue(regionId < Region.MAX_ID_WITH_MANUAL_CONF, "opts.regionId must < "
                                                                    + Region.MAX_ID_WITH_MANUAL_CONF);
    final byte[] startKey = opts.getStartKeyBytes();
    final byte[] endKey = opts.getEndKeyBytes();
    final String initialServerList = opts.getInitialServerList();
    final Region region = new Region();
    final Configuration conf = new Configuration();
    // region
    region.setId(regionId);
    region.setStartKey(startKey);
    region.setEndKey(endKey);
    region.setRegionEpoch(new RegionEpoch(-1, -1));
    // peers
    Requires.requireTrue(Strings.isNotBlank(initialServerList), "opts.initialServerList is blank");
    conf.parse(initialServerList);
    region.setPeers(JRaftHelper.toPeerList(conf.listPeers()));
    this.regionRouteTable.addOrUpdateRegion(region);
    return region;
}
 
Example 3
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 4
Source File: JRaftUtils.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Create a configuration from a string in the form of "host1:port1[:idx],host2:port2[:idx]......",
 * returns a empty configuration when string is blank.
 */
public static Configuration getConfiguration(final String s) {
    final Configuration conf = new Configuration();
    if (StringUtils.isBlank(s)) {
        return conf;
    }
    if (conf.parse(s)) {
        return conf;
    }
    throw new IllegalArgumentException("Invalid conf str:" + s);
}
 
Example 5
Source File: RouteTable.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Update configuration of group in route table.
 *
 * @param groupId raft group id
 * @param confStr configuration string
 * @return true on success
 */
public boolean updateConfiguration(final String groupId, final String confStr) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireTrue(!StringUtils.isBlank(confStr), "Blank configuration");

    final Configuration conf = new Configuration();
    if (conf.parse(confStr)) {
        return updateConfiguration(groupId, conf);
    } else {
        LOG.error("Fail to parse confStr: {}", confStr);
        return false;
    }
}
 
Example 6
Source File: CounterClient.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
    if (args.length != 2) {
        System.out.println("Useage : java com.alipay.sofa.jraft.example.counter.CounterClient {groupId} {conf}");
        System.out
            .println("Example: java com.alipay.sofa.jraft.example.counter.CounterClient counter 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083");
        System.exit(1);
    }
    final String groupId = args[0];
    final String confStr = args[1];

    final Configuration conf = new Configuration();
    if (!conf.parse(confStr)) {
        throw new IllegalArgumentException("Fail to parse conf:" + confStr);
    }

    RouteTable.getInstance().updateConfiguration(groupId, conf);

    final CliClientServiceImpl cliClientService = new CliClientServiceImpl();
    cliClientService.init(new CliOptions());

    if (!RouteTable.getInstance().refreshLeader(cliClientService, groupId, 1000).isOk()) {
        throw new IllegalStateException("Refresh leader failed");
    }

    final PeerId leader = RouteTable.getInstance().selectLeader(groupId);
    System.out.println("Leader is " + leader);
    final int n = 1000;
    final CountDownLatch latch = new CountDownLatch(n);
    final long start = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        incrementAndGet(cliClientService, leader, i, latch);
    }
    latch.await();
    System.out.println(n + " ops, cost : " + (System.currentTimeMillis() - start) + " ms.");
    System.exit(0);
}
 
Example 7
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 8
Source File: RaftClient.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * @param groupId
 * @param confStr  Example: 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083
 */
public RaftClient(String groupId, String confStr) {

    this.groupId = groupId;
    conf = new Configuration();
    if (!conf.parse(confStr)) {
        throw new IllegalArgumentException("Fail to parse conf:" + confStr);
    }
    cliOptions = new CliOptions();
    cliClientService = new BoltCliClientService();
}
 
Example 9
Source File: RaftClient.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * @param groupId
 * @param confStr
 * @param cliClientService
 */
public RaftClient(String groupId, String confStr, AbstractBoltClientService cliClientService) {

    this.groupId = groupId;
    conf = new Configuration();
    if (!conf.parse(confStr)) {
        throw new IllegalArgumentException("Fail to parse conf:" + confStr);
    }
    cliOptions = new CliOptions();
    this.cliClientService = (BoltCliClientService) cliClientService;
}
 
Example 10
Source File: RaftServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param dataPath    Example: /tmp/server1
 * @param groupId
 * @param serverIdStr Example: 127.0.0.1:8081
 * @param initConfStr Example: 127.0.0.1:8081,127.0.0.1:8082,127.0.0.1:8083
 * @throws IOException
 */
public RaftServer(String dataPath, String groupId, String serverIdStr, String initConfStr) {
    this.dataPath = dataPath;
    this.groupId = groupId;
    serverId = new PeerId();
    if (!serverId.parse(serverIdStr)) {
        throw new IllegalArgumentException("Fail to parse serverId:" + serverIdStr);
    }

    initConf = new Configuration();
    if (!initConf.parse(initConfStr)) {
        throw new IllegalArgumentException("Fail to parse initConf:" + initConfStr);
    }
}
 
Example 11
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 12
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();
    }