|
| 1 | +/* |
| 2 | + * Copyright 2020 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; |
| 17 | + |
| 18 | +import com.google.api.core.BetaApi; |
| 19 | +import com.google.api.gax.batching.BatchingCallSettings; |
| 20 | +import com.google.api.gax.batching.BatchingDescriptor; |
| 21 | +import com.google.api.gax.batching.BatchingSettings; |
| 22 | +import com.google.api.gax.retrying.RetrySettings; |
| 23 | +import com.google.api.gax.rpc.StatusCode; |
| 24 | +import com.google.api.gax.rpc.UnaryCallSettings; |
| 25 | +import com.google.cloud.bigtable.data.v2.models.Query; |
| 26 | +import com.google.cloud.bigtable.data.v2.models.Row; |
| 27 | +import com.google.common.base.Preconditions; |
| 28 | +import com.google.protobuf.ByteString; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Set; |
| 31 | +import javax.annotation.Nonnull; |
| 32 | + |
| 33 | +/** |
| 34 | + * This settings holds the batching thresholds as well as retry configuration for bulk read API. |
| 35 | + * |
| 36 | + * <p>Sample configuration: |
| 37 | + * |
| 38 | + * <pre>{@code |
| 39 | + * BigtableBulkReadRowsCallSettings defaultBulkReadCallSettings = |
| 40 | + * bigtableDataCallSettings.getStubSettings().bulkReadRowsSettings(); |
| 41 | + * |
| 42 | + * BigtableBulkReadRowsCallSettings customBulkReadCallSettings = defaultBulkReadCallSettings |
| 43 | + * .toBuilder() |
| 44 | + * .setBatchingSettings( |
| 45 | + * defaultBulkReadCallSettings.getBatchingSettings().toBuilder() |
| 46 | + * .setDelayThreshold(Duration.ofSeconds(10)) |
| 47 | + * .build()) |
| 48 | + * .setRetryableCodes(Code.DEADLINE_EXCEEDED) |
| 49 | + * .build(); |
| 50 | + * }</pre> |
| 51 | + * |
| 52 | + * @see BatchingSettings for batching thresholds explantion. |
| 53 | + * @see RetrySettings for retry configuration. |
| 54 | + */ |
| 55 | +@BetaApi("This surface is likely to change as the batching surface evolves.") |
| 56 | +public class BigtableBulkReadRowsCallSettings extends UnaryCallSettings<Query, List<Row>> { |
| 57 | + |
| 58 | + private final BatchingCallSettings<ByteString, Row, Query, List<Row>> batchingCallSettings; |
| 59 | + |
| 60 | + private BigtableBulkReadRowsCallSettings(Builder builder) { |
| 61 | + super(builder); |
| 62 | + batchingCallSettings = |
| 63 | + BatchingCallSettings.newBuilder(builder.batchingDescriptor) |
| 64 | + .setBatchingSettings(builder.batchingSettings) |
| 65 | + .setRetrySettings(builder.getRetrySettings()) |
| 66 | + .setRetryableCodes(builder.getRetryableCodes()) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + /** Returns batching settings which contains multiple batch threshold levels. */ |
| 71 | + public BatchingSettings getBatchingSettings() { |
| 72 | + return batchingCallSettings.getBatchingSettings(); |
| 73 | + } |
| 74 | + |
| 75 | + /** Returns an adapter that packs and unpacks batching elements. */ |
| 76 | + BatchingDescriptor<ByteString, Row, Query, List<Row>> getBatchingDescriptor() { |
| 77 | + return batchingCallSettings.getBatchingDescriptor(); |
| 78 | + } |
| 79 | + |
| 80 | + static BigtableBulkReadRowsCallSettings.Builder newBuilder( |
| 81 | + BatchingDescriptor<ByteString, Row, Query, List<Row>> batchingDescriptor) { |
| 82 | + return new Builder(batchingDescriptor); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get a builder with the same values as this object. See the class documentation of {@link |
| 87 | + * BigtableBatchingCallSettings} for a sample settings configuration. |
| 88 | + */ |
| 89 | + @Override |
| 90 | + public final BigtableBulkReadRowsCallSettings.Builder toBuilder() { |
| 91 | + return new BigtableBulkReadRowsCallSettings.Builder(this); |
| 92 | + } |
| 93 | + |
| 94 | + public static class Builder extends UnaryCallSettings.Builder<Query, List<Row>> { |
| 95 | + |
| 96 | + private BatchingDescriptor<ByteString, Row, Query, List<Row>> batchingDescriptor; |
| 97 | + private BatchingSettings batchingSettings; |
| 98 | + |
| 99 | + private Builder( |
| 100 | + @Nonnull BatchingDescriptor<ByteString, Row, Query, List<Row>> batchingDescriptor) { |
| 101 | + this.batchingDescriptor = |
| 102 | + Preconditions.checkNotNull(batchingDescriptor, "batching descriptor can't be null"); |
| 103 | + } |
| 104 | + |
| 105 | + private Builder(@Nonnull BigtableBulkReadRowsCallSettings settings) { |
| 106 | + super(settings); |
| 107 | + this.batchingDescriptor = settings.getBatchingDescriptor(); |
| 108 | + this.batchingSettings = settings.getBatchingSettings(); |
| 109 | + } |
| 110 | + |
| 111 | + /** Sets the batching settings with various thresholds. */ |
| 112 | + public Builder setBatchingSettings(@Nonnull BatchingSettings batchingSettings) { |
| 113 | + Preconditions.checkNotNull(batchingSettings, "batching settings can't be null"); |
| 114 | + this.batchingSettings = batchingSettings; |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + /** Returns the {@link BatchingSettings}. */ |
| 119 | + public BatchingSettings getBatchingSettings() { |
| 120 | + return batchingSettings; |
| 121 | + } |
| 122 | + |
| 123 | + /** Sets the rpc failure {@link StatusCode.Code code}, for which retries should be performed. */ |
| 124 | + @Override |
| 125 | + public Builder setRetryableCodes(StatusCode.Code... codes) { |
| 126 | + super.setRetryableCodes(codes); |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + /** Sets the rpc failure {@link StatusCode.Code code}, for which retries should be performed. */ |
| 131 | + @Override |
| 132 | + public Builder setRetryableCodes(Set<StatusCode.Code> retryableCodes) { |
| 133 | + super.setRetryableCodes(retryableCodes); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + /** Sets the {@link RetrySettings} values for each retry attempts. */ |
| 138 | + @Override |
| 139 | + public Builder setRetrySettings(@Nonnull RetrySettings retrySettings) { |
| 140 | + super.setRetrySettings(retrySettings); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /** Builds the {@link BigtableBulkReadRowsCallSettings} object with provided configuration. */ |
| 145 | + @Override |
| 146 | + public BigtableBulkReadRowsCallSettings build() { |
| 147 | + return new BigtableBulkReadRowsCallSettings(this); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments