com.alipay.sofa.jraft.option.NodeOptions Java Examples

The following examples show how to use com.alipay.sofa.jraft.option.NodeOptions. 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: BaseCliRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testManyNodes() {
    Node node1 = Mockito.mock(Node.class);
    Mockito.when(node1.getGroupId()).thenReturn("test");
    Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    NodeOptions opts = new NodeOptions();
    Mockito.when(node1.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8081));
    NodeManager.getInstance().add(node1);

    Node node2 = Mockito.mock(Node.class);
    Mockito.when(node2.getGroupId()).thenReturn("test");
    Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
    Mockito.when(node2.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8082));
    NodeManager.getInstance().add(node2);

    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
    assertEquals("Peer must be specified since there're 2 nodes in group test", resp.getErrorMsg());
}
 
Example #2
Source File: AbstractCliRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testHandleRequest() {
    this.mockNodes(3);
    Mockito.when(this.node.getGroupId()).thenReturn(this.groupId);
    PeerId peerId = new PeerId();
    peerId.parse(this.peerIdStr);
    Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
    Mockito.when(this.node.getNodeId()).thenReturn(new NodeId("test", peerId));
    NodeManager.getInstance().addAddress(peerId.getEndpoint());
    NodeManager.getInstance().add(this.node);

    BaseCliRequestProcessor<T> processor = newProcessor();
    processor.handleRequest(this.asyncContext, createRequest(this.groupId, peerId));
    ArgumentCaptor<Closure> doneArg = ArgumentCaptor.forClass(Closure.class);
    verify(processor.interest(), this.node, doneArg);
}
 
Example #3
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 #4
Source File: SnapshotExecutorTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoSnapshotWithIntervalDist() throws Exception {
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setSnapshotLogIndexMargin(5);
    Mockito.when(this.node.getOptions()).thenReturn(nodeOptions);
    Mockito.when(this.fSMCaller.getLastAppliedIndex()).thenReturn(6L);

    final ArgumentCaptor<SaveSnapshotClosure> saveSnapshotClosureArg = ArgumentCaptor
        .forClass(SaveSnapshotClosure.class);
    Mockito.when(this.fSMCaller.onSnapshotSave(saveSnapshotClosureArg.capture())).thenReturn(true);
    final SynchronizedClosure done = new SynchronizedClosure();
    this.executor.doSnapshot(done);
    final SaveSnapshotClosure closure = saveSnapshotClosureArg.getValue();
    assertNotNull(closure);
    closure.start(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(6).setLastIncludedTerm(1).build());
    closure.run(Status.OK());
    done.await();
    this.executor.join();

    assertEquals(1, this.executor.getLastSnapshotTerm());
    assertEquals(6, this.executor.getLastSnapshotIndex());

}
 
Example #5
Source File: LocalSnapshotCopierTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
@Before
public void setup() throws Exception {
    super.setup();
    this.timerManager = new TimerManager(5);
    this.raftOptions = new RaftOptions();
    this.writer = new LocalSnapshotWriter(this.path, this.snapshotStorage, this.raftOptions);
    this.reader = new LocalSnapshotReader(this.snapshotStorage, null, new Endpoint("localhost", 8081),
        this.raftOptions, this.path);

    Mockito.when(this.snapshotStorage.open()).thenReturn(this.reader);
    Mockito.when(this.snapshotStorage.create(true)).thenReturn(this.writer);

    this.table = new LocalSnapshotMetaTable(this.raftOptions);
    this.table.addFile("testFile", LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("test").build());
    this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(1).build());
    this.uri = "remote://" + this.hostPort + "/" + this.readerId;
    this.copier = new LocalSnapshotCopier();
    this.copyOpts = new CopyOptions();
    Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
    assertTrue(this.copier.init(this.uri, new SnapshotCopierOptions(this.raftClientService, this.timerManager,
        this.raftOptions, new NodeOptions())));
    this.copier.setStorage(this.snapshotStorage);
}
 
Example #6
Source File: DefaultRaftClientService.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized boolean init(final RpcOptions rpcOptions) {
    final boolean ret = super.init(rpcOptions);
    if (ret) {
        this.nodeOptions = (NodeOptions) rpcOptions;
    }
    return ret;
}
 
Example #7
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 #8
Source File: BaseCliRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private Node mockNode(boolean disableCli) {
    Node node = Mockito.mock(Node.class);
    Mockito.when(node.getGroupId()).thenReturn("test");
    Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", this.peer.copy()));
    NodeOptions opts = new NodeOptions();
    opts.setDisableCli(disableCli);
    Mockito.when(node.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(this.peer.getEndpoint());
    NodeManager.getInstance().add(node);
    return node;
}
 
Example #9
Source File: SnapshotExecutorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setup() throws Exception {
    super.setup();
    this.timerManager = new TimerManager(5);
    this.raftOptions = new RaftOptions();
    this.writer = new LocalSnapshotWriter(this.path, this.snapshotStorage, this.raftOptions);
    this.reader = new LocalSnapshotReader(this.snapshotStorage, null, new Endpoint("localhost", 8081),
        this.raftOptions, this.path);

    Mockito.when(this.snapshotStorage.open()).thenReturn(this.reader);
    Mockito.when(this.snapshotStorage.create(true)).thenReturn(this.writer);

    this.table = new LocalSnapshotMetaTable(this.raftOptions);
    this.table.addFile("testFile", LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("test").build());
    this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(1).build());
    this.uri = "remote://" + this.hostPort + "/" + this.readerId;
    this.copyOpts = new CopyOptions();

    Mockito.when(this.node.getRaftOptions()).thenReturn(new RaftOptions());
    Mockito.when(this.node.getOptions()).thenReturn(new NodeOptions());
    Mockito.when(this.node.getRpcService()).thenReturn(this.raftClientService);
    Mockito.when(this.node.getTimerManager()).thenReturn(this.timerManager);
    Mockito.when(this.node.getServiceFactory()).thenReturn(DefaultJRaftServiceFactory.newInstance());
    this.executor = new SnapshotExecutorImpl();
    final SnapshotExecutorOptions opts = new SnapshotExecutorOptions();
    opts.setFsmCaller(this.fSMCaller);
    opts.setInitTerm(0);
    opts.setNode(this.node);
    opts.setLogManager(this.logManager);
    opts.setUri(this.path);
    this.addr = new Endpoint("localhost", 8081);
    opts.setAddr(this.addr);
    assertTrue(this.executor.init(opts));
}
 
