|
| 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 com.rabbitmq.stream.Codec; |
| 18 | +import com.rabbitmq.stream.ConfirmationHandler; |
| 19 | +import com.rabbitmq.stream.Message; |
| 20 | +import com.rabbitmq.stream.ObservationCollector; |
| 21 | +import com.rabbitmq.stream.compression.Compression; |
| 22 | +import com.rabbitmq.stream.compression.CompressionCodec; |
| 23 | +import com.rabbitmq.stream.impl.ProducerUtils.AccumulatedEntity; |
| 24 | +import io.netty.buffer.ByteBufAllocator; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.function.ToLongFunction; |
| 29 | + |
| 30 | +final class DynamicBatchMessageAccumulator implements MessageAccumulator { |
| 31 | + |
| 32 | + private static final Function<Message, String> NULL_FILTER_VALUE_EXTRACTOR = m -> null; |
| 33 | + |
| 34 | + private final DynamicBatch<Object> dynamicBatch; |
| 35 | + private final ObservationCollector<Object> observationCollector; |
| 36 | + private final ToLongFunction<Message> publishSequenceFunction; |
| 37 | + private final String stream; |
| 38 | + private final StreamProducer producer; |
| 39 | + private final Codec codec; |
| 40 | + private final int maxFrameSize; |
| 41 | + private final Clock clock; |
| 42 | + private final Function<Message, String> filterValueExtractor; |
| 43 | + |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + DynamicBatchMessageAccumulator( |
| 46 | + int subEntrySize, |
| 47 | + int batchSize, |
| 48 | + Codec codec, |
| 49 | + int maxFrameSize, |
| 50 | + ToLongFunction<Message> publishSequenceFunction, |
| 51 | + Function<Message, String> filterValueExtractor, |
| 52 | + Clock clock, |
| 53 | + String stream, |
| 54 | + CompressionCodec compressionCodec, |
| 55 | + ByteBufAllocator byteBufAllocator, |
| 56 | + ObservationCollector<?> observationCollector, |
| 57 | + StreamProducer producer) { |
| 58 | + this.producer = producer; |
| 59 | + this.stream = stream; |
| 60 | + this.publishSequenceFunction = publishSequenceFunction; |
| 61 | + this.observationCollector = (ObservationCollector<Object>) observationCollector; |
| 62 | + this.codec = codec; |
| 63 | + this.clock = clock; |
| 64 | + this.maxFrameSize = maxFrameSize; |
| 65 | + this.filterValueExtractor = |
| 66 | + filterValueExtractor == null ? NULL_FILTER_VALUE_EXTRACTOR : filterValueExtractor; |
| 67 | + if (subEntrySize <= 1) { |
| 68 | + this.dynamicBatch = new DynamicBatch<>(this::publish, batchSize); |
| 69 | + } else { |
| 70 | + byte compressionCode = |
| 71 | + compressionCodec == null ? Compression.NONE.code() : compressionCodec.code(); |
| 72 | + this.dynamicBatch = |
| 73 | + new DynamicBatch<>( |
| 74 | + items -> { |
| 75 | + if (this.producer.canSend()) { |
| 76 | + List<Object> subBatches = new ArrayList<>(); |
| 77 | + int count = 0; |
| 78 | + ProducerUtils.Batch batch = |
| 79 | + new ProducerUtils.Batch( |
| 80 | + Client.EncodedMessageBatch.create( |
| 81 | + byteBufAllocator, compressionCode, compressionCodec, subEntrySize), |
| 82 | + new ProducerUtils.CompositeConfirmationCallback( |
| 83 | + new ArrayList<>(subEntrySize))); |
| 84 | + AccumulatedEntity lastMessageInBatch = null; |
| 85 | + for (Object msg : items) { |
| 86 | + AccumulatedEntity message = (AccumulatedEntity) msg; |
| 87 | + this.observationCollector.published( |
| 88 | + message.observationContext(), message.confirmationCallback().message()); |
| 89 | + lastMessageInBatch = message; |
| 90 | + batch.add( |
| 91 | + (Codec.EncodedMessage) message.encodedEntity(), |
| 92 | + message.confirmationCallback()); |
| 93 | + count++; |
| 94 | + if (count == subEntrySize) { |
| 95 | + batch.time = lastMessageInBatch.time(); |
| 96 | + batch.publishingId = lastMessageInBatch.publishingId(); |
| 97 | + batch.encodedMessageBatch.close(); |
| 98 | + subBatches.add(batch); |
| 99 | + lastMessageInBatch = null; |
| 100 | + batch = |
| 101 | + new ProducerUtils.Batch( |
| 102 | + Client.EncodedMessageBatch.create( |
| 103 | + byteBufAllocator, |
| 104 | + compressionCode, |
| 105 | + compressionCodec, |
| 106 | + subEntrySize), |
| 107 | + new ProducerUtils.CompositeConfirmationCallback( |
| 108 | + new ArrayList<>(subEntrySize))); |
| 109 | + count = 0; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (!batch.isEmpty() && count < subEntrySize) { |
| 114 | + batch.time = lastMessageInBatch.time(); |
| 115 | + batch.publishingId = lastMessageInBatch.publishingId(); |
| 116 | + batch.encodedMessageBatch.close(); |
| 117 | + subBatches.add(batch); |
| 118 | + } |
| 119 | + |
| 120 | + return this.publish(subBatches); |
| 121 | + } else { |
| 122 | + return false; |
| 123 | + } |
| 124 | + }, |
| 125 | + batchSize * subEntrySize); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void add(Message message, ConfirmationHandler confirmationHandler) { |
| 131 | + Object observationContext = this.observationCollector.prePublish(this.stream, message); |
| 132 | + Codec.EncodedMessage encodedMessage = this.codec.encode(message); |
| 133 | + Client.checkMessageFitsInFrame(this.maxFrameSize, encodedMessage); |
| 134 | + long publishingId = this.publishSequenceFunction.applyAsLong(message); |
| 135 | + this.dynamicBatch.add( |
| 136 | + new ProducerUtils.SimpleAccumulatedEntity( |
| 137 | + this.clock.time(), |
| 138 | + publishingId, |
| 139 | + this.filterValueExtractor.apply(message), |
| 140 | + this.codec.encode(message), |
| 141 | + new ProducerUtils.SimpleConfirmationCallback(message, confirmationHandler), |
| 142 | + observationContext)); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int size() { |
| 147 | + // TODO compute dynamic batch message accumulator pending message count |
| 148 | + return 0; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void flush(boolean force) {} |
| 153 | + |
| 154 | + private boolean publish(List<Object> entities) { |
| 155 | + if (this.producer.canSend()) { |
| 156 | + this.producer.publishInternal(entities); |
| 157 | + return true; |
| 158 | + } else { |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments