Repository navigation
perf(issues): stop holding the team row for the whole create - #240
Conversation
allocateIssueNumber took a row lock on the team inside the create transaction and held it until commit, so labels, activity, notifications and the sync id all ran with the team row locked. Two people creating issues on the same team queued behind each other for the full write, which showed up as the slowest statement in production: 103ms mean over 471 calls, 48.6s total. Hoist the team lookup and the number allocation out of the transaction. The allocation commits on its own, so a refused write now leaves a gap in the numbering rather than reusing a number, which identifiers already tolerate. 12 concurrent creates: 616ms before, 214ms after.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughIssue creation now loads the team and allocates the issue number before starting its transaction. New tests cover concurrent uniqueness, increasing issue numbers, and number consumption when creation fails. ChangesIssue Number Allocation
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/work/issue-service.ts`:
- Around line 718-720: Update createIssue so the target team is revalidated
inside the db.transaction immediately before the issue insert, and use that
transaction-scoped team for the issue’s team id/key and issueIdentifier
generation. Avoid relying on the pre-transaction team object or key after
allocateIssueNumber.
In `@packages/core/tests/work/issue-service.test.ts`:
- Around line 1243-1259: Update the test case “does not hold the team row while
the rest of the write runs” to create a deterministic pause after the first
issue’s number allocation, start the second create while that transaction
remains paused, then release the barrier and assert the second allocation
completes successfully. Ensure the test would fail if the allocation still held
the team row for the remainder of the transaction.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 189ade56-8d7f-4a87-aae9-1c6967597f57
📒 Files selected for processing (2)
packages/core/src/work/issue-service.tspackages/core/tests/work/issue-service.test.ts
Moving the allocation out of the transaction also moved the team check out, so a membership change, an archive or a key rename between the check and the insert could produce an issue with stale authorization or the wrong identifier. Re-require the team inside the transaction and build the identifier from that row. requireTeam is a plain select with no row lock, so this costs one round trip and reintroduces none of the contention the change was about. Also replaces a test that did not discriminate. It created issues in sequence and asserted the numbers rose, which the old transaction scoped implementation satisfied too. It now asserts the counter advances even when the write that follows is refused, which only holds when the allocation commits on its own, plus a case covering the team check for a principal who may not write to that team.
What
allocateIssueNumbertook a row lock on the team inside the create transaction and held it until commit. Everything after it, labels, activity, notifications, the sync id, ran with the team row still locked, so two people creating issues on the same team queued behind each other for the whole write rather than just the increment.In production this was the single worst statement by total time:
update team set issue_counter = issue_counter + $3Nothing else came close.
The change
Hoist
requireTeamandallocateIssueNumberout of the transaction. The allocation now commits on its own, before the rest of the write starts.The tradeoff, stated plainly
A refused write now leaves a gap in the numbering rather than reusing the number. Identifiers already tolerate gaps, and this is the same tradeoff a Postgres sequence makes. The alternative, keeping the allocation inside the transaction but moving it last, keeps numbering dense but reintroduces the lock on the retry path.
This reasoning is specific to issue numbers. It does not transfer to
sync_id, where a gap is a delivery bug rather than a cosmetic one.Measured
12 concurrent creates, 3 rounds, against local Postgres:
2.9x faster.
Tests
Four new tests in
packages/core/tests/work/issue-service.test.ts. Honest note on what they prove: the three concurrency tests pass against the old code too, because the row lock also guaranteed distinct numbers. Onlyleaves a gap rather than reusing a number when the write is refuseddiscriminates between the two implementations, verified by mutation testing.Full core suite: 729 pass, 0 fail.
Greptile Summary
This PR shortens contention during issue creation by allocating and committing the team’s next issue number before starting the main creation transaction.
Confidence Score: 5/5
The PR appears safe to merge.
No blocking failure remains.
Important Files Changed
Sequence Diagram
sequenceDiagram participant C as Caller participant DB as Database participant TX as Create transaction C->>DB: requireTeam(teamId) C->>DB: increment issueCounter DB-->>C: allocated number (committed) C->>TX: begin TX->>DB: requireTeam(teamId) TX->>DB: validate and insert issue alt creation succeeds TX->>DB: commit issue and related writes TX-->>C: created issue else creation is refused TX->>DB: rollback issue writes Note over DB: Allocated number remains consumed TX-->>C: error endReviews (2): Last reviewed commit: "fix(issues): revalidate the team inside ..." | Re-trigger Greptile