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 21 commits into
base: main
Choose a base branch
from
Draft

Conversation

vbabanin
Copy link
Member

@vbabanin vbabanin commented May 6, 2025

Description

This PR updates the Java sync and reactive-streams driver to support dynamic client metadata updates, enabling wrapping libraries and integrations to append metadata about themselves when they do not instantiate the MongoClient directly.

Pull request status:

This pull request is kept in draft until the associated specification (mongodb/specifications#1798) is approved and merged.

JAVA-5870

@vbabanin vbabanin self-assigned this May 8, 2025
Comment on lines +109 to +111
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.

@vbabanin vbabanin requested review from a team and nhachicha and removed request for a team May 8, 2025 05:55
@vbabanin vbabanin requested a review from jyemin May 20, 2025 20:58
Copy link
Collaborator

@jyemin jyemin left a comment

Choose a reason for hiding this comment

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

Initial feedback

* @param mongoDriverInformation the driver information to append to the existing metadata
* @since 5.6
*/
void updateMetadata(MongoDriverInformation mongoDriverInformation);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the argument for putting the method on MongoClient rather than MongoCluster?

Copy link
Member Author

@vbabanin vbabanin May 21, 2025

Choose a reason for hiding this comment

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

Just conceptually, MongoCluster acts as a view with overridden configurations (e.g., codec registry, read preference) and doesn’t own the client’s lifecycle or global state. In my opinion, placing updateMetadata on MongoCluster raises the question: does it update metadata for just that instance or for all instances? That behavior would require clear documentation.

MongoClient already handles driver-wide operations (e.g., close), so updateMetadata aligns with its role.

* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public class ClientMetadata {
private volatile BsonDocument clientMetadataBsonDocument;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't thing that a volatile fields is enough. If append is called concurrently by multiple threads, then one of the updates to metadata will likely be lost.

Copy link
Member Author

@vbabanin vbabanin May 21, 2025

Choose a reason for hiding this comment

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

You're right - volatile alone isn't enough for concurrent updates. I initially didn’t add locking because I expected concurrent metadata updates to be extremely rare in practice, but I’ve now added a lock to ensure consistency. Let me know what you think.

UPD: discussed offline. Removed clone() from updateMetadata(...) for clarify: 8ade58b

@vbabanin vbabanin requested a review from jyemin May 21, 2025 19:19
* Returns mutable BsonDocument that represents the client metadata.
*/
public BsonDocument getBsonDocument() {
return clientMetadataBsonDocument;
Copy link
Contributor

Choose a reason for hiding this comment

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

Return a defensive copy (instead of a reference to this mutable instance?)

Suggested change
return clientMetadataBsonDocument;
return clientMetadataBsonDocument.clone();

*
* 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,
Copy link
Contributor

Choose a reason for hiding this comment

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

typo

Suggested change
public static BsonDocument updateClientMedataDocument(final BsonDocument clientMetadataDocument,
public static BsonDocument updateClientMetadataDocument(final BsonDocument clientMetadataDocument,

*/
public static BsonDocument updateClientMedataDocument(final BsonDocument clientMetadataDocument,
final MongoDriverInformation mongoDriverInformation) {
BsonDocument driverInformation = clientMetadataDocument.getDocument("driver");
Copy link
Contributor

Choose a reason for hiding this comment

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

what if the user doesn't supply the driverName when usingMongoDriverInformation.builder(). Should we handle the NPE?


tryWithLimit(clientMetadataDocument, d -> {
putAtPath(d, "driver.name", listToString(updatedDriverNames));
putAtPath(d, "driver.version", listToString(updatedDriverVersions));
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we try in a dedicated tryWithLimit?

updateDriverPlatforms.addAll(driverPlatformsToAppend);

tryWithLimit(clientMetadataDocument, d -> {
putAtPath(d, "driver.name", listToString(updatedDriverNames));
Copy link
Contributor

Choose a reason for hiding this comment

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

tryWithLimit invokes the Consumer<BsonDocument> lambda twice, we could build the appended string outside the tryWithLimit

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

tryWithLimit(clientMetadataDocument, d -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should tryWithLimit return a boolean indicating whether the document was added (i.e enough space) or not. The boolean will avoid subsequent call to tryWithLimit if it previously failed due to reached capacity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants