Skip to content

Commit

Permalink
fix(kafka): close consumer before retrying on failures (#3018)
Browse files Browse the repository at this point in the history
* fix(kafka): close consumer before retrying on failures

* fix test

(cherry picked from commit 71fe743)
  • Loading branch information
chillleader committed Aug 2, 2024
1 parent 989ab23 commit d7ac96d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.slf4j.LoggerFactory;

public class KafkaConnectorConsumer {

private static final Logger LOG = LoggerFactory.getLogger(KafkaConnectorConsumer.class);

private final InboundConnectorContext context;
Expand Down Expand Up @@ -102,6 +103,14 @@ public void startConsumer() {
return null;
} catch (Exception ex) {
LOG.error("Consumer loop failure, retry pending: {}", ex.getMessage());
try {
consumer.close();
} catch (Exception e) {
LOG.error(
"Failed to close consumer before retrying, reason: {}. "
+ "This error will be ignored. If the consumer is still running, it will be disconnected after max.poll.interval.ms.",
e.getMessage());
}
throw ex;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ void testActivateAndDeactivate_consumerThrows() {
.atMost(Duration.ofSeconds(5))
.pollInterval(Duration.ofMillis(500))
.untilAsserted(() -> assertFalse(kafkaExecutable.kafkaConnectorConsumer.shouldLoop));
kafkaExecutable.deactivate();

// Then
verify(mockConsumer, times(MAX_ATTEMPTS)).poll(any(Duration.class));
verify(mockConsumer, times(MAX_ATTEMPTS)).close();
}

@Test
Expand Down

0 comments on commit d7ac96d

Please sign in to comment.