* Introduced `volatile ChatRoomData#active`, which initially is `false`.
* `ChatRoomData#listen()` throws `ChatRoomInactiveException` if inactive.
* `ChatRoomData#addMessage(..)` throws `ChatRoomInactiveException` if
inactive.
* `SimpleChatHomeService` explicitly activates restored and newly created
instances of `ChatRoomData`.
* `DataChannel` explicitly activates instances of `ChatRoomData`, if
they are restored during partition-assignment or, if a new chat-room
is created.
* `DataChannel` explicitly _deactivates_ instances of `ChatRoomData`,
if the associated partition is revoked.
* Also: Introduced `ChatMessageService#getChatRoomId()`.
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
+import java.util.UUID;
public interface ChatMessageService
{
+ UUID getChatRoomId();
+
Mono<Message> persistMessage(
Message.MessageKey key,
LocalDateTime timestamp,
package de.juplo.kafka.chat.backend.domain;
+import de.juplo.kafka.chat.backend.domain.exceptions.ChatRoomInactiveException;
import de.juplo.kafka.chat.backend.domain.exceptions.InvalidUsernameException;
import de.juplo.kafka.chat.backend.domain.exceptions.MessageMutationException;
import lombok.extern.slf4j.Slf4j;
private final Clock clock;
private final int historyLimit;
private Sinks.Many<Message> sink;
+ private volatile boolean active = false;
public ChatRoomData(
// @RequiredArgsConstructor unfortunately not possible, because
// the `historyLimit` is not set, if `createSink()` is called
// from the variable declaration!
- this.sink = createSink();
}
sink.error(new MessageMutationException(existing, text));
}
})
- .switchIfEmpty(
- Mono
+ .switchIfEmpty(active
+ ? Mono
.defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
.doOnNext(m ->
{
{
log.warn("Emitting of message failed with {} for {}", result.name(), m);
}
- }));
+ })
+ : Mono.error(new ChatRoomInactiveException(service.getChatRoomId())));
}
synchronized public Flux<Message> listen()
{
- return sink
- .asFlux()
- .doOnCancel(() -> sink = createSink()); // Sink hast to be recreated on auto-cancel!
+ return active
+ ? sink
+ .asFlux()
+ .doOnCancel(() -> sink = createSink()) // Sink hast to be recreated on auto-cancel!
+ : Flux
+ .error(new ChatRoomInactiveException(service.getChatRoomId()));
+
}
public Flux<Message> getMessages()
public void activate()
{
+ if (active)
+ {
+ log.info("{} is already active!", service.getChatRoomId());
+ return;
+ }
+
+ log.info("{} is being activated", service.getChatRoomId());
+ this.sink = createSink();
+ active = true;
}
public void deactivate()
{
+ log.info("{} is being deactivated", service.getChatRoomId());
+ active = false;
+ sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
}
private Sinks.Many<Message> createSink()
--- /dev/null
+package de.juplo.kafka.chat.backend.domain.exceptions;
+
+import lombok.Getter;
+
+import java.util.UUID;
+
+
+public class ChatRoomInactiveException extends IllegalStateException
+{
+ @Getter
+ private final UUID chatRoomId;
+
+
+ public ChatRoomInactiveException(UUID chatRoomId)
+ {
+ super("Chat-Room " + chatRoomId + " is currently inactive.");
+ this.chatRoomId = chatRoomId;
+ }
+}
import de.juplo.kafka.chat.backend.domain.ChatMessageService;
import de.juplo.kafka.chat.backend.domain.Message;
import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
+import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Slf4j
public class InMemoryChatMessageService implements ChatMessageService
{
+ @Getter
private final UUID chatRoomId;
private final LinkedHashMap<Message.MessageKey, Message> messages;
new InMemoryChatMessageService(chatRoomId);
chatRoomInfo.put(chatRoomId, info);
- chatRoomData.put(
- info.getId(),
+ ChatRoomData chatRoomData =
new ChatRoomData(
clock,
chatMessageService,
- historyLimit));
+ historyLimit);
+ chatRoomData.activate();
+ this.chatRoomData.put(info.getId(), chatRoomData);
return chatMessageService.restore(storageStrategy);
})
ChatRoomInfo chatRoomInfo = new ChatRoomInfo(id, name, shard);
this.chatRoomInfo.put(id, chatRoomInfo);
ChatRoomData chatRoomData = new ChatRoomData(clock, service, historyLimit);
+ chatRoomData.activate();
this.chatRoomData.put(id, chatRoomData);
return Mono.just(chatRoomInfo);
}
int partition = topicPartition.partition();
isShardOwned[partition] = false;
nextOffset[partition] = consumer.position(topicPartition);
+
log.info("Partition revoked: {} - next={}", partition, nextOffset[partition]);
+
+ chatRoomData[partition]
+ .values()
+ .forEach(chatRoomData -> chatRoomData.deactivate());
+
channelMediator.shardRevoked(partition);
});
}
{
log.info("Loading of messages completed! Pausing all owned partitions...");
pauseAllOwnedPartions();
+ activateAllOwnedChatRooms();
log.info("Resuming normal operations...");
channelState = ChannelState.READY;
}
.toList());
}
+ private void activateAllOwnedChatRooms()
+ {
+ IntStream
+ .range(0, numShards)
+ .filter(shard -> isShardOwned[shard])
+ .forEach(shard -> chatRoomData[shard]
+ .values()
+ .forEach(chatRoomData -> chatRoomData.activate()));
+ }
+
int[] getOwnedShards()
{
void createChatRoomData(ChatRoomInfo chatRoomInfo)
{
- computeChatRoomData(chatRoomInfo.getId(), chatRoomInfo.getShard());
+ int shard = chatRoomInfo.getShard();
+
+ ChatRoomData chatRoomData = computeChatRoomData(
+ chatRoomInfo.getId(),
+ chatRoomInfo.getShard());
+
+ // TODO: Possible race-condition in case of an ongoing rebalance!
+ if (isShardOwned[shard])
+ {
+ chatRoomData.activate();
+ }
}
Mono<ChatRoomData> getChatRoomData(int shard, UUID id)
package de.juplo.kafka.chat.backend.implementation.kafka;
import de.juplo.kafka.chat.backend.domain.ChatMessageService;
-import de.juplo.kafka.chat.backend.domain.Message;import lombok.RequiredArgsConstructor;
+import de.juplo.kafka.chat.backend.domain.Message;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class KafkaChatMessageService implements ChatMessageService
{
private final DataChannel dataChannel;
+ @Getter
private final UUID chatRoomId;
private final LinkedHashMap<Message.MessageKey, Message> messages = new LinkedHashMap<>();