projects
/
demos
/
kafka
/
chat
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
feat: Introduced a kafka-like `ShardingStrategy` for `inmemory`
[demos/kafka/chat]
/
src
/
main
/
java
/
de
/
juplo
/
kafka
/
chat
/
backend
/
api
/
ChatBackendController.java
diff --git
a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
index
d0d2763
..
9be7fa1
100644
(file)
--- a/
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
+++ b/
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
@@
-2,6
+2,7
@@
package de.juplo.kafka.chat.backend.api;
import de.juplo.kafka.chat.backend.domain.ChatHome;
import de.juplo.kafka.chat.backend.domain.ChatRoom;
import de.juplo.kafka.chat.backend.domain.ChatHome;
import de.juplo.kafka.chat.backend.domain.ChatRoom;
+import de.juplo.kafka.chat.backend.domain.ChatRoomFactory;
import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.http.codec.ServerSentEvent;
import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.http.codec.ServerSentEvent;
@@
-16,26
+17,35
@@
import java.util.UUID;
@RequiredArgsConstructor
public class ChatBackendController
{
@RequiredArgsConstructor
public class ChatBackendController
{
- private final ChatHome chatHome;
+ private final ChatHome[] chatHomes;
+ private final ShardingStrategy selectionStrategy;
+ private final ChatRoomFactory factory;
private final StorageStrategy storageStrategy;
@PostMapping("create")
public Mono<ChatRoomTo> create(@RequestBody String name)
{
private final StorageStrategy storageStrategy;
@PostMapping("create")
public Mono<ChatRoomTo> create(@RequestBody String name)
{
- return chatHome.createChatroom(name).map(ChatRoomTo::from);
+ UUID chatRoomId = UUID.randomUUID();
+ return factory
+ .createChatRoom(chatRoomId, name)
+ .flatMap(chatRoom -> chatHomes[chatRoom.getShard()].putChatRoom(chatRoom))
+ .map(ChatRoomTo::from);
}
@GetMapping("list")
public Flux<ChatRoomTo> list()
{
}
@GetMapping("list")
public Flux<ChatRoomTo> list()
{
- return chatHome.getChatRooms().map(chatroom -> ChatRoomTo.from(chatroom));
+ return Flux
+ .fromArray(chatHomes)
+ .flatMap(chatHome -> chatHome.getChatRooms())
+ .map(chatroom -> ChatRoomTo.from(chatroom));
}
@GetMapping("{chatroomId}/list")
public Flux<MessageTo> list(@PathVariable UUID chatroomId)
{
}
@GetMapping("{chatroomId}/list")
public Flux<MessageTo> list(@PathVariable UUID chatroomId)
{
- return chatHome
+ return chatHome
s[selectShard(chatroomId)]
.getChatRoom(chatroomId)
.flatMapMany(chatroom -> chatroom
.getMessages()
.getChatRoom(chatroomId)
.flatMapMany(chatroom -> chatroom
.getMessages()
@@
-45,7
+55,9
@@
public class ChatBackendController
@GetMapping("{chatroomId}")
public Mono<ChatRoomTo> get(@PathVariable UUID chatroomId)
{
@GetMapping("{chatroomId}")
public Mono<ChatRoomTo> get(@PathVariable UUID chatroomId)
{
- return chatHome.getChatRoom(chatroomId).map(chatroom -> ChatRoomTo.from(chatroom));
+ return chatHomes[selectShard(chatroomId)]
+ .getChatRoom(chatroomId)
+ .map(chatroom -> ChatRoomTo.from(chatroom));
}
@PutMapping("{chatroomId}/{username}/{messageId}")
}
@PutMapping("{chatroomId}/{username}/{messageId}")
@@
-56,7
+68,7
@@
public class ChatBackendController
@RequestBody String text)
{
return
@RequestBody String text)
{
return
- chatHome
+ chatHome
s[selectShard(chatroomId)]
.getChatRoom(chatroomId)
.flatMap(chatroom -> put(chatroom, username, messageId, text));
}
.getChatRoom(chatroomId)
.flatMap(chatroom -> put(chatroom, username, messageId, text));
}
@@
-83,7
+95,7
@@
public class ChatBackendController
@PathVariable Long messageId)
{
return
@PathVariable Long messageId)
{
return
- chatHome
+ chatHome
s[selectShard(chatroomId)]
.getChatRoom(chatroomId)
.flatMap(chatroom -> get(chatroom, username, messageId));
}
.getChatRoom(chatroomId)
.flatMap(chatroom -> get(chatroom, username, messageId));
}
@@
-102,7
+114,7
@@
public class ChatBackendController
@GetMapping(path = "{chatroomId}/listen")
public Flux<ServerSentEvent<MessageTo>> listen(@PathVariable UUID chatroomId)
{
@GetMapping(path = "{chatroomId}/listen")
public Flux<ServerSentEvent<MessageTo>> listen(@PathVariable UUID chatroomId)
{
- return chatHome
+ return chatHome
s[selectShard(chatroomId)]
.getChatRoom(chatroomId)
.flatMapMany(chatroom -> listen(chatroom));
}
.getChatRoom(chatroomId)
.flatMapMany(chatroom -> listen(chatroom));
}
@@
-124,6
+136,12
@@
public class ChatBackendController
@PostMapping("/store")
public void store()
{
@PostMapping("/store")
public void store()
{
- storageStrategy.write(chatHome.getChatRooms());
+ for (int shard = 0; shard < chatHomes.length; shard++)
+ storageStrategy.write(chatHomes[shard].getChatRooms());
+ }
+
+ private int selectShard(UUID chatroomId)
+ {
+ return selectionStrategy.selectShard(chatroomId);
}
}
}
}