Skip to content
Closed
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 @@ -322,10 +322,21 @@ public void close() {
}
}
finally {
this.scheduler = null;
this.metadataConsumers.values().forEach(Consumer::close);
this.metadataConsumers.clear();
this.consumerFactoryLock.unlock();
try {
this.scheduler = null;
this.metadataConsumers.forEach((group, consumer) -> {
try {
consumer.close();
}
catch (Exception ex) {
LOG.warn("Failed to close the metadata consumer for group: " + group, ex);
}
});
this.metadataConsumers.clear();
}
finally {
this.consumerFactoryLock.unlock();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -56,6 +57,7 @@
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -434,6 +436,49 @@ void closeDoesNotDeadlockWithFirstOffsetLagComputation() throws Exception {
assertThat(metadataConsumers).isEmpty();
}

@Test
void closeReleasesConsumerFactoryLockWhenMetadataConsumerCloseFails() throws Exception {
topicsInUse.put(
TEST_TOPIC,
new TopicInformation("group8-metrics", partitions(new Node(0, null, 0)), false)
);
topicsInUse.put(
"test2",
new TopicInformation("group9-metrics", partitions(new Node(0, null, 0)), false)
);
KafkaConsumer failingConsumer = mock(KafkaConsumer.class);
Mockito.doThrow(new KafkaException("close failed")).when(failingConsumer).close();
org.mockito.BDDMockito
.given(consumerFactory.createConsumer(
ArgumentMatchers.eq("group9-metrics"), ArgumentMatchers.any()))
.willReturn(failingConsumer);

metrics.bindTo(meterRegistry);
meterRegistry.get(KafkaBinderMetrics.OFFSET_LAG_METRIC_NAME)
.tag("group", "group8-metrics").tag("topic", TEST_TOPIC).gauge().value();
meterRegistry.get(KafkaBinderMetrics.OFFSET_LAG_METRIC_NAME)
.tag("group", "group9-metrics").tag("topic", "test2").gauge().value();
Map<?, ?> metadataConsumers = TestUtils.getPropertyValue(metrics, "metadataConsumers", Map.class);
assertThat(metadataConsumers).hasSize(2);

Throwable closeFailure = catchThrowable(metrics::close);

ReentrantLock lock = TestUtils.getPropertyValue(metrics, "consumerFactoryLock", ReentrantLock.class);
boolean acquiredByAnotherThread = CompletableFuture.supplyAsync(() -> {
boolean acquired = lock.tryLock();
if (acquired) {
lock.unlock();
}
return acquired;
}).get(10, TimeUnit.SECONDS);
assertThat(acquiredByAnotherThread)
.as("consumerFactoryLock is still held after close()")
.isTrue();
assertThat(closeFailure).isNull();
Mockito.verify(consumer).close();
assertThat(metadataConsumers).isEmpty();
}

private List<PartitionInfo> partitions(Node... nodes) {
List<PartitionInfo> partitions = new ArrayList<>();
for (int i = 0; i < nodes.length; i++) {
Expand Down
Loading