Java Code Examples for org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver#onNext()

The following examples show how to use org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver#onNext() . 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: 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 2
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 3
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 4
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 5
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 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: 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 8
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 9
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 10
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 11
Source File: FakeRatisFollower.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
public static StreamObserver<AppendEntriesRequestProto> appendEntries(
    RaftPeerId raftPeerId,
    StreamObserver<AppendEntriesReplyProto> responseHandler) {
  return new StreamObserver<AppendEntriesRequestProto>() {
    private long maxIndex = -1L;

    @Override
    public void onNext(AppendEntriesRequestProto value) {

      for (LogEntryProto entry : value.getEntriesList()) {
        if (entry.getIndex() > maxIndex) {
          maxIndex = entry.getIndex();
        }
      }

      long maxCommitted = value.getCommitInfosList()
          .stream()
          .mapToLong(CommitInfoProto::getCommitIndex)
          .max().orElseGet(() -> 0L);

      maxCommitted = Math.min(maxCommitted, maxIndex);

      AppendEntriesReplyProto response = AppendEntriesReplyProto.newBuilder()
          .setNextIndex(maxIndex + 1)
          .setFollowerCommit(maxCommitted)
          .setResult(AppendResult.SUCCESS)
          .setTerm(value.getLeaderTerm())
          .setMatchIndex(maxIndex)
          .setServerReply(RaftRpcReplyProto.newBuilder()
              .setSuccess(true)
              .setRequestorId(value.getServerRequest().getRequestorId())
              .setReplyId(raftPeerId.toByteString())
              .setCallId(value.getServerRequest().getCallId())
              .setRaftGroupId(value.getServerRequest().getRaftGroupId()))
          .build();
      maxCommitted = Math.min(value.getLeaderCommit(), maxIndex);
      addLatency();
      responseHandler.onNext(response);
    }

    @Override
    public void onError(Throwable t) {

    }

    @Override
    public void onCompleted() {

    }
  };
}
 
Example 12
Source File: XceiverClientGrpc.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public XceiverClientReply sendCommandAsync(
    ContainerCommandRequestProto request, DatanodeDetails dn)
    throws IOException, InterruptedException {
  checkOpen(dn, request.getEncodedToken());
  UUID dnId = dn.getUuid();
  if (LOG.isDebugEnabled()) {
    LOG.debug("Send command {} to datanode {}",
        request.getCmdType(), dn.getNetworkFullPath());
  }
  final CompletableFuture<ContainerCommandResponseProto> replyFuture =
      new CompletableFuture<>();
  semaphore.acquire();
  long requestTime = System.nanoTime();
  metrics.incrPendingContainerOpsMetrics(request.getCmdType());
  // create a new grpc stream for each non-async call.

  // TODO: for async calls, we should reuse StreamObserver resources.
  // set the grpc dealine here so as if the response is not received
  // in the configured time, the rpc will fail with DEADLINE_EXCEEDED here
  final StreamObserver<ContainerCommandRequestProto> requestObserver =
      asyncStubs.get(dnId).withDeadlineAfter(timeout, TimeUnit.SECONDS)
          .send(new StreamObserver<ContainerCommandResponseProto>() {
            @Override
            public void onNext(ContainerCommandResponseProto value) {
              replyFuture.complete(value);
              metrics.decrPendingContainerOpsMetrics(request.getCmdType());
              metrics.addContainerOpsLatency(request.getCmdType(),
                  System.nanoTime() - requestTime);
              semaphore.release();
            }

            @Override
            public void onError(Throwable t) {
              replyFuture.completeExceptionally(t);
              metrics.decrPendingContainerOpsMetrics(request.getCmdType());
              metrics.addContainerOpsLatency(request.getCmdType(),
                  System.nanoTime() - requestTime);
              semaphore.release();
            }

            @Override
            public void onCompleted() {
              if (!replyFuture.isDone()) {
                replyFuture.completeExceptionally(new IOException(
                    "Stream completed but no reply for request " + request));
              }
            }
          });
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  return new XceiverClientReply(replyFuture);
}
 
Example 13
Source File: GrpcLogAppender.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/**
 * Send installSnapshot request to Follower with a snapshot.
 * @param snapshot the snapshot to be sent to Follower
 */
private void installSnapshot(SnapshotInfo snapshot) {
  LOG.info("{}: followerNextIndex = {} but logStartIndex = {}, send snapshot {} to follower",
      this, getFollower().getNextIndex(), getRaftLog().getStartIndex(), snapshot);

  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);
        getFollower().updateLastRpcSendTime();
        responseHandler.addPending(request);
      } else {
        break;
      }
    }
    snapshotRequestObserver.onCompleted();
    grpcServerMetrics.onInstallSnapshot();
  } catch (Exception e) {
    LOG.warn("{}: failed to install snapshot {}: {}", 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()) {
    getFollower().setSnapshotIndex(snapshot.getTermIndex().getIndex());
    LOG.info("{}: installed snapshot {} successfully", this, snapshot);
  }
}
 
Example 14
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());
  }
}