Merge branch 'stored-state' into stored-offsets
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
1 package de.juplo.kafka;
2
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
5 import org.apache.kafka.clients.consumer.ConsumerRecord;
6 import org.apache.kafka.clients.consumer.ConsumerRecords;
7 import org.apache.kafka.clients.consumer.KafkaConsumer;
8 import org.apache.kafka.common.TopicPartition;
9 import org.apache.kafka.common.errors.WakeupException;
10 import org.apache.kafka.common.serialization.StringDeserializer;
11
12 import javax.annotation.PreDestroy;
13 import java.time.Duration;
14 import java.util.*;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.atomic.AtomicBoolean;
19
20
21 @Slf4j
22 public class EndlessConsumer implements Runnable
23 {
24   private final ExecutorService executor;
25   private final PartitionStatisticsRepository repository;
26   private final String bootstrapServer;
27   private final String groupId;
28   private final String id;
29   private final String topic;
30   private final String autoOffsetReset;
31
32   private AtomicBoolean running = new AtomicBoolean();
33   private long consumed = 0;
34   private KafkaConsumer<String, String> consumer = null;
35   private Future<?> future = null;
36
37   private final Map<Integer, Map<String, Integer>> seen = new HashMap<>();
38
39
40   public EndlessConsumer(
41       ExecutorService executor,
42       PartitionStatisticsRepository repository,
43       String bootstrapServer,
44       String groupId,
45       String clientId,
46       String topic,
47       String autoOffsetReset)
48   {
49     this.executor = executor;
50     this.repository = repository;
51     this.bootstrapServer = bootstrapServer;
52     this.groupId = groupId;
53     this.id = clientId;
54     this.topic = topic;
55     this.autoOffsetReset = autoOffsetReset;
56   }
57
58   @Override
59   public void run()
60   {
61     try
62     {
63       Properties props = new Properties();
64       props.put("bootstrap.servers", bootstrapServer);
65       props.put("group.id", groupId);
66       props.put("client.id", id);
67       props.put("enable.auto.commit", false);
68       props.put("auto.offset.reset", autoOffsetReset);
69       props.put("metadata.max.age.ms", "1000");
70       props.put("key.deserializer", StringDeserializer.class.getName());
71       props.put("value.deserializer", StringDeserializer.class.getName());
72
73       this.consumer = new KafkaConsumer<>(props);
74
75       log.info("{} - Subscribing to topic {}", id, topic);
76       consumer.subscribe(Arrays.asList(topic), new ConsumerRebalanceListener()
77       {
78         @Override
79         public void onPartitionsRevoked(Collection<TopicPartition> partitions)
80         {
81           partitions.forEach(tp ->
82           {
83             log.info("{} - removing partition: {}", id, tp);
84             Map<String, Integer> removed = seen.remove(tp.partition());
85             for (String key : removed.keySet())
86             {
87               log.info(
88                   "{} - Seen {} messages for partition={}|key={}",
89                   id,
90                   removed.get(key),
91                   tp.partition(),
92                   key);
93             }
94             repository.save(new StatisticsDocument(tp.partition(), removed, consumer.position(tp)));
95           });
96         }
97
98         @Override
99         public void onPartitionsAssigned(Collection<TopicPartition> partitions)
100         {
101           partitions.forEach(tp ->
102           {
103             log.info("{} - adding partition: {}", id, tp);
104             StatisticsDocument document =
105                 repository
106                     .findById(Integer.toString(tp.partition()))
107                     .orElse(new StatisticsDocument(tp.partition()));
108             consumer.seek(tp, document.offset);
109             seen.put(tp.partition(), document.statistics);
110           });
111         }
112       });
113
114       while (true)
115       {
116         ConsumerRecords<String, String> records =
117             consumer.poll(Duration.ofSeconds(1));
118
119         // Do something with the data...
120         log.info("{} - Received {} messages", id, records.count());
121         for (ConsumerRecord<String, String> record : records)
122         {
123           consumed++;
124           log.info(
125               "{} - {}: {}/{} - {}={}",
126               id,
127               record.offset(),
128               record.topic(),
129               record.partition(),
130               record.key(),
131               record.value()
132           );
133
134           Integer partition = record.partition();
135           String key = record.key() == null ? "NULL" : record.key();
136           Map<String, Integer> byKey = seen.get(partition);
137
138           if (!byKey.containsKey(key))
139             byKey.put(key, 0);
140
141           int seenByKey = byKey.get(key);
142           seenByKey++;
143           byKey.put(key, seenByKey);
144         }
145
146         seen.forEach((partiton, statistics) -> repository.save(
147             new StatisticsDocument(
148                 partiton,
149                 statistics,
150                 consumer.position(new TopicPartition(topic, partiton)))));
151       }
152     }
153     catch(WakeupException e)
154     {
155       log.info("{} - RIIING!", id);
156     }
157     catch(Exception e)
158     {
159       log.error("{} - Unexpected error: {}", id, e.toString(), e);
160       running.set(false); // Mark the instance as not running
161     }
162     finally
163     {
164       log.info("{} - Closing the KafkaConsumer", id);
165       consumer.close();
166       log.info("{} - Consumer-Thread exiting", id);
167     }
168   }
169
170   public Map<Integer, Map<String, Integer>> getSeen()
171   {
172     return seen;
173   }
174
175   public synchronized void start()
176   {
177     boolean stateChanged = running.compareAndSet(false, true);
178     if (!stateChanged)
179       throw new RuntimeException("Consumer instance " + id + " is already running!");
180
181     log.info("{} - Starting - consumed {} messages before", id, consumed);
182     future = executor.submit(this);
183   }
184
185   public synchronized void stop() throws ExecutionException, InterruptedException
186   {
187     boolean stateChanged = running.compareAndSet(true, false);
188     if (!stateChanged)
189       throw new RuntimeException("Consumer instance " + id + " is not running!");
190
191     log.info("{} - Stopping", id);
192     consumer.wakeup();
193     future.get();
194     log.info("{} - Stopped - consumed {} messages so far", id, consumed);
195   }
196
197   @PreDestroy
198   public void destroy() throws ExecutionException, InterruptedException
199   {
200     log.info("{} - Destroy!", id);
201     try
202     {
203       stop();
204     }
205     catch (IllegalStateException e)
206     {
207       log.info("{} - Was already stopped", id);
208     }
209     finally
210     {
211       log.info("{}: Consumed {} messages in total, exiting!", id, consumed);
212     }
213   }
214 }