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

The following examples show how to use com.alipay.sofa.jraft.option.NodeOptions#setSnapshotUri() . 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: NodeTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testInitShutdown() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setFsm(new MockStateMachine(addr));
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");

    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));

    node.shutdown();
    node.join();
}
 
Example 2
Source File: AtomicRangeGroup.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public AtomicRangeGroup(String dataPath, String groupId, PeerId serverId, long minSlot, long maxSlot,
                        NodeOptions nodeOptions, RpcServer rpcServer) throws IOException {
    // Init file path
    FileUtils.forceMkdir(new File(dataPath));
    this.minSlot = minSlot;
    this.maxSlot = maxSlot;

    // Init statemachine
    this.fsm = new AtomicStateMachine();

    // Set statemachine to bootstrap options
    nodeOptions.setFsm(this.fsm);
    nodeOptions.setEnableMetrics(true);
    nodeOptions.getRaftOptions().setReplicatorPipeline(true);
    nodeOptions.getRaftOptions().setSync(true);
    nodeOptions.getRaftOptions().setReadOnlyOptions(ReadOnlyOption.ReadOnlySafe);

    // Set the data path
    // Log, required
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    // Metadata, required
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "raft_meta");
    // Snapshot, not required, but recommend
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    // Init raft group service framework
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
    // Startup node
    this.node = this.raftGroupService.start();

    final ConsoleReporter reporter = ConsoleReporter.forRegistry(node.getNodeMetrics().getMetricRegistry())
        .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    reporter.start(60, TimeUnit.SECONDS);

}
 
Example 3
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleNode() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);

    NodeManager.getInstance().addAddress(addr);
    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)));
    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()));
    }
    node.shutdown();
    node.join();
}
 
Example 4
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoSnapshot() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotIntervalSecs(10);
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));

    final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
    assertTrue(node.init(nodeOptions));
    // wait node elect self as leader
    Thread.sleep(2000);

    sendTestTaskAndWait(node);

    // wait for auto snapshot
    Thread.sleep(10000);
    // first snapshot will be triggered randomly
    final int times = fsm.getSaveSnapshotTimes();
    assertTrue("snapshotTimes=" + times, times >= 1);
    assertTrue(fsm.getSnapshotIndex() > 0);

    final CountDownLatch latch = new CountDownLatch(1);
    node.shutdown(new ExpectClosure(latch));
    node.join();
    waitLatch(latch);
}
 
Example 5
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testBootStrapWithoutSnapshot() throws Exception {
    final Endpoint addr = JRaftUtils.getEndPoint("127.0.0.1:5006");
    final MockStateMachine fsm = new MockStateMachine(addr);

    final BootstrapOptions opts = new BootstrapOptions();
    opts.setLastLogIndex(0);
    opts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    opts.setLogUri(this.dataPath + File.separator + "log");
    opts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
    opts.setFsm(fsm);

    NodeManager.getInstance().addAddress(addr);
    assertTrue(JRaftUtils.bootstrap(opts));

    final NodeOptions nodeOpts = createNodeOptionsWithSharedTimer();
    nodeOpts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOpts.setLogUri(this.dataPath + File.separator + "log");
    nodeOpts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOpts.setFsm(fsm);

    final NodeImpl node = new NodeImpl("test", new PeerId(addr, 0));
    assertTrue(node.init(nodeOpts));
    while (!node.isLeader()) {
        Thread.sleep(20);
    }
    this.sendTestTaskAndWait(node);
    assertEquals(10, fsm.getLogs().size());
    node.shutdown();
    node.join();
}
 
Example 6
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 7
Source File: DmetaServer.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public DmetaServer(final String dataPath, final String groupId, final PeerId serverId,
                   final NodeOptions nodeOptions) throws IOException {
  // init path
  FileUtils.forceMkdir(new File(dataPath));

  // make raft RPC and work RPC use same RPC server
  final RpcServer rpcServer = new RpcServer(serverId.getPort());
  RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);
  // Registration processor
  rpcServer.registerUserProcessor(new HeartbeatRequestProcessor(this));
  rpcServer.registerUserProcessor(new GetGlobalViewRequestProcessor(this));
  // init StateMachine
  this.fsm = new MetaStateMachine();
  // set StateMachine
  nodeOptions.setFsm(this.fsm);
  // set data path
  // log
  nodeOptions.setLogUri(dataPath + File.separator + "log" + serverId.getPort());
  // meta info
  nodeOptions.setRaftMetaUri(dataPath + File.separator + "raft_meta");
  // snapshot,optional
  nodeOptions.setSnapshotUri(dataPath + File.separator + "counter_snapshot");
  // init raft group framework
  this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
  // start
  this.node = this.raftGroupService.start();
}
 
Example 8
Source File: KVStateMachineTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws IOException, InterruptedException {
    final Region region = new Region();
    region.setId(1);
    final StoreEngine storeEngine = new MockStoreEngine();
    final KVStoreStateMachine fsm = new KVStoreStateMachine(region, storeEngine);
    final NodeOptions nodeOpts = new NodeOptions();
    final Configuration conf = new Configuration();
    conf.addPeer(PeerId.parsePeer("127.0.0.1:8081"));
    nodeOpts.setInitialConf(conf);
    nodeOpts.setFsm(fsm);

    final String raftDataPath = "raft_st_test";
    this.raftDataPath = new File(raftDataPath);
    if (this.raftDataPath.exists()) {
        FileUtils.forceDelete(this.raftDataPath);
    }
    FileUtils.forceMkdir(this.raftDataPath);

    final Path logUri = Paths.get(raftDataPath, "log");
    nodeOpts.setLogUri(logUri.toString());

    final Path meteUri = Paths.get(raftDataPath, "meta");
    nodeOpts.setRaftMetaUri(meteUri.toString());

    final Path snapshotUri = Paths.get(raftDataPath, "snapshot");
    nodeOpts.setSnapshotUri(snapshotUri.toString());

    final Endpoint serverAddress = new Endpoint("127.0.0.1", 8081);
    final PeerId serverId = new PeerId(serverAddress, 0);
    this.raftGroupService = new RaftGroupService("st_test", serverId, nodeOpts, null, true);

    final Node node = this.raftGroupService.start(false);

    for (int i = 0; i < 100; i++) {
        if (node.isLeader()) {
            break;
        }
        Thread.sleep(100);
    }

    final RawKVStore rawKVStore = storeEngine.getRawKVStore();
    this.raftRawKVStore = new RaftRawKVStore(node, rawKVStore, null);
}
 
Example 9
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 10
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 11
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Test
public void testNodeTaskOverload() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);

    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final RaftOptions raftOptions = new RaftOptions();
    raftOptions.setDisruptorBufferSize(2);
    nodeOptions.setRaftOptions(raftOptions);
    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)));
    final Node node = new NodeImpl("unittest", peer);
    assertTrue(node.init(nodeOptions));

    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));

    while (!node.isLeader()) {
        ;
    }

    final List<Task> tasks = new ArrayList<>();
    final AtomicInteger c = new AtomicInteger(0);
    for (int i = 0; i < 10; i++) {
        final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
        final Task task = new Task(data, new JoinableClosure(status -> {
            System.out.println(status);
            if (!status.isOk()) {
                assertTrue(
                        status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
            }
            c.incrementAndGet();
        }));
        node.apply(task);
        tasks.add(task);
    }
    try {
        Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
        assertEquals(10, c.get());
    } finally {
        node.shutdown();
        node.join();
    }
}
 
Example 12
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Test
public void testBootStrapWithSnapshot() throws Exception {
    final Endpoint addr = JRaftUtils.getEndPoint("127.0.0.1:5006");
    final MockStateMachine fsm = new MockStateMachine(addr);

    for (char ch = 'a'; ch <= 'z'; ch++) {
        fsm.getLogs().add(ByteBuffer.wrap(new byte[] { (byte) ch }));
    }

    final BootstrapOptions opts = new BootstrapOptions();
    opts.setLastLogIndex(fsm.getLogs().size());
    opts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    opts.setLogUri(this.dataPath + File.separator + "log");
    opts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
    opts.setFsm(fsm);

    NodeManager.getInstance().addAddress(addr);
    assertTrue(JRaftUtils.bootstrap(opts));

    final NodeOptions nodeOpts = createNodeOptionsWithSharedTimer();
    nodeOpts.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOpts.setLogUri(this.dataPath + File.separator + "log");
    nodeOpts.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOpts.setFsm(fsm);

    final NodeImpl node = new NodeImpl("test", new PeerId(addr, 0));
    assertTrue(node.init(nodeOpts));
    assertEquals(26, fsm.getLogs().size());

    for (int i = 0; i < 26; i++) {
        assertEquals('a' + i, fsm.getLogs().get(i).get());
    }

    while (!node.isLeader()) {
        Thread.sleep(20);
    }
    this.sendTestTaskAndWait(node);
    assertEquals(36, fsm.getLogs().size());
    node.shutdown();
    node.join();
}
 
Example 13
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 14
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 15
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 16
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();
    }