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

The following examples show how to use org.apache.ratis.protocol.RaftPeerId#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));
  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 2
Source File: TestRetryCacheMetrics.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() {
  RaftServerImpl raftServer = mock(RaftServerImpl.class);

  RaftGroupId raftGroupId = RaftGroupId.randomId();
  RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
  RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId
      .valueOf(raftPeerId, raftGroupId);
  when(raftServer.getMemberId()).thenReturn(raftGroupMemberId);

  retryCache = new RetryCache(TimeDuration.valueOf(60, TimeUnit.SECONDS));
  when(raftServer.getRetryCache()).thenReturn(retryCache);

  RaftServerMetrics raftServerMetrics = RaftServerMetrics
      .getRaftServerMetrics(raftServer);
  ratisMetricRegistry = raftServerMetrics.getRegistry();
}
 
Example 3
Source File: TestRaftServerJmx.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) {
  final RaftServerMXBean mBean = new RaftServerMXBean() {
    @Override
    public String getId() { return null; }
    @Override
    public String getLeaderId() { return null; }
    @Override
    public long getCurrentTerm() { return 0; }
    @Override
    public String getGroupId() { return null; }
    @Override
    public String getRole() { return null; }
    @Override
    public List<String> getFollowers() { return null; }
  };
  final RaftPeerId id = RaftPeerId.valueOf(name);
  final RaftGroupId groupId = RaftGroupId.randomId();
  final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx);
  Assert.assertEquals(expectToSucceed, succeeded);
}
 
Example 4
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 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: TestRaftServerJmx.java    From ratis with Apache License 2.0 6 votes vote down vote up
static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) {
  final RaftServerMXBean mBean = new RaftServerMXBean() {
    @Override
    public String getId() { return null; }
    @Override
    public String getLeaderId() { return null; }
    @Override
    public long getCurrentTerm() { return 0; }
    @Override
    public String getGroupId() { return null; }
    @Override
    public String getRole() { return null; }
    @Override
    public List<String> getFollowers() { return null; }
  };
  final RaftPeerId id = RaftPeerId.valueOf(name);
  final RaftGroupId groupId = RaftGroupId.randomId();
  final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx);
  Assert.assertEquals(expectToSucceed, succeeded);
}
 
Example 7
Source File: TestLeaderElectionMetrics.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  RaftServerImpl raftServer = mock(RaftServerImpl.class);
  ServerState serverStateMock = mock(ServerState.class);
  when(raftServer.getState()).thenReturn(serverStateMock);
  when(serverStateMock.getLastLeaderElapsedTimeMs()).thenReturn(1000L);
  RaftGroupId raftGroupId = RaftGroupId.randomId();
  RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
  RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
  when(raftServer.getMemberId()).thenReturn(raftGroupMemberId);
  leaderElectionMetrics = LeaderElectionMetrics.getLeaderElectionMetrics(raftServer);
  ratisMetricRegistry = leaderElectionMetrics.getRegistry();
}
 
Example 8
Source File: OzoneManagerRatisServer.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Get the gorup info (peer role and leader peer id) from Ratis server and
 * update the OM server role.
 */
public void updateServerRole() {
  try {
    GroupInfoReply groupInfo = getGroupInfo();
    RoleInfoProto roleInfoProto = groupInfo.getRoleInfoProto();
    RaftPeerRole thisNodeRole = roleInfoProto.getRole();

    if (thisNodeRole.equals(RaftPeerRole.LEADER)) {
      setServerRole(thisNodeRole, raftPeerId);

    } else if (thisNodeRole.equals(RaftPeerRole.FOLLOWER)) {
      ByteString leaderNodeId = roleInfoProto.getFollowerInfo()
          .getLeaderInfo().getId().getId();
      // There may be a chance, here we get leaderNodeId as null. For
      // example, in 3 node OM Ratis, if 2 OM nodes are down, there will
      // be no leader.
      RaftPeerId leaderPeerId = null;
      if (leaderNodeId != null && !leaderNodeId.isEmpty()) {
        leaderPeerId = RaftPeerId.valueOf(leaderNodeId);
      }

      setServerRole(thisNodeRole, leaderPeerId);

    } else {
      setServerRole(thisNodeRole, null);

    }
  } catch (IOException e) {
    LOG.error("Failed to retrieve RaftPeerRole. Setting cached role to " +
        "{} and resetting leader info.", RaftPeerRole.UNRECOGNIZED, e);
    setServerRole(null, null);
  }
}
 
Example 9
Source File: GrpcService.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(RequestVoteRequestProto request)
    throws IOException {
  CodeInjectionForTesting.execute(GRPC_SEND_SERVER_REQUEST, getId(),
      null, request);

  final RaftPeerId target = RaftPeerId.valueOf(request.getServerRequest().getReplyId());
  return getProxies().getProxy(target).requestVote(request);
}
 
Example 10
Source File: TestRaftConfiguration.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testPeerConfiguration() {
  final RaftPeer[] peers = {
      new RaftPeer(RaftPeerId.valueOf("s0")),
      new RaftPeer(RaftPeerId.valueOf("s1")),
      new RaftPeer(RaftPeerId.valueOf("s0")),
  };
  testFailureCase("Duplicated peers", () -> {
    new PeerConfiguration(Arrays.asList(peers));
  }, IllegalArgumentException.class);
}
 
Example 11
Source File: TestPeerProxyMap.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10_000)
public void testCloseDeadLock() throws Exception {
  final PeerProxyMap<DummyProxy> map = new PeerProxyMap<>("test", DummyProxy::new);
  final RaftPeerId id = RaftPeerId.valueOf("s0");
  final RaftPeer peer = new RaftPeer(id);
  map.computeIfAbsent(peer);

  final DummyProxy proxy = map.getProxy(id);

  final Thread t = new Thread(() -> {
    // hold proxy lock and then getProxy(..) which requires resetLock
    synchronized (proxy) {
      LOG.info("Acquired lock");
      try {
        HUNDRED_MILLIS.sleep();
        LOG.info("Try getProxy");
        final DummyProxy newProxy = map.getProxy(id);
        Assert.assertNotSame(proxy, newProxy);
      } catch (Exception e) {
        setFirstException(e);
      }
      LOG.info("Will release lock");
    }
  });
  t.start();

  map.resetProxy(id); // hold resetLock and then call close() with requires proxy lock
  t.join();
}
 
Example 12
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void handlePipelineFailure(RaftGroupId groupId,
    RoleInfoProto roleInfoProto) {
  String msg;
  UUID datanode = RatisHelper.toDatanodeId(roleInfoProto.getSelf());
  RaftPeerId id = RaftPeerId.valueOf(roleInfoProto.getSelf().getId());
  switch (roleInfoProto.getRole()) {
  case CANDIDATE:
    msg = datanode + " is in candidate state for " +
        roleInfoProto.getCandidateInfo().getLastLeaderElapsedTimeMs() + "ms";
    break;
  case LEADER:
    StringBuilder sb = new StringBuilder();
    sb.append(datanode).append(" has not seen follower/s");
    for (RaftProtos.ServerRpcProto follower : roleInfoProto.getLeaderInfo()
        .getFollowerInfoList()) {
      if (follower.getLastRpcElapsedTimeMs() > nodeFailureTimeoutMs) {
        sb.append(" ").append(RatisHelper.toDatanodeId(follower.getId()))
            .append(" for ").append(follower.getLastRpcElapsedTimeMs())
            .append("ms");
      }
    }
    msg = sb.toString();
    break;
  default:
    LOG.error("unknown state: {}", roleInfoProto.getRole());
    throw new IllegalStateException("node" + id + " is in illegal role "
        + roleInfoProto.getRole());
  }

  triggerPipelineClose(groupId, msg,
      ClosePipelineInfo.Reason.PIPELINE_FAILED, false);
}
 
Example 13
Source File: TestGrpcServerMetrics.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  RaftServerImpl raftServer = mock(RaftServerImpl.class);
  ServerState serverStateMock = mock(ServerState.class);
  when(raftServer.getState()).thenReturn(serverStateMock);
  when(serverStateMock.getLastLeaderElapsedTimeMs()).thenReturn(1000L);
  raftGroupId = RaftGroupId.randomId();
  raftPeerId = RaftPeerId.valueOf("TestId");
  followerId = RaftPeerId.valueOf("FollowerId");
  RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
  when(raftServer.getMemberId()).thenReturn(raftGroupMemberId);
  grpcServerMetrics = new GrpcServerMetrics(raftGroupMemberId.toString());
  ratisMetricRegistry = grpcServerMetrics.getRegistry();
}
 
