This is an automated email from the ASF dual-hosted git repository.
Croway 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 5734bb014040 CAMEL-27010: camel-mongodb - the consumer stop does not
wait without bound for a thread blocked reading from MongoDB
5734bb014040 is described below
commit 5734bb014040aaf1474c8f00aa42c6efc27400c9
Author: croway <[email protected]>
AuthorDate: Mon Nov 16 10:05:57 2026 +0100
CAMEL-27010: camel-mongodb - the consumer stop does not wait without bound
for a thread blocked reading from MongoDB
MongoAbstractConsumerThread.stop() closed the cursor and waited for the
consumer thread with stoppedLatch.await(), without a timeout. Closing the
cursor does not end a getMore in progress (the driver closes the cursor
once it returns), and a read from a connection that stopped answering does
not return: consumer.stop() did not return, and a route or CamelContext
stop ran into the graceful shutdown timeout, then failed with an
InterruptedException and left the consumer's thread pool to the fail-safe
of the ExecutorServiceManager (2 x shutdownAwaitTermination more).
While the thread reads from MongoDB, the stop now waits for it at most the
shutdownAwaitTermination of the ExecutorServiceManager (10 s by default),
then logs a WARN, interrupts the thread and leaves it behind. A thread that
routes an exchange is waited for as before, and not interrupted. The latch
is created with the thread object, so a stop before run() begins no longer
meets a null latch.
---
.../camel/catalog/docs/mongodb-component.adoc | 7 +
.../src/main/docs/mongodb-component.adoc | 7 +
.../mongodb/MongoAbstractConsumerThread.java | 48 +++-
.../component/mongodb/MongoDbConsumerStopTest.java | 270 +++++++++++++++++++++
4 files changed, 325 insertions(+), 7 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
index 59c089bb160c..55f9b0d837cf 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mongodb-component.adoc
@@ -952,6 +952,13 @@ There are several types of consumers:
. Tailable Cursor Consumer
. Change Streams Consumer
+Both run in a thread of their own. When a consumer stops, it closes its cursor
and waits for that thread to end.
+Closing the cursor does not end a read from MongoDB in progress on the thread,
and a read from a connection that
+stopped answering does not return: the consumer waits for such a read up to
the `shutdownAwaitTermination` of the
+`ExecutorServiceManager` (10 seconds by default), then logs a WARN, interrupts
the thread and leaves it behind. A
+thread that routes an exchange is waited for, and not interrupted, as the
graceful shutdown bounds that wait with its
+own timeout.
+
==== Tailable Cursor Consumer
MongoDB offers a mechanism to instantaneously consume ongoing data from
diff --git a/components/camel-mongodb/src/main/docs/mongodb-component.adoc
b/components/camel-mongodb/src/main/docs/mongodb-component.adoc
index 59c089bb160c..55f9b0d837cf 100644
--- a/components/camel-mongodb/src/main/docs/mongodb-component.adoc
+++ b/components/camel-mongodb/src/main/docs/mongodb-component.adoc
@@ -952,6 +952,13 @@ There are several types of consumers:
. Tailable Cursor Consumer
. Change Streams Consumer
+Both run in a thread of their own. When a consumer stops, it closes its cursor
and waits for that thread to end.
+Closing the cursor does not end a read from MongoDB in progress on the thread,
and a read from a connection that
+stopped answering does not return: the consumer waits for such a read up to
the `shutdownAwaitTermination` of the
+`ExecutorServiceManager` (10 seconds by default), then logs a WARN, interrupts
the thread and leaves it behind. A
+thread that routes an exchange is waited for, and not interrupted, as the
graceful shutdown bounds that wait with its
+own timeout.
+
==== Tailable Cursor Consumer
MongoDB offers a mechanism to instantaneously consume ongoing data from
diff --git
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
index 94d4e9bdab30..511b1121ece0 100644
---
a/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
+++
b/components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoAbstractConsumerThread.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.mongodb;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
@@ -42,8 +43,12 @@ abstract class MongoAbstractConsumerThread implements
Runnable {
protected volatile MongoCursor cursor;
volatile boolean keepRunning = true;
- private volatile boolean stopped;
- private volatile CountDownLatch stoppedLatch;
+ // counted down when run() ends; created with the thread, so that a stop
before run() begins can wait for it too
+ private final CountDownLatch stoppedLatch = new CountDownLatch(1);
+ // the thread that runs this consumer thread, while it runs
+ private volatile Thread runner;
+ // whether the thread routes an exchange, rather than reads from MongoDB
+ private volatile boolean processing;
MongoAbstractConsumerThread(MongoDbEndpoint endpoint, Consumer consumer) {
this.endpoint = endpoint;
@@ -71,10 +76,13 @@ abstract class MongoAbstractConsumerThread implements
Runnable {
* @return {@code true} if the route processed the exchange
without a failure
*/
protected boolean processExchange(Exchange exchange) {
+ processing = true;
try {
consumer.getProcessor().process(exchange);
} catch (Exception e) {
exchange.setException(e);
+ } finally {
+ processing = false;
}
Exception cause = exchange.getException();
if (cause != null) {
@@ -97,7 +105,7 @@ abstract class MongoAbstractConsumerThread implements
Runnable {
*/
@Override
public void run() {
- stoppedLatch = new CountDownLatch(1);
+ runner = Thread.currentThread();
// the failures since a cursor was last opened: while the server
cannot be reached, every attempt fails the same
// way, so only the first one logs the stack trace
int failures = 0;
@@ -159,7 +167,7 @@ abstract class MongoAbstractConsumerThread implements
Runnable {
}
}
} finally {
- stopped = true;
+ runner = null;
stoppedLatch.countDown();
}
}
@@ -182,10 +190,36 @@ abstract class MongoAbstractConsumerThread implements
Runnable {
}
}
+ /**
+ * Waits for the thread to end.
+ * <p>
+ * Closing the cursor does not end a read from MongoDB in progress on the
thread: the driver only closes the cursor
+ * once that read returns, and a read from a connection that stopped
answering does not return. So while the thread
+ * reads from MongoDB, the wait is bounded, by the time to wait for a
thread pool to shut down
+ * ({@code ExecutorServiceManager.getShutdownAwaitTermination()}, 10
seconds by default): the thread is then
+ * interrupted and left behind. An exchange that the thread routes is
waited for as before, as the graceful shutdown
+ * bounds the wait for an exchange in flight with its own timeout.
+ */
private void awaitStopped() throws InterruptedException {
- if (!stopped) {
- log.info("Going to wait for stopping");
- stoppedLatch.await();
+ if (stoppedLatch.getCount() == 0) {
+ return;
+ }
+ log.info("Going to wait for stopping");
+ // at least a little, so that the loop does not spin while the thread
routes an exchange
+ long timeout = Math.max(100,
endpoint.getCamelContext().getExecutorServiceManager().getShutdownAwaitTermination());
+ while (!stoppedLatch.await(timeout, TimeUnit.MILLISECONDS)) {
+ if (!processing) {
+ log.warn("The consumer thread did not end within {} millis
after its cursor was closed, while it reads from"
+ + " MongoDB: closing the cursor does not end a read
in progress, and a read from a connection"
+ + " that stopped answering does not return. The
thread is interrupted and left behind, it ends"
+ + " once the read returns.",
+ timeout);
+ Thread thread = runner;
+ if (thread != null) {
+ thread.interrupt();
+ }
+ return;
+ }
}
}
}
diff --git
a/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
new file mode 100644
index 000000000000..0ffad0e4472f
--- /dev/null
+++
b/components/camel-mongodb/src/test/java/org/apache/camel/component/mongodb/MongoDbConsumerStopTest.java
@@ -0,0 +1,270 @@
+/*
+ * 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.mongodb;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+
+import com.mongodb.CursorType;
+import com.mongodb.client.ChangeStreamIterable;
+import com.mongodb.client.FindIterable;
+import com.mongodb.client.ListCollectionNamesIterable;
+import com.mongodb.client.MongoChangeStreamCursor;
+import com.mongodb.client.MongoClient;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoCursor;
+import com.mongodb.client.MongoDatabase;
+import com.mongodb.client.model.changestream.FullDocument;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.bson.Document;
+import org.bson.conversions.Bson;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Stopping a consumer closes its cursor and waits for the consumer thread to
end. Closing the cursor does not end a
+ * read that is in progress on the consumer thread: the driver only closes the
cursor once that read returns, and a read
+ * from a connection that stopped answering does not return. The stop must not
wait for that thread without a bound, but
+ * still wait for the step of the route in progress, without interrupting it.
+ */
+class MongoDbConsumerStopTest {
+
+ private final MongoClient client = mock(MongoClient.class);
+ private final MongoCollection<Document> collection = mockCollection();
+ // the consumer thread is in the read of the cursor
+ private final CountDownLatch reading = new CountDownLatch(1);
+ // the server answers the read, only when the test ends
+ private final CountDownLatch answer = new CountDownLatch(1);
+ private final AtomicBoolean interruptedWhileReading = new AtomicBoolean();
+ private CamelContext context;
+
+ @SuppressWarnings("unchecked")
+ private static MongoCollection<Document> mockCollection() {
+ return mock(MongoCollection.class);
+ }
+
+ @BeforeEach
+ @SuppressWarnings("unchecked")
+ void setUp() {
+ MongoDatabase database = mock(MongoDatabase.class);
+ ListCollectionNamesIterable names =
mock(ListCollectionNamesIterable.class);
+ when(names.spliterator()).thenAnswer(i ->
List.of("events").spliterator());
+ when(database.listCollectionNames()).thenReturn(names);
+ when(database.getCollection("events",
Document.class)).thenReturn(collection);
+ when(database.getName()).thenReturn("test");
+ // the tailable consumer only accepts a capped collection
+ when(database.runCommand(any(Bson.class))).thenReturn(new
Document("capped", true));
+ when(client.getDatabase("test")).thenReturn(database);
+
+ context = new DefaultCamelContext();
+ context.getRegistry().bind("myDb", client);
+ // how long the consumer waits for its thread to end, while it reads
from MongoDB
+ context.getExecutorServiceManager().setShutdownAwaitTermination(500);
+ // longer than the test waits for the stop: the stop must not end
because the graceful shutdown timed out
+ context.getShutdownStrategy().setTimeout(60);
+ }
+
+ @AfterEach
+ void tearDown() {
+ // the read returns, so a consumer thread that the stop left behind
ends
+ answer.countDown();
+ if (context != null) {
+ context.stop();
+ }
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ void tailableConsumerStopsWhileItsThreadIsBlockedReadingTheCursor() throws
Exception {
+ MongoCursor<Document> cursor = mock(MongoCursor.class);
+ when(cursor.hasNext()).thenAnswer(i -> readThatDoesNotReturn());
+ FindIterable<Document> find = mock(FindIterable.class);
+ when(collection.find()).thenReturn(find);
+ when(find.cursorType(CursorType.TailableAwait)).thenReturn(find);
+ when(find.iterator()).thenReturn(cursor);
+
+ assertStopDoesNotWaitForTheBlockedThread(
+
"mongodb:myDb?database=test&collection=events&tailTrackIncreasingField=increasing",
+ MongoDbTailingThread.class);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ void changeStreamsConsumerStopsWhileItsThreadIsBlockedReadingTheCursor()
throws Exception {
+ MongoChangeStreamCursor<?> cursor =
mock(MongoChangeStreamCursor.class);
+ when(cursor.hasNext()).thenAnswer(i -> readThatDoesNotReturn());
+ ChangeStreamIterable<Document> watch =
mock(ChangeStreamIterable.class);
+ when(collection.watch()).thenReturn(watch);
+ when(watch.fullDocument(any(FullDocument.class))).thenReturn(watch);
+ when(watch.iterator()).thenReturn((MongoChangeStreamCursor) cursor);
+
+ assertStopDoesNotWaitForTheBlockedThread(
+
"mongodb:myDb?database=test&collection=events&consumerType=changeStreams",
+ MongoDbChangeStreamsThread.class);
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ void stopWaitsForTheRouteWithoutInterruptingIt() throws Exception {
+ MongoCursor<Document> cursor = mock(MongoCursor.class);
+ when(cursor.hasNext()).thenReturn(true, false);
+ when(cursor.next()).thenReturn(new Document("increasing", 1));
+ FindIterable<Document> find = mock(FindIterable.class);
+ when(collection.find()).thenReturn(find);
+ when(find.cursorType(CursorType.TailableAwait)).thenReturn(find);
+ when(find.iterator()).thenReturn(cursor);
+
+ // the route takes longer than the consumer waits for a thread that
reads from MongoDB
+ CountDownLatch routing = new CountDownLatch(1);
+ CountDownLatch routeMayEnd = new CountDownLatch(1);
+ AtomicBoolean interruptedInRoute = new AtomicBoolean();
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from("mongodb:myDb?database=test&collection=events&tailTrackIncreasingField=increasing")
+ .routeId("mongo")
+ .process(exchange -> {
+ routing.countDown();
+ try {
+ if (!routeMayEnd.await(20, TimeUnit.SECONDS)) {
+ throw new IllegalStateException("the test
did not let the route end");
+ }
+ } catch (InterruptedException e) {
+ interruptedInRoute.set(true);
+ throw e;
+ }
+ });
+ }
+ });
+ context.start();
+ assertTrue(routing.await(10, TimeUnit.SECONDS), "the route processes
the exchange");
+
+ CountDownLatch stopped = new CountDownLatch(1);
+ AtomicReference<Exception> failure = new AtomicReference<>();
+ Thread stopper = stopRouteInAnotherThread(stopped, failure);
+ // well after the 500 millis that the stop waits for a thread that
reads from MongoDB
+ await().atMost(5, TimeUnit.SECONDS).during(1500,
TimeUnit.MILLISECONDS).until(() -> stopped.getCount() == 1);
+
+ routeMayEnd.countDown();
+ assertTrue(stopped.await(10, TimeUnit.SECONDS), "the route stops once
the processor returned");
+ stopper.join(TimeUnit.SECONDS.toMillis(10));
+ assertNull(failure.get());
+ assertEquals(ServiceStatus.Stopped,
context.getRouteController().getRouteStatus("mongo"));
+ assertFalse(interruptedInRoute.get(), "the thread that routes the
exchange is not interrupted");
+ }
+
+ /**
+ * A read from a connection that does not answer: like a socket read,
neither closing the cursor nor an interrupt
+ * ends it.
+ */
+ private boolean readThatDoesNotReturn() {
+ reading.countDown();
+ boolean interrupted = false;
+ try {
+ while (true) {
+ try {
+ answer.await();
+ return false;
+ } catch (InterruptedException e) {
+ interrupted = true;
+ interruptedWhileReading.set(true);
+ }
+ }
+ } finally {
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+ private void assertStopDoesNotWaitForTheBlockedThread(String uri, Class<?>
threadClass) throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from(uri).routeId("mongo").to("mock:result");
+ }
+ });
+ context.start();
+ assertTrue(reading.await(10, TimeUnit.SECONDS), "the consumer thread
reads from the cursor");
+
+ List<LogEvent> events = new CopyOnWriteArrayList<>();
+ AbstractAppender appender = new AbstractAppender("consumerStop", null,
null, true, Property.EMPTY_ARRAY) {
+ @Override
+ public void append(LogEvent event) {
+ events.add(event.toImmutable());
+ }
+ };
+ appender.start();
+ Logger logger = (Logger) LogManager.getLogger(threadClass);
+ logger.addAppender(appender);
+ CountDownLatch stopped = new CountDownLatch(1);
+ AtomicReference<Exception> failure = new AtomicReference<>();
+ try {
+ stopRouteInAnotherThread(stopped, failure);
+ assertTrue(stopped.await(10, TimeUnit.SECONDS),
+ "the route stops while the consumer thread is blocked
reading the cursor");
+ } finally {
+ logger.removeAppender(appender);
+ appender.stop();
+ }
+
+ assertNull(failure.get());
+ assertEquals(ServiceStatus.Stopped,
context.getRouteController().getRouteStatus("mongo"));
+ assertTrue(interruptedWhileReading.get(), "the consumer thread left
behind is interrupted");
+ List<String> warnings = events.stream().filter(e -> e.getLevel() ==
Level.WARN)
+ .map(e -> e.getMessage().getFormattedMessage()).toList();
+ assertEquals(1, warnings.size(), "one WARN for the consumer thread
left behind: " + warnings);
+ assertTrue(warnings.get(0).contains("did not end within 500 millis"),
warnings.get(0));
+ }
+
+ private Thread stopRouteInAnotherThread(CountDownLatch stopped,
AtomicReference<Exception> failure) {
+ Thread stopper = new Thread(() -> {
+ try {
+ context.getRouteController().stopRoute("mongo");
+ } catch (Exception e) {
+ failure.set(e);
+ } finally {
+ stopped.countDown();
+ }
+ }, "stopper");
+ stopper.start();
+ return stopper;
+ }
+}