Verbesserte Tests und Korrekturen gemerged: stored-offsets -> stored-state stored-state
authorKai Moritz <kai@juplo.de>
Sun, 14 Aug 2022 19:23:49 +0000 (21:23 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 14 Aug 2022 19:23:49 +0000 (21:23 +0200)
README.sh
docker-compose.yml
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ApplicationRebalanceListener.java
src/main/java/de/juplo/kafka/EndlessConsumer.java
src/main/java/de/juplo/kafka/PollIntervalAwareConsumerRebalanceListener.java
src/main/java/de/juplo/kafka/StateDocument.java
src/test/java/de/juplo/kafka/ApplicationIT.java
src/test/java/de/juplo/kafka/GenericApplicationTests.java

index 133af42..8bc97f7 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -25,17 +25,65 @@ fi
 
 echo "Waiting for the Kafka-Cluster to become ready..."
 docker-compose exec cli cub kafka-ready -b kafka:9092 1 60 > /dev/null 2>&1 || exit 1
-docker-compose up setup
-docker-compose up -d producer peter beate
+docker-compose up -d kafka-ui
 
-sleep 15
+docker-compose exec -T cli bash << 'EOF'
+echo "Creating topic with 3 partitions..."
+kafka-topics --bootstrap-server kafka:9092 --delete --if-exists --topic test
+# tag::createtopic[]
+kafka-topics --bootstrap-server kafka:9092 --create --topic test --partitions 3
+# end::createtopic[]
+kafka-topics --bootstrap-server kafka:9092 --describe --topic test
+EOF
 
-http -v post :8082/stop
+docker-compose up -d consumer
+
+docker-compose up -d producer
 sleep 10
-docker-compose kill -s 9 peter
-http -v post :8082/start
-sleep 60
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+
+docker-compose stop producer
+docker-compose exec -T cli bash << 'EOF'
+echo "Altering number of partitions from 3 to 7..."
+# tag::altertopic[]
+kafka-topics --bootstrap-server kafka:9092 --alter --topic test --partitions 7
+kafka-topics --bootstrap-server kafka:9092 --describe --topic test
+# end::altertopic[]
+EOF
 
-docker-compose stop producer peter beate
-docker-compose logs beate
-docker-compose logs --tail=10 peter
+docker-compose start producer
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+sleep 1
+http -v :8081/state
+docker-compose stop producer consumer
index 7ab77b2..7bcf68c 100644 (file)
@@ -43,13 +43,13 @@ services:
     depends_on:
       - mongo
 
-  setup:
-    image: juplo/toolbox
-    command: >
-      bash -c "
-        kafka-topics --bootstrap-server kafka:9092 --delete --if-exists --topic test
-        kafka-topics --bootstrap-server kafka:9092 --create --topic test --partitions 2
-      "
+  kafka-ui:
+    image: provectuslabs/kafka-ui:0.3.3
+    ports:
+      - 8080:8080
+    environment:
+      KAFKA_CLUSTERS_0_NAME: local
+      KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
 
   cli:
     image: juplo/toolbox
@@ -63,30 +63,16 @@ services:
       server.port: 8080
       producer.bootstrap-server: kafka:9092
       producer.client-id: producer
-      producer.topic: test
-      producer.throttle-ms: 500
+      producer.throttle-ms: 10
 
 
-  peter:
+  consumer:
     image: juplo/endless-consumer:1.0-SNAPSHOT
     ports:
       - 8081:8080
     environment:
       server.port: 8080
       consumer.bootstrap-server: kafka:9092
-      consumer.client-id: peter
-      consumer.topic: test
-      spring.data.mongodb.uri: mongodb://juplo:training@mongo:27017
-      spring.data.mongodb.database: juplo
-
-  beate:
-    image: juplo/endless-consumer:1.0-SNAPSHOT
-    ports:
-      - 8082:8080
-    environment:
-      server.port: 8080
-      consumer.bootstrap-server: kafka:9092
-      consumer.client-id: beate
-      consumer.topic: test
+      consumer.client-id: consumer
       spring.data.mongodb.uri: mongodb://juplo:training@mongo:27017
       spring.data.mongodb.database: juplo
index a9d9b15..0743fd8 100644 (file)
@@ -1,6 +1,5 @@
 package de.juplo.kafka;
 
-import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.clients.consumer.KafkaConsumer;
 import org.apache.kafka.common.serialization.LongDeserializer;
 import org.apache.kafka.common.serialization.StringDeserializer;
@@ -28,17 +27,14 @@ public class ApplicationConfiguration
   public ApplicationRebalanceListener rebalanceListener(
       ApplicationRecordHandler recordHandler,
       StateRepository stateRepository,
-      Consumer<String, Long> consumer,
       ApplicationProperties properties)
   {
     return new ApplicationRebalanceListener(
         recordHandler,
         stateRepository,
         properties.getClientId(),
-        properties.getTopic(),
         Clock.systemDefaultZone(),
-        properties.getCommitInterval(),
-        consumer);
+        properties.getCommitInterval());
   }
 
   @Bean
