0xffff-zhiyan opened a new pull request, #20481:
URL: https://github.com/apache/kafka/pull/20481

   This PR implements 
[KIP-1207](https://cwiki.apache.org/confluence/display/KAFKA/KIP-1207%3A+Fix+anomaly+of+JMX+metrics+RequestHandlerAvgIdlePercent+in+kraft+combined+mode)
   
   This PR implements a global shared thread counter mechanism to properly 
calculate the `RequestHandlerAvgIdlePercent` metric across all 
`KafkaRequestHandlerPool` instances within the same JVM process in Kraft 
combined mode. This ensures accurate idle percentage calculations, especially 
in combined KRaft mode where both broker and controller request handler pools 
coexist.
   
   Previously, each `KafkaRequestHandlerPool` calculated idle percentages 
independently using only its own thread count as the denominator. In combined 
KRaft mode, this led to:
   
   - Inaccurate aggregate idle percentage calculations
   - Potential metric values exceeding 100% (values > 1.0)
   - Inconsistent behavior between isolated and combined modes
   
   **Core Changes**
   
   1. Global Thread Counter: `Added sharedAggregateTotalThreads` as a global 
AtomicInteger in `KafkaRequestHandlerPool `
   2. Modified KafkaRequestHandler to calculate two idle metrics:
   Per-pool metric: Uses local thread count (totalHandlerThreads.get)
   Aggregate metric: Uses global thread count (sharedAggregateTotalThreads.get)
   
   **Test**
   Added perPoolIdleMeter parameter to all KafkaRequestHandler instantiations
   Added global counter initialization: 
KafkaRequestHandlerPool.sharedAggregateTotalThreads.set(1) in test class setup
   Added new unit test verifies:
   1.Global counter accumulation across multiple pools
   2.Proper idle percentage calculation within [0, 1.05] range
   3.Counter cleanup after pool shutdown
   
   POC locally: 
   <img width="648" height="270" alt="Screenshot 2025-09-04 at 15 32 32" 
src="https://github.com/user-attachments/assets/6fe03ff5-3cb2-4a6d-af2b-097017c0aadd";
 />
   <img width="651" height="275" alt="Screenshot 2025-09-04 at 15 34 26" 
src="https://github.com/user-attachments/assets/b64a0713-6c99-4997-bebd-f93c634f2242";
 />
   
   


-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to