X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fimplementation%2Finmemory%2FShardedChatHomeService.java;h=ab7f8d43ecf5e8d1635c50547ebce4ab29f5e5b2;hb=f70bb25f9b0769068028348995a78ece2130cd35;hp=06c197bf65f2a2cb423f96ca82e7756a43169c3f;hpb=33f1841f44878ce6a4896802d995d10a6aeef6d4;p=demos%2Fkafka%2Fchat diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeService.java index 06c197bf..ab7f8d43 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/ShardedChatHomeService.java @@ -8,6 +8,8 @@ import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import java.net.URI; +import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.UUID; @@ -17,16 +19,25 @@ import java.util.stream.Collectors; @Slf4j public class ShardedChatHomeService implements ChatHomeService { + private final String instanceId; private final SimpleChatHomeService[] chatHomes; private final Set ownedShards; + private final String[] shardOwners; private final ShardingStrategy shardingStrategy; public ShardedChatHomeService( + String instanceId, SimpleChatHomeService[] chatHomes, + URI[] shardOwners, ShardingStrategy shardingStrategy) { + this.instanceId = instanceId; this.chatHomes = chatHomes; + this.shardOwners = Arrays + .stream(shardOwners) + .map(uri -> uri.toASCIIString()) + .toArray(size -> new String[size]); this.shardingStrategy = shardingStrategy; this.ownedShards = new HashSet<>(); for (int shard = 0; shard < chatHomes.length; shard++) @@ -46,7 +57,7 @@ public class ShardedChatHomeService implements ChatHomeService { int shard = shardingStrategy.selectShard(id); return chatHomes[shard] == null - ? Mono.error(new ShardNotOwnedException(shard)) + ? Mono.error(new ShardNotOwnedException(instanceId, shard)) : chatHomes[shard].createChatRoom(id, name); } @@ -55,7 +66,7 @@ public class ShardedChatHomeService implements ChatHomeService { int shard = selectShard(id); return chatHomes[shard] == null - ? Mono.error(new ShardNotOwnedException(shard)) + ? Mono.error(new ShardNotOwnedException(instanceId, shard)) : chatHomes[shard] .getChatRoomInfo(id) .onErrorMap(throwable -> throwable instanceof UnknownChatroomException @@ -79,7 +90,7 @@ public class ShardedChatHomeService implements ChatHomeService { int shard = selectShard(id); return chatHomes[shard] == null - ? Mono.error(new ShardNotOwnedException(shard)) + ? Mono.error(new ShardNotOwnedException(instanceId, shard)) : chatHomes[shard] .getChatRoomData(id) .onErrorMap(throwable -> throwable instanceof UnknownChatroomException @@ -90,6 +101,12 @@ public class ShardedChatHomeService implements ChatHomeService : throwable); } + @Override + public Mono getShardOwners() + { + return Mono.just(shardOwners); + } + private int selectShard(UUID chatroomId) { return shardingStrategy.selectShard(chatroomId);