From 2eac9d547bb7c43cd7ff3ae1cb7cb98ee9b5929a Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 3 Feb 2024 15:20:25 +0100 Subject: [PATCH] refactor: separated message- and record- (aka offset-) handling --- .../implementation/kafka/InfoChannel.java | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 deletions(-) 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 3429d405..3fe15c44 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 @@ -196,7 +196,11 @@ public class InfoChannel implements Runnable { ConsumerRecords records = consumer.poll(Duration.ofMinutes(1)); log.debug("Fetched {} messages", records.count()); - handleMessages(records); + for (ConsumerRecord record : records) + { + handleMessage(record); + updateNextOffset(record.partition(), record.offset() + 1); + } } catch (WakeupException e) { @@ -208,47 +212,47 @@ public class InfoChannel implements Runnable log.info("Exiting normally"); } - private void handleMessages(ConsumerRecords records) + private void updateNextOffset(int partition, long nextOffset) { - for (ConsumerRecord record : records) - { - switch (record.value().getType()) - { - case EVENT_CHATROOM_CREATED: - EventChatRoomCreated eventChatRoomCreated = - (EventChatRoomCreated) record.value(); - createChatRoom(eventChatRoomCreated.toChatRoomInfo()); - break; - - case EVENT_SHARD_ASSIGNED: - EventShardAssigned eventShardAssigned = - (EventShardAssigned) record.value(); - log.info( - "Shard {} was assigned to {}", - eventShardAssigned.getShard(), - eventShardAssigned.getUri()); - shardOwners[eventShardAssigned.getShard()] = eventShardAssigned.getUri(); - break; - - case EVENT_SHARD_REVOKED: - EventShardRevoked eventShardRevoked = - (EventShardRevoked) record.value(); - log.info( - "Shard {} was revoked from {}", - eventShardRevoked.getShard(), - eventShardRevoked.getUri()); - shardOwners[eventShardRevoked.getShard()] = null; - break; - - default: - log.debug( - "Ignoring message for key={} with offset={}: {}", - record.key(), - record.offset(), - record.value()); - } + this.nextOffset[partition] = nextOffset; + } - nextOffset[record.partition()] = record.offset() + 1; + private void handleMessage(ConsumerRecord record) + { + switch (record.value().getType()) + { + case EVENT_CHATROOM_CREATED: + EventChatRoomCreated eventChatRoomCreated = + (EventChatRoomCreated) record.value(); + createChatRoom(eventChatRoomCreated.toChatRoomInfo()); + break; + + case EVENT_SHARD_ASSIGNED: + EventShardAssigned eventShardAssigned = + (EventShardAssigned) record.value(); + log.info( + "Shard {} was assigned to {}", + eventShardAssigned.getShard(), + eventShardAssigned.getUri()); + shardOwners[eventShardAssigned.getShard()] = eventShardAssigned.getUri(); + break; + + case EVENT_SHARD_REVOKED: + EventShardRevoked eventShardRevoked = + (EventShardRevoked) record.value(); + log.info( + "Shard {} was revoked from {}", + eventShardRevoked.getShard(), + eventShardRevoked.getUri()); + shardOwners[eventShardRevoked.getShard()] = null; + break; + + default: + log.debug( + "Ignoring message for key={} with offset={}: {}", + record.key(), + record.offset(), + record.value()); } } -- 2.20.1