forked from spring-projects/spring-kafka
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContainerPartitionPausingBackOffManager.java
91 lines (78 loc) · 3.64 KB
/
ContainerPartitionPausingBackOffManager.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
/*
* Copyright 2018-2022 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.listener;
import org.apache.commons.logging.LogFactory;
import org.apache.kafka.common.TopicPartition;
import org.springframework.core.log.LogAccessor;
import org.springframework.util.Assert;
/**
*
* A manager that backs off consumption for a given topic if the timestamp provided is not
* due. Use with {@link DefaultErrorHandler} to guarantee that the message is read
* again after partition consumption is resumed (or seek it manually by other means).
* Note that when a record backs off the partition consumption gets paused for
* approximately that amount of time, so you must have a fixed backoff value per partition.
*
* @author Tomaz Fernandes
* @author Gary Russell
* @since 2.9
* @see DefaultErrorHandler
*/
public class ContainerPartitionPausingBackOffManager implements KafkaConsumerBackoffManager {
private static final LogAccessor LOGGER = new LogAccessor(LogFactory.getLog(KafkaConsumerBackoffManager.class));
private final ListenerContainerRegistry listenerContainerRegistry;
private final BackOffHandler backOffHandler;
/**
* Construct an instance with the provided registry and back off handler.
* @param listenerContainerRegistry
* @param backOffHandler
*/
public ContainerPartitionPausingBackOffManager(ListenerContainerRegistry listenerContainerRegistry,
BackOffHandler backOffHandler) {
this.listenerContainerRegistry = listenerContainerRegistry;
this.backOffHandler = backOffHandler;
}
/**
* Backs off if the current time is before the dueTimestamp provided
* in the {@link Context} object.
* @param context the back off context for this execution.
*/
@Override
public void backOffIfNecessary(Context context) {
long backoffTime = context.getDueTimestamp() - System.currentTimeMillis();
LOGGER.debug(() -> "Back off time: " + backoffTime + " Context: " + context);
if (backoffTime > 0) {
pauseConsumptionAndThrow(context, backoffTime);
}
}
private void pauseConsumptionAndThrow(Context context, Long backOffTime) throws KafkaBackoffException {
TopicPartition topicPartition = context.getTopicPartition();
getListenerContainerFromContext(context).pausePartition(topicPartition);
this.backOffHandler.onNextBackOff(getListenerContainerFromContext(context), topicPartition, backOffTime);
throw new KafkaBackoffException(String.format("Partition %s from topic %s is not ready for consumption, " +
"backing off for approx. %s millis.", context.getTopicPartition().partition(),
context.getTopicPartition().topic(), backOffTime),
topicPartition, context.getListenerId(), context.getDueTimestamp());
}
private MessageListenerContainer getListenerContainerFromContext(Context context) {
MessageListenerContainer container = this.listenerContainerRegistry.getListenerContainer(context.getListenerId()); // NOSONAR
if (container == null) {
container = this.listenerContainerRegistry.getUnregisteredListenerContainer(context.getListenerId());
}
Assert.notNull(container, () -> "No container found with id: " + context.getListenerId());
return container;
}
}