fix(kafka): fall back to topic names when metadata times out

This commit is contained in:
t8y2 committed 2026-08-28 06:11:26 +00:00
1 parent 2467d212d5
commit 17e2573fbe
2 files changed
+41 -1

No files matched your search

@@ -650,9 +650,15 @@ public final class KafkaAgent {
topics.add(topic); topics.add(topic);
} }
} catch (Exception error) { } catch (Exception error) {
if (!isUnsupportedVersionError(error)) { if (!isUnsupportedVersionError(error) && !isTimeoutError(error)) {
throw error; throw error;
} }
if (isTimeoutError(error)) {
logger().warn(
"Kafka topic descriptions timed out; returning topic names without partition metadata",
error
);
}
for (TopicListing listing : listings) { for (TopicListing listing : listings) {
Map<String, Object> topic = new LinkedHashMap<>(); Map<String, Object> topic = new LinkedHashMap<>();
topic.put("name", listing.name()); topic.put("name", listing.name());
@@ -2582,6 +2588,21 @@ public final class KafkaAgent {
return false; return false;
} }
/**
* A topic listing is still useful when the broker cannot describe every topic before the
* request deadline. Keep timeout detection narrow so authorization, transport, and unknown
* topic failures remain visible to the caller instead of being mistaken for a partial result.
*/
private static boolean isTimeoutError(Throwable error) {
for (Throwable current : causeChain(error)) {
if (current instanceof java.util.concurrent.TimeoutException
|| current instanceof org.apache.kafka.common.errors.TimeoutException) {
return true;
}
}
return false;
}
static boolean hasUnsupportedVersionException(Throwable error) { static boolean hasUnsupportedVersionException(Throwable error) {
return causeChain(error).stream().anyMatch(UnsupportedVersionException.class::isInstance); return causeChain(error).stream().anyMatch(UnsupportedVersionException.class::isInstance);
} }
@@ -255,6 +255,25 @@ class KafkaAgentTest {
assertFalse(topics.get(1).containsKey("partitions")); assertFalse(topics.get(1).containsKey("partitions"));
} }
@Test
void topicListingFallsBackWhenDescriptionsTimeOut() throws Exception {
Object result = KafkaAgent.topicListResult(
Arrays.asList(
new TopicListing("orders", Uuid.randomUuid(), false),
new TopicListing("payments", Uuid.randomUuid(), false)
),
names -> {
throw new java.util.concurrent.TimeoutException("describeTopics timed out");
}
);
@SuppressWarnings("unchecked")
List<Map<String, Object>> topics = (List<Map<String, Object>>) ((Map<String, Object>) result).get("topics");
assertEquals(Arrays.asList("orders", "payments"), topics.stream().map(topic -> topic.get("name")).toList());
assertFalse(topics.get(0).containsKey("partitions"));
assertFalse(topics.get(1).containsKey("replicationFactor"));
}
@Test @Test
void topicListingPreservesNonVersionDescriptionErrors() { void topicListingPreservesNonVersionDescriptionErrors() {
IllegalStateException failure = new IllegalStateException("metadata authorization failed"); IllegalStateException failure = new IllegalStateException("metadata authorization failed");