Skip to content

GH-5385: Improve performance of MongoStepExecutionDao::getLastStepExecution#5405

Open
seonwooj0810 wants to merge 1 commit into
spring-projects:mainfrom
seonwooj0810:fix/issue-5385-mongo-last-step-execution
Open

GH-5385: Improve performance of MongoStepExecutionDao::getLastStepExecution#5405
seonwooj0810 wants to merge 1 commit into
spring-projects:mainfrom
seonwooj0810:fix/issue-5385-mongo-last-step-execution

Conversation

@seonwooj0810
Copy link
Copy Markdown

Fixes #5385.

Summary

MongoStepExecutionDao#getLastStepExecution currently fetches all step executions for the job instance into memory, then filters by step name and sorts on (createTime, stepExecutionId) in Java. The single-row contract makes this an O(N) cost that grows with the step-execution history despite the O(1) result.

This PR pushes the step name filter, ordering (createTime DESC, then stepExecutionId DESC) and limit(1) down to MongoDB, mirroring the optimization PR #4798 applied to the JDBC variant. It also removes the existing // TODO optimize the query marker.

The contract StepExecutionDao#getLastStepExecution ("Retrieve the last StepExecution for a given JobInstance ordered by creation time and then id") is preserved.

Changes

  • MongoStepExecutionDao#getLastStepExecution: filter (name), sort (createTime DESC, stepExecutionId DESC), and limit(1) are now applied via the Query and resolved by MongoOperations#findOne (which respects Query#getSortObject by delegating to find with limit(1)).
  • MongoStepExecutionDaoIntegrationTests: added testGetLastExecutionFiltersByStepName to assert correct filtering when multiple step names share the same job instance, and to assert that a non-existent step name returns null. Existing tests (testSaveAndGetLastExecution, testSaveAndGetLastExecutionWhenSameStartTime) remain unchanged and continue to cover the createTime-then-id ordering contract.

Test plan

  • ./mvnw -pl spring-batch-core compile -DskipTests — passes
  • ./mvnw -pl spring-batch-core test-compile -DskipTests — passes
  • ./mvnw -pl spring-batch-core spring-javaformat:validate — passes
  • Mongo integration tests are gated on @Testcontainers(disabledWithoutDocker = true) and could not be executed in my local environment (no Docker daemon access). They are exercised by upstream CI.

Verification done

  • (1) PR list searched on 5385: none found at the time of writing.
  • (2) Issue thread: no in-flight claim; the reporter mentioned willingness to submit a PR on 2026-04-22 but no PR has materialized in the 36 days since. Treating as stalled per "no follow-up in >21 days." Credit to @config25 for the detailed problem description, current-behavior trace, and direction in the issue body.
  • (3) Code-focused change: .java only.
  • (4) Verified the pattern still exists on main (// TODO optimize the query + in-memory filter/sort at MongoStepExecutionDao.java:115-147).
  • (5) Prior art: PR Improve performance of JdbcStepExecutionDao::getLastStepExecution #4798 (Improve performance of JdbcStepExecutionDao::getLastStepExecution) — same shape of fix on the JDBC side, mirrored here for Mongo.

…:getLastStepExecution

Push the step name filter, ordering (createTime DESC, then stepExecutionId
DESC), and limit(1) down to MongoDB instead of fetching all step executions
for the job instance and filtering/sorting in Java.

This mirrors the optimization applied to the JDBC variant in spring-projects#4798 and
addresses the existing `// TODO optimize the query` marker.

Signed-off-by: Seonwoo Jung <laborlawseon@kap.kr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize MongoStepExecutionDao#getLastStepExecution to filter and sort at the database level

1 participant