@@ -74,7 +70,6 @@ public class ApplicationConfiguration
     props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor");
     props.put("group.id", properties.getGroupId());
     props.put("client.id", properties.getClientId());
-    props.put("enable.auto.commit", false);
     props.put("auto.offset.reset", properties.getAutoOffsetReset());
     props.put("auto.commit.interval.ms", (int)properties.getCommitInterval().toMillis());
     props.put("metadata.max.age.ms", "1000");
index 444b7b7..247b6f7 100644 (file)
@@ -2,7 +2,6 @@ package de.juplo.kafka;
 
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.Consumer;
 import org.apache.kafka.common.TopicPartition;
 
 import java.time.Clock;
@@ -19,13 +18,10 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
   private final ApplicationRecordHandler recordHandler;
   private final StateRepository stateRepository;
   private final String id;
-  private final String topic;
   private final Clock clock;
   private final Duration commitInterval;
-  private final Consumer<String, Long> consumer;
 
   private Instant lastCommit = Instant.EPOCH;
-  private boolean commitsEnabled = true;
 
   @Override
   public void onPartitionsAssigned(Collection<TopicPartition> partitions)
@@ -33,17 +29,11 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
     partitions.forEach(tp ->
     {
       Integer partition = tp.partition();
+      log.info("{} - adding partition: {}", id, partition);
       StateDocument document =
           stateRepository
               .findById(Integer.toString(partition))
               .orElse(new StateDocument(partition));
-      log.info("{} - adding partition: {}, offset={}", id, partition, document.offset);
-      if (document.offset >= 0)
-      {
-        // Only seek, if a stored offset was found
-        // Otherwise: Use initial offset, generated by Kafka
-        consumer.seek(tp, document.offset);
-      }
       recordHandler.addPartition(partition, document.state);
     });
   }
@@ -54,21 +44,18 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
     partitions.forEach(tp ->
     {
       Integer partition = tp.partition();
-      Long offset = consumer.position(tp);
-      log.info(
-          "{} - removing partition: {}, offset of next message {})",
-          id,
-          partition,
-          offset);
-      if (commitsEnabled)
-      {
-        Map<String, Long> removed = recordHandler.removePartition(partition);
-        stateRepository.save(new StateDocument(partition, removed, offset));
-      }
-      else
+      log.info("{} - removing partition: {}", id, partition);
+      Map<String, Long> removed = recordHandler.removePartition(partition);
+      for (String key : removed.keySet())
       {
-        log.info("Offset commits are disabled! Last commit: {}", lastCommit);
+        log.info(
+            "{} - Seen {} messages for partition={}|key={}",
+            id,
+            removed.get(key),
+            partition,
+            key);
       }
+      stateRepository.save(new StateDocument(partition, removed));
     });
   }
 
@@ -76,33 +63,14 @@ public class ApplicationRebalanceListener implements PollIntervalAwareConsumerRe
   @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);
-      recordHandler.getState().forEach((partiton, state) -> stateRepository.save(
+      log.debug("Storing data, last commit: {}", lastCommit);
+      recordHandler.getState().forEach((partiton, statistics) -> stateRepository.save(
           new StateDocument(
               partiton,
-              state,
-              consumer.position(new TopicPartition(topic, partiton)))));
+              statistics)));
       lastCommit = clock.instant();
     }
   }
-
-  @Override
-  public void enableCommits()
-  {
-    commitsEnabled = true;
-  }
-
-  @Override
-  public void disableCommits()
-  {
-    commitsEnabled = false;
-  }
 }
index 58374f4..0238521 100644 (file)
@@ -42,7 +42,6 @@ public class EndlessConsumer<K, V> implements Runnable
     try
     {
       log.info("{} - Subscribing to topic {}", id, topic);
-      rebalanceListener.enableCommits();
       consumer.subscribe(Arrays.asList(topic), rebalanceListener);
 
       while (true)
@@ -75,6 +74,7 @@ public class EndlessConsumer<K, V> implements Runnable
     catch(WakeupException e)
     {
       log.info("{} - RIIING! Request to stop consumption - commiting current offsets!", id);
+      consumer.commitSync();
       shutdown();
     }
     catch(RecordDeserializationException e)
@@ -88,12 +88,12 @@ public class EndlessConsumer<K, V> implements Runnable
           offset,
           e.getCause().toString());
 
+      consumer.commitSync();
       shutdown(e);
     }
     catch(Exception e)
     {
-      log.error("{} - Unexpected error: {}, disabling commits", id, e.toString(), e);
-      rebalanceListener.disableCommits();
+      log.error("{} - Unexpected error: {}", id, e.toString(), e);
       shutdown(e);
     }
     finally
index c59418c..8abec12 100644 (file)
@@ -6,7 +6,4 @@ import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
 public interface PollIntervalAwareConsumerRebalanceListener extends ConsumerRebalanceListener
 {
   default void beforeNextPoll() {}
-
-  default void enableCommits() {}
-  default void disableCommits() {}
 }
index bb1c701..b37b8a9 100644 (file)
@@ -14,7 +14,6 @@ public class StateDocument
 {
   @Id
   public String id;
-  public long offset = -1l;
   public Map<String, Long> state;
 
   public StateDocument()
@@ -27,10 +26,9 @@ public class StateDocument
     this.state = new HashMap<>();
   }
 
-  public StateDocument(Integer partition, Map<String, Long> state, long offset)
+  public StateDocument(Integer partition, Map<String, Long> state)
   {
     this.id = Integer.toString(partition);
     this.state = state;
-    this.offset = offset;
   }
 }
index cded0ee..d1d8e50 100644 (file)
@@ -32,7 +32,7 @@ public class ApplicationIT
 
 
   @Test
-  public void testApplicationStartup()
+  public void   testApplicationStartup()
   {
     restTemplate.getForObject(
         "http://localhost:" + port + "/actuator/health",
index fa3d911..a64ebd0 100644 (file)
@@ -60,8 +60,6 @@ abstract class GenericApplicationTests<K, V>
        @Autowired
        ExecutorService executor;
        @Autowired
-       StateRepository stateRepository;
-       @Autowired
        PollIntervalAwareConsumerRebalanceListener rebalanceListener;
        @Autowired
        RecordHandler<K, V> recordHandler;
@@ -227,29 +225,23 @@ abstract class GenericApplicationTests<K, V>
        void seekToEnd()
        {
                offsetConsumer.assign(partitions());
+               offsetConsumer.seekToEnd(partitions());
                partitions().forEach(tp ->
                {
+                       // seekToEnd() works lazily: it only takes effect on poll()/position()
                        Long offset = offsetConsumer.position(tp);
                        log.info("New position for {}: {}", tp, offset);
-                       Integer partition = tp.partition();
-                       StateDocument document =
-                                       stateRepository
-                                                       .findById(partition.toString())
-                                                       .orElse(new StateDocument(partition));
-                       document.offset = offset;
-                       stateRepository.save(document);
                });
+               // The new positions must be commited!
+               offsetConsumer.commitSync();
                offsetConsumer.unsubscribe();
        }
 
        void doForCurrentOffsets(BiConsumer<TopicPartition, Long> consumer)
        {
-               partitions().forEach(tp ->
-               {
-                       String partition = Integer.toString(tp.partition());
-                       Optional<Long> offset = stateRepository.findById(partition).map(document -> document.offset);
-                       consumer.accept(tp, offset.orElse(0l));
-               });
+               offsetConsumer.assign(partitions());
+               partitions().forEach(tp -> consumer.accept(tp, offsetConsumer.position(tp)));
+               offsetConsumer.unsubscribe();
        }
 
        List<TopicPartition> partitions()