davsclaus opened a new pull request, #30547: URL: https://github.com/apache/camel/pull/30547
## Summary [CAMEL-27193](https://issues.apache.org/jira/browse/CAMEL-27193): the `mongodb` endpoint declares many client options, but `MongoDbEndpoint.createMongoClientSettings()` built the settings from the connection string and the TLS options only (CAMEL-27218, #30362). These options were never used: `replicaSet`, `readPreference`, `readPreferenceTags`, `maxStalenessSeconds`, `appName`, `compressors`, `zlibCompressionLevel`, `connectTimeoutMS`, `socketTimeoutMS`, `serverSelectionTimeoutMS`, `localThresholdMS`, `heartbeatFrequencyMS`, `maxIdleTimeMS`, `maxLifeTimeMS`, `minPoolSize`, `maxPoolSize`, `maxConnecting`, `waitQueueTimeoutMS`, `srvServiceName`, `srvMaxHosts`, `retryWrites`, `retryReads`, `directConnection` and `loadBalanced`. For example, a tailable consumer with `readPreference=SECONDARY` still read from the primary. The endpoint now applies them to the client it creates from `hosts` or `connectionUriString`: cluster, server, socket and connection pool settings, the read preference, `applicationName`, `compressorList`, `retryWrites` and `retryReads`. This unblocks a `readPreference` option on the `mongodb-*` Kamelets (apache/camel-kamelets#3280). ## Decisions **The same option in `connectionUriString`:** an endpoint option is applied only when its value differs from its default, and then it overrides the connection string. An option left at its default keeps the value of the connection string. So `connectionUriString=...?readPreference=nearest` still reads from the nearest member, which already worked before this change. A `PRIMARY` default applied on top would have broken it. The defaults are the driver defaults, so an endpoint that sets none of these options gets the same client as before. The one edge case: setting an option explicitly to its default value (for example `maxPoolSize=100`) does not override a different value in the connection string. The fields keep their current defaults and primitive types, so no getter or setter changes. **The read preference:** `readPreference`, `readPreferenceTags` and `maxStalenessSeconds` together form one read preference. When any of the three is set, they replace the read preference of the connection string. **A `MongoClient` the endpoint did not create** (set with `mongoConnection`, on the component, or a registry bean): its settings cannot be changed, so these options cannot apply to it. This is documented on the component page and in the upgrade guide. The endpoint also logs a WARN at start that lists the options it does not use. I did not apply `readPreference` to such a client through `MongoCollection.withReadPreference(...)`, as the JIRA suggests. The producer's `dynamicity` path takes other databases straight from the client, so it would only partly work. Configure the client instead. **Smaller fixes on the way:** - The documented `readPreference` values `PRIMARY_PREFERRED` and `SECONDARY_PREFERRED` failed in the driver's `ReadPreference.valueOf` ("No match for read preference"). The underscore is now removed before the lookup. The driver names (`secondaryPreferred`) still work. - `readPreferenceTags` takes tag sets separated by `;` (`dc:ny,rack:1;dc:ny;`). The connection-string form that the option's description showed (`dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=`) is accepted too. - An invalid value (an unknown compressor, or tags with `PRIMARY`) now makes the endpoint fail to start. ## Upgrade guide There is an entry in `camel-4x-upgrade-guide-4_24.adoc`: options that were silently ignored now take effect. For example, `readPreference=SECONDARY` now reads from a secondary, and `replicaSet` with one host now discovers the other members. ## Tests - `MongoDbEndpointClientOptionsTest` (22 unit tests) checks the built `MongoClientSettings`: defaults equal the driver defaults, each option is applied, connection-string values are kept or overridden, tag and compressor parsing, invalid values fail, `getMongoCollection().getReadPreference()` matches, and a registry client lists the options it does not use. - `MongoDbClientOptionsIT` (testcontainers) checks that `appName` reaches the server (`currentOp` filtered by `appName`), and that `readPreference=SECONDARY_PREFERRED` is the read preference of the endpoint's collection and a count works. Both tests fail without the fix. - Run locally: all `MongoDbEndpoint*Test` tests, plus `MongoDbClientOptionsIT`, `MongoDbReadPreferenceOptionIT`, `MongoDbTailableCursorConsumerIT`, `MongoDbOperationsIT` and `MongoDbChangeStreamsConsumerIT`. The test container is a one-member replica set that is reached directly, so a 3-member reproducer that reads from a secondary was not practical as an IT. The unit tests cover the read preference on the created client. Regenerated: the component JSON, the catalog JSON and `MongoDbEndpointBuilderFactory` (the `readPreferenceTags` description changed). This PR adds `assertj-core` as a test dependency of camel-mongodb. _Claude Code on behalf of Claus Ibsen_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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]
