import de.juplo.kafka.chat.backend.domain.ChatHomeService;
import de.juplo.kafka.chat.backend.domain.ChatRoom;
+import de.juplo.kafka.chat.backend.domain.ShardNotOwnedException;
import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.Consumer;
-import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
-import org.apache.kafka.common.TopicPartition;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Slf4j
-public class KafkaChatHomeService implements ChatHomeService, ConsumerRebalanceListener
+public class KafkaChatHomeService implements ChatHomeService
{
- private final Consumer<String, MessageTo> consumer;
- private final String topic;
- private final long[] offsets;
- private final Map<UUID, ChatRoom>[] chatrooms;
+ private final ChatMessageChannel chatMessageChanel;
- public KafkaChatHomeService(
- Consumer<String, MessageTo> consumer,
- String topic,
- int numShards)
+ public KafkaChatHomeService(ChatMessageChannel chatMessageChannel)
{
log.debug("Creating KafkaChatHomeService");
- this.consumer = consumer;
- this.topic = topic;
- this.offsets = new long[numShards];
- this.chatrooms = new Map<>[numShards];
- for (int i=0; i< numShards; i++)
- {
- this.offsets[i] = 0l;
- this.chatrooms[i] = chat
- }
- this.chatrooms = new Map[numShards];
- this.chatrooms = new Map[numShards];
+ this.chatMessageChanel = chatMessageChannel;
}
@Override
- public void onPartitionsAssigned(Collection<TopicPartition> partitions)
- {
- consumer.endOffsets(partitions).forEach((tp, currentOffset) ->
- {
- if (!tp.topic().equals(topic))
- {
- log.warn("Ignoring partition from unwanted topic: {}", tp);
- return;
- }
-
- int partition = tp.partition();
- long unseenOffset = offsets[partition];
-
- log.info(
- "Loading messages from partition {}: start-offset={} -> current-offset={}",
- partition,
- unseenOffset,
- currentOffset);
- handlers[partition] = new ChatRoomLoadingMessageHandlingStrategy(tp, currentOffset, unseenOffset);
- });
- }
-
- @Override
- public void onPartitionsRevoked(Collection<TopicPartition> partitions)
+ public Mono<ChatRoom> getChatRoom(int shard, UUID id)
{
- partitions.forEach(tp ->
+ if (chatMessageChanel.isLoadInProgress())
{
- if (!tp.topic().equals(topic))
- {
- log.warn("Ignoring partition from unwanted topic: {}", tp);
- return;
- }
-
- int partition = tp.partition();
- long unseenOffset = offsets[partition];
-
- log.info("Reading partition {} from {} -> {}", partition, unseenOffset, currentOffset);
- });
- log.info("Revoked partitions: {}", partitions);
- }
-
- @Override
- public void onPartitionsLost(Collection<TopicPartition> partitions)
- {
- log.info("Revoked partitions: {}", partitions);
- }
-
- private void foo()
- {
- Set<Integer> owned = Arrays
- .stream(ownedShards)
- .collect(
- () -> new HashSet<>(),
- (set, i) -> set.add(i),
- (a, b) -> a.addAll(b));
- for (int shard = 0; shard < numShards; shard++)
+ throw new ShardNotOwnedException(shard);
+ }
+ else
{
- chatrooms[shard] = owned.contains(shard)
- ? new HashMap<>()
- : null;
+ return chatMessageChanel.getChatRoom(shard, id);
}
- chatroomFlux
- .filter(chatRoom ->
- {
- if (owned.contains(chatRoom.getShard()))
- {
- return true;
- }
- else
- {
- log.info("Ignoring not owned chat-room {}", chatRoom);
- return false;
- }
- })
- .toStream()
- .forEach(chatroom -> chatrooms[chatroom.getShard()].put(chatroom.getId(), chatroom));
- }
-
- @Override
- public Mono<ChatRoom> putChatRoom(ChatRoom chatRoom)
- {
- chatrooms[chatRoom.getShard()].put(chatRoom.getId(), chatRoom);
- return Mono.just(chatRoom);
- }
-
- @Override
- public Mono<ChatRoom> getChatRoom(int shard, UUID id)
- {
- return Mono.justOrEmpty(chatrooms[shard].get(id));
}
@Override
public Flux<ChatRoom> getChatRooms(int shard)
{
- return Flux.fromStream(chatrooms[shard].values().stream());
+ if (chatMessageChanel.isLoadInProgress())
+ {
+ throw new ShardNotOwnedException(shard);
+ }
+ else
+ {
+ return chatMessageChanel.getChatRooms(shard);
+ }
}
}