From cd192a26afb8beb0e45cb0807e7f63e30e87966a Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 19 Aug 2023 17:33:03 +0200 Subject: [PATCH] feat: Implemented new Default-`StorageStrategy` `none' * If `none` is selected as storage strategy, an empty implementation of `Storage-Strategy` instanciated. * The owned shardes are derived from the according configuration property. * Before, they were derived from the stored data. --- .../chat/backend/ChatBackendProperties.java | 4 +- .../InMemoryServicesConfiguration.java | 15 ++---- .../files/FilesStorageConfiguration.java | 3 +- .../NoStorageStorageConfiguration.java | 48 +++++++++++++++++++ ...ryWithFilesAndShardingConfigurationIT.java | 1 + .../InMemoryWithFilesConfigurationIT.java | 1 + 6 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java index 177d4f51..def9de17 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java @@ -25,11 +25,11 @@ public class ChatBackendProperties private ShardingStrategyType shardingStrategy = ShardingStrategyType.none; private int numShards = 1; private int[] ownedShards = new int[] { 0 }; - private StorageStrategyType storageStrategy = StorageStrategyType.files; + private StorageStrategyType storageStrategy = StorageStrategyType.none; private String storageDirectory = Paths.get(System.getProperty("java.io.tmpdir"),"chat", "backend").toString(); } public enum ServiceType { inmemory } - public enum StorageStrategyType { files, mongodb } + public enum StorageStrategyType { none, files, mongodb } public enum ShardingStrategyType { none, kafkalike } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java index de504485..175f7140 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java @@ -13,6 +13,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.time.Clock; +import java.util.stream.IntStream; @ConditionalOnProperty( @@ -41,19 +42,13 @@ public class InMemoryServicesConfiguration havingValue = "kafkalike") ChatHome kafkalikeShardingChatHome( ChatBackendProperties properties, - InMemoryChatHomeService chatHomeService, - StorageStrategy storageStrategy) + InMemoryChatHomeService chatHomeService) { int numShards = properties.getInmemory().getNumShards(); SimpleChatHome[] chatHomes = new SimpleChatHome[numShards]; - storageStrategy - .read() - .subscribe(chatRoom -> - { - int shard = chatRoom.getShard(); - if (chatHomes[shard] == null) - chatHomes[shard] = new SimpleChatHome(chatHomeService, shard); - }); + IntStream + .of(properties.getInmemory().getOwnedShards()) + .forEach(shard -> chatHomes[shard] = new SimpleChatHome(chatHomeService, shard)); ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards); return new ShardedChatHome(chatHomes, strategy); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java index df730aaf..702d1d58 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java @@ -19,8 +19,7 @@ import java.time.Clock; @ConditionalOnProperty( prefix = "chat.backend.inmemory", name = "storage-strategy", - havingValue = "files", - matchIfMissing = true) + havingValue = "files") @Configuration @EnableAutoConfiguration( exclude = { diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java new file mode 100644 index 00000000..824c6f2a --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java @@ -0,0 +1,48 @@ +package de.juplo.kafka.chat.backend.persistence.storage.nostorage; + +import com.fasterxml.jackson.databind.ObjectMapper; +import de.juplo.kafka.chat.backend.ChatBackendProperties; +import de.juplo.kafka.chat.backend.domain.ChatRoom; +import de.juplo.kafka.chat.backend.domain.ShardingStrategy; +import de.juplo.kafka.chat.backend.persistence.StorageStrategy; +import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration; +import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import reactor.core.publisher.Flux; + +import java.nio.file.Paths; +import java.time.Clock; + + +@ConditionalOnProperty( + prefix = "chat.backend.inmemory", + name = "storage-strategy", + havingValue = "none", + matchIfMissing = true) +@Configuration +@EnableAutoConfiguration( + exclude = { + MongoRepositoriesAutoConfiguration.class, + MongoAutoConfiguration.class }) +public class NoStorageStorageConfiguration +{ + @Bean + public StorageStrategy storageStrategy() + { + return new StorageStrategy() + { + @Override + public void write(Flux chatroomFlux) {} + + @Override + public Flux read() + { + return Flux.empty(); + } + }; + } +} diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesAndShardingConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesAndShardingConfigurationIT.java index d792d258..72230e9c 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesAndShardingConfigurationIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesAndShardingConfigurationIT.java @@ -6,6 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { + "chat.backend.inmemory.storage-strategy=files", "chat.backend.inmemory.storage-directory=target/test-classes/data/files", "chat.backend.inmemory.sharding-strategy=kafkalike", "chat.backend.inmemory.num-shards=10", diff --git a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java index 151a833a..2ff9e111 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java @@ -7,6 +7,7 @@ import org.springframework.boot.test.context.SpringBootTest; webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "chat.backend.inmemory.sharding-strategy=none", + "chat.backend.inmemory.storage-strategy=files", "chat.backend.inmemory.storage-directory=target/test-classes/data/files" }) class InMemoryWithFilesConfigurationIT extends AbstractConfigurationIT { -- 2.20.1