← Back to Pull requests

feat(share-consumer): consume records with implicit acknowledgement (KIP-932) #545

Closed
unknown closed this pull request without merging on Dec 02, 2026 07:46 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 12:33 UTC
Author

Next part of #480, on top of #536: ShareConsumer.consume() fetches records from a share group and acknowledges them implicitly. Explicit acknowledgements (accept, release, reject, renew) come in the next PR.

What it does

  • consume({ topics }) joins the group if needed and returns a ShareMessagesStream. If the consumer has already joined, the topics must be the same. Only one stream can be open at a time, since the broker keeps one share session per member on each broker.

  • The stream sends ShareFetch to the leader of each assigned partition, one request at a time per broker, and follows the assignment the heartbeats bring. Epoch 0 opens a session. New partitions are added to the session and revoked ones are removed with forgotten_topics_data.

  • Only records inside the acquired ranges are delivered, with their deliveryCount. Offsets of an acquired range without a record (transaction markers, compacted records) are acknowledged as GAP.

  • A record is accepted once the stream hands it to the application, through for await, a data listener, a pipe or read(). The acknowledgement goes with the next ShareFetch to the same broker. While the buffer is full no ShareFetch is sent, so the pending acknowledgements go with a ShareAcknowledge on each heartbeat, before the acquisition locks expire.

  • stream.close() releases the records still in the buffer and closes each session with ShareAcknowledge epoch -1, sending the pending acknowledgements. It waits for the request inflight on the session first, so the acknowledgements of a request which fails are sent with the closing one. A for await loop ends cleanly, and the buffered records never reach it.

  • leaveGroup() fails while a stream is consuming, like Consumer.leaveGroup(). With force it closes the stream first, since the broker refuses the acknowledgements of a member that left. close() works the same way, like Consumer.close(). A stream that a for await loop left with break does not need force.

  • SHARE_SESSION_NOT_FOUND and INVALID_SHARE_SESSION_EPOCH open a new session right away. The acquired records stay locked for the member, not the session, so the pending acknowledgements are sent in the new one. A replaced session is opened even when its broker no longer leads an assigned partition, so its pending acknowledgements are still sent, also when the stream is closed. Other errors, including the errors of single partitions, are retried with retries and retryDelay, or destroy the stream; a partition error which cannot be retried wins over one which can. Partition errors also refresh the metadata.

  • If a topic of the assignment was just deleted, it is left out until the heartbeats bring a new assignment, so the other topics are still fetched and acknowledged. For this the unknown topic UserError of Base now carries the protocol error as cause and the topic name; the message is unchanged.

  • A session is closed once its broker leads no assigned partition anymore and none of its records is buffered or left to acknowledge, so it does not keep a slot in the share session cache.

  • Acknowledgements the broker refuses are emitted as share-consumer:acknowledgement:error with the topic and the partition.

  • New options on the constructor and on consume: deserializers, minBytes, maxBytes, maxWaitTime, maxRecords (500) and highWaterMark (defaults to maxRecords). ShareConsumer takes the same four type parameters as Consumer, with the same defaults.
  • After the reviews

  • Accepting on read is kept.

  • leaveGroup() and close() need force to close a consuming stream, as in Consumer.

  • Rejecting records that cannot be deserialized comes with explicit acknowledgements in the next PR.

  • Records that waited in the buffer longer than the acquisition lock are still delivered. The docs now say that they can reach the application more than once.

  • The fetch options must be integers, and joinGroup() and consume() say when the consumer is still joining.

  • From the second review: acknowledgements are sent with the heartbeats while the buffer is full, closing waits for the request inflight, idle sessions are closed, all partition errors are kept, the deleted topic case is matched exactly, read() alone works, leaveGroup() waits for a forced leave in progress, and there is no any left.

  • From the third review: close() takes force, like Consumer.close(). Neither close() nor leaveGroup() needs it for a stream that a for await loop left with break, which is still closing its sessions.
  • Tests

  • 60 tests with the network mocked: delivery and gaps, acceptance only after reading, release on close, close() from for await, from a data handler and with a readable listener, session errors (including a replaced session whose broker leads nothing anymore), acknowledgements sent with the heartbeats under backpressure, requests failing while the stream closes, idle sessions being closed, retries, partition and acknowledgement errors, assignment changes, several leaders, backpressure, waiting for the membership, ShareFetch and ShareAcknowledge v1 and v2, deserializers, a deleted topic of the assignment, leaveGroup() and close() with and without force, and both after breaking out of a for await loop. They cover 98.5% of the lines and 93.7% of the branches of messages-stream.ts, so the coverage gate holds where the broker test is skipped.

  • One test against the single broker: the first consumer reads 4 of 10 records and closes, and a second consumer gets the other 6 with a delivery count of 2. It passes locally on cp-kafka:8.2.0, and skips itself like the #536 test where share groups are not available.

  • Checked by hand on cp-kafka:8.2.0:

  • - acknowledgements in an epoch 0 request fail with INVALID_REQUEST
    - a partition can be acknowledged and removed from the session in the same request
    - closing a session releases its records
    - an empty epoch 0 session can be opened, and its closing ShareAcknowledge accepts the records acquired by an older session
    - a new epoch 0 session of the same member does not release the records of the old one, and can accept them
    - a transaction marker inside an acquired range is accepted as GAP
    - accepting a record after its lock expired fails with INVALID_RECORD_STATE
    - with highWaterMark: 1, a 15 s lock and one record read out of five, then 17 s of waiting: before this change the record was delivered again to the next member (delivery count 2), now it is accepted in time and is not
    - with three members, a stream that read 10 of 30 records and closed left exactly the other 20 for the next member, and nothing for a third
  • Not run locally: the rest of the suite, which needs the 3-broker cluster, and the other Kafka versions.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: ShogunPanda Review duration: 6912m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #545"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%