ryerraguntla commented on code in PR #4258:
URL: https://github.com/apache/iggy/pull/4258#discussion_r4077904722
##########
gateways/kafka/src/protocol/handlers/metadata.rs:
##########
@@ -53,23 +73,203 @@ 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) => topics.into_iter().map(found_result).collect(),
+ 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) => {
+ let distinct_names: HashSet<&str> =
names.iter().map(StrBytes::as_str).collect();
+ if distinct_names.len() > MAX_BRIDGE_BACKED_TOPICS {
+ tracing::warn!(
+ distinct_topics = distinct_names.len(),
+ max = MAX_BRIDGE_BACKED_TOPICS,
+ "Metadata request addresses too many distinct topics;
rejecting"
+ );
+ names
+ .iter()
+ .map(|name| error_result(name.clone(),
ERROR_INVALID_REQUEST))
+ .collect()
+ } else {
+ match tokio::time::timeout(REQUEST_DEADLINE,
resolve_named_topics(bridge, &names))
+ .await
+ {
+ Ok(results) => results,
+ Err(_elapsed) => {
+ tracing::warn!(
+ distinct_topics = distinct_names.len(),
+ deadline_secs = REQUEST_DEADLINE.as_secs(),
+ "Metadata request's aggregate bridge work exceeded
its deadline; \
+ answering retriable instead of blocking further"
+ );
+ names
+ .iter()
+ .map(|name| error_result(name.clone(),
ERROR_REQUEST_TIMED_OUT))
+ .collect()
+ }
+ }
+ }
}
+ };
+
+ // `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, or one `list_kafka_topics()` call, can
still expand into a
+ // response `bounds_guard` never had the information to price in before
this bridge round
+ // trip returned. Checked here, before `encode_real_response` builds one
+ // `MetadataResponsePartition` per partition, not after - the expensive
part is building that
+ // `Vec`, not encoding the bytes that follow it.
+ let total_partitions: usize = results
+ .iter()
+ .filter(|result| result.error_code == ERROR_NONE)
+ .map(|result| result.partitions_count as usize)
+ .sum();
+ if response_would_exceed_frame_size(total_partitions,
state.max_frame_size) {
+ tracing::warn!(
+ total_partitions,
+ max_frame_size = state.max_frame_size,
+ "Metadata response would exceed max_frame_size; closing connection"
+ );
+ return HandleOutcome::Close;
Review Comment:
All-topics Metadata response now truncates to fit max_frame_size (keeping
whole topics, listing order) instead of closing the connection — closing was a
permanent, cluster-size-triggered outage for every client's bootstrap call.
Named-lookup path still closes (unchanged, client-bounded via the 100-topic
cap).
--
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]