Search in sources :

Example 1 with MutateRowsRequest

use of com.google.bigtable.v2.MutateRowsRequest in project java-bigtable by googleapis.

the class EnhancedBigtableStub method createBulkMutateRowsCallable.

/**
 * Creates a callable chain to handle MutatesRows RPCs. This is meant to be used for manual
 * batching. The chain will:
 *
 * <ul>
 *   <li>Convert a {@link BulkMutation} into a {@link MutateRowsRequest}.
 *   <li>Process the response and schedule retries. At the end of each attempt, entries that have
 *       been applied, are filtered from the next attempt. Also, any entries that failed with a
 *       nontransient error, are filtered from the next attempt. This will continue until there
 *       are no more entries or there are no more retry attempts left.
 *   <li>Wrap batch failures in a {@link
 *       com.google.cloud.bigtable.data.v2.models.MutateRowsException}.
 *   <li>Add tracing & metrics.
 * </ul>
 */
private UnaryCallable<BulkMutation, Void> createBulkMutateRowsCallable() {
    UnaryCallable<MutateRowsRequest, Void> baseCallable = createMutateRowsBaseCallable();
    UnaryCallable<MutateRowsRequest, Void> flowControlCallable = null;
    if (settings.bulkMutateRowsSettings().isLatencyBasedThrottlingEnabled()) {
        flowControlCallable = new DynamicFlowControlCallable(baseCallable, bulkMutationFlowController, bulkMutationDynamicFlowControlStats, settings.bulkMutateRowsSettings().getTargetRpcLatencyMs(), FLOW_CONTROL_ADJUSTING_INTERVAL_MS);
    }
    UnaryCallable<BulkMutation, Void> userFacing = new BulkMutateRowsUserFacingCallable(flowControlCallable != null ? flowControlCallable : baseCallable, requestContext);
    SpanName spanName = getSpanName("MutateRows");
    UnaryCallable<BulkMutation, Void> tracedBatcher = new TracedBatcherUnaryCallable<>(userFacing);
    UnaryCallable<BulkMutation, Void> withHeaderTracer = new HeaderTracerUnaryCallable<>(tracedBatcher);
    UnaryCallable<BulkMutation, Void> traced = new TracedUnaryCallable<>(withHeaderTracer, clientContext.getTracerFactory(), spanName);
    return traced.withDefaultCallContext(clientContext.getDefaultCallContext());
}
Also used : BulkMutation(com.google.cloud.bigtable.data.v2.models.BulkMutation) BulkMutateRowsUserFacingCallable(com.google.cloud.bigtable.data.v2.stub.mutaterows.BulkMutateRowsUserFacingCallable) SpanName(com.google.api.gax.tracing.SpanName) TracedUnaryCallable(com.google.api.gax.tracing.TracedUnaryCallable) HeaderTracerUnaryCallable(com.google.cloud.bigtable.data.v2.stub.metrics.HeaderTracerUnaryCallable) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) TracedBatcherUnaryCallable(com.google.cloud.bigtable.data.v2.stub.metrics.TracedBatcherUnaryCallable)

Example 2 with MutateRowsRequest

use of com.google.bigtable.v2.MutateRowsRequest in project java-bigtable by googleapis.

the class EnhancedBigtableStub method createMutateRowsBaseCallable.

/**
 * Internal helper to create the base MutateRows callable chain. The chain is responsible for
 * retrying individual entry in case of error.
 *
 * <p>NOTE: the caller is responsible for adding tracing & metrics.
 *
 * @see MutateRowsRetryingCallable for more details
 */
