|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.data.v2.stub.mutaterows; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.api.gax.batching.PartitionKey; |
| 20 | +import com.google.api.gax.batching.RequestBuilder; |
| 21 | +import com.google.api.gax.grpc.GrpcStatusCode; |
| 22 | +import com.google.api.gax.rpc.ApiException; |
| 23 | +import com.google.api.gax.rpc.ApiExceptionFactory; |
| 24 | +import com.google.api.gax.rpc.BatchedRequestIssuer; |
| 25 | +import com.google.api.gax.rpc.BatchingDescriptor; |
| 26 | +import com.google.api.gax.rpc.StatusCode; |
| 27 | +import com.google.bigtable.v2.MutateRowsRequest; |
| 28 | +import com.google.bigtable.v2.MutateRowsResponse; |
| 29 | +import com.google.common.base.Preconditions; |
| 30 | +import com.google.common.collect.ImmutableSet; |
| 31 | +import com.google.common.primitives.Ints; |
| 32 | +import com.google.rpc.Code; |
| 33 | +import com.google.rpc.Status; |
| 34 | +import io.grpc.StatusException; |
| 35 | +import io.grpc.StatusRuntimeException; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.Set; |
| 38 | + |
| 39 | +/** |
| 40 | + * A custom implementation of a {@link BatchingDescriptor} to split individual results of a bulk |
| 41 | + * MutateRowsResponse. Each individual result will be matched with its issuer. Since the embedded |
| 42 | + * results bypass gax's result processing chains, this class is responsible for wrapping errors in |
| 43 | + * {@link ApiException}s and marking each error as retryable. |
| 44 | + * |
| 45 | + * <p>This class is considered an internal implementation detail and not meant to be used by |
| 46 | + * applications directly. |
| 47 | + */ |
| 48 | +@InternalApi |
| 49 | +public class MutateRowsBatchingDescriptor |
| 50 | + implements BatchingDescriptor<MutateRowsRequest, MutateRowsResponse> { |
| 51 | + |
| 52 | + // Shared response to notify individual issuers of a successful mutation. |
| 53 | + private static final MutateRowsResponse OK_RESPONSE = |
| 54 | + MutateRowsResponse.newBuilder() |
| 55 | + .addEntries( |
| 56 | + MutateRowsResponse.Entry.newBuilder() |
| 57 | + .setIndex(0) |
| 58 | + .setStatus(Status.newBuilder().setCode(Code.OK_VALUE))) |
| 59 | + .build(); |
| 60 | + |
| 61 | + private final ImmutableSet<StatusCode.Code> retryableCodes; |
| 62 | + |
| 63 | + public MutateRowsBatchingDescriptor(Set<StatusCode.Code> retryableCodes) { |
| 64 | + this.retryableCodes = ImmutableSet.copyOf(retryableCodes); |
| 65 | + } |
| 66 | + |
| 67 | + /** Return the target table name. This will be used to combine batcheable requests */ |
| 68 | + @Override |
| 69 | + public PartitionKey getBatchPartitionKey(MutateRowsRequest request) { |
| 70 | + return new PartitionKey(request.getTableName()); |
| 71 | + } |
| 72 | + |
| 73 | + /** {@inheritDoc} */ |
| 74 | + @Override |
| 75 | + public RequestBuilder<MutateRowsRequest> getRequestBuilder() { |
| 76 | + return new MyRequestBuilder(); |
| 77 | + } |
| 78 | + |
| 79 | + /** {@inheritDoc} */ |
| 80 | + @Override |
| 81 | + public void splitResponse( |
| 82 | + MutateRowsResponse batchResponse, |
| 83 | + Collection<? extends BatchedRequestIssuer<MutateRowsResponse>> batch) { |
| 84 | + |
| 85 | + // Sort the result entries by index. |
| 86 | + Status[] sortedEntries = new Status[batchResponse.getEntriesCount()]; |
| 87 | + |
| 88 | + for (MutateRowsResponse.Entry entry : batchResponse.getEntriesList()) { |
| 89 | + int index = Ints.checkedCast(entry.getIndex()); |
| 90 | + Preconditions.checkState( |
| 91 | + sortedEntries[index] == null, "Got multiple results for the same sub-mutation"); |
| 92 | + sortedEntries[index] = entry.getStatus(); |
| 93 | + } |
| 94 | + |
| 95 | + // Notify all of issuers of the corresponding result. |
| 96 | + int i = 0; |
| 97 | + for (BatchedRequestIssuer<MutateRowsResponse> issuer : batch) { |
| 98 | + Status entry = sortedEntries[i++]; |
| 99 | + Preconditions.checkState(entry != null, "Missing result for entry"); |
| 100 | + |
| 101 | + if (entry.getCode() == Code.OK_VALUE) { |
| 102 | + issuer.setResponse(OK_RESPONSE); |
| 103 | + } else { |
| 104 | + issuer.setException(createElementException(entry)); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** {@inheritDoc} */ |
| 110 | + @Override |
| 111 | + public void splitException( |
| 112 | + Throwable throwable, Collection<? extends BatchedRequestIssuer<MutateRowsResponse>> batch) { |
| 113 | + throwable = createElementException(throwable); |
| 114 | + |
| 115 | + for (BatchedRequestIssuer<MutateRowsResponse> responder : batch) { |
| 116 | + responder.setException(throwable); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** {@inheritDoc} */ |
| 121 | + @Override |
| 122 | + public long countElements(MutateRowsRequest request) { |
| 123 | + return request.getEntriesCount(); |
| 124 | + } |
| 125 | + |
| 126 | + /** {@inheritDoc} */ |
| 127 | + @Override |
| 128 | + public long countBytes(MutateRowsRequest request) { |
| 129 | + return request.getSerializedSize(); |
| 130 | + } |
| 131 | + |
| 132 | + /** Convert an element error Status into an ApiException */ |
| 133 | + private ApiException createElementException(Status protoStatus) { |
| 134 | + Preconditions.checkArgument(protoStatus.getCode() != Code.OK_VALUE, "OK is not an error"); |
| 135 | + |
| 136 | + StatusRuntimeException throwable = |
| 137 | + io.grpc.Status.fromCodeValue(protoStatus.getCode()) |
| 138 | + .withDescription(protoStatus.getMessage()) |
| 139 | + .asRuntimeException(); |
| 140 | + |
| 141 | + return createElementException(throwable); |
| 142 | + } |
| 143 | + |
| 144 | + /** Convert a Throwable into an ApiException, marking it as retryable when appropriate. */ |
| 145 | + private ApiException createElementException(Throwable throwable) { |
| 146 | + final io.grpc.Status.Code code; |
| 147 | + |
| 148 | + if (throwable instanceof ApiException) { |
| 149 | + return (ApiException) throwable; |
| 150 | + } else if (throwable instanceof StatusRuntimeException) { |
| 151 | + code = ((StatusRuntimeException) throwable).getStatus().getCode(); |
| 152 | + } else if (throwable instanceof StatusException) { |
| 153 | + code = ((StatusException) throwable).getStatus().getCode(); |
| 154 | + } else { |
| 155 | + code = io.grpc.Status.Code.UNKNOWN; |
| 156 | + } |
| 157 | + |
| 158 | + GrpcStatusCode gaxStatusCode = GrpcStatusCode.of(code); |
| 159 | + boolean isRetryable = retryableCodes.contains(gaxStatusCode.getCode()); |
| 160 | + |
| 161 | + return ApiExceptionFactory.createException(throwable, gaxStatusCode, isRetryable); |
| 162 | + } |
| 163 | + |
| 164 | + /** A {@link com.google.api.gax.batching.RequestBuilder} that can aggregate MutateRowsRequest */ |
| 165 | + static class MyRequestBuilder implements RequestBuilder<MutateRowsRequest> { |
| 166 | + private MutateRowsRequest.Builder builder; |
| 167 | + |
| 168 | + @Override |
| 169 | + public void appendRequest(MutateRowsRequest request) { |
| 170 | + if (builder == null) { |
| 171 | + builder = request.toBuilder(); |
| 172 | + } else { |
| 173 | + builder.addAllEntries(request.getEntriesList()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public MutateRowsRequest build() { |
| 179 | + return builder.build(); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments