From: Kai Moritz Date: Sat, 3 Feb 2024 14:32:25 +0000 (+0100) Subject: refactor: compute `loadInProgress` on offset-change X-Git-Tag: rebase--2024-02-20--10-29~19 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=56cb19c83265e6007668e2747221ac47b1388732;p=demos%2Fkafka%2Fchat refactor: compute `loadInProgress` on offset-change --- 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..e6c18c77 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 @@ -5,6 +5,7 @@ import de.juplo.kafka.chat.backend.implementation.kafka.messages.AbstractMessage 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; @@ -38,6 +39,8 @@ public class InfoChannel implements Runnable private final String instanceUri; private boolean running; + @Getter + private volatile boolean loadInProgress = true; public InfoChannel( @@ -68,13 +71,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,6 +185,7 @@ public class InfoChannel implements Runnable IntStream .range(0, numShards) .forEach(partition -> this.nextOffset[partition] = 0l); + loadInProgress = true; while (running) { @@ -215,6 +212,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)