-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
vbabanin
wants to merge
22
commits into
mongodb:main
Choose a base branch
from
vbabanin:JAVA-5854
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9753f28
Add Client Metadata update support.
vbabanin 889d5c8
Add prose tests.
vbabanin 4b3065c
Add prose tests.
vbabanin 2a684bf
Add ClientMetadata entity.
vbabanin 28c1844
Update tests.
vbabanin 371ce0b
Merge branch 'main' into JAVA-5854
vbabanin 972fe9d
Fix tests.
vbabanin bcf9cc8
Fix tests.
vbabanin 179b262
Spotless fix.
vbabanin bc43ba0
Skip tests when auth is enabled.
vbabanin 3f5fc91
Apply Scala spotless.
vbabanin 61192d8
Update tests.
vbabanin e9f8dd6
Fix tests.
vbabanin 95c1bb1
Add parametrized tests.
vbabanin dd63a49
Apply Kotlin spotless.
vbabanin 89d67bb
Move update to separate variables.
vbabanin 8a18d39
Merge branch 'main' into JAVA-5854
vbabanin f296d0c
Merge branch 'main' into JAVA-5854
vbabanin a8dc4fb
Add lock for updates.
vbabanin 8ade58b
Clone document before passing it as an argument.
vbabanin 71350fa
Rename to "appendMetadata".
vbabanin 9890aa1
Add ReadWriteLock and rename method.
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add lock for updates.
- Loading branch information
commit a8dc4fbdb2445c1d5c110f9b8ef50a7e70ea4b54
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you remove the |
||
|
||
public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) { | ||
|
@@ -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) | ||
); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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()
fromupdateMetadata(...)
for clarify: 8ade58b