-
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
base: main
Are you sure you want to change the base?
Changes from 18 commits
9753f28
889d5c8
4b3065c
2a684bf
28c1844
371ce0b
972fe9d
bcf9cc8
179b262
bc43ba0
3f5fc91
61192d8
e9f8dd6
95c1bb1
dd63a49
89d67bb
8a18d39
f296d0c
a8dc4fb
8ade58b
71350fa
9890aa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||
/* | ||||||
* Copyright 2008-present MongoDB, Inc. | ||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* | ||||||
* http://www.apache.org/licenses/LICENSE-2.0 | ||||||
* | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
*/ | ||||||
|
||||||
package com.mongodb.internal.connection; | ||||||
|
||||||
import com.mongodb.MongoDriverInformation; | ||||||
import com.mongodb.lang.Nullable; | ||||||
import org.bson.BsonDocument; | ||||||
|
||||||
import static com.mongodb.internal.connection.ClientMetadataHelper.createClientMetadataDocument; | ||||||
import static com.mongodb.internal.connection.ClientMetadataHelper.updateClientMedataDocument; | ||||||
|
||||||
/** | ||||||
* 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 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. I don't thing that a volatile fields is enough. If 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. 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 |
||||||
|
||||||
public ClientMetadata(@Nullable final String applicationName, final MongoDriverInformation mongoDriverInformation) { | ||||||
this.clientMetadataBsonDocument = createClientMetadataDocument(applicationName, mongoDriverInformation); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Returns mutable BsonDocument that represents the client metadata. | ||||||
*/ | ||||||
public BsonDocument getBsonDocument() { | ||||||
return 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. Return a defensive copy (instead of a reference to this mutable instance?)
Suggested change
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. We mostly pass mutable 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. I'm fine without defensive copy if it's consistent throughout the codebase. |
||||||
} | ||||||
|
||||||
public void append(final MongoDriverInformation mongoDriverInformation) { | ||||||
this.clientMetadataBsonDocument = updateClientMedataDocument(clientMetadataBsonDocument, mongoDriverInformation); | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,6 +32,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.function.Consumer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -180,6 +181,38 @@ static boolean clientMetadataDocumentTooLarge(final BsonDocument document) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public static BsonDocument updateClientMedataDocument(final BsonDocument clientMetadataDocument, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. typo
Suggested change
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. Good catch! Renamed, thanks! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
final MongoDriverInformation mongoDriverInformation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BsonDocument updatedClientMetadataDocument = clientMetadataDocument.clone(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BsonDocument driverInformation = clientMetadataDocument.getDocument("driver"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. what if the user doesn't supply the 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. Do you mean during initial metadata creation or subsequent updates? For updates: mongo-java-driver/driver-core/src/main/com/mongodb/MongoDriverInformation.java Lines 158 to 172 in d65def4
For initial metadata creation: mongo-java-driver/driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java Lines 296 to 305 in 8ade58b
This is also tested via prose tests: Lines 75 to 82 in 71350fa
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
List<String> driverNamesToAppend = mongoDriverInformation.getDriverNames(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
List<String> driverVersionsToAppend = mongoDriverInformation.getDriverVersions(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
List<String> driverPlatformsToAppend = mongoDriverInformation.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.addAll(driverNamesToAppend); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
updatedDriverVersions.add(driverInformation.getString("version").getValue()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
updatedDriverVersions.addAll(driverVersionsToAppend); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
updateDriverPlatforms.add(clientMetadataDocument.getString("platform").getValue()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
updateDriverPlatforms.addAll(driverPlatformsToAppend); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tryWithLimit(updatedClientMetadataDocument, d -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
putAtPath(d, "driver.name", listToString(updatedDriverNames)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
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. That's a fair point. In this case, I’d choose to follow the existing pattern in 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. +1 for consistency |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
putAtPath(d, "driver.version", listToString(updatedDriverVersions)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Should we try in a dedicated 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. By the spec, driver name and version must be updated atomically. If both can't fit, then both must be omitted - so splitting them in a mongo-java-driver/driver-core/src/main/com/mongodb/internal/connection/ClientMetadataHelper.java Lines 100 to 103 in 8ade58b
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tryWithLimit(updatedClientMetadataDocument, d -> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
putAtPath(d, "platform", listToString(updateDriverPlatforms)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return updatedClientMetadataDocument; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public enum ContainerRuntime { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DOCKER("docker") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
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.