[ 
https://issues.apache.org/jira/browse/NIFI-16632?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xinyu Wang reassigned NIFI-16632:
---------------------------------

    Assignee: Xinyu Wang

> CachingLeaderElectionCommandProvider sends concurrent Lease requests when a 
> cached entry expires
> ------------------------------------------------------------------------------------------------
>
>                 Key: NIFI-16632
>                 URL: https://issues.apache.org/jira/browse/NIFI-16632
>             Project: Apache NiFi
>          Issue Type: Improvement
>          Components: Core Framework
>    Affects Versions: 2.12.0
>            Reporter: Xinyu Wang
>            Assignee: Xinyu Wang
>            Priority: Minor
>
> NIFI-16120 added CachingLeaderElectionCommandProvider, which caches the 
> result of findLeader() for 5 seconds so that isLeader() doesn't hit the 
> Kubernetes API on every call.
> The cache does its job in the steady state, but there is no coordination 
> between threads when an entry is missing or has expired:
> {code:java}
> final CachedLeader cachedLeader = cachedLeaders.get(name);
> if (cachedLeader == null || cachedLeader.isExpired()) {
>     leader = provider.findLeader(name);
>     ...
>     cachedLeaders.put(name, new CachedLeader(...));
> }
> {code}
> # Every thread that calls findLeader() after the entry expired and before the 
> first request has returned also misses the cache and sends its own request. 
> With a 5 second TTL a node should send about 0.2 requests per second for each 
> election name, but the number of requests per expiration isn't capped at one. 
> It grows with the number of threads that call findLeader() while the first 
> request is still running.
> # If provider.findLeader() throws, nothing is cached, so every following 
> caller sends a new request while the Kubernetes API is slow or rate limiting.
> h3. Observed
> We saw this on a 3-node cluster on EKS with 100 timer-driven threads and 
> about 30 Primary-only processors running. With the NIFI-16120 change in 
> place, each non-primary node still sent roughly 4 to 5 Lease GET requests per 
> second (and about 18 to 21 per second when about 290 Primary-only processors 
> were running). These are counts from the Kubernetes API server audit log. The 
> leader elector itself accounts for about 0.5 GET per second for each node and 
> lease, and the cache should add about 0.2 on top of that, so the observed 
> numbers are much too high and point to many concurrent requests on every 
> expiration. {{nifi.sh diagnostics}} reports hundreds of Leader Election polls 
> per second on each node, so the cache is hit very often and the cost sits at 
> the moment of expiration.
> This was a 2.5.0 based build with the NIFI-16120 change. The code is the same 
> in 2.12.0 and main.
> h3. Proposal
> Let concurrent callers of findLeader() for the same election name share one 
> in-flight request:
> * The first caller that finds a missing or expired entry sends the request 
> and stores the result.
> * Other callers for that name wait for the result instead of sending their 
> own request.
> * A failure is passed on to the waiting callers but isn't cached, so the next 
> call after it sends a new request, same as today.
> The TTL stays at 5 seconds, there are no stale values and the public API 
> doesn't change.
> h3. Out of scope
> * How often isLeader() gets called. That depends on how Primary-only 
> processors are scheduled on non-primary nodes, see NIFI-16633.
> * A negative cache or backoff for failures. That could be a follow-up if the 
> maintainers want it.



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

Reply via email to