From dfb3853ad45e3623ac7e5265b795a5f62f40f322 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 19 Aug 2023 11:30:20 +0200 Subject: [PATCH] feat: `UnknownChatroomException` records shard and owned shards --- .../api/ChatBackendControllerAdvice.java | 2 ++ .../chat/backend/domain/ChatHomeService.java | 1 + .../chat/backend/domain/SimpleChatHome.java | 5 +++- .../domain/UnknownChatroomException.java | 23 ++++++++++++++++--- .../inmemory/InMemoryChatHomeService.java | 10 ++++++++ .../kafka/KafkaChatHomeService.java | 9 ++++++++ .../api/ChatBackendControllerTest.java | 5 ++++ 7 files changed, 51 insertions(+), 4 deletions(-) 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/ChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeService.java index 19ff4aa4..e7de9e10 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeService.java @@ -9,5 +9,6 @@ import java.util.UUID; public interface ChatHomeService { Mono getChatRoom(int shard, UUID id); + int[] getOwnedShards(); Flux getChatRooms(int shard); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java index 11542edd..bcb356e6 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java @@ -32,7 +32,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/domain/UnknownChatroomException.java b/src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java index 1f70f110..aaeaa204 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,34 @@ package de.juplo.kafka.chat.backend.domain; import lombok.Getter; +import java.util.Arrays; 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 Integer shard; + @Getter + private final int[] ownedShards; - public UnknownChatroomException(UUID chatroomId) + public UnknownChatroomException(UUID chatroomId, int shard, int[] ownedShards) { - super("Chatroom does not exist: " + chatroomId); + 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 = shard; + this.ownedShards = 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 8f262a0b..9efd09a5 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 @@ -7,6 +7,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.*; +import java.util.stream.IntStream; @Slf4j @@ -62,6 +63,15 @@ public class InMemoryChatHomeService implements ChatHomeService return Mono.justOrEmpty(chatrooms[shard].get(id)); } + @Override + public int[] getOwnedShards() + { + return IntStream + .range(0, chatrooms.length) + .filter(i -> chatrooms[i] != null) + .toArray(); + } + @Override public Flux getChatRooms(int shard) { diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java index ce1a72eb..a67d6299 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java @@ -63,6 +63,15 @@ public class KafkaChatHomeService implements ChatHomeService return Mono.justOrEmpty(chatrooms[shard].get(id)); } + @Override + public int[] getOwnedShards() + { + return IntStream + .range(0, chatrooms.length) + .filter(i -> chatrooms[i] != null) + .toArray(); + } + @Override public Flux getChatRooms(int shard) { 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 aea72dca..244cbea8 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 @@ -52,6 +52,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 @@ -72,6 +73,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 @@ -93,6 +95,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 @@ -119,6 +122,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 @@ -142,6 +146,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 -- 2.20.1