Java Code Examples for org.apache.ratis.server.RaftServerConfigKeys#setStorageDir()

The following examples show how to use org.apache.ratis.server.RaftServerConfigKeys#setStorageDir() . 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: Server.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftPeerId peerId = RaftPeerId.valueOf(id);
  RaftProperties properties = new RaftProperties();

  final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);
  properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
  RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
  StateMachine stateMachine = new ArithmeticStateMachine();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());
  RaftServer raftServer = RaftServer.newBuilder()
      .setServerId(RaftPeerId.valueOf(id))
      .setStateMachine(stateMachine).setProperties(properties)
      .setGroup(raftGroup)
      .build();
  raftServer.start();

  for(; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED;) {
    TimeUnit.SECONDS.sleep(1);
  }
}
 
Example 2
Source File: TestLogSegment.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  RaftProperties properties = new RaftProperties();
  storageDir = getTestDir();
  RaftServerConfigKeys.setStorageDir(properties,  Collections.singletonList(storageDir));
  this.segmentMaxSize =
      RaftServerConfigKeys.Log.segmentSizeMax(properties).getSize();
  this.preallocatedSize =
      RaftServerConfigKeys.Log.preallocatedSize(properties).getSize();
  this.bufferSize =
      RaftServerConfigKeys.Log.writeBufferSize(properties).getSizeInt();
}
 
Example 3
Source File: TestSegmentedRaftLog.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  storageDir = getTestDir();
  properties = new RaftProperties();
  RaftServerConfigKeys.setStorageDir(properties,  Collections.singletonList(storageDir));
  storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR);
  this.segmentMaxSize =
      RaftServerConfigKeys.Log.segmentSizeMax(properties).getSize();
  this.preallocatedSize =
      RaftServerConfigKeys.Log.preallocatedSize(properties).getSize();
  this.bufferSize =
      RaftServerConfigKeys.Log.writeBufferSize(properties).getSizeInt();
}
 
Example 4
Source File: TestRaftLogReadWrite.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  storageDir = getTestDir();
  RaftProperties properties = new RaftProperties();
  RaftServerConfigKeys.setStorageDir(properties,  Collections.singletonList(storageDir));
  this.segmentMaxSize =
      RaftServerConfigKeys.Log.segmentSizeMax(properties).getSize();
  this.preallocatedSize =
      RaftServerConfigKeys.Log.preallocatedSize(properties).getSize();
  this.bufferSize =
      RaftServerConfigKeys.Log.writeBufferSize(properties).getSizeInt();
}
 
Example 5
Source File: TestRaftServerWithGrpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
void runTestServerRestartOnException(MiniRaftClusterWithGrpc cluster) throws Exception {
  final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
  final RaftPeerId leaderId = leader.getId();

  final RaftProperties p = getProperties();
  GrpcConfigKeys.Server.setPort(p, leader.getServerRpc().getInetSocketAddress().getPort());

  // Create a raft server proxy with server rpc bound to a different address
  // compared to leader. This helps in locking the raft storage directory to
  // be used by next raft server proxy instance.
  final StateMachine stateMachine = cluster.getLeader().getStateMachine();
  RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
  ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, p, null);
  // Close the server rpc for leader so that new raft server can be bound to it.
  cluster.getLeader().getServerRpc().close();

  // Create a raft server proxy with server rpc bound to same address as
  // the leader. This step would fail as the raft storage has been locked by
  // the raft server proxy created earlier. Raft server proxy should close
  // the rpc server on failure.
  RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
  testFailureCase("start a new server with the same address",
      () -> ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, p, null).start(),
      IOException.class, OverlappingFileLockException.class);
  // Try to start a raft server rpc at the leader address.
  cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId));
}
 
Example 6
Source File: Server.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  JVMMetrics.initJvmMetrics(TimeDuration.valueOf(10, TimeUnit.SECONDS));

  RaftPeerId peerId = RaftPeerId.valueOf(id);
  RaftProperties properties = new RaftProperties();

  final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);
  properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
  RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
  ConfUtils.setFile(properties::setFile, FileStoreCommon.STATEMACHINE_DIR_KEY,
      storageDir);
  StateMachine stateMachine = new FileStoreStateMachine(properties);

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());
  RaftServer raftServer = RaftServer.newBuilder()
      .setServerId(RaftPeerId.valueOf(id))
      .setStateMachine(stateMachine).setProperties(properties)
      .setGroup(raftGroup)
      .build();

  raftServer.start();

  for (; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED; ) {
    TimeUnit.SECONDS.sleep(1);
  }
}
 
Example 7
Source File: CounterServer.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
  if (args.length < 1) {
    System.err.println("Usage: java -cp *.jar org.apache.ratis.examples.counter.server.CounterServer {serverIndex}");
    System.err.println("{serverIndex} could be 1, 2 or 3");
    System.exit(1);
  }

  //find current peer object based on application parameter
  RaftPeer currentPeer =
      CounterCommon.PEERS.get(Integer.parseInt(args[0]) - 1);

  //create a property object
  RaftProperties properties = new RaftProperties();

  //set the storage directory (different for each peer) in RaftProperty object
  File raftStorageDir = new File("./" + currentPeer.getId().toString());
  RaftServerConfigKeys.setStorageDir(properties,
      Collections.singletonList(raftStorageDir));

  //set the port which server listen to in RaftProperty object
  final int port = NetUtils.createSocketAddr(currentPeer.getAddress()).getPort();
  GrpcConfigKeys.Server.setPort(properties, port);

  //create the counter state machine which hold the counter value
  CounterStateMachine counterStateMachine = new CounterStateMachine();

  //create and start the Raft server
  RaftServer server = RaftServer.newBuilder()
      .setGroup(CounterCommon.RAFT_GROUP)
      .setProperties(properties)
      .setServerId(currentPeer.getId())
      .setStateMachine(counterStateMachine)
      .build();
  server.start();

  //exit when any input entered
  Scanner scanner = new Scanner(System.in);
  scanner.nextLine();
  server.close();
}
 
Example 8
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private RaftServerProxy newRaftServer(RaftPeerId id, RaftGroup group, boolean format) {
  LOG.info("newRaftServer: {}, {}, format? {}", id, group, format);
  try {
    final File dir = getStorageDir(id);
    if (format) {
      FileUtils.deleteFully(dir);
      LOG.info("Formatted directory {}", dir);
    }
    final RaftProperties prop = new RaftProperties(properties);
    RaftServerConfigKeys.setStorageDir(prop, Collections.singletonList(dir));
    return newRaftServer(id, getStateMachineRegistry(properties), group, prop);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 9
Source File: TestCacheEviction.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Test
public void testEvictionInSegmentedLog() throws Exception {
  final RaftProperties prop = new RaftProperties();
  prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
      SimpleStateMachine4Testing.class, StateMachine.class);
  RaftServerConfigKeys.Log.setSegmentSizeMax(prop, SizeInBytes.valueOf("8KB"));
  RaftServerConfigKeys.Log.setPreallocatedSize(prop, SizeInBytes.valueOf("8KB"));
  final RaftPeerId peerId = RaftPeerId.valueOf("s0");
  final RaftGroupId groupId = RaftGroupId.randomId();
  final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
  final int maxCachedNum = RaftServerConfigKeys.Log.segmentCacheNumMax(prop);

  File storageDir = getTestDir();
  RaftServerConfigKeys.setStorageDir(prop,  Collections.singletonList(storageDir));
  RaftStorage storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR);

  RaftServerImpl server = Mockito.mock(RaftServerImpl.class);
  ServerState state = Mockito.mock(ServerState.class);
  Mockito.when(server.getState()).thenReturn(state);
  Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{});
  Mockito.when(state.getLastAppliedIndex()).thenReturn(0L);

  SegmentedRaftLog raftLog = new SegmentedRaftLog(memberId, server, storage, -1, prop);
  raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
  List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(0, maxCachedNum, 7, 0);
  LogEntryProto[] entries = generateEntries(slist);
  raftLog.append(entries).forEach(CompletableFuture::join);

  // check the current cached segment number: the last segment is still open
  Assert.assertEquals(maxCachedNum - 1,
      raftLog.getRaftLogCache().getCachedSegmentNum());

  Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{21, 40, 40});
  Mockito.when(state.getLastAppliedIndex()).thenReturn(35L);
  slist = TestSegmentedRaftLog.prepareRanges(maxCachedNum, maxCachedNum + 2, 7, 7 * maxCachedNum);
  entries = generateEntries(slist);
  raftLog.append(entries).forEach(CompletableFuture::join);

  // check the cached segment number again. since the slowest follower is on
  // index 21, the eviction should happen and evict 3 segments
  Assert.assertEquals(maxCachedNum + 1 - 3,
      raftLog.getRaftLogCache().getCachedSegmentNum());
}
 
Example 10
Source File: MetadataServer.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public void start() throws IOException  {
    final ServerOpts opts = getServerOpts();
    if (opts.getHost() == null) {
        opts.setHost(LogServiceUtils.getHostName());
    }
    this.lifeCycle = new LifeCycle(this.id);
    RaftProperties properties = new RaftProperties();
    if(opts.getWorkingDir() != null) {
        RaftServerConfigKeys.setStorageDir(properties,
          Collections.singletonList(new File(opts.getWorkingDir())));
    } else {
      String path = getConfig().get(Constants.META_SERVER_WORKDIR_KEY);
      if (path != null) {
        RaftServerConfigKeys.setStorageDir(properties,
          Collections.singletonList(new File(path)));
      }
    }

    // Set properties common to all log service state machines
    setRaftProperties(properties);
    long failureDetectionPeriod = getConfig().
            getLong(Constants.LOG_SERVICE_PEER_FAILURE_DETECTION_PERIOD_KEY,
            Constants.DEFAULT_PEER_FAILURE_DETECTION_PERIOD);
    Set<RaftPeer> peers = getPeersFromQuorum(opts.getMetaQuorum());
    RaftGroupId raftMetaGroupId = RaftGroupId.valueOf(opts.getMetaGroupId());
    RaftGroup metaGroup = RaftGroup.valueOf(raftMetaGroupId, peers);
    metaStateMachine = new MetaStateMachine(raftMetaGroupId, RaftGroupId.valueOf(opts.getLogServerGroupId()),
            failureDetectionPeriod);

    // Make sure that we aren't setting any invalid/harmful properties
    validateRaftProperties(properties);

    server = RaftServer.newBuilder()
            .setGroup(metaGroup)
            .setServerId(RaftPeerId.valueOf(id))
            .setStateMachineRegistry(raftGroupId -> {
                if(raftGroupId.equals(META_GROUP_ID)) {
                    return metaStateMachine;
                }
                return null;
            })
            .setProperties(properties).build();
    lifeCycle.startAndTransition(() -> {
        server.start();
    }, IOException.class);
}
 
Example 11
Source File: LogServer.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public void start() throws IOException {
    final ServerOpts opts = getServerOpts();
    Set<RaftPeer> peers = LogServiceUtils.getPeersFromQuorum(opts.getMetaQuorum());
    RaftProperties properties = new RaftProperties();

    // Set properties for the log server state machine
    setRaftProperties(properties);

    InetSocketAddress addr = new InetSocketAddress(opts.getHost(), opts.getPort());
    if(opts.getWorkingDir() != null) {
        RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(new File(opts.getWorkingDir())));
    }
    String id = opts.getHost() +"_" +  opts.getPort();
    RaftPeer peer = new RaftPeer(RaftPeerId.valueOf(id), addr);
    final RaftGroupId logServerGroupId = RaftGroupId.valueOf(opts.getLogServerGroupId());
    RaftGroup all = RaftGroup.valueOf(logServerGroupId, peer);
    RaftGroup meta = RaftGroup.valueOf(RaftGroupId.valueOf(opts.getMetaGroupId()), peers);

    // Make sure that we aren't setting any invalid/harmful properties
    validateRaftProperties(properties);

    raftServer = RaftServer.newBuilder()
            .setStateMachineRegistry(new StateMachine.Registry() {
                @Override
                public StateMachine apply(RaftGroupId raftGroupId) {
                    // TODO this looks wrong. Why isn't this metaGroupId?
                    if(raftGroupId.equals(logServerGroupId)) {
                        return new ManagementStateMachine();
                    }
                    return new LogStateMachine(properties);
                }
            })
            .setProperties(properties)
            .setServerId(RaftPeerId.valueOf(id))
            .setGroup(all)
            .build();
    raftServer.start();

    metaClient = RaftClient.newBuilder()
            .setRaftGroup(meta)
            .setClientId(ClientId.randomId())
            .setProperties(properties)
            .build();
    metaClient.send(() -> MetaServiceProtoUtil.toPingRequestProto(peer).toByteString());
    daemon = new Daemon(new HeartbeatSender(new RaftPeer(raftServer.getId())),
            "heartbeat-Sender"+raftServer.getId());
    daemon.start();
}