-
Notifications
You must be signed in to change notification settings - Fork 595
/
ConsumerConfigProperties.java
458 lines (366 loc) · 14.7 KB
/
ConsumerConfigProperties.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.cloud.stream.binder.pulsar.properties;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.pulsar.client.api.ConsumerCryptoFailureAction;
import org.apache.pulsar.client.api.MessageId;
import org.springframework.boot.autoconfigure.pulsar.PulsarProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.util.Assert;
/**
* Configuration properties used to specify Pulsar consumers.
*
* @author Chris Bono
*/
public class ConsumerConfigProperties extends PulsarProperties.Consumer {
private final Acknowledgement ack = new Acknowledgement();
private final Chunking chunk = new Chunking();
private final Subscription subscription = new Subscription();
/**
* Number of messages that can be accumulated before the consumer calls "receive".
*/
private Integer receiverQueueSize = 1000;
/**
* Maximum number of messages that a consumer can be pushed at once from a broker
* across all partitions.
*/
private Integer maxTotalReceiverQueueSizeAcrossPartitions = 50000;
/**
* Action the consumer will take in case of decryption failure.
*/
private ConsumerCryptoFailureAction cryptoFailureAction = ConsumerCryptoFailureAction.FAIL;
/**
* Map of properties to add to the consumer.
*/
private SortedMap<String, String> properties = new TreeMap<>();
/**
* Auto-discovery period for topics when topic pattern is used in minutes.
*/
private Integer patternAutoDiscoveryPeriod = 1;
/**
* Whether the consumer auto-subscribes for partition increase. This is only for
* partitioned consumers.
*/
private Boolean autoUpdatePartitions = true;
/**
* Interval of partitions discovery updates.
*/
private Duration autoUpdatePartitionsInterval = Duration.ofMinutes(1);
/**
* Whether to include the given position of any reset operation like
* {@link org.apache.pulsar.client.api.Consumer#seek(long) or
* {@link ConsumerConfigProperties#seek(MessageId)}}.
*/
private Boolean resetIncludeHead = false;
/**
* Whether pooling of messages and the underlying data buffers is enabled.
*/
private Boolean poolMessages = false;
/**
* Whether to start the consumer in a paused state.
*/
private Boolean startPaused = false;
public Acknowledgement getAck() {
return this.ack;
}
public Chunking getChunk() {
return this.chunk;
}
public Subscription getSubscription() {
return this.subscription;
}
public Integer getReceiverQueueSize() {
return this.receiverQueueSize;
}
public void setReceiverQueueSize(Integer receiverQueueSize) {
this.receiverQueueSize = receiverQueueSize;
}
public Integer getMaxTotalReceiverQueueSizeAcrossPartitions() {
return this.maxTotalReceiverQueueSizeAcrossPartitions;
}
public void setMaxTotalReceiverQueueSizeAcrossPartitions(Integer maxTotalReceiverQueueSizeAcrossPartitions) {
this.maxTotalReceiverQueueSizeAcrossPartitions = maxTotalReceiverQueueSizeAcrossPartitions;
}
public ConsumerCryptoFailureAction getCryptoFailureAction() {
return this.cryptoFailureAction;
}
public void setCryptoFailureAction(ConsumerCryptoFailureAction cryptoFailureAction) {
this.cryptoFailureAction = cryptoFailureAction;
}
public SortedMap<String, String> getProperties() {
return this.properties;
}
public void setProperties(SortedMap<String, String> properties) {
this.properties = properties;
}
public Integer getPatternAutoDiscoveryPeriod() {
return this.patternAutoDiscoveryPeriod;
}
public void setPatternAutoDiscoveryPeriod(Integer patternAutoDiscoveryPeriod) {
this.patternAutoDiscoveryPeriod = patternAutoDiscoveryPeriod;
}
public Boolean getAutoUpdatePartitions() {
return this.autoUpdatePartitions;
}
public void setAutoUpdatePartitions(Boolean autoUpdatePartitions) {
this.autoUpdatePartitions = autoUpdatePartitions;
}
public Duration getAutoUpdatePartitionsInterval() {
return this.autoUpdatePartitionsInterval;
}
public void setAutoUpdatePartitionsInterval(Duration autoUpdatePartitionsInterval) {
this.autoUpdatePartitionsInterval = autoUpdatePartitionsInterval;
}
public Boolean getResetIncludeHead() {
return this.resetIncludeHead;
}
public void setResetIncludeHead(Boolean resetIncludeHead) {
this.resetIncludeHead = resetIncludeHead;
}
public Boolean getPoolMessages() {
return this.poolMessages;
}
public void setPoolMessages(Boolean poolMessages) {
this.poolMessages = poolMessages;
}
public Boolean getStartPaused() {
return this.startPaused;
}
public void setStartPaused(Boolean startPaused) {
this.startPaused = startPaused;
}
/**
* Gets a map representation of the base consumer properties (those defined in parent
* class).
* @return map of base consumer properties and associated values.
*/
public Map<String, Object> toBaseConsumerPropertiesMap() {
var consumerProps = new ConsumerConfigProperties.Properties();
var map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(this::getDeadLetterPolicy).as(this::toPulsarDeadLetterPolicy).to(consumerProps.in("deadLetterPolicy"));
map.from(this::getName).to(consumerProps.in("consumerName"));
map.from(this::getPriorityLevel).to(consumerProps.in("priorityLevel"));
map.from(this::isReadCompacted).to(consumerProps.in("readCompacted"));
map.from(this::isRetryEnable).to(consumerProps.in("retryEnable"));
map.from(this::getTopics).to(consumerProps.in("topicNames"));
map.from(this::getTopicsPattern).to(consumerProps.in("topicsPattern"));
mapBaseSubscriptionProperties(this.getSubscription(), consumerProps, map);
return consumerProps;
}
private org.apache.pulsar.client.api.DeadLetterPolicy toPulsarDeadLetterPolicy(DeadLetterPolicy policy) {
Assert.state(policy.getMaxRedeliverCount() > 0,
"Pulsar DeadLetterPolicy must have a positive 'max-redelivery-count' property value");
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
org.apache.pulsar.client.api.DeadLetterPolicy.DeadLetterPolicyBuilder builder = org.apache.pulsar.client.api.DeadLetterPolicy
.builder();
map.from(policy::getMaxRedeliverCount).to(builder::maxRedeliverCount);
map.from(policy::getRetryLetterTopic).to(builder::retryLetterTopic);
map.from(policy::getDeadLetterTopic).to(builder::deadLetterTopic);
map.from(policy::getInitialSubscriptionName).to(builder::initialSubscriptionName);
return builder.build();
}
private void mapBaseSubscriptionProperties(PulsarProperties.Consumer.Subscription subscription, Properties consumerProps, PropertyMapper map) {
map.from(subscription::getName).to(consumerProps.in("subscriptionName"));
map.from(subscription::getType).to(consumerProps.in("subscriptionType"));
map.from(subscription::getMode).to(consumerProps.in("subscriptionMode"));
map.from(subscription::getInitialPosition).to(consumerProps.in("subscriptionInitialPosition"));
map.from(subscription::getTopicsMode).to(consumerProps.in("regexSubscriptionMode"));
}
/**
* Gets a map representation of the extended consumer properties (those defined in
* this class).
* @return map of extended consumer properties and associated values.
*/
public Map<String, Object> toExtendedConsumerPropertiesMap() {
var consumerProps = new ConsumerConfigProperties.Properties();
var map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(this::getAutoUpdatePartitions).to(consumerProps.in("autoUpdatePartitions"));
map.from(this::getAutoUpdatePartitionsInterval).as(Duration::toSeconds)
.to(consumerProps.in("autoUpdatePartitionsIntervalSeconds"));
map.from(this::getCryptoFailureAction).to(consumerProps.in("cryptoFailureAction"));
map.from(this::getMaxTotalReceiverQueueSizeAcrossPartitions)
.to(consumerProps.in("maxTotalReceiverQueueSizeAcrossPartitions"));
map.from(this::getPatternAutoDiscoveryPeriod).to(consumerProps.in("patternAutoDiscoveryPeriod"));
map.from(this::getPoolMessages).to(consumerProps.in("poolMessages"));
map.from(this::getProperties).to(consumerProps.in("properties"));
map.from(this::getReceiverQueueSize).to(consumerProps.in("receiverQueueSize"));
map.from(this::getResetIncludeHead).to(consumerProps.in("resetIncludeHead"));
map.from(this::getStartPaused).to(consumerProps.in("startPaused"));
this.mapAcknowledgementProperties(this.getAck(), consumerProps, map);
this.mapChunkProperties(this.getChunk(), consumerProps, map);
this.mapExtendedSubscriptionProperties(this.getSubscription(), consumerProps, map);
return consumerProps;
}
private void mapAcknowledgementProperties(Acknowledgement ack, Properties consumerProps, PropertyMapper map) {
map.from(ack::getGroupTime).as(it -> it.toNanos() / 1000)
.to(consumerProps.in("acknowledgementsGroupTimeMicros"));
map.from(ack::getRedeliveryDelay).as(it -> it.toNanos() / 1000)
.to(consumerProps.in("negativeAckRedeliveryDelayMicros"));
map.from(ack::getTimeout).as(Duration::toMillis)
.to(consumerProps.in("ackTimeoutMillis"));
map.from(ack::getTimeoutTickDuration).as(Duration::toMillis)
.to(consumerProps.in("tickDurationMillis"));
map.from(ack::getBatchIndexEnabled).to(consumerProps.in("batchIndexAckEnabled"));
map.from(ack::getReceiptEnabled).to(consumerProps.in("ackReceiptEnabled"));
}
private void mapChunkProperties(Chunking chunk, Properties consumerProps, PropertyMapper map) {
map.from(chunk::getExpireTimeIncomplete).as(Duration::toMillis)
.to(consumerProps.in("expireTimeOfIncompleteChunkedMessageMillis"));
map.from(chunk::getAutoAckOldestOnQueueFull)
.to(consumerProps.in("autoAckOldestChunkedMessageOnQueueFull"));
map.from(chunk::getMaxPendingMessages).to(consumerProps.in("maxPendingChunkedMessage"));
}
private void mapExtendedSubscriptionProperties(Subscription subscription, Properties consumerProps, PropertyMapper map) {
map.from(subscription::getProperties).to(consumerProps.in("subscriptionProperties"));
map.from(subscription::getReplicateState).to(consumerProps.in("replicateSubscriptionState"));
}
/**
* Gets a map representation of base and extended consumer properties.
* @return map of base and extended consumer properties and associated values.
*/
public Map<String, Object> toAllConsumerPropertiesMap() {
var consumerProps = this.toBaseConsumerPropertiesMap();
consumerProps.putAll(this.toExtendedConsumerPropertiesMap());
return consumerProps;
}
public static class Acknowledgement {
/**
* Whether the batching index acknowledgment is enabled.
*/
private Boolean batchIndexEnabled = false;
/**
* Time to group acknowledgements before sending them to the broker.
*/
private Duration groupTime = Duration.ofMillis(100);
/**
* Whether an acknowledgement receipt is enabled.
*/
private Boolean receiptEnabled = false;
/**
* Delay before re-delivering messages that have failed to be processed.
*/
private Duration redeliveryDelay = Duration.ofMinutes(1);
/**
* Timeout for unacked messages to be redelivered.
*/
private Duration timeout = Duration.ZERO;
/**
* Precision for the ack timeout messages tracker.
*/
private Duration timeoutTickDuration = Duration.ofSeconds(1);
public Boolean getBatchIndexEnabled() {
return this.batchIndexEnabled;
}
public void setBatchIndexEnabled(Boolean batchIndexEnabled) {
this.batchIndexEnabled = batchIndexEnabled;
}
public Duration getGroupTime() {
return this.groupTime;
}
public void setGroupTime(Duration groupTime) {
this.groupTime = groupTime;
}
public Boolean getReceiptEnabled() {
return this.receiptEnabled;
}
public void setReceiptEnabled(Boolean receiptEnabled) {
this.receiptEnabled = receiptEnabled;
}
public Duration getRedeliveryDelay() {
return this.redeliveryDelay;
}
public void setRedeliveryDelay(Duration redeliveryDelay) {
this.redeliveryDelay = redeliveryDelay;
}
public Duration getTimeout() {
return this.timeout;
}
public void setTimeout(Duration timeout) {
this.timeout = timeout;
}
public Duration getTimeoutTickDuration() {
return this.timeoutTickDuration;
}
public void setTimeoutTickDuration(Duration timeoutTickDuration) {
this.timeoutTickDuration = timeoutTickDuration;
}
}
public static class Chunking {
/**
* Whether to automatically drop outstanding uncompleted chunked messages once the
* consumer queue reaches the threshold set by the 'maxPendingMessages' property.
*/
private Boolean autoAckOldestOnQueueFull = true;
/**
* The maximum time period for a consumer to receive all chunks of a message - if
* this threshold is exceeded the consumer will expire the incomplete chunks.
*/
private Duration expireTimeIncomplete = Duration.ofMinutes(1);
/**
* Maximum number of chunked messages to be kept in memory.
*/
private Integer maxPendingMessages = 10;
public Boolean getAutoAckOldestOnQueueFull() {
return this.autoAckOldestOnQueueFull;
}
public void setAutoAckOldestOnQueueFull(Boolean autoAckOldestOnQueueFull) {
this.autoAckOldestOnQueueFull = autoAckOldestOnQueueFull;
}
public Duration getExpireTimeIncomplete() {
return this.expireTimeIncomplete;
}
public void setExpireTimeIncomplete(Duration expireTimeIncomplete) {
this.expireTimeIncomplete = expireTimeIncomplete;
}
public Integer getMaxPendingMessages() {
return this.maxPendingMessages;
}
public void setMaxPendingMessages(Integer maxPendingMessages) {
this.maxPendingMessages = maxPendingMessages;
}
}
public static class Subscription extends PulsarProperties.Consumer.Subscription {
/**
* Map of properties to add to the subscription.
*/
private Map<String, String> properties = new HashMap<>();
/**
* Whether to replicate subscription state.
*/
private Boolean replicateState = false;
public Map<String, String> getProperties() {
return this.properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public Boolean getReplicateState() {
return this.replicateState;
}
public void setReplicateState(Boolean replicateState) {
this.replicateState = replicateState;
}
}
static class Properties extends HashMap<String, Object> {
<V> java.util.function.Consumer<V> in(String key) {
return (value) -> put(key, value);
}
}
}