ryerraguntla commented on code in PR #4258:
URL: https://github.com/apache/iggy/pull/4258#discussion_r4093329062


##########
gateways/kafka/src/protocol/handlers/metadata.rs:
##########
@@ -53,23 +73,271 @@ pub async fn handle(state: &GatewayState, api_version: 
i16, body: Bytes) -> Hand
         );
         return HandleOutcome::Close;
     }
-    match decode_topics(api_version, body, state.max_frame_size) {
-        Ok(topics) => respond_or_close(
-            encode_response(api_version, &topics, &state.broker, ERROR_NONE),
-            "Metadata",
-        ),
+
+    let Some(bridge) = &state.bridge else {
+        return match decode_topics(api_version, body, state.max_frame_size) {
+            Ok(topics) => respond_or_close(
+                encode_response(api_version, &topics, &state.broker, 
ERROR_NONE),
+                "Metadata",
+            ),
+            Err(error) => {
+                // Metadata has no top-level error field; a malformed body 
cannot carry
+                // INVALID_REQUEST in a version-correct way for every client. 
Close.
+                // debug!, not warn!: attacker-controlled, not 
operator-actionable.
+                tracing::debug!(
+                    %error,
+                    api_version,
+                    "Failed to decode Metadata request; closing connection"
+                );
+                HandleOutcome::Close
+            }
+        };
+    };
+
+    let requested = match decode_requested_topics(api_version, body, 
state.max_frame_size) {
+        Ok(requested) => requested,
         Err(error) => {
-            // Metadata has no top-level error field; a malformed body cannot 
carry
-            // INVALID_REQUEST in a version-correct way for every client. 
Close.
-            // debug!, not warn!: attacker-controlled, not operator-actionable.
             tracing::debug!(
                 %error,
                 api_version,
                 "Failed to decode Metadata request; closing connection"
             );
-            HandleOutcome::Close
+            return HandleOutcome::Close;
         }
+    };
+
+    let results = match requested {
+        None => match bridge.list_kafka_topics().await {
+            Ok(topics) => {
+                let results: Vec<TopicResult> = 
topics.into_iter().map(found_result).collect();
+                truncate_all_topics_to_frame_budget(results, 
state.max_frame_size)
+            }
+            Err(error) => {
+                // Same "no top-level error field" constraint as a decode 
failure: there is no
+                // way to answer "the bridge itself is unreachable" for an 
all-topics request
+                // that doesn't also falsely claim zero topics exist.
+                tracing::warn!(%error, "Failed to list Kafka topics from the 
Iggy bridge; closing connection");
+                return HandleOutcome::Close;
+            }
+        },
+        Some(names) => resolve_requested_named_topics(bridge, &names).await,
+    };
+
+    // `bounds_guard` cannot see this: it charges the projected response by 
*requested* element
+    // (one topic name), but one real topic can carry up to Iggy's own 
per-topic partition cap
+    // (1000) - a handful of names can still expand into a response 
`bounds_guard` never had the
+    // information to price in before this bridge round trip returned. The 
all-topics arm is
+    // pre-truncated to this same budget above 
(`truncate_all_topics_to_frame_budget`) since its
+    // size is server-side, not client-controllable - closing over it would 
take down every
+    // client's bootstrap Metadata call, permanently, the moment the catalog 
grows past the
+    // trip point. This check is what still enforces the budget for the 
named-lookup arm, where
+    // the cap (100 distinct topics) bounds the request but not what each one 
costs to answer.
+    let total_bytes: usize = 
results.iter().map(estimated_response_bytes).sum();
+    if response_would_exceed_frame_size(total_bytes, state.max_frame_size) {
+        tracing::warn!(
+            total_bytes,
+            max_frame_size = state.max_frame_size,
+            "Metadata response would exceed max_frame_size; closing connection"
+        );
+        return HandleOutcome::Close;
     }
+
+    respond_or_close(
+        encode_real_response(api_version, &results, &state.broker),
+        "Metadata",
+    )
+}
+
+/// Resolves a named-lookup Metadata request's topics: dedupes first so every 
path below (cap,
+/// deadline, success) builds its response from the distinct set rather than 
one entry per
+/// request occurrence - real Kafka answers a topic named more than once with 
one response entry,
+/// not one per repeat, and re-expanding to match the request let a handful of 
repeats of one
+/// large topic name amplify a response sized off the repeat count instead of 
the distinct count.
+async fn resolve_requested_named_topics(
+    bridge: &IggyBridge,
+    names: &[StrBytes],
+) -> Vec<TopicResult> {
+    let distinct = dedup_topic_names(names);
+    if distinct.len() > MAX_BRIDGE_BACKED_TOPICS {

Review Comment:
   removed. Added IggyBridge::get_kafka_topics, which batches by the stream 
each name resolves to rather than by name count — a long-lived Java producer 
whose ProducerMetadata cumulative topic set crosses 100 no longer gets 
permanently bricked.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to