shashank created CAMEL-27592:
--------------------------------
Summary: camel-cluster - ClusteredRoutePolicy: a route added while
the node loses the leadership keeps running on a node that is not the leader,
and after a CamelContext stop and start the leader never starts its routes
Key: CAMEL-27592
URL: https://issues.apache.org/jira/browse/CAMEL-27592
Project: Camel
Issue Type: Bug
Components: camel-core
Reporter: shashank
Assignee: shashank
Fix For: 4.24.0
h3. What happens
*1. A route added while the leadership is lost keeps running on a node that is
not the leader.* When a route is added to a running CamelContext, {{onInit}}
reads {{isLeader()}} without the policy lock. If the node is the leader, the
route is recorded as started and the route controller starts it as usual. If
the leadership is lost after {{onInit}} but before the route has started, the
policy thread applies the loss while the route cannot be stopped yet (the
policy is not started yet, or the route status is still null, Initialized or
Starting), so it skips it. The route controller then starts the route, and
nothing stops it afterwards. The new leader runs the route too.
*2. After {{CamelContext.stop()}} and {{start()}}, the leader never starts the
routes of the policy.* A {{ClusteredRoutePolicy}} set on a route definition is
used again by the routes created at the restart. The stop shut the policy down:
the executor of the policy thread was shut down (it was created once, in
{{setCamelContext}}), the event notifier was removed and the retry flag was
left set, and none of this was undone. Every leadership change was then
rejected, and the started event of the restart did not reach the policy. The
view and the policy both say leader, but the routes stay stopped on every node.
*3 (smaller).* A route start that throws an {{Error}} (such as a
{{NoClassDefFoundError}}) killed the pass and was never tried again, because
only {{Exception}} was caught.
h3. How to reproduce
With a stand-in cluster service in camel-core:
* a second route policy of the same route, whose {{onInit}} runs right after
the clustered one, takes the leadership away and waits until the policy thread
has looked at the route: the route is Started 10 seconds later on the node that
is not the leader;
* the same with the leadership taken away while the route is starting (from the
{{onStart}} of a second policy);
* a route started on the leader, then {{context.stop(); context.start()}}: the
route is still Stopped 10 seconds later, and a leadership taken after the
restart does not start it either.
Found with a TLA+ model of the policy (the hand-off to the policy thread, the
passes, route add/remove and the CamelContext lifecycle) and reproduced against
the real class.
h3. Fix
* A route added while the node is the leader is still started by the route
controller, when {{addRoutes}} runs. When such a route starts ({{onStart}}) on
a node that is no longer the leader, the policy thread applies the leadership
again, and a route found still starting by a stop pass is stopped once started
(retry after {{retryDelay}}).
* The policy is made usable again when a route is created with it after it was
shut down: a new executor for the policy thread, the event notifier added back,
and its flags reset, before it takes the leadership. The routes are then
started once the CamelContext has started, as at the first start.
* An {{Error}} from a route start or stop counts as a failed start or stop
(tried again); a {{VirtualMachineError}} is rethrown after the retry is
scheduled, and logged once at ERROR. If the retry timer cannot be created (for
example an {{OutOfMemoryError}} when no native thread can be created), the
retry flag is reset, so a later failure schedules a retry again.
* A route added while the node loses the leadership runs on that node for about
one {{retryDelay}} (5 seconds by default) before it is stopped, instead of
forever: its {{onStart}} runs while the route is still starting, so the stop is
tried again after the retry delay.
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)