Merge branch 'rebalance-listener' into stored-state
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
index c7bc852..7cb77aa 100644 (file)
@@ -22,6 +22,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 public class EndlessConsumer implements Runnable
 {
   private final ExecutorService executor;
+  private final PartitionStatisticsRepository repository;
   private final String bootstrapServer;
   private final String groupId;
   private final String id;
@@ -33,11 +34,12 @@ public class EndlessConsumer implements Runnable
   private KafkaConsumer<String, String> consumer = null;
   private Future<?> future = null;
 
-  private final Map<TopicPartition, PartitionStatistics> seen = new HashMap<>();
+  private final Map<Integer, Map<String, Integer>> seen = new HashMap<>();
 
 
   public EndlessConsumer(
       ExecutorService executor,
+      PartitionStatisticsRepository repository,
       String bootstrapServer,
       String groupId,
       String clientId,
@@ -45,6 +47,7 @@ public class EndlessConsumer implements Runnable
       String autoOffsetReset)
   {
     this.executor = executor;
+    this.repository = repository;
     this.bootstrapServer = bootstrapServer;
     this.groupId = groupId;
     this.id = clientId;
@@ -77,16 +80,17 @@ public class EndlessConsumer implements Runnable
           partitions.forEach(tp ->
           {
             log.info("{} - removing partition: {}", id, tp);
-            PartitionStatistics removed = seen.remove(tp);
-            for (KeyCounter counter : removed.getStatistics())
+            Map<String, Integer> removed = seen.remove(tp.partition());
+            for (String key : removed.keySet())
             {
               log.info(
                   "{} - Seen {} messages for partition={}|key={}",
                   id,
-                  counter.getResult(),
-                  removed.getPartition(),
-                  counter.getKey());
+                  removed.get(key),
+                  tp.partition(),
+                  key);
             }
+            repository.save(new StatisticsDocument(tp.partition(), removed));
           });
         }
 
@@ -96,7 +100,12 @@ public class EndlessConsumer implements Runnable
           partitions.forEach(tp ->
           {
             log.info("{} - adding partition: {}", id, tp);
-            seen.put(tp, new PartitionStatistics(tp));
+            seen.put(
+                tp.partition(),
+                repository
+                    .findById(Integer.toString(tp.partition()))
+                    .map(document -> document.statistics)
+                    .orElse(new HashMap<>()));
           });
         }
       });
@@ -121,9 +130,16 @@ public class EndlessConsumer implements Runnable
               record.value()
           );
 
-          TopicPartition partition = new TopicPartition(record.topic(), record.partition());
+          Integer partition = record.partition();
           String key = record.key() == null ? "NULL" : record.key();
-          seen.get(partition).increment(key);
+          Map<String, Integer> byKey = seen.get(partition);
+
+          if (!byKey.containsKey(key))
+            byKey.put(key, 0);
+
+          int seenByKey = byKey.get(key);
+          seenByKey++;
+          byKey.put(key, seenByKey);
         }
       }
     }
@@ -144,7 +160,7 @@ public class EndlessConsumer implements Runnable
     }
   }
 
-  public Map<TopicPartition, PartitionStatistics> getSeen()
+  public Map<Integer, Map<String, Integer>> getSeen()
   {
     return seen;
   }