|
| 1 | +// Copyright (c) 2023 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 5 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 6 | +// please see LICENSE-APACHE2. |
| 7 | +// |
| 8 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// either express or implied. See the LICENSE file for specific language governing |
| 10 | +// rights and limitations of this software. |
| 11 | +// |
| 12 | +// If you have any questions regarding licensing, please contact us at |
| 13 | + |
| 14 | +package com.rabbitmq.stream; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.AtomicLong; |
| 17 | + |
| 18 | +/** |
| 19 | + * Contract to determine when a subscription provides credits to get more messages. |
| 20 | + * |
| 21 | + * <p>The broker delivers "chunks" of messages to consumers. A chunk can contain from 1 to several |
| 22 | + * thousands of messages. The broker send chunks as long as the subscription has <em>credits</em>. A |
| 23 | + * client connection can provide credits for a given subscription and the broker will send the |
| 24 | + * corresponding number of chunks (1 credit = 1 chunk). |
| 25 | + * |
| 26 | + * <p>This credit mechanism avoids overwhelming a consumer with messages. A consumer does not want |
| 27 | + * to provide a credit only when it is done with messages of a chunk, because it will be idle |
| 28 | + * between its credit request and the arrival of the next chunk. The idea is to keep consumers busy |
| 29 | + * as much as possible, without accumulating an in-memory backlog on the client side. There is no |
| 30 | + * ideal solution, it depends on the use cases and several parameters (processing time, network, |
| 31 | + * etc). |
| 32 | + * |
| 33 | + * <p>This is an experimental API, subject to change. |
| 34 | + * |
| 35 | + * @since 0.12.0 |
| 36 | + * @see MessageHandler.Context#processed() |
| 37 | + * @see ConsumerBuilder#flow() |
| 38 | + */ |
| 39 | +public interface ConsumerFlowStrategy { |
| 40 | + |
| 41 | + /** |
| 42 | + * The initial number of credits for a subscription. |
| 43 | + * |
| 44 | + * <p>It must be greater than 0. Values are usually between 1 and 10. |
| 45 | + * |
| 46 | + * @return initial number of credits |
| 47 | + */ |
| 48 | + int initialCredits(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Return the behavior for {@link MessageHandler.Context#processed()} calls. |
| 52 | + * |
| 53 | + * <p>This method is called for each chunk of messages. Implementations return a callback that |
| 54 | + * will be called when applications consider a message dealt with and call {@link |
| 55 | + * MessageHandler.Context#processed()}. The callback can count messages and provide credits |
| 56 | + * accordingly. |
| 57 | + * |
| 58 | + * @param context chunk context |
| 59 | + * @return the message processed callback |
| 60 | + */ |
| 61 | + MessageProcessedCallback start(Context context); |
| 62 | + |
| 63 | + /** Chunk context. */ |
| 64 | + interface Context { |
| 65 | + |
| 66 | + /** |
| 67 | + * Provide credits for the subscription. |
| 68 | + * |
| 69 | + * <p>{@link ConsumerFlowStrategy} implementation should always provide 1 credit a given chunk. |
| 70 | + * |
| 71 | + * @param credits the number of credits provided, usually 1 |
| 72 | + */ |
| 73 | + void credits(int credits); |
| 74 | + |
| 75 | + /** |
| 76 | + * The number of messages in the chunk. |
| 77 | + * |
| 78 | + * @return number of messages in the chunk |
| 79 | + */ |
| 80 | + long messageCount(); |
| 81 | + } |
| 82 | + |
| 83 | + /** Behavior for {@link MessageHandler.Context#processed()} calls. */ |
| 84 | + @FunctionalInterface |
| 85 | + interface MessageProcessedCallback { |
| 86 | + |
| 87 | + /** |
| 88 | + * Method called when {@link MessageHandler.Context#processed()} is called. |
| 89 | + * |
| 90 | + * <p>There is one instance of this class for a given chunk and it is called for the <code> |
| 91 | + * processed()</code> calls of the message of this chunk. |
| 92 | + * |
| 93 | + * <p>Implementations can count messages and call {@link Context#credits(int)} when appropriate. |
| 94 | + * |
| 95 | + * <p>Note calls to {@link MessageHandler.Context#processed()} are not idempotent: an |
| 96 | + * application can call the method several times for the same message and implementations must |
| 97 | + * deal with these multiple calls if they impact their logic. |
| 98 | + * |
| 99 | + * @param messageContext context of the message |
| 100 | + */ |
| 101 | + void processed(MessageHandler.Context messageContext); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Strategy that provides 1 initial credit and a credit on each new chunk. |
| 106 | + * |
| 107 | + * <p>Calls to {@link MessageHandler.Context#processed()} are ignored. |
| 108 | + * |
| 109 | + * @return flow strategy |
| 110 | + */ |
| 111 | + static ConsumerFlowStrategy creditOnChunkArrival() { |
| 112 | + return creditOnChunkArrival(1); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Strategy that provides the specified number of initial credits and a credit on each new chunk. |
| 117 | + * |
| 118 | + * <p>Calls to {@link MessageHandler.Context#processed()} are ignored. |
| 119 | + * |
| 120 | + * @param initialCredits number of initial credits |
| 121 | + * @return flow strategy |
| 122 | + */ |
| 123 | + static ConsumerFlowStrategy creditOnChunkArrival(int initialCredits) { |
| 124 | + return new CreditOnChunkArrivalConsumerFlowStrategy(initialCredits); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Strategy that provides 1 initial credit and a credit when half of the chunk messages are |
| 129 | + * processed. |
| 130 | + * |
| 131 | + * <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using |
| 132 | + * this strategy, otherwise the broker may stop sending messages to the consumer. |
| 133 | + * |
| 134 | + * @return flow strategy |
| 135 | + */ |
| 136 | + static ConsumerFlowStrategy creditWhenHalfMessagesProcessed() { |
| 137 | + return creditOnProcessedMessageCount(1, 0.5); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Strategy that provides the specified number of initial credits and a credit when half of the |
| 142 | + * chunk messages are processed. |
| 143 | + * |
| 144 | + * <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using |
| 145 | + * this strategy, otherwise the broker may stop sending messages to the consumer. |
| 146 | + * |
| 147 | + * @param initialCredits number of initial credits |
| 148 | + * @return flow strategy |
| 149 | + */ |
| 150 | + static ConsumerFlowStrategy creditWhenHalfMessagesProcessed(int initialCredits) { |
| 151 | + return creditOnProcessedMessageCount(initialCredits, 0.5); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Strategy that provides the specified number of initial credits and a credit when the specified |
| 156 | + * ratio of the chunk messages are processed. |
| 157 | + * |
| 158 | + * <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using |
| 159 | + * this strategy, otherwise the broker may stop sending messages to the consumer. |
| 160 | + * |
| 161 | + * @param initialCredits number of initial credits |
| 162 | + * @return flow strategy |
| 163 | + */ |
| 164 | + static ConsumerFlowStrategy creditOnProcessedMessageCount(int initialCredits, double ratio) { |
| 165 | + return new MessageCountConsumerFlowStrategy(initialCredits, ratio); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Strategy that provides the specified number of initial credits and a credit on each new chunk. |
| 170 | + * |
| 171 | + * <p>Calls to {@link MessageHandler.Context#processed()} are ignored. |
| 172 | + */ |
| 173 | + class CreditOnChunkArrivalConsumerFlowStrategy implements ConsumerFlowStrategy { |
| 174 | + |
| 175 | + private final int initialCredits; |
| 176 | + |
| 177 | + private CreditOnChunkArrivalConsumerFlowStrategy(int initialCredits) { |
| 178 | + this.initialCredits = initialCredits; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public int initialCredits() { |
| 183 | + return this.initialCredits; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public MessageProcessedCallback start(Context context) { |
| 188 | + context.credits(1); |
| 189 | + return value -> {}; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Strategy that provides the specified number of initial credits and a credit when the specified |
| 195 | + * ratio of the chunk messages are processed. |
| 196 | + * |
| 197 | + * <p>Make sure to call {@link MessageHandler.Context#processed()} on every message when using |
| 198 | + * this strategy, otherwise the broker may stop sending messages to the consumer. |
| 199 | + */ |
| 200 | + class MessageCountConsumerFlowStrategy implements ConsumerFlowStrategy { |
| 201 | + |
| 202 | + private final int initialCredits; |
| 203 | + private final double ratio; |
| 204 | + |
| 205 | + private MessageCountConsumerFlowStrategy(int initialCredits, double ratio) { |
| 206 | + this.initialCredits = initialCredits; |
| 207 | + this.ratio = ratio; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public int initialCredits() { |
| 212 | + return this.initialCredits; |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public MessageProcessedCallback start(Context context) { |
| 217 | + long l = (long) (context.messageCount() * ratio); |
| 218 | + long limit = Math.max(1, l); |
| 219 | + AtomicLong processedMessages = new AtomicLong(0); |
| 220 | + return messageOffset -> { |
| 221 | + if (processedMessages.incrementAndGet() == limit) { |
| 222 | + context.credits(1); |
| 223 | + } |
| 224 | + }; |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments