davsclaus commented on code in PR #30739:
URL: https://github.com/apache/camel/pull/30739#discussion_r4684534715


##########
core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java:
##########
@@ -2531,6 +2591,47 @@ protected void doShutdown() throws Exception {
      * as {@code at route1[to3] orders.camel.yaml:18}. Empty when nothing was 
captured - message history or source
      * location can be off - so the message keeps its shape (CAMEL-24974).
      */
+    /**
+     * Fails the exchange with a {@link VirtualMachineError} that no 
onException of the route names (CAMEL-27515). Camel
+     * does not recover from it on its own: there is no redelivery and no dead 
letter channel, and the caller gets the
+     * failure. It is logged once at ERROR, also when the exchange goes 
through the error handlers of several routes.
+     */
+    protected void handleFatalFailure(Exchange exchange, Throwable fatal) {

Review Comment:
   Non-blocking: the two new methods were inserted between the existing Javadoc 
of `failureOrigin` (the `... as {@code at route1[to3] orders.camel.yaml:18} ... 
(CAMEL-24974)` block just above) and its method. That block now dangles in 
front of the `handleFatalFailure` Javadoc, and `failureOrigin` has none. Moving 
`handleFatalFailure` and `logIgnoredAfterCallback` above that Javadoc block 
fixes it.



##########
components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/queue/HazelcastQueueConsumer.java:
##########
@@ -28,10 +28,14 @@
 import org.apache.camel.Processor;
 import org.apache.camel.component.hazelcast.HazelcastDefaultConsumer;
 import org.apache.camel.component.hazelcast.listener.CamelItemListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class HazelcastQueueConsumer extends HazelcastDefaultConsumer {
 
     // the minimum delay before polling again after a poll error, so a 
pollingTimeout of 0 does not spin
+    private static final Logger LOG = 
LoggerFactory.getLogger(HazelcastQueueConsumer.class);
+
     private static final long MIN_POLL_ERROR_DELAY = 1000L;

Review Comment:
   Nit: the comment for `MIN_POLL_ERROR_DELAY` is now above `LOG`.
   
   ```suggestion
       private static final Logger LOG = 
LoggerFactory.getLogger(HazelcastQueueConsumer.class);
   
       // the minimum delay before polling again after a poll error, so a 
pollingTimeout of 0 does not spin
       private static final long MIN_POLL_ERROR_DELAY = 1000L;
   ```



##########
components/camel-timer/src/main/java/org/apache/camel/component/timer/TimerConsumer.java:
##########
@@ -267,6 +274,12 @@ public void onCamelContextStarted(CamelContext context, 
boolean alreadyStarted)
     /**
      * Whether the timer task is allow to run or not
      */
+    @Override
+    protected boolean isStopOnFatalError() {
+        // a timer fires in a loop: it stops after a VirtualMachineError 
(CAMEL-27515)
+        return true;
+    }
+

Review Comment:
   Non-blocking: the existing Javadoc `Whether the timer task is allow to run 
or not` now sits on the new `isStopOnFatalError()` override, and 
`isTaskRunAllowed()` lost it.
   
   ```suggestion
       @Override
       protected boolean isStopOnFatalError() {
           // a timer fires in a loop: it stops after a VirtualMachineError 
(CAMEL-27515)
           return true;
       }
   
       /**
        * Whether the timer task is allow to run or not
        */
   ```



##########
core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java:
##########
@@ -1155,12 +1155,11 @@ private void submitCompletions(String key, 
List<Exchange> aggregated) {
         for (Exchange agg : aggregated) {
             try {
                 onSubmitCompletion(key, agg);
-            } catch (VirtualMachineError e) {
-                // left as before: a JVM-level failure is not handled here
-                throw e;
             } catch (RuntimeException | Error e) {
-                // also an Error of the aggregation strategy, as for an 
exception
-                if (failure == null) {
+                // also an Error of the aggregation strategy, as for an 
exception, but a VirtualMachineError is not hidden
+                // by the exception of an earlier group (CAMEL-27515)
+                if (failure == null || ObjectHelper.getVirtualMachineError(e) 
!= null
+                        && ObjectHelper.getVirtualMachineError(failure) == 
null) {

Review Comment:
   Nit: correct as written (`&&` binds tighter than `||`), but parentheses make 
the intent explicit, and Sonar flags mixed `||`/`&&` without them.
   
   ```suggestion
                   if (failure == null || 
(ObjectHelper.getVirtualMachineError(e) != null
                           && ObjectHelper.getVirtualMachineError(failure) == 
null)) {
   ```



##########
core/camel-support/src/main/java/org/apache/camel/support/DefaultConsumer.java:
##########
@@ -238,16 +239,75 @@ protected void doInit() throws Exception {
     @Override
     protected void doStart() throws Exception {
         LOG.debug("Starting consumer: {}", this);
+        // a restart brings the consumer back after a fatal error
+        fatalError = null;
         exchangeFactory.setRouteId(routeId);
         ServiceHelper.startService(exchangeFactory, processor);
     }
 
     @Override
     protected void doStop() throws Exception {
         LOG.debug("Stopping consumer: {}", this);
+        fatalError = null;
         ServiceHelper.stopService(exchangeFactory, processor);
     }
 
+    /**
+     * The {@link VirtualMachineError} (such as an {@link OutOfMemoryError} or 
a {@link StackOverflowError}) that
+     * stopped this consumer, or <tt>null</tt>. It is kept until the consumer 
is stopped, and the health check of the
+     * consumer reports DOWN until then.
+     *
+     * @since 4.24
+     */
+    public Throwable getFatalError() {
+        return fatalError;
+    }
+
+    /**
+     * Whether this consumer stops when a {@link VirtualMachineError} ends its 
loop, or fails one of its exchanges
+     * without the route catching it. A consumer that pulls work in a loop 
(polling, timer, a queue) returns
+     * <tt>true</tt>: Camel cannot know whether going on is safe. A consumer 
that is pushed requests (such as HTTP or
+     * direct) keeps the default <tt>false</tt>: only that request fails.
+     *
+     * @since 4.24
+     */
+    protected boolean isStopOnFatalError() {
+        return false;
+    }
+
+    /**
+     * Handles a {@link VirtualMachineError} that ended a loop or a task of 
this consumer, or failed one of its
+     * exchanges without the route catching it. A consumer that stops on a 
fatal error (see
+     * {@link #isStopOnFatalError()}) keeps it as its {@link #getFatalError() 
fatal error}: it no longer runs
+     * ({@link #isRunAllowed()} is <tt>false</tt>), its health check reports 
DOWN, and the error is logged once at
+     * ERROR, until the route is restarted. Restarting a route, a pod or the 
JVM is the choice of the user or of the
+     * platform.
+     *
+     * @param  error the virtual machine error
+     * @return       <tt>true</tt> if this consumer stops
+     * @since        4.24
+     */
+    public boolean handleFatalError(Throwable error) {
+        if (!isStopOnFatalError() || !super.isRunAllowed()) {
+            return false;
+        }
+        synchronized (this) {

Review Comment:
   Suggestion: `synchronized (this)` is the only one in `camel-support`. Camel 
moved its services to `Lock`s for virtual threads (see `BaseService.lock`), and 
synchronizing on the consumer's own monitor can contend with subclasses that do 
the same. The lifecycle `lock` is not a good fit here either, as a stop that 
waits for inflight exchanges could hold it. A compare-and-set does the job 
without a lock, for example an `AtomicReference<Throwable> fatalError` with `if 
(!fatalError.compareAndSet(null, error)) return true;`.



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