Skip to content

Add Client Metadata Update Support. #1708

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

Merged
merged 48 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
9753f28
Add Client Metadata update support.
vbabanin May 6, 2025
889d5c8
Add prose tests.
vbabanin May 8, 2025
4b3065c
Add prose tests.
vbabanin May 8, 2025
2a684bf
Add ClientMetadata entity.
vbabanin May 8, 2025
28c1844
Update tests.
vbabanin May 8, 2025
371ce0b
Merge branch 'main' into JAVA-5854
vbabanin May 8, 2025
972fe9d
Fix tests.
vbabanin May 8, 2025
bcf9cc8
Fix tests.
vbabanin May 8, 2025
179b262
Spotless fix.
vbabanin May 8, 2025
bc43ba0
Skip tests when auth is enabled.
vbabanin May 9, 2025
3f5fc91
Apply Scala spotless.
vbabanin May 9, 2025
61192d8
Update tests.
vbabanin May 10, 2025
e9f8dd6
Fix tests.
vbabanin May 10, 2025
95c1bb1
Add parametrized tests.
vbabanin May 19, 2025
dd63a49
Apply Kotlin spotless.
vbabanin May 19, 2025
89d67bb
Move update to separate variables.
vbabanin May 19, 2025
8a18d39
Merge branch 'main' into JAVA-5854
vbabanin May 20, 2025
f296d0c
Merge branch 'main' into JAVA-5854
vbabanin May 20, 2025
a8dc4fb
Add lock for updates.
vbabanin May 21, 2025
8ade58b
Clone document before passing it as an argument.
vbabanin May 21, 2025
71350fa
Rename to "appendMetadata".
vbabanin May 21, 2025
9890aa1
Add ReadWriteLock and rename method.
vbabanin May 27, 2025
28f7d88
Add parametrized tests.
vbabanin Jun 4, 2025
246a040
Add readLock.
vbabanin Jun 4, 2025
8a58294
Fix readLock issue.
vbabanin Jun 4, 2025
15ecef8
Merge branch 'main' into JAVA-5854
vbabanin Jun 4, 2025
5c7a6e3
Add Kotlin and Scala API.
vbabanin Jun 6, 2025
f466d08
Add unified tests.
vbabanin Jun 10, 2025
3132541
Merge branch 'main' into JAVA-5854
vbabanin Jun 10, 2025
d961447
Fix static checks.
vbabanin Jun 10, 2025
74d8558
Change specification submodule commit.
vbabanin Jun 10, 2025
31ef18e
Revert specifications submodule URL to upstream repository.
vbabanin Jun 17, 2025
38ba5e6
Update specifications submodule commit hash.
vbabanin Jun 17, 2025
edafc54
Merge branch 'main' into JAVA-5854
vbabanin Jun 17, 2025
6aa0a1e
Merge branch 'main' into JAVA-5854
vbabanin Jun 17, 2025
dab5451
Remove duplicate import of CLIENT_METADATA in MultiServerClusterSpeci…
vbabanin Jun 17, 2025
1550122
Merge branch 'main' into JAVA-5854
vbabanin Jun 19, 2025
b5c4c20
Remove ClientMetadataHelper.
vbabanin Jun 20, 2025
b6b4d67
Update native-image.properties.
vbabanin Jun 20, 2025
d43972d
Refactor tests to use JUnit assertions and remove unused dependencies.
vbabanin Jun 24, 2025
73bdbd8
Fix test.
vbabanin Jun 24, 2025
66a5913
Add internal driver information.
vbabanin Jun 25, 2025
26246f0
Fix tests.
vbabanin Jun 25, 2025
843d890
Merge branch 'main' into JAVA-5854
vbabanin Jun 25, 2025
850ebe7
Add metadata logging.
vbabanin Jun 26, 2025
d0cabe4
Merge branch 'main' into JAVA-5854
vbabanin Jun 30, 2025
cb40b6a
Add test assertion.
vbabanin Jun 30, 2025
c44200f
Fix GSSAPI test.
vbabanin Jun 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/conventions/testing-base.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ testlogger {
showFailedStandardStreams = true
logLevel = LogLevel.LIFECYCLE
}

