org.apache.ratis.protocol.RaftPeer Java Examples

The following examples show how to use org.apache.ratis.protocol.RaftPeer. 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: RatisPipelineUtils.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Sends ratis command to destroy pipeline on the given datanode.
 *
 * @param dn         - Datanode on which pipeline needs to be destroyed
 * @param pipelineID - ID of pipeline to be destroyed
 * @param ozoneConf  - Ozone configuration
 * @param grpcTlsConfig - grpc tls configuration
 * @throws IOException
 */
static void destroyPipeline(DatanodeDetails dn, PipelineID pipelineID,
    ConfigurationSource ozoneConf, GrpcTlsConfig grpcTlsConfig)
    throws IOException {
  final String rpcType = ozoneConf
      .get(ScmConfigKeys.DFS_CONTAINER_RATIS_RPC_TYPE_KEY,
          ScmConfigKeys.DFS_CONTAINER_RATIS_RPC_TYPE_DEFAULT);
  final RetryPolicy retryPolicy = RatisHelper.createRetryPolicy(ozoneConf);
  final RaftPeer p = RatisHelper.toRaftPeer(dn);
  try(RaftClient client = RatisHelper
      .newRaftClient(SupportedRpcType.valueOfIgnoreCase(rpcType), p,
          retryPolicy, grpcTlsConfig, ozoneConf)) {
    client.groupRemove(RaftGroupId.valueOf(pipelineID.getId()),
        true, p.getId());
  }
}
 
Example #2
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * prepare the peer list when removing some peers from the conf
 */
public PeerChanges removePeers(int number, boolean removeLeader,
    Collection<RaftPeer> excluded) {
  Collection<RaftPeer> peers = new ArrayList<>(group.getPeers());
  List<RaftPeer> removedPeers = new ArrayList<>(number);
  if (removeLeader) {
    final RaftPeer leader = toRaftPeer(getLeader());
    assert !excluded.contains(leader);
    peers.remove(leader);
    removedPeers.add(leader);
  }
  List<RaftServerImpl> followers = getFollowers();
  for (int i = 0, removed = 0; i < followers.size() &&
      removed < (removeLeader ? number - 1 : number); i++) {
    RaftPeer toRemove = toRaftPeer(followers.get(i));
    if (!excluded.contains(toRemove)) {
      peers.remove(toRemove);
      removedPeers.add(toRemove);
      removed++;
    }
  }
  RaftPeer[] p = peers.toArray(new RaftPeer[peers.size()]);
  group = RaftGroup.valueOf(group.getGroupId(), p);
  return new PeerChanges(p, new RaftPeer[0],
      removedPeers.toArray(new RaftPeer[removedPeers.size()]));
}
 
Example #3
Source File: ProtoUtils.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static Iterable<RaftPeerProto> toRaftPeerProtos(
    final Collection<RaftPeer> peers) {
  return () -> new Iterator<RaftPeerProto>() {
    private final Iterator<RaftPeer> i = peers.iterator();

    @Override
    public boolean hasNext() {
      return i.hasNext();
    }

    @Override
    public RaftPeerProto next() {
      return i.next().getRaftPeerProto();
    }
  };
}
 
Example #4
Source File: MiniRaftCluster.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * prepare the peer list when removing some peers from the conf
 */
public PeerChanges removePeers(int number, boolean removeLeader,
    Collection<RaftPeer> excluded) throws InterruptedException {
  Collection<RaftPeer> peers = new ArrayList<>(group.getPeers());
  List<RaftPeer> removedPeers = new ArrayList<>(number);
  if (removeLeader) {
    final RaftPeer leader = toRaftPeer(RaftTestUtil.waitForLeader(this));
    Preconditions.assertTrue(!excluded.contains(leader));
    peers.remove(leader);
    removedPeers.add(leader);
  }
  List<RaftServerImpl> followers = getFollowers();
  for (int i = 0, removed = 0; i < followers.size() &&
      removed < (removeLeader ? number - 1 : number); i++) {
    RaftPeer toRemove = toRaftPeer(followers.get(i));
    if (!excluded.contains(toRemove)) {
      peers.remove(toRemove);
      removedPeers.add(toRemove);
      removed++;
    }
  }
  final RaftPeer[] p = peers.toArray(RaftPeer.emptyArray());
  group = RaftGroup.valueOf(group.getGroupId(), p);
  return new PeerChanges(p, RaftPeer.emptyArray(), removedPeers.toArray(RaftPeer.emptyArray()));
}
 
Example #5
Source File: ProtoUtils.java    From ratis with Apache License 2.0 6 votes vote down vote up
static Iterable<RaftPeerProto> toRaftPeerProtos(
    final Collection<RaftPeer> peers) {
  return () -> new Iterator<RaftPeerProto>() {
    final Iterator<RaftPeer> i = peers.iterator();

    @Override
    public boolean hasNext() {
      return i.hasNext();
    }

    @Override
    public RaftPeerProto next() {
      return i.next().getRaftPeerProto();
    }
  };
}
 
Example #6
Source File: TestLogAppenderMetrics.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  RaftGroupId raftGroupId = RaftGroupId.randomId();
  raftPeerId = RaftPeerId.valueOf("TestId");
  RaftPeer raftPeer = new RaftPeer(raftPeerId);
  RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
  LogAppender logAppender = mock(LogAppender.class);
  followerInfo = new TestFollowerInfo(raftGroupMemberId, raftPeer, Timestamp.currentTime(), 100L, true, 1000);
  when(logAppender.getFollower()).thenReturn(followerInfo);
  LogAppenderMetrics logAppenderMetrics = new LogAppenderMetrics(raftGroupMemberId);
  ratisMetricRegistry = logAppenderMetrics.getRegistry();
  logAppenderMetrics.addFollowerGauges(followerInfo);
}
 
Example #7
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 #8
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 #9
Source File: Server.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * @return the peer with the given id if it is in this group; otherwise, return null.
 */
public RaftPeer getPeer(RaftPeerId id) {
  Objects.requireNonNull(id, "id == null");
  for (RaftPeer p : getPeers()) {
    if (id.equals(p.getId())) {
      return p;
    }
  }
  throw new IllegalArgumentException("Raft peer id " + id + " is not part of the raft group definitions " + peers);
}
 
Example #10
Source File: PeerConfiguration.java    From ratis with Apache License 2.0 5 votes vote down vote up
List<RaftPeer> getOtherPeers(RaftPeerId selfId) {
  List<RaftPeer> others = new ArrayList<>();
  for (Map.Entry<RaftPeerId, RaftPeer> entry : peers.entrySet()) {
    if (!selfId.equals(entry.getValue().getId())) {
      others.add(entry.getValue());
    }
  }
  return others;
}
 
Example #11
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 #12
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 5 votes vote down vote up
public void setConfiguration(RaftPeer... peers) throws IOException {
  try(RaftClient client = createClient()) {
    LOG.info("Start changing the configuration: {}", Arrays.asList(peers));
    final RaftClientReply reply = client.setConfiguration(peers);
    Preconditions.assertTrue(reply.isSuccess());
  }
}
 
Example #13
Source File: RatisHelper.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
public static RaftClient newRaftClient(RpcType rpcType, RaftPeer leader,
    RetryPolicy retryPolicy, GrpcTlsConfig tlsConfig,
    ConfigurationSource configuration) {
  return newRaftClient(rpcType, leader.getId(),
      newRaftGroup(Collections.singletonList(leader)), retryPolicy,
      tlsConfig, configuration);
}
 
Example #14
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 #15
Source File: PeerProxyMap.java    From ratis with Apache License 2.0 5 votes vote down vote up
public void resetProxy(RaftPeerId id) {
  LOG.debug("{}: reset proxy for {}", name, id );
  synchronized (resetLock) {
    final PeerAndProxy pp = peers.remove(id);
    final RaftPeer peer = pp.getPeer();
    pp.close();
    computeIfAbsent(peer);
  }
}
 
Example #16
Source File: Server.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * @return the peer with the given id if it is in this group; otherwise, return null.
 */
public RaftPeer getPeer(RaftPeerId id) {
  Objects.requireNonNull(id, "id == null");
  for (RaftPeer p : getPeers()) {
    if (id.equals(p.getId())) {
      return p;
    }
  }
  throw new IllegalArgumentException("Raft peer id " + id + " is not part of the raft group definitions " + peers);
}
 
Example #17
Source File: PeerConfiguration.java    From ratis with Apache License 2.0 5 votes vote down vote up
PeerConfiguration(Iterable<RaftPeer> peers) {
  Objects.requireNonNull(peers);
  Map<RaftPeerId, RaftPeer> map = new HashMap<>();
  for(RaftPeer p : peers) {
    map.put(p.getId(), p);
  }
  this.peers = Collections.unmodifiableMap(map);
}
 
Example #18
Source File: ServerProtoUtils.java    From ratis with Apache License 2.0 5 votes vote down vote up
static ServerRpcProto toServerRpcProto(RaftPeer peer, long delay) {
  if (peer == null) {
    // if no peer information return empty
    return ServerRpcProto.getDefaultInstance();
  }
  return ServerRpcProto.newBuilder()
      .setId(peer.getRaftPeerProto())
      .setLastRpcElapsedTimeMs(delay)
      .build();
}
 
Example #19
Source File: SubCommandBase.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * @return the peer with the given id if it is in this group; otherwise, return null.
 */
public RaftPeer getPeer(RaftPeerId raftPeerId) {
  Objects.requireNonNull(raftPeerId, "raftPeerId == null");
  for (RaftPeer p : getPeers()) {
    if (raftPeerId.equals(p.getId())) {
      return p;
    }
  }
  throw new IllegalArgumentException("Raft peer id " + raftPeerId + " is not part of the raft group definitions " +
          this.peers);
}
 
Example #20
Source File: NettyRpcProxy.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public NettyRpcProxy createProxyImpl(RaftPeer peer)
        throws IOException {
  try {
    return new NettyRpcProxy(peer, properties, group);
  } catch (InterruptedException e) {
    throw IOUtils.toInterruptedIOException("Failed connecting to " + peer, e);
  }
}
 
Example #21
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 #22
Source File: FollowerInfo.java    From ratis with Apache License 2.0 5 votes vote down vote up
FollowerInfo(RaftPeerId id, RaftPeer peer, Timestamp lastRpcTime, long nextIndex,
    boolean attendVote, int rpcSlownessTimeoutMs) {
  this.name = id + "->" + peer.getId();
  this.infoIndexChange = s -> LOG.info("{}: {}", name, s);
  this.debugIndexChange = s -> LOG.debug("{}: {}", name, s);

  this.peer = peer;
  this.lastRpcResponseTime = new AtomicReference<>(lastRpcTime);
  this.lastRpcSendTime = new AtomicReference<>(lastRpcTime);
  this.nextIndex = new RaftLogIndex("nextIndex", nextIndex);
  this.attendVote = attendVote;
  this.rpcSlownessTimeoutMs = rpcSlownessTimeoutMs;
}
 
Example #23
Source File: RaftConfiguration.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
boolean hasNoChange(Collection<RaftPeer> newMembers) {
  if (!isStable() || conf.size() != newMembers.size()) {
    return false;
  }
  for (RaftPeer peer : newMembers) {
    if (!conf.contains(peer.getId())) {
      return false;
    }
  }
  return true;
}
 
Example #24
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GrpcClientProtocolClient(ClientId id, RaftPeer target,
                                RaftProperties properties,
                                GrpcTlsConfig tlsConf) {
  this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
  this.target = target;
  final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
  final SizeInBytes maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug);
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConf!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConf.getTrustStore() != null) {
      sslContextBuilder.trustManager(tlsConf.getTrustStore());
    }
    if (tlsConf.getMtlsEnabled()) {
      sslContextBuilder.keyManager(tlsConf.getCertChain(),
          tlsConf.getPrivateKey());
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow.getSizeInt())
      .maxInboundMessageSize(maxMessageSize.getSizeInt())
      .build();
  blockingStub = RaftClientProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftClientProtocolServiceGrpc.newStub(channel);
  adminBlockingStub = AdminProtocolServiceGrpc.newBlockingStub(channel);
  this.requestTimeoutDuration = RaftClientConfigKeys.Rpc.requestTimeout(properties);
}
 
Example #25
Source File: MiniRaftCluster.java    From ratis with Apache License 2.0 5 votes vote down vote up
protected int getPort(RaftPeerId id, RaftGroup g) {
  final RaftPeer p = g != null? g.getPeer(id): peers.get(id);
  final String address = p == null? null : p.getAddress();
  final InetSocketAddress inetAddress = address != null?
      NetUtils.createSocketAddr(address): NetUtils.createLocalServerAddress();
  return inetAddress.getPort();
}
 
Example #26
Source File: RaftConfiguration.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * @return all the peers other than the given self id from the conf,
 *         and the old conf if it exists.
 */
public Collection<RaftPeer> getOtherPeers(RaftPeerId selfId) {
  Collection<RaftPeer> others = conf.getOtherPeers(selfId);
  if (oldConf != null) {
    oldConf.getOtherPeers(selfId).stream()
        .filter(p -> !others.contains(p))
        .forEach(others::add);
  }
  return others;
}
 
Example #27
Source File: RaftServerTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void waitAndCheckNewConf(MiniRaftCluster cluster,
    RaftPeer[] peers, int numOfRemovedPeers, Collection<RaftPeerId> deadPeers)
    throws Exception {
  final TimeDuration sleepTime = cluster.getTimeoutMax().apply(n -> n * (numOfRemovedPeers + 2));
  JavaUtils.attempt(() -> waitAndCheckNewConf(cluster, peers, deadPeers),
      10, sleepTime, "waitAndCheckNewConf", LOG);
}
 
Example #28
Source File: MetaStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public TransactionContext applyTransactionSerial(TransactionContext trx) {
    RaftProtos.LogEntryProto x = trx.getLogEntry();
    MetaSMRequestProto req = null;
    try {
        req = MetaSMRequestProto.parseFrom(x.getStateMachineLogEntry().getLogData());
    } catch (InvalidProtocolBufferException e) {
        e.printStackTrace();
    }
    switch (req.getTypeCase()) {
        case REGISTERREQUEST:
            LogServiceRegisterLogRequestProto r = req.getRegisterRequest();
            LogName logname = LogServiceProtoUtil.toLogName(r.getLogname());
            RaftGroup rg = MetaServiceProtoUtil.toRaftGroup(r.getRaftGroup());
            map.put(logname, rg);
            LOG.info("Log {} registered at {} with group {} ", logname, getId(), rg );
            break;
        case UNREGISTERREQUEST:
            LogServiceUnregisterLogRequestProto unregReq = req.getUnregisterRequest();
            logname = LogServiceProtoUtil.toLogName(unregReq.getLogname());
            map.remove(logname);
            break;
        case PINGREQUEST:
            LogServicePingRequestProto pingRequest = req.getPingRequest();
            RaftPeer peer = MetaServiceProtoUtil.toRaftPeer(pingRequest.getPeer());
            if (peers.contains(peer)) {
                //Do Nothing, that's just heartbeat
            } else {
                peers.add(peer);
                avail.add(new PeerGroups(peer));
            }
            break;

        default:
    }
    return super.applyTransactionSerial(trx);
}
 
Example #29
Source File: LeaderElection.java    From ratis with Apache License 2.0 5 votes vote down vote up
private int submitRequests(final long electionTerm, final TermIndex lastEntry) {
  int submitted = 0;
  for (final RaftPeer peer : others) {
    final RequestVoteRequestProto r = server.createRequestVoteRequest(
        peer.getId(), electionTerm, lastEntry);
    service.submit(
        () -> server.getServerRpc().requestVote(r));
    submitted++;
  }
  return submitted;
}
 
Example #30
Source File: PeerConfiguration.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
Collection<RaftPeer> getPeers() {
  return Collections.unmodifiableCollection(peers.values());
}