-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageRateLimiterBehavior.java
238 lines (205 loc) · 8.77 KB
/
MessageRateLimiterBehavior.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright (c) 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.amqp;
import java.time.Duration;
import org.eclipse.ditto.connectivity.service.config.Amqp10Config;
import org.eclipse.ditto.connectivity.service.config.ConnectionThrottlingConfig;
import akka.actor.AbstractActor;
import akka.actor.Actor;
import akka.actor.Timers;
import akka.event.LoggingAdapter;
import akka.japi.pf.ReceiveBuilder;
/**
* Mixin for limitation of unacknowledged messages or messages
* Subclasses must extend {@code AbstractActorWithTimers} (self-type requirement).
* Necessary for AMQP 1.0 due to JMS client's not exposing protocol-level flow control to the application level
* (throttling via prefetch-policy is not possible).
*
* @param <S> type of message IDs.
*/
interface MessageRateLimiterBehavior<S> extends Actor, Timers {
/**
* Require a logger for this actor.
*
* @return this actor's logger.
*/
LoggingAdapter log();
/**
* Start message consumer. NOT required to be thread-safe. May be asynchronous.
*/
void startMessageConsumerDueToRateLimit();
/**
* Stop message consumer. NOT required to be thread-safe. May be asynchronous.
*
* @param reason why the message consumer should be stopped.
*/
void stopMessageConsumerDueToRateLimit(final String reason);
/**
* MUST be thread-safe, even though the result is not thread-safe.
*
* @return the rate limiter state.
*/
MessageRateLimiter<S> getMessageRateLimiter();
/**
* Create the rate-limiter state to be included in the actor state.
*
* @param config the configuration.
* @return the rate limiter as a part of the actor state.
*/
default MessageRateLimiter<S> initMessageRateLimiter(final Amqp10Config config) {
final ConnectionThrottlingConfig throttlingConfig = config.getConsumerConfig().getThrottlingConfig();
if (throttlingConfig.isEnabled()) {
// schedule periodic throughput check
timers().startTimerAtFixedRate(Control.CHECK_RATE_LIMIT, Control.CHECK_RATE_LIMIT,
throttlingConfig.getInterval());
}
return MessageRateLimiter.of(config);
}
/**
* Create the rate-limiting behavior to be included in the actor behavior.
*
* @return the rate-limiting behavior.
*/
default AbstractActor.Receive getRateLimiterBehavior() {
if (getMessageRateLimiter().isEnabled()) {
return ReceiveBuilder.create()
.match(AckStatus.class, this::checkAckStatus)
.matchEquals(Control.CHECK_RATE_LIMIT, this::checkRateLimitAsScheduled)
.match(Forget.class, this::forgetPendingRedelivery)
.build();
} else {
return ReceiveBuilder.create().build();
}
}
/**
* Record an incoming message for rate limiting.
* NOT thread-safe! Only call in the actor's thread!
*
* @param messageId the ID of the incoming message.
*/
default void recordIncomingForRateLimit(final S messageId) {
final MessageRateLimiter<S> rateLimiter = getMessageRateLimiter();
if (rateLimiter.isEnabled()) {
// message counted as in-flight and for current period
rateLimiter.incoming(messageId);
// message no longer counted as pending redelivery (this is THE redelivery)
rateLimiter.forgetPendingRedelivery(messageId);
timers().cancel(messageId);
// only check for total throughput here; check in-flight at regular intervals
checkRateLimitForConsumedThisPeriod(rateLimiter);
}
}
/**
* Record the acknowledgement of an incoming message for rate limiting.
*
* @param messageId ID of the incoming message.
* @param isSuccess whether success is being acknowledged.
* @param redeliver whether redelivery is requested in case of failure.
*/
default void recordAckForRateLimit(final S messageId, final boolean isSuccess, final boolean redeliver) {
if (getMessageRateLimiter().isEnabled()) {
self().tell(new AckStatus<>(messageId, isSuccess, redeliver), self());
}
}
private void checkRateLimitForConsumedThisPeriod(final MessageRateLimiter<S> rateLimiter) {
if (rateLimiter.isMaxPerPeriodExceeded() && rateLimiter.isConsumerOpen()) {
stopConsumerDueToRateLimit(rateLimiter, true, false);
}
}
private void checkRateLimitAsScheduled(final Control trigger) {
final MessageRateLimiter<S> rateLimiter = getMessageRateLimiter();
// allocate more credit for consumption
rateLimiter.reduceConsumedInPeriod();
final boolean maxPerPeriodExceeded = rateLimiter.isMaxPerPeriodExceeded();
final boolean maxInFlightExceeded = rateLimiter.isMaxInFlightExceeded();
final boolean isConsumerOpen = rateLimiter.isConsumerOpen();
final boolean shouldConsumerBeOpen = !maxPerPeriodExceeded && !maxInFlightExceeded;
if (isConsumerOpen != shouldConsumerBeOpen) {
if (shouldConsumerBeOpen) {
startConsumerDueToRateLimit(rateLimiter);
} else {
stopConsumerDueToRateLimit(rateLimiter, maxPerPeriodExceeded, maxInFlightExceeded);
}
}
}
private void startConsumerDueToRateLimit(final MessageRateLimiter<S> rateLimiter) {
logRateLimiter(rateLimiter, "Starting");
startMessageConsumerDueToRateLimit();
rateLimiter.setIsConsumerOpen(true);
}
private void stopConsumerDueToRateLimit(final MessageRateLimiter<S> rateLimiter,
final boolean maxPerPeriodExceeded,
final boolean maxInFlightExceeded) {
logRateLimiter(rateLimiter, "Stopping");
final String reason = getMessageConsumerStoppedReason(maxPerPeriodExceeded, maxInFlightExceeded);
stopMessageConsumerDueToRateLimit(reason);
rateLimiter.setIsConsumerOpen(false);
}
private void logRateLimiter(final MessageRateLimiter<S> rateLimiter, final String action) {
log().info("RATELIMITER {} in-flight={} redelivering={} max-in-flight={}",
action + " message consumer. period=" +
rateLimiter.getConsumedInPeriod() + "/" + rateLimiter.getMaxPerPeriod(),
rateLimiter.getInFlight(),
rateLimiter.getToBeRedelivered(),
rateLimiter.getMaxInFlight()
);
}
private void checkAckStatus(final AckStatus<S> ack) {
final MessageRateLimiter<S> rateLimiter = getMessageRateLimiter();
final boolean isRedeliveryRequested = !ack.isSuccess && ack.shouldRedeliver;
if (isRedeliveryRequested) {
rateLimiter.redeliveryRequested(ack.messageId);
scheduleForgettingPendingRedelivery(ack.messageId, rateLimiter.getRedeliveryExpectationTimeout());
} else {
rateLimiter.terminallyAcknowledged(ack.messageId);
timers().cancel(ack.messageId);
}
}
private void scheduleForgettingPendingRedelivery(final S messageId, final Duration redeliveryExpectationTimeout) {
timers().startSingleTimer(messageId, new Forget<>(messageId), redeliveryExpectationTimeout);
}
private void forgetPendingRedelivery(final Forget<S> forget) {
getMessageRateLimiter().forgetPendingRedelivery(forget.messageId);
}
private String getMessageConsumerStoppedReason(final boolean maxPerPeriodExceeded,
final boolean maxInFlightExceeded) {
if (maxPerPeriodExceeded) {
if (maxInFlightExceeded) {
return "excessive messaging and too many unacknowledged commands";
} else {
return "excessive messaging";
}
} else {
return "too many unacknowledged commands";
}
}
final class Forget<S> {
private final S messageId;
public Forget(final S messageId) {
this.messageId = messageId;
}
}
final class AckStatus<S> {
private final S messageId;
private final boolean isSuccess;
private final boolean shouldRedeliver;
private AckStatus(final S messageId, final boolean isSuccess, final boolean shouldRedeliver) {
this.messageId = messageId;
this.isSuccess = isSuccess;
this.shouldRedeliver = shouldRedeliver;
}
}
enum Control {
CHECK_RATE_LIMIT
}
}