Skip to content

Commit 84a80a6

Browse files
committed
Remove assumption that mapReduce always returns statistics
Starting with MongoDB 4.4, MongoDB will no longer produce execution statistics for the mapReduce command. The driver no longer assumes that they will be in the response, and if they are missing will just produce zero values for all statistics. JAVA-3522
1 parent 4d7a0d5 commit 84a80a6

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

driver-core/src/main/com/mongodb/operation/MapReduceHelper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@
1717
package com.mongodb.operation;
1818

1919
import org.bson.BsonDocument;
20+
import org.bson.BsonInt32;
2021

2122
final class MapReduceHelper {
2223

2324
static MapReduceStatistics createStatistics(final BsonDocument result) {
2425
return new MapReduceStatistics(getInputCount(result), getOutputCount(result), getEmitCount(result),
25-
getDuration(result));
26+
getDuration(result));
2627
}
2728

2829
private static int getInputCount(final BsonDocument result) {
29-
return result.getDocument("counts").getNumber("input").intValue();
30+
return result.getDocument("counts", new BsonDocument()).getNumber("input", new BsonInt32(0)).intValue();
3031
}
3132

3233
private static int getOutputCount(final BsonDocument result) {
33-
return result.getDocument("counts").getNumber("output").intValue();
34+
return result.getDocument("counts", new BsonDocument()).getNumber("output", new BsonInt32(0)).intValue();
3435
}
3536

3637
private static int getEmitCount(final BsonDocument result) {
37-
return result.getDocument("counts").getNumber("emit").intValue();
38+
return result.getDocument("counts", new BsonDocument()).getNumber("emit", new BsonInt32(0)).intValue();
3839
}
3940

4041
private static int getDuration(final BsonDocument result) {
41-
return result.getNumber("timeMillis").intValue();
42+
return result.getNumber("timeMillis", new BsonInt32(0)).intValue();
4243
}
4344

4445
private MapReduceHelper() {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import spock.lang.IgnoreIf
4040
import static com.mongodb.ClusterFixture.getBinding
4141
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet
4242
import static com.mongodb.ClusterFixture.serverVersionAtLeast
43+
import static com.mongodb.ClusterFixture.serverVersionGreaterThan
44+
import static com.mongodb.ClusterFixture.serverVersionLessThan
4345
import static com.mongodb.client.model.Filters.gte
4446
import static java.util.concurrent.TimeUnit.MILLISECONDS
4547

@@ -139,6 +141,7 @@ class MapReduceToCollectionOperationSpecification extends OperationFunctionalSpe
139141
operation.getCollation() == defaultCollation
140142
}
141143

144+
@IgnoreIf({ serverVersionGreaterThan('4.2') })
142145
def 'should return the correct statistics and save the results'() {
143146
when:
144147
MapReduceStatistics results = execute(mapReduceOperation, async)
@@ -154,6 +157,22 @@ class MapReduceToCollectionOperationSpecification extends OperationFunctionalSpe
154157
async << [true, false]
155158
}
156159

160+
@IgnoreIf({ serverVersionLessThan('4.3') })
161+
def 'should return zero-valued statistics and save the results'() {
162+
when:
163+
MapReduceStatistics results = execute(mapReduceOperation, async)
164+
165+
then:
166+
results.emitCount == 0
167+
results.inputCount == 0
168+
results.outputCount == 0
169+
helper.count() == 2
170+
helper.find() as Set == expectedResults
171+
172+
where:
173+
async << [true, false]
174+
}
175+
157176

158177
@IgnoreIf({ !serverVersionAtLeast(3, 2) })
159178
def 'should support bypassDocumentValidation'() {

driver-legacy/src/test/functional/com/mongodb/MapReduceTest.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet;
3131
import static com.mongodb.ClusterFixture.isSharded;
3232
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
33+
import static com.mongodb.ClusterFixture.serverVersionLessThan;
3334
import static com.mongodb.DBObjectMatchers.hasFields;
3435
import static com.mongodb.DBObjectMatchers.hasSubdocument;
3536
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -309,11 +310,15 @@ public void shouldReturnStatisticsForInlineMapReduce() {
309310
MapReduceOutput output = collection.mapReduce(command);
310311

311312
//then
312-
//duration is not working on the unstable server version
313-
// assertThat(output.getDuration(), is(greaterThan(0)));
314-
assertThat(output.getEmitCount(), is(6));
315-
assertThat(output.getInputCount(), is(3));
316-
assertThat(output.getOutputCount(), is(4));
313+
if (serverVersionLessThan(4, 3)) {
314+
assertThat(output.getEmitCount(), is(6));
315+
assertThat(output.getInputCount(), is(3));
316+
assertThat(output.getOutputCount(), is(4));
317+
} else {
318+
assertThat(output.getEmitCount(), is(0));
319+
assertThat(output.getInputCount(), is(0));
320+
assertThat(output.getOutputCount(), is(0));
321+
}
317322
}
318323

319324
@Test
@@ -329,10 +334,17 @@ public void shouldReturnStatisticsForMapReduceIntoACollection() {
329334
MapReduceOutput output = collection.mapReduce(command);
330335

331336
//then
332-
assertThat(output.getDuration(), is(greaterThanOrEqualTo(0)));
333-
assertThat(output.getEmitCount(), is(6));
334-
assertThat(output.getInputCount(), is(3));
335-
assertThat(output.getOutputCount(), is(4));
337+
if (serverVersionLessThan(4, 3)) {
338+
assertThat(output.getDuration(), is(greaterThanOrEqualTo(0)));
339+
assertThat(output.getEmitCount(), is(6));
340+
assertThat(output.getInputCount(), is(3));
341+
assertThat(output.getOutputCount(), is(4));
342+
} else {
343+
assertThat(output.getDuration(), is(0));
344+
assertThat(output.getEmitCount(), is(0));
345+
assertThat(output.getInputCount(), is(0));
346+
assertThat(output.getOutputCount(), is(0));
347+
}
336348
}
337349

338350

0 commit comments

Comments
 (0)