-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stabilize adaptive rate limit by considering current rate #1027
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f45324a
Stabilize adaptive rate limit by considering current rate
ykmr1224 0016506
Fix bulk retry condition
ykmr1224 2809df2
Fix FlintSparkConfSuite
ykmr1224 615a949
Fix FlintSparkConfSuite
ykmr1224 f3da7af
Reformat
ykmr1224 2bf3e74
Use Queue interface instead of List
ykmr1224 b111326
Fix doc for BULK_INITIAL_BACKOFF
ykmr1224 c6199b1
Add retryable http status code
ykmr1224 5105a41
Fix test
ykmr1224 956a34d
Fix RequestRateMeter.addDataPoint
ykmr1224 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
48 changes: 48 additions & 0 deletions
48
flint-core/src/main/scala/org/opensearch/flint/core/storage/RequestRateMeter.java
This file contains 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,48 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.storage; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Track the current request rate based on the past requests within ESTIMATE_RANGE_DURATION_MSEC | ||
* milliseconds period. | ||
*/ | ||
public class RequestRateMeter { | ||
private static final long ESTIMATE_RANGE_DURATION_MSEC = 3000; | ||
|
||
private static class DataPoint { | ||
long timestamp; | ||
long requestCount; | ||
public DataPoint(long timestamp, long requestCount) { | ||
this.timestamp = timestamp; | ||
this.requestCount = requestCount; | ||
} | ||
} | ||
|
||
private Queue<DataPoint> dataPoints = new LinkedList<>(); | ||
private long currentSum = 0; | ||
|
||
public synchronized void addDataPoint(long timestamp, long requestCount) { | ||
dataPoints.add(new DataPoint(timestamp, requestCount)); | ||
currentSum += requestCount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could removeOldDataPoint during add to reduce memory usage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. Memory usage is almost ignorable. |
||
removeOldDataPoints(); | ||
} | ||
|
||
public synchronized long getCurrentEstimatedRate() { | ||
removeOldDataPoints(); | ||
return currentSum * 1000 / ESTIMATE_RANGE_DURATION_MSEC; | ||
} | ||
|
||
private synchronized void removeOldDataPoints() { | ||
long curr = System.currentTimeMillis(); | ||
while (!dataPoints.isEmpty() && dataPoints.peek().timestamp < curr - ESTIMATE_RANGE_DURATION_MSEC) { | ||
currentSum -= dataPoints.remove().requestCount; | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
flint-core/src/test/java/org/opensearch/flint/core/storage/RequestRateMeterTest.java
This file contains 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,66 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.flint.core.storage; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class RequestRateMeterTest { | ||
|
||
private RequestRateMeter requestRateMeter; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
requestRateMeter = new RequestRateMeter(); | ||
} | ||
|
||
@Test | ||
void testAddDataPoint() { | ||
long timestamp = System.currentTimeMillis(); | ||
requestRateMeter.addDataPoint(timestamp, 30); | ||
assertEquals(10, requestRateMeter.getCurrentEstimatedRate()); | ||
} | ||
|
||
@Test | ||
void testAddDataPointRemoveOldDataPoint() { | ||
long timestamp = System.currentTimeMillis(); | ||
requestRateMeter.addDataPoint(timestamp - 4000, 30); | ||
requestRateMeter.addDataPoint(timestamp, 90); | ||
assertEquals(90 / 3, requestRateMeter.getCurrentEstimatedRate()); | ||
} | ||
|
||
@Test | ||
void testRemoveOldDataPoints() { | ||
long currentTime = System.currentTimeMillis(); | ||
requestRateMeter.addDataPoint(currentTime - 4000, 30); | ||
requestRateMeter.addDataPoint(currentTime - 2000, 60); | ||
requestRateMeter.addDataPoint(currentTime, 90); | ||
|
||
assertEquals((60 + 90)/3, requestRateMeter.getCurrentEstimatedRate()); | ||
} | ||
|
||
@Test | ||
void testGetCurrentEstimatedRate() { | ||
long currentTime = System.currentTimeMillis(); | ||
requestRateMeter.addDataPoint(currentTime - 2500, 30); | ||
requestRateMeter.addDataPoint(currentTime - 1500, 60); | ||
requestRateMeter.addDataPoint(currentTime - 500, 90); | ||
|
||
assertEquals((30 + 60 + 90)/3, requestRateMeter.getCurrentEstimatedRate()); | ||
} | ||
|
||
@Test | ||
void testEmptyRateMeter() { | ||
assertEquals(0, requestRateMeter.getCurrentEstimatedRate()); | ||
} | ||
|
||
@Test | ||
void testSingleDataPoint() { | ||
requestRateMeter.addDataPoint(System.currentTimeMillis(), 30); | ||
assertEquals(30 / 3, requestRateMeter.getCurrentEstimatedRate()); | ||
} | ||
} |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason choose 4s as default value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was fixed value and I made it a configuration. The original intention is having higher initial backoff to quickly reduce the rate.