WAS:TMP:IS?FIX:WIP:test: `*ConfigurationIT` asserts, if restored messages can be...
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / implementation / kafka / DataChannel.java
1 package de.juplo.kafka.chat.backend.implementation.kafka;
2
3 import de.juplo.kafka.chat.backend.domain.ChatRoomData;
4 import de.juplo.kafka.chat.backend.domain.ChatRoomInfo;
5 import de.juplo.kafka.chat.backend.domain.Message;
6 import de.juplo.kafka.chat.backend.domain.ShardingPublisherStrategy;
7 import de.juplo.kafka.chat.backend.domain.exceptions.ShardNotOwnedException;
8 import de.juplo.kafka.chat.backend.implementation.kafka.messages.AbstractMessageTo;
9 import de.juplo.kafka.chat.backend.implementation.kafka.messages.data.EventChatMessageReceivedTo;
10 import lombok.Getter;
11 import lombok.ToString;
12 import lombok.extern.slf4j.Slf4j;
13 import org.apache.kafka.clients.consumer.*;
14 import org.apache.kafka.clients.producer.Producer;
15 import org.apache.kafka.clients.producer.ProducerRecord;
16 import org.apache.kafka.common.TopicPartition;
17 import org.apache.kafka.common.errors.WakeupException;
18 import reactor.core.publisher.Mono;
19
20 import java.time.*;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.UUID;
25 import java.util.stream.IntStream;
26
27
28 @ToString(of = { "topic", "instanceId" })
29 @Slf4j
30 public class DataChannel implements Channel, ConsumerRebalanceListener
31 {
32   private final String instanceId;
33   private final String topic;
34   private final Producer<String, AbstractMessageTo> producer;
35   private final Consumer<String, AbstractMessageTo> consumer;
36   private final ZoneId zoneId;
37   private final int numShards;
38   private final Duration pollingInterval;
39   private final int historyLimit;
40   private final Clock clock;
41   private final boolean[] isShardOwned;
42   private final long[] currentOffset;
43   private final long[] nextOffset;
44   private final Map<UUID, ChatRoomData>[] chatRoomData;
45   private final ChannelMediator channelMediator;
46   private final ShardingPublisherStrategy shardingPublisherStrategy;
47
48   private boolean running;
49   @Getter
50   private volatile ChannelState channelState = ChannelState.STARTING;
51
52
53   public DataChannel(
54     String instanceId,
55     String topic,
56     Producer<String, AbstractMessageTo> producer,
57     Consumer<String, AbstractMessageTo> dataChannelConsumer,
58     ZoneId zoneId,
59     int numShards,
60     Duration pollingInterval,
61     int historyLimit,
62     Clock clock,
63     ChannelMediator channelMediator,
64     ShardingPublisherStrategy shardingPublisherStrategy)
65   {
66     log.debug(
67         "{}: Creating DataChannel for topic {} with {} partitions",
68         instanceId,
69         topic,
70         numShards);
71     this.instanceId = instanceId;
72     this.topic = topic;
73     this.consumer = dataChannelConsumer;
74     this.producer = producer;
75     this.zoneId = zoneId;
76     this.numShards = numShards;
77     this.pollingInterval = pollingInterval;
78     this.historyLimit = historyLimit;
79     this.clock = clock;
80     this.isShardOwned = new boolean[numShards];
81     this.currentOffset = new long[numShards];
82     this.nextOffset = new long[numShards];
83     this.chatRoomData = new Map[numShards];
84     IntStream
85         .range(0, numShards)
86         .forEach(shard -> this.chatRoomData[shard] = new HashMap<>());
87     this.channelMediator = channelMediator;
88     this.shardingPublisherStrategy = shardingPublisherStrategy;
89   }
90
91
92
93   Mono<Message> sendChatMessage(
94       UUID chatRoomId,
95       Message.MessageKey key,
96       LocalDateTime timestamp,
97       String text)
98   {
99     ZonedDateTime zdt = ZonedDateTime.of(timestamp, zoneId);
100     return Mono.create(sink ->
101     {
102       ProducerRecord<String, AbstractMessageTo> record =
103           new ProducerRecord<>(
104               topic,
105               null,
106               zdt.toEpochSecond(),
107               chatRoomId.toString(),
108               EventChatMessageReceivedTo.of(key.getUsername(), key.getMessageId(), text));
109
110       producer.send(record, ((metadata, exception) ->
111       {
112         if (exception == null)
113         {
114           // On successful send
115           Message message = new Message(key, metadata.offset(), timestamp, text);
116           log.info("Successfully send message {}", message);
117           sink.success(message);
118         }
119         else
120         {
121           // On send-failure
122           log.error(
123               "Could not send message for chat-room={}, key={}, timestamp={}, text={}: {}",
124               chatRoomId,
125               key,
126               timestamp,
127               text,
128               exception);
129           sink.error(exception);
130         }
131       }));
132     });
133   }
134
135   @Override
136   public void onPartitionsAssigned(Collection<TopicPartition> partitions)
137   {
138     log.info("Newly assigned partitions! Pausing normal operations...");
139     channelState = ChannelState.LOAD_IN_PROGRESS;
140
141     consumer.endOffsets(partitions).forEach((topicPartition, currentOffset) ->
142     {
143       int partition = topicPartition.partition();
144       isShardOwned[partition] =  true;
145       this.currentOffset[partition] = currentOffset;
146
147       log.info(
148           "Partition assigned: {} - loading messages: next={} -> current={}",
149           partition,
150           nextOffset[partition],
151           currentOffset);
152
153       consumer.seek(topicPartition, nextOffset[partition]);
154       channelMediator.shardAssigned(partition);
155       shardingPublisherStrategy
156           .publishOwnership(partition)
157           .doOnSuccess(instanceId -> log.info(
158               "Successfully published instance {} as owner of shard {}",
159               instanceId,
160               partition))
161           .doOnError(throwable -> log.error(
162               "Could not publish instance {} as owner of shard {}: {}",
163               instanceId,
164               partition,
165               throwable.toString()))
166           .onErrorComplete()
167           .block();
168     });
169
170     consumer.resume(partitions);
171   }
172
173   @Override
174   public void onPartitionsRevoked(Collection<TopicPartition> partitions)
175   {
176     partitions.forEach(topicPartition ->
177     {
178       int partition = topicPartition.partition();
179       chatRoomData[partition]
180           .values()
181           .forEach(chatRoomData -> chatRoomData.close());
182       isShardOwned[partition] = false;
183       nextOffset[partition] = consumer.position(topicPartition);
184       log.info("Partition revoked: {} - next={}", partition, nextOffset[partition]);
185       channelMediator.shardRevoked(partition);
186     });
187   }
188
189   @Override
190   public void onPartitionsLost(Collection<TopicPartition> partitions)
191   {
192     log.warn("Lost partitions: {}, partitions");
193     // TODO: Muss auf den Verlust anders reagiert werden?
194     onPartitionsRevoked(partitions);
195   }
196
197   @Override
198   public void run()
199   {
200     running = true;
201
202     while (running)
203     {
204       try
205       {
206         ConsumerRecords<String, AbstractMessageTo> records = consumer.poll(pollingInterval);
207         log.info("Fetched {} messages", records.count());
208
209         switch (channelState)
210         {
211           case LOAD_IN_PROGRESS ->
212           {
213             loadChatRoomData(records);
214
215             if (isLoadingCompleted())
216             {
217               log.info("Loading of messages completed! Pausing all owned partitions...");
218               pauseAllOwnedPartions();
219               log.info("Resuming normal operations...");
220               channelState = ChannelState.READY;
221             }
222           }
223           case SHUTTING_DOWN -> log.info("Shutdown in progress: ignoring {} fetched messages.", records.count());
224           default ->
225           {
226             if (!records.isEmpty())
227             {
228               throw new IllegalStateException("All owned partitions should be paused, when in state " + channelState);
229             }
230           }
231         }
232       }
233       catch (WakeupException e)
234       {
235         log.info("Received WakeupException, exiting!");
236         channelState = ChannelState.SHUTTING_DOWN;
237         running = false;
238       }
239     }
240
241     log.info("Exiting normally");
242   }
243
244   private void loadChatRoomData(ConsumerRecords<String, AbstractMessageTo> records)
245   {
246     for (ConsumerRecord<String, AbstractMessageTo> record : records)
247     {
248       UUID chatRoomId = UUID.fromString(record.key());
249
250       switch (record.value().getType())
251       {
252         case EVENT_CHATMESSAGE_RECEIVED:
253           Instant instant = Instant.ofEpochSecond(record.timestamp());
254           LocalDateTime timestamp = LocalDateTime.ofInstant(instant, zoneId);
255           loadChatMessage(
256               chatRoomId,
257               timestamp,
258               record.offset(),
259               (EventChatMessageReceivedTo) record.value(),
260               record.partition());
261           break;
262
263         default:
264           log.debug(
265               "Ignoring message for chat-room {} with offset {}: {}",
266               chatRoomId,
267               record.offset(),
268               record.value());
269       }
270
271       nextOffset[record.partition()] = record.offset() + 1;
272     }
273   }
274
275   private void loadChatMessage(
276       UUID chatRoomId,
277       LocalDateTime timestamp,
278       long offset,
279       EventChatMessageReceivedTo chatMessageTo,
280       int partition)
281   {
282     Message.MessageKey key = Message.MessageKey.of(chatMessageTo.getUser(), chatMessageTo.getId());
283     Message message = new Message(key, offset, timestamp, chatMessageTo.getText());
284
285     ChatRoomData chatRoomData = computeChatRoomData(chatRoomId, partition);
286     KafkaChatMessageService kafkaChatRoomService =
287         (KafkaChatMessageService) chatRoomData.getChatRoomService();
288
289     log.debug(
290         "Loaded message from partition={} at offset={}: {}",
291         partition,
292         offset,
293         message);
294     kafkaChatRoomService.persistMessage(message);
295   }
296
297   private boolean isLoadingCompleted()
298   {
299     return IntStream
300         .range(0, numShards)
301         .filter(shard -> isShardOwned[shard])
302         .allMatch(shard ->
303         {
304           TopicPartition partition = new TopicPartition(topic, shard);
305           long position = consumer.position(partition);
306           return position >= currentOffset[shard];
307         });
308   }
309
310   private void pauseAllOwnedPartions()
311   {
312     consumer.pause(IntStream
313         .range(0, numShards)
314         .filter(shard -> isShardOwned[shard])
315         .mapToObj(shard -> new TopicPartition(topic, shard))
316         .toList());
317   }
318
319
320   int[] getOwnedShards()
321   {
322     return IntStream
323         .range(0, numShards)
324         .filter(shard -> isShardOwned[shard])
325         .toArray();
326   }
327
328   void createChatRoomData(ChatRoomInfo chatRoomInfo)
329   {
330     computeChatRoomData(chatRoomInfo.getId(), chatRoomInfo.getShard());
331   }
332
333   Mono<ChatRoomData> getChatRoomData(int shard, UUID id)
334   {
335     ChannelState capturedState = channelState;
336     if (capturedState != ChannelState.READY)
337     {
338       return Mono.error(new ChannelNotReadyException(capturedState));
339     }
340
341     if (!isShardOwned[shard])
342     {
343       return Mono.error(new ShardNotOwnedException(instanceId, shard));
344     }
345
346     return Mono.justOrEmpty(chatRoomData[shard].get(id));
347   }
348
349   private ChatRoomData computeChatRoomData(UUID chatRoomId, int shard)
350   {
351     ChatRoomData chatRoomData = this.chatRoomData[shard].get(chatRoomId);
352
353     if (chatRoomData != null)
354     {
355       log.info(
356           "Ignoring request to create already existing ChatRoomData for {}",
357           chatRoomId);
358     }
359     else
360     {
361       log.info("Creating ChatRoomData {} with history-limit {}", chatRoomId, historyLimit);
362       KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
363       chatRoomData = new ChatRoomData(clock, service, historyLimit);
364       this.chatRoomData[shard].put(chatRoomId, chatRoomData);
365     }
366
367     return chatRoomData;
368   }
369
370   ConsumerGroupMetadata getConsumerGroupMetadata()
371   {
372     return consumer.groupMetadata();
373   }
374 }