Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fkreis opened this issue Feb 19, 2024 · 2 comments
Closed

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

fkreis opened this issue Feb 19, 2024 · 2 comments
Assignees
Labels
type: bug A general bug

Comments

@fkreis
Copy link

fkreis commented Feb 19, 2024

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!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 19, 2024
@mp911de mp911de self-assigned this Feb 19, 2024
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 19, 2024
@mp911de mp911de changed the title AggregationOption 'maxTime' is ignored for MongoTemplate.aggregateStream() (but works for mongoTemplate.aggregate()) AggregationOptions.maxTime ignored for MongoTemplate.aggregateStream() Feb 19, 2024
@mp911de
Copy link
Member

mp911de commented Feb 19, 2024

Thanks for the report. We need to fix this bug.

@fkreis
Copy link
Author

fkreis commented Feb 19, 2024

That was fast, thank you! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants