feat: `UnknownChatroomException` records shard and owned shards
authorKai Moritz <kai@juplo.de>
Sat, 19 Aug 2023 09:30:20 +0000 (11:30 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 20 Aug 2023 09:26:23 +0000 (11:26 +0200)
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java
src/main/java/de/juplo/kafka/chat/backend/domain/ChatHomeService.java
src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java
src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java
src/test/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerTest.java

index ad90c4b..47b46bc 100644 (file)
@@ -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;
   }
index 19ff4aa..e7de9e1 100644 (file)
@@ -9,5 +9,6 @@ import java.util.UUID;
 public interface ChatHomeService
 {
   Mono<ChatRoom> getChatRoom(int shard, UUID id);
+  int[] getOwnedShards();
   Flux<ChatRoom> getChatRooms(int shard);
 }
index 11542ed..bcb356e 100644 (file)
@@ -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
index 1f70f11..aaeaa20 100644 (file)
@@ -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;
   }
 }
index 8f262a0..9efd09a 100644 (file)
@@ -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<ChatRoom> getChatRooms(int shard)
   {
index ce1a72e..a67d629 100644 (file)
@@ -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<ChatRoom> getChatRooms(int shard)
   {
index aea72dc..244cbea 100644 (file)
@@ -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