Search in sources :

Example 16 with StreamController

use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.

the class ReadRowsAttemptCallable method call.

/**
 * Sends the actual RPC. The request being sent will first be transformed by the {@link
 * StreamResumptionStrategy}.
 *
 * <p>This method expects to be called by one thread at a time. Furthermore, it expects that the
 * current RPC finished before the next time it's called.
 */
@Override
public Void call() {
    Preconditions.checkState(isStarted, "Must be started first");
    ReadRowsRequest request = (++numAttempts == 1) ? initialRequest : resumptionStrategy.getResumeRequest(initialRequest);
    // Should never happen. onAttemptError will check if ResumptionStrategy can create a resume
    // request,
    // which the RetryingFuture/StreamResumptionStrategy should respect.
    Preconditions.checkState(request != null, "ResumptionStrategy returned a null request.");
    innerAttemptFuture = SettableApiFuture.create();
    seenSuccessSinceLastError = false;
    ApiCallContext attemptContext = context;
    if (!outerRetryingFuture.getAttemptSettings().getRpcTimeout().isZero()) {
        attemptContext = attemptContext.withStreamWaitTimeout(outerRetryingFuture.getAttemptSettings().getRpcTimeout());
    }
    attemptContext.getTracer().attemptStarted(outerRetryingFuture.getAttemptSettings().getOverallAttemptCount());
    innerCallable.call(request, new StateCheckingResponseObserver<ReadRowsResponse>() {

        @Override
        public void onStartImpl(StreamController controller) {
            onAttemptStart(controller);
        }

        @Override
        public void onResponseImpl(ReadRowsResponse response) {
            onAttemptResponse(response);
        }

        @Override
        public void onErrorImpl(Throwable t) {
            onAttemptError(t);
        }

        @Override
        public void onCompleteImpl() {
            onAttemptComplete();
        }
    }, attemptContext);
    outerRetryingFuture.setAttemptFuture(innerAttemptFuture);
    return null;
}
Also used : StreamController(com.google.api.gax.rpc.StreamController) ReadRowsResponse(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse) ReadRowsRequest(com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest) ApiCallContext(com.google.api.gax.rpc.ApiCallContext)

Example 17 with StreamController

use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.

the class ReadRowsAttemptCallable method onRequest.

/**
 * Called when the outer {@link ResponseObserver} is ready for more data.
 *
 * @see StreamController#request(int)
 */
private void onRequest(int count) {
    Preconditions.checkState(!autoFlowControl, "Automatic flow control is enabled");
    Preconditions.checkArgument(count > 0, "Count must be > 0");
    final StreamController localInnerController;
    synchronized (lock) {
        int maxInc = Integer.MAX_VALUE - pendingRequests;
        count = Math.min(maxInc, count);
        pendingRequests += count;
        localInnerController = this.innerController;
    }
    // ignore it and the current controller will pick it up onStart.
    if (localInnerController != null) {
        localInnerController.request(count);
    }
}
Also used : StreamController(com.google.api.gax.rpc.StreamController)

Example 18 with StreamController

use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.

the class ReadRowsAttemptCallable method start.

/**
 * Starts the initial call. The call is attempted on the caller's thread. Further call attempts
 * will be scheduled by the {@link RetryingFuture}.
 */
public void start() {
    Preconditions.checkState(!isStarted, "Already started");
    // Initialize the outer observer
    outerObserver.onStart(new StreamController() {

        @Override
        public void disableAutoInboundFlowControl() {
            Preconditions.checkState(!isStarted, "Can't disable auto flow control once the stream is started");
            autoFlowControl = false;
        }

        @Override
        public void request(int count) {
            onRequest(count);
        }

        @Override
        public void cancel() {
            onCancel();
        }
    });
    if (autoFlowControl) {
        synchronized (lock) {
            pendingRequests = Integer.MAX_VALUE;
        }
    }
    isStarted = true;
    // Propagate the totalTimeout as the overall stream deadline.
    Duration totalTimeout = outerRetryingFuture.getAttemptSettings().getGlobalSettings().getTotalTimeout();
    if (totalTimeout != null && context != null) {
        context = context.withTimeout(totalTimeout);
    }
    // Call the inner callable
    call();
}
Also used : StreamController(com.google.api.gax.rpc.StreamController) Duration(org.threeten.bp.Duration)

Example 19 with StreamController

use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.

the class ReadRowsAttemptCallable method onCancel.

/**
 * Called when the outer {@link ResponseObserver} wants to prematurely cancel the stream.
 *
 * @see StreamController#cancel()
 */
private void onCancel() {
    StreamController localInnerController;
    synchronized (lock) {
        if (cancellationCause != null) {
            return;
        }
        // NOTE: BasicRetryingFuture will replace j.u.c.CancellationExceptions with it's own,
        // which will not have the current stacktrace, so a special wrapper has be used here.
        cancellationCause = new ServerStreamingAttemptException(new CancellationException("User cancelled stream"), resumptionStrategy.canResume(), seenSuccessSinceLastError);
        localInnerController = innerController;
    }
    if (localInnerController != null) {
        localInnerController.cancel();
    }
}
Also used : StreamController(com.google.api.gax.rpc.StreamController) CancellationException(java.util.concurrent.CancellationException) ServerStreamingAttemptException(com.google.api.gax.retrying.ServerStreamingAttemptException)

Example 20 with StreamController

use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.

the class ReadRowsAttemptCallable method start.

/**
 * Starts the initial call. The call is attempted on the caller's thread. Further call attempts
 * will be scheduled by the {@link RetryingFuture}.
 */
public void start() {
    Preconditions.checkState(!isStarted, "Already started");
    // Initialize the outer observer
    outerObserver.onStart(new StreamController() {

        @Override
        public void disableAutoInboundFlowControl() {
            Preconditions.checkState(!isStarted, "Can't disable auto flow control once the stream is started");
            autoFlowControl = false;
        }

        @Override
        public void request(int count) {
            onRequest(count);
        }

        @Override
        public void cancel() {
            onCancel();
        }
    });
    if (autoFlowControl) {
        synchronized (lock) {
            pendingRequests = Integer.MAX_VALUE;
        }
    }
    isStarted = true;
    // Propagate the totalTimeout as the overall stream deadline.
    Duration totalTimeout = outerRetryingFuture.getAttemptSettings().getGlobalSettings().getTotalTimeout();
    if (totalTimeout != null && context != null) {
        context = context.withTimeout(totalTimeout);
    }
    // Call the inner callable
    call();
}
Also used : StreamController(com.google.api.gax.rpc.StreamController) Duration(org.threeten.bp.Duration)

Aggregations

StreamController (com.google.api.gax.rpc.StreamController)21 Duration (org.threeten.bp.Duration)4 ServerStreamingAttemptException (com.google.api.gax.retrying.ServerStreamingAttemptException)3 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)3 CancellationException (java.util.concurrent.CancellationException)3 Test (org.junit.Test)3 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)2 Duration (com.google.protobuf.Duration)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 ArrayList (java.util.ArrayList)2 AudioFormat (javax.sound.sampled.AudioFormat)2 DataLine (javax.sound.sampled.DataLine)2 Info (javax.sound.sampled.DataLine.Info)2 TargetDataLine (javax.sound.sampled.TargetDataLine)2 AbortedException (com.google.api.gax.rpc.AbortedException)1 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)1 BidiStreamObserver (com.google.api.gax.rpc.BidiStreamObserver)1 ClientStream (com.google.api.gax.rpc.ClientStream)1 ResponseObserver (com.google.api.gax.rpc.ResponseObserver)1 ServerStream (com.google.api.gax.rpc.ServerStream)1