fix(core): Retry role mapping order races#29494
Conversation
There was a problem hiding this comment.
No issues found across 2 files
Architecture diagram
sequenceDiagram
participant API as API/Provisioning Logic
participant Service as RoleMappingRuleService
participant Repo as RoleMappingRuleRepository
participant DB as Database (Postgres/SQLite)
Note over API,DB: Role Mapping Rule Creation Flow
API->>Service: create(dto)
Service->>Repo: find(existing rules for type)
Repo-->>Service: rules[]
Service->>Service: Calculate tempOrder (max + 1)
rect rgb(240, 240, 240)
Note over Service,DB: NEW: Retry Logic for Concurrent Inserts
loop Until Success or Max Retries
Service->>Repo: save(new rule with tempOrder)
alt Success Path
Repo->>DB: INSERT rule
DB-->>Repo: OK
Repo-->>Service: saved entity
else CHANGED: Conflict (Race Condition)
DB-->>Repo: Error: Unique Constraint (type, order)
Repo-->>Service: throws ConflictError
Service->>Repo: NEW: re-read current rules
Repo-->>Service: latest rules[]
Service->>Service: NEW: Recalculate tempOrder
end
end
end
Note over Service,DB: Two-Phase Ordering (applyOrder)
Service->>DB: BEGIN TRANSACTION
rect rgb(230, 245, 255)
Note over Service,DB: Phase 1: Vacate slots (Move to high offset)
loop for each ruleId in reorderedIds
Service->>DB: UPDATE order = offset + i
end
end
rect rgb(230, 255, 230)
Note over Service,DB: Phase 2: Normalize (Sequence from 0)
loop for each ruleId in reorderedIds
Service->>DB: UPDATE order = i
end
end
Service->>DB: COMMIT
Service-->>API: RoleMappingRuleResponse
|
Hey @haimingZZ, Thank you for your contribution. We appreciate the time and effort you’ve taken to submit this pull request. Before we can proceed, please ensure the following: Regarding new nodes: If your node integrates with an AI service that you own or represent, please email nodes@n8n.io and we will be happy to discuss the best approach. About review timelines: Thank you again for contributing to n8n. |
f1b6921 to
fcec9cd
Compare
|
The cla-bot has been summoned, and re-checked this pull request! |
Summary
(type, order)slotReview notes
This builds on #28722. That change avoids ordinary requested-order collisions and serializes the UI, but two backend/API creates can still read the same snapshot and choose the same temporary order for the initial insert. This keeps the existing two-phase
applyOrderflow and only retries unique insert conflicts after re-reading.Validation
git diff --checkpnpm --filter n8n test:unit -- role-mapping-rule.service.ee.test.ts(not run: this worktree has nonode_modules, sojestis not installed)