Search in sources :

Example 11 with MutateRowsRequest

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

the class MutateRowsAttemptCallableTest method nextAttemptTest.

@Test
public void nextAttemptTest() {
    // Setup the request & response for the first call
    MutateRowsRequest request = MutateRowsRequest.newBuilder().addEntries(Entry.newBuilder().setRowKey(ByteString.copyFromUtf8("0-ok"))).addEntries(Entry.newBuilder().setRowKey(ByteString.copyFromUtf8("1-unavailable"))).addEntries(Entry.newBuilder().setRowKey(ByteString.copyFromUtf8("2-invalid"))).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 first call
    attemptCallable.call();
    // Setup the request & response for the next call
    innerCallable.response = Lists.newArrayList(MutateRowsResponse.newBuilder().addEntries(MutateRowsResponse.Entry.newBuilder().setIndex(0).setStatus(OK_STATUS_PROTO)).build());
    attemptCallable.call();
    // Make sure that only the entry with the transient error is resubmitted
    assertThat(innerCallable.lastRequest.getEntriesCount()).isEqualTo(1);
    assertThat(innerCallable.lastRequest.getEntries(0).getRowKey()).isEqualTo(ByteString.copyFromUtf8("1-unavailable"));
    // 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()).isFalse();
    // Entry expectations
    @SuppressWarnings("ConstantConditions") List<FailedMutation> failedMutations = ((MutateRowsException) actualError).getFailedMutations();
    assertThat(failedMutations).hasSize(1);
    assertThat(failedMutations.get(0).getIndex()).isEqualTo(2);
    assertThat(failedMutations.get(0).getError().getStatusCode().getCode()).isEqualTo(Code.INVALID_ARGUMENT);
    assertThat(failedMutations.get(0).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)

Example 12 with MutateRowsRequest

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

the class BulkMutationTest method cloneTest.

@Test
public void cloneTest() {
    BulkMutation originalBulkMutation = BulkMutation.create(TABLE_ID).add("test-rowKey", Mutation.create().setCell("fake-family1", "fake-qualifier1", 12345, "fake-value1"));
    MutateRowsRequest originalRequest = originalBulkMutation.toProto(REQUEST_CONTEXT);
    BulkMutation clonedMutation = originalBulkMutation.clone();
    MutateRowsRequest clonedRequest = clonedMutation.toProto(REQUEST_CONTEXT);
    // Both BulkMutations should be equals.
    assertThat(clonedRequest).isEqualTo(originalRequest);
    assertThat(clonedRequest.getTableName()).isEqualTo(originalRequest.getTableName());
    assertThat(clonedRequest.getEntriesList()).isEqualTo(originalRequest.getEntriesList());
    // Mutating cloned BulkMutation
    clonedMutation.add("another-rowKey", Mutation.create().deleteCells("delete-family", "delete-qualifier"));
    assertThat(clonedMutation.toProto(REQUEST_CONTEXT)).isNotEqualTo(originalRequest);
}
Also used : MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) Test(org.junit.Test)

Example 13 with MutateRowsRequest

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

the class MutateRowsAttemptCallable method handleAttemptSuccess.

/**
 * Handle entry level failures. All new response entries are inspected for failure. If any
 * transient failures are found, their corresponding mutations are scheduled for the next RPC. The
 * caller is notified of both new found errors and pre-existing permanent errors in the thrown
 * {@link MutateRowsException}. If no errors exist, then the attempt future is successfully
 * completed.
 */
private void handleAttemptSuccess(List<MutateRowsResponse> responses) {
    List<FailedMutation> allFailures = Lists.newArrayList(permanentFailures);
    MutateRowsRequest lastRequest = currentRequest;
    Builder builder = lastRequest.toBuilder().clearEntries();
    List<Integer> newOriginalIndexes = Lists.newArrayList();
    for (MutateRowsResponse response : responses) {
        for (Entry entry : response.getEntriesList()) {
            if (entry.getStatus().getCode() == Code.OK_VALUE) {
                continue;
            }
            int origIndex = getOriginalIndex((int) entry.getIndex());
            FailedMutation failedMutation = FailedMutation.create(origIndex, createEntryError(entry.getStatus()));
            allFailures.add(failedMutation);
            if (!failedMutation.getError().isRetryable()) {
                permanentFailures.add(failedMutation);
            } else {
                // Schedule the mutation entry for the next RPC by adding it to the request builder and
                // recording it's original index
                newOriginalIndexes.add(origIndex);
                builder.addEntries(lastRequest.getEntries((int) entry.getIndex()));
            }
        }
    }
    currentRequest = builder.build();
    originalIndexes = newOriginalIndexes;
    if (!allFailures.isEmpty()) {
        boolean isRetryable = builder.getEntriesCount() > 0;
        throw new MutateRowsException(null, allFailures, isRetryable);
    }
}
Also used : Entry(com.google.bigtable.v2.MutateRowsResponse.Entry) MutateRowsResponse(com.google.bigtable.v2.MutateRowsResponse) MutateRowsException(com.google.cloud.bigtable.data.v2.models.MutateRowsException) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) Builder(com.google.bigtable.v2.MutateRowsRequest.Builder) FailedMutation(com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation)

Example 14 with MutateRowsRequest

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

the class MutateRowsAttemptCallable method handleAttemptError.

/**
 * Handle an RPC level failure by generating a {@link FailedMutation} for each expected entry. The
 * newly generated {@link FailedMutation}s will be combined with the permanentFailures to give the
 * caller the whole picture since the first call. This method will always throw a {@link
 * MutateRowsException}.
 */
private void handleAttemptError(Throwable rpcError) {
    ApiException entryError = createSyntheticErrorForRpcFailure(rpcError);
    ImmutableList.Builder<FailedMutation> allFailures = ImmutableList.builder();
    MutateRowsRequest lastRequest = currentRequest;
    allFailures.addAll(permanentFailures);
    Builder builder = lastRequest.toBuilder().clearEntries();
    List<Integer> newOriginalIndexes = Lists.newArrayList();
    for (int i = 0; i < currentRequest.getEntriesCount(); i++) {
        int origIndex = getOriginalIndex(i);
        FailedMutation failedMutation = FailedMutation.create(origIndex, entryError);
        allFailures.add(failedMutation);
        if (!failedMutation.getError().isRetryable()) {
            permanentFailures.add(failedMutation);
        } else {
            // Schedule the mutation entry for the next RPC by adding it to the request builder and
            // recording its original index
            newOriginalIndexes.add(origIndex);
            builder.addEntries(lastRequest.getEntries(i));
        }
    }
    currentRequest = builder.build();
    originalIndexes = newOriginalIndexes;
    throw new MutateRowsException(rpcError, allFailures.build(), entryError.isRetryable());
}
Also used : MutateRowsException(com.google.cloud.bigtable.data.v2.models.MutateRowsException) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) ImmutableList(com.google.common.collect.ImmutableList) Builder(com.google.bigtable.v2.MutateRowsRequest.Builder) FailedMutation(com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation) ApiException(com.google.api.gax.rpc.ApiException)

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