|
| 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 com.google.api.core.InternalApi; |
| 33 | +import com.google.api.gax.batching.FlowController.LimitExceededBehavior; |
| 34 | +import com.google.auto.value.AutoValue; |
| 35 | +import com.google.common.base.Preconditions; |
| 36 | +import javax.annotation.Nullable; |
| 37 | + |
| 38 | +/** Settings for dynamic flow control */ |
| 39 | +@AutoValue |
| 40 | +@InternalApi("For google-cloud-java client use only") |
| 41 | +public abstract class DynamicFlowControlSettings { |
| 42 | + |
| 43 | + /** Number of outstanding elements that {@link FlowController} allows when it's initiated. */ |
| 44 | + @Nullable |
| 45 | + public abstract Long getInitialOutstandingElementCount(); |
| 46 | + |
| 47 | + /** Number of outstanding bytes that {@link FlowController} allows when it's initiated. */ |
| 48 | + @Nullable |
| 49 | + public abstract Long getInitialOutstandingRequestBytes(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Maximum number of outstanding elements {@link FlowController} allows before enforcing flow |
| 53 | + * control. |
| 54 | + */ |
| 55 | + @Nullable |
| 56 | + public abstract Long getMaxOutstandingElementCount(); |
| 57 | + |
| 58 | + /** |
| 59 | + * Maximum number of outstanding bytes {@link FlowController} allows before enforcing flow |
| 60 | + * control. |
| 61 | + */ |
| 62 | + @Nullable |
| 63 | + public abstract Long getMaxOutstandingRequestBytes(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Minimum number of outstanding elements {@link FlowController} allows before enforcing flow |
| 67 | + * control. |
| 68 | + */ |
| 69 | + @Nullable |
| 70 | + public abstract Long getMinOutstandingElementCount(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Minimum number of outstanding bytes {@link FlowController} allows before enforcing flow |
| 74 | + * control. |
| 75 | + */ |
| 76 | + @Nullable |
| 77 | + public abstract Long getMinOutstandingRequestBytes(); |
| 78 | + |
| 79 | + /** @see FlowControlSettings#getLimitExceededBehavior() */ |
| 80 | + public abstract LimitExceededBehavior getLimitExceededBehavior(); |
| 81 | + |
| 82 | + public abstract Builder toBuilder(); |
| 83 | + |
| 84 | + public static Builder newBuilder() { |
| 85 | + return new AutoValue_DynamicFlowControlSettings.Builder() |
| 86 | + .setLimitExceededBehavior(LimitExceededBehavior.Block); |
| 87 | + } |
| 88 | + |
| 89 | + @AutoValue.Builder |
| 90 | + public abstract static class Builder { |
| 91 | + |
| 92 | + public abstract Builder setInitialOutstandingElementCount(Long value); |
| 93 | + |
| 94 | + public abstract Builder setInitialOutstandingRequestBytes(Long value); |
| 95 | + |
| 96 | + public abstract Builder setMaxOutstandingElementCount(Long value); |
| 97 | + |
| 98 | + public abstract Builder setMaxOutstandingRequestBytes(Long value); |
| 99 | + |
| 100 | + public abstract Builder setMinOutstandingElementCount(Long value); |
| 101 | + |
| 102 | + public abstract Builder setMinOutstandingRequestBytes(Long value); |
| 103 | + |
| 104 | + public abstract Builder setLimitExceededBehavior(LimitExceededBehavior value); |
| 105 | + |
| 106 | + abstract DynamicFlowControlSettings autoBuild(); |
| 107 | + |
| 108 | + public DynamicFlowControlSettings build() { |
| 109 | + DynamicFlowControlSettings settings = autoBuild(); |
| 110 | + |
| 111 | + verifyElementCountSettings(settings); |
| 112 | + verifyRequestBytesSettings(settings); |
| 113 | + |
| 114 | + return settings; |
| 115 | + } |
| 116 | + |
| 117 | + private void verifyElementCountSettings(DynamicFlowControlSettings settings) { |
| 118 | + boolean isEnabled = |
| 119 | + settings.getInitialOutstandingElementCount() != null |
| 120 | + || settings.getMinOutstandingElementCount() != null |
| 121 | + || settings.getMaxOutstandingElementCount() != null; |
| 122 | + if (!isEnabled) { |
| 123 | + return; |
| 124 | + } |
| 125 | + Preconditions.checkState( |
| 126 | + settings.getInitialOutstandingElementCount() != null |
| 127 | + && settings.getMinOutstandingElementCount() != null |
| 128 | + && settings.getMaxOutstandingElementCount() != null, |
| 129 | + "Throttling on element count is disabled by default. To enable this setting," |
| 130 | + + " minOutstandingElementCount, initialOutstandingElementCount, and " |
| 131 | + + "maxOutstandingElementCount must all be set."); |
| 132 | + Preconditions.checkState( |
| 133 | + settings.getMinOutstandingElementCount() > 0 |
| 134 | + && settings.getInitialOutstandingElementCount() |
| 135 | + <= settings.getMaxOutstandingElementCount() |
| 136 | + && settings.getInitialOutstandingElementCount() |
| 137 | + >= settings.getMinOutstandingElementCount(), |
| 138 | + "If throttling on element count is set, minOutstandingElementCount must be" |
| 139 | + + " greater than 0, and minOutstandingElementCount <= " |
| 140 | + + "initialOutstandingElementCount <= maxOutstandingElementCount"); |
| 141 | + } |
| 142 | + |
| 143 | + private void verifyRequestBytesSettings(DynamicFlowControlSettings settings) { |
| 144 | + boolean isEnabled = |
| 145 | + settings.getInitialOutstandingRequestBytes() != null |
| 146 | + || settings.getMinOutstandingRequestBytes() != null |
| 147 | + || settings.getMaxOutstandingRequestBytes() != null; |
| 148 | + if (!isEnabled) { |
| 149 | + return; |
| 150 | + } |
| 151 | + Preconditions.checkState( |
| 152 | + settings.getInitialOutstandingRequestBytes() != null |
| 153 | + && settings.getMinOutstandingRequestBytes() != null |
| 154 | + && settings.getMaxOutstandingRequestBytes() != null, |
| 155 | + "Throttling on number of bytes is disabled by default. To enable this " |
| 156 | + + "setting, minOutstandingRequestBytes, initialOutstandingRequestBytes, and " |
| 157 | + + "maxOutstandingRequestBytes must all be set"); |
| 158 | + Preconditions.checkState( |
| 159 | + settings.getMinOutstandingRequestBytes() > 0 |
| 160 | + && settings.getInitialOutstandingRequestBytes() |
| 161 | + <= settings.getMaxOutstandingRequestBytes() |
| 162 | + && settings.getInitialOutstandingRequestBytes() |
| 163 | + >= settings.getMinOutstandingRequestBytes(), |
| 164 | + "If throttling on number of bytes is set, minOutstandingRequestBytes must " |
| 165 | + + "be greater than 0, and minOutstandingRequestBytes <= " |
| 166 | + + "initialOutstandingRequestBytes <= maxOutstandingRequestBytes"); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments