-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
SearchBackpressureState.java
104 lines (88 loc) · 3.15 KB
/
SearchBackpressureState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.search.backpressure;
import org.opensearch.common.util.TokenBucket;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.LongSupplier;
/**
* Tracks the current state of task completions and cancellations.
*
* @opensearch.internal
*/
public class SearchBackpressureState implements CancellationSettingsListener {
private final AtomicReference<TokenBucket> rateLimiter;
private final AtomicReference<TokenBucket> ratioLimiter;
private final LongSupplier timeNanosSupplier;
/**
* The number of successful task completions.
*/
private final AtomicLong completionCount = new AtomicLong();
/**
* The number of task cancellations due to limit breaches.
*/
private final AtomicLong cancellationCount = new AtomicLong();
/**
* The number of times task cancellation limit was reached.
*/
private final AtomicLong limitReachedCount = new AtomicLong();
private volatile double cancellationBurst;
private volatile double cancellationRate;
private volatile double cancellationRatio;
SearchBackpressureState(
LongSupplier timeNanosSupplier,
double cancellationRateNanos,
double cancellationBurst,
double cancellationRatio
) {
rateLimiter = new AtomicReference<>(new TokenBucket(timeNanosSupplier, cancellationRateNanos, cancellationBurst));
ratioLimiter = new AtomicReference<>(new TokenBucket(this::getCompletionCount, cancellationRatio, cancellationBurst));
this.timeNanosSupplier = timeNanosSupplier;
this.cancellationBurst = cancellationBurst;
}
public long getCompletionCount() {
return completionCount.get();
}
long incrementCompletionCount() {
return completionCount.incrementAndGet();
}
public long getCancellationCount() {
return cancellationCount.get();
}
long incrementCancellationCount() {
return cancellationCount.incrementAndGet();
}
public long getLimitReachedCount() {
return limitReachedCount.get();
}
long incrementLimitReachedCount() {
return limitReachedCount.incrementAndGet();
}
public TokenBucket getRateLimiter() {
return rateLimiter.get();
}
public TokenBucket getRatioLimiter() {
return ratioLimiter.get();
}
@Override
public void onRatioChanged(double ratio) {
this.cancellationRatio = ratio;
ratioLimiter.set(new TokenBucket(this::getCompletionCount, cancellationRatio, cancellationBurst));
}
@Override
public void onRateChanged(double rate) {
this.cancellationRate = rate;
rateLimiter.set(new TokenBucket(timeNanosSupplier, cancellationRate, cancellationBurst));
}
@Override
public void onBurstChanged(double burst) {
this.cancellationBurst = burst;
onRateChanged(cancellationRate);
onRatioChanged(cancellationRatio);
}
}