Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public MongoCursor<Document> apply(MongoCollection<Document> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,11 @@ static List<Document> buildAutoBuckets(MongoDatabase mongoDatabase, Read spec) {
BsonDocument bucketAuto = new BsonDocument("$bucketAuto", bucketAutoConfig);
List<BsonDocument> aggregates = new ArrayList<>();
aggregates.add(bucketAuto);
AggregateIterable<Document> 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<Document> buckets =
mongoCollection.aggregate(aggregates).allowDiskUse(true);

for (Document bucket : buckets) {
Document filter = new Document();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -207,6 +212,68 @@ public void testBuildAutoBuckets() {
assertEquals(10, buckets.size());
}

@Test
public void testBuildAutoBucketsAllowsDiskUse() {
List<BsonDocument> 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<BsonDocument> commands = new ArrayList<>();
try (MongoClient recordingClient = createCommandRecordingClient(commands)) {
MongoCollection<Document> 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<BsonDocument> 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<BsonDocument> 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<Document> output =
Expand Down
Loading