dependencies {
testImplementation(libs.assertj)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AssertJ has been used in this PR and added to test-base as it is a useful library that could be shared across all modules.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A decision to use a new assertion library seems like something that should be debated a bit more widely, rather than being brought in as part of a PR like this one. How important do you think it is to the assertions required for this PR?

Copy link
Member Author

@vbabanin vbabanin Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that adopting a new assertion or testing approach should generally be a broader team decision. This isn’t a critical for this PR, so I can remove it from this PR. AssertJ has already been used in the Kotlin coroutine module (reference) and is present in the toml file, though it’s not widely used in the codebase. This PR made the library visible to all modules, based on its usefulness in the Hibernate repository, which was the motivation for extending its usage further.

For now, I will revert this change and we can revisit the discussion as a team.

Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ private ServerTuple(final ClusterableServer server, final ServerDescription desc
}
}

AbstractMultiServerCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory) {
super(clusterId, settings, serverFactory);
AbstractMultiServerCluster(final ClusterId clusterId,
final ClusterSettings settings,
final ClusterableServerFactory serverFactory,
final ClientMetadata clientMetadata) {
super(clusterId, settings, serverFactory, clientMetadata);
isTrue("connection mode is multiple", settings.getMode() == MULTIPLE);
clusterType = settings.getRequiredClusterType();
replicaSetName = settings.getRequiredReplicaSetName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

import static com.mongodb.assertions.Assertions.assertNotNull;
import static com.mongodb.assertions.Assertions.isTrue;
Expand Down Expand Up @@ -106,28 +106,38 @@ abstract class BaseCluster implements Cluster {
private final ClusterListener clusterListener;
private final Deque<ServerSelectionRequest> waitQueue = new ConcurrentLinkedDeque<>();
private final ClusterClock clusterClock = new ClusterClock();
private final ClientMetadata clientMetadata;
private Thread waitQueueHandler;

private volatile boolean isClosed;
private volatile ClusterDescription description;

BaseCluster(final ClusterId clusterId, final ClusterSettings settings, final ClusterableServerFactory serverFactory) {
BaseCluster(final ClusterId clusterId,
final ClusterSettings settings,
final ClusterableServerFactory serverFactory,
final ClientMetadata clientMetadata) {
this.clusterId = notNull("clusterId", clusterId);
this.settings = notNull("settings", settings);
this.serverFactory = notNull("serverFactory", serverFactory);
this.clusterListener = singleClusterListener(settings);
ClusterOpeningEvent clusterOpeningEvent = new ClusterOpeningEvent(clusterId);
clusterListener.clusterOpening(clusterOpeningEvent);
this.clusterListener.clusterOpening(clusterOpeningEvent);
logTopologyOpening(clusterId, clusterOpeningEvent);
description = new ClusterDescription(settings.getMode(), UNKNOWN, emptyList(),
this.description = new ClusterDescription(settings.getMode(), UNKNOWN, emptyList(),
settings, serverFactory.getSettings());
this.clientMetadata = clientMetadata;
}

@Override
public ClusterClock getClock() {
return clusterClock;
}

@Override
public ClientMetadata getClientMetadata() {
return clientMetadata;
}

@Override
public ServerTuple selectServer(final ServerSelector serverSelector, final OperationContext operationContext) {
isTrue("open", !isClosed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.internal.connection;

import com.mongodb.MongoDriverInformation;
import com.mongodb.annotations.ThreadSafe;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.internal.build.MongoDriverVersion;
import com.mongodb.lang.Nullable;
Expand All @@ -32,52 +33,50 @@
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;

import static com.mongodb.assertions.Assertions.isTrueArgument;
import static com.mongodb.internal.Locks.withLock;
import static com.mongodb.internal.connection.FaasEnvironment.getFaasEnvironment;
import static java.lang.String.format;
import static java.lang.System.getProperty;
import static java.nio.file.Paths.get;

/**
* Represents metadata of the current MongoClient.
*
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public final class ClientMetadataHelper {
@ThreadSafe
public class ClientMetadata {
private static final String SEPARATOR = "|";

private static final int MAXIMUM_CLIENT_METADATA_ENCODED_SIZE = 512;
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private BsonDocument clientMetadataBsonDocument;

@VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PRIVATE)
static String getOperatingSystemType(final String operatingSystemName) {
if (nameStartsWith(operatingSystemName, "linux")) {
return "Linux";
} else if (nameStartsWith(operatingSystemName, "mac")) {
return "Darwin";
} else if (nameStartsWith(operatingSystemName, "windows")) {
return "Windows";
} else if (nameStartsWith(operatingSystemName, "hp-ux", "aix", "irix", "solaris", "sunos")) {
return "Unix";
} else {
return "unknown";
}
public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) {
withLock(readWriteLock.writeLock(), () -> {
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
});
}

private static String getOperatingSystemName() {
return getProperty("os.name", "unknown");
/**
* Returns mutable BsonDocument that represents the client metadata.
*/
public BsonDocument getBsonDocument() {
return withLock(readWriteLock.readLock(), () -> clientMetadataBsonDocument);
}

private static boolean nameStartsWith(final String name, final String... prefixes) {
for (String prefix : prefixes) {
if (name.toLowerCase().startsWith(prefix.toLowerCase())) {
return true;
}
}
return false;
public void append(final MongoDriverInformation mongoDriverInformation) {
withLock(readWriteLock.writeLock(), () ->
this.clientMetadataBsonDocument = updateClientMetadataDocument(clientMetadataBsonDocument.clone(), mongoDriverInformation)
);
}

public static BsonDocument createClientMetadataDocument(@Nullable final String applicationName,
private static BsonDocument createClientMetadataDocument(@Nullable final String applicationName,
@Nullable final MongoDriverInformation mongoDriverInformation) {
if (applicationName != null) {
isTrueArgument("applicationName UTF-8 encoding length <= 128",
Expand All @@ -103,8 +102,8 @@ public static BsonDocument createClientMetadataDocument(@Nullable final String a

// optional fields:
FaasEnvironment faasEnvironment = getFaasEnvironment();
ContainerRuntime containerRuntime = ContainerRuntime.determineExecutionContainer();
Orchestrator orchestrator = Orchestrator.determineExecutionOrchestrator();
ClientMetadata.ContainerRuntime containerRuntime = ClientMetadata.ContainerRuntime.determineExecutionContainer();
ClientMetadata.Orchestrator orchestrator = ClientMetadata.Orchestrator.determineExecutionOrchestrator();

tryWithLimit(client, d -> putAtPath(d, "platform", listToString(baseDriverInfor.getDriverPlatforms())));
tryWithLimit(client, d -> putAtPath(d, "platform", listToString(fullDriverInfo.getDriverPlatforms())));
Expand All @@ -123,6 +122,44 @@ public static BsonDocument createClientMetadataDocument(@Nullable final String a
return client;
}

/**
* Modifies the given client metadata document by appending the driver information.
* Driver name and version are appended atomically to the existing driver name and version if they do not exceed
* {@value MAXIMUM_CLIENT_METADATA_ENCODED_SIZE} bytes.
* <p>
* Platform is appended separately to the existing platform if it does not exceed {@value MAXIMUM_CLIENT_METADATA_ENCODED_SIZE} bytes.
*/
private static BsonDocument updateClientMetadataDocument(final BsonDocument clientMetadataDocument,
final MongoDriverInformation driverInformationToAppend) {
BsonDocument currentDriverInformation = clientMetadataDocument.getDocument("driver");

List<String> driverNamesToAppend = driverInformationToAppend.getDriverNames();
List<String> driverVersionsToAppend = driverInformationToAppend.getDriverVersions();
List<String> driverPlatformsToAppend = driverInformationToAppend.getDriverPlatforms();

List<String> updatedDriverNames = new ArrayList<>(driverNamesToAppend.size() + 1);
List<String> updatedDriverVersions = new ArrayList<>(driverVersionsToAppend.size() + 1);
List<String> updateDriverPlatforms = new ArrayList<>(driverPlatformsToAppend.size() + 1);

updatedDriverNames.add(currentDriverInformation.getString("name").getValue());
updatedDriverNames.addAll(driverNamesToAppend);

updatedDriverVersions.add(currentDriverInformation.getString("version").getValue());
updatedDriverVersions.addAll(driverVersionsToAppend);

updateDriverPlatforms.add(clientMetadataDocument.getString("platform").getValue());
updateDriverPlatforms.addAll(driverPlatformsToAppend);

tryWithLimit(clientMetadataDocument, d -> {
putAtPath(d, "driver.name", listToString(updatedDriverNames));
putAtPath(d, "driver.version", listToString(updatedDriverVersions));
});
tryWithLimit(clientMetadataDocument, d -> {
putAtPath(d, "platform", listToString(updateDriverPlatforms));
});
return clientMetadataDocument;
}


private static void putAtPath(final BsonDocument d, final String path, @Nullable final String value) {
if (value == null) {
Expand Down Expand Up @@ -180,7 +217,7 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
}

public enum ContainerRuntime {
private enum ContainerRuntime {
DOCKER("docker") {
@Override
boolean isCurrentRuntimeContainer() {
Expand Down Expand Up @@ -210,8 +247,8 @@ boolean isCurrentRuntimeContainer() {
return false;
}

static ContainerRuntime determineExecutionContainer() {
for (ContainerRuntime allegedContainer : ContainerRuntime.values()) {
static ClientMetadata.ContainerRuntime determineExecutionContainer() {
for (ClientMetadata.ContainerRuntime allegedContainer : ClientMetadata.ContainerRuntime.values()) {
if (allegedContainer.isCurrentRuntimeContainer()) {
return allegedContainer;
}
Expand Down Expand Up @@ -245,8 +282,8 @@ boolean isCurrentOrchestrator() {
return false;
}

static Orchestrator determineExecutionOrchestrator() {
for (Orchestrator alledgedOrchestrator : Orchestrator.values()) {
static ClientMetadata.Orchestrator determineExecutionOrchestrator() {
for (ClientMetadata.Orchestrator alledgedOrchestrator : ClientMetadata.Orchestrator.values()) {
if (alledgedOrchestrator.isCurrentOrchestrator()) {
return alledgedOrchestrator;
}
Expand Down Expand Up @@ -279,6 +316,31 @@ private static String listToString(final List<String> listOfStrings) {
return stringBuilder.toString();
}

private ClientMetadataHelper() {
@VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PRIVATE)
public static String getOperatingSystemType(final String operatingSystemName) {
if (nameStartsWith(operatingSystemName, "linux")) {
return "Linux";
} else if (nameStartsWith(operatingSystemName, "mac")) {
return "Darwin";
} else if (nameStartsWith(operatingSystemName, "windows")) {
return "Windows";
} else if (nameStartsWith(operatingSystemName, "hp-ux", "aix", "irix", "solaris", "sunos")) {
return "Unix";
} else {
return "unknown";
}
}

private static String getOperatingSystemName() {
return getProperty("os.name", "unknown");
}

private static boolean nameStartsWith(final String name, final String... prefixes) {
for (String prefix : prefixes) {
if (name.toLowerCase().startsWith(prefix.toLowerCase())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public interface Cluster extends Closeable {
*/
ClusterClock getClock();

ClientMetadata getClientMetadata();

ServerTuple selectServer(ServerSelector serverSelector, OperationContext operationContext);

void selectServerAsync(ServerSelector serverSelector, OperationContext operationContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,29 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
InternalOperationContextFactory heartBeatOperationContextFactory =
new InternalOperationContextFactory(heartbeatTimeoutSettings, serverApi);

ClientMetadata clientMetadata = new ClientMetadata(
applicationName,
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build());

if (clusterSettings.getMode() == ClusterConnectionMode.LOAD_BALANCED) {
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings,
connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, loggerSettings, commandListener,
applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(),
compressorList, serverApi, clusterOperationContextFactory);
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, clientMetadata, dnsSrvRecordMonitorFactory);
} else {
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings,
connectionPoolSettings, internalConnectionPoolSettings,
clusterOperationContextFactory, streamFactory, heartBeatOperationContextFactory, heartbeatStreamFactory, credential,
loggerSettings, commandListener, applicationName,
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,
loggerSettings, commandListener, compressorList,
serverApi, FaasEnvironment.getFaasEnvironment() != FaasEnvironment.UNKNOWN);

if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
return new SingleServerCluster(clusterId, clusterSettings, serverFactory);
return new SingleServerCluster(clusterId, clusterSettings, serverFactory, clientMetadata);
} else if (clusterSettings.getMode() == ClusterConnectionMode.MULTIPLE) {
if (clusterSettings.getSrvHost() == null) {
return new MultiServerCluster(clusterId, clusterSettings, serverFactory);
return new MultiServerCluster(clusterId, clusterSettings, serverFactory, clientMetadata);
} else {
return new DnsMultiServerCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
return new DnsMultiServerCluster(clusterId, clusterSettings, serverFactory, clientMetadata, dnsSrvRecordMonitorFactory);
}
} else {
throw new UnsupportedOperationException("Unsupported cluster mode: " + clusterSettings.getMode());
Expand Down
Loading