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 22 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Add lock for updates.
  • Loading branch information
vbabanin committed May 21, 2025
commit a8dc4fbdb2445c1d5c110f9b8ef50a7e70ea4b54
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.mongodb.lang.Nullable;
import org.bson.BsonDocument;

import java.util.concurrent.locks.ReentrantLock;

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

Expand All @@ -29,6 +32,7 @@
* <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
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

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


public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) {
Expand All @@ -43,7 +47,9 @@ public BsonDocument getBsonDocument() {
}

public void append(final MongoDriverInformation mongoDriverInformation) {
this.clientMetadataBsonDocument = updateClientMedataDocument(clientMetadataBsonDocument, mongoDriverInformation);
withLock(updateLock, () ->
this.clientMetadataBsonDocument = updateClientMedataDocument(clientMetadataBsonDocument, mongoDriverInformation)
);
}
}