Skip to content

AggregationOptions.maxTime ignored for MongoTemplate.aggregateStream() #4644

Closed
@fkreis

Description

@fkreis

Dear spring-data-mongodb team,
I am using Spring Boot v3.2.2, hence spring-data-mongodb v4.2.2.

If I execute a long-running aggregation (>5s) with the following code

AggregationOptions aggregationOptions = AggregationOptions.builder()
        .maxTime(Duration.ofSeconds(5))
        .build();
Aggregation aggregation = Aggregation.newAggregation(ops).withOptions(aggregationOptions);
log.debug("before aggregation");
AggregationResults<ResultList> aggregate = mongoTemplate.aggregate(aggregation, ResultList.class, ResultList.class);
log.debug("after aggregation");

I see the following logs, as expected:

09:52:54.176 DEBUG before aggregation
09:52:59.245 ERROR [...]MongoExecutionTimeoutException[...]

Please note that I get a MongoExecutionTimeoutException exactly 5s after I start the aggregation execution which is exactly as configured in the AggregationOptions via the maxTime option. Please also note that I executed the aggregation using the mongoTemplate.aggregate() method.

However, if I execute the same long-running aggregation using mongoTemplate.aggregateStream() instead:

AggregationOptions aggregationOptions = AggregationOptions.builder()
        .maxTime(Duration.ofSeconds(5))
        .build();
Aggregation aggregation = Aggregation.newAggregation(ops).withOptions(aggregationOptions);
log.debug("before aggregation");
Stream<ResultList> aggregate = mongoTemplate.aggregateStream(aggregation, ResultList.class, ResultList.class);
log.debug("after aggregation");

I see the following logs:

10:02:36.662 DEBUG before aggregation
10:03:00.870 DEBUG after aggregation

Please note that no exception is thrown and I reach the debug log "after aggregation" even though the aggregation took around 24s which is a lot more than the configured 5s. My expectation would be that instead an exception should be thrown after 5s as for the aggregate() method.

Further hints

I did some further analysis and debugging and found out that in org.springframework.data.mongodb.core.MongoTemplate l2277 the AggregateIterable cursor's internal field maxTimeMS is still set to 0 even though the option's field maxTime field es properly set to "PT5s" and also delegate.sources[0].maxTime is "PT5s". I believe that either mongoTemplate itself or the delegate should copy all the aggregate options properly to the cursor.
As an experiment I manually called

cursor.maxTime(5000, TimeUnit.MILLISECONDS);

in l2277 using the debugger and after 5s I got the MongoExecutionTimeoutException as expected!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions