Search in sources :

Example 11 with ApiCallContext

use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.

the class DynamicFlowControlCallableTest method testTriggeringAdjustingThreshold.

@Test
public void testTriggeringAdjustingThreshold() throws Exception {
    Map<String, List<String>> extraHeaders = new HashMap<>();
    extraHeaders.put(LATENCY_HEADER, Arrays.asList(String.valueOf(TARGET_LATENCY_MS * 4)));
    long currentTimeMs = System.currentTimeMillis();
    ApiCallContext newContext = context.withExtraHeaders(extraHeaders);
    ApiFuture future = callableToTest.futureCall(request, newContext);
    future.get();
    assertThat(stats.getMeanLatency()).isAtLeast(TARGET_LATENCY_MS * DynamicFlowControlCallable.VERY_HIGH_LATENCY_MULTIPLIER);
    assertThat(stats.getLastAdjustedTimestampMs()).isGreaterThan(currentTimeMs);
    long expectedStep = Math.round(MAX_ELEMENT * DynamicFlowControlCallable.VERY_HIGH_LATENCY_DECREASE_CONCURRENCY_RATE);
    assertThat(flowController.getCurrentElementCountLimit()).isEqualTo(INITIAL_ELEMENT - expectedStep);
}
Also used : ApiFuture(com.google.api.core.ApiFuture) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Test(org.junit.Test)

Example 12 with ApiCallContext

use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.

the class MutateRowsAttemptCallableTest method rpcPermanentError.

@Test
public void rpcPermanentError() {
    // Setup the request & response
    MutateRowsRequest request = MutateRowsRequest.newBuilder().addEntries(Entry.getDefaultInstance()).addEntries(Entry.getDefaultInstance()).build();
    final UnavailableException rpcError = new UnavailableException("fake error", null, GrpcStatusCode.of(io.grpc.Status.Code.INVALID_ARGUMENT), false);
    UnaryCallable<MutateRowsRequest, List<MutateRowsResponse>> innerCallable = new UnaryCallable<MutateRowsRequest, List<MutateRowsResponse>>() {

        @Override
        public ApiFuture<List<MutateRowsResponse>> futureCall(MutateRowsRequest request, ApiCallContext context) {
            return ApiFutures.immediateFailedFuture(rpcError);
        }
    };
    // Make the call
    MutateRowsAttemptCallable attemptCallable = new MutateRowsAttemptCallable(innerCallable, request, callContext, retryCodes);
    attemptCallable.setExternalFuture(parentFuture);
    attemptCallable.call();
    // Overall expectations: retryable error
    Throwable actualError = null;
    try {
        parentFuture.attemptFuture.get();
    } catch (Throwable t) {
        actualError = t.getCause();
    }
    assertThat(actualError).isInstanceOf(MutateRowsException.class);
    assertThat(((MutateRowsException) actualError).isRetryable()).isFalse();
    // Entry expectations: both entries failed with an error whose cause is the rpc error
    @SuppressWarnings("ConstantConditions") List<FailedMutation> failedMutations = ((MutateRowsException) actualError).getFailedMutations();
    assertThat(failedMutations).hasSize(2);
    assertThat(failedMutations.get(0).getIndex()).isEqualTo(0);
    assertThat(failedMutations.get(0).getError().isRetryable()).isFalse();
    assertThat(failedMutations.get(0).getError().getCause()).isEqualTo(rpcError);
    assertThat(failedMutations.get(1).getIndex()).isEqualTo(1);
    assertThat(failedMutations.get(1).getError().isRetryable()).isFalse();
    assertThat(failedMutations.get(1).getError().getCause()).isEqualTo(rpcError);
}
Also used : UnaryCallable(com.google.api.gax.rpc.UnaryCallable) UnavailableException(com.google.api.gax.rpc.UnavailableException) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) FailedMutation(com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation) MutateRowsResponse(com.google.bigtable.v2.MutateRowsResponse) MutateRowsException(com.google.cloud.bigtable.data.v2.models.MutateRowsException) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) List(java.util.List) Test(org.junit.Test)

Example 13 with ApiCallContext

use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.

the class MutateRowsAttemptCallable method call.

/**
 * Send the current request and the parent {@link RetryingFuture} with this attempt's future.
 *
 * <p>On RPC completion this method will preprocess all errors (both RPC level and entry level)
 * and wrap them in a {@link MutateRowsException}. Please note that the results of the RPC are
 * only available in the attempt future that is set on the parent {@link RetryingFuture} and the
 * return of this method should just be ignored.
 */
