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


##########
gateways/kafka/src/group/mod.rs:
##########
@@ -451,6 +489,37 @@ impl GroupCoordinator {
         state::leave_step(&mut groups, request, Instant::now())
     }
 
+    /// Snapshot of each distinct group, in first-seen order. `None` means the 
group is not here.
+    ///
+    /// Ids are deduped before the lock is taken. Under the lock each 
remaining group is ticked
+    /// once and, when the encoded response fits `max_frame_size`, snapshotted 
once. Encoding
+    /// happens after this returns. `ExceedsFrame` means the lengths already 
stored would not
+    /// fit, and no member snapshot was built.
+    pub async fn describe_groups(
+        &self,
+        group_ids: &[StrBytes],
+        version: i16,
+        max_frame_size: usize,
+    ) -> DescribeGroupsView {
+        let mut groups = self.groups.lock().await;
+        state::describe_groups(
+            &mut groups,
+            group_ids,
+            version,
+            max_frame_size,
+            Instant::now(),
+        )
+    }
+
+    /// Every group currently in the map, ordered by group id.
+    ///
+    /// Does not expire sessions. Expiry runs on the request that names a 
group; a list names
+    /// none, and sweeping every group here would open rebalances as a side 
effect of reading.
+    pub async fn list_groups(&self) -> Vec<GroupListing> {

Review Comment:
   fixed



##########
gateways/kafka/src/protocol/handlers/list_groups.rs:
##########
@@ -0,0 +1,196 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! `ListGroups` (API key 16).
+//!
+//! Lists the groups this coordinator holds. v4+ `states_filter` and v5 
`types_filter` keep a
+//! group only when the filter is empty or one entry matches, 
case-insensitively, after trim.
+//! A filter value this coordinator does not use matches nothing, so the group 
is left out and
+//! the response error stays 0. Kafka 4.0's `listGroups` does the same ("if 
invalid, no groups
+//! are returned").
+
+use bytes::Bytes;
+use kafka_protocol::messages::list_groups_response::ListedGroup;
+use kafka_protocol::messages::{GroupId, ListGroupsRequest, ListGroupsResponse};
+use kafka_protocol::protocol::StrBytes;
+
+use crate::error::Result;
+use crate::group::GroupListing;
+use crate::protocol::api::{
+    API_KEY_LIST_GROUPS, ApiVersionRange, ERROR_INVALID_REQUEST, 
ERROR_UNSUPPORTED_VERSION,
+    GatewayState, HandleOutcome, is_supported_version,
+};
+use crate::protocol::bounds_guard::validate_list_groups_shape;
+use crate::protocol::handlers::{
+    decode_guarded, encode_message, respond_or_close, 
unsupported_version_response,
+};
+use crate::protocol::header::response_header_version;
+use crate::protocol::wire_len::{kafka_compact_prefix, kafka_string_len};
+
+pub const RANGE: ApiVersionRange = ApiVersionRange {
+    api_key: API_KEY_LIST_GROUPS,
+    min_version: 0,
+    max_version: 5,
+};
+
+/// `ListGroups` v5 `group_type` for the classic protocol this coordinator 
runs.
+const CLASSIC_GROUP_TYPE: &str = "classic";
+
+pub async fn handle(state: &GatewayState, api_version: i16, body: Bytes) -> 
HandleOutcome {
+    if !is_supported_version(API_KEY_LIST_GROUPS, api_version) {
+        return unsupported_version_response(API_KEY_LIST_GROUPS, api_version, 
|version| {
+            encode_error_response(version, ERROR_UNSUPPORTED_VERSION)
+        });
+    }
+    let request = match decode_guarded::<ListGroupsRequest>(api_version, body, 
|version, body| {
+        validate_list_groups_shape(version, body, state.max_frame_size)
+    }) {
+        Ok(request) => request,
+        Err(error) => {
+            // debug!, not warn!: attacker-controlled, not operator-actionable.
+            tracing::debug!(%error, api_version, "Failed to decode ListGroups 
request");
+            return respond_or_close(
+                encode_error_response(api_version, ERROR_INVALID_REQUEST),
+                "ListGroups",
+            );
+        }
+    };
+
+    let listings: Vec<GroupListing> = state
+        .groups
+        .list_groups()
+        .await
+        .into_iter()
+        .filter(|group| {
+            selected(&request.states_filter, group.state)
+                && selected(&request.types_filter, CLASSIC_GROUP_TYPE)
+        })
+        .collect();
+    // `protocol_type` is echoed for every group. The shape guard never sees 
that list, so a
+    // coordinator holding many long types can still outgrow the frame - 
truncate rather than
+    // close, since this is server-sized state, not a request the client chose 
(same approach as
+    // `metadata::truncate_all_topics_to_frame_budget` for the identical 
problem).
+    let listings = truncate_listings_to_frame_budget(api_version, listings, 
state.max_frame_size);
+    let groups = listings.into_iter().map(listed_group).collect();
+    respond_or_close(encode_response(api_version, groups), "ListGroups")
+}
+
+fn selected(filter: &[StrBytes], value: &str) -> bool {
+    filter.is_empty()
+        || filter
+            .iter()
+            .any(|wanted| wanted.as_str().trim().eq_ignore_ascii_case(value))
+}
+
+/// Keeps as many listed groups as fit `max_frame_size`, dropping the rest 
rather than closing
+/// the connection - same approach as 
`metadata::truncate_all_topics_to_frame_budget` for the
+/// identical problem: this coordinator's own state, not a request the client 
chose, is what
+/// grew the response past the budget.
+fn truncate_listings_to_frame_budget(
+    version: i16,
+    mut listings: Vec<GroupListing>,
+    max_frame_size: usize,
+) -> Vec<GroupListing> {
+    let mut cumulative_bytes = list_response_frame_len(version, &[]);
+    let mut keep = listings.len();
+    for (index, group) in listings.iter().enumerate() {
+        let next = cumulative_bytes + listed_group_len(version, group);
+        if next > max_frame_size {
+            keep = index;
+            break;
+        }
+        cumulative_bytes = next;
+    }
+    if keep < listings.len() {
+        tracing::warn!(
+            total_groups = listings.len(),
+            kept_groups = keep,
+            max_frame_size,
+            "ListGroups response would exceed max_frame_size; truncating 
rather than closing \
+             the connection"
+        );
+        listings.truncate(keep);
+    }
+    listings
+}
+
+/// Header plus body, the length `send_response` writes and `read_frame` 
compares to
+/// `max_frame_size`.
+fn list_response_frame_len(version: i16, groups: &[GroupListing]) -> usize {

Review Comment:
   fixed



-- 
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