Search in sources :

Example 1 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project sofa-jraft by sofastack.

the class RegionEngine method init.

@Override
public synchronized boolean init(final RegionEngineOptions opts) {
    if (this.started) {
        LOG.info("[RegionEngine: {}] already started.", this.region);
        return true;
    }
    this.regionOpts = Requires.requireNonNull(opts, "opts");
    this.fsm = new KVStoreStateMachine(this.region, this.storeEngine);
    // node options
    NodeOptions nodeOpts = opts.getNodeOptions();
    if (nodeOpts == null) {
        nodeOpts = new NodeOptions();
    }
    final long metricsReportPeriod = opts.getMetricsReportPeriod();
    if (metricsReportPeriod > 0) {
        // metricsReportPeriod > 0 means enable metrics
        nodeOpts.setEnableMetrics(true);
    }
    final Configuration initialConf = new Configuration();
    if (!initialConf.parse(opts.getInitialServerList())) {
        LOG.error("Fail to parse initial configuration {}.", opts.getInitialServerList());
        return false;
    }
    nodeOpts.setInitialConf(initialConf);
    nodeOpts.setFsm(this.fsm);
    final String raftDataPath = opts.getRaftDataPath();
    try {
        FileUtils.forceMkdir(new File(raftDataPath));
    } catch (final Throwable t) {
        LOG.error("Fail to make dir for raftDataPath {}.", raftDataPath);
        return false;
    }
    if (Strings.isBlank(nodeOpts.getLogUri())) {
        final Path logUri = Paths.get(raftDataPath, "log");
        nodeOpts.setLogUri(logUri.toString());
    }
    if (Strings.isBlank(nodeOpts.getRaftMetaUri())) {
        final Path meteUri = Paths.get(raftDataPath, "meta");
        nodeOpts.setRaftMetaUri(meteUri.toString());
    }
    if (Strings.isBlank(nodeOpts.getSnapshotUri())) {
        final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
        nodeOpts.setSnapshotUri(snapshotUri.toString());
    }
    LOG.info("[RegionEngine: {}], log uri: {}, raft meta uri: {}, snapshot uri: {}.", this.region, nodeOpts.getLogUri(), nodeOpts.getRaftMetaUri(), nodeOpts.getSnapshotUri());
    final Endpoint serverAddress = opts.getServerAddress();
    final PeerId serverId = new PeerId(serverAddress, 0);
    final RpcServer rpcServer = this.storeEngine.getRpcServer();
    this.raftGroupService = new RaftGroupService(opts.getRaftGroupId(), serverId, nodeOpts, rpcServer, true);
    this.node = this.raftGroupService.start(false);
    RouteTable.getInstance().updateConfiguration(this.raftGroupService.getGroupId(), nodeOpts.getInitialConf());
    if (this.node != null) {
        final RawKVStore rawKVStore = this.storeEngine.getRawKVStore();
        final Executor readIndexExecutor = this.storeEngine.getReadIndexExecutor();
        this.raftRawKVStore = new RaftRawKVStore(this.node, rawKVStore, readIndexExecutor);
        this.metricsRawKVStore = new MetricsRawKVStore(this.region.getId(), this.raftRawKVStore);
        // metrics config
        if (this.regionMetricsReporter == null && metricsReportPeriod > 0) {
            final MetricRegistry metricRegistry = this.node.getNodeMetrics().getMetricRegistry();
            if (metricRegistry != null) {
                final ScheduledExecutorService scheduler = this.storeEngine.getMetricsScheduler();
                // start raft node metrics reporter
                this.regionMetricsReporter = // 
                Slf4jReporter.forRegistry(metricRegistry).prefixedWith(// 
                "region_" + this.region.getId()).withLoggingLevel(// 
                Slf4jReporter.LoggingLevel.INFO).outputTo(// 
                LOG).scheduleOn(// 
                scheduler).shutdownExecutorOnStop(// 
                scheduler != null).build();
                this.regionMetricsReporter.start(metricsReportPeriod, TimeUnit.SECONDS);
            }
        }
        this.started = true;
        LOG.info("[RegionEngine] start successfully: {}.", this);
    }
    return this.started;
}
Also used : Path(java.nio.file.Path) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) MetricRegistry(com.codahale.metrics.MetricRegistry) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) KVStoreStateMachine(com.alipay.sofa.jraft.rhea.storage.KVStoreStateMachine) MetricsRawKVStore(com.alipay.sofa.jraft.rhea.storage.MetricsRawKVStore) RaftRawKVStore(com.alipay.sofa.jraft.rhea.storage.RaftRawKVStore) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) MetricsRawKVStore(com.alipay.sofa.jraft.rhea.storage.MetricsRawKVStore) Executor(java.util.concurrent.Executor) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) File(java.io.File) RaftRawKVStore(com.alipay.sofa.jraft.rhea.storage.RaftRawKVStore) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 2 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer in project sofa-jraft by sofastack.

the class TestCluster method start.

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;
}
Also used : Configuration(com.alipay.sofa.jraft.conf.Configuration) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) File(java.io.File) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 3 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer 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 4 with RpcServer

use of com.alipay.sofa.jraft.rpc.RpcServer 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 5 with RpcServer

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

Aggregations

RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)12 PeerId (com.alipay.sofa.jraft.entity.PeerId)8 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)6 Configuration (com.alipay.sofa.jraft.conf.Configuration)6 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)6 File (java.io.File)6 Endpoint (com.alipay.sofa.jraft.util.Endpoint)4 IOException (java.io.IOException)3 Node (com.alipay.sofa.jraft.Node)2 GetRequest (com.alibaba.nacos.consistency.entity.GetRequest)1 Log (com.alibaba.nacos.consistency.entity.Log)1 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)1 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 NacosGetRequestProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosGetRequestProcessor)1 NacosLogProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosLogProcessor)1 NacosReadRequestProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosReadRequestProcessor)1 NacosWriteRequestProcessor (com.alibaba.nacos.core.distributed.raft.processor.NacosWriteRequestProcessor)1 KVStoreStateMachine (com.alipay.sofa.jraft.rhea.storage.KVStoreStateMachine)1 MetricsRawKVStore (com.alipay.sofa.jraft.rhea.storage.MetricsRawKVStore)1