-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add server selection log messages #1221
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
14b3b1a
Add unified tests
stIncMale cbf661a
Add `ServerSelectionLoggingTest`, implement schema-1.14.json
stIncMale 51c1220
Implement server selection logging
stIncMale f2dc65e
Do fixes
stIncMale b1292b9
Do fixes
stIncMale f4a4013
Do fixes
stIncMale ed34386
Update locking to match the decision made in https://jira.mongodb.org…
stIncMale e09930f
Address review concerns
stIncMale 7998d1a
Partially restore the `MongoTimeoutException` message
stIncMale ef5cd3a
Fix `BaseClusterSpecification`
stIncMale File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
216 changes: 158 additions & 58 deletions
216
driver-core/src/main/com/mongodb/internal/connection/BaseCluster.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import com.mongodb.MongoException; | ||
import com.mongodb.ReadPreference; | ||
import com.mongodb.ServerApi; | ||
import com.mongodb.connection.ClusterDescription; | ||
import com.mongodb.connection.ServerDescription; | ||
import com.mongodb.internal.IgnorableRequestContext; | ||
import com.mongodb.internal.binding.StaticBindingContext; | ||
|
@@ -29,6 +30,7 @@ | |
import com.mongodb.internal.selector.ReadPreferenceServerSelector; | ||
import com.mongodb.internal.validator.NoOpFieldNameValidator; | ||
import com.mongodb.lang.Nullable; | ||
import com.mongodb.selector.ServerSelector; | ||
import com.mongodb.session.ServerSession; | ||
import org.bson.BsonArray; | ||
import org.bson.BsonBinary; | ||
|
@@ -114,24 +116,40 @@ private void endClosedSessions() { | |
return; | ||
} | ||
|
||
List<ServerDescription> primaryPreferred = new ReadPreferenceServerSelector(ReadPreference.primaryPreferred()) | ||
ReadPreference primaryPreferred = ReadPreference.primaryPreferred(); | ||
List<ServerDescription> primaryPreferredServers = new ReadPreferenceServerSelector(primaryPreferred) | ||
.select(cluster.getCurrentDescription()); | ||
if (primaryPreferred.isEmpty()) { | ||
if (primaryPreferredServers.isEmpty()) { | ||
// Skip doing server selection if we anticipate that no server is readily selectable. | ||
// This approach is racy, and it is still possible to become blocked selecting a server | ||
// even if `primaryPreferredServers` is not empty. | ||
return; | ||
} | ||
|
||
Connection connection = null; | ||
try { | ||
StaticBindingContext context = new StaticBindingContext(NoOpSessionContext.INSTANCE, serverApi, | ||
IgnorableRequestContext.INSTANCE, new OperationContext()); | ||
connection = cluster.selectServer(clusterDescription -> { | ||
for (ServerDescription cur : clusterDescription.getServerDescriptions()) { | ||
if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) { | ||
return Collections.singletonList(cur); | ||
} | ||
} | ||
return Collections.emptyList(); | ||
}, context.getOperationContext()).getServer().getConnection(context.getOperationContext()); | ||
connection = cluster.selectServer( | ||
new ServerSelector() { | ||
@Override | ||
public List<ServerDescription> select(final ClusterDescription clusterDescription) { | ||
for (ServerDescription cur : clusterDescription.getServerDescriptions()) { | ||
if (cur.getAddress().equals(primaryPreferredServers.get(0).getAddress())) { | ||
return Collections.singletonList(cur); | ||
} | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ReadPreferenceServerSelector{" | ||
+ "readPreference=" + primaryPreferred | ||
+ '}'; | ||
} | ||
}, | ||
context.getOperationContext()).getServer().getConnection(context.getOperationContext()); | ||
Comment on lines
+119
to
+152
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. This change is just to implement |
||
|
||
connection.command("admin", | ||
new BsonDocument("endSessions", new BsonArray(identifiers)), new NoOpFieldNameValidator(), | ||
|
107 changes: 107 additions & 0 deletions
107
...r-core/src/test/resources/unified-test-format/server-selection/logging/load-balanced.json
This file contains 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{ | ||
"description": "server-selection-logging", | ||
"schemaVersion": "1.13", | ||
"runOnRequirements": [ | ||
{ | ||
"topologies": [ | ||
"load-balanced" | ||
] | ||
} | ||
], | ||
"createEntities": [ | ||
{ | ||
"client": { | ||
"id": "client", | ||
"uriOptions": { | ||
"heartbeatFrequencyMS": 500 | ||
}, | ||
"observeLogMessages": { | ||
"serverSelection": "debug" | ||
}, | ||
"observeEvents": [ | ||
"serverDescriptionChangedEvent" | ||
] | ||
} | ||
}, | ||
{ | ||
"database": { | ||
"id": "database", | ||
"client": "client", | ||
"databaseName": "logging-tests" | ||
} | ||
}, | ||
{ | ||
"collection": { | ||
"id": "collection", | ||
"database": "database", | ||
"collectionName": "server-selection" | ||
} | ||
} | ||
], | ||
"tests": [ | ||
{ | ||
"description": "A successful operation - load balanced cluster", | ||
"operations": [ | ||
{ | ||
"name": "waitForEvent", | ||
"object": "testRunner", | ||
"arguments": { | ||
"client": "client", | ||
"event": { | ||
"serverDescriptionChangedEvent": { | ||
"newDescription": { | ||
"type": "LoadBalancer" | ||
} | ||
} | ||
}, | ||
"count": 1 | ||
} | ||
}, | ||
{ | ||
"name": "insertOne", | ||
"object": "collection", | ||
"arguments": { | ||
"document": { | ||
"x": 1 | ||
} | ||
} | ||
} | ||
], | ||
"expectLogMessages": [ | ||
{ | ||
"client": "client", | ||
"messages": [ | ||
{ | ||
"level": "debug", | ||
"component": "serverSelection", | ||
"data": { | ||
"message": "Server selection started", | ||
"selector": { | ||
"$$exists": true | ||
}, | ||
"operation": "insert", | ||
"topologyDescription": { | ||
"$$exists": true | ||
} | ||
} | ||
}, | ||
{ | ||
"level": "debug", | ||
"component": "serverSelection", | ||
"data": { | ||
"message": "Server selection succeeded", | ||
"selector": { | ||
"$$exists": true | ||
}, | ||
"operation": "insert", | ||
"topologyDescription": { | ||
"$$exists": true | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
Oops, something went wrong.
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.
The final decision on whether to support
operation
and to what extent will be done in https://jira.mongodb.org/browse/JAVA-5197.