- @Override
- public void onPartitionsLost(Collection<TopicPartition> partitions)
- {
- log.warn("Lost partitions: {}, partitions");
- // TODO: Muss auf den Verlust anders reagiert werden?
- onPartitionsRevoked(partitions);
- }
-
- @Override
- public void run()
- {
- chatMessagesConsumer.subscribe(List.of(chatMessagesTopic));
-
- running = true;
-
- while (running)
- {
- try
- {
- ConsumerRecords<String, MessageTo> records = chatMessagesConsumer.poll(Duration.ofMinutes(5));
- log.info("Fetched {} messages", records.count());
-
- if (loadInProgress)
- {
- loadMessages(records);
-
- if (isLoadingCompleted())
- {
- log.info("Loading of messages completed! Pausing all owned partitions...");
- pauseAllOwnedPartions();
- log.info("Resuming normal operations...");
- loadInProgress = false;
- }
- }
- else
- {
- if (!records.isEmpty())
- {
- throw new IllegalStateException("All owned partitions should be paused, when no load is in progress!");
- }
- }
- }
- catch (WakeupException e)
- {
- }
- catch (RecordDeserializationException e)
- {
- }
- }
- }
-
- void loadMessages(ConsumerRecords<String, MessageTo> records)
- {
- for (ConsumerRecord<String, MessageTo> record : records)
- {
- nextOffset[record.partition()] = record.offset() + 1;
- UUID chatRoomId = UUID.fromString(record.key());
- MessageTo messageTo = record.value();
-
- Message.MessageKey key = Message.MessageKey.of(messageTo.getUser(), messageTo.getId());
-
- Instant instant = Instant.ofEpochSecond(record.timestamp());
- LocalDateTime timestamp = LocalDateTime.ofInstant(instant, zoneId);
-
- Message message = new Message(key, record.offset(), timestamp, messageTo.getText());
-
- ChatRoom chatRoom = chatrooms[record.partition()].get(chatRoomId);
- KafkaChatRoomService kafkaChatRoomService =
- (KafkaChatRoomService) chatRoom.getChatRoomService();
-
- kafkaChatRoomService.persistMessage(message);
- }
- }
-
- boolean isLoadingCompleted()
- {
- return IntStream
- .range(0, numShards)
- .filter(shard -> isShardOwned[shard])
- .mapToObj(shard -> nextOffset[shard] >= currentOffset[shard])
- .collect(
- () -> Boolean.TRUE,
- (acc, v) -> Boolean.valueOf(acc && v),
- (a, b) -> Boolean.valueOf(a && b));
- }
-
- void pauseAllOwnedPartions()
- {
- chatMessagesConsumer.pause(IntStream
- .range(0, numShards)
- .filter(shard -> isShardOwned[shard])
- .mapToObj(shard -> new TopicPartition(chatMessagesTopic, shard))
- .toList());
- }
-
- Mono<Message> sendMessage(
- UUID chatRoomId,
- Message.MessageKey key,
- LocalDateTime timestamp,
- String text)
- {
- int shard = this.shardingStrategy.selectShard(chatRoomId);
- TopicPartition tp = new TopicPartition(chatMessagesTopic, shard);
- ZonedDateTime zdt = ZonedDateTime.of(timestamp, zoneId);
- return Mono.create(sink ->
- {
- ProducerRecord<String, MessageTo> record =
- new ProducerRecord<>(
- tp.topic(),
- tp.partition(),
- zdt.toEpochSecond(),
- chatRoomId.toString(),
- MessageTo.of(key.getUsername(), key.getMessageId(), text));
-
- chatMessagesProducer.send(record, ((metadata, exception) ->
- {
- if (metadata != null)
- {
- // On successful send
- Message message = new Message(key, metadata.offset(), timestamp, text);
- log.info("Successfully send message {}", message);
- sink.success(message);
- }
- else
- {
- // On send-failure
- log.error(
- "Could not send message for chat-room={}, key={}, timestamp={}, text={}: {}",
- chatRoomId,
- key,
- timestamp,
- text,
- exception);
- sink.error(exception);
- }
- }));
- });
- }
-
-
- public void putChatRoom(ChatRoom chatRoom)
- {
-
- ProducerRecord<Integer, ChatRoomTo> record = new ProducerRecord<>(chatRoom.getShard(), );
- // TODO: Nachricht senden!
- chatrooms[chatRoom.getShard()].put(chatRoom.getId(), chatRoom);
- }