-
Notifications
You must be signed in to change notification settings - Fork 5.5k
chore(deps): Upgrading mongodb driver to mongodb driver sync #27685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
197d3d2
25143c2
7f8fcdc
e76b873
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,16 @@ | |
| import com.google.inject.Module; | ||
| import com.google.inject.Provides; | ||
| import com.google.inject.Scopes; | ||
| import com.mongodb.MongoClient; | ||
| import com.mongodb.MongoClientOptions; | ||
| import com.mongodb.ConnectionString; | ||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.ReadPreference; | ||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.client.MongoClients; | ||
| import jakarta.inject.Singleton; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static com.facebook.airlift.configuration.ConfigBinder.configBinder; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
|
|
@@ -46,38 +52,74 @@ public static MongoSession createMongoSession(TypeManager typeManager, MongoClie | |
| { | ||
| requireNonNull(config, "config is null"); | ||
|
|
||
| MongoClientOptions.Builder options = MongoClientOptions.builder() | ||
| .connectionsPerHost(config.getConnectionsPerHost()) | ||
| .connectTimeout(config.getConnectionTimeout()) | ||
| .socketTimeout(config.getSocketTimeout()) | ||
| .socketKeepAlive(config.getSocketKeepAlive()) | ||
| .maxWaitTime(config.getMaxWaitTime()) | ||
| .minConnectionsPerHost(config.getMinConnectionsPerHost()) | ||
| .writeConcern(config.getWriteConcern().getWriteConcern()); | ||
| MongoClientSettings.Builder settingsBuilder = MongoClientSettings.builder(); | ||
|
|
||
| String connectionString = buildConnectionString(config); | ||
| settingsBuilder.applyConnectionString(new ConnectionString(connectionString)); | ||
|
|
||
| if (!config.getCredentials().isEmpty()) { | ||
| if (config.getCredentials().size() > 1) { | ||
| throw new IllegalArgumentException( | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this okay to fail if multiple credentials are present?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure what the intention was for having credential config as a list?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "Multiple credentials are not supported. Only one credential can be configured."); | ||
| } | ||
| settingsBuilder.credential(config.getCredentials().get(0)); | ||
| } | ||
|
|
||
| settingsBuilder.applyToConnectionPoolSettings(builder -> builder | ||
| .maxSize(config.getConnectionsPerHost()) | ||
| .minSize(config.getMinConnectionsPerHost()) | ||
| .maxWaitTime(config.getMaxWaitTime(), TimeUnit.MILLISECONDS)); | ||
|
|
||
| settingsBuilder.applyToSocketSettings(builder -> builder | ||
| .connectTimeout(config.getConnectionTimeout(), TimeUnit.MILLISECONDS) | ||
| .readTimeout(config.getSocketTimeout(), TimeUnit.MILLISECONDS)); | ||
|
|
||
| settingsBuilder.writeConcern(config.getWriteConcern().getWriteConcern()); | ||
|
|
||
| settingsBuilder.readPreference(configureReadPreference(config)); | ||
|
|
||
| if (config.getRequiredReplicaSetName() != null) { | ||
| options.requiredReplicaSetName(config.getRequiredReplicaSetName()); | ||
| settingsBuilder.applyToClusterSettings(builder -> | ||
| builder.requiredReplicaSetName(config.getRequiredReplicaSetName())); | ||
| } | ||
|
|
||
| configureReadPreference(options, config); | ||
| configureSsl(options, config); | ||
| configureSsl(settingsBuilder, config); | ||
|
|
||
| MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build()); | ||
| MongoClient client = MongoClients.create(settingsBuilder.build()); | ||
|
|
||
| return new MongoSession(typeManager, client, config); | ||
| } | ||
|
|
||
| private static void configureReadPreference(MongoClientOptions.Builder options, MongoClientConfig config) | ||
| private static String buildConnectionString(MongoClientConfig config) | ||
| { | ||
| StringBuilder connectionString = new StringBuilder("mongodb://"); | ||
|
|
||
| connectionString.append(config.getSeeds().stream() | ||
| .map(addr -> addr.getHost() + ":" + addr.getPort()) | ||
| .collect(Collectors.joining(","))); | ||
|
|
||
| if (!config.getCredentials().isEmpty()) { | ||
| connectionString.append("/") | ||
| .append(config.getCredentials().get(0).getSource()); | ||
| } | ||
|
|
||
| // Enable replica set discovery when replica set name is configured or multiple seeds are provided | ||
| if (config.getRequiredReplicaSetName() != null || config.getSeeds().size() > 1) { | ||
| connectionString.append("?directConnection=false"); | ||
| } | ||
| return connectionString.toString(); | ||
| } | ||
| private static ReadPreference configureReadPreference(MongoClientConfig config) | ||
| { | ||
| if (config.getReadPreferenceTags().isEmpty()) { | ||
| options.readPreference(config.getReadPreference().getReadPreference()); | ||
| return config.getReadPreference().getReadPreference(); | ||
| } | ||
| else { | ||
| options.readPreference(config.getReadPreference().getReadPreferenceWithTags(config.getReadPreferenceTags())); | ||
| return config.getReadPreference().getReadPreferenceWithTags(config.getReadPreferenceTags()); | ||
| } | ||
| } | ||
|
|
||
| private static void configureSsl(MongoClientOptions.Builder options, MongoClientConfig config) | ||
| private static void configureSsl(MongoClientSettings.Builder settings, MongoClientConfig config) | ||
| { | ||
| if (config.isTlsEnabled()) { | ||
| SslContextProvider sslContextProvider = new SslContextProvider( | ||
|
|
@@ -88,8 +130,9 @@ private static void configureSsl(MongoClientOptions.Builder options, MongoClient | |
|
|
||
| sslContextProvider.buildSslContext() | ||
| .ifPresent(sslContext -> { | ||
| options.sslContext(sslContext); | ||
| options.sslEnabled(true); | ||
| settings.applyToSslSettings(builder -> builder | ||
| .enabled(true) | ||
| .context(sslContext)); | ||
| }); | ||
| } | ||
| } | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
socketKeepAliveis removed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The
socketKeepAlive()method has been removed from the driver API