Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.errors.RebalanceInProgressException;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.TopicPartition;
Expand Down Expand Up @@ -354,11 +355,15 @@ private void commitOffsets(boolean forceCommit) {
offsetsToCommit.forEach(((partition, offset) -> updateCommitCountMetric(partition, offset)));
try {
consumer.commitSync(offsetsToCommit);
lastCommitTime = currentTimeMillis;
} catch (final RebalanceInProgressException ex) {
LOG.error("Failed to commit offsets in topic {} due to rebalance in progress", topicName, ex);
return;
} catch (Exception e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, this exception set the lastCommitTime. But, now we don't. Is that ok?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it was incorrectly saying we committed offsets when we didn't if we get an exception

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dlvenable it is correct because the last commit time should be set only if commit really happens.

LOG.error("Failed to commit offsets in topic {}", topicName, e);
}

offsetsToCommit.clear();
lastCommitTime = currentTimeMillis;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.RebalanceInProgressException;
import org.apache.kafka.common.errors.RecordDeserializationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -60,6 +61,8 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -589,6 +592,88 @@ public void testAwsGlueErrorWithAcknowledgements() throws Exception {
});
}

@Test
public void testCommitOffsets_RebalanceInProgressException_DoesNotClearOffsets() throws Exception {
String topic = topicConfig.getName();
TopicPartition topicPartition = new TopicPartition(topic, testPartition);

when(topicConfig.getSerdeFormat()).thenReturn(MessageFormat.PLAINTEXT);
when(topicConfig.getAutoCommit()).thenReturn(false);
when(topicConfig.getCommitInterval()).thenReturn(Duration.ofMillis(0));

consumer = createObjectUnderTest("plaintext", false);
consumer.onPartitionsAssigned(List.of(topicPartition));

consumerRecords = createPlainTextRecords(topic, 100L);
when(kafkaConsumer.poll(any(Duration.class))).thenReturn(consumerRecords);

doThrow(new RebalanceInProgressException("Rebalance in progress"))
.when(kafkaConsumer).commitSync(anyMap());

consumer.consumeRecords();

Map<TopicPartition, OffsetAndMetadata> offsetsBeforeCommit = new HashMap<>(consumer.getOffsetsToCommit());
Assertions.assertFalse(offsetsBeforeCommit.isEmpty(), "Offsets should be populated after consuming records");
Assertions.assertEquals(102L, offsetsBeforeCommit.get(topicPartition).offset());

Thread testThread = new Thread(() -> {
try {
java.lang.reflect.Method method = consumer.getClass().getDeclaredMethod("commitOffsets", boolean.class);
method.setAccessible(true);
method.invoke(consumer, true);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
testThread.start();
testThread.join(5000);

Map<TopicPartition, OffsetAndMetadata> offsetsAfterFailedCommit = consumer.getOffsetsToCommit();
Assertions.assertFalse(offsetsAfterFailedCommit.isEmpty(),
"Offsets should NOT be cleared after RebalanceInProgressException");
Assertions.assertEquals(offsetsBeforeCommit.get(topicPartition).offset(),
offsetsAfterFailedCommit.get(topicPartition).offset(),
"Offset value should remain unchanged for retry after rebalance completes");
}

@Test
public void testCommitOffsets_OtherException_ClearsOffsets() throws Exception {
String topic = topicConfig.getName();
TopicPartition topicPartition = new TopicPartition(topic, testPartition);

when(topicConfig.getAutoCommit()).thenReturn(false);
when(topicConfig.getCommitInterval()).thenReturn(Duration.ofMillis(0));

consumer = createObjectUnderTest("plaintext", false);
consumer.onPartitionsAssigned(List.of(topicPartition));

consumerRecords = createPlainTextRecords(topic, 100L);
when(kafkaConsumer.poll(any(Duration.class))).thenReturn(consumerRecords);

doThrow(new RuntimeException("Generic commit failure"))
.when(kafkaConsumer).commitSync(anyMap());

consumer.consumeRecords();

Assertions.assertFalse(consumer.getOffsetsToCommit().isEmpty(),
"Offsets should be populated after consuming records");

Thread testThread = new Thread(() -> {
try {
java.lang.reflect.Method method = consumer.getClass().getDeclaredMethod("commitOffsets", boolean.class);
method.setAccessible(true);
method.invoke(consumer, true);
} catch (Exception e) {
}
});
testThread.start();
testThread.join(5000);

Map<TopicPartition, OffsetAndMetadata> offsetsAfterFailedCommit = consumer.getOffsetsToCommit();
Assertions.assertTrue(offsetsAfterFailedCommit.isEmpty(),
"Offsets should be cleared after non-rebalance exception");
}

private ConsumerRecords createPlainTextRecords(String topic, final long startOffset) {
Map<TopicPartition, List<ConsumerRecord>> records = new HashMap<>();
ConsumerRecord<String, String> record1 = new ConsumerRecord<>(topic, testPartition, startOffset, testKey1, testValue1);
Expand Down
Loading