Skip to content

Fix: N+1 performance regression in DRAFT_CANCEL - #606

Open
akgarg06 wants to merge 8 commits into
developfrom
RBSDMS-cancel-draft-issue-fix-feature
Open

akgarg06 wants to merge 8 commits into
developfrom
RBSDMS-cancel-draft-issue-fix-feature

Conversation

@akgarg06

Copy link
Copy Markdown
Contributor

Problem

handleDraftDiscardForLinks is registered as @Before(DRAFT_CANCEL) for all application services globally. It calls revertNestedEntityLinks → processNestedEntityComposition, which fetched all draft rows across the entire tenant with no parent filter:

// Before — fetches ALL tenant drafts, no parent scope
Select.from(nestedDraftEntity.get())
    .where(e -> e.get("IsActiveEntity").eq(false))

For each row returned, revertLinksForComposition fired an additional DB query — a classic N+1 problem where N is the total number of accumulated draft rows in the tenant. With ~5,000 drafts, this caused 20–26 second delays on every DRAFT_CANCEL across the entire application.

Fix

parentKeys is already extracted from the cancel CQN in handleDraftDiscardForLinks. It is now threaded through revertNestedEntityLinks into processNestedEntityComposition, which adds a parent ID filter so only the cancelled entity's own rows are fetched:

// After — scoped to the cancelled entity only
Select.from(nestedDraftEntity.get())
    .where(e -> e.get(upIdKey).eq(parentId)
                 .and(e.get("IsActiveEntity").eq(false)))

Two defensive guards are also added to handle edge cases gracefully:

  • If upIdKey resolves to null or empty → log debug and skip the nested entity
  • If parentId is null (key name mismatch between entity and CQN) → log warn and skip, preventing a silent WHERE up__ID IS NULL full-scan fallback

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Checklist before requesting a review

  • I follow Java Development Guidelines for SAP
  • I have tested the functionality on my cloud environment.
  • I have provided sufficient automated/ unit tests for the code.
  • I have increased or maintained the test coverage.
  • I have ran integration tests on my cloud environment.
  • I have validated blackduck portal for any vulnerability after my commit.

Upload Screenshots/lists of the scenarios tested

  • I have Uploaded Screenshots or added lists of the scenarios tested in description

Changelog IDOR guard (fetchChangelog scoped lookup, lookupAttachmentScoped
helper, DBQuery scoped overload) belongs to a separate branch. This commit
restores fetchChangelog to the develop baseline and removes the associated
test mocks, leaving only the N+1 fix for processNestedEntityComposition.
Pass Map.of("ID","parent-id") instead of an empty map so the parentId
null-guard added by the N+1 fix does not short-circuit the method before
persistenceService.run() is reached.

This branch had an error being deployed

1 failed deployment
pr-analysis — bc3fc149 Deployed Nov 14, 2026 by akgarg06 via build #2117
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.

1 participant