WIP: shard assigned/revoked events
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / implementation / kafka / DataChannel.java
index d2d6f30..4eedeb4 100644 (file)
@@ -19,6 +19,7 @@ import reactor.core.publisher.Mono;
 
 import java.time.*;
 import java.util.*;
+import java.util.function.Function;
 import java.util.stream.IntStream;
 
 
@@ -137,6 +138,7 @@ public class DataChannel implements Runnable, ConsumerRebalanceListener
           currentOffset);
 
       consumer.seek(topicPartition, nextOffset[partition]);
+      infoChannel.sendShardAssignedEvent(partition);
     });
 
     consumer.resume(partitions);
@@ -150,6 +152,7 @@ public class DataChannel implements Runnable, ConsumerRebalanceListener
       int partition = topicPartition.partition();
       isShardOwned[partition] = false;
       log.info("Partition revoked: {} - next={}", partition, nextOffset[partition]);
+      infoChannel.sendShardRevokedEvent(partition);
     });
   }
 
@@ -244,14 +247,9 @@ public class DataChannel implements Runnable, ConsumerRebalanceListener
     Message.MessageKey key = Message.MessageKey.of(chatMessageTo.getUser(), chatMessageTo.getId());
     Message message = new Message(key, offset, timestamp, chatMessageTo.getText());
 
-    ChatRoomData chatRoomData = this.chatRoomData[partition].computeIfAbsent(
-        chatRoomId,
-        (id) ->
-        {
-          log.info("Creating ChatRoom {} with buffer-size {}", id, bufferSize);
-          KafkaChatMessageService service = new KafkaChatMessageService(this, id);
-          return new ChatRoomData(clock, service, bufferSize);
-        });
+    ChatRoomData chatRoomData = this
+        .chatRoomData[partition]
+        .computeIfAbsent(chatRoomId, this::computeChatRoomData);
     KafkaChatMessageService kafkaChatRoomService =
         (KafkaChatMessageService) chatRoomData.getChatRoomService();
 
@@ -298,13 +296,14 @@ public class DataChannel implements Runnable, ConsumerRebalanceListener
 
     return infoChannel
         .getChatRoomInfo(id)
-        .map(chatRoomInfo -> chatRoomData[shard].computeIfAbsent(
-            id,
-            (chatRoomId) ->
-            {
-              log.info("Creating ChatRoom {} with buffer-size {}", chatRoomId, bufferSize);
-              KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
-              return new ChatRoomData(clock, service, bufferSize);
-            }));
+        .map(chatRoomInfo ->
+            chatRoomData[shard].computeIfAbsent(id, this::computeChatRoomData));
+  }
+
+  private ChatRoomData computeChatRoomData(UUID chatRoomId)
+  {
+    log.info("Creating ChatRoom {} with buffer-size {}", chatRoomId, bufferSize);
+    KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
+    return new ChatRoomData(clock, service, bufferSize);
   }
 }