Java Code Examples for com.alipay.sofa.jraft.rpc.RaftRpcServerFactory#createRaftRpcServer()

The following examples show how to use com.alipay.sofa.jraft.rpc.RaftRpcServerFactory#createRaftRpcServer() . 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: AtomicServer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public void start() throws IOException {
    PeerId serverId = new PeerId();
    if (!serverId.parse(conf.getServerAddress())) {
        throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
    }

    FileUtils.forceMkdir(new File(conf.getDataPath()));
    // The same in-process raft group shares the same RPC Server.
    RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    // Register biz handler
    rpcServer.registerProcessor(new GetSlotsCommandProcessor(this));
    rpcServer.registerProcessor(new GetCommandProcessor(this));
    rpcServer.registerProcessor(new IncrementAndGetCommandProcessor(this));
    rpcServer.registerProcessor(new CompareAndSetCommandProcessor(this));
    rpcServer.registerProcessor(new SetCommandProcessor(this));

    long step = conf.getMaxSlot() / totalSlots;
    if (conf.getMaxSlot() % totalSlots > 0) {
        step = step + 1;
    }
    for (int i = 0; i < totalSlots; i++) {
        long min = i * step;
        long mayMax = (i + 1) * step;
        long max = mayMax > conf.getMaxSlot() || mayMax <= 0 ? conf.getMaxSlot() : mayMax;
        StartupConf nodeConf = new StartupConf();
        String nodeDataPath = conf.getDataPath() + File.separator + i;
        nodeConf.setDataPath(nodeDataPath);
        String nodeGroup = conf.getGroupId() + "_" + i;
        nodeConf.setGroupId(nodeGroup);
        nodeConf.setMaxSlot(max);
        nodeConf.setMinSlot(min);
        nodeConf.setConf(conf.getConf());
        nodeConf.setServerAddress(conf.getServerAddress());
        nodeConf.setTotalSlots(conf.getTotalSlots());
        LOG.info("Starting range node {}-{} with conf {}", min, max, nodeConf);
        nodes.put(i * step, AtomicRangeGroup.start(nodeConf, rpcServer));
        groups.put(i * step, nodeGroup);
    }
}
 
Example 2
Source File: CounterServer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public CounterServer(final String dataPath, final String groupId, final PeerId serverId,
                     final NodeOptions nodeOptions) throws IOException {
    // 初始化路径
    FileUtils.forceMkdir(new File(dataPath));

    // 这里让 raft RPC 和业务 RPC 使用同一个 RPC server, 通常也可以分开
    final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint());
    // 注册业务处理器
    CounterService counterService = new CounterServiceImpl(this);
    rpcServer.registerProcessor(new GetValueRequestProcessor(counterService));
    rpcServer.registerProcessor(new IncrementAndGetRequestProcessor(counterService));
    // 初始化状态机
    this.fsm = new CounterStateMachine();
    // 设置状态机到启动参数
    nodeOptions.setFsm(this.fsm);
    // 设置存储路径
    // 日志, 必须
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    // 元信息, 必须
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "raft_meta");
    // snapshot, 可选, 一般都推荐
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    // 初始化 raft group 服务框架
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
    // 启动
    this.node = this.raftGroupService.start();
}
 
Example 3
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 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) 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 5
Source File: RaftGroupService.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public RaftGroupService(final String groupId, final PeerId serverId, final NodeOptions nodeOptions) {
    this(groupId, serverId, nodeOptions, RaftRpcServerFactory.createRaftRpcServer(serverId.getEndpoint(),
        JRaftUtils.createExecutor("RAFT-RPC-executor-", nodeOptions.getRaftRpcThreadPoolSize()),
        JRaftUtils.createExecutor("CLI-RPC-executor-", nodeOptions.getCliRpcThreadPoolSize())));
}
 
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;
}