refactor: Pushed sharding one layer down in the architecture
[demos/kafka/chat] / src / main / java / de / juplo / kafka / chat / backend / persistence / inmemory / InMemoryServicesConfiguration.java
index e40d950..cb1a070 100644 (file)
@@ -1,8 +1,9 @@
 package de.juplo.kafka.chat.backend.persistence.inmemory;
 
 import de.juplo.kafka.chat.backend.ChatBackendProperties;
-import de.juplo.kafka.chat.backend.api.KafkaLikeShardingStrategy;
-import de.juplo.kafka.chat.backend.api.ShardingStrategy;
+import de.juplo.kafka.chat.backend.ChatBackendProperties.ShardingStrategyType;
+import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
+import de.juplo.kafka.chat.backend.domain.ShardingStrategy;
 import de.juplo.kafka.chat.backend.domain.ChatHome;
 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -21,21 +22,9 @@ import java.time.Clock;
 public class InMemoryServicesConfiguration
 {
   @Bean
-  ChatHome[] chatHomes(
-      ChatBackendProperties properties,
-      InMemoryChatHomeService chatHomeService,
-      StorageStrategy storageStrategy)
+  ChatHome chatHome(InMemoryChatHomeService chatHomeService)
   {
-    ChatHome[] chatHomes = new ChatHome[properties.getInmemory().getNumShards()];
-    storageStrategy
-        .read()
-        .subscribe(chatRoom ->
-        {
-          int shard = chatRoom.getShard();
-          if (chatHomes[shard] == null)
-            chatHomes[shard] = new ChatHome(chatHomeService, shard);
-        });
-    return chatHomes;
+    return new ChatHome(chatHomeService);
   }
 
   @Bean
@@ -43,19 +32,45 @@ public class InMemoryServicesConfiguration
       ChatBackendProperties properties,
       StorageStrategy storageStrategy)
   {
+    ShardingStrategyType sharding =
+        properties.getInmemory().getShardingStrategy();
+
+    int numShards;
+    int[] ownedShards;
+    ShardingStrategy shardingStrategy;
+
+    switch (sharding)
+    {
+      case none:
+        numShards = 1;
+        ownedShards = new int[] { 0 };
+        shardingStrategy = id -> 0;
+        break;
+      case kafkalike:
+        numShards = properties.getInmemory().getNumShards();
+        ownedShards = properties.getInmemory().getOwnedShards();
+        shardingStrategy = new KafkaLikeShardingStrategy(numShards);
+        break;
+      default:
+        throw new IllegalArgumentException("Unknown sharding strategy: " + sharding);
+    }
+
     return new InMemoryChatHomeService(
-        properties.getInmemory().getNumShards(),
-        properties.getInmemory().getOwnedShards(),
+        numShards,
+        ownedShards,
+        shardingStrategy,
         storageStrategy.read());
   }
 
   @Bean
   InMemoryChatRoomFactory chatRoomFactory(
+      InMemoryChatHomeService service,
       ShardingStrategy strategy,
       Clock clock,
       ChatBackendProperties properties)
   {
     return new InMemoryChatRoomFactory(
+        service,
         strategy,
         clock,
         properties.getChatroomBufferSize());