Example 14
Source File: NettyRpcService.java    From ratis with Apache License 2.0 5 votes vote down vote up
private RaftNettyServerReplyProto sendRaftNettyServerRequestProto(
    RaftRpcRequestProto request, RaftNettyServerRequestProto proto)
    throws IOException {
  final RaftPeerId id = RaftPeerId.valueOf(request.getReplyId());
  try {
    final NettyRpcProxy p = getProxies().getProxy(id);
    return p.send(request, proto);
  } catch (Exception e) {
    getProxies().handleException(id, e, false);
    throw e;
  }
}
 
Example 15
Source File: TestRetryPolicy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private static RaftClientRequest newRaftClientRequest(RaftClientRequest.Type type) {
  return new RaftClientRequest(ClientId.randomId(), RaftPeerId.valueOf("s0"), RaftGroupId.randomId(), 1L, type);
}
 
Example 16
Source File: LeaderElection.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private ResultAndTerm waitForResults(final long electionTerm, final int submitted,
    RaftConfiguration conf, Executor voteExecutor) throws InterruptedException {
  final Timestamp timeout = Timestamp.currentTime().addTimeMs(server.getRandomTimeoutMs());
  final Map<RaftPeerId, RequestVoteReplyProto> responses = new HashMap<>();
  final List<Exception> exceptions = new ArrayList<>();
  int waitForNum = submitted;
  Collection<RaftPeerId> votedPeers = new ArrayList<>();
  while (waitForNum > 0 && shouldRun(electionTerm)) {
    final TimeDuration waitTime = timeout.elapsedTime().apply(n -> -n);
    if (waitTime.isNonPositive()) {
      return logAndReturn(Result.TIMEOUT, responses, exceptions, -1);
    }

    try {
      final Future<RequestVoteReplyProto> future = voteExecutor.poll(waitTime);
      if (future == null) {
        continue; // poll timeout, continue to return Result.TIMEOUT
      }

      final RequestVoteReplyProto r = future.get();
      final RaftPeerId replierId = RaftPeerId.valueOf(r.getServerReply().getReplyId());
      final RequestVoteReplyProto previous = responses.putIfAbsent(replierId, r);
      if (previous != null) {
        if (LOG.isWarnEnabled()) {
          LOG.warn("{} received duplicated replies from {}, the 2nd reply is ignored: 1st={}, 2nd={}",
              this, replierId, ServerProtoUtils.toString(previous), ServerProtoUtils.toString(r));
        }
        continue;
      }
      if (r.getShouldShutdown()) {
        return logAndReturn(Result.SHUTDOWN, responses, exceptions, -1);
      }
      if (r.getTerm() > electionTerm) {
        return logAndReturn(Result.DISCOVERED_A_NEW_TERM, responses,
            exceptions, r.getTerm());
      }
      if (r.getServerReply().getSuccess()) {
        votedPeers.add(replierId);
        if (conf.hasMajority(votedPeers, server.getId())) {
          return logAndReturn(Result.PASSED, responses, exceptions, -1);
        }
      }
    } catch(ExecutionException e) {
      LogUtils.infoOrTrace(LOG, () -> this + " got exception when requesting votes", e);
      exceptions.add(e);
    }
    waitForNum--;
  }
  // received all the responses
  return logAndReturn(Result.REJECTED, responses, exceptions, -1);
}
 
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: ProtoUtils.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static RaftPeer toRaftPeer(RaftPeerProto p) {
  return new RaftPeer(RaftPeerId.valueOf(p.getId()), p.getAddress());
}
 
Example 19
Source File: LogServer.java    From 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();
    properties.set("raft.client.rpc.request.timeout", "100000");
    GrpcConfigKeys.Server.setPort(properties, opts.getPort());
    NettyConfigKeys.Server.setPort(properties, opts.getPort());
    InetSocketAddress addr = new InetSocketAddress(opts.getHost(), opts.getPort());
    if(opts.getWorkingDir() != null) {
        RaftServerConfigKeys.setStorageDirs(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);
    raftServer = RaftServer.newBuilder()
            .setStateMachineRegistry(new StateMachine.Registry() {
                private final StateMachine managementMachine = new ManagementStateMachine();
                private final StateMachine logMachine  = new LogStateMachine();
                @Override
                public StateMachine apply(RaftGroupId raftGroupId) {
                    // TODO this looks wrong. Why isn't this metaGroupId?
                    if(raftGroupId.equals(logServerGroupId)) {
                        return managementMachine;
                    }
                    return logMachine;
                }
            })
            .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());
}
 
Example 20
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static RaftPeer toRaftPeer(RaftPeerProto p) {
  return new RaftPeer(RaftPeerId.valueOf(p.getId()), p.getAddress());
}