Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package org.springframework.kafka.listener;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.kafka.common.TopicPartition;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
Expand Down Expand Up @@ -97,6 +100,12 @@ public void clearThreadState() {
this.delegates.values().forEach(CommonErrorHandler::clearThreadState);
}

@Override
public void clearThreadStateFor(Collection<TopicPartition> partitions) {
this.defaultErrorHandler.clearThreadStateFor(partitions);
this.delegates.values().forEach(h -> h.clearThreadStateFor(partitions));
}

@Override
public boolean isAckAfterHandle() {
return this.defaultErrorHandler.isAckAfterHandle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ default int deliveryAttempt(TopicPartitionOffset topicPartitionOffset) {
default void clearThreadState() {
}

/**
* Clear thread state only for the given partitions. Called after a successful batch
* invocation to avoid resetting backoff state for partitions that are still retrying.
* @param partitions the partitions whose retry state should be cleared.
* @since 4.0
*/
default void clearThreadStateFor(Collection<TopicPartition> partitions) {
}

/**
* Return true if the offset should be committed for a handled error (no exception
* thrown).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package org.springframework.kafka.listener;

import java.util.Collection;
import java.util.List;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.common.TopicPartition;

import org.springframework.kafka.support.TopicPartitionOffset;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -105,6 +107,12 @@ public void clearThreadState() {
this.recordErrorHandler.clearThreadState();
}

@Override
public void clearThreadStateFor(Collection<TopicPartition> partitions) {
this.batchErrorHandler.clearThreadStateFor(partitions);
this.recordErrorHandler.clearThreadStateFor(partitions);
}

@Override
public boolean isAckAfterHandle() {
return this.recordErrorHandler.isAckAfterHandle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package org.springframework.kafka.listener;

import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;

import org.apache.commons.logging.LogFactory;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.TopicPartition;
import org.jspecify.annotations.Nullable;

import org.springframework.core.log.LogAccessor;
Expand Down Expand Up @@ -179,4 +181,8 @@ public void clearThreadState() {
this.failureTracker.clearThreadState();
}

public void clearThreadStateFor(Collection<TopicPartition> partitions) {
this.failureTracker.clearThreadStateFor(partitions);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ void clearThreadState() {
this.failures.remove(Thread.currentThread());
}

void clearThreadStateFor(java.util.Collection<TopicPartition> partitions) {
Map<TopicPartition, FailedRecord> map = this.failures.get(Thread.currentThread());
if (map != null) {
partitions.forEach(map::remove);
}
}

ConsumerAwareRecordRecoverer getRecoverer() {
return this.recoverer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2397,7 +2397,7 @@ private List<ConsumerRecord<K, V>> createRecordList(final ConsumerRecords<K, V>
if (this.batchFailed) {
this.batchFailed = false;
if (this.commonErrorHandler != null) {
this.commonErrorHandler.clearThreadState();
this.commonErrorHandler.clearThreadStateFor(records.partitions());
}
getAfterRollbackProcessor().clearThreadState();
}
Expand Down