X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fimplementation%2Fkafka%2FInfoChannel.java;h=a6351d0ff52354241c57bf63b3a68c9956682d0a;hb=13f86063f851fc2c4ad6de56c8edb78bff9d0592;hp=3fe15c44a61410f17a8b2723c92e77fb2e104cce;hpb=2eac9d547bb7c43cd7ff3ae1cb7cb98ee9b5929a;p=demos%2Fkafka%2Fchat diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java index 3fe15c44..a6351d0f 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java @@ -1,10 +1,12 @@ package de.juplo.kafka.chat.backend.implementation.kafka; import de.juplo.kafka.chat.backend.domain.ChatRoomInfo; +import de.juplo.kafka.chat.backend.domain.exceptions.LoadInProgressException; import de.juplo.kafka.chat.backend.implementation.kafka.messages.AbstractMessageTo; import de.juplo.kafka.chat.backend.implementation.kafka.messages.info.EventChatRoomCreated; import de.juplo.kafka.chat.backend.implementation.kafka.messages.info.EventShardAssigned; import de.juplo.kafka.chat.backend.implementation.kafka.messages.info.EventShardRevoked; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; @@ -30,6 +32,7 @@ public class InfoChannel implements Runnable private final String topic; private final Producer producer; private final Consumer consumer; + private final Duration pollingInterval; private final int numShards; private final String[] shardOwners; private final long[] currentOffset; @@ -38,12 +41,16 @@ public class InfoChannel implements Runnable private final String instanceUri; private boolean running; + @Getter + private volatile boolean loadInProgress = true; public InfoChannel( String topic, Producer producer, Consumer infoChannelConsumer, + Duration pollingInterval, + int numShards, URI instanceUri) { log.debug( @@ -54,9 +61,9 @@ public class InfoChannel implements Runnable this.producer = producer; this.chatRoomInfo = new HashMap<>(); - this.numShards = consumer - .partitionsFor(topic) - .size(); + this.pollingInterval = pollingInterval; + + this.numShards = numShards; this.shardOwners = new String[numShards]; this.currentOffset = new long[numShards]; this.nextOffset = new long[numShards]; @@ -68,13 +75,6 @@ public class InfoChannel implements Runnable } - boolean isLoadInProgress() - { - return IntStream - .range(0, numShards) - .anyMatch(partition -> nextOffset[partition] < currentOffset[partition]); - } - Mono sendChatRoomCreatedEvent( UUID chatRoomId, String name, @@ -189,12 +189,13 @@ public class InfoChannel implements Runnable IntStream .range(0, numShards) .forEach(partition -> this.nextOffset[partition] = 0l); + loadInProgress = true; while (running) { try { - ConsumerRecords records = consumer.poll(Duration.ofMinutes(1)); + ConsumerRecords records = consumer.poll(pollingInterval); log.debug("Fetched {} messages", records.count()); for (ConsumerRecord record : records) { @@ -215,6 +216,11 @@ public class InfoChannel implements Runnable private void updateNextOffset(int partition, long nextOffset) { this.nextOffset[partition] = nextOffset; + if (loadInProgress) { + loadInProgress = IntStream + .range(0, numShards) + .anyMatch(shard -> this.nextOffset[shard] < currentOffset[partition]); + } } private void handleMessage(ConsumerRecord record) @@ -286,6 +292,11 @@ public class InfoChannel implements Runnable Mono getChatRoomInfo(UUID id) { + if (loadInProgress) + { + return Mono.error(new LoadInProgressException()); + } + return Mono.fromSupplier(() -> chatRoomInfo.get(id)); }