Skip to content

Commit 90c7062

Browse files
authored
Handle new explain output in tests (#1191)
The response document for the explain command has changed in 7.2 sharded clusters. It introduces a top-level "shards" document that contains the explain output for each shard. This patch supports this new field in tests of explain API in the driver. JAVA-5137
1 parent d3d3c16 commit 90c7062

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

driver-core/src/test/functional/com/mongodb/internal/operation/AggregateOperationSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class AggregateOperationSpecification extends OperationFunctionalSpecification {
320320
def result = execute(operation, async)
321321

322322
then:
323-
result.containsKey('stages') || result.containsKey('queryPlanner')
323+
result.containsKey('stages') || result.containsKey('queryPlanner') || result.containsKey('shards')
324324

325325
where:
326326
async << [true, false]

driver-sync/src/test/functional/com/mongodb/client/AbstractExplainTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,50 @@ public void testExplainOfAggregateWithNewResponseStructure() {
9999
AggregateIterable<BsonDocument> iterable = collection
100100
.aggregate(singletonList(Aggregates.match(Filters.eq("_id", 1))));
101101

102-
Document explainDocument = iterable.explain();
103-
assertNotNull(explainDocument);
102+
Document explainDocument = getAggregateExplainDocument(iterable.explain());
104103
assertTrue(explainDocument.containsKey("queryPlanner"));
105104
assertTrue(explainDocument.containsKey("executionStats"));
106105

107-
explainDocument = iterable.explain(ExplainVerbosity.QUERY_PLANNER);
106+
explainDocument = getAggregateExplainDocument(iterable.explain(ExplainVerbosity.QUERY_PLANNER));
108107
assertNotNull(explainDocument);
109108
assertTrue(explainDocument.containsKey("queryPlanner"));
110109
assertFalse(explainDocument.containsKey("executionStats"));
111110

112-
BsonDocument explainBsonDocument = iterable.explain(BsonDocument.class);
111+
BsonDocument explainBsonDocument = getAggregateExplainDocument(iterable.explain(BsonDocument.class));
113112
assertNotNull(explainBsonDocument);
114113
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
115114
assertTrue(explainBsonDocument.containsKey("executionStats"));
116115

117-
explainBsonDocument = iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER);
116+
explainBsonDocument = getAggregateExplainDocument(iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER));
118117
assertNotNull(explainBsonDocument);
119118
assertTrue(explainBsonDocument.containsKey("queryPlanner"));
120119
assertFalse(explainBsonDocument.containsKey("executionStats"));
121120
}
122121

122+
// Post-MongoDB 7.0, sharded cluster responses move the explain plan document into a "shards" document, which a plan for each shard.
123+
// This method grabs the explain plan document from the first shard when this new structure is present.
124+
private static Document getAggregateExplainDocument(final Document rootAggregateExplainDocument) {
125+
assertNotNull(rootAggregateExplainDocument);
126+
Document aggregateExplainDocument = rootAggregateExplainDocument;
127+
if (rootAggregateExplainDocument.containsKey("shards")) {
128+
Document shardDocument = rootAggregateExplainDocument.get("shards", Document.class);
129+
String firstKey = shardDocument.keySet().iterator().next();
130+
aggregateExplainDocument = shardDocument.get(firstKey, Document.class);
131+
}
132+
return aggregateExplainDocument;
133+
}
134+
135+
private static BsonDocument getAggregateExplainDocument(final BsonDocument rootAggregateExplainDocument) {
136+
assertNotNull(rootAggregateExplainDocument);
137+
BsonDocument aggregateExplainDocument = rootAggregateExplainDocument;
138+
if (rootAggregateExplainDocument.containsKey("shards")) {
139+
BsonDocument shardDocument = rootAggregateExplainDocument.getDocument("shards");
140+
String firstKey = shardDocument.getFirstKey();
141+
aggregateExplainDocument = shardDocument.getDocument(firstKey);
142+
}
143+
return aggregateExplainDocument;
144+
}
145+
123146
@Test
124147
public void testExplainOfAggregateWithOldResponseStructure() {
125148
// Aggregate explain is supported on earlier versions, but the structure of the response on which we're asserting in this test

0 commit comments

Comments
 (0)