-
Notifications
You must be signed in to change notification settings - Fork 218
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
681 throughput limit #707
Merged
Merged
681 throughput limit #707
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
98 changes: 98 additions & 0 deletions
98
...in/java/pl/allegro/tech/hermes/frontend/publishing/handlers/DynamicThroughputLimiter.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,98 @@ | ||
package pl.allegro.tech.hermes.frontend.publishing.handlers; | ||
|
||
import com.codahale.metrics.Metered; | ||
import pl.allegro.tech.hermes.api.TopicName; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static pl.allegro.tech.hermes.frontend.publishing.handlers.ThroughputLimiter.QuotaInsight.globalQuotaViolation; | ||
import static pl.allegro.tech.hermes.frontend.publishing.handlers.ThroughputLimiter.QuotaInsight.quotaConfirmed; | ||
import static pl.allegro.tech.hermes.frontend.publishing.handlers.ThroughputLimiter.QuotaInsight.quotaViolation; | ||
|
||
public class DynamicThroughputLimiter implements ThroughputLimiter, Runnable { | ||
private final long max; | ||
private final long threshold; | ||
private final long desired; | ||
private final double idleThreshold; | ||
private final Metered globalThroughputMeter; | ||
|
||
private final ScheduledExecutorService executor; | ||
private final int checkInterval; | ||
|
||
private ConcurrentHashMap<TopicName, Throughput> users = new ConcurrentHashMap<>(); | ||
|
||
public DynamicThroughputLimiter(long max, | ||
long threshold, | ||
long desired, | ||
double idleThreshold, | ||
int checkInterval, | ||
Metered globalThroughput, | ||
ScheduledExecutorService executor) { | ||
this.max = max; | ||
this.threshold = threshold; | ||
this.desired = desired; | ||
this.idleThreshold = idleThreshold; | ||
this.globalThroughputMeter = globalThroughput; | ||
this.checkInterval = checkInterval; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public QuotaInsight checkQuota(TopicName topic, Metered rate) { | ||
Throughput throughput = users.computeIfAbsent(topic, name -> new Throughput(rate, max)); | ||
long value = throughput.getRoundedOneMinuteRate(); | ||
if (value > throughput.max) { | ||
return quotaViolation(value, throughput.max); | ||
} else if (globalThroughputMeter.getOneMinuteRate() > max) { | ||
return globalQuotaViolation(); | ||
} else { | ||
return quotaConfirmed(); | ||
} | ||
} | ||
|
||
@Override | ||
public void start() { | ||
executor.scheduleAtFixedRate(this, checkInterval, checkInterval, TimeUnit.SECONDS); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (globalThroughputMeter.getOneMinuteRate() > threshold) { | ||
calibrateLimits(); | ||
} | ||
} | ||
|
||
private void calibrateLimits() { | ||
users.entrySet().removeIf(entry -> entry.getValue().getOneMinuteRate() <= idleThreshold); | ||
int userCount = users.size(); | ||
if (userCount > 0) { | ||
long total = users.reduceValuesToLong(Long.MAX_VALUE, Throughput::getRoundedOneMinuteRate, 0, ((left, right) -> left + right)); | ||
long mean = total / userCount; | ||
long desiredMean = desired / userCount; | ||
users.entrySet() | ||
.stream() | ||
.filter(entry -> entry.getValue().getRoundedOneMinuteRate() >= mean) | ||
.forEach(entry -> entry.getValue().max = desiredMean); | ||
} | ||
} | ||
|
||
private static class Throughput { | ||
Metered current; | ||
volatile long max; | ||
|
||
Throughput(Metered current, long max) { | ||
this.current = current; | ||
this.max = max; | ||
} | ||
|
||
long getRoundedOneMinuteRate() { | ||
return (long) Math.floor(current.getOneMinuteRate()); | ||
} | ||
|
||
double getOneMinuteRate() { | ||
return current.getOneMinuteRate(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...main/java/pl/allegro/tech/hermes/frontend/publishing/handlers/FixedThroughputLimiter.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,22 @@ | ||
package pl.allegro.tech.hermes.frontend.publishing.handlers; | ||
|
||
import com.codahale.metrics.Metered; | ||
import pl.allegro.tech.hermes.api.TopicName; | ||
|
||
import static pl.allegro.tech.hermes.frontend.publishing.handlers.ThroughputLimiter.QuotaInsight.quotaConfirmed; | ||
import static pl.allegro.tech.hermes.frontend.publishing.handlers.ThroughputLimiter.QuotaInsight.quotaViolation; | ||
|
||
public class FixedThroughputLimiter implements ThroughputLimiter { | ||
private long limit; | ||
|
||
public FixedThroughputLimiter(long limit) { | ||
this.limit = limit; | ||
} | ||
|
||
@Override | ||
public QuotaInsight checkQuota(TopicName topic, Metered throughput) { | ||
long rate = (long) Math.floor(throughput.getOneMinuteRate()); | ||
return rate > limit ? quotaViolation(rate, limit) : quotaConfirmed(); | ||
} | ||
|
||
} |
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
52 changes: 52 additions & 0 deletions
52
.../src/main/java/pl/allegro/tech/hermes/frontend/publishing/handlers/ThroughputLimiter.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,52 @@ | ||
package pl.allegro.tech.hermes.frontend.publishing.handlers; | ||
|
||
import com.codahale.metrics.Metered; | ||
import pl.allegro.tech.hermes.api.TopicName; | ||
|
||
import static java.lang.String.format; | ||
|
||
public interface ThroughputLimiter { | ||
QuotaInsight checkQuota(TopicName topic, Metered throughput); | ||
default void start() {} | ||
default void stop() {} | ||
|
||
class QuotaInsight { | ||
private static final QuotaInsight QUOTA_CONFIRMED = new QuotaInsight(); | ||
private static final QuotaInsight GLOBAL_VIOLATION = new QuotaInsight(false, | ||
"Global throughput exceeded. Sorry for the inconvenience."); | ||
private static final String DEFAULT_REASON = "unknown"; | ||
|
||
private boolean hasQuota = true; | ||
private String reason; | ||
|
||
private QuotaInsight() { | ||
} | ||
|
||
private QuotaInsight(boolean pass, String reason) { | ||
this.hasQuota = pass; | ||
this.reason = reason; | ||
} | ||
|
||
public boolean hasQuota() { | ||
return hasQuota; | ||
} | ||
|
||
public String getReason() { | ||
return reason != null ? reason : DEFAULT_REASON; | ||
} | ||
|
||
public static QuotaInsight quotaConfirmed() { | ||
return QUOTA_CONFIRMED; | ||
} | ||
|
||
public static QuotaInsight quotaViolation(long current, long limit) { | ||
return new QuotaInsight(false, | ||
format("Current throughput exceeded limit [current:%s, limit:%s].", current, limit)); | ||
} | ||
|
||
public static QuotaInsight globalQuotaViolation() { | ||
return GLOBAL_VIOLATION; | ||
} | ||
|
||
} | ||
} |
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.
i see a lot of potential for race conditions here - you remove entries here while
check
(which as i see is called for each request) makesputIfAbsent
, then you check for size.. will it not create strange results?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.
not really because removal is done only for idle users, it takes minutes to become idle in the current implementation
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.
furthermore it is rather difficult to publish messages and be idle at the same time