From: Kai Moritz Date: Mon, 21 Aug 2023 16:26:53 +0000 (+0200) Subject: feat: `UnknownChatroomException` records shard and owned shards X-Git-Tag: wip-kafka-chat-home-test--springboot~10 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=fdf9188a7abebcd709635f3eb7c5205f7f2405c7;p=demos%2Fkafka%2Fchat feat: `UnknownChatroomException` records shard and owned shards --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java index ad90c4b6..47b46bc5 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java @@ -47,6 +47,8 @@ public class ChatBackendControllerAdvice problem.setDetail(stringBuilder.toString()); problem.setProperty("chatroomId", e.getChatroomId()); + problem.setProperty("shard", e.getShard()); + problem.setProperty("ownedShards", e.getOwnedShards()); return problem; } diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java b/src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java index 1f70f110..714c2207 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java @@ -2,17 +2,43 @@ package de.juplo.kafka.chat.backend.domain; import lombok.Getter; +import java.util.Arrays; +import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; -public class UnknownChatroomException extends RuntimeException +public class UnknownChatroomException extends IllegalStateException { @Getter private final UUID chatroomId; + @Getter + private final Optional shard; + @Getter + private final Optional ownedShards; public UnknownChatroomException(UUID chatroomId) { super("Chatroom does not exist: " + chatroomId); this.chatroomId = chatroomId; + this.shard = Optional.empty(); + this.ownedShards = Optional.empty(); + } + + public UnknownChatroomException(UUID chatroomId, int shard, int[] ownedShards) + { + super( + "Chatroom does not exist (here): " + + chatroomId + + " shard=" + + shard + + ", owned=" + + Arrays + .stream(ownedShards) + .mapToObj(ownedShard -> Integer.toString(ownedShard)) + .collect(Collectors.joining(","))); + this.chatroomId = chatroomId; + this.shard = Optional.of(shard); + this.ownedShards = Optional.of(ownedShards); } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java index 13341244..29f13127 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java @@ -6,6 +6,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.*; +import java.util.stream.IntStream; @Slf4j @@ -60,6 +61,14 @@ public class InMemoryChatHomeService return Mono.justOrEmpty(chatrooms[shard].get(id)); } + public int[] getOwnedShards() + { + return IntStream + .range(0, chatrooms.length) + .filter(i -> chatrooms[i] != null) + .toArray(); + } + public Flux getChatRooms(int shard) { return Flux.fromStream(chatrooms[shard].values().stream()); diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java index e35a6611..f99bc9d8 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java @@ -35,7 +35,10 @@ public class SimpleChatHome implements ChatHome { return service .getChatRoom(shard, id) - .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id))); + .switchIfEmpty(Mono.error(() -> new UnknownChatroomException( + id, + shard, + service.getOwnedShards()))); } @Override diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHome.java index ab72269d..07fb8858 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHome.java @@ -26,7 +26,10 @@ public class KafkaChatHome implements ChatHome int shard = selectShard(id); return chatRoomChannel .getChatRoom(shard, id) - .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id))); + .switchIfEmpty(Mono.error(() -> new UnknownChatroomException( + id, + shard, + chatRoomChannel.getOwnedShards()))); } int selectShard(UUID chatRoomId) diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java index 5df43143..32b1e357 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java +++ b/src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java @@ -53,6 +53,7 @@ public class ChatBackendControllerTest // Given UUID chatroomId = getRandomIdForOwnedShard(); when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 }); // When WebTestClient.ResponseSpec responseSpec = client @@ -73,6 +74,7 @@ public class ChatBackendControllerTest // Given UUID chatroomId = getRandomIdForOwnedShard(); when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 }); // When WebTestClient.ResponseSpec responseSpec = client @@ -94,6 +96,7 @@ public class ChatBackendControllerTest String username = "foo"; Long messageId = 66l; when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 }); // When WebTestClient.ResponseSpec responseSpec = client @@ -120,6 +123,7 @@ public class ChatBackendControllerTest String username = "foo"; Long messageId = 66l; when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 }); // When WebTestClient.ResponseSpec responseSpec = client @@ -143,6 +147,7 @@ public class ChatBackendControllerTest // Given UUID chatroomId = getRandomIdForOwnedShard(); when(chatHomeService.getChatRoom(anyInt(), any(UUID.class))).thenReturn(Mono.empty()); + when(chatHomeService.getOwnedShards()).thenReturn(new int[] { 6 }); // When WebTestClient.ResponseSpec responseSpec = client