-
Notifications
You must be signed in to change notification settings - Fork 2k
Add workqueue support #624
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
Merged
Merged
Changes from 1 commit
Commits
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
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
30 changes: 0 additions & 30 deletions
30
extended/src/main/java/io/kubernetes/client/extended/workqueue/RateLimiter.java
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
.../src/main/java/io/kubernetes/client/extended/workqueue/ratelimiter/BucketRateLimiter.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,56 @@ | ||
package io.kubernetes.client.extended.workqueue.ratelimiter; | ||
|
||
import io.github.bucket4j.*; | ||
import java.time.Duration; | ||
|
||
/** A light-weight token bucket implementation for RateLimiter. */ | ||
public class BucketRateLimiter<T> implements RateLimiter<T> { | ||
cizezsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private Bucket bucket; | ||
private long tokensInQueue; | ||
private long tokensGeneratedInPeriod; | ||
private Duration period; | ||
|
||
/** | ||
* @param capacity Capacity is the maximum number of tokens can be consumed. | ||
* @param tokensGeneratedInPeriod Tokens generated in period. | ||
* @param period Period that generating specific number of tokens. | ||
*/ | ||
public BucketRateLimiter(long capacity, long tokensGeneratedInPeriod, Duration period) { | ||
Bandwidth bandwidth = | ||
Bandwidth.classic(capacity, Refill.greedy(tokensGeneratedInPeriod, period)); | ||
|
||
this.bucket = Bucket4j.builder().addLimit(bandwidth).build(); | ||
this.tokensInQueue = 0; | ||
this.tokensGeneratedInPeriod = tokensGeneratedInPeriod; | ||
this.period = period; | ||
} | ||
|
||
@Override | ||
public synchronized Duration when(T item) { | ||
cizezsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tokensInQueue++; | ||
|
||
long consumedTokens = bucket.tryConsumeAsMuchAsPossible(tokensInQueue); | ||
if (tokensInQueue - consumedTokens == 0) { | ||
tokensInQueue = 0; | ||
return Duration.ZERO; | ||
} | ||
|
||
tokensInQueue = tokensInQueue - consumedTokens; | ||
|
||
return durationFromTokens(tokensInQueue, tokensGeneratedInPeriod, period); | ||
} | ||
|
||
@Override | ||
public void forget(T item) {} | ||
|
||
@Override | ||
public int numRequeues(T item) { | ||
return 0; | ||
} | ||
|
||
private Duration durationFromTokens( | ||
long tokensNeedToBeConsumed, long tokensGeneratedInPeriod, Duration period) { | ||
return period.dividedBy(tokensGeneratedInPeriod).multipliedBy(tokensNeedToBeConsumed); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ava/io/kubernetes/client/extended/workqueue/ratelimiter/DefaultControllerRateLimiter.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,37 @@ | ||
package io.kubernetes.client.extended.workqueue.ratelimiter; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* DefaultControllerRateLimiter is a default rate limiter for workqueue. It has both overall and | ||
* per-item rate limiting. The overall is a token bucket and the per-item is exponential | ||
*/ | ||
public class DefaultControllerRateLimiter<T> implements RateLimiter<T> { | ||
|
||
private RateLimiter<T> internalRateLimiter; | ||
|
||
public DefaultControllerRateLimiter() { | ||
this.internalRateLimiter = | ||
new MaxOfRateLimiter<>( | ||
Arrays.asList( | ||
new ItemExponentialFailureRateLimiter<>( | ||
Duration.ofMillis(5), Duration.ofSeconds(1000)), | ||
new BucketRateLimiter<>(100, 10, Duration.ofMinutes(1)))); | ||
} | ||
|
||
@Override | ||
public Duration when(T item) { | ||
return internalRateLimiter.when(item); | ||
} | ||
|
||
@Override | ||
public void forget(T item) { | ||
internalRateLimiter.forget(item); | ||
} | ||
|
||
@Override | ||
public int numRequeues(T item) { | ||
return internalRateLimiter.numRequeues(item); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...o/kubernetes/client/extended/workqueue/ratelimiter/ItemExponentialFailureRateLimiter.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,51 @@ | ||
package io.kubernetes.client.extended.workqueue.ratelimiter; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* ItemExponentialFailureRateLimiter does a simple baseDelay*10<sup>num-failures</sup> limit dealing | ||
* with max failures and expiration are up to the caller | ||
*/ | ||
public class ItemExponentialFailureRateLimiter<T> implements RateLimiter<T> { | ||
|
||
private Duration baseDelay; | ||
private Duration maxDelay; | ||
|
||
private Map<T, Integer> failures; | ||
|
||
public ItemExponentialFailureRateLimiter(Duration baseDelay, Duration maxDelay) { | ||
this.baseDelay = baseDelay; | ||
this.maxDelay = maxDelay; | ||
|
||
failures = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public synchronized Duration when(T item) { | ||
cizezsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int exp = failures.getOrDefault(item, 0); | ||
failures.put(item, exp + 1); | ||
|
||
double backOff = baseDelay.toNanos() * Math.pow(2, exp); | ||
if (backOff > Long.MAX_VALUE) { | ||
cizezsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return maxDelay; | ||
} | ||
|
||
if (backOff > maxDelay.toNanos()) { | ||
return maxDelay; | ||
} | ||
|
||
return Duration.ofNanos((long) backOff); | ||
} | ||
|
||
@Override | ||
public synchronized void forget(T item) { | ||
failures.remove(item); | ||
} | ||
|
||
@Override | ||
public synchronized int numRequeues(T item) { | ||
return failures.getOrDefault(item, 0); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ain/java/io/kubernetes/client/extended/workqueue/ratelimiter/ItemFastSlowRateLimiter.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,48 @@ | ||
package io.kubernetes.client.extended.workqueue.ratelimiter; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* ItemFastSlowRateLimiter does a quick retry for a certain number of attempts, then a slow retry | ||
* after that | ||
*/ | ||
public class ItemFastSlowRateLimiter<T> implements RateLimiter<T> { | ||
|
||
private Map<T, Integer> failures; | ||
|
||
private Duration fastDelay; | ||
private Duration slowDelay; | ||
private int maxFastAttempts; | ||
|
||
public ItemFastSlowRateLimiter(Duration fastDelay, Duration slowDelay, int maxFastAttempts) { | ||
this.fastDelay = fastDelay; | ||
this.slowDelay = slowDelay; | ||
this.maxFastAttempts = maxFastAttempts; | ||
|
||
failures = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public synchronized Duration when(T item) { | ||
cizezsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int attempts = failures.getOrDefault(item, 0); | ||
failures.put(item, attempts + 1); | ||
|
||
if (attempts + 1 <= maxFastAttempts) { | ||
return fastDelay; | ||
} | ||
|
||
return slowDelay; | ||
} | ||
|
||
@Override | ||
public synchronized void forget(T item) { | ||
failures.remove(item); | ||
} | ||
|
||
@Override | ||
public synchronized int numRequeues(T item) { | ||
return failures.getOrDefault(item, 0); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...d/src/main/java/io/kubernetes/client/extended/workqueue/ratelimiter/MaxOfRateLimiter.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,55 @@ | ||
package io.kubernetes.client.extended.workqueue.ratelimiter; | ||
|
||
import java.time.Duration; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* MaxOfRateLimiter calls every RateLimiter and returns the worst case response When used with a | ||
* token bucket limiter, the burst could be apparently exceeded in cases where particular items were | ||
* separately delayed a longer time. | ||
*/ | ||
public class MaxOfRateLimiter<T> implements RateLimiter<T> { | ||
private List<RateLimiter<T>> rateLimiters; | ||
|
||
public MaxOfRateLimiter(List<RateLimiter<T>> rateLimiters) { | ||
this.rateLimiters = rateLimiters; | ||
} | ||
|
||
@SafeVarargs | ||
@SuppressWarnings("varargs") | ||
public MaxOfRateLimiter(RateLimiter<T>... rateLimiters) { | ||
this(Arrays.asList(rateLimiters)); | ||
} | ||
|
||
@Override | ||
public Duration when(T item) { | ||
Duration max = Duration.ZERO; | ||
for (RateLimiter<T> r : rateLimiters) { | ||
Duration current = r.when(item); | ||
if (current.compareTo(max) > 0) { | ||
max = current; | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
|
||
@Override | ||
public void forget(T item) { | ||
rateLimiters.forEach(r -> r.forget(item)); | ||
} | ||
|
||
@Override | ||
public int numRequeues(T item) { | ||
int max = 0; | ||
for (RateLimiter<T> r : rateLimiters) { | ||
int current = r.numRequeues(item); | ||
if (current > max) { | ||
max = current; | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
} |
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.