Java Code Examples for org.apache.ratis.protocol.RaftGroup#valueOf()

The following examples show how to use org.apache.ratis.protocol.RaftGroup#valueOf() . 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 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();

  RaftPeer[] peers = getPeers();
  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.setStorageDirs(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(raftGroupId)), peers);
  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: GroupManagementBaseTest.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testGroupAlreadyExists() throws Exception {
  final MiniRaftCluster cluster = getCluster(1);
  cluster.start();
  final RaftPeer peer = cluster.getPeers().get(0);
  final RaftPeerId peerId = peer.getId();
  final RaftGroup group = RaftGroup.valueOf(cluster.getGroupId(), peer);
  try (final RaftClient client = cluster.createClient()) {
    Assert.assertEquals(group, cluster.getRaftServerImpl(peerId).getGroup());
    try {
      client.groupAdd(group, peer.getId());
    } catch (IOException ex) {
      // HadoopRPC throws RemoteException, which makes it hard to check if
      // the exception is instance of AlreadyExistsException
      Assert.assertTrue(ex.toString().contains(AlreadyExistsException.class.getCanonicalName()));
    }
    Assert.assertEquals(group, cluster.getRaftServerImpl(peerId).getGroup());
    cluster.shutdown();
  }
}
 
Example 3
Source File: Server.java    From 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();

  RaftPeer[] peers = getPeers();
  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.setStorageDirs(properties, Collections.singletonList(storageDir));
  StateMachine stateMachine = new ArithmeticStateMachine();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), peers);
  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 4
Source File: Client.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
  RaftProperties raftProperties = new RaftProperties();

  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)),
      parsePeers(peers));

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);


}
 
Example 5
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 6
Source File: Client.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  int raftSegmentPreallocatedSize = 1024 * 1024 * 1024;
  RaftProperties raftProperties = new RaftProperties();
  RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC);
  GrpcConfigKeys.setMessageSizeMax(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties,
      SizeInBytes.valueOf(1 * 1024 * 1024 * 1024));

  RaftServerConfigKeys.Log.setMaxCachedSegmentNum(raftProperties, 2);

  RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties,
      TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS));
  RaftClientConfigKeys.Async.setSchedulerThreads(raftProperties, 10);
  RaftClientConfigKeys.Async.setMaxOutstandingRequests(raftProperties, 1000);


  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)),
      parsePeers(peers));

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);
}
 
Example 7
Source File: TestStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testStateMachineRegistry() throws Throwable {
  final Map<RaftGroupId, StateMachine> registry = new ConcurrentHashMap<>();
  registry.put(RaftGroupId.randomId(), new SimpleStateMachine4Testing());
  registry.put(RaftGroupId.randomId(), new SMTransactionContext());

  try(MiniRaftClusterWithSimulatedRpc cluster = newCluster(0)) {
    cluster.setStateMachineRegistry(registry::get);

    final RaftPeerId id = RaftPeerId.valueOf("s0");
    cluster.putNewServer(id, null, true);
    cluster.start();

    for(RaftGroupId gid : registry.keySet()) {
      final RaftGroup newGroup = RaftGroup.valueOf(gid, cluster.getPeers());
      LOG.info("add new group: " + newGroup);
      final RaftClient client = cluster.createClient(newGroup);
      for(RaftPeer p : newGroup.getPeers()) {
        client.groupAdd(newGroup, p.getId());
      }
    }

    final RaftServerProxy proxy = cluster.getServer(id);
    for(Map.Entry<RaftGroupId, StateMachine> e: registry.entrySet()) {
      final RaftServerImpl impl = RaftServerTestUtil.getRaftServerImpl(proxy, e.getKey());
      Assert.assertSame(e.getValue(), impl.getStateMachine());
    }
  }
}
 
