diff --git a/CHANGES.md b/CHANGES.md index 3fbc0a0b184a..0979a4a4f099 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -160,6 +160,7 @@ * (Python) Fixed `WatchFilePattern` comparing model files only against their own path, so an older model could be chosen and a poll that saw several models could crash `RunInference` (`AsSingleton` requires one value). Workers compare file times with the pipeline construction time sent with the job, so a model uploaded after submission is not treated as a file that already existed ([#40638](https://github.com/apache/beam/issues/40638)). * (Python) Fixed local filesystem copies deleting source data when copying onto the same file or directory, and deleting an existing destination when the source is missing ([#41561](https://github.com/apache/beam/issues/41561)). * (Python) Fixed reading concatenated bzip2, zstd and lzma files when a compressed member ends exactly at an internal read boundary ([#41669](https://github.com/apache/beam/issues/41669)). +* (Java) Fixed `MongoDbIO` reads with `withBucketAuto(true)` or an `AggregationQuery` failing on large collections with `Sort exceeded memory limit of 104857600 bytes` on MongoDB servers older than 6.0. The connector now sets `allowDiskUse` on its aggregations, as the Python and Go connectors do ([#26495](https://github.com/apache/beam/issues/26495)). * (Java) Fixed Dataflow's streaming `WriteFiles` sharding override dropping options configured on the transform (such as `maxNumWritersPerBundle`, `evictWritersWhenFull`, `skipIfEmpty`, `shardingFunction`, and `badRecordErrorHandler`) when a fixed shard count is not set ([#40533](https://github.com/apache/beam/issues/40533)). * (Java) Fixed Dataflow Streaming Engine `MapState` and `MultimapState` being weighed as 1 byte in the worker state cache, which let the cache grow far past `--workerCacheMb` and run workers out of memory ([#41597](https://github.com/apache/beam/issues/41597)). * (Python) Fixed Python and Beam YAML pipelines submitted to Spark with `--spark_submit_uber_jar` failing on the driver unless `--storage_level` was set. The Spark runner now defaults to `MEMORY_ONLY` when no Java runner is available to choose the storage level from ([#41080](https://github.com/apache/beam/issues/41080)). diff --git a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/AggregationQuery.java b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/AggregationQuery.java index a96641123ac2..3d1d3a328b4c 100644 --- a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/AggregationQuery.java +++ b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/AggregationQuery.java @@ -70,6 +70,8 @@ public MongoCursor apply(MongoCollection collection) { if (bucket() != null) { pipeline.add(bucket()); } - return collection.aggregate(pipeline).iterator(); + // stages such as $sort, $group and $bucketAuto are capped at 100 MB of memory. Servers older + // than 6.0 fail past that cap unless the request allows spilling to disk + return collection.aggregate(pipeline).allowDiskUse(true).iterator(); } } diff --git a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java index 501eaa4867c4..91db87f73480 100644 --- a/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java +++ b/sdks/java/io/mongodb/src/main/java/org/apache/beam/sdk/io/mongodb/MongoDbIO.java @@ -731,7 +731,11 @@ static List buildAutoBuckets(MongoDatabase mongoDatabase, Read spec) { BsonDocument bucketAuto = new BsonDocument("$bucketAuto", bucketAutoConfig); List aggregates = new ArrayList<>(); aggregates.add(bucketAuto); - AggregateIterable buckets = mongoCollection.aggregate(aggregates); + // $bucketAuto sorts every document in the collection, and MongoDB caps an in-memory sort at + // 100 MB. Servers older than 6.0 fail past that cap with "Sort exceeded memory limit" unless + // the request allows spilling the sort to disk + AggregateIterable buckets = + mongoCollection.aggregate(aggregates).allowDiskUse(true); for (Document bucket : buckets) { Document filter = new Document(); diff --git a/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java b/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java index e0a0eabd5be3..43dc6ac8ac9b 100644 --- a/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java +++ b/sdks/java/io/mongodb/src/test/java/org/apache/beam/sdk/io/mongodb/MongoDbIOTest.java @@ -21,11 +21,15 @@ import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.assertEquals; +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.Filters; +import com.mongodb.event.CommandListener; +import com.mongodb.event.CommandStartedEvent; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -38,6 +42,7 @@ import org.apache.beam.sdk.values.KV; import org.apache.beam.sdk.values.PCollection; import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterators; +import org.bson.BsonBoolean; import org.bson.BsonDocument; import org.bson.BsonInt32; import org.bson.BsonString; @@ -207,6 +212,68 @@ public void testBuildAutoBuckets() { assertEquals(10, buckets.size()); } + @Test + public void testBuildAutoBucketsAllowsDiskUse() { + List commands = new ArrayList<>(); + try (MongoClient recordingClient = createCommandRecordingClient(commands)) { + MongoDbIO.Read spec = + MongoDbIO.read() + .withUri(MONGO_CONTAINER.getReplicaSetUrl()) + .withDatabase(DATABASE_NAME) + .withCollection(COLLECTION_NAME); + + MongoDbIO.BoundedMongoDbSource.buildAutoBuckets( + recordingClient.getDatabase(DATABASE_NAME), spec); + } + + assertEquals(BsonBoolean.TRUE, findAggregateCommand(commands).get("allowDiskUse")); + } + + @Test + public void testAggregationQueryAllowsDiskUse() { + List commands = new ArrayList<>(); + try (MongoClient recordingClient = createCommandRecordingClient(commands)) { + MongoCollection collection = + recordingClient.getDatabase(DATABASE_NAME).getCollection(COLLECTION_NAME); + + AggregationQuery.create() + .withMongoDbPipeline(Collections.emptyList()) + .apply(collection) + .close(); + } + + assertEquals(BsonBoolean.TRUE, findAggregateCommand(commands).get("allowDiskUse")); + } + + /** Creates a client that adds every command it sends to the server to {@code commands}. */ + private static MongoClient createCommandRecordingClient(List commands) { + CommandListener listener = + new CommandListener() { + @Override + public void commandStarted(CommandStartedEvent event) { + // the driver frees the buffer behind getCommand() once this callback returns, so + // reading the original document later throws IndexOutOfBoundsException + synchronized (commands) { + commands.add(event.getCommand().clone()); + } + } + }; + return MongoClients.create( + MongoClientSettings.builder() + .applyConnectionString(new ConnectionString(MONGO_CONTAINER.getReplicaSetUrl())) + .addCommandListener(listener) + .build()); + } + + private static BsonDocument findAggregateCommand(List commands) { + synchronized (commands) { + return commands.stream() + .filter(command -> command.containsKey("aggregate")) + .findFirst() + .orElseThrow(() -> new AssertionError("no aggregate command was sent")); + } + } + @Test public void testFullRead() { PCollection output =