private UnaryCallable<MutateRowsRequest, Void> createMutateRowsBaseCallable() {
    ServerStreamingCallable<MutateRowsRequest, MutateRowsResponse> base = GrpcRawCallableFactory.createServerStreamingCallable(GrpcCallSettings.<MutateRowsRequest, MutateRowsResponse>newBuilder().setMethodDescriptor(BigtableGrpc.getMutateRowsMethod()).setParamsExtractor(new RequestParamsExtractor<MutateRowsRequest>() {

        @Override
        public Map<String, String> extract(MutateRowsRequest mutateRowsRequest) {
            return ImmutableMap.of("table_name", mutateRowsRequest.getTableName(), "app_profile_id", mutateRowsRequest.getAppProfileId());
        }
    }).build(), settings.bulkMutateRowsSettings().getRetryableCodes());
    ServerStreamingCallable<MutateRowsRequest, MutateRowsResponse> withStatsHeaders = new StatsHeadersServerStreamingCallable<>(base);
    RetryAlgorithm<Void> retryAlgorithm = new RetryAlgorithm<>(new ApiResultRetryAlgorithm<Void>(), new ExponentialRetryAlgorithm(settings.bulkMutateRowsSettings().getRetrySettings(), clientContext.getClock()));
    RetryingExecutorWithContext<Void> retryingExecutor = new ScheduledRetryingExecutor<>(retryAlgorithm, clientContext.getExecutor());
    return new MutateRowsRetryingCallable(clientContext.getDefaultCallContext(), withStatsHeaders, retryingExecutor, settings.bulkMutateRowsSettings().getRetryableCodes());
}
Also used : RetryAlgorithm(com.google.api.gax.retrying.RetryAlgorithm) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) ApiResultRetryAlgorithm(com.google.cloud.bigtable.gaxx.retrying.ApiResultRetryAlgorithm) ExponentialRetryAlgorithm(com.google.api.gax.retrying.ExponentialRetryAlgorithm) StatsHeadersServerStreamingCallable(com.google.cloud.bigtable.data.v2.stub.metrics.StatsHeadersServerStreamingCallable) ScheduledRetryingExecutor(com.google.api.gax.retrying.ScheduledRetryingExecutor) MutateRowsResponse(com.google.bigtable.v2.MutateRowsResponse) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) MutateRowsRetryingCallable(com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsRetryingCallable)

Example 3 with MutateRowsRequest

use of com.google.bigtable.v2.MutateRowsRequest in project java-bigtable by googleapis.

the class RowMutationTest method toBulkProtoTest.

@Test
public void toBulkProtoTest() {
    long timestampMin = System.currentTimeMillis() * 1_000;
    RowMutation rowMutation = RowMutation.create("fake-table", "fake-key").setCell("fake-family", "fake-qualifier", "fake-value");
    MutateRowsRequest actualRowMutation = rowMutation.toBulkProto(REQUEST_CONTEXT);
    com.google.common.collect.Range<Long> timestampRange = com.google.common.collect.Range.closed(timestampMin, System.currentTimeMillis() * 1_000);
    assertThat(actualRowMutation.getTableName()).isEqualTo(NameUtil.formatTableName(PROJECT_ID, INSTANCE_ID, TABLE_ID));
    assertThat(actualRowMutation.getAppProfileId()).isEqualTo(APP_PROFILE_ID);
    assertThat(actualRowMutation.getEntriesList()).hasSize(1);
    assertThat(actualRowMutation.getEntries(0).getMutationsList()).hasSize(1);
    assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getValue()).isEqualTo(ByteString.copyFromUtf8("fake-value"));
    assertThat(actualRowMutation.getEntries(0).getMutations(0).getSetCell().getTimestampMicros()).isIn(timestampRange);
}
Also used : MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) Test(org.junit.Test)

Example 4 with MutateRowsRequest

use of com.google.bigtable.v2.MutateRowsRequest in project java-bigtable by googleapis.

the class MutateRowsAttemptCallableTest method singleEntrySuccessTest.

@Test
public void singleEntrySuccessTest() throws Exception {
    MutateRowsRequest request = MutateRowsRequest.newBuilder().addEntries(Entry.getDefaultInstance()).build();
    innerCallable.response.add(MutateRowsResponse.newBuilder().addEntries(MutateRowsResponse.Entry.newBuilder().setIndex(0).setStatus(OK_STATUS_PROTO)).build());
    MutateRowsAttemptCallable attemptCallable = new MutateRowsAttemptCallable(innerCallable, request, callContext, retryCodes);
    attemptCallable.setExternalFuture(parentFuture);
    attemptCallable.call();
    // Attempt completed successfully and the useless response has been suppressed
    assertThat(parentFuture.attemptFuture.get()).isNull();
    // innerCallable received the request
    assertThat(innerCallable.lastRequest).isEqualTo(request);
}
Also used : MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) Test(org.junit.Test)

Example 5 with MutateRowsRequest

use of com.google.bigtable.v2.MutateRowsRequest in project java-bigtable by googleapis.

the class MutateRowsAttemptCallableTest method mixedTest.

@Test
public void mixedTest() {
    // Setup the request & response
    MutateRowsRequest request = MutateRowsRequest.newBuilder().addEntries(Entry.getDefaultInstance()).addEntries(Entry.getDefaultInstance()).addEntries(Entry.getDefaultInstance()).build();
    innerCallable.response.add(MutateRowsResponse.newBuilder().addEntries(MutateRowsResponse.Entry.newBuilder().setIndex(0).setStatus(OK_STATUS_PROTO)).addEntries(MutateRowsResponse.Entry.newBuilder().setIndex(1).setStatus(TRANSIENT_ERROR_STATUS_PROTO)).addEntries(MutateRowsResponse.Entry.newBuilder().setIndex(2).setStatus(PERMENANT_ERROR_STATUS_PROTO)).build());
    MutateRowsAttemptCallable attemptCallable = new MutateRowsAttemptCallable(innerCallable, request, callContext, retryCodes);
    attemptCallable.setExternalFuture(parentFuture);
    // Make the only call
    attemptCallable.call();
    // Overall error expectations
    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
    @SuppressWarnings("ConstantConditions") List<FailedMutation> failedMutations = ((MutateRowsException) actualError).getFailedMutations();
    assertThat(failedMutations).hasSize(2);
    assertThat(failedMutations.get(0).getIndex()).isEqualTo(1);
    assertThat(failedMutations.get(0).getError().getStatusCode().getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(failedMutations.get(0).getError().isRetryable()).isTrue();
    assertThat(failedMutations.get(1).getIndex()).isEqualTo(2);
    assertThat(failedMutations.get(1).getError().getStatusCode().getCode()).isEqualTo(Code.INVALID_ARGUMENT);
    assertThat(failedMutations.get(1).getError().isRetryable()).isFalse();
}
Also used : MutateRowsException(com.google.cloud.bigtable.data.v2.models.MutateRowsException) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) FailedMutation(com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation) Test(org.junit.Test)

Aggregations

MutateRowsRequest (com.google.bigtable.v2.MutateRowsRequest)14 Test (org.junit.Test)10 MutateRowsException (com.google.cloud.bigtable.data.v2.models.MutateRowsException)6 FailedMutation (com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation)6 MutateRowsResponse (com.google.bigtable.v2.MutateRowsResponse)4 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)2 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)2 UnavailableException (com.google.api.gax.rpc.UnavailableException)2 Builder (com.google.bigtable.v2.MutateRowsRequest.Builder)2 List (java.util.List)2 ExponentialRetryAlgorithm (com.google.api.gax.retrying.ExponentialRetryAlgorithm)1 RetryAlgorithm (com.google.api.gax.retrying.RetryAlgorithm)1 ScheduledRetryingExecutor (com.google.api.gax.retrying.ScheduledRetryingExecutor)1 ApiException (com.google.api.gax.rpc.ApiException)1 SpanName (com.google.api.gax.tracing.SpanName)1 TracedUnaryCallable (com.google.api.gax.tracing.TracedUnaryCallable)1 Entry (com.google.bigtable.v2.MutateRowsResponse.Entry)1 BulkMutation (com.google.cloud.bigtable.data.v2.models.BulkMutation)1 HeaderTracerUnaryCallable (com.google.cloud.bigtable.data.v2.stub.metrics.HeaderTracerUnaryCallable)1 StatsHeadersServerStreamingCallable (com.google.cloud.bigtable.data.v2.stub.metrics.StatsHeadersServerStreamingCallable)1