@Slf4j
public class DataChannel implements Runnable, ConsumerRebalanceListener
{
+ private final String instanceId;
private final String topic;
private final Producer<String, AbstractMessageTo> producer;
private final Consumer<String, AbstractMessageTo> consumer;
public DataChannel(
+ String instanceId,
String topic,
Producer<String, AbstractMessageTo> producer,
Consumer<String, AbstractMessageTo> dataChannelConsumer,
InfoChannel infoChannel)
{
log.debug(
- "Creating DataChannel for topic {} with {} partitions",
+ "{}: Creating DataChannel for topic {} with {} partitions",
+ instanceId,
topic,
numShards);
+ this.instanceId = instanceId;
this.topic = topic;
this.consumer = dataChannelConsumer;
this.producer = producer;
producer.send(record, ((metadata, exception) ->
{
- if (metadata != null)
+ if (exception == null)
{
// On successful send
Message message = new Message(key, metadata.offset(), timestamp, text);
currentOffset);
consumer.seek(topicPartition, nextOffset[partition]);
+ infoChannel.sendShardAssignedEvent(partition);
});
consumer.resume(partitions);
int partition = topicPartition.partition();
isShardOwned[partition] = false;
log.info("Partition revoked: {} - next={}", partition, nextOffset[partition]);
+ infoChannel.sendShardRevokedEvent(partition);
});
}
Message.MessageKey key = Message.MessageKey.of(chatMessageTo.getUser(), chatMessageTo.getId());
Message message = new Message(key, offset, timestamp, chatMessageTo.getText());
- ChatRoomData chatRoomData = this.chatRoomData[partition].computeIfAbsent(
- chatRoomId,
- (id) ->
- {
- log.info("Creating ChatRoom {} with buffer-size {}", id, bufferSize);
- KafkaChatMessageService service = new KafkaChatMessageService(this, id);
- return new ChatRoomData(clock, service, bufferSize);
- });
+ ChatRoomData chatRoomData = this
+ .chatRoomData[partition]
+ .computeIfAbsent(chatRoomId, this::computeChatRoomData);
KafkaChatMessageService kafkaChatRoomService =
(KafkaChatMessageService) chatRoomData.getChatRoomService();
if (!isShardOwned[shard])
{
- return Mono.error(new ShardNotOwnedException(shard));
+ return Mono.error(new ShardNotOwnedException(instanceId, shard));
}
return infoChannel
.getChatRoomInfo(id)
- .map(chatRoomInfo -> chatRoomData[shard].computeIfAbsent(
- id,
- (chatRoomId) ->
- {
- log.info("Creating ChatRoom {} with buffer-size {}", chatRoomId, bufferSize);
- KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
- return new ChatRoomData(clock, service, bufferSize);
- }));
+ .map(chatRoomInfo ->
+ chatRoomData[shard].computeIfAbsent(id, this::computeChatRoomData));
+ }
+
+ private ChatRoomData computeChatRoomData(UUID chatRoomId)
+ {
+ log.info("Creating ChatRoom {} with buffer-size {}", chatRoomId, bufferSize);
+ KafkaChatMessageService service = new KafkaChatMessageService(this, chatRoomId);
+ return new ChatRoomData(clock, service, bufferSize);
}
}