mvanhorn opened a new pull request, #21714:
URL: https://github.com/apache/kafka/pull/21714

   When re-authentication fails in the consumer (e.g., temporary OAuth token 
server unavailability with SASL_OAUTHBEARER), the heartbeat timer is left in an 
expired state. This causes `timeToNextHeartbeat()` to return 0, leading the 
consumer poll loop to busy-wait with a 0ms timeout - pinning a CPU core at 100% 
(ClassicKafkaConsumer) or 40-50% (AsyncKafkaConsumer).
   
   ## Changes
   
   ### ClassicKafkaConsumer (AbstractCoordinator heartbeat thread)
   
   The heartbeat thread's `catch (AuthenticationException)` and `catch 
(GroupAuthorizationException)` blocks call `setFailureCause()` but never call 
`heartbeat.failHeartbeat()`. This leaves the heartbeat timer expired. After the 
user catches the exception and calls `poll()` again, `timeToNextHeartbeat()` 
returns 0, causing the main thread to spin.
   
   **Fix:** Call `heartbeat.failHeartbeat()` before `setFailureCause()` in both 
exception handlers. This resets the heartbeat timer with exponential backoff, 
preventing the 0-timeout spin.
   
   ### AsyncKafkaConsumer (AbstractHeartbeatRequestManager + 
StreamsGroupHeartbeatRequestManager)
   
   `maximumTimeToWait()` returns 0 when `shouldHeartbeatNow()` is true, even if 
the heartbeat request is in backoff (can't actually be sent). This causes the 
background network thread to spin.
   
   **Fix:** Check `canSendRequest()` before returning 0. If the request is in 
backoff, return the remaining backoff time instead.
   
   ## Testing
   
   - Added `HeartbeatTest.testFailHeartbeatResetsTimerWithBackoff()` - verifies 
that `failHeartbeat()` resets the timer to a positive backoff value after expiry
   - Added 
`ConsumerHeartbeatRequestManagerTest.testMaximumTimeToWaitRespectsBackoffWhenHeartbeatNow()`
 - verifies that `maximumTimeToWait()` returns a positive value when 
`shouldHeartbeatNow()` is true but the request is in backoff
   
   ## Reproducer
   
   See https://github.com/mstruk/kafka-consumer-reproducer for a standalone 
reproducer demonstrating the CPU spin with SASL_OAUTHBEARER.
   
   This contribution was developed with AI assistance (Claude Code).


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