Example 8
Source File: RaftAsyncExceptionTests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void runTestGroupMismatchException(CLUSTER cluster) throws Exception {
  // send a message to make sure the cluster is working
  try(RaftClient client = cluster.createClient()) {
    final RaftClientReply reply = client.sendAsync(new SimpleMessage("first")).get();
    Assert.assertTrue(reply.isSuccess());
  }

  // create another group
  final RaftGroup clusterGroup = cluster.getGroup();
  final RaftGroup anotherGroup = RaftGroup.valueOf(RaftGroupId.randomId(), clusterGroup.getPeers());
  Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());

  // create another client using another group
  final SimpleMessage[] messages = SimpleMessage.create(5);
  try(RaftClient client = cluster.createClient(anotherGroup)) {
    // send a few messages
    final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
    for(SimpleMessage m : messages) {
      futures.add(client.sendAsync(m));
    }
    Assert.assertEquals(messages.length, futures.size());

    // check replies
    final Iterator<CompletableFuture<RaftClientReply>> i = futures.iterator();
    testFailureCase("First reply is GroupMismatchException",
        () -> i.next().get(),
        ExecutionException.class, GroupMismatchException.class);
    for(; i.hasNext(); ) {
      testFailureCase("Following replies are AlreadyClosedException caused by GroupMismatchException",
          () -> i.next().get(),
          ExecutionException.class, AlreadyClosedException.class, GroupMismatchException.class);
    }
  }
}
 
Example 9
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static RaftGroup initRaftGroup(Collection<String> ids) {
  final RaftPeer[] peers = ids.stream()
      .map(RaftPeerId::valueOf)
      .map(id -> new RaftPeer(id, NetUtils.createLocalServerAddress()))
      .toArray(RaftPeer[]::new);
  return RaftGroup.valueOf(RaftGroupId.randomId(), peers);
}
 
Example 10
Source File: Client.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  int raftSegmentPreallocatedSize = 1024 * 1024 * 1024;
  RaftProperties raftProperties = new RaftProperties();
  RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC);
  GrpcConfigKeys.setMessageSizeMax(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties,
      SizeInBytes.valueOf(1 * 1024 * 1024 * 1024));

  RaftServerConfigKeys.Log.setSegmentCacheNumMax(raftProperties, 2);

  RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties,
      TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS));
  RaftClientConfigKeys.Async.setOutstandingRequestsMax(raftProperties, 1000);


  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);
}
 
Example 11
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 12
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static RaftGroup initRaftGroup(Collection<String> ids) {
  final RaftPeer[] peers = ids.stream()
      .map(RaftPeerId::valueOf)
      .map(id -> new RaftPeer(id, NetUtils.createLocalServerAddress()))
      .toArray(RaftPeer[]::new);
  return RaftGroup.valueOf(RaftGroupId.randomId(), peers);
}
 
Example 13
Source File: LeaderAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void configureGroup() throws IOException {
  ClientId clientId = ClientId.randomId();

  RaftGroupId groupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));
  RaftPeerId peerId =
      RaftPeerId.getRaftPeerId(serverId);

  RaftGroup group = RaftGroup.valueOf(groupId,
      new RaftPeer(RaftPeerId.valueOf(serverId), serverAddress),
      new RaftPeer(RaftPeerId.valueOf(FAKE_FOLLOWER_ID1),
          FAKE_LEADER_ADDDRESS1),
      new RaftPeer(RaftPeerId.valueOf(FAKE_FOLLOWER_ID1),
          FAKE_LEADER_ADDDRESS2));
  RaftClient client = RaftClient.newBuilder()
      .setClientId(clientId)
      .setProperties(new RaftProperties(true))
      .setRaftGroup(group)
      .build();

  RaftClientReply raftClientReply = client.groupAdd(group, peerId);

  LOG.info(
      "Group is configured in the RAFT server (with two fake leader leader)"
          + ": {}",
      raftClientReply);
}
 
Example 14
Source File: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Configure raft group before using raft service.
 */
