|
| 1 | +/* |
| 2 | + * Copyright 2023 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.gax.rpc.ApiCallContext; |
| 19 | +import com.google.api.gax.rpc.DeadlineExceededException; |
| 20 | +import com.google.api.gax.rpc.ResourceExhaustedException; |
| 21 | +import com.google.api.gax.rpc.ResponseObserver; |
| 22 | +import com.google.api.gax.rpc.ServerStreamingCallable; |
| 23 | +import com.google.api.gax.rpc.StreamController; |
| 24 | +import com.google.api.gax.rpc.UnavailableException; |
| 25 | +import com.google.bigtable.v2.MutateRowsRequest; |
| 26 | +import com.google.bigtable.v2.MutateRowsResponse; |
| 27 | +import com.google.bigtable.v2.RateLimitInfo; |
| 28 | +import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracer; |
| 29 | +import com.google.common.annotations.VisibleForTesting; |
| 30 | +import com.google.common.base.Preconditions; |
| 31 | +import com.google.common.base.Stopwatch; |
| 32 | +import com.google.common.util.concurrent.RateLimiter; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.atomic.AtomicReference; |
| 35 | +import java.util.logging.Level; |
| 36 | +import java.util.logging.Logger; |
| 37 | +import javax.annotation.Nonnull; |
| 38 | +import org.threeten.bp.Duration; |
| 39 | +import org.threeten.bp.Instant; |
| 40 | + |
| 41 | +class RateLimitingServerStreamingCallable |
| 42 | + extends ServerStreamingCallable<MutateRowsRequest, MutateRowsResponse> { |
| 43 | + private static final Logger logger = |
| 44 | + Logger.getLogger(RateLimitingServerStreamingCallable.class.getName()); |
| 45 | + |
| 46 | + // When the mutation size is large, starting with a higher QPS will make |
| 47 | + // the dataflow job fail very quickly. Start with lower QPS and increase |
| 48 | + // the QPS gradually if the server doesn't push back |
| 49 | + private static final long DEFAULT_QPS = 10; |
| 50 | + |
| 51 | + // Default interval before changing the QPS on error responses |
| 52 | + private static final Duration DEFAULT_PERIOD = Duration.ofSeconds(10); |
| 53 | + |
| 54 | + // Minimum QPS to make sure the job is not stuck |
| 55 | + private static final double MIN_QPS = 0.1; |
| 56 | + private static final double MAX_QPS = 100_000; |
| 57 | + |
| 58 | + // QPS can be lowered to at most MIN_FACTOR * currentQps. When server returned |
| 59 | + // an error, use MIN_FACTOR to calculate the new QPS. This is the same as |
| 60 | + // the server side cap. |
| 61 | + @VisibleForTesting static final double MIN_FACTOR = 0.7; |
| 62 | + |
| 63 | + // QPS can be increased to at most MAX_FACTOR * currentQps. This is the same |
| 64 | + // as the server side cap |
| 65 | + private static final double MAX_FACTOR = 1.3; |
| 66 | + |
| 67 | + private final RateLimiter limiter; |
| 68 | + |
| 69 | + private final AtomicReference<Instant> lastQpsChangeTime = new AtomicReference<>(Instant.now()); |
| 70 | + private final ServerStreamingCallable<MutateRowsRequest, MutateRowsResponse> innerCallable; |
| 71 | + |
| 72 | + RateLimitingServerStreamingCallable( |
| 73 | + @Nonnull ServerStreamingCallable<MutateRowsRequest, MutateRowsResponse> innerCallable) { |
| 74 | + this.limiter = RateLimiter.create(DEFAULT_QPS); |
| 75 | + this.innerCallable = Preconditions.checkNotNull(innerCallable, "Inner callable must be set"); |
| 76 | + logger.info("Rate limiting is enabled with initial QPS of " + limiter.getRate()); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void call( |
| 81 | + MutateRowsRequest request, |
| 82 | + ResponseObserver<MutateRowsResponse> responseObserver, |
| 83 | + ApiCallContext context) { |
| 84 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
| 85 | + limiter.acquire(); |
| 86 | + stopwatch.stop(); |
| 87 | + if (context.getTracer() instanceof BigtableTracer) { |
| 88 | + ((BigtableTracer) context.getTracer()) |
| 89 | + .batchRequestThrottled(stopwatch.elapsed(TimeUnit.MILLISECONDS)); |
| 90 | + } |
| 91 | + RateLimitingResponseObserver innerObserver = |
| 92 | + new RateLimitingResponseObserver(limiter, lastQpsChangeTime, responseObserver); |
| 93 | + innerCallable.call(request, innerObserver, context); |
| 94 | + } |
| 95 | + |
| 96 | + class RateLimitingResponseObserver extends SafeResponseObserver<MutateRowsResponse> { |
| 97 | + private final ResponseObserver<MutateRowsResponse> outerObserver; |
| 98 | + private final RateLimiter rateLimiter; |
| 99 | + |
| 100 | + private final AtomicReference<Instant> lastQpsChangeTime; |
| 101 | + |
| 102 | + RateLimitingResponseObserver( |
| 103 | + RateLimiter rateLimiter, |
| 104 | + AtomicReference<Instant> lastQpsChangeTime, |
| 105 | + ResponseObserver<MutateRowsResponse> observer) { |
| 106 | + super(observer); |
| 107 | + this.outerObserver = observer; |
| 108 | + this.rateLimiter = rateLimiter; |
| 109 | + this.lastQpsChangeTime = lastQpsChangeTime; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected void onStartImpl(StreamController controller) { |
| 114 | + outerObserver.onStart(controller); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected void onResponseImpl(MutateRowsResponse response) { |
| 119 | + if (response.hasRateLimitInfo()) { |
| 120 | + RateLimitInfo info = response.getRateLimitInfo(); |
| 121 | + // RateLimitInfo is an optional field. However, proto3 sub-message field always |
| 122 | + // have presence even thought it's marked as "optional". Check the factor and |
| 123 | + // period to make sure they're not 0. |
| 124 | + if (info.getFactor() != 0 && info.getPeriod().getSeconds() != 0) { |
| 125 | + updateQps( |
| 126 | + info.getFactor(), |
| 127 | + Duration.ofSeconds(com.google.protobuf.util.Durations.toSeconds(info.getPeriod()))); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected void onErrorImpl(Throwable t) { |
| 134 | + // When server returns DEADLINE_EXCEEDED, UNAVAILABLE or RESOURCE_EXHAUSTED, |
| 135 | + // assume cbt server is overloaded |
| 136 | + if (t instanceof DeadlineExceededException |
| 137 | + || t instanceof UnavailableException |
| 138 | + || t instanceof ResourceExhaustedException) { |
| 139 | + updateQps(MIN_FACTOR, DEFAULT_PERIOD); |
| 140 | + } |
| 141 | + outerObserver.onError(t); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + protected void onCompleteImpl() { |
| 146 | + outerObserver.onComplete(); |
| 147 | + } |
| 148 | + |
| 149 | + private void updateQps(double factor, Duration period) { |
| 150 | + Instant lastTime = lastQpsChangeTime.get(); |
| 151 | + Instant now = Instant.now(); |
| 152 | + |
| 153 | + if (now.minus(period).isAfter(lastTime) && lastQpsChangeTime.compareAndSet(lastTime, now)) { |
| 154 | + double cappedFactor = Math.min(Math.max(factor, MIN_FACTOR), MAX_FACTOR); |
| 155 | + double currentRate = limiter.getRate(); |
| 156 | + limiter.setRate(Math.min(Math.max(currentRate * cappedFactor, MIN_QPS), MAX_QPS)); |
| 157 | + logger.log( |
| 158 | + Level.FINE, |
| 159 | + "Updated QPS from {0} to {1}, server returned factor is {2}, capped factor is {3}", |
| 160 | + new Object[] {currentRate, limiter.getRate(), factor, cappedFactor}); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @VisibleForTesting |
| 166 | + AtomicReference<Instant> getLastQpsChangeTime() { |
| 167 | + return lastQpsChangeTime; |
| 168 | + } |
| 169 | + |
| 170 | + @VisibleForTesting |
| 171 | + double getCurrentRate() { |
| 172 | + return limiter.getRate(); |
| 173 | + } |
| 174 | +} |
0 commit comments