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);
}
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);
}
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;
}
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);
}
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);
}
Aggregations