private void configureGroup() throws IOException {

  ClientId clientId = ClientId.randomId();

  RaftGroupId groupId = RaftGroupId
      .valueOf(UUID.fromString(pipelineId));

  RaftPeerId peerId =
      RaftPeerId.getRaftPeerId(serverId);

  RaftGroup group = RaftGroup.valueOf(groupId,
      new RaftPeer(RaftPeerId.valueOf(serverId), serverAddress),
      new RaftPeer(RaftPeerId.valueOf(FAKE_LEADER_ID),
          FAKE_LEADER_ADDDRESS));
  RaftClient client = RaftClient.newBuilder()
      .setClientId(clientId)
      .setProperties(new RaftProperties(true))
      .setRaftGroup(group)
      .build();

  RaftClientReply raftClientReply = client.groupAdd(group, peerId);

  LOG.info(
      "Group is configured in the RAFT server (one follower, one fake "
          + "leader): {}", raftClientReply);
}
 
Example 15
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
public static RaftGroup newRaftGroup(RaftGroupId groupId,
    Collection<DatanodeDetails> peers) {
  final List<RaftPeer> newPeers = peers.stream()
      .map(RatisHelper::toRaftPeer)
      .collect(Collectors.toList());
  return peers.isEmpty() ? RaftGroup.valueOf(groupId, Collections.emptyList())
      : RaftGroup.valueOf(groupId, newPeers);
}
 
Example 16
Source File: ProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static RaftGroup toRaftGroup(RaftGroupProto proto) {
  return RaftGroup.valueOf(toRaftGroupId(proto.getGroupId()), toRaftPeers(proto.getPeersList()));
}
 
Example 17
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();
}
 
Example 18
Source File: OzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an OM Ratis server.
 * @param conf configuration
 * @param om the OM instance starting the ratis server
 * @param raftGroupIdStr raft group id string
 * @param localRaftPeerId raft peer id of this Ratis server
 * @param addr address of the ratis server
 * @param raftPeers peer nodes in the raft ring
 * @throws IOException
 */
private OzoneManagerRatisServer(ConfigurationSource conf,
    OzoneManager om,
    String raftGroupIdStr, RaftPeerId localRaftPeerId,
    InetSocketAddress addr, List<RaftPeer> raftPeers)
    throws IOException {
  this.ozoneManager = om;
  this.omRatisAddress = addr;
  this.port = addr.getPort();
  RaftProperties serverProperties = newRaftProperties(conf);

  this.raftPeerId = localRaftPeerId;
  this.raftGroupId = RaftGroupId.valueOf(
      getRaftGroupIdFromOmServiceId(raftGroupIdStr));
  this.raftGroup = RaftGroup.valueOf(raftGroupId, raftPeers);

  StringBuilder raftPeersStr = new StringBuilder();
  for (RaftPeer peer : raftPeers) {
    raftPeersStr.append(", ").append(peer.getAddress());
  }
  LOG.info("Instantiating OM Ratis server with GroupID: {} and " +
      "Raft Peers: {}", raftGroupIdStr, raftPeersStr.toString().substring(2));

  this.omStateMachine = getStateMachine(conf);

  this.server = RaftServer.newBuilder()
      .setServerId(this.raftPeerId)
      .setGroup(this.raftGroup)
      .setProperties(serverProperties)
      .setStateMachine(omStateMachine)
      .build();

  // Run a scheduler to check and update the server role on the leader
  // periodically
  this.scheduledRoleChecker = Executors.newSingleThreadScheduledExecutor();
  this.scheduledRoleChecker.scheduleWithFixedDelay(new Runnable() {
    @Override
    public void run() {
      // Run this check only on the leader OM
      if (cachedPeerRole.isPresent() &&
          cachedPeerRole.get() == RaftPeerRole.LEADER) {
        updateServerRole();
      }
    }
  }, roleCheckInitialDelayMs, roleCheckIntervalMs, TimeUnit.MILLISECONDS);
}
 
Example 19
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static RaftGroup toRaftGroup(RaftGroupProto proto) {
  return RaftGroup.valueOf(toRaftGroupId(proto.getGroupId()), toRaftPeerArray(proto.getPeersList()));
}
 
Example 20
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
private static RaftGroup newRaftGroup(Collection<RaftPeer> peers) {
  return peers.isEmpty()? emptyRaftGroup()
      : RaftGroup.valueOf(DUMMY_GROUP_ID, peers);
}