/
DefaultKafkaProducerFactory.java
628 lines (550 loc) · 19.2 KB
/
DefaultKafkaProducerFactory.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
* Copyright 2016-2019 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.kafka.core;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.ProducerFencedException;
import org.apache.kafka.common.errors.TimeoutException;
import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.common.utils.AppInfoParser;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.kafka.support.TransactionSupport;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* The {@link ProducerFactory} implementation for a {@code singleton} shared
* {@link Producer} instance.
* <p>
* This implementation will return the same {@link Producer} instance (if transactions are
* not enabled) for the provided {@link Map} {@code configs} and optional {@link Serializer}
* {@code keySerializer}, {@code valueSerializer} implementations on each
* {@link #createProducer()} invocation.
* <p>
* The {@link Producer} is wrapped and the underlying {@link KafkaProducer} instance is
* not actually closed when {@link Producer#close()} is invoked. The {@link KafkaProducer}
* is physically closed when {@link DisposableBean#destroy()} is invoked or when the
* application context publishes a {@link ContextStoppedEvent}. You can also invoke
* {@link #reset()}.
* <p>
* Setting {@link #setTransactionIdPrefix(String)} enables transactions; in which case, a
* cache of producers is maintained; closing a producer returns it to the cache. The
* producers are closed and the cache is cleared when the factory is destroyed, the
* application context stopped, or the {@link #reset()} method is called.
*
* @param <K> the key type.
* @param <V> the value type.
*
* @author Gary Russell
* @author Murali Reddy
* @author Nakul Mishra
*/
public class DefaultKafkaProducerFactory<K, V> implements ProducerFactory<K, V>, ApplicationContextAware,
ApplicationListener<ContextStoppedEvent>, DisposableBean {
private static final int DEFAULT_PHYSICAL_CLOSE_TIMEOUT = 30;
private static final Log logger = LogFactory.getLog(DefaultKafkaProducerFactory.class); // NOSONAR
private final Map<String, Object> configs;
private final AtomicInteger transactionIdSuffix = new AtomicInteger();
private final BlockingQueue<CloseSafeProducer<K, V>> cache = new LinkedBlockingQueue<>();
private final Map<String, CloseSafeProducer<K, V>> consumerProducers = new HashMap<>();
private final AtomicInteger clientIdCounter = new AtomicInteger();
private volatile CloseSafeProducer<K, V> producer;
private Serializer<K> keySerializer;
private Serializer<V> valueSerializer;
private int physicalCloseTimeout = DEFAULT_PHYSICAL_CLOSE_TIMEOUT;
private String transactionIdPrefix;
private ApplicationContext applicationContext;
private boolean producerPerConsumerPartition = true;
private String clientIdPrefix;
/**
* Construct a factory with the provided configuration.
* @param configs the configuration.
*/
public DefaultKafkaProducerFactory(Map<String, Object> configs) {
this(configs, null, null);
}
/**
* Construct a factory with the provided configuration and {@link Serializer}s.
* @param configs the configuration.
* @param keySerializer the key {@link Serializer}.
* @param valueSerializer the value {@link Serializer}.
*/
public DefaultKafkaProducerFactory(Map<String, Object> configs,
@Nullable Serializer<K> keySerializer,
@Nullable Serializer<V> valueSerializer) {
this.configs = new HashMap<>(configs);
this.keySerializer = keySerializer;
this.valueSerializer = valueSerializer;
if (configs.get(ProducerConfig.CLIENT_ID_CONFIG) instanceof String) {
this.clientIdPrefix = (String) configs.get(ProducerConfig.CLIENT_ID_CONFIG);
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public void setKeySerializer(@Nullable Serializer<K> keySerializer) {
this.keySerializer = keySerializer;
}
public void setValueSerializer(@Nullable Serializer<V> valueSerializer) {
this.valueSerializer = valueSerializer;
}
/**
* The time to wait when physically closing the producer via the factory rather than
* closing the producer itself (when {@link #reset()}, {@link #destroy() or
* #closeProducerFor(String)} are invoked). Specified in seconds; default
* {@link #DEFAULT_PHYSICAL_CLOSE_TIMEOUT}.
* @param physicalCloseTimeout the timeout in seconds.
* @since 1.0.7
*/
public void setPhysicalCloseTimeout(int physicalCloseTimeout) {
this.physicalCloseTimeout = physicalCloseTimeout;
}
/**
* Set the transactional.id prefix.
* @param transactionIdPrefix the prefix.
* @since 1.3
*/
public void setTransactionIdPrefix(String transactionIdPrefix) {
Assert.notNull(transactionIdPrefix, "'transactionIdPrefix' cannot be null");
this.transactionIdPrefix = transactionIdPrefix;
enableIdempotentBehaviour();
}
/**
* When set to 'true', the producer will ensure that exactly one copy of each message is written in the stream.
*/
private void enableIdempotentBehaviour() {
Object previousValue = this.configs.putIfAbsent(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
if (logger.isDebugEnabled() && Boolean.FALSE.equals(previousValue)) {
logger.debug("The '" + ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG +
"' is set to false, may result in duplicate messages");
}
}
/**
* Set to false to revert to the previous behavior of a simple incrementing
* trasactional.id suffix for each producer instead of maintaining a producer
* for each group/topic/partition.
* @param producerPerConsumerPartition false to revert.
* @since 1.3.7
*/
public void setProducerPerConsumerPartition(boolean producerPerConsumerPartition) {
this.producerPerConsumerPartition = producerPerConsumerPartition;
}
/**
* Return the producerPerConsumerPartition.
* @return the producerPerConsumerPartition.
* @since 1.3.8
*/
@Override
public boolean isProducerPerConsumerPartition() {
return this.producerPerConsumerPartition;
}
/**
* Return an unmodifiable reference to the configuration map for this factory.
* Useful for cloning to make a similar factory.
* @return the configs.
* @since 1.3
*/
public Map<String, Object> getConfigurationProperties() {
return Collections.unmodifiableMap(this.configs);
}
@Override
public boolean transactionCapable() {
return this.transactionIdPrefix != null;
}
@SuppressWarnings("resource")
@Override
public void destroy() {
CloseSafeProducer<K, V> producerToClose = this.producer;
this.producer = null;
if (producerToClose != null) {
producerToClose.delegate.close(this.physicalCloseTimeout, TimeUnit.SECONDS);
}
producerToClose = this.cache.poll();
while (producerToClose != null) {
try {
producerToClose.delegate.close(this.physicalCloseTimeout, TimeUnit.SECONDS);
}
catch (Exception e) {
logger.error("Exception while closing producer", e);
}
producerToClose = this.cache.poll();
}
synchronized (this.consumerProducers) {
this.consumerProducers.forEach(
(k, v) -> v.delegate.close(this.physicalCloseTimeout, TimeUnit.SECONDS));
this.consumerProducers.clear();
}
}
@Override
public void onApplicationEvent(ContextStoppedEvent event) {
if (event.getApplicationContext().equals(this.applicationContext)) {
reset();
}
}
/**
* NoOp.
* @deprecated {@link org.springframework.context.Lifecycle} is no longer implemented.
*/
@Deprecated
public void start() {
// NOSONAR
}
/**
* NoOp.
* @deprecated {@link org.springframework.context.Lifecycle} is no longer implemented;
* use {@link #reset()} to close the {@link Producer}(s).
*/
@Deprecated
public void stop() {
reset();
}
/**
* Close the {@link Producer}(s) and clear the cache of transactional
* {@link Producer}(s).
* @since 2.2
*/
public void reset() {
try {
destroy();
}
catch (Exception e) {
logger.error("Exception while closing producer", e);
}
}
/**
* NoOp.
* @return always true.
* @deprecated {@link org.springframework.context.Lifecycle} is no longer implemented.
*/
@Deprecated
public boolean isRunning() {
return true;
}
@Override
public Producer<K, V> createProducer() {
if (this.transactionIdPrefix != null) {
if (this.producerPerConsumerPartition) {
return createTransactionalProducerForPartition();
}
else {
return createTransactionalProducer();
}
}
if (this.producer == null) {
synchronized (this) {
if (this.producer == null) {
this.producer = new CloseSafeProducer<K, V>(createKafkaProducer());
}
}
}
return this.producer;
}
/**
* Subclasses must return a raw producer which will be wrapped in a
* {@link CloseSafeProducer}.
* @return the producer.
*/
protected Producer<K, V> createKafkaProducer() {
if (this.clientIdPrefix == null) {
return new KafkaProducer<K, V>(this.configs, this.keySerializer, this.valueSerializer);
}
else {
Map<String, Object> newConfigs = new HashMap<>(this.configs);
newConfigs.put(ProducerConfig.CLIENT_ID_CONFIG,
this.clientIdPrefix + "-" + this.clientIdCounter.incrementAndGet());
return new KafkaProducer<>(newConfigs, this.keySerializer, this.valueSerializer);
}
}
Producer<K, V> createTransactionalProducerForPartition() {
String suffix = TransactionSupport.getTransactionIdSuffix();
if (suffix == null) {
return createTransactionalProducer();
}
else {
synchronized (this.consumerProducers) {
if (!this.consumerProducers.containsKey(suffix)) {
CloseSafeProducer<K, V> newProducer = doCreateTxProducer(suffix, this::removeConsumerProducer);
this.consumerProducers.put(suffix, newProducer);
return newProducer;
}
else {
return this.consumerProducers.get(suffix);
}
}
}
}
private void removeConsumerProducer(CloseSafeProducer<K, V> producer) {
synchronized (this.consumerProducers) {
Iterator<Entry<String, CloseSafeProducer<K, V>>> iterator = this.consumerProducers.entrySet().iterator();
while (iterator.hasNext()) {
if (iterator.next().getValue().equals(producer)) {
iterator.remove();
break;
}
}
}
}
/**
* Subclasses must return a producer from the {@link #getCache()} or a
* new raw producer wrapped in a {@link CloseSafeProducer}.
* @return the producer - cannot be null.
* @since 1.3
*/
protected Producer<K, V> createTransactionalProducer() {
Producer<K, V> cachedProducer = this.cache.poll();
if (cachedProducer == null) {
return doCreateTxProducer("" + this.transactionIdSuffix.getAndIncrement(), null);
}
else {
return cachedProducer;
}
}
private CloseSafeProducer<K, V> doCreateTxProducer(String suffix, Consumer<CloseSafeProducer<K, V>> remover) {
Producer<K, V> newProducer;
Map<String, Object> newProducerConfigs = new HashMap<>(this.configs);
newProducerConfigs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, this.transactionIdPrefix + suffix);
if (this.clientIdPrefix != null) {
newProducerConfigs.put(ProducerConfig.CLIENT_ID_CONFIG,
this.clientIdPrefix + "-" + this.clientIdCounter.incrementAndGet());
}
newProducer = new KafkaProducer<K, V>(newProducerConfigs, this.keySerializer, this.valueSerializer);
newProducer.initTransactions();
return new CloseSafeProducer<K, V>(newProducer, this.cache, remover,
(String) newProducerConfigs.get(ProducerConfig.TRANSACTIONAL_ID_CONFIG));
}
protected BlockingQueue<CloseSafeProducer<K, V>> getCache() {
return this.cache;
}
@Override
public void closeProducerFor(String transactionIdSuffix) {
if (this.producerPerConsumerPartition) {
synchronized (this.consumerProducers) {
CloseSafeProducer<K, V> removed = this.consumerProducers.remove(transactionIdSuffix);
if (removed != null) {
removed.delegate.close(this.physicalCloseTimeout, TimeUnit.SECONDS);
}
}
}
}
/**
* A wrapper class for the delegate.
*
* @param <K> the key type.
* @param <V> the value type.
*
*/
protected static class CloseSafeProducer<K, V> implements Producer<K, V> {
private static final Duration CLOSE_TIMEOUT_AFTER_TX_TIMEOUT = Duration.ofMillis(0);
private static final Method CLOSE_WITH_DURATION;
static {
Method method = null;
String clientVersion = AppInfoParser.getVersion();
try {
if (!clientVersion.startsWith("1.") && !clientVersion.startsWith("2.0.")
&& !clientVersion.startsWith("2.1.")) {
method = KafkaProducer.class.getDeclaredMethod("close", Duration.class);
}
}
catch (NoSuchMethodException e) {
logger.error("Failed to get close(Duration) method for version: " + clientVersion, e);
}
CLOSE_WITH_DURATION = method;
}
private final Producer<K, V> delegate;
private final BlockingQueue<CloseSafeProducer<K, V>> cache;
private final Consumer<CloseSafeProducer<K, V>> removeConsumerProducer;
private final String txId;
private volatile Exception txFailed;
CloseSafeProducer(Producer<K, V> delegate) {
this(delegate, null, null);
Assert.isTrue(!(delegate instanceof CloseSafeProducer), "Cannot double-wrap a producer");
}
CloseSafeProducer(Producer<K, V> delegate, BlockingQueue<CloseSafeProducer<K, V>> cache) {
this(delegate, cache, null);
}
CloseSafeProducer(Producer<K, V> delegate, @Nullable BlockingQueue<CloseSafeProducer<K, V>> cache,
@Nullable Consumer<CloseSafeProducer<K, V>> removeConsumerProducer) {
this(delegate, cache, removeConsumerProducer, null);
}
CloseSafeProducer(Producer<K, V> delegate, @Nullable BlockingQueue<CloseSafeProducer<K, V>> cache,
@Nullable Consumer<CloseSafeProducer<K, V>> removeConsumerProducer, @Nullable String txId) {
this.delegate = delegate;
this.cache = cache;
this.removeConsumerProducer = removeConsumerProducer;
this.txId = txId;
}
@Override
public Future<RecordMetadata> send(ProducerRecord<K, V> record) {
return this.delegate.send(record);
}
@Override
public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callback) {
return this.delegate.send(record, callback);
}
@Override
public void flush() {
this.delegate.flush();
}
@Override
public List<PartitionInfo> partitionsFor(String topic) {
return this.delegate.partitionsFor(topic);
}
@Override
public Map<MetricName, ? extends Metric> metrics() {
return this.delegate.metrics();
}
@Override
public void initTransactions() {
this.delegate.initTransactions();
}
@Override
public void beginTransaction() throws ProducerFencedException {
if (logger.isDebugEnabled()) {
logger.debug("beginTransaction: " + this);
}
try {
this.delegate.beginTransaction();
}
catch (RuntimeException e) {
if (logger.isErrorEnabled()) {
logger.error("beginTransaction failed: " + this, e);
}
this.txFailed = e;
throw e;
}
}
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId)
throws ProducerFencedException {
this.delegate.sendOffsetsToTransaction(offsets, consumerGroupId);
}
@Override
public void commitTransaction() throws ProducerFencedException {
if (logger.isDebugEnabled()) {
logger.debug("commitTransaction: " + this);
}
try {
this.delegate.commitTransaction();
}
catch (RuntimeException e) {
if (logger.isErrorEnabled()) {
logger.error("commitTransaction failed: " + this, e);
}
this.txFailed = e;
throw e;
}
}
@Override
public void abortTransaction() throws ProducerFencedException {
if (logger.isDebugEnabled()) {
logger.debug("abortTransaction: " + this);
}
try {
this.delegate.abortTransaction();
}
catch (RuntimeException e) {
if (logger.isErrorEnabled()) {
logger.error("Abort failed: " + this, e);
}
this.txFailed = e;
throw e;
}
}
@Override
public void close() {
close(0, null);
}
@Override
public void close(long timeout, @Nullable TimeUnit unit) {
if (this.cache != null) {
Duration closeTimeout = this.txFailed instanceof TimeoutException || unit == null
? CLOSE_TIMEOUT_AFTER_TX_TIMEOUT
: Duration.ofMillis(unit.toMillis(timeout));
if (this.txFailed != null) {
if (logger.isWarnEnabled()) {
logger.warn("Error during transactional operation; producer removed from cache; "
+ "possible cause: "
+ "broker restarted during transaction: " + this);
}
closeDelegate(closeTimeout);
if (this.removeConsumerProducer != null) {
this.removeConsumerProducer.accept(this);
}
}
else {
if (this.removeConsumerProducer == null) { // dedicated consumer producers are not cached
synchronized (this) {
if (!this.cache.contains(this)
&& !this.cache.offer(this)) {
closeDelegate(closeTimeout);
}
}
}
}
}
}
private void closeDelegate(Duration closeTimeout) {
if (CLOSE_WITH_DURATION != null) {
try {
CLOSE_WITH_DURATION.invoke(this.delegate, closeTimeout);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.error("Failed to invoke close(Duration) with reflection", e);
}
}
else {
this.delegate.close(closeTimeout.toMillis(), TimeUnit.MILLISECONDS);
}
}
@Override
public String toString() {
return "CloseSafeProducer [delegate=" + this.delegate + ""
+ (this.txId != null ? ", txId=" + this.txId : "")
+ "]";
}
}
}