Java Code Examples for io.grpc.stub.ServerCallStreamObserver#setOnCancelHandler()

The following examples show how to use io.grpc.stub.ServerCallStreamObserver#setOnCancelHandler() . 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: DefaultSupervisorServiceGrpc.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
public void observeEvents(Empty request, StreamObserver<SupervisorEvent> responseObserver) {
    Subscription subscription = supervisorOperations.events()
            .map(SupervisorGrpcModelConverters::toGrpcEvent)
            .subscribe(
                    responseObserver::onNext,
                    e -> responseObserver.onError(
                            new StatusRuntimeException(Status.INTERNAL
                                    .withDescription("Supervisor event stream terminated with an error")
                                    .withCause(e))
                    ),
                    responseObserver::onCompleted
            );
    ServerCallStreamObserver<SupervisorEvent> serverObserver = (ServerCallStreamObserver<SupervisorEvent>) responseObserver;
    serverObserver.setOnCancelHandler(subscription::unsubscribe);
}
 
Example 2
Source File: DefaultJobManagementServiceGrpc.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void observeJobs(ObserveJobsQuery query, StreamObserver<JobChangeNotification> responseObserver) {
    JobQueryCriteria<TaskStatus.TaskState, JobDescriptor.JobSpecCase> criteria = toJobQueryCriteria(query);
    V3JobQueryCriteriaEvaluator jobsPredicate = new V3JobQueryCriteriaEvaluator(criteria, titusRuntime);
    V3TaskQueryCriteriaEvaluator tasksPredicate = new V3TaskQueryCriteriaEvaluator(criteria, titusRuntime);

    Observable<JobChangeNotification> eventStream = jobOperations.observeJobs(jobsPredicate, tasksPredicate)
            // avoid clogging the computation scheduler
            .observeOn(observeJobsScheduler)
            .subscribeOn(observeJobsScheduler, false)
            .map(event -> GrpcJobManagementModelConverters.toGrpcJobChangeNotification(event, logStorageInfo))
            .compose(ObservableExt.head(() -> {
                List<JobChangeNotification> snapshot = createJobsSnapshot(jobsPredicate, tasksPredicate);
                snapshot.add(SNAPSHOT_END_MARKER);
                return snapshot;
            }))
            .map(this::addTaskContextToJobChangeNotification)
            .doOnError(e -> logger.error("Unexpected error in jobs event stream", e));

    Subscription subscription = eventStream.subscribe(
            responseObserver::onNext,
            e -> responseObserver.onError(
                    new StatusRuntimeException(Status.INTERNAL
                            .withDescription("All jobs monitoring stream terminated with an error")
                            .withCause(e))
            ),
            responseObserver::onCompleted
    );

    ServerCallStreamObserver<JobChangeNotification> serverObserver = (ServerCallStreamObserver<JobChangeNotification>) responseObserver;
    serverObserver.setOnCancelHandler(subscription::unsubscribe);
}
 
Example 3
Source File: DefaultJobManagementServiceGrpc.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void observeJob(JobId request, StreamObserver<JobChangeNotification> responseObserver) {
    String jobId = request.getId();
    Observable<JobChangeNotification> eventStream = jobOperations.observeJob(jobId)
            // avoid clogging the computation scheduler
            .observeOn(observeJobsScheduler)
            .subscribeOn(observeJobsScheduler, false)
            .map(event -> GrpcJobManagementModelConverters.toGrpcJobChangeNotification(event, logStorageInfo))
            .compose(ObservableExt.head(() -> {
                List<JobChangeNotification> snapshot = createJobSnapshot(jobId);
                snapshot.add(SNAPSHOT_END_MARKER);
                return snapshot;
            }))
            .map(this::addTaskContextToJobChangeNotification)
            .doOnError(e -> {
                if (!JobManagerException.isExpected(e)) {
                    logger.error("Unexpected error in job {} event stream", jobId, e);
                } else {
                    logger.debug("Error in job {} event stream", jobId, e);
                }
            });

    Subscription subscription = eventStream.subscribe(
            responseObserver::onNext,
            e -> responseObserver.onError(
                    new StatusRuntimeException(Status.INTERNAL
                            .withDescription(jobId + " job monitoring stream terminated with an error")
                            .withCause(e))
            ),
            responseObserver::onCompleted
    );

    ServerCallStreamObserver<JobChangeNotification> serverObserver = (ServerCallStreamObserver<JobChangeNotification>) responseObserver;
    serverObserver.setOnCancelHandler(subscription::unsubscribe);
}
 
Example 4
Source File: GrpcEvictionService.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public void observeEvents(ObserverEventRequest request, StreamObserver<EvictionServiceEvent> responseObserver) {
    Disposable subscription = evictionOperations.events(request.getIncludeSnapshot()).subscribe(
            next -> toGrpcEvent(next).ifPresent(responseObserver::onNext),
            e -> responseObserver.onError(
                    new StatusRuntimeException(Status.INTERNAL
                            .withDescription("Eviction event stream terminated with an error")
                            .withCause(e))
            ),
            responseObserver::onCompleted
    );
    ServerCallStreamObserver<EvictionServiceEvent> serverObserver = (ServerCallStreamObserver<EvictionServiceEvent>) responseObserver;
    serverObserver.setOnCancelHandler(subscription::dispose);
}
 
Example 5
Source File: ExecutionService.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private void withCancellation(
    ServerCallStreamObserver<Operation> serverCallStreamObserver, ListenableFuture<Void> future) {
  addCallback(
      future,
      new FutureCallback<Void>() {
        boolean isCancelled() {
          return serverCallStreamObserver.isCancelled() || Context.current().isCancelled();
        }

        @Override
        public void onSuccess(Void result) {
          if (!isCancelled()) {
            try {
              serverCallStreamObserver.onCompleted();
            } catch (Exception e) {
              onFailure(e);
            }
          }
        }

        @Override
        public void onFailure(Throwable t) {
          if (!isCancelled() && !(t instanceof CancellationException)) {
            logger.log(Level.WARNING, "error occurred during execution", t);
            serverCallStreamObserver.onError(Status.fromThrowable(t).asException());
          }
        }
      },
      Context.current().fixedContextExecutor(directExecutor()));
  serverCallStreamObserver.setOnCancelHandler(() -> future.cancel(false));
}
 
Example 6
Source File: GrpcUtil.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public static void attachCancellingCallback(StreamObserver responseObserver, Subscription subscription) {
    ServerCallStreamObserver serverObserver = (ServerCallStreamObserver) responseObserver;
    serverObserver.setOnCancelHandler(subscription::unsubscribe);
}
 
Example 7
Source File: GrpcUtil.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public static void attachCancellingCallback(StreamObserver responseObserver, Disposable disposable) {
    ServerCallStreamObserver serverObserver = (ServerCallStreamObserver) responseObserver;
    serverObserver.setOnCancelHandler(disposable::dispose);
}
 
Example 8
Source File: ReactorToGrpcClientBuilderTest.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private void registerCancellationCallback(StreamObserver<?> responseObserver) {
    ServerCallStreamObserver serverCallStreamObserver = (ServerCallStreamObserver) responseObserver;
    serverCallStreamObserver.setOnCancelHandler(() -> cancelled = true);
}
 
Example 9
Source File: ExecutionService.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
KeepaliveWatcher(ServerCallStreamObserver serverCallStreamObserver) {
  this.serverCallStreamObserver = serverCallStreamObserver;
  serverCallStreamObserver.setOnCancelHandler(this::cancel);
}