Tomer Z created FLINK-41683:
-------------------------------

             Summary: SourceReaderBase busy-spins the task thread while waiting 
for a rate limiter permit
                 Key: FLINK-41683
                 URL: https://issues.apache.org/jira/browse/FLINK-41683
             Project: Flink
          Issue Type: Bug
          Components: Connectors / Common
    Affects Versions: 2.2.1, 2.3.0, 2.4.0
            Reporter: Tomer Z


When a source built on {{SourceReaderBase}} is given a {{RateLimiterStrategy}} 
(FLIP-535, FLINK-38497), the task thread spins at 100% of a core while it waits 
for a permit.

h3. Cause
* While {{rateLimitPermissionFuture}} is not done, {{pollNextWithRateLimiting}} 
returns {{InputStatus.MORE_AVAILABLE}} without emitting a record.
* {{isAvailable()}} returns {{AVAILABLE}} whenever {{currentFetch != null}}, so 
it doesn't reflect the pending permit.
* The mailbox loop only suspends on {{NOTHING_AVAILABLE}}, so it calls 
{{pollNext}} again immediately, until the rate limiter's own thread completes 
the future.

h3. Reproduction
A minimal endless source on {{SingleThreadMultiplexSourceReaderBase}}, limited 
with {{RateLimiterStrategy.perSecond(10)}}. Flink 2.2.1, local environment, 
parallelism 1, JDK 17. Per-thread CPU time from {{ThreadMXBean}} over a 10 s 
window after a 5 s warmup:

||How the limit is applied||Records/s||CPU of the source task thread||
|{{RateLimiterStrategy}} passed to the {{SourceReaderBase}} 
constructor|10.0|99.8%|
|Same reader without a strategy, wrapped in {{RateLimitedSourceReader}} 
(FLIP-238)|10.0|0.2%|

The fetcher thread is idle in both cases (the element queue is full), so the 
CPU is the task thread polling.

h3. Suggested fix
Do what {{RateLimitedSourceReader}} already does: while the permit is pending, 
return {{NOTHING_AVAILABLE}}, and have {{isAvailable()}} also wait on 
{{rateLimitPermissionFuture}}.

{{SourceReaderBase}} has not changed on master since FLINK-38497, so 2.3.0 and 
master are affected as well



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to