Fixed bug on first start-up (no current offsets)
[demos/kafka/outbox] / delivery / src / main / java / de / juplo / kafka / outbox / delivery / OutboxProducer.java
1 package de.juplo.kafka.outbox.delivery;
2
3 import com.google.common.primitives.Longs;
4 import org.apache.kafka.clients.consumer.ConsumerRecords;
5 import org.apache.kafka.clients.consumer.KafkaConsumer;
6 import org.apache.kafka.clients.consumer.OffsetAndMetadata;
7 import org.apache.kafka.common.PartitionInfo;
8 import org.apache.kafka.common.TopicPartition;
9 import org.apache.kafka.common.serialization.StringDeserializer;
10 import org.apache.kafka.common.serialization.StringSerializer;
11
12 import java.time.Clock;
13 import java.time.Duration;
14 import java.time.LocalTime;
15 import java.util.*;
16
17 import org.apache.kafka.clients.producer.KafkaProducer;
18 import org.apache.kafka.clients.producer.ProducerRecord;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.scheduling.annotation.Scheduled;
22
23 import javax.annotation.PreDestroy;
24
25 import static org.apache.kafka.clients.CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG;
26 import static org.apache.kafka.clients.consumer.ConsumerConfig.*;
27 import static org.apache.kafka.clients.producer.ProducerConfig.*;
28
29
30 public class OutboxProducer
31 {
32   final static Logger LOG = LoggerFactory.getLogger(OutboxProducer.class);
33
34   public final static String HEADER = "#";
35
36   private final OutboxRepository repository;
37   private final KafkaProducer<String, String> producer;
38   private final String topic;
39   private final Set<Long> send = new HashSet<>();
40   private final Clock clock;
41   private final Duration cleanupInterval;
42
43   private long sequenceNumber = 0l;
44   private LocalTime nextCleanup;
45
46   public OutboxProducer(
47       ApplicationProperties properties,
48       OutboxRepository repository,
49       Clock clock)
50   {
51     this.repository = repository;
52
53     Properties props = new Properties();
54     props.put(BOOTSTRAP_SERVERS_CONFIG, properties.bootstrapServers);
55     props.put(KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
56     props.put(VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
57     props.put(ENABLE_IDEMPOTENCE_CONFIG, true);
58
59     this.producer = new KafkaProducer<>(props);
60     this.topic = properties.topic;
61
62     props = new Properties();
63     props.put(BOOTSTRAP_SERVERS_CONFIG, properties.bootstrapServers);
64     props.put(GROUP_ID_CONFIG, "outbox");
65     props.put(AUTO_OFFSET_RESET_CONFIG, "earliest");
66     props.put(KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
67     props.put(VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
68
69     List<PartitionInfo> partitions = consumer.listTopics().get(this.topic);
70     Set<TopicPartition> assignment = new HashSet<>();
71     for (PartitionInfo info : partitions)
72     {
73       LOG.debug("Found {}/{} (ISR: {})", info.topic(), info.partition(), info.inSyncReplicas());
74       assignment.add(new TopicPartition(info.topic(), info.partition()));
75     }
76
77     LOG.info("Using topic {} with {} partitions", topic, partitions);
78
79     KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
80     consumer.assign(assignment);
81
82     long[] currentOffsets = new long[partitions.size()];
83     for (Map.Entry<TopicPartition, OffsetAndMetadata> entry : consumer.committed(assignment).entrySet())
84     {
85       if (entry.getValue() == null)
86       {
87         LOG.debug("Found no offset for partition {}", entry.getKey());
88         currentOffsets[entry.getKey().partition()] = -1l;
89       }
90       else
91       {
92         LOG.debug("Found current offset {} for partition {}", entry.getValue(), entry.getKey());
93         currentOffsets[entry.getKey().partition()] = entry.getValue().offset() - 1l;
94       }
95     }
96     LOG.info("Current offsets: {}", currentOffsets);
97
98     long[] endOffsets = new long[partitions.size()];
99     for (Map.Entry<TopicPartition, Long> entry : consumer.endOffsets(assignment).entrySet())
100     {
101       LOG.debug("Found next offset {} for partition {}", entry.getValue(), entry.getKey());
102       endOffsets[entry.getKey().partition()] = entry.getValue() - 1l;
103     }
104     LOG.info("End-offsets: {}", endOffsets);
105
106     int deleted = 0;
107     while(!Arrays.equals(currentOffsets, endOffsets))
108     {
109       ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
110       LOG.debug("Fetched {} records", records.count());
111       records.forEach(record ->
112       {
113         long recordSequenceNumber = Longs.fromByteArray(record.headers().lastHeader(HEADER).value());
114         LOG.debug(
115             "Found message #{} on offset {} of partition {}",
116             recordSequenceNumber,
117             record.offset(),
118             record.partition());
119         send.add(recordSequenceNumber);
120         currentOffsets[record.partition()] = record.offset();
121       });
122       deleted += cleanUp();
123       LOG.debug("Current offsets: {}", currentOffsets);
124     }
125
126     LOG.info("Cleaned up {} already send entries from outbox table", deleted);
127
128     consumer.close();
129
130     this.clock = clock;
131     this.cleanupInterval = properties.cleanupInterval;
132     this.nextCleanup = LocalTime.now(clock).plus(this.cleanupInterval);
133   }
134
135   @Scheduled(fixedDelayString = "${de.juplo.kafka.outbox.interval}")
136   public void poll()
137   {
138     List<OutboxItem> items;
139     do
140     {
141       items = repository.fetch(sequenceNumber);
142       LOG.debug("Polled {} new items", items.size());
143       for (OutboxItem item : items)
144         send(item);
145       if (nextCleanup.isBefore(LocalTime.now(clock)))
146       {
147         cleanUp();
148         nextCleanup = LocalTime.now(clock).plus(cleanupInterval);
149         LOG.debug("Next clean-up: {}", nextCleanup);
150       }
151     }
152     while (items.size() > 0);
153   }
154
155   int cleanUp()
156   {
157     int deleted = repository.delete(send);
158     LOG.debug("Cleaned up {}/{} entries from outbox", deleted, send.size());
159     send.clear();
160     return deleted;
161   }
162
163   void send(OutboxItem item)
164   {
165     final ProducerRecord<String, String> record =
166         new ProducerRecord<>(topic, item.getKey(), item.getValue());
167
168     sequenceNumber = item.getSequenceNumber();
169     record.headers().add(HEADER, Longs.toByteArray(sequenceNumber));
170
171     producer.send(record, (metadata, e) ->
172     {
173       if (metadata != null)
174       {
175         send.add(item.getSequenceNumber());
176         LOG.info(
177             "{}/{}:{} - {}:{}={}",
178             metadata.topic(),
179             metadata.partition(),
180             metadata.offset(),
181             item.getSequenceNumber(),
182             record.key(),
183             record.value());
184       }
185       else
186       {
187         // HANDLE ERROR
188         LOG.error(
189             "{}/{} - {}:{}={} -> ",
190             record.topic(),
191             record.partition(),
192             item.getSequenceNumber(),
193             record.key(),
194             record.value(),
195             e);
196       }
197     });
198   }
199
200
201   @PreDestroy
202   public void close()
203   {
204     producer.close(Duration.ofSeconds(5));
205   }
206 }