shashank created CAMEL-27509:
--------------------------------

             Summary: camel-infinispan - a stopping cluster view keeps the 
leadership while it waits for its leadership thread, and the stop can take 
twice the shutdown timeout
                 Key: CAMEL-27509
                 URL: https://issues.apache.org/jira/browse/CAMEL-27509
             Project: Camel
          Issue Type: Bug
          Components: camel-infinispan
            Reporter: shashank
            Assignee: shashank


h3. What happens

{{InfinispanRemoteClusterView}} (and {{InfinispanEmbeddedClusterView}}) refresh 
the leader key from a single-thread scheduled executor. Each run of the 
leadership task takes the lock of the view's {{LeadershipService}} 
({{BaseService.lock}}). {{BaseService.stop()}} holds the same lock for the 
whole {{doStop()}}, and {{doStop()}} waits for that executor with 
{{shutdownGraceful}}, and only after that calls {{setLeader(false)}} and 
removes the leader key.

So a stop and a leadership run wait for each other:

* If a run starts while the stop is in {{doStop()}} (the periodic run, a run 
queued by a removed or expired event, or the first run right after a quick 
start and stop), the run blocks in {{lock.lock()}}, which an interrupt does not 
end. The executor cannot terminate: the stop waits the shutdown await 
termination (10 s by default), calls {{shutdownNow}}, and waits it again. A 
stop takes twice the shutdown await termination.
* If a run is waiting for the server (a slow Hot Rod call) when the stop is 
called, {{stop()}} waits for the lock until that call returns.

All that time the stopping node keeps {{leader = true}}, fires no leadership 
lost event, and does not refresh the leader key. When the key expires 
meanwhile, another node takes it, and both nodes are leader: clustered routes 
and master consumers run on both. With the default lifespan (30 s, refreshed 
every 15 s), this happens when the last refresh was more than about 10 s before 
the stop.

h3. How to reproduce

Remote view with {{shutdownAwaitTermination}} = 1 s and the removal of the 
client listener held for a moment during the stop, while a leadership run 
starts: the stop takes about 2 s (two waits), and the node is still leader 
during the removal. With a second node and a 2 s lifespan, both nodes were 
leader for 929 ms.

Found with a TLA+ model of the view's leadership thread, its stop and the Hot 
Rod server (a node in {{doStop}} keeps the flag while another acquires the 
key), and reproduced against the real view.

h3. Fix

* {{doStop}} sets {{running}} to false and gives up the leadership 
({{setLeader(false)}}, which fires the lost event) first, before it removes the 
client listener and waits for the executor. The leader key is still removed 
afterwards, as before.
* A leadership run no longer takes the lock of the service. It returns at once 
when the service is stopped, so a run that starts during the stop ends at once, 
and the stop waits for the executor only as long as a run in progress needs (a 
Hot Rod call that still waits for the server after the shutdown await 
termination is interrupted by {{shutdownNow}}).
* A run takes the leadership only while the service runs (checked under a small 
lock with the stop), so a run that acquires the key while the view stops does 
not make the stopped node leader again; it keeps the version so the stop still 
removes the key.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to