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 3a99019..cb1a070 100644 (file)
@@ -1,6 +1,10 @@
 package de.juplo.kafka.chat.backend.persistence.inmemory;
 
 import de.juplo.kafka.chat.backend.ChatBackendProperties;
+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;
 import org.springframework.context.annotation.Bean;
@@ -12,20 +16,85 @@ import java.time.Clock;
 @ConditionalOnProperty(
     prefix = "chat.backend",
     name = "services",
-    havingValue = "in-memory",
+    havingValue = "inmemory",
     matchIfMissing = true)
 @Configuration
 public class InMemoryServicesConfiguration
 {
+  @Bean
+  ChatHome chatHome(InMemoryChatHomeService chatHomeService)
+  {
+    return new ChatHome(chatHomeService);
+  }
+
   @Bean
   InMemoryChatHomeService chatHomeService(
-      StorageStrategy storageStrategy,
+      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(
+        numShards,
+        ownedShards,
+        shardingStrategy,
+        storageStrategy.read());
+  }
+
+  @Bean
+  InMemoryChatRoomFactory chatRoomFactory(
+      InMemoryChatHomeService service,
+      ShardingStrategy strategy,
       Clock clock,
       ChatBackendProperties properties)
   {
-    return new InMemoryChatHomeService(
-        storageStrategy.read(),
+    return new InMemoryChatRoomFactory(
+        service,
+        strategy,
         clock,
         properties.getChatroomBufferSize());
   }
+
+  @ConditionalOnProperty(
+      prefix = "chat.backend.inmemory",
+      name = "sharding-strategy",
+      havingValue = "none",
+      matchIfMissing = true)
+  @Bean
+  ShardingStrategy defaultShardingStrategy()
+  {
+    return chatRoomId -> 0;
+  }
+
+  @ConditionalOnProperty(
+      prefix = "chat.backend.inmemory",
+      name = "sharding-strategy",
+      havingValue = "kafkalike")
+  @Bean
+  ShardingStrategy kafkalikeShardingStrategy(ChatBackendProperties properties)
+  {
+    return new KafkaLikeShardingStrategy(
+        properties.getInmemory().getNumShards());
+  }
 }