-
Notifications
You must be signed in to change notification settings - Fork 277
/
DefaultAsyncBucketProxy.java
418 lines (368 loc) · 17.7 KB
/
DefaultAsyncBucketProxy.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*-
* ========================LICENSE_START=================================
* Bucket4j
* %%
* Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package io.github.bucket4j.distributed.proxy;
import io.github.bucket4j.*;
import io.github.bucket4j.distributed.AsyncBucketProxy;
import io.github.bucket4j.distributed.AsyncOptimizationController;
import io.github.bucket4j.distributed.AsyncVerboseBucket;
import io.github.bucket4j.distributed.remote.CommandResult;
import io.github.bucket4j.distributed.remote.RemoteCommand;
import io.github.bucket4j.distributed.remote.RemoteVerboseResult;
import io.github.bucket4j.distributed.remote.commands.*;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import static io.github.bucket4j.LimitChecker.*;
public class DefaultAsyncBucketProxy implements AsyncBucketProxy, AsyncOptimizationController, SchedulingBucket {
private final AsyncCommandExecutor commandExecutor;
private final RecoveryStrategy recoveryStrategy;
private final Supplier<CompletableFuture<BucketConfiguration>> configurationSupplier;
private final BucketListener listener;
private final AtomicBoolean wasInitialized;
@Override
public AsyncVerboseBucket asVerbose() {
return asyncVerboseView;
}
@Override
public AsyncBucketProxy toListenable(BucketListener listener) {
return new DefaultAsyncBucketProxy(commandExecutor, recoveryStrategy, configurationSupplier, wasInitialized, listener);
}
@Override
public SchedulingBucket asScheduler() {
return this;
}
public DefaultAsyncBucketProxy(AsyncCommandExecutor commandExecutor, RecoveryStrategy recoveryStrategy, Supplier<CompletableFuture<BucketConfiguration>> configurationSupplier) {
this(commandExecutor, recoveryStrategy, configurationSupplier, new AtomicBoolean(false), BucketListener.NOPE);
}
private DefaultAsyncBucketProxy(AsyncCommandExecutor commandExecutor, RecoveryStrategy recoveryStrategy, Supplier<CompletableFuture<BucketConfiguration>> configurationSupplier, AtomicBoolean wasInitialized, BucketListener listener) {
this.commandExecutor = Objects.requireNonNull(commandExecutor);
this.recoveryStrategy = recoveryStrategy;
this.configurationSupplier = configurationSupplier;
this.wasInitialized = wasInitialized;
if (listener == null) {
throw BucketExceptions.nullListener();
}
this.listener = listener;
}
private final AsyncVerboseBucket asyncVerboseView = new AsyncVerboseBucket() {
@Override
public CompletableFuture<VerboseResult<Boolean>> tryConsume(long tokensToConsume) {
checkTokensToConsume(tokensToConsume);
VerboseCommand<Boolean> command = new VerboseCommand<>(new TryConsumeCommand(tokensToConsume));
return execute(command).thenApply(consumed -> {
if (consumed.getValue()) {
listener.onConsumed(tokensToConsume);
} else {
listener.onRejected(tokensToConsume);
}
return consumed.asLocal();
});
}
@Override
public CompletableFuture<VerboseResult<Long>> consumeIgnoringRateLimits(long tokensToConsume) {
checkTokensToConsume(tokensToConsume);
VerboseCommand<Long> command = new VerboseCommand<>(new ConsumeIgnoringRateLimitsCommand(tokensToConsume));
return execute(command).thenApply(penaltyNanos -> {
if (penaltyNanos.getValue() == INFINITY_DURATION) {
throw BucketExceptions.reservationOverflow();
}
listener.onConsumed(tokensToConsume);
return penaltyNanos.asLocal();
});
}
@Override
public CompletableFuture<VerboseResult<ConsumptionProbe>> tryConsumeAndReturnRemaining(long tokensToConsume) {
checkTokensToConsume(tokensToConsume);
VerboseCommand<ConsumptionProbe> command = new VerboseCommand<>(new TryConsumeAndReturnRemainingTokensCommand(tokensToConsume));
return execute(command).thenApply(probe -> {
if (probe.getValue().isConsumed()) {
listener.onConsumed(tokensToConsume);
} else {
listener.onRejected(tokensToConsume);
}
return probe.asLocal();
});
}
@Override
public CompletableFuture<VerboseResult<EstimationProbe>> estimateAbilityToConsume(long numTokens) {
checkTokensToConsume(numTokens);
return execute(new VerboseCommand<>(new EstimateAbilityToConsumeCommand(numTokens)))
.thenApply(RemoteVerboseResult::asLocal);
}
@Override
public CompletableFuture<VerboseResult<Long>> tryConsumeAsMuchAsPossible() {
VerboseCommand<Long> command = new VerboseCommand<>(new ConsumeAsMuchAsPossibleCommand (UNLIMITED_AMOUNT));
return execute(command).thenApply(consumedTokens -> {
long actuallyConsumedTokens = consumedTokens.getValue();
if (actuallyConsumedTokens > 0) {
listener.onConsumed(actuallyConsumedTokens);
}
return consumedTokens.asLocal();
});
}
@Override
public CompletableFuture<VerboseResult<Long>> tryConsumeAsMuchAsPossible(long limit) {
checkTokensToConsume(limit);
VerboseCommand<Long> verboseCommand = new VerboseCommand<>(new ConsumeAsMuchAsPossibleCommand(limit));
return execute(verboseCommand).thenApply(consumedTokens -> {
long actuallyConsumedTokens = consumedTokens.getValue();
if (actuallyConsumedTokens > 0) {
listener.onConsumed(actuallyConsumedTokens);
}
return consumedTokens.asLocal();
});
}
@Override
public CompletableFuture<VerboseResult<Nothing>> addTokens(long tokensToAdd) {
checkTokensToAdd(tokensToAdd);
VerboseCommand<Nothing> verboseCommand = new VerboseCommand<>(new AddTokensCommand(tokensToAdd));
return execute(verboseCommand).thenApply(RemoteVerboseResult::asLocal);
}
@Override
public CompletableFuture<VerboseResult<Nothing>> forceAddTokens(long tokensToAdd) {
checkTokensToAdd(tokensToAdd);
VerboseCommand<Nothing> verboseCommand = new VerboseCommand<>(new ForceAddTokensCommand(tokensToAdd));
return execute(verboseCommand).thenApply(RemoteVerboseResult::asLocal);
}
@Override
public CompletableFuture<VerboseResult<Nothing>> reset() {
VerboseCommand<Nothing> verboseCommand = new VerboseCommand<>(new ResetCommand());
return execute(verboseCommand).thenApply(RemoteVerboseResult::asLocal);
}
@Override
public CompletableFuture<VerboseResult<Nothing>> replaceConfiguration(BucketConfiguration newConfiguration, TokensInheritanceStrategy tokensInheritanceStrategy) {
checkConfiguration(newConfiguration);
checkMigrationMode(tokensInheritanceStrategy);
VerboseCommand<Nothing> command = new VerboseCommand<>(new ReplaceConfigurationCommand(newConfiguration, tokensInheritanceStrategy));
return execute(command).thenApply(RemoteVerboseResult::asLocal);
}
@Override
public CompletableFuture<VerboseResult<Long>> getAvailableTokens() {
VerboseCommand<Long> command = new VerboseCommand<>(new GetAvailableTokensCommand());
return execute(command).thenApply(RemoteVerboseResult::asLocal);
}
};
@Override
public CompletableFuture<Long> consumeIgnoringRateLimits(long tokensToConsume) {
checkTokensToConsume(tokensToConsume);
return execute(new ConsumeIgnoringRateLimitsCommand(tokensToConsume)).thenApply(penaltyNanos -> {
if (penaltyNanos == INFINITY_DURATION) {
throw BucketExceptions.reservationOverflow();
}
listener.onConsumed(tokensToConsume);
return penaltyNanos;
});
}
@Override
public CompletableFuture<Boolean> tryConsume(long tokensToConsume) {
checkTokensToConsume(tokensToConsume);
return execute(new TryConsumeCommand(tokensToConsume)).thenApply(consumed -> {
if (consumed) {
listener.onConsumed(tokensToConsume);
} else {
listener.onRejected(tokensToConsume);
}
return consumed;
});
}
@Override
public CompletableFuture<ConsumptionProbe> tryConsumeAndReturnRemaining(long tokensToConsume) {
checkTokensToConsume(tokensToConsume);
return execute(new TryConsumeAndReturnRemainingTokensCommand(tokensToConsume)).thenApply(probe -> {
if (probe.isConsumed()) {
listener.onConsumed(tokensToConsume);
} else {
listener.onRejected(tokensToConsume);
}
return probe;
});
}
@Override
public CompletableFuture<EstimationProbe> estimateAbilityToConsume(long numTokens) {
checkTokensToConsume(numTokens);
return execute(new EstimateAbilityToConsumeCommand(numTokens));
}
@Override
public CompletableFuture<Long> tryConsumeAsMuchAsPossible() {
return execute(new ConsumeAsMuchAsPossibleCommand(UNLIMITED_AMOUNT)).thenApply(consumedTokens -> {
if (consumedTokens > 0) {
listener.onConsumed(consumedTokens);
}
return consumedTokens;
});
}
@Override
public CompletableFuture<Long> tryConsumeAsMuchAsPossible(long limit) {
checkTokensToConsume(limit);
return execute(new ConsumeAsMuchAsPossibleCommand(limit)).thenApply(consumedTokens -> {
if (consumedTokens > 0) {
listener.onConsumed(consumedTokens);
}
return consumedTokens;
});
}
@Override
public CompletableFuture<Boolean> tryConsume(long tokensToConsume, long maxWaitTimeNanos, ScheduledExecutorService scheduler) {
checkMaxWaitTime(maxWaitTimeNanos);
checkTokensToConsume(tokensToConsume);
checkScheduler(scheduler);
CompletableFuture<Boolean> resultFuture = new CompletableFuture<>();
ReserveAndCalculateTimeToSleepCommand consumeCommand = new ReserveAndCalculateTimeToSleepCommand(tokensToConsume, maxWaitTimeNanos);
CompletableFuture<Long> reservationFuture = execute(consumeCommand);
reservationFuture.whenComplete((nanosToSleep, exception) -> {
if (exception != null) {
resultFuture.completeExceptionally(exception);
return;
}
if (nanosToSleep == INFINITY_DURATION) {
resultFuture.complete(false);
listener.onRejected(tokensToConsume);
return;
}
if (nanosToSleep == 0L) {
resultFuture.complete(true);
listener.onConsumed(tokensToConsume);
return;
}
try {
listener.onConsumed(tokensToConsume);
listener.onDelayed(nanosToSleep);
Runnable delayedCompletion = () -> resultFuture.complete(true);
scheduler.schedule(delayedCompletion, nanosToSleep, TimeUnit.NANOSECONDS);
} catch (Throwable t) {
resultFuture.completeExceptionally(t);
}
});
return resultFuture;
}
@Override
public CompletableFuture<Void> consume(long tokensToConsume, ScheduledExecutorService scheduler) {
checkTokensToConsume(tokensToConsume);
checkScheduler(scheduler);
CompletableFuture<Void> resultFuture = new CompletableFuture<>();
ReserveAndCalculateTimeToSleepCommand consumeCommand = new ReserveAndCalculateTimeToSleepCommand(tokensToConsume, INFINITY_DURATION);
CompletableFuture<Long> reservationFuture = execute(consumeCommand);
reservationFuture.whenComplete((nanosToSleep, exception) -> {
if (exception != null) {
resultFuture.completeExceptionally(exception);
return;
}
if (nanosToSleep == INFINITY_DURATION) {
resultFuture.completeExceptionally(BucketExceptions.reservationOverflow());
return;
}
if (nanosToSleep == 0L) {
resultFuture.complete(null);
listener.onConsumed(tokensToConsume);
return;
}
try {
listener.onConsumed(tokensToConsume);
listener.onDelayed(nanosToSleep);
Runnable delayedCompletion = () -> resultFuture.complete(null);
scheduler.schedule(delayedCompletion, nanosToSleep, TimeUnit.NANOSECONDS);
} catch (Throwable t) {
resultFuture.completeExceptionally(t);
}
});
return resultFuture;
}
@Override
public CompletableFuture<Void> replaceConfiguration(BucketConfiguration newConfiguration, TokensInheritanceStrategy tokensInheritanceStrategy) {
checkConfiguration(newConfiguration);
checkMigrationMode(tokensInheritanceStrategy);
ReplaceConfigurationCommand replaceConfigCommand = new ReplaceConfigurationCommand(newConfiguration, tokensInheritanceStrategy);
CompletableFuture<Nothing> result = execute(replaceConfigCommand);
return result.thenApply(nothing -> null);
}
@Override
public CompletableFuture<Void> addTokens(long tokensToAdd) {
checkTokensToAdd(tokensToAdd);
CompletableFuture<Nothing> future = execute(new AddTokensCommand(tokensToAdd));
return future.thenApply(nothing -> null);
}
@Override
public CompletableFuture<Void> forceAddTokens(long tokensToAdd) {
checkTokensToAdd(tokensToAdd);
CompletableFuture<Nothing> future = execute(new ForceAddTokensCommand(tokensToAdd));
return future.thenApply(nothing -> null);
}
@Override
public CompletableFuture<Void> reset() {
CompletableFuture<Nothing> future = execute(new ResetCommand());
return future.thenApply(nothing -> null);
}
@Override
public CompletableFuture<Long> getAvailableTokens() {
return execute(new GetAvailableTokensCommand());
}
@Override
public AsyncOptimizationController getOptimizationController() {
return this;
}
@Override
public CompletableFuture<Void> syncByCondition(long unsynchronizedTokens, Duration timeSinceLastSync) {
return execute(new SyncCommand(unsynchronizedTokens, timeSinceLastSync.toNanos())).thenApply(nothing -> null);
}
private <T> CompletableFuture<T> execute(RemoteCommand<T> command) {
boolean wasInitializedBeforeExecution = wasInitialized.get();
CompletableFuture<CommandResult<T>> futureResult = commandExecutor.executeAsync(command);
return futureResult.thenCompose(cmdResult -> {
if (!cmdResult.isBucketNotFound()) {
T resultDate = cmdResult.getData();
return CompletableFuture.completedFuture(resultDate);
}
if (recoveryStrategy == RecoveryStrategy.THROW_BUCKET_NOT_FOUND_EXCEPTION && wasInitializedBeforeExecution) {
CompletableFuture<T> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(new BucketNotFoundException());
return failedFuture;
}
CompletableFuture<BucketConfiguration> configurationFuture;
try {
configurationFuture = configurationSupplier.get();
} catch (Throwable t) {
CompletableFuture<T> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(t);
return failedFuture;
}
if (configurationFuture == null) {
CompletableFuture<T> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(BucketExceptions.nullConfigurationFuture());
return failedFuture;
}
return configurationFuture.thenCompose(configuration -> {
if (configuration == null) {
CompletableFuture<T> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(BucketExceptions.nullConfiguration());
return failedFuture;
}
CreateInitialStateAndExecuteCommand<T> initAndExecute = new CreateInitialStateAndExecuteCommand<>(configuration, command);
return commandExecutor.executeAsync(initAndExecute).thenApply(initAndExecuteCmdResult -> {
wasInitialized.set(true);
return initAndExecuteCmdResult.getData();
});
});
});
}
}