This repository was archived by the owner on Sep 26, 2023. It is now read-only.
Repository navigation
feat: dynamic flow control p3: add FlowControllerEventStats #1332
Merged
igorbernstein2
merged 14 commits into
googleapis:master
from
mutianf:dynamic_flow_control_p3
Apr 5, 2021
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
557faca
feat: dynamic flow control p3: add FlowControllerEventStats
mutianf 76d8c39
add some more tests
mutianf 2985152
add more documentation
mutianf 5fe9b2d
fix the formatting
mutianf fb0c59d
fix the failed test
mutianf 0ab107d
Update comments
mutianf f0357ba
Merge branch 'master' into dynamic_flow_control_p3
igorbernstein2 0e2f63b
updates based on review
mutianf 317489c
missed one review
mutianf e51ff12
Merge branch 'master' into dynamic_flow_control_p3
igorbernstein2 74eae2b
remove the flaky test
mutianf 5e3cb67
deflake test
mutianf ed83ec1
add some comments
mutianf 522be6c
Merge branch 'master' into dynamic_flow_control_p3
igorbernstein2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
gax/src/main/java/com/google/api/gax/batching/FlowControlEventStats.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * Copyright 2021 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.batching; | ||
|
|
||
| import static com.google.api.gax.batching.FlowController.LimitExceededBehavior; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import com.google.api.gax.batching.FlowController.FlowControlException; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Preconditions; | ||
| import java.util.concurrent.TimeUnit; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Record the statistics of flow control events. | ||
| * | ||
| * <p>This class is populated by FlowController, which will record throttling events. Currently it | ||
| * only keeps the last flow control event, but it could be expanded to record more information in | ||
| * the future. The events can be used to dynamically adjust concurrency in the client. For example: | ||
| * | ||
| * <pre>{@code | ||
| * // Increase flow control limits if there was throttling in the past 5 minutes and throttled time | ||
| * // was longer than 1 minute. | ||
| * while(true) { | ||
| * FlowControlEvent event = flowControlEventStats.getLastFlowControlEvent(); | ||
| * if (event != null | ||
| * && event.getTimestampMs() > System.currentMillis() - TimeUnit.MINUTES.toMillis(5) | ||
| * && event.getThrottledTimeInMs() > TimeUnit.MINUTES.toMillis(1)) { | ||
| * flowController.increaseThresholds(elementSteps, byteSteps); | ||
| * } | ||
| * Thread.sleep(TimeUnit.MINUTE.toMillis(10)); | ||
| * } | ||
| * }</pre> | ||
| */ | ||
|
mutianf marked this conversation as resolved.
Resolved
|
||
| @InternalApi("For google-cloud-java client use only") | ||
| public class FlowControlEventStats { | ||
|
|
||
| private volatile FlowControlEvent lastFlowControlEvent; | ||
|
|
||
| // We only need the last event to check if there was throttling in the past X minutes so this | ||
| // doesn't need to be super accurate. | ||
| void recordFlowControlEvent(FlowControlEvent event) { | ||
| if (lastFlowControlEvent == null || event.compareTo(lastFlowControlEvent) > 0) { | ||
| lastFlowControlEvent = event; | ||
|
igorbernstein2 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
igorbernstein2 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| public FlowControlEvent getLastFlowControlEvent() { | ||
| return lastFlowControlEvent; | ||
| } | ||
|
|
||
| /** | ||
| * A flow control event. Record throttled time if {@link LimitExceededBehavior} is {@link | ||
| * LimitExceededBehavior#Block}, or the exception if the behavior is {@link | ||
| * LimitExceededBehavior#ThrowException}. | ||
| */ | ||
| public static class FlowControlEvent implements Comparable<FlowControlEvent> { | ||
| static FlowControlEvent createReserveDelayed(long throttledTimeInMs) { | ||
|
igorbernstein2 marked this conversation as resolved.
Resolved
|
||
| return createReserveDelayed(System.currentTimeMillis(), throttledTimeInMs); | ||
| } | ||
|
|
||
| static FlowControlEvent createReserveDenied(FlowControlException exception) { | ||
| return createReserveDenied(System.currentTimeMillis(), exception); | ||
| } | ||
|
|
||
| /** Package-private for use in testing. */ | ||
| @VisibleForTesting | ||
| static FlowControlEvent createReserveDelayed(long timestampMs, long throttledTimeInMs) { | ||
| Preconditions.checkArgument(timestampMs > 0, "timestamp must be greater than 0"); | ||
| Preconditions.checkArgument(throttledTimeInMs > 0, "throttled time must be greater than 0"); | ||
| return new FlowControlEvent(timestampMs, throttledTimeInMs, null); | ||
| } | ||
|
|
||
| /** Package-private for use in testing. */ | ||
| @VisibleForTesting | ||
| static FlowControlEvent createReserveDenied(long timestampMs, FlowControlException exception) { | ||
| Preconditions.checkArgument(timestampMs > 0, "timestamp must be greater than 0"); | ||
| Preconditions.checkNotNull( | ||
| exception, "FlowControlException can't be null when reserve is denied"); | ||
| return new FlowControlEvent(timestampMs, null, exception); | ||
| } | ||
|
|
||
| private long timestampMs; | ||
| private Long throttledTimeMs; | ||
| private FlowControlException exception; | ||
|
|
||
| private FlowControlEvent( | ||
| long timestampMs, | ||
| @Nullable Long throttledTimeMs, | ||
| @Nullable FlowControlException exception) { | ||
| this.timestampMs = timestampMs; | ||
| this.throttledTimeMs = throttledTimeMs; | ||
| this.exception = exception; | ||
| } | ||
|
|
||
| public long getTimestampMs() { | ||
| return timestampMs; | ||
| } | ||
|
|
||
| @Nullable | ||
| public FlowControlException getException() { | ||
| return exception; | ||
| } | ||
|
|
||
| @Nullable | ||
| public Long getThrottledTime(TimeUnit timeUnit) { | ||
| return throttledTimeMs == null | ||
| ? null | ||
| : timeUnit.convert(throttledTimeMs, TimeUnit.MILLISECONDS); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(FlowControlEvent otherEvent) { | ||
| return Long.compare(this.getTimestampMs(), otherEvent.getTimestampMs()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
gax/src/test/java/com/google/api/gax/batching/FlowControlEventStatsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * Copyright 2021 Google LLC | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.batching; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import com.google.api.gax.batching.FlowControlEventStats.FlowControlEvent; | ||
| import com.google.api.gax.batching.FlowController.MaxOutstandingRequestBytesReachedException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class FlowControlEventStatsTest { | ||
|
|
||
| @Test | ||
| public void testCreateEvent() { | ||
| long timestamp = 12345, throttledTimeMs = 5000; | ||
| FlowControlEvent event = FlowControlEvent.createReserveDelayed(timestamp, throttledTimeMs); | ||
| assertEquals(event.getTimestampMs(), event.getTimestampMs()); | ||
| assertEquals(throttledTimeMs / 1000, event.getThrottledTime(TimeUnit.SECONDS).longValue()); | ||
| assertNull(event.getException()); | ||
|
|
||
| MaxOutstandingRequestBytesReachedException exception = | ||
| new MaxOutstandingRequestBytesReachedException(100); | ||
| event = FlowControlEvent.createReserveDenied(timestamp, exception); | ||
| assertEquals(timestamp, event.getTimestampMs()); | ||
| assertNotNull(event.getException()); | ||
| assertEquals(exception, event.getException()); | ||
| assertNull(event.getThrottledTime(TimeUnit.MILLISECONDS)); | ||
|
|
||
| try { | ||
| event = FlowControlEvent.createReserveDenied(null); | ||
| fail("FlowControlEvent did not throw exception"); | ||
| } catch (NullPointerException e) { | ||
| // expected, ignore | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetLastEvent() throws InterruptedException { | ||
| final FlowControlEventStats stats = new FlowControlEventStats(); | ||
| final long currentTime = System.currentTimeMillis(); | ||
|
|
||
| List<Thread> threads = new ArrayList<>(); | ||
| for (int i = 1; i <= 10; i++) { | ||
| final int timeElapsed = i; | ||
| Thread t = | ||
| new Thread() { | ||
| @Override | ||
| public void run() { | ||
| stats.recordFlowControlEvent( | ||
| FlowControlEvent.createReserveDelayed(currentTime + timeElapsed, timeElapsed)); | ||
| } | ||
| }; | ||
| threads.add(t); | ||
| t.start(); | ||
| } | ||
|
|
||
| for (Thread t : threads) { | ||
| t.join(10); | ||
| } | ||
|
|
||
| assertEquals(currentTime + 10, stats.getLastFlowControlEvent().getTimestampMs()); | ||
| assertEquals( | ||
| 10, stats.getLastFlowControlEvent().getThrottledTime(TimeUnit.MILLISECONDS).longValue()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.