-
Notifications
You must be signed in to change notification settings - Fork 1.6k
GH-656: Fix seek on rollback [Backport] #659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2018 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 | ||
* | ||
* http://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 java.util.List; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
/** | ||
* Invoked by a listener container with remaining, unprocessed, records | ||
* (including the failed record). Implementations should seek the desired | ||
* topics/partitions so that records will be re-fetched on the next | ||
* poll. When used with a batch listener, the entire batch of records is | ||
* provided. | ||
* | ||
* @param <K> the key type. | ||
* @param <V> the value type. | ||
* | ||
* @author Gary Russell | ||
* | ||
* @since 1.3.5 | ||
* | ||
*/ | ||
@FunctionalInterface | ||
public interface AfterRollbackProcessor<K, V> { | ||
|
||
/** | ||
* Process the remaining records. | ||
* @param records the records. | ||
* @param consumer the consumer. | ||
*/ | ||
void process(List<ConsumerRecord<K, V>> records, Consumer<K, V> consumer); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2018 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 | ||
* | ||
* http://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 java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.kafka.clients.consumer.Consumer; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.common.TopicPartition; | ||
|
||
/** | ||
* Default implementation of {@link AfterRollbackProcessor}. Seeks all | ||
* topic/partitions so the records will be re-fetched, including the failed | ||
* record. | ||
* | ||
* @param <K> the key type. | ||
* @param <V> the value type. | ||
* | ||
* @author Gary Russell | ||
* | ||
* @since 1.3.5 | ||
* | ||
*/ | ||
public class DefaultAfterRollbackProcessor<K, V> implements AfterRollbackProcessor<K, V> { | ||
|
||
private static final Log logger = LogFactory.getLog(DefaultAfterRollbackProcessor.class); | ||
|
||
@Override | ||
public void process(List<ConsumerRecord<K, V>> records, Consumer<K, V> consumer) { | ||
Map<TopicPartition, Long> seekOffsets = new HashMap<>(); | ||
Iterator<ConsumerRecord<K, V>> iterator = records.iterator(); | ||
while (iterator.hasNext()) { | ||
ConsumerRecord<K, V> record = iterator.next(); | ||
TopicPartition topicPartition = new TopicPartition(record.topic(), record.partition()); | ||
if (!seekOffsets.containsKey(topicPartition)) { | ||
seekOffsets.put(topicPartition, record.offset()); | ||
} | ||
} | ||
for (Entry<TopicPartition, Long> entry : seekOffsets.entrySet()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this extra loop if we can just perform There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems much more complicated to me to keep track of which ones we've already seeked. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I mean something like this: Set<TopicPartition> seekOffsets = new HashSet<>();
Iterator<ConsumerRecord<K, V>> iterator = records.iterator();
while (iterator.hasNext()) {
ConsumerRecord<K, V> record = iterator.next();
TopicPartition topicPartition = new TopicPartition(record.topic(), record.partition());
if (seekOffsets.add(topicPartition)) {
try {
consumer.seek(topicPartition, record.offset());
}
catch (Exception e) {
logger.error("Failed to seek " + entry.getKey() + " to " + entry.getValue());
}
}
} No? What am I missing? Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a big difference and I doubt the performance will be any better - it has to calculate hash codes instead performing the second loop - but feel free to change it if you insist. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
??? Don't you do that already with the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh, of course; ok. Feel free to change it. |
||
try { | ||
consumer.seek(entry.getKey(), entry.getValue()); | ||
} | ||
catch (Exception e) { | ||
logger.error("Failed to seek " + entry.getKey() + " to " + entry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not going to work on Java 7, I guess.
Or will be ignored at runtime...