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

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static com.mongodb.internal.Locks.withLock;
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument;
import static com.mongodb.internal.connection.ClientMetadataHelper.updateClientMedataDocument;
import static com.mongodb.internal.connection.ClientMetadataHelper.updateClientMetadataDocument;

/**
* 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 class ClientMetadata {
private final ReentrantLock updateLock = new ReentrantLock();
private volatile BsonDocument clientMetadataBsonDocument;
Copy link
Contributor

@nhachicha nhachicha May 27, 2025

Choose a reason for hiding this comment

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

If you remove the volatile, then I think you need to use the updateLock.readLock() in getBsonDocument

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I intended to use the readLock() here. Thanks for catching that - updated accordingly!

private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private BsonDocument clientMetadataBsonDocument;

public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) {
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
withLock(readWriteLock.writeLock(), () -> {
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, mongoDriverInformation);
});
}

/**
Expand All @@ -47,8 +49,8 @@ public BsonDocument getBsonDocument() {
}

public void append(final MongoDriverInformation mongoDriverInformation) {
withLock(updateLock, () ->
this.clientMetadataBsonDocument = updateClientMedataDocument(clientMetadataBsonDocument.clone(), mongoDriverInformation)
withLock(readWriteLock.writeLock(), () ->
this.clientMetadataBsonDocument = updateClientMetadataDocument(clientMetadataBsonDocument.clone(), mongoDriverInformation)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,29 +180,30 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build());
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
}
/**
* 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.
*
* Platform is appended separately to the existing platform if it does not exceed {@value MAXIMUM_CLIENT_METADATA_ENCODED_SIZE} bytes.
*/
public static BsonDocument updateClientMedataDocument(final BsonDocument clientMetadataDocument,
final MongoDriverInformation mongoDriverInformation) {
BsonDocument driverInformation = clientMetadataDocument.getDocument("driver");

List<String> driverNamesToAppend = mongoDriverInformation.getDriverNames();
List<String> driverVersionsToAppend = mongoDriverInformation.getDriverVersions();
List<String> driverPlatformsToAppend = mongoDriverInformation.getDriverPlatforms();
/**
* 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.
*/
public 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(driverInformation.getString("name").getValue());
updatedDriverNames.add(currentDriverInformation.getString("name").getValue());
updatedDriverNames.addAll(driverNamesToAppend);

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

updateDriverPlatforms.add(clientMetadataDocument.getString("platform").getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import static com.mongodb.client.WithWrapper.withWrapper;
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument;
import static com.mongodb.internal.connection.ClientMetadataHelper.getOperatingSystemType;
import static com.mongodb.internal.connection.ClientMetadataHelper.updateClientMedataDocument;
import static com.mongodb.internal.connection.ClientMetadataHelper.updateClientMetadataDocument;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -349,7 +349,7 @@ void testUpdateClientMetadataDocument() {
.build();

//when
BsonDocument updatedClientMetadata = updateClientMedataDocument(initialClientMetadataDocument, metadataToAppend);
BsonDocument updatedClientMetadata = updateClientMetadataDocument(initialClientMetadataDocument, metadataToAppend);

//then
assertEquals(
Expand Down