From 461c4014c22b0c0600f8d08578f5676f12d3b036 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 14 Aug 2022 18:16:34 +0200 Subject: [PATCH] =?utf8?q?GR=C3=9CN:=20Korrektur=20des=20=C3=BCber=20die?= =?utf8?q?=20versch=C3=A4rften=20Tests=20aufgedeckten=20Fehlers?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../juplo/kafka/AdderRebalanceListener.java | 28 ++++++++++++++++++- .../java/de/juplo/kafka/EndlessConsumer.java | 4 ++- ...ntervalAwareConsumerRebalanceListener.java | 3 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/juplo/kafka/AdderRebalanceListener.java b/src/main/java/de/juplo/kafka/AdderRebalanceListener.java index 284aff5..7526929 100644 --- a/src/main/java/de/juplo/kafka/AdderRebalanceListener.java +++ b/src/main/java/de/juplo/kafka/AdderRebalanceListener.java @@ -24,6 +24,7 @@ public class AdderRebalanceListener implements PollIntervalAwareConsumerRebalanc private final Consumer consumer; private Instant lastCommit = Instant.EPOCH; + private boolean commitsEnabled = true; @Override public void onPartitionsAssigned(Collection partitions) @@ -59,7 +60,14 @@ public class AdderRebalanceListener implements PollIntervalAwareConsumerRebalanc id, partition, newOffset); - repository.save(new StateDocument(partition, handler.removePartition(partition), newOffset)); + if (commitsEnabled) + { + repository.save(new StateDocument(partition, handler.removePartition(partition), newOffset)); + } + else + { + log.info("Offset commits are disabled! Last commit: {}", lastCommit); + } }); } @@ -67,6 +75,12 @@ public class AdderRebalanceListener implements PollIntervalAwareConsumerRebalanc @Override public void beforeNextPoll() { + if (!commitsEnabled) + { + log.info("Offset commits are disabled! Last commit: {}", lastCommit); + return; + } + if (lastCommit.plus(commitInterval).isBefore(clock.instant())) { log.debug("Storing data and offsets, last commit: {}", lastCommit); @@ -78,4 +92,16 @@ public class AdderRebalanceListener implements PollIntervalAwareConsumerRebalanc lastCommit = clock.instant(); } } + + @Override + public void enableCommits() + { + commitsEnabled = true; + } + + @Override + public void disableCommits() + { + commitsEnabled = false; + } } diff --git a/src/main/java/de/juplo/kafka/EndlessConsumer.java b/src/main/java/de/juplo/kafka/EndlessConsumer.java index 58557f2..cfba6df 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -42,6 +42,7 @@ public class EndlessConsumer implements Runnable try { log.info("{} - Subscribing to topic {}", id, topic); + pollIntervalAwareRebalanceListener.enableCommits(); consumer.subscribe(Arrays.asList(topic), pollIntervalAwareRebalanceListener); while (true) @@ -91,7 +92,8 @@ public class EndlessConsumer implements Runnable } catch(Exception e) { - log.error("{} - Unexpected error: {}", id, e.toString(), e); + log.error("{} - Unexpected error: {}, disabling commits", id, e.toString(), e); + pollIntervalAwareRebalanceListener.disableCommits(); shutdown(e); } finally diff --git a/src/main/java/de/juplo/kafka/PollIntervalAwareConsumerRebalanceListener.java b/src/main/java/de/juplo/kafka/PollIntervalAwareConsumerRebalanceListener.java index 8abec12..c59418c 100644 --- a/src/main/java/de/juplo/kafka/PollIntervalAwareConsumerRebalanceListener.java +++ b/src/main/java/de/juplo/kafka/PollIntervalAwareConsumerRebalanceListener.java @@ -6,4 +6,7 @@ import org.apache.kafka.clients.consumer.ConsumerRebalanceListener; public interface PollIntervalAwareConsumerRebalanceListener extends ConsumerRebalanceListener { default void beforeNextPoll() {} + + default void enableCommits() {} + default void disableCommits() {} } -- 2.20.1