X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fimplementation%2Finmemory%2FShardedChatHomeService.java;h=9a3845332a848f8ff5f6eddd1466d5074bad7c17;hb=4aacef9fa8d116ea4ef9687c42b8744841c09457;hp=c281d9e1d463d74130b000cb3cdbee47e3a3c0f9;hpb=a12ced5d2e733b957b106e8393f115941d983823;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 c281d9e1..9a384533 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 @@ -19,6 +19,7 @@ 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; @@ -26,10 +27,12 @@ public class ShardedChatHomeService implements ChatHomeService public ShardedChatHomeService( + String instanceId, SimpleChatHomeService[] chatHomes, URI[] shardOwners, ShardingStrategy shardingStrategy) { + this.instanceId = instanceId; this.chatHomes = chatHomes; this.shardOwners = Arrays .stream(shardOwners) @@ -40,12 +43,7 @@ public class ShardedChatHomeService implements ChatHomeService for (int shard = 0; shard < chatHomes.length; shard++) if(chatHomes[shard] != null) this.ownedShards.add(shard); - log.info( - "Created ShardedChatHome for shards: {}", - ownedShards - .stream() - .map(String::valueOf) - .collect(Collectors.joining(", "))); + log.info("Created {}", this); } @@ -54,7 +52,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); } @@ -63,7 +61,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 @@ -87,7 +85,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 @@ -108,4 +106,18 @@ public class ShardedChatHomeService implements ChatHomeService { return shardingStrategy.selectShard(chatroomId); } + + @Override + public String toString() + { + StringBuffer stringBuffer = new StringBuffer(ShardedChatHomeService.class.getSimpleName()); + stringBuffer.append(", shards=["); + stringBuffer.append(ownedShards + .stream() + .sorted() + .map(String::valueOf) + .collect(Collectors.joining(","))); + stringBuffer.append("]"); + return stringBuffer.toString(); + } }