63f36f53d504ec2bb30219e253069f270d13dbb4
[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       isShardOwned[partition] = false;
180       nextOffset[partition] = consumer.position(topicPartition);
181       log.info("Partition revoked: {} - next={}", partition, nextOffset[partition]);
182       channelMediator.shardRevoked(partition);
183     });
184   }
185
186   @Override
187   public void onPartitionsLost(Collection<TopicPartition> partitions)
188   {
189     log.warn("Lost partitions: {}, partitions");
190     // TODO: Muss auf den Verlust anders reagiert werden?
191     onPartitionsRevoked(partitions);
192   }
193
194   @Override
195   public void run()
196   {
197     running = true;
198
199     while (running)
200     {
201       try
202       {
203         ConsumerRecords<String, AbstractMessageTo> records = consumer.poll(pollingInterval);
204         log.info("Fetched {} messages", records.count());
205
206         switch (channelState)
207         {
208           case LOAD_IN_PROGRESS ->
209           {
210             loadChatRoomData(records);
211
212             if (isLoadingCompleted())
213             {
214               log.info("Loading of messages completed! Pausing all owned partitions...");
215               pauseAllOwnedPartions();
216               log.info("Resuming normal operations...");
217               channelState = ChannelState.READY;
218             }
219           }
220           case SHUTTING_DOWN -> log.info("Shutdown in progress: ignoring {} fetched messages.", records.count());
221           default ->
222           {
223             if (!records.isEmpty())
224             {
225               throw new IllegalStateException("All owned partitions should be paused, when in state " + channelState);
226             }
227           }
228         }
229       }
230       catch (WakeupException e)
231       {
232         log.info("Received WakeupException, exiting!");
233         channelState = ChannelState.SHUTTING_DOWN;
234         running = false;
235       }
236     }
237
238     log.info("Exiting normally");
239   }
240
241   private void loadChatRoomData(ConsumerRecords<String, AbstractMessageTo> records)
242   {
243     for (ConsumerRecord<String, AbstractMessageTo> record : records)
244     {
245       UUID chatRoomId = UUID.fromString(record.key());
246
247       switch (record.value().getType())
248       {
249         case EVENT_CHATMESSAGE_RECEIVED:
250           Instant instant = Instant.ofEpochSecond(record.timestamp());
251           LocalDateTime timestamp = LocalDateTime.ofInstant(instant, zoneId);
252           loadChatMessage(
253               chatRoomId,
254               timestamp,
255               record.offset(),
256               (EventChatMessageReceivedTo) record.value(),
257               record.partition());
258           break;
259
260         default:
261           log.debug(
262               "Ignoring message for chat-room {} with offset {}: {}",
263               chatRoomId,
264               record.offset(),
265               record.value());
266       }
267
268       nextOffset[record.partition()] = record.offset() + 1;
269     }
270   }
271
272   private void loadChatMessage(
273       UUID chatRoomId,
274       LocalDateTime timestamp,
275       long offset,
276       EventChatMessageReceivedTo chatMessageTo,
277       int partition)
278   {
279     Message.MessageKey key = Message.MessageKey.of(chatMessageTo.getUser(), chatMessageTo.getId());
280     Message message = new Message(key, offset, timestamp, chatMessageTo.getText());
281
282     ChatRoomData chatRoomData = computeChatRoomData(chatRoomId, partition);
283     KafkaChatMessageService kafkaChatRoomService =
284         (KafkaChatMessageService) chatRoomData.getChatRoomService();
285
286     log.debug(
287         "Loaded message from partition={} at offset={}: {}",
288         partition,
289         offset,
290         message);
291     kafkaChatRoomService.persistMessage(message);
292   }
293
294   private boolean isLoadingCompleted()
295   {
296     return IntStream
297         .range(0, numShards)
298         .filter(shard -> isShardOwned[shard])
299         .allMatch(shard ->
300         {
301           TopicPartition partition = new TopicPartition(topic, shard);
302           long position = consumer.position(partition);
303           return position >= currentOffset[shard];
304         });
305   }
306
307   private void pauseAllOwnedPartions()
308   {
309     consumer.pause(IntStream
310         .range(0, numShards)
311         .filter(shard -> isShardOwned[shard])
312         .mapToObj(shard -> new TopicPartition(topic, shard))
313         .toList());
314   }
315
316
317   int[] getOwnedShards()
318   {
319     return IntStream
320         .range(0, numShards)
321         .filter(shard -> isShardOwned[shard])
322         .toArray();
323   }
324
325   void createChatRoomData(ChatRoomInfo chatRoomInfo)
326   {
327     computeChatRoomData(chatRoomInfo.getId(), chatRoomInfo.getShard());
328   }
329
330   Mono<ChatRoomData> getChatRoomData(int shard, UUID id)
331   {
332     ChannelState capturedState = channelState;
333     if (capturedState != ChannelState.READY)
334     {
335       return Mono.error(new ChannelNotReadyException(capturedState));
336     }
337
338     if (!isShardOwned[shard])
339     {
340       return Mono.error(new ShardNotOwnedException(instanceId, shard));
341     }
342
343     return Mono.justOrEmpty(chatRoomData[shard].get(id));
344   }
345
346   private ChatRoomData computeChatRoomData(UUID chatRoomId, int shard)
347   {
348     ChatRoomData chatRoomData = this.chatRoomData[shard].get(chatRoomId);
349
350     if (chatRoomData != null)
351     {
352       log.info(
353           "Ignoring request to create already existing ChatRoomData for {}",
354           chatRoomId);
355     }
356     else
357     {
358       log.info("Creating ChatRoomData {} with history-limit {}", chatRoomId, historyLimit);
359       KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
360       chatRoomData = new ChatRoomData(clock, service, historyLimit);
361       this.chatRoomData[shard].put(chatRoomId, chatRoomData);
362     }
363
364     return chatRoomData;
365   }
366
367   ConsumerGroupMetadata getConsumerGroupMetadata()
368   {
369     return consumer.groupMetadata();
370   }
371 }