org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver Java Examples

The following examples show how to use org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver. 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: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void appendLog() throws IOException {
  final AppendEntriesRequestProto pending;
  final AppendEntriesRequest request;
  final StreamObserver<AppendEntriesRequestProto> s;
  synchronized (this) {
    // prepare and enqueue the append request. note changes on follower's
    // nextIndex and ops on pendingRequests should always be associated
    // together and protected by the lock
    pending = createRequest(callId++);
    if (pending == null) {
      return;
    }
    request = new AppendEntriesRequest(pending, getFollowerId(), grpcServerMetrics);
    pendingRequests.put(request);
    increaseNextIndex(pending);
    if (appendLogRequestObserver == null) {
      appendLogRequestObserver = getClient().appendEntries(new AppendLogResponseHandler());
    }
    s = appendLogRequestObserver;
  }

  if (isAppenderRunning()) {
    sendRequest(request, pending, s);
  }
}
 
Example #2
Source File: GrpcUtil.java    From ratis with Apache License 2.0 6 votes vote down vote up
static <REPLY extends RaftClientReply, REPLY_PROTO> void asyncCall(
    StreamObserver<REPLY_PROTO> responseObserver,
    CheckedSupplier<CompletableFuture<REPLY>, IOException> supplier,
    Function<REPLY, REPLY_PROTO> toProto) {
  try {
    supplier.get().whenCompleteAsync((reply, exception) -> {
      if (exception != null) {
        responseObserver.onError(GrpcUtil.wrapException(exception));
      } else {
        responseObserver.onNext(toProto.apply(reply));
        responseObserver.onCompleted();
      }
    });
  } catch (Exception e) {
    responseObserver.onError(GrpcUtil.wrapException(e));
  }
}
 
Example #3
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 6 votes vote down vote up
private void appendLog() throws IOException {
  final AppendEntriesRequestProto pending;
  final StreamObserver<AppendEntriesRequestProto> s;
  synchronized (this) {
    // prepare and enqueue the append request. note changes on follower's
    // nextIndex and ops on pendingRequests should always be associated
    // together and protected by the lock
    pending = createRequest(callId++);
    if (pending == null) {
      return;
    }
    pendingRequests.put(pending.getServerRequest().getCallId(), pending);
    updateNextIndex(pending);
    if (appendLogRequestObserver == null) {
      appendLogRequestObserver = getClient().appendEntries(new AppendLogResponseHandler());
    }
    s = appendLogRequestObserver;
  }

  if (isAppenderRunning()) {
    sendRequest(pending, s);
  }
}
 
Example #4
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
protected void runAppenderImpl() throws IOException {
  for(; isAppenderRunning(); mayWait()) {
    if (shouldSendRequest()) {
      SnapshotInfo snapshot = shouldInstallSnapshot();
      if (snapshot != null) {
        installSnapshot(snapshot);
      } else if (!shouldWait()) {
        // keep appending log entries or sending heartbeats
        appendLog();
      }
    }
    checkSlowness();
  }

  Optional.ofNullable(appendLogRequestObserver).ifPresent(StreamObserver::onCompleted);
}
 
Example #5
Source File: GrpcUtil.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static <REPLY extends RaftClientReply, REPLY_PROTO> void asyncCall(
    StreamObserver<REPLY_PROTO> responseObserver,
    CheckedSupplier<CompletableFuture<REPLY>, IOException> supplier,
    Function<REPLY, REPLY_PROTO> toProto) {
  try {
    supplier.get().whenCompleteAsync((reply, exception) -> {
      if (exception != null) {
        responseObserver.onError(GrpcUtil.wrapException(exception));
      } else {
        responseObserver.onNext(toProto.apply(reply));
        responseObserver.onCompleted();
      }
    });
  } catch (Exception e) {
    responseObserver.onError(GrpcUtil.wrapException(e));
  }
}
 
Example #6
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Send installSnapshot request to Follower with only a notification that a snapshot needs to be installed.
 * @param firstAvailableLogTermIndex the first available log's index on the Leader
 */
private void installSnapshot(TermIndex firstAvailableLogTermIndex) {
  LOG.info("{}: followerNextIndex = {} but logStartIndex = {}, notify follower to install snapshot-{}",
      this, getFollower().getNextIndex(), getRaftLog().getStartIndex(), firstAvailableLogTermIndex);

  final InstallSnapshotResponseHandler responseHandler = new InstallSnapshotResponseHandler(true);
  StreamObserver<InstallSnapshotRequestProto> snapshotRequestObserver = null;
  // prepare and enqueue the notify install snapshot request.
  final InstallSnapshotRequestProto request = createInstallSnapshotNotificationRequest(firstAvailableLogTermIndex);
  if (LOG.isInfoEnabled()) {
    LOG.info("{}: send {}", this, ServerProtoUtils.toString(request));
  }
  try {
    snapshotRequestObserver = getClient().installSnapshot(responseHandler);
    snapshotRequestObserver.onNext(request);
    getFollower().updateLastRpcSendTime();
    responseHandler.addPending(request);
    snapshotRequestObserver.onCompleted();
  } catch (Exception e) {
    GrpcUtil.warn(LOG, () -> this + ": Failed to notify follower to install snapshot.", e);
    if (snapshotRequestObserver != null) {
      snapshotRequestObserver.onError(e);
    }
    return;
  }

  synchronized (this) {
    if (isAppenderRunning() && !responseHandler.isDone()) {
      try {
        wait();
      } catch (InterruptedException ignored) {
      }
    }
  }
}
 
Example #7
Source File: GrpcReplicationService.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public void download(CopyContainerRequestProto request,
    StreamObserver<CopyContainerResponseProto> responseObserver) {
  long containerID = request.getContainerID();
  LOG.info("Streaming container data ({}) to other datanode", containerID);
  try {
    GrpcOutputStream outputStream =
        new GrpcOutputStream(responseObserver, containerID, BUFFER_SIZE);
    source.copyData(containerID, outputStream);
  } catch (IOException e) {
    LOG.error("Error streaming container {}", containerID, e);
    responseObserver.onError(e);
  }
}
 
Example #8
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 5 votes vote down vote up
private void sendRequest(AppendEntriesRequestProto request,
    StreamObserver<AppendEntriesRequestProto> s) {
  CodeInjectionForTesting.execute(GrpcService.GRPC_SEND_SERVER_REQUEST,
      server.getId(), null, request);

  s.onNext(request);
  scheduler.onTimeout(requestTimeoutDuration, () -> timeoutAppendRequest(request), LOG,
      () -> "Timeout check failed for append entry request: " + request);
  follower.updateLastRpcSendTime();
}
 
Example #9
Source File: GrpcOutputStream.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
GrpcOutputStream(
    StreamObserver<CopyContainerResponseProto> responseObserver,
    long containerId, int bufferSize) {
  this.responseObserver = responseObserver;
  this.containerId = containerId;
  this.bufferSize = bufferSize;
  buffer = ByteString.newOutput(bufferSize);
}
 
Example #10
Source File: GrpcXceiverService.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<ContainerCommandRequestProto> send(
    StreamObserver<ContainerCommandResponseProto> responseObserver) {
  return new StreamObserver<ContainerCommandRequestProto>() {
    private final AtomicBoolean isClosed = new AtomicBoolean(false);

    @Override
    public void onNext(ContainerCommandRequestProto request) {
      try {
        ContainerCommandResponseProto resp =
            dispatcher.dispatch(request, null);
        responseObserver.onNext(resp);
      } catch (Throwable e) {
        LOG.error("Got exception when processing"
                  + " ContainerCommandRequestProto {}", request, e);
        responseObserver.onError(e);
      }
    }

    @Override
    public void onError(Throwable t) {
      // for now we just log a msg
      LOG.error("ContainerCommand send on error. Exception: ", t);
    }

    @Override
    public void onCompleted() {
      if (isClosed.compareAndSet(false, true)) {
        LOG.debug("ContainerCommand send completed");
        responseObserver.onCompleted();
      }
    }
  };
}
 
Example #11
Source File: GrpcAdminProtocolService.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void groupList(GroupListRequestProto proto,
      StreamObserver<GroupListReplyProto> responseObserver) {
  final GroupListRequest request = ClientProtoUtils.toGroupListRequest(proto);
  GrpcUtil.asyncCall(responseObserver, () -> protocol.getGroupListAsync(request),
      ClientProtoUtils::toGroupListReplyProto);
}
 
Example #12
Source File: GrpcServerProtocolService.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public StreamObserver<InstallSnapshotRequestProto> installSnapshot(
    StreamObserver<InstallSnapshotReplyProto> responseObserver) {
  return new StreamObserver<InstallSnapshotRequestProto>() {
    @Override
    public void onNext(InstallSnapshotRequestProto request) {
      try {
        final InstallSnapshotReplyProto reply = server.installSnapshot(request);
        responseObserver.onNext(reply);
      } catch (Throwable e) {
        GrpcUtil.warn(LOG, () -> getId() + ": Failed installSnapshot " + ProtoUtils.toString(request.getServerRequest()), e);
        responseObserver.onError(GrpcUtil.wrapException(e));
      }
    }

    @Override
    public void onError(Throwable t) {
      GrpcUtil.warn(LOG, () -> getId() + ": installSnapshot onError", t);
    }

    @Override
    public void onCompleted() {
      LOG.info("{}: installSnapshot completed", getId());
      responseObserver.onCompleted();
    }
  };
}
 
Example #13
Source File: GrpcServerProtocolService.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void requestVote(RequestVoteRequestProto request,
    StreamObserver<RequestVoteReplyProto> responseObserver) {
  try {
    final RequestVoteReplyProto reply = server.requestVote(request);
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
  } catch (Throwable e) {
    GrpcUtil.warn(LOG, () -> getId() + ": Failed requestVote " + ProtoUtils.toString(request.getServerRequest()), e);
    responseObserver.onError(GrpcUtil.wrapException(e));
  }
}
 
Example #14
Source File: GrpcClientRpc.java    From ratis with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<RaftClientReply> sendRequest(
    RaftClientRequest request, GrpcClientProtocolClient proxy) throws IOException {
  final RaftClientRequestProto requestProto =
      toRaftClientRequestProto(request);
  final CompletableFuture<RaftClientReplyProto> replyFuture =
      new CompletableFuture<>();
  // create a new grpc stream for each non-async call.
  final StreamObserver<RaftClientRequestProto> requestObserver =
      proxy.appendWithTimeout(new StreamObserver<RaftClientReplyProto>() {
        @Override
        public void onNext(RaftClientReplyProto value) {
          replyFuture.complete(value);
        }

        @Override
        public void onError(Throwable t) {
          replyFuture.completeExceptionally(GrpcUtil.unwrapIOException(t));
        }

        @Override
        public void onCompleted() {
          if (!replyFuture.isDone()) {
            replyFuture.completeExceptionally(
                new IOException(clientId + ": Stream completed but no reply for request " + request));
          }
        }
      });
  requestObserver.onNext(requestProto);
  requestObserver.onCompleted();

  return replyFuture.thenApply(ClientProtoUtils::toRaftClientReply);
}
 
Example #15
Source File: GrpcClientProtocolService.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void setConfiguration(SetConfigurationRequestProto proto,
    StreamObserver<RaftClientReplyProto> responseObserver) {
  final SetConfigurationRequest request = ClientProtoUtils.toSetConfigurationRequest(proto);
  GrpcUtil.asyncCall(responseObserver, () -> protocol.setConfigurationAsync(request),
      ClientProtoUtils::toRaftClientReplyProto);
}
 
Example #16
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 #17
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void sendRequest(AppendEntriesRequest request, AppendEntriesRequestProto proto,
      StreamObserver<AppendEntriesRequestProto> s) {
  CodeInjectionForTesting.execute(GrpcService.GRPC_SEND_SERVER_REQUEST,
      getServer().getId(), null, proto);
  request.startRequestTimer();
  s.onNext(proto);
  scheduler.onTimeout(requestTimeoutDuration,
      () -> timeoutAppendRequest(request.getCallId(), request.isHeartbeat()),
      LOG, () -> "Timeout check failed for append entry request: " + request);
  getFollower().updateLastRpcSendTime();
}
 
Example #18
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
protected void runAppenderImpl() throws IOException {
  boolean shouldAppendLog;
  for(; isAppenderRunning(); mayWait()) {
    shouldAppendLog = true;
    if (shouldSendRequest()) {
      if (installSnapshotEnabled) {
        SnapshotInfo snapshot = shouldInstallSnapshot();
        if (snapshot != null) {
          installSnapshot(snapshot);
          shouldAppendLog = false;
        }
      } else {
        TermIndex installSnapshotNotificationTermIndex = shouldNotifyToInstallSnapshot();
        if (installSnapshotNotificationTermIndex != null) {
          installSnapshot(installSnapshotNotificationTermIndex);
          shouldAppendLog = false;
        }
      }
      if (shouldHeartbeat() || (shouldAppendLog && !shouldWait())) {
        // keep appending log entries or sending heartbeats
        appendLog();
      }
    }
    checkSlowness();
  }

  Optional.ofNullable(appendLogRequestObserver).ifPresent(StreamObserver::onCompleted);
}
 
Example #19
Source File: GrpcAdminProtocolService.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void groupList(GroupListRequestProto proto,
      StreamObserver<GroupListReplyProto> responseObserver) {
  final GroupListRequest request = ClientProtoUtils.toGroupListRequest(proto);
  GrpcUtil.asyncCall(responseObserver, () -> protocol.getGroupListAsync(request),
      ClientProtoUtils::toGroupListReplyProto);
}
 
Example #20
Source File: GrpcAdminProtocolService.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void groupManagement(GroupManagementRequestProto proto,
      StreamObserver<RaftClientReplyProto> responseObserver) {
  final GroupManagementRequest request = ClientProtoUtils.toGroupManagementRequest(proto);
  GrpcUtil.asyncCall(responseObserver, () -> protocol.groupManagementAsync(request),
      ClientProtoUtils::toRaftClientReplyProto);
}
 
Example #21
Source File: GrpcClientProtocolService.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void setConfiguration(SetConfigurationRequestProto proto,
    StreamObserver<RaftClientReplyProto> responseObserver) {
  final SetConfigurationRequest request = ClientProtoUtils.toSetConfigurationRequest(proto);
  GrpcUtil.asyncCall(responseObserver, () -> protocol.setConfigurationAsync(request),
      ClientProtoUtils::toRaftClientReplyProto);
}
 
Example #22
Source File: GrpcServerProtocolService.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void requestVote(RequestVoteRequestProto request,
    StreamObserver<RequestVoteReplyProto> responseObserver) {
  try {
    final RequestVoteReplyProto reply = server.requestVote(request);
    responseObserver.onNext(reply);
    responseObserver.onCompleted();
  } catch (Throwable e) {
    GrpcUtil.warn(LOG, () -> getId() + ": Failed requestVote " + ProtoUtils.toString(request.getServerRequest()), e);
    responseObserver.onError(GrpcUtil.wrapException(e));
  }
}
 
Example #23
Source File: GrpcClientProtocolClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
synchronized boolean onNext(RaftClientRequestProto request) {
  final StreamObserver<RaftClientRequestProto> s = streamObserver.get();
  if (s != null) {
    s.onNext(request);
    return true;
  }
  return false;
}
 
