Skip to content

Commit 9753f28

Browse files
committed
Add Client Metadata update support.
1 parent 912cad7 commit 9753f28

File tree

27 files changed

+191
-76
lines changed

27 files changed

+191
-76
lines changed

driver-core/src/main/com/mongodb/internal/connection/AbstractMultiServerCluster.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ private ServerTuple(final ClusterableServer server, final ServerDescription desc
7777
}
7878
}
7979

80-
AbstractMultiServerCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory) {
81-
super(clusterId, settings, serverFactory);
80+
AbstractMultiServerCluster(final ClusterId clusterId,
81+
final ClusterSettings settings,
82+
final ClusterableServerFactory serverFactory,
83+
final ClientMetadata clientMetadata) {
84+
super(clusterId, settings, serverFactory, clientMetadata);
8285
isTrue("connection mode is multiple", settings.getMode() == MULTIPLE);
8386
clusterType = settings.getRequiredClusterType();
8487
replicaSetName = settings.getRequiredReplicaSetName();

driver-core/src/main/com/mongodb/internal/connection/BaseCluster.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@
5555
import java.util.Iterator;
5656
import java.util.List;
5757
import java.util.Objects;
58-
import java.util.stream.Stream;
5958
import java.util.concurrent.ConcurrentLinkedDeque;
6059
import java.util.concurrent.CountDownLatch;
6160
import java.util.concurrent.atomic.AtomicReference;
6261
import java.util.concurrent.locks.ReentrantLock;
62+
import java.util.stream.Stream;
6363

6464
import static com.mongodb.assertions.Assertions.assertNotNull;
6565
import static com.mongodb.assertions.Assertions.isTrue;
@@ -101,26 +101,36 @@ abstract class BaseCluster implements Cluster {
101101
private final ClusterListener clusterListener;
102102
private final Deque<ServerSelectionRequest> waitQueue = new ConcurrentLinkedDeque<>();
103103
private final ClusterClock clusterClock = new ClusterClock();
104+
private final ClientMetadata clientMetadata;
104105
private Thread waitQueueHandler;
105106

106107
private volatile boolean isClosed;
107108
private volatile ClusterDescription description;
108109

109-
BaseCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory) {
110+
BaseCluster(final ClusterId clusterId,
111+
final ClusterSettings settings,
112+
final ClusterableServerFactory serverFactory,
113+
final ClientMetadata clientMetadata) {
110114
this.clusterId = notNull("clusterId", clusterId);
111115
this.settings = notNull("settings", settings);
112116
this.serverFactory = notNull("serverFactory", serverFactory);
113117
this.clusterListener = singleClusterListener(settings);
114-
clusterListener.clusterOpening(new ClusterOpeningEvent(clusterId));
115-
description = new ClusterDescription(settings.getMode(), ClusterType.UNKNOWN, Collections.emptyList(),
118+
this.clusterListener.clusterOpening(new ClusterOpeningEvent(clusterId));
119+
this.description = new ClusterDescription(settings.getMode(), ClusterType.UNKNOWN, Collections.emptyList(),
116120
settings, serverFactory.getSettings());
121+
this.clientMetadata = clientMetadata;
117122
}
118123

119124
@Override
120125
public ClusterClock getClock() {
121126
return clusterClock;
122127
}
123128

129+
@Override
130+
public ClientMetadata getClientMetadata() {
131+
return clientMetadata;
132+
}
133+
124134
@Override
125135
public ServerTuple selectServer(final ServerSelector serverSelector, final OperationContext operationContext) {
126136
isTrue("open", !isClosed());

driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,31 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
180180
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
181181
}
182182

183+
public static BsonDocument updateClientMedataDocument(final BsonDocument clientMetadataDocument,
184+
final MongoDriverInformation mongoDriverInformation) {
185+
BsonDocument updatedClientMetadataDocument = clientMetadataDocument.clone();
186+
BsonDocument driverInformation = clientMetadataDocument.getDocument("driver");
187+
188+
MongoDriverInformation.Builder builder = MongoDriverInformation.builder(mongoDriverInformation)
189+
.driverName(driverInformation.getString("name").getValue())
190+
.driverVersion(driverInformation.getString("version").getValue());
191+
192+
if (updatedClientMetadataDocument.containsKey("platform")) {
193+
builder.driverPlatform(updatedClientMetadataDocument.getString("platform").getValue());
194+
}
195+
196+
MongoDriverInformation updatedDriverInformation = builder.build();
197+
198+
tryWithLimit(updatedClientMetadataDocument, d -> {
199+
putAtPath(d, "driver.name", listToString(updatedDriverInformation.getDriverNames()));
200+
putAtPath(d, "driver.version", listToString(updatedDriverInformation.getDriverVersions()));
201+
});
202+
tryWithLimit(updatedClientMetadataDocument, d -> {
203+
putAtPath(d, "platform", listToString(updatedDriverInformation.getDriverPlatforms()));
204+
});
205+
return updatedClientMetadataDocument;
206+
}
207+
183208
public enum ContainerRuntime {
184209
DOCKER("docker") {
185210
@Override

driver-core/src/main/com/mongodb/internal/connection/Cluster.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public interface Cluster extends Closeable {
5757
*/
5858
ClusterClock getClock();
5959

60+
ClientMetadata getClientMetadata();
61+
6062
ServerTuple selectServer(ServerSelector serverSelector, OperationContext operationContext);
6163

6264
void selectServerAsync(ServerSelector serverSelector, OperationContext operationContext,

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,29 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
107107
InternalOperationContextFactory heartBeatOperationContextFactory =
108108
new InternalOperationContextFactory(heartbeatTimeoutSettings, serverApi);
109109

110+
ClientMetadata clientMetadata = new ClientMetadata(
111+
applicationName,
112+
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build());
113+
110114
if (clusterSettings.getMode() == ClusterConnectionMode.LOAD_BALANCED) {
111115
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings,
112116
connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, loggerSettings, commandListener,
113-
applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(),
114117
compressorList, serverApi, clusterOperationContextFactory);
115-
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
118+
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, clientMetadata, dnsSrvRecordMonitorFactory);
116119
} else {
117120
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings,
118121
connectionPoolSettings, internalConnectionPoolSettings,
119122
clusterOperationContextFactory, streamFactory, heartBeatOperationContextFactory, heartbeatStreamFactory, credential,
120-
loggerSettings, commandListener, applicationName,
121-
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,
123+
loggerSettings, commandListener, compressorList,
122124
serverApi, FaasEnvironment.getFaasEnvironment() != FaasEnvironment.UNKNOWN);
123125

124126
if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
125-
return new SingleServerCluster(clusterId, clusterSettings, serverFactory);
127+
return new SingleServerCluster(clusterId, clusterSettings, serverFactory, clientMetadata);
126128
} else if (clusterSettings.getMode() == ClusterConnectionMode.MULTIPLE) {
127129
if (clusterSettings.getSrvHost() == null) {
128-
return new MultiServerCluster(clusterId, clusterSettings, serverFactory);
130+
return new MultiServerCluster(clusterId, clusterSettings, serverFactory, clientMetadata);
129131
} else {
130-
return new DnsMultiServerCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
132+
return new DnsMultiServerCluster(clusterId, clusterSettings, serverFactory, clientMetadata, dnsSrvRecordMonitorFactory);
131133
}
132134
} else {
133135
throw new UnsupportedOperationException("Unsupported cluster mode: " + clusterSettings.getMode());

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.mongodb.LoggerSettings;
2020
import com.mongodb.MongoCompressor;
2121
import com.mongodb.MongoCredential;
22-
import com.mongodb.MongoDriverInformation;
2322
import com.mongodb.ServerAddress;
2423
import com.mongodb.ServerApi;
2524
import com.mongodb.connection.ClusterConnectionMode;
@@ -50,8 +49,6 @@ public class DefaultClusterableServerFactory implements ClusterableServerFactory
5049
private final MongoCredentialWithCache credential;
5150
private final LoggerSettings loggerSettings;
5251
private final CommandListener commandListener;
53-
private final String applicationName;
54-
private final MongoDriverInformation mongoDriverInformation;
5552
private final List<MongoCompressor> compressorList;
5653
@Nullable
5754
private final ServerApi serverApi;
@@ -63,8 +60,7 @@ public DefaultClusterableServerFactory(
6360
final InternalOperationContextFactory clusterOperationContextFactory, final StreamFactory streamFactory,
6461
final InternalOperationContextFactory heartbeatOperationContextFactory, final StreamFactory heartbeatStreamFactory,
6562
@Nullable final MongoCredential credential, final LoggerSettings loggerSettings,
66-
@Nullable final CommandListener commandListener, @Nullable final String applicationName,
67-
@Nullable final MongoDriverInformation mongoDriverInformation,
63+
@Nullable final CommandListener commandListener,
6864
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi, final boolean isFunctionAsAServiceEnvironment) {
6965
this.serverSettings = serverSettings;
7066
this.connectionPoolSettings = connectionPoolSettings;
@@ -76,8 +72,6 @@ public DefaultClusterableServerFactory(
7672
this.credential = credential == null ? null : new MongoCredentialWithCache(credential);
7773
this.loggerSettings = loggerSettings;
7874
this.commandListener = commandListener;
79-
this.applicationName = applicationName;
80-
this.mongoDriverInformation = mongoDriverInformation;
8175
this.compressorList = compressorList;
8276
this.serverApi = serverApi;
8377
this.isFunctionAsAServiceEnvironment = isFunctionAsAServiceEnvironment;
@@ -88,15 +82,17 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve
8882
ServerId serverId = new ServerId(cluster.getClusterId(), serverAddress);
8983
ClusterConnectionMode clusterMode = cluster.getSettings().getMode();
9084
SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
85+
ClientMetadata clientMetadata = cluster.getClientMetadata();
86+
9187
ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings,
9288
// no credentials, compressor list, or command listener for the server monitor factory
93-
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName,
94-
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi),
89+
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, clientMetadata,
90+
emptyList(), loggerSettings, null, serverApi),
9591
clusterMode, serverApi, isFunctionAsAServiceEnvironment, sdamProvider, heartbeatOperationContextFactory);
9692

9793
ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
98-
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
99-
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi),
94+
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, clientMetadata,
95+
compressorList, loggerSettings, commandListener, serverApi),
10096
connectionPoolSettings, internalConnectionPoolSettings, sdamProvider, clusterOperationContextFactory);
10197
ServerListener serverListener = singleServerListener(serverSettings);
10298
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor,

driver-core/src/main/com/mongodb/internal/connection/DnsMultiServerCluster.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ public final class DnsMultiServerCluster extends AbstractMultiServerCluster {
4040
private final DnsSrvRecordMonitor dnsSrvRecordMonitor;
4141
private volatile MongoException srvResolutionException;
4242

43-
public DnsMultiServerCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory,
43+
public DnsMultiServerCluster(final ClusterId clusterId, final ClusterSettings settings,
44+
final ClusterableServerFactory serverFactory,
45+
final ClientMetadata clientMetadata,
4446
final DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory) {
45-
super(clusterId, settings, serverFactory);
47+
super(clusterId, settings, serverFactory, clientMetadata);
4648
dnsSrvRecordMonitor = dnsSrvRecordMonitorFactory.create(assertNotNull(settings.getSrvHost()), settings.getSrvServiceName(),
4749
new DnsSrvRecordInitializer() {
4850
private volatile boolean initialized;

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionFactory.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,22 @@
1919
import com.mongodb.AuthenticationMechanism;
2020
import com.mongodb.LoggerSettings;
2121
import com.mongodb.MongoCompressor;
22-
import com.mongodb.MongoDriverInformation;
2322
import com.mongodb.ServerApi;
2423
import com.mongodb.connection.ClusterConnectionMode;
2524
import com.mongodb.connection.ServerId;
2625
import com.mongodb.event.CommandListener;
2726
import com.mongodb.lang.Nullable;
28-
import org.bson.BsonDocument;
2927

3028
import java.util.List;
3129

3230
import static com.mongodb.assertions.Assertions.notNull;
33-
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument;
3431

3532
class InternalStreamConnectionFactory implements InternalConnectionFactory {
3633
private final ClusterConnectionMode clusterConnectionMode;
3734
private final boolean isMonitoringConnection;
3835
private final StreamFactory streamFactory;
39-
private final BsonDocument clientMetadataDocument;
36+
//TODO UPDATE
37+
private final ClientMetadata clientMetadata;
4038
private final List<MongoCompressor> compressorList;
4139
private final LoggerSettings loggerSettings;
4240
private final CommandListener commandListener;
@@ -47,17 +45,17 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory {
4745
InternalStreamConnectionFactory(final ClusterConnectionMode clusterConnectionMode,
4846
final StreamFactory streamFactory,
4947
@Nullable final MongoCredentialWithCache credential,
50-
@Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation,
48+
final ClientMetadata clientMetadata,
5149
final List<MongoCompressor> compressorList,
5250
final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) {
53-
this(clusterConnectionMode, false, streamFactory, credential, applicationName, mongoDriverInformation, compressorList,
51+
this(clusterConnectionMode, false, streamFactory, credential, clientMetadata, compressorList,
5452
loggerSettings, commandListener, serverApi);
5553
}
5654

5755
InternalStreamConnectionFactory(final ClusterConnectionMode clusterConnectionMode, final boolean isMonitoringConnection,
5856
final StreamFactory streamFactory,
5957
@Nullable final MongoCredentialWithCache credential,
60-
@Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation,
58+
final ClientMetadata clientMetadata,
6159
final List<MongoCompressor> compressorList,
6260
final LoggerSettings loggerSettings, @Nullable final CommandListener commandListener, @Nullable final ServerApi serverApi) {
6361
this.clusterConnectionMode = clusterConnectionMode;
@@ -67,15 +65,15 @@ class InternalStreamConnectionFactory implements InternalConnectionFactory {
6765
this.loggerSettings = loggerSettings;
6866
this.commandListener = commandListener;
6967
this.serverApi = serverApi;
70-
this.clientMetadataDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
68+
this.clientMetadata = clientMetadata;
7169
this.credential = credential;
7270
}
7371

7472
@Override
7573
public InternalConnection create(final ServerId serverId, final ConnectionGenerationSupplier connectionGenerationSupplier) {
7674
Authenticator authenticator = credential == null ? null : createAuthenticator(credential);
7775
InternalStreamConnectionInitializer connectionInitializer = new InternalStreamConnectionInitializer(
78-
clusterConnectionMode, authenticator, clientMetadataDocument, compressorList, serverApi);
76+
clusterConnectionMode, authenticator, clientMetadata.getClientMetadataBsonDocument(), compressorList, serverApi);
7977
return new InternalStreamConnection(
8078
clusterConnectionMode, authenticator,
8179
isMonitoringConnection, serverId, connectionGenerationSupplier,

driver-core/src/main/com/mongodb/internal/connection/LoadBalancedCluster.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ final class LoadBalancedCluster implements Cluster {
7676
private final ClusterId clusterId;
7777
private final ClusterSettings settings;
7878
private final ClusterClock clusterClock = new ClusterClock();
79+
private final ClientMetadata clientMetadata;
7980
private final ClusterListener clusterListener;
8081
private ClusterDescription description;
8182
@Nullable
@@ -91,6 +92,7 @@ final class LoadBalancedCluster implements Cluster {
9192
private final Condition condition = lock.newCondition();
9293

9394
LoadBalancedCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory,
95+
final ClientMetadata clientMetadata,
9496
final DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory) {
9597
assertTrue(settings.getMode() == ClusterConnectionMode.LOAD_BALANCED);
9698
LOGGER.info(format("Cluster created with id %s and settings %s", clusterId, settings.getShortDescription()));
@@ -100,6 +102,7 @@ final class LoadBalancedCluster implements Cluster {
100102
this.clusterListener = singleClusterListener(settings);
101103
this.description = new ClusterDescription(settings.getMode(), ClusterType.UNKNOWN, emptyList(), settings,
102104
serverFactory.getSettings());
105+
this.clientMetadata = clientMetadata;
103106

104107
if (settings.getSrvHost() == null) {
105108
dnsSrvRecordMonitor = null;
@@ -204,6 +207,11 @@ public ClusterClock getClock() {
204207
return clusterClock;
205208
}
206209

210+
@Override
211+
public ClientMetadata getClientMetadata() {
212+
return clientMetadata;
213+
}
214+
207215
@Override
208216
public ServerTuple selectServer(final ServerSelector serverSelector, final OperationContext operationContext) {
209217
isTrue("open", !isClosed());

0 commit comments

Comments
 (0)