problem.setDetail(stringBuilder.toString());
problem.setProperty("chatroomId", e.getChatroomId());
+ problem.setProperty("shard", e.getShard());
+ problem.setProperty("ownedShards", e.getOwnedShards());
return problem;
}
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<Integer> shard;
+ @Getter
+ private final Optional<int[]> 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);
}
}
import reactor.core.publisher.Mono;
import java.util.*;
+import java.util.stream.IntStream;
@Slf4j
return Mono.justOrEmpty(chatrooms[shard].get(id));
}
+ public int[] getOwnedShards()
+ {
+ return IntStream
+ .range(0, chatrooms.length)
+ .filter(i -> chatrooms[i] != null)
+ .toArray();
+ }
+
public Flux<ChatRoom> getChatRooms(int shard)
{
return Flux.fromStream(chatrooms[shard].values().stream());
{
return service
.getChatRoom(shard, id)
- .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
+ .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(
+ id,
+ shard,
+ service.getOwnedShards())));
}
@Override
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)
// 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