|
| 1 | +// Copyright (c) 2024 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// |
| 4 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 5 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 6 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | +package com.rabbitmq.stream.impl; |
| 16 | + |
| 17 | +import static java.lang.Math.max; |
| 18 | +import static java.lang.Math.min; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.BlockingQueue; |
| 23 | +import java.util.concurrent.LinkedBlockingQueue; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.function.BiPredicate; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +final class DynamicBatch<T> implements AutoCloseable { |
| 30 | + |
| 31 | + private static final Logger LOGGER = LoggerFactory.getLogger(DynamicBatch.class); |
| 32 | + private static final int MIN_BATCH_SIZE = 32; |
| 33 | + private static final int MAX_BATCH_SIZE = 8192; |
| 34 | + |
| 35 | + private final BlockingQueue<T> requests = new LinkedBlockingQueue<>(); |
| 36 | + private final BiPredicate<List<T>, Boolean> consumer; |
| 37 | + private final int configuredBatchSize; |
| 38 | + private final Thread thread; |
| 39 | + |
| 40 | + DynamicBatch(BiPredicate<List<T>, Boolean> consumer, int batchSize) { |
| 41 | + this.consumer = consumer; |
| 42 | + this.configuredBatchSize = min(max(batchSize, MIN_BATCH_SIZE), MAX_BATCH_SIZE); |
| 43 | + this.thread = ConcurrencyUtils.defaultThreadFactory().newThread(this::loop); |
| 44 | + this.thread.start(); |
| 45 | + } |
| 46 | + |
| 47 | + void add(T item) { |
| 48 | + try { |
| 49 | + requests.put(item); |
| 50 | + } catch (InterruptedException e) { |
| 51 | + throw new RuntimeException(e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void loop() { |
| 56 | + State<T> state = new State<>(); |
| 57 | + state.batchSize = this.configuredBatchSize; |
| 58 | + state.items = new ArrayList<>(state.batchSize); |
| 59 | + state.retry = false; |
| 60 | + Thread currentThread = Thread.currentThread(); |
| 61 | + T item; |
| 62 | + while (!currentThread.isInterrupted()) { |
| 63 | + try { |
| 64 | + item = this.requests.poll(100, TimeUnit.MILLISECONDS); |
| 65 | + } catch (InterruptedException e) { |
| 66 | + currentThread.interrupt(); |
| 67 | + return; |
| 68 | + } |
| 69 | + if (item != null) { |
| 70 | + state.items.add(item); |
| 71 | + if (state.items.size() >= state.batchSize) { |
| 72 | + this.maybeCompleteBatch(state, true); |
| 73 | + } else { |
| 74 | + item = this.requests.poll(); |
| 75 | + if (item == null) { |
| 76 | + this.maybeCompleteBatch(state, false); |
| 77 | + } else { |
| 78 | + state.items.add(item); |
| 79 | + if (state.items.size() >= state.batchSize) { |
| 80 | + this.maybeCompleteBatch(state, true); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } else { |
| 85 | + this.maybeCompleteBatch(state, false); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static final class State<T> { |
| 91 | + |
| 92 | + int batchSize; |
| 93 | + List<T> items; |
| 94 | + boolean retry; |
| 95 | + } |
| 96 | + |
| 97 | + private void maybeCompleteBatch(State<T> state, boolean increaseIfCompleted) { |
| 98 | + try { |
| 99 | + boolean completed = this.consumer.test(state.items, state.retry); |
| 100 | + if (completed) { |
| 101 | + if (increaseIfCompleted) { |
| 102 | + state.batchSize = min(state.batchSize * 2, MAX_BATCH_SIZE); |
| 103 | + } else { |
| 104 | + state.batchSize = max(state.batchSize / 2, MIN_BATCH_SIZE); |
| 105 | + } |
| 106 | + state.items = new ArrayList<>(state.batchSize); |
| 107 | + state.retry = false; |
| 108 | + } else { |
| 109 | + state.retry = true; |
| 110 | + } |
| 111 | + } catch (Exception e) { |
| 112 | + LOGGER.warn("Error during dynamic batch completion: {}", e.getMessage()); |
| 113 | + state.retry = true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void close() { |
| 119 | + this.thread.interrupt(); |
| 120 | + } |
| 121 | +} |
0 commit comments