Example #24
Source File: GrpcClientRpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private CompletableFuture<RaftClientReply> sendRequest(
    RaftClientRequest request, GrpcClientProtocolClient proxy) throws IOException {
  final RaftClientRequestProto requestProto =
      toRaftClientRequestProto(request);
  final CompletableFuture<RaftClientReplyProto> replyFuture = new CompletableFuture<>();
  // create a new grpc stream for each non-async call.
  final StreamObserver<RaftClientRequestProto> requestObserver =
      proxy.orderedWithTimeout(new StreamObserver<RaftClientReplyProto>() {
        @Override
        public void onNext(RaftClientReplyProto value) {
          replyFuture.complete(value);
        }

        @Override
        public void onError(Throwable t) {
          replyFuture.completeExceptionally(GrpcUtil.unwrapIOException(t));
        }

        @Override
        public void onCompleted() {
          if (!replyFuture.isDone()) {
            replyFuture.completeExceptionally(
                new AlreadyClosedException(clientId + ": Stream completed but no reply for request " + request));
          }
        }
      });
  requestObserver.onNext(requestProto);
  requestObserver.onCompleted();

  return replyFuture.thenApply(ClientProtoUtils::toRaftClientReply);
}
 
Example #25
Source File: GrpcServerProtocolClient.java    From ratis with Apache License 2.0 4 votes vote down vote up
StreamObserver<InstallSnapshotRequestProto> installSnapshot(
    StreamObserver<InstallSnapshotReplyProto> responseHandler) {
  return asyncStub.withDeadlineAfter(requestTimeoutDuration.getDuration(), requestTimeoutDuration.getUnit())
      .installSnapshot(responseHandler);
}
 
Example #26
Source File: GrpcServerProtocolClient.java    From ratis with Apache License 2.0 4 votes vote down vote up
StreamObserver<AppendEntriesRequestProto> appendEntries(
    StreamObserver<AppendEntriesReplyProto> responseHandler) {
  return asyncStub.appendEntries(responseHandler);
}
 
Example #27
Source File: GrpcLogAppender.java    From ratis with Apache License 2.0 4 votes vote down vote up
private void installSnapshot(SnapshotInfo snapshot) {
  LOG.info("{}: follower {}'s next index is {}," +
          " log's start index is {}, need to install snapshot",
      server.getId(), follower.getPeer(), follower.getNextIndex(),
      raftLog.getStartIndex());

  final InstallSnapshotResponseHandler responseHandler = new InstallSnapshotResponseHandler();
  StreamObserver<InstallSnapshotRequestProto> snapshotRequestObserver = null;
  final String requestId = UUID.randomUUID().toString();
  try {
    snapshotRequestObserver = getClient().installSnapshot(responseHandler);
    for (InstallSnapshotRequestProto request :
        new SnapshotRequestIter(snapshot, requestId)) {
      if (isAppenderRunning()) {
        snapshotRequestObserver.onNext(request);
        follower.updateLastRpcSendTime();
        responseHandler.addPending(request);
      } else {
        break;
      }
    }
    snapshotRequestObserver.onCompleted();
  } catch (Exception e) {
    LOG.warn("{} failed to install snapshot {}. Exception: {}", this,
        snapshot.getFiles(), e);
    if (snapshotRequestObserver != null) {
      snapshotRequestObserver.onError(e);
    }
    return;
  }

  synchronized (this) {
    while (isAppenderRunning() && !responseHandler.isDone()) {
      try {
        wait();
      } catch (InterruptedException ignored) {
      }
    }
  }

  if (responseHandler.hasAllResponse()) {
    follower.setSnapshotIndex(snapshot.getTermIndex().getIndex());
    LOG.info("{}: install snapshot-{} successfully on follower {}",
        server.getId(), snapshot.getTermIndex().getIndex(), follower.getPeer());
  }
}
 
Example #28
Source File: GrpcClientProtocolService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public StreamObserver<RaftClientRequestProto> ordered(StreamObserver<RaftClientReplyProto> responseObserver) {
  final OrderedRequestStreamObserver so = new OrderedRequestStreamObserver(responseObserver);
  orderedStreamObservers.putNew(so);
  return so;
}
 
Example #29
Source File: GrpcClientProtocolService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
@Override
public StreamObserver<RaftClientRequestProto> unordered(StreamObserver<RaftClientReplyProto> responseObserver) {
  return new UnorderedRequestStreamObserver(responseObserver);
}
 
Example #30
Source File: GrpcClientProtocolService.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
RequestStreamObserver(StreamObserver<RaftClientReplyProto> responseObserver) {
  LOG.debug("new {}", name);
  this.responseObserver = responseObserver;
}