[ 
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 
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). Both numbers are well above the 0.2 requests per second 
expected from a 5 second TTL, which indicates many concurrent requests on every 
expiration. 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.

  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:

    final CachedLeader cachedLeader = cachedLeaders.get(name);
    if (cachedLeader == null || cachedLeader.isExpired()) {
        leader = provider.findLeader(name);
        ...
        cachedLeaders.put(name, new CachedLeader(...));
    }

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

2. When provider.findLeader() throws, nothing is cached, so every following 
caller sends a new request while the
   Kubernetes API is slow or rate limiting. Under load this increases the 
pressure on the API server instead of
   reducing it.

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). Both numbers are well
above the 0.2 requests per second expected from a 5 second TTL, which indicates 
many concurrent requests on every
expiration. 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.

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.

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.

        Summary: CachingLeaderElectionCommandProvider sends concurrent Lease 
requests when a cached entry expires  (was: 
CachingLeaderElectionCommandProvider sends concurrent Lease requests on cache 
expiry and does not limit retries after failures)

> 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
>            Reporter: Xinyu Wang
>            Priority: Minor
>
> 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). Both numbers are well above the 0.2 requests per second 
> expected from a 5 second TTL, which indicates many concurrent requests on 
> every expiration. 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.



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

Reply via email to