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-kamelets.git


The following commit(s) were added to refs/heads/main by this push:
     new ed80b0913 Fix #3280: readPreference on mongodb-source and 
mongodb-changes-stream-source (#3323)
ed80b0913 is described below

commit ed80b0913500768f1ae81602ff7792b9a5dddc19
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 21 23:41:53 2026 +0100

    Fix #3280: readPreference on mongodb-source and 
mongodb-changes-stream-source (#3323)
    
    * Fix #3280: readPreference on mongodb-source and 
mongodb-changes-stream-source
    
    CAMEL-27193 makes the readPreference option of the mongodb endpoint
    work (it was never applied before 4.24). Both source Kamelets get it as
    an option, passed on to the endpoint. mongodb-sink does not: it only
    writes, which always goes to the primary.
    
    The mongodb-source test checks with $currentOp that the tailable cursor
    of the Kamelet is opened with the read preference secondaryPreferred.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
    
    * chore: pass readPreference to the SslAwareMongoClient of the MongoDB 
Kamelets (#3280)
    
    The Kamelets set their client on the component as mongoConnection, so
    the endpoint options of CAMEL-27193 do not reach it. CAMEL-27346 adds a
    readPreference property to SslAwareMongoClient, which the template now
    sets. With connectionString the option is not used; the connection
    string says the read preference instead.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Claude-Session: https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../mongodb-changes-stream-source.kamelet.yaml     |  7 ++++
 kamelets/mongodb-source.kamelet.yaml               |  9 +++++-
 .../main/resources/META-INF/kamelets-index.json    |  2 +-
 .../mongodb-changes-stream-source.kamelet.yaml     |  7 ++++
 .../resources/kamelets/mongodb-source.kamelet.yaml |  9 +++++-
 .../mongo/mongodb-changes-stream-source-route.yaml |  2 ++
 .../mongo/mongodb-source-route.citrus.it.yaml      |  5 +++
 .../test/resources/mongo/mongodb-source-route.yaml |  2 ++
 .../src/test/resources/mongo/readPreference.groovy | 37 ++++++++++++++++++++++
 9 files changed, 77 insertions(+), 3 deletions(-)

diff --git a/kamelets/mongodb-changes-stream-source.kamelet.yaml 
b/kamelets/mongodb-changes-stream-source.kamelet.yaml
index 938b1bee6..4bc5d15ce 100644
--- a/kamelets/mongodb-changes-stream-source.kamelet.yaml
+++ b/kamelets/mongodb-changes-stream-source.kamelet.yaml
@@ -106,6 +106,12 @@ spec:
         description: "A reference to a `StateRepository` bean that stores the 
resume token after each change, such as `#changeStreamTokens`, so that after a 
restart the Kamelet goes on from where it stopped. Declare the bean in the 
integration, for example an 
`org.apache.camel.support.processor.state.FileStateRepository` whose 
`fileStore` property is set to 
`#class:java.io.File('/data/change-stream-tokens.dat')`. Without it, the 
changes made while the Kamelet is stopped are not sent."
         type: string
         example: "#changeStreamTokens"
+      readPreference:
+        title: Read Preference
+        description: "Which members of the replica set the Kamelet reads from: 
`PRIMARY` (the primary only), `PRIMARY_PREFERRED` (the primary, or a secondary 
while there is no primary), `SECONDARY` (a secondary only), 
`SECONDARY_PREFERRED` (a secondary, or the primary when there is no secondary) 
or `NEAREST` (the member with the lowest network latency). Reading from a 
secondary takes load off the primary, but a secondary can be a little behind 
it. With `SECONDARY` and no secondary availa [...]
+        type: string
+        enum: ["PRIMARY", "PRIMARY_PREFERRED", "SECONDARY", 
"SECONDARY_PREFERRED", "NEAREST"]
+        example: SECONDARY_PREFERRED
   dataTypes:
     out:
       default: json
@@ -141,6 +147,7 @@ spec:
           hosts: "{{hosts:}}"
           ssl: "{{ssl}}"
           sslValidationEnabled: "{{sslValidationEnabled}}"
+          readPreference: "{{?readPreference}}"
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
         properties:
diff --git a/kamelets/mongodb-source.kamelet.yaml 
b/kamelets/mongodb-source.kamelet.yaml
index b0224899c..97027d911 100644
--- a/kamelets/mongodb-source.kamelet.yaml
+++ b/kamelets/mongodb-source.kamelet.yaml
@@ -38,7 +38,7 @@ spec:
 
       Output: the body is one document as JSON text in bytes, and the 
`Content-Type` header is `application/json`. An `_id` that is an ObjectId comes 
out as an object with only its time (`timestamp` and `date`), not as the ID 
text, and the format of dates depends on the Camel version. The 
`CamelMongoDbDatabase` and `CamelMongoDbCollection` headers name where the 
document comes from.
 
-      Good to know: the collection must be capped (created with a fixed size), 
or the Kamelet fails to start. Each time the integration starts, it reads the 
whole collection again from the start, unless `persistentTailTracking` is true. 
It stores its place only when the integration stops, so after a crash it reads 
again the documents since the last stop. Set either `hosts` or 
`connectionString`. With `hosts`, it connects with TLS by default; set `ssl` to 
false for a MongoDB without TLS,  [...]
+      Good to know: the collection must be capped (created with a fixed size), 
or the Kamelet fails to start. Each time the integration starts, it reads the 
whole collection again from the start, unless `persistentTailTracking` is true. 
It stores its place only when the integration stops, so after a crash it reads 
again the documents since the last stop. Set either `hosts` or 
`connectionString`. With `hosts`, it connects with TLS by default; set `ssl` to 
false for a MongoDB without TLS,  [...]
     required:
       - collection
       - database
@@ -118,6 +118,12 @@ spec:
         type: string
         pattern: "^[A-Za-z0-9_.-]+$"
         example: trackers
+      readPreference:
+        title: Read Preference
+        description: "Which members of the replica set the Kamelet reads from: 
`PRIMARY` (the primary only), `PRIMARY_PREFERRED` (the primary, or a secondary 
while there is no primary), `SECONDARY` (a secondary only), 
`SECONDARY_PREFERRED` (a secondary, or the primary when there is no secondary) 
or `NEAREST` (the member with the lowest network latency). Reading from a 
secondary takes load off the primary, but a secondary can be a little behind 
it. With `SECONDARY` and no secondary availa [...]
+        type: string
+        enum: ["PRIMARY", "PRIMARY_PREFERRED", "SECONDARY", 
"SECONDARY_PREFERRED", "NEAREST"]
+        example: SECONDARY_PREFERRED
   dataTypes:
     out:
       default: json
@@ -153,6 +159,7 @@ spec:
           hosts: "{{hosts:}}"
           ssl: "{{ssl}}"
           sslValidationEnabled: "{{sslValidationEnabled}}"
+          readPreference: "{{?readPreference}}"
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
         properties:
diff --git 
a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json 
b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
index 32afd0f08..01bb20cc5 100644
--- a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
+++ b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
@@ -1465,7 +1465,7 @@
     "supportLevel" : "Stable",
     "title" : "MongoDB Source",
     "summary" : "Reads the documents of a capped collection in a MongoDB 
database, first those already there and then each new one as it is added, one 
message per document, as JSON text.",
-    "description" : "Reads the documents of a capped collection in a MongoDB 
database, first those already there and then each new one as it is added, one 
message per document, as JSON text.\n\nUse it to pass on records that an 
application appends to a capped collection, such as log entries or events, as 
they arrive. For a normal collection, or to also see updates and deletes, use 
`mongodb-changes-stream-source`. To write documents, use 
`mongodb-sink`.\n\nOutput: the body is one document [...]
+    "description" : "Reads the documents of a capped collection in a MongoDB 
database, first those already there and then each new one as it is added, one 
message per document, as JSON text.\n\nUse it to pass on records that an 
application appends to a capped collection, such as log entries or events, as 
they arrive. For a normal collection, or to also see updates and deletes, use 
`mongodb-changes-stream-source`. To write documents, use 
`mongodb-sink`.\n\nOutput: the body is one document [...]
     "group" : "MongoDB",
     "namespace" : "Nosql",
     "provider" : "Apache Software Foundation"
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
index 938b1bee6..4bc5d15ce 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-changes-stream-source.kamelet.yaml
@@ -106,6 +106,12 @@ spec:
         description: "A reference to a `StateRepository` bean that stores the 
resume token after each change, such as `#changeStreamTokens`, so that after a 
restart the Kamelet goes on from where it stopped. Declare the bean in the 
integration, for example an 
`org.apache.camel.support.processor.state.FileStateRepository` whose 
`fileStore` property is set to 
`#class:java.io.File('/data/change-stream-tokens.dat')`. Without it, the 
changes made while the Kamelet is stopped are not sent."
         type: string
         example: "#changeStreamTokens"
+      readPreference:
+        title: Read Preference
+        description: "Which members of the replica set the Kamelet reads from: 
`PRIMARY` (the primary only), `PRIMARY_PREFERRED` (the primary, or a secondary 
while there is no primary), `SECONDARY` (a secondary only), 
`SECONDARY_PREFERRED` (a secondary, or the primary when there is no secondary) 
or `NEAREST` (the member with the lowest network latency). Reading from a 
secondary takes load off the primary, but a secondary can be a little behind 
it. With `SECONDARY` and no secondary availa [...]
+        type: string
+        enum: ["PRIMARY", "PRIMARY_PREFERRED", "SECONDARY", 
"SECONDARY_PREFERRED", "NEAREST"]
+        example: SECONDARY_PREFERRED
   dataTypes:
     out:
       default: json
@@ -141,6 +147,7 @@ spec:
           hosts: "{{hosts:}}"
           ssl: "{{ssl}}"
           sslValidationEnabled: "{{sslValidationEnabled}}"
+          readPreference: "{{?readPreference}}"
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
         properties:
diff --git 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
index b0224899c..97027d911 100644
--- 
a/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
+++ 
b/library/camel-kamelets/src/main/resources/kamelets/mongodb-source.kamelet.yaml
@@ -38,7 +38,7 @@ spec:
 
       Output: the body is one document as JSON text in bytes, and the 
`Content-Type` header is `application/json`. An `_id` that is an ObjectId comes 
out as an object with only its time (`timestamp` and `date`), not as the ID 
text, and the format of dates depends on the Camel version. The 
`CamelMongoDbDatabase` and `CamelMongoDbCollection` headers name where the 
document comes from.
 
-      Good to know: the collection must be capped (created with a fixed size), 
or the Kamelet fails to start. Each time the integration starts, it reads the 
whole collection again from the start, unless `persistentTailTracking` is true. 
It stores its place only when the integration stops, so after a crash it reads 
again the documents since the last stop. Set either `hosts` or 
`connectionString`. With `hosts`, it connects with TLS by default; set `ssl` to 
false for a MongoDB without TLS,  [...]
+      Good to know: the collection must be capped (created with a fixed size), 
or the Kamelet fails to start. Each time the integration starts, it reads the 
whole collection again from the start, unless `persistentTailTracking` is true. 
It stores its place only when the integration stops, so after a crash it reads 
again the documents since the last stop. Set either `hosts` or 
`connectionString`. With `hosts`, it connects with TLS by default; set `ssl` to 
false for a MongoDB without TLS,  [...]
     required:
       - collection
       - database
@@ -118,6 +118,12 @@ spec:
         type: string
         pattern: "^[A-Za-z0-9_.-]+$"
         example: trackers
+      readPreference:
+        title: Read Preference
+        description: "Which members of the replica set the Kamelet reads from: 
`PRIMARY` (the primary only), `PRIMARY_PREFERRED` (the primary, or a secondary 
while there is no primary), `SECONDARY` (a secondary only), 
`SECONDARY_PREFERRED` (a secondary, or the primary when there is no secondary) 
or `NEAREST` (the member with the lowest network latency). Reading from a 
secondary takes load off the primary, but a secondary can be a little behind 
it. With `SECONDARY` and no secondary availa [...]
+        type: string
+        enum: ["PRIMARY", "PRIMARY_PREFERRED", "SECONDARY", 
"SECONDARY_PREFERRED", "NEAREST"]
+        example: SECONDARY_PREFERRED
   dataTypes:
     out:
       default: json
@@ -153,6 +159,7 @@ spec:
           hosts: "{{hosts:}}"
           ssl: "{{ssl}}"
           sslValidationEnabled: "{{sslValidationEnabled}}"
+          readPreference: "{{?readPreference}}"
       - name: local-mongodb
         type: "#class:org.apache.camel.component.mongodb.MongoDbComponent"
         properties:
diff --git 
a/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-changes-stream-source-route.yaml
 
b/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-changes-stream-source-route.yaml
index a4c68d313..dad4845ce 100644
--- 
a/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-changes-stream-source-route.yaml
+++ 
b/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-changes-stream-source-route.yaml
@@ -23,6 +23,8 @@
         database: "testdb"
         collection: "orders"
         ssl: false
+        # a replica set with one member has no secondary, so the Kamelet reads 
from the primary;
+        readPreference: SECONDARY_PREFERRED
       steps:
         # what the Kamelet gives: the Java type of the body and the 
Content-Type
         - setHeader:
diff --git 
a/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.citrus.it.yaml
 
b/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.citrus.it.yaml
index 6c98b4461..978855ca1 100644
--- 
a/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.citrus.it.yaml
+++ 
b/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.citrus.it.yaml
@@ -60,3 +60,8 @@ actions:
           status: 200
           reasonPhrase: "OK"
           version: "HTTP/1.1"
+
+  # The tailable cursor stays open, and MongoDB shows that it reads with the 
readPreference of the Kamelet
+  - groovy:
+      script:
+        file: "mongo/readPreference.groovy"
diff --git 
a/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.yaml 
b/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.yaml
index 71d565f00..ee29851ea 100644
--- 
a/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.yaml
+++ 
b/tests/camel-kamelets-itest/src/test/resources/mongo/mongodb-source-route.yaml
@@ -25,6 +25,8 @@
         ssl: "{{mongodb.ssl}}"
         sslValidationEnabled: "{{mongodb.sslValidationEnabled}}"
         persistentTailTracking: false
+        # a replica set with one member has no secondary, so the Kamelet reads 
from the primary;
+        readPreference: SECONDARY_PREFERRED
       steps:
         # what the Kamelet gives: the Java type of the body and the 
Content-Type
         - setHeader:
diff --git 
a/tests/camel-kamelets-itest/src/test/resources/mongo/readPreference.groovy 
b/tests/camel-kamelets-itest/src/test/resources/mongo/readPreference.groovy
new file mode 100644
index 000000000..c7f32cdea
--- /dev/null
+++ b/tests/camel-kamelets-itest/src/test/resources/mongo/readPreference.groovy
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+// Checks that the open cursor of the Kamelet on the collection "testcol" (the 
tailable cursor of
+// mongodb-source) was opened with the read preference secondaryPreferred, 
which the Kamelet's
+// readPreference option sets. $currentOp with idleCursors shows the command 
that opened it.
+
+import com.mongodb.client.MongoClients
+import org.bson.Document
+import org.citrusframework.exceptions.ValidationException
+
+def client = 
MongoClients.create('mongodb://${CITRUS_TESTCONTAINERS_MONGODB_HOST}:${CITRUS_TESTCONTAINERS_MONGODB_PORT}')
+try {
+    def pipeline = [
+            new Document('$currentOp', new Document('idleCursors', 
true).append('allUsers', true)),
+            new Document('$match', new Document('type', 
'idleCursor').append('ns', 'testdb.testcol'))]
+    def cursors = client.getDatabase('admin').aggregate(pipeline).into([])
+    def modes = cursors.collect { it.get('cursor', 
Document)?.get('originatingCommand', Document)?.get('$readPreference', 
Document)?.getString('mode') }
+    if (!modes.contains('secondaryPreferred')) {
+        throw new ValidationException('Expected a cursor on testdb.testcol 
opened with the read preference secondaryPreferred, but the cursors have ' + 
modes)
+    }
+} finally {
+    client.close()
+}

Reply via email to