This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 04ad430076f3 CAMEL-27459: camel-aws2-kinesis - an Error in one run of 
the shard monitor no longer stops it for good (#30843)
04ad430076f3 is described below

commit 04ad430076f3c9dbfd537599f72e36e77f4c1c90
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Nov 27 12:06:44 2026 +0530

    CAMEL-27459: camel-aws2-kinesis - an Error in one run of the shard monitor 
no longer stops it for good (#30843)
    
    ShardMonitor.run caught only Exception. An Error of one run 
(NoClassDefFoundError, ExceptionInInitializerError, an
    AssertionError) ended the task scheduled with scheduleAtFixedRate: the 
executor kept the error in the ScheduledFuture,
    which nothing reads, so nothing was logged and the task never ran again. 
The consumer went on reading the shards of the
    last list, so the shards created by a later split or merge were never read, 
and the consumer stayed UP until the route
    was restarted.
    
    As in CAMEL-26906, such an Error is now logged at WARN, recorded as the 
shard list error that poll() reports while no
    shard was listed (now a Throwable), and the task runs again at the next 
interval.
    
    As CAMEL-27515 decided for a consumer that pulls work in a loop, and as the 
SQS visibility extender does (CAMEL-27473),
    a VirtualMachineError (such as an OutOfMemoryError) is logged at ERROR and 
handed to handleFatalError: the consumer
    stops consuming until the route is restarted, and its health check is DOWN 
with getFatalError(). The error is thrown
    on, which ends the scheduled task; a stop and start schedules a new monitor.
    
    ShardMonitor is package-private so that the test runs it.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../component/aws2/kinesis/Kinesis2Consumer.java   |  21 ++-
 .../KinesisConsumerShardMonitorErrorTest.java      | 197 +++++++++++++++++++++
 2 files changed, 214 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
 
b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
index a93b4bf6c922..5842d9537b8e 100644
--- 
a/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
+++ 
b/components/camel-aws/camel-aws2-kinesis/src/main/java/org/apache/camel/component/aws2/kinesis/Kinesis2Consumer.java
@@ -105,7 +105,7 @@ public class Kinesis2Consumer extends 
ScheduledBatchPollingConsumer implements R
     // The failure of the last ListShards of the shard monitor, null once a 
ListShards succeeded. While the shards have
     // not been listed, poll() reports it, so that a consumer that cannot list 
the shards (no permission, no such
     // stream) is not seen healthy while it reads nothing.
-    private volatile Exception shardListError;
+    private volatile Throwable shardListError;
 
     private volatile List<Shard> currentShardList = List.of();
     private static final String SHARD_MONITOR_EXECUTOR_NAME = 
"Kinesis_shard_monitor";
@@ -138,7 +138,7 @@ public class Kinesis2Consumer extends 
ScheduledBatchPollingConsumer implements R
         List<Shard> shardList = getCurrentShardList();
         if (shardList.isEmpty()) {
             // the shard monitor has not listed the shards yet, so there is 
nothing to read
-            Exception error = shardListError;
+            Throwable error = shardListError;
             if (error != null) {
                 throw new IllegalStateException(
                         "Cannot list the shards of stream " + 
getEndpoint().getConfiguration().getStreamName() + ": "
@@ -717,8 +717,21 @@ public class Kinesis2Consumer extends 
ScheduledBatchPollingConsumer implements R
                     trackChildShards(latestShardList);
                     setCurrentShardList(latestShardList);
                 }
-            } catch (Exception e) {
-                LOG.warn("Exception getting latest shard list", e);
+            } catch (VirtualMachineError e) {
+                // a JVM-level failure, not a failure of the monitor: the 
consumer stops consuming until the route is
+                // restarted, and its health check is DOWN with the error 
(CAMEL-27515). Thrown on, which ends the
+                // scheduled task: the consumer no longer polls 
(isRunAllowed() is false), so listing the shards again
+                // has no use, and a restart schedules a new monitor
+                LOG.error("Fatal error listing the shards of stream {}. The 
consumer stops consuming until the route is"
+                          + " restarted.",
+                        getEndpoint().getConfiguration().getStreamName(), e);
+                handleFatalError(e);
+                throw e;
+            } catch (Exception | Error e) {
+                // an Exception, or another Error (such as a 
NoClassDefFoundError), only fails this run: an Error thrown
+                // on would end the scheduled task without a log line, and the 
shards created by a later split or merge
+                // would never be listed
+                LOG.warn("Error getting latest shard list", e);
                 shardListError = e;
             }
         }
diff --git 
a/components/camel-aws/camel-aws2-kinesis/src/test/java/org/apache/camel/component/aws2/kinesis/KinesisConsumerShardMonitorErrorTest.java
 
b/components/camel-aws/camel-aws2-kinesis/src/test/java/org/apache/camel/component/aws2/kinesis/KinesisConsumerShardMonitorErrorTest.java
new file mode 100644
index 000000000000..87ef89e1ddac
--- /dev/null
+++ 
b/components/camel-aws/camel-aws2-kinesis/src/test/java/org/apache/camel/component/aws2/kinesis/KinesisConsumerShardMonitorErrorTest.java
@@ -0,0 +1,197 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.aws2.kinesis;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.AsyncProcessor;
+import org.apache.camel.CamelContext;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.ScheduledPollConsumerHealthCheck;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+import software.amazon.awssdk.core.SdkBytes;
+import software.amazon.awssdk.services.kinesis.KinesisClient;
+import software.amazon.awssdk.services.kinesis.model.GetRecordsRequest;
+import software.amazon.awssdk.services.kinesis.model.GetRecordsResponse;
+import software.amazon.awssdk.services.kinesis.model.GetShardIteratorRequest;
+import software.amazon.awssdk.services.kinesis.model.GetShardIteratorResponse;
+import software.amazon.awssdk.services.kinesis.model.ListShardsRequest;
+import software.amazon.awssdk.services.kinesis.model.ListShardsResponse;
+import software.amazon.awssdk.services.kinesis.model.Record;
+import software.amazon.awssdk.services.kinesis.model.Shard;
+import software.amazon.awssdk.services.kinesis.model.ShardIteratorType;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The shard monitor lists the shards at a fixed rate, and finds the shards 
created by a split or a merge. An
+ * {@link Error} of one of its runs must not end the scheduled task (the 
executor would not run it again, and nothing
+ * would be logged); a {@link VirtualMachineError} stops the consumer until it 
is restarted, and its health check is
+ * DOWN with the error (CAMEL-27515).
+ */
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+class KinesisConsumerShardMonitorErrorTest {
+
+    private static final Shard PARENT = Shard.builder().shardId("s0").build();
+    private static final List<Shard> AFTER_SPLIT = List.of(PARENT,
+            Shard.builder().shardId("s1").parentShardId("s0").build(),
+            Shard.builder().shardId("s2").parentShardId("s0").build());
+
+    @Mock
+    private KinesisClient kinesisClient;
+    @Mock
+    private AsyncProcessor processor;
+
+    private final CamelContext context = new DefaultCamelContext();
+    private final Kinesis2Component component = new Kinesis2Component(context);
+    private final AtomicInteger listShardsCalls = new AtomicInteger();
+    private Kinesis2Consumer consumer;
+
+    @AfterEach
+    void tearDown() {
+        if (consumer != null) {
+            consumer.stop();
+        }
+        context.stop();
+    }
+
+    @Test
+    void anErrorOfOneRunDoesNotStopTheMonitor() {
+        
when(kinesisClient.listShards(any(ListShardsRequest.class))).thenAnswer(invocation
 -> {
+            int call = listShardsCalls.incrementAndGet();
+            if (call == 1) {
+                return ListShardsResponse.builder().shards(PARENT).build();
+            }
+            if (call == 2) {
+                throw new NoClassDefFoundError("simulated");
+            }
+            // the stream was split after the failed run
+            return ListShardsResponse.builder().shards(AFTER_SPLIT).build();
+        });
+        // the scheduled monitor, every 10 ms
+        startConsumer(10);
+
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
consumer.getCurrentShardList().size() == 3);
+        consumer.run();
+
+        // the children are read from their first record
+        
verify(kinesisClient).getShardIterator(argThat((GetShardIteratorRequest r) -> 
"s1".equals(r.shardId())
+                && r.shardIteratorType() == ShardIteratorType.TRIM_HORIZON));
+        
verify(kinesisClient).getShardIterator(argThat((GetShardIteratorRequest r) -> 
"s2".equals(r.shardId())
+                && r.shardIteratorType() == ShardIteratorType.TRIM_HORIZON));
+        assertEquals(0L, consumer.getErrorCounter());
+    }
+
+    @Test
+    void aVirtualMachineErrorStopsTheConsumerUntilRestarted() {
+        OutOfMemoryError error = new OutOfMemoryError("simulated");
+        
when(kinesisClient.listShards(any(ListShardsRequest.class))).thenAnswer(invocation
 -> {
+            listShardsCalls.incrementAndGet();
+            return ListShardsResponse.builder().shards(PARENT).build();
+        });
+        // the monitor runs once at start; the test runs it after that
+        startConsumer(TimeUnit.DAYS.toMillis(1));
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
consumer.getCurrentShardList().size() == 1);
+        ScheduledPollConsumerHealthCheck healthCheck = new 
ScheduledPollConsumerHealthCheck(consumer, "consumer:orders");
+        consumer.run();
+        verify(kinesisClient, 
times(1)).getRecords(any(GetRecordsRequest.class));
+        assertEquals(HealthCheck.State.UP, readiness(healthCheck).getState());
+
+        
when(kinesisClient.listShards(any(ListShardsRequest.class))).thenThrow(error);
+        // thrown on, which ends the scheduled task, after it stopped the 
consumer
+        assertSame(error, assertThrows(OutOfMemoryError.class, () -> 
consumer.new ShardMonitor().run()));
+        assertSame(error, consumer.getFatalError());
+        HealthCheck.Result result = readiness(healthCheck);
+        assertEquals(HealthCheck.State.DOWN, result.getState());
+        assertSame(error, result.getError().orElse(null));
+
+        // the next run does not poll
+        consumer.run();
+        verify(kinesisClient, 
times(1)).getRecords(any(GetRecordsRequest.class));
+        assertEquals(HealthCheck.State.DOWN, 
readiness(healthCheck).getState());
+
+        // a stop and start clears the fatal error, and the new monitor lists 
the shards again
+        int listedBeforeRestart = listShardsCalls.get();
+        
when(kinesisClient.listShards(any(ListShardsRequest.class))).thenAnswer(invocation
 -> {
+            listShardsCalls.incrementAndGet();
+            return ListShardsResponse.builder().shards(AFTER_SPLIT).build();
+        });
+        consumer.stop();
+        consumer.start();
+        assertNull(consumer.getFatalError());
+        await().atMost(20, TimeUnit.SECONDS).until(() -> 
consumer.getCurrentShardList().size() == 3);
+        assertTrue(listShardsCalls.get() > listedBeforeRestart);
+
+        // and the consumer reads the three shards again (one read before the 
error), the children from their first
+        // record
+        consumer.run();
+        verify(kinesisClient, 
times(4)).getRecords(any(GetRecordsRequest.class));
+        
verify(kinesisClient).getShardIterator(argThat((GetShardIteratorRequest r) -> 
"s1".equals(r.shardId())
+                && r.shardIteratorType() == ShardIteratorType.TRIM_HORIZON));
+        assertEquals(HealthCheck.State.UP, readiness(healthCheck).getState());
+    }
+
+    private void startConsumer(long shardMonitorInterval) {
+        
when(kinesisClient.getShardIterator(any(GetShardIteratorRequest.class)))
+                
.thenReturn(GetShardIteratorResponse.builder().shardIterator("it").build());
+        
when(kinesisClient.getRecords(any(GetRecordsRequest.class))).thenReturn(GetRecordsResponse.builder()
+                .nextShardIterator("next")
+                .records(Record.builder().sequenceNumber("1").partitionKey("p")
+                        .data(SdkBytes.fromString("x", 
StandardCharsets.UTF_8)).build())
+                .build());
+
+        component.start();
+        Kinesis2Configuration configuration = new Kinesis2Configuration();
+        configuration.setAmazonKinesisClient(kinesisClient);
+        configuration.setStreamName("orders");
+        configuration.setIteratorType(ShardIteratorType.LATEST);
+        configuration.setShardMonitorInterval(shardMonitorInterval);
+        Kinesis2Endpoint endpoint = new 
Kinesis2Endpoint("aws2-kinesis:orders", configuration, component);
+        endpoint.start();
+        consumer = new Kinesis2Consumer(endpoint, processor);
+        consumer.setConnection(component.getConnection());
+        // the test polls with run(), which updates the state the health check 
reads as a scheduled poll does
+        consumer.setStartScheduler(false);
+        consumer.start();
+    }
+
+    private static HealthCheck.Result 
readiness(ScheduledPollConsumerHealthCheck healthCheck) {
+        return healthCheck.call(Map.of(HealthCheck.CHECK_KIND, 
HealthCheck.Kind.READINESS));
+    }
+}

Reply via email to