@Override
public Void call() {
    try {
        // externalFuture is set from MutateRowsRetryingCallable before invoking this method. It
        // shouldn't be null unless the code changed
        Preconditions.checkNotNull(externalFuture, "External future must be set before starting an attempt");
        // attemptStared should be called at the very start of the operation. This will initialize
        // variables in ApiTracer and avoid exceptions when the tracer marks the attempt as finished
        callContext.getTracer().attemptStarted(externalFuture.getAttemptSettings().getOverallAttemptCount());
        Preconditions.checkState(currentRequest.getEntriesCount() > 0, "Request doesn't have any mutations to send");
        // Configure the deadline
        ApiCallContext currentCallContext = callContext;
        if (currentCallContext.getTimeout() == null && !externalFuture.getAttemptSettings().getRpcTimeout().isZero()) {
            currentCallContext = currentCallContext.withTimeout(externalFuture.getAttemptSettings().getRpcTimeout());
        }
        // Handle concurrent cancellation
        externalFuture.setAttemptFuture(new NonCancellableFuture<Void>());
        if (externalFuture.isDone()) {
            return null;
        }
        // Make the actual call
        ApiFuture<List<MutateRowsResponse>> innerFuture = innerCallable.futureCall(currentRequest, currentCallContext);
        // Handle RPC level errors by wrapping them in a MutateRowsException
        ApiFuture<List<MutateRowsResponse>> catching = ApiFutures.catching(innerFuture, Throwable.class, attemptFailedCallback, MoreExecutors.directExecutor());
        // Inspect the results and either propagate the success, or prepare to retry the failed
        // mutations
        ApiFuture<Void> transformed = ApiFutures.transform(catching, attemptSuccessfulCallback, MoreExecutors.directExecutor());
        // Notify the parent of the attempt
        externalFuture.setAttemptFuture(transformed);
    } catch (Throwable e) {
        externalFuture.setAttemptFuture(ApiFutures.<Void>immediateFailedFuture(e));
    }
    return null;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ApiCallContext(com.google.api.gax.rpc.ApiCallContext)

Example 14 with ApiCallContext

use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.

the class StatsHeadersServerStreamingCallable method call.

@Override
public void call(RequestT request, ResponseObserver<ResponseT> responseObserver, ApiCallContext apiCallContext) {
    ApiCallContext newCallContext = apiCallContext.withExtraHeaders(Util.createStatsHeaders(apiCallContext));
    innerCallable.call(request, responseObserver, newCallContext);
}
Also used : ApiCallContext(com.google.api.gax.rpc.ApiCallContext)

Example 15 with ApiCallContext

use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.

the class MutateRowsAttemptCallableTest method rpcRetryableError.

@Test
public void rpcRetryableError() {
    // Setup the request & response
    MutateRowsRequest request = MutateRowsRequest.newBuilder().addEntries(Entry.getDefaultInstance()).addEntries(Entry.getDefaultInstance()).build();
    final UnavailableException rpcError = new UnavailableException("fake error", null, GrpcStatusCode.of(io.grpc.Status.Code.UNAVAILABLE), true);
    UnaryCallable<MutateRowsRequest, List<MutateRowsResponse>> innerCallable = new UnaryCallable<MutateRowsRequest, List<MutateRowsResponse>>() {

        @Override
        public ApiFuture<List<MutateRowsResponse>> futureCall(MutateRowsRequest request, ApiCallContext context) {
            return ApiFutures.immediateFailedFuture(rpcError);
        }
    };
    // Make the call
    MutateRowsAttemptCallable attemptCallable = new MutateRowsAttemptCallable(innerCallable, request, callContext, retryCodes);
    attemptCallable.setExternalFuture(parentFuture);
    attemptCallable.call();
    // Overall expectations: retryable error
    Throwable actualError = null;
    try {
        parentFuture.attemptFuture.get();
    } catch (Throwable t) {
        actualError = t.getCause();
    }
    assertThat(actualError).isInstanceOf(MutateRowsException.class);
    assertThat(((MutateRowsException) actualError).isRetryable()).isTrue();
    // Entry expectations: both entries failed with an error whose cause is the rpc error
    @SuppressWarnings("ConstantConditions") List<FailedMutation> failedMutations = ((MutateRowsException) actualError).getFailedMutations();
    assertThat(failedMutations).hasSize(2);
    assertThat(failedMutations.get(0).getIndex()).isEqualTo(0);
    assertThat(failedMutations.get(0).getError().isRetryable()).isTrue();
    assertThat(failedMutations.get(0).getError().getCause()).isEqualTo(rpcError);
    assertThat(failedMutations.get(1).getIndex()).isEqualTo(1);
    assertThat(failedMutations.get(1).getError().isRetryable()).isTrue();
    assertThat(failedMutations.get(1).getError().getCause()).isEqualTo(rpcError);
}
Also used : UnaryCallable(com.google.api.gax.rpc.UnaryCallable) UnavailableException(com.google.api.gax.rpc.UnavailableException) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) FailedMutation(com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation) MutateRowsResponse(com.google.bigtable.v2.MutateRowsResponse) MutateRowsException(com.google.cloud.bigtable.data.v2.models.MutateRowsException) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) List(java.util.List) Test(org.junit.Test)

Aggregations

ApiCallContext (com.google.api.gax.rpc.ApiCallContext)21 Test (org.junit.Test)11 List (java.util.List)8 ApiFuture (com.google.api.core.ApiFuture)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)3 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)3 MutateRowsRequest (com.google.bigtable.v2.MutateRowsRequest)3 MutateRowsResponse (com.google.bigtable.v2.MutateRowsResponse)3 CallContextConfigurator (com.google.cloud.spanner.SpannerOptions.CallContextConfigurator)3 UnavailableException (com.google.api.gax.rpc.UnavailableException)2 MutateRowsException (com.google.cloud.bigtable.data.v2.models.MutateRowsException)2 FailedMutation (com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation)2 SpannerCallContextTimeoutConfigurator (com.google.cloud.spanner.SpannerOptions.SpannerCallContextTimeoutConfigurator)2 ImmutableList (com.google.common.collect.ImmutableList)2 Context (io.grpc.Context)2 MethodDescriptor (io.grpc.MethodDescriptor)2 ApiService (com.google.api.core.ApiService)1 Batcher (com.google.api.gax.batching.Batcher)1