Claus Ibsen created CAMEL-27193:
-----------------------------------
Summary: camel-mongodb - readPreference and other connection
options are never applied
Key: CAMEL-27193
URL: https://issues.apache.org/jira/browse/CAMEL-27193
Project: Camel
Issue Type: Bug
Components: camel-mongodb
Reporter: Claus Ibsen
h3. What happens
The endpoint option {{readPreference}} has no effect. With
{{readPreference=SECONDARY}} on a {{mongodb}} endpoint, the reads still go to
the primary of the replica set.
{{MongoDbEndpoint.getReadPreferenceBean()}} turns the option into a
{{com.mongodb.ReadPreference}}, but nothing calls it. The same is true for the
other connection options declared on the endpoint (present since 4.0.0): their
getters have no caller in {{camel-mongodb}}, so setting them changes nothing:
{noformat}
replicaSet, readPreference, readPreferenceTags, maxStalenessSeconds, appName,
compressors, zlibCompressionLevel, connectTimeoutMS, socketTimeoutMS,
serverSelectionTimeoutMS, localThresholdMS, heartbeatFrequencyMS, maxIdleTimeMS,
maxLifeTimeMS, minPoolSize, maxPoolSize, maxConnecting, waitQueueTimeoutMS,
srvServiceName, srvMaxHosts, retryWrites, retryReads, directConnection,
loadBalanced
{noformat}
Only {{authSource}} (put into the connection string) and {{writeConcern}}
(applied by the producer with {{withWriteConcern}}) are used. The same code is
in 4.0.0, 4.18 and 4.23.
h3. How to reproduce
A tailable cursor consumer on a 3-member replica set:
{code}
mongodb:mongo?hosts=localhost:37101,localhost:37102,localhost:37103&database=shop&collection=events&consumerType=tailable&tailTrackIncreasingField=seq&readPreference=SECONDARY
{code}
{{$currentOp}} with {{idleCursors: true}} shows the tailable cursor on the
primary. With the read preference in the connection string instead
({{connectionUriString=mongodb://...?replicaSet=rs0&readPreference=secondaryPreferred}}),
the cursor is on a secondary, as expected.
The Kamelet {{mongodb-source}} cannot offer a {{readPreference}} option because
of this; it documents the connection string as the way to set it.
h3. Where
{{components/camel-mongodb/src/main/java/org/apache/camel/component/mongodb/MongoDbEndpoint.java}}
* {{resolveMongoConnection()}}: builds
{{mongodb://<credentials><hosts>/?authSource=...}} (or uses
{{connectionUriString}} as is) and calls {{MongoClients.create(...)}}; none of
the options above is added.
* {{getReadPreferenceBean()}}: no caller.
h3. Suggested fix
When the endpoint creates its client (from {{hosts}} or
{{connectionUriString}}), build {{MongoClientSettings}} with
{{applyConnectionString(...)}} and then apply each of these options that was
set on the endpoint (cluster, connection pool, socket, server and read
preference settings, {{applicationName}}, {{compressorList}}, {{retryWrites}},
{{retryReads}}). Options that are not set should not override what the
connection string says: today a {{readPreference}} in {{connectionUriString}}
works, and a default of {{PRIMARY}} applied on top would break it. For a client
from the registry or {{mongoConnection}}, {{readPreference}} could still be
applied with {{MongoCollection.withReadPreference(...)}} when it is set.
Add a test that checks the settings of the created client (e.g.
{{getReadPreference()}} on the collection, the pool size).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)