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

Xinyu Wang updated NIFI-16632:
------------------------------
    Description: 
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.

  was:
NIFI-16120 added CachingLeaderElectionCommandProvider, which caches the result 
of findLeader() for 5 seconds so that 
KubernetesLeaderElectionManager.isLeader() does not send a Kubernetes API 
request on every call.

The cache works for the steady state, but findLeader() has no coordination 
between threads when an entry is missing or 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() between the expiration and the 
completion of the first request also misses the cache and sends its own 
request. With a cache TTL of 5 seconds a single node should send about 0.2 
requests per second for each election name, but the number of requests per 
expiration is not bounded by one; it grows with the number of threads that call 
findLeader() while the first request is still running.
# When 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

On a 3-node cluster running on EKS, with about 30 Primary-only processors in 
the RUNNING state and 100 timer-driven threads, the non-primary nodes sent 
roughly 4 to 5 Lease GET requests per second each after the change from 
NIFI-16120 (and roughly 18 to 21 per second with about 290 Primary-only 
processors running). These are Lease GET counts taken from the Kubernetes API 
server audit log. The leader elector itself already accounts for about 0.5 GET 
per second for each node and lease, and the cache should add about 0.2 per 
second on top of that. What we see is far higher, so there must be many 
concurrent requests each time the entry expires. The Leader Election poll count 
reported by {{nifi.sh diagnostics}} is in the range of hundreds of calls per 
second on each node, so the cache is consulted very often and the cost is 
concentrated at expiration time.

These numbers were measured on a 2.5.0 based build that includes the NIFI-16120 
change. The same code is present in 2.12.0 and main.

h3. Proposal

Make concurrent callers of findLeader() for the same election name share one 
in-flight request to the delegated provider:

* The first caller that finds a missing or expired entry sends the request and 
stores the result in the cache.
* Other callers for the same election name wait for that result instead of 
sending their own request.
* A failure is passed on to the waiting callers but is not cached, so the next 
call after the failure sends a new request, which is the current behavior.

This does not change the 5 second TTL, does not return stale values, and does 
not change the public API.

h3. Out of scope

* How often isLeader() is called. This depends on the scheduling of 
Primary-only processors on non-primary nodes and will be discussed separately.
* A negative cache or back-off for failures, which could be a follow-up if the 
maintainers prefer it.


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