|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.batching; |
| 31 | + |
| 32 | +import static com.google.api.gax.batching.FlowController.LimitExceededBehavior; |
| 33 | + |
| 34 | +import com.google.api.core.InternalApi; |
| 35 | +import com.google.api.gax.batching.FlowController.FlowControlException; |
| 36 | +import com.google.common.annotations.VisibleForTesting; |
| 37 | +import com.google.common.base.Preconditions; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import javax.annotation.Nullable; |
| 40 | + |
| 41 | +/** |
| 42 | + * Record the statistics of flow control events. |
| 43 | + * |
| 44 | + * <p>This class is populated by FlowController, which will record throttling events. Currently it |
| 45 | + * only keeps the last flow control event, but it could be expanded to record more information in |
| 46 | + * the future. The events can be used to dynamically adjust concurrency in the client. For example: |
| 47 | + * |
| 48 | + * <pre>{@code |
| 49 | + * // Increase flow control limits if there was throttling in the past 5 minutes and throttled time |
| 50 | + * // was longer than 1 minute. |
| 51 | + * while(true) { |
| 52 | + * FlowControlEvent event = flowControlEventStats.getLastFlowControlEvent(); |
| 53 | + * if (event != null |
| 54 | + * && event.getTimestampMs() > System.currentMillis() - TimeUnit.MINUTES.toMillis(5) |
| 55 | + * && event.getThrottledTimeInMs() > TimeUnit.MINUTES.toMillis(1)) { |
| 56 | + * flowController.increaseThresholds(elementSteps, byteSteps); |
| 57 | + * } |
| 58 | + * Thread.sleep(TimeUnit.MINUTE.toMillis(10)); |
| 59 | + * } |
| 60 | + * }</pre> |
| 61 | + */ |
| 62 | +@InternalApi("For google-cloud-java client use only") |
| 63 | +public class FlowControlEventStats { |
| 64 | + |
| 65 | + private volatile FlowControlEvent lastFlowControlEvent; |
| 66 | + |
| 67 | + // We only need the last event to check if there was throttling in the past X minutes so this |
| 68 | + // doesn't need to be super accurate. |
| 69 | + void recordFlowControlEvent(FlowControlEvent event) { |
| 70 | + if (lastFlowControlEvent == null || event.compareTo(lastFlowControlEvent) > 0) { |
| 71 | + lastFlowControlEvent = event; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public FlowControlEvent getLastFlowControlEvent() { |
| 76 | + return lastFlowControlEvent; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * A flow control event. Record throttled time if {@link LimitExceededBehavior} is {@link |
| 81 | + * LimitExceededBehavior#Block}, or the exception if the behavior is {@link |
| 82 | + * LimitExceededBehavior#ThrowException}. |
| 83 | + */ |
| 84 | + public static class FlowControlEvent implements Comparable<FlowControlEvent> { |
| 85 | + static FlowControlEvent createReserveDelayed(long throttledTimeInMs) { |
| 86 | + return createReserveDelayed(System.currentTimeMillis(), throttledTimeInMs); |
| 87 | + } |
| 88 | + |
| 89 | + static FlowControlEvent createReserveDenied(FlowControlException exception) { |
| 90 | + return createReserveDenied(System.currentTimeMillis(), exception); |
| 91 | + } |
| 92 | + |
| 93 | + /** Package-private for use in testing. */ |
| 94 | + @VisibleForTesting |
| 95 | + static FlowControlEvent createReserveDelayed(long timestampMs, long throttledTimeInMs) { |
| 96 | + Preconditions.checkArgument(timestampMs > 0, "timestamp must be greater than 0"); |
| 97 | + Preconditions.checkArgument(throttledTimeInMs > 0, "throttled time must be greater than 0"); |
| 98 | + return new FlowControlEvent(timestampMs, throttledTimeInMs, null); |
| 99 | + } |
| 100 | + |
| 101 | + /** Package-private for use in testing. */ |
| 102 | + @VisibleForTesting |
| 103 | + static FlowControlEvent createReserveDenied(long timestampMs, FlowControlException exception) { |
| 104 | + Preconditions.checkArgument(timestampMs > 0, "timestamp must be greater than 0"); |
| 105 | + Preconditions.checkNotNull( |
| 106 | + exception, "FlowControlException can't be null when reserve is denied"); |
| 107 | + return new FlowControlEvent(timestampMs, null, exception); |
| 108 | + } |
| 109 | + |
| 110 | + private long timestampMs; |
| 111 | + private Long throttledTimeMs; |
| 112 | + private FlowControlException exception; |
| 113 | + |
| 114 | + private FlowControlEvent( |
| 115 | + long timestampMs, |
| 116 | + @Nullable Long throttledTimeMs, |
| 117 | + @Nullable FlowControlException exception) { |
| 118 | + this.timestampMs = timestampMs; |
| 119 | + this.throttledTimeMs = throttledTimeMs; |
| 120 | + this.exception = exception; |
| 121 | + } |
| 122 | + |
| 123 | + public long getTimestampMs() { |
| 124 | + return timestampMs; |
| 125 | + } |
| 126 | + |
| 127 | + @Nullable |
| 128 | + public FlowControlException getException() { |
| 129 | + return exception; |
| 130 | + } |
| 131 | + |
| 132 | + @Nullable |
| 133 | + public Long getThrottledTime(TimeUnit timeUnit) { |
| 134 | + return throttledTimeMs == null |
| 135 | + ? null |
| 136 | + : timeUnit.convert(throttledTimeMs, TimeUnit.MILLISECONDS); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int compareTo(FlowControlEvent otherEvent) { |
| 141 | + return Long.compare(this.getTimestampMs(), otherEvent.getTimestampMs()); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments