org.apache.ratis.proto.RaftProtos.RequestVoteReplyProto Java Examples

The following examples show how to use org.apache.ratis.proto.RaftProtos.RequestVoteReplyProto. 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: FakeRatisFollower.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
public static RequestVoteReplyProto requestVote(RaftPeerId raftPeerId,
    RequestVoteRequestProto request) {
  addLatency();
  System.out.println("Request vote response");
  return RequestVoteReplyProto.newBuilder()
      .setServerReply(
          RaftRpcReplyProto.newBuilder()
              .setSuccess(true)
              .setRequestorId(request.getServerRequest().getRequestorId())
              .setReplyId(raftPeerId.toByteString())
              .setCallId(request.getServerRequest().getCallId())
              .setRaftGroupId(request.getServerRequest().getRaftGroupId())
      )
      .setTerm(request.getCandidateTerm())
      .build();
}
 
Example #2
Source File: FollowerAppendLogEntryGenerator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Pseudo sync call to request a vote.
 *
 */
private CompletableFuture<RequestVoteReplyProto> requestVote() {
  CompletableFuture<RequestVoteReplyProto> response =
      new CompletableFuture<>();
  RequestVoteRequestProto voteRequest = RequestVoteRequestProto.newBuilder()
      .setServerRequest(createServerRequest(callIdRandom.nextLong()))
      .setCandidateLastEntry(
          TermIndexProto.newBuilder()
              .setIndex(0L)
              .setTerm(term)
              .build()
      )
      .build();

  stub.requestVote(voteRequest,
      new StreamObserver<RequestVoteReplyProto>() {
        @Override
        public void onNext(RequestVoteReplyProto value) {
          response.complete(value);
        }

        @Override
        public void onError(Throwable t) {
          response.completeExceptionally(t);
        }

        @Override
        public void onCompleted() {

        }
      });
  return response;
}
 
Example #3
Source File: RaftServerProtocolServerSideTranslatorPB.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(
    RpcController unused, RequestVoteRequestProto request)
    throws ServiceException {
  try {
    return impl.requestVote(request);
  } catch(IOException ioe) {
    throw new ServiceException(ioe);
  }
}
 
Example #4
Source File: LeaderElection.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private ResultAndTerm logAndReturn(Result result,
    Map<RaftPeerId, RequestVoteReplyProto> responses,
    List<Exception> exceptions, long newTerm) {
  LOG.info(this + ": Election " + result + "; received " + responses.size() + " response(s) "
      + responses.values().stream().map(ServerProtoUtils::toString).collect(Collectors.toList())
      + " and " + exceptions.size() + " exception(s); " + server.getState());
  int i = 0;
  for(Exception e : exceptions) {
    final int j = i++;
    LogUtils.infoOrTrace(LOG, () -> "  Exception " + j, e);
  }
  return new ResultAndTerm(result, newTerm);
}
 
Example #5
Source File: LeaderElection.java    From ratis with Apache License 2.0 5 votes vote down vote up
private ResultAndTerm logAndReturn(Result result,
    List<RequestVoteReplyProto> responses,
    List<Exception> exceptions, long newTerm) {
  LOG.info(server.getId() + ": Election " + result + "; received "
      + responses.size() + " response(s) "
      + responses.stream().map(ProtoUtils::toString).collect(Collectors.toList())
      + " and " + exceptions.size() + " exception(s); " + server.getState());
  int i = 0;
  for(Exception e : exceptions) {
    LOG.info("  " + i++ + ": " + e);
    LOG.trace("TRACE", e);
  }
  return new ResultAndTerm(result, newTerm);
}
 
Example #6
Source File: RaftServerProtocolServerSideTranslatorPB.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(
    RpcController unused, RequestVoteRequestProto request)
    throws ServiceException {
  try {
    return impl.requestVote(request);
  } catch(IOException ioe) {
    throw new ServiceException(ioe);
  }
}
 
Example #7
Source File: SimulatedServerRpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(RequestVoteRequestProto request)
    throws IOException {
  RaftServerReply reply = serverHandler.getRpc()
      .sendRequest(new RaftServerRequest(request));
  return reply.getRequestVote();
}
 
Example #8
Source File: RaftServerReply.java    From ratis with Apache License 2.0 4 votes vote down vote up
RequestVoteReplyProto getRequestVote() {
  return requestVote;
}
 
Example #9
Source File: RaftServerReply.java    From ratis with Apache License 2.0 4 votes vote down vote up
RaftServerReply(RequestVoteReplyProto r) {
  appendEntries = null;
  requestVote = Objects.requireNonNull(r);
  installSnapshot = null;
}
 
Example #10
Source File: LeaderElection.java    From ratis with Apache License 2.0 4 votes vote down vote up
private ResultAndTerm waitForResults(final long electionTerm,
    final int submitted) throws InterruptedException {
  final Timestamp timeout = Timestamp.currentTime().addTimeMs(server.getRandomTimeoutMs());
  final List<RequestVoteReplyProto> responses = new ArrayList<>();
  final List<Exception> exceptions = new ArrayList<>();
  int waitForNum = submitted;
  Collection<RaftPeerId> votedPeers = new ArrayList<>();
  while (waitForNum > 0 && running && server.isCandidate()) {
    final long waitTime = -timeout.elapsedTimeMs();
    if (waitTime <= 0) {
      return logAndReturn(Result.TIMEOUT, responses, exceptions, -1);
    }

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

      final RequestVoteReplyProto r = future.get();
      responses.add(r);
      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(RaftPeerId.valueOf(r.getServerReply().getReplyId()));
        if (conf.hasMajority(votedPeers, server.getId())) {
          return logAndReturn(Result.PASSED, responses, exceptions, -1);
        }
      }
    } catch(ExecutionException e) {
      LOG.info("{} got exception when requesting votes: {}", server.getId(), e);
      LOG.trace("TRACE", e);
      exceptions.add(e);
    }
    waitForNum--;
  }
  // received all the responses
  return logAndReturn(Result.REJECTED, responses, exceptions, -1);
}
 
Example #11
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(RequestVoteRequestProto request) throws IOException {
  return getImpl(request.getServerRequest()).requestVote(request);
}
 
Example #12
Source File: HadoopRpcService.java    From ratis with Apache License 2.0 4 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(
    RequestVoteRequestProto request) throws IOException {
  return processRequest(request, request.getServerRequest().getReplyId(),
      proxy -> proxy.requestVote(null, request));
}
 
Example #13
Source File: ProtoUtils.java    From ratis with Apache License 2.0 4 votes vote down vote up
static String toString(RequestVoteReplyProto proto) {
  return toString(proto.getServerReply()) + "-t" + proto.getTerm();
}
 
Example #14
Source File: RaftServerReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
RequestVoteReplyProto getRequestVote() {
  return requestVote;
}
 
Example #15
Source File: RaftServerReply.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
RaftServerReply(RequestVoteReplyProto r) {
  appendEntries = null;
  requestVote = Objects.requireNonNull(r);
  installSnapshot = null;
}
 
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: LeaderElection.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
Future<RequestVoteReplyProto> poll(TimeDuration waitTime) throws InterruptedException {
  return service.poll(waitTime.getDuration(), waitTime.getUnit());
}
 
Example #18
Source File: LeaderElection.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void submit(Callable<RequestVoteReplyProto> task) {
  service.submit(task);
}
 
Example #19
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(RequestVoteRequestProto request) throws IOException {
  return getImpl(request.getServerRequest()).requestVote(request);
}
 
Example #20
Source File: HadoopRpcService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public RequestVoteReplyProto requestVote(
    RequestVoteRequestProto request) throws IOException {
  return processRequest(request, request.getServerRequest().getReplyId(),
      proxy -> proxy.requestVote(null, request));
}
 
Example #21
Source File: RaftServerProtocol.java    From ratis with Apache License 2.0 votes vote down vote up
RequestVoteReplyProto requestVote(RequestVoteRequestProto request) throws IOException; 
Example #22
Source File: RaftServerProtocol.java    From incubator-ratis with Apache License 2.0 votes vote down vote up
RequestVoteReplyProto requestVote(RequestVoteRequestProto request) throws IOException;