problem.setDetail(stringBuilder.toString());
problem.setProperty("chatroomId", e.getChatroomId());
+ problem.setProperty("shard", e.getShard());
+ problem.setProperty("ownedShards", e.getOwnedShards());
return problem;
}
public interface ChatHomeService
{
Mono<ChatRoom> getChatRoom(int shard, UUID id);
+ int[] getOwnedShards();
Flux<ChatRoom> getChatRooms(int shard);
}
{
return service
.getChatRoom(shard, id)
- .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
+ .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(
+ id,
+ shard,
+ service.getOwnedShards())));
}
@Override
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;
}
}
import reactor.core.publisher.Mono;
import java.util.*;
+import java.util.stream.IntStream;
@Slf4j
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)
{
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)
{
// 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
// 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
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
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
// 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