allthingssecurity commented on code in PR #30909:
URL: https://github.com/apache/camel/pull/30909#discussion_r4717041704


##########
components/camel-master/src/main/java/org/apache/camel/component/master/MasterConsumer.java:
##########
@@ -290,19 +296,35 @@ private boolean startDelegatedConsumer(BackgroundTask 
task, AtomicReference<Cons
                 throw new TaskRunFailureException(message, cause);
             }
 
-            if (!leadershipTaken || !isRunAllowed()) {
-                // the leadership went away while the consumer was starting, 
so stop what was just started
-                // instead of publishing it. No leadership event is going to 
do it, delegatedConsumer is unset
-                LOG.info("Leadership lost while the consumer was starting. 
Stopping consumer: {}", delegatedEndpoint);
-                ServiceHelper.stopAndShutdownServices(consumer, 
delegatedEndpoint);
+            // an attempt of an earlier lifecycle of this consumer (a stop and 
start of the same instance) runs on the
+            // pool of that lifecycle. Within one lifecycle, a task newer than 
this one only exists when the leadership
+            // was lost and taken again while this attempt was starting, and 
as the pool has a single thread, that task
+            // has not run yet: this attempt publishes its consumer below and 
cancels it
+            boolean earlierLifecycle = pool != leaderPool;
+            if (earlierLifecycle || delegatedConsumer != null || 
!leadershipTaken || !isRunAllowed()) {
+                // the leadership went away, or this consumer was stopped (and 
maybe started again), while the
+                // consumer was starting. This attempt is no longer the 
current one, so stop what it started instead
+                // of publishing it over the consumer of a newer attempt. Only 
the consumer: the endpoint is shared
+                // with that newer consumer, and onLeadershipLost and doStop 
stop it. And only stop it, not shut it
+                // down: a shutdown cannot be undone, and it would also shut 
down the processor of the route, which
+                // a newer consumer uses
+                LOG.info("Leadership lost or consumer stopped while the 
delegated consumer was starting. Stopping it: {}",
+                        delegatedEndpoint);
+                ServiceHelper.stopService(consumer);
+                if (delegatedConsumer != null) {
+                    // stopping a consumer also stops its processor, the 
processor of the route, which the
+                    // running delegated consumer uses too
+                    ServiceHelper.startService(processor);
+                }

Review Comment:
   The window is real but rare: the stale consumer is only stopped while 
another delegated consumer runs when an attempt from before a restart outlives 
doStop's shutdownGraceful wait (10 s by default) and the new attempt starts 
first. Until startService(processor), an exchange of the running consumer that 
reaches a stopped SendProcessor fails ('has not been started'). 
DefaultConsumer.doStop always stops its processor, so it cannot be avoided 
cheaply; fcd8f3fa now spells this out in the comment.



##########
components/camel-master/src/main/java/org/apache/camel/component/master/MasterConsumer.java:
##########
@@ -290,19 +296,35 @@ private boolean startDelegatedConsumer(BackgroundTask 
task, AtomicReference<Cons
                 throw new TaskRunFailureException(message, cause);
             }
 
-            if (!leadershipTaken || !isRunAllowed()) {
-                // the leadership went away while the consumer was starting, 
so stop what was just started
-                // instead of publishing it. No leadership event is going to 
do it, delegatedConsumer is unset
-                LOG.info("Leadership lost while the consumer was starting. 
Stopping consumer: {}", delegatedEndpoint);
-                ServiceHelper.stopAndShutdownServices(consumer, 
delegatedEndpoint);
+            // an attempt of an earlier lifecycle of this consumer (a stop and 
start of the same instance) runs on the
+            // pool of that lifecycle. Within one lifecycle, a task newer than 
this one only exists when the leadership
+            // was lost and taken again while this attempt was starting, and 
as the pool has a single thread, that task
+            // has not run yet: this attempt publishes its consumer below and 
cancels it
+            boolean earlierLifecycle = pool != leaderPool;
+            if (earlierLifecycle || delegatedConsumer != null || 
!leadershipTaken || !isRunAllowed()) {
+                // the leadership went away, or this consumer was stopped (and 
maybe started again), while the
+                // consumer was starting. This attempt is no longer the 
current one, so stop what it started instead
+                // of publishing it over the consumer of a newer attempt. Only 
the consumer: the endpoint is shared
+                // with that newer consumer, and onLeadershipLost and doStop 
stop it. And only stop it, not shut it
+                // down: a shutdown cannot be undone, and it would also shut 
down the processor of the route, which
+                // a newer consumer uses

Review Comment:
   Done in fcd8f3fa: the comment now says why it is stopService and not 
stopAndShutdownServices (doShutdown shuts down the route processor, which 
cannot be undone).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to