Example #10
Source File: SnapshotExecutorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotDoSnapshotWithIntervalDist() throws Exception {
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setSnapshotLogIndexMargin(10);
    Mockito.when(this.node.getOptions()).thenReturn(nodeOptions);
    Mockito.when(this.fSMCaller.getLastAppliedIndex()).thenReturn(1L);
    this.executor.doSnapshot(null);
    this.executor.join();

    assertEquals(0, this.executor.getLastSnapshotTerm());
    assertEquals(0, this.executor.getLastSnapshotIndex());

}
 
Example #11
Source File: RaftServiceFactory.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Create and initialize a raft node with node options.
 * Throw {@link IllegalStateException} when fail to initialize.
 */
public static Node createAndInitRaftNode(final String groupId, final PeerId serverId, final NodeOptions opts) {
    final Node ret = createRaftNode(groupId, serverId);
    if (!ret.init(opts)) {
        throw new IllegalStateException("Fail to init node, please see the logs to find the reason.");
    }
    return ret;
}
 
Example #12
Source File: RaftGroupService.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
/**
 * Set node options.
 */
public void setNodeOptions(final NodeOptions nodeOptions) {
    if (this.started) {
        throw new IllegalStateException("Raft group service already started");
    }
    if (nodeOptions == null) {
        throw new IllegalArgumentException("Invalid node options.");
    }
    nodeOptions.validate();
    this.nodeOptions = nodeOptions;
}
 
Example #13
Source File: RaftGroupService.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public RaftGroupService(final String groupId, final PeerId serverId, final NodeOptions nodeOptions,
                        final RpcServer rpcServer, final boolean sharedRpcServer) {
    super();
    this.groupId = groupId;
    this.serverId = serverId;
    this.nodeOptions = nodeOptions;
    this.rpcServer = rpcServer;
    this.sharedRpcServer = sharedRpcServer;
}
 
Example #14
Source File: RemoteFileCopierTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Test
public void testInit() {
    Mockito.when(rpcService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
    assertTrue(copier.init("remote://localhost:8081/999", null, new SnapshotCopierOptions(rpcService, timerManager,
        new RaftOptions(), new NodeOptions())));
    assertEquals(999, copier.getReaderId());
    Assert.assertEquals("localhost", copier.getEndpoint().getIp());
    Assert.assertEquals(8081, copier.getEndpoint().getPort());
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
Source File: RaftServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * start raft server
 * @param raftServerConfig
 * @throws IOException
 */
public void start(RaftServerConfig raftServerConfig) throws IOException {

    FileUtils.forceMkdir(new File(dataPath));

    serverHandlers.add(new RaftServerHandler(this));
    serverHandlers.add(new RaftServerConnectionHandler());

    boltServer = new BoltServer(new URL(NetUtil.getLocalAddress().getHostAddress(),
        serverId.getPort()), serverHandlers);

    boltServer.initServer();

    RpcServer rpcServer = boltServer.getRpcServer();

    RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);

    this.fsm = ServiceStateMachine.getInstance();
    this.fsm.setLeaderProcessListener(leaderProcessListener);
    this.fsm.setFollowerProcessListener(followerProcessListener);

    NodeOptions nodeOptions = initNodeOptions(raftServerConfig);

    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
    //start
    this.node = this.raftGroupService.start();

    if (raftServerConfig.isEnableMetrics()) {
        ReporterUtils.startSlf4jReporter(raftServerConfig.getEnableMetricsReporterPeriod(),
            node.getNodeMetrics().getMetricRegistry(), raftServerConfig.getMetricsLogger());
    }

    RpcClient raftClient = ((AbstractBoltClientService) (((NodeImpl) node).getRpcService()))
        .getRpcClient();

    NotifyLeaderChangeHandler notifyLeaderChangeHandler = new NotifyLeaderChangeHandler(
        groupId, null);
    raftClient.registerUserProcessor(new SyncUserProcessorAdapter(notifyLeaderChangeHandler));
}
 
Example #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
Source File: NodeTest.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private NodeOptions createNodeOptionsWithSharedTimer() {
    final NodeOptions options = new NodeOptions();
    options.setSharedElectionTimer(true);
    options.setSharedVoteTimer(true);
    return options;
}
 
Example #27
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 #28
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();
    }
 
Example #29
Source File: PriorityElectionNodeOptions.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public NodeOptions getNodeOptions() {
    return nodeOptions;
}
 
Example #30
Source File: PriorityElectionNodeOptions.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
public void setNodeOptions(NodeOptions nodeOptions) {
    this.nodeOptions = nodeOptions;
}