gnodet-bot commented on code in PR #30909:
URL: https://github.com/apache/camel/pull/30909#discussion_r4716748636


##########
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:
   ⚠️ **Observation (brief processor stop window):** `stopService(consumer)` 
calls `DefaultConsumer.doStop()`, which stops the shared `processor` via its 
exchange factory chain. The immediately following `startService(processor)` 
restarts it, but there is a brief window (while both calls execute sequentially 
under `leadershipLock`) where the actively running delegated consumer could 
fail a message if it concurrently routes through the now-stopped processor.
   
   In practice this is benign — the stale consumer was not yet actively 
consuming messages (it just finished starting), and the window is 
sub-millisecond. Worth documenting in the comment so future readers understand 
the trade-off and do not add `stopAndShutdownServices` here.



##########
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:
   💡 **Nit:** The comment explains why the endpoint is not stopped but does not 
explain why `stopService` is used instead of `stopAndShutdownServices` for the 
consumer itself. `DefaultConsumer.doShutdown()` shuts down the route processor, 
and that cannot be undone — worth spelling that out explicitly.
   
   ```suggestion
                   // 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 calls DefaultConsumer.doShutdown which 
shuts down the route processor, and that
                   // cannot be undone — the newer consumer that uses the same 
processor would be broken
   ```



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