Skip to content

fix(zero): never lower startTxnTs when purging the conflict map - #9903

Open
myk-dol wants to merge 1 commit into
dgraph-io:mainfrom
myk-dol:fix/zero-purge-starttxnts
Open

myk-dol wants to merge 1 commit into
dgraph-io:mainfrom
myk-dol:fix/zero-purge-starttxnts

Conversation

@myk-dol

@myk-dol myk-dol commented Nov 28, 2026 •

Copy link
Copy Markdown

Description

When a Zero becomes leader (after an election, or when a single Zero restarts), updateLeases calls updateStartTxnTs(MaxTxnTs+1), which also resets keyCommit. The new leader has lost the previous term's conflict map, so hasConflict relies on startTxnTs to abort every transaction that started before the change.

purgeBelow runs when Zero applies one of its own snapshot proposals (applySnapshot), and it set startTxnTs = minTs unconditionally. minTs is the lowest checkpoint ts reported by the Alpha groups. A snapshot proposal can carry a checkpoint below the floor set at the leader change, for example when a group is behind. In that case startTxnTs drops below the start ts of a transaction that is still open from before the change. That transaction then commits with no conflict check against writes committed in the previous term: a lost update.

The unconditional assignment came in with #7096. This PR changes it to max(startTxnTs, minTs), so the floor holds until a checkpoint exceeds it. Trimming of commits and keyCommit is unchanged.

Testing

TestPurgeBelowDoesNotLowerStartTxnTs (dgraph/cmd/zero/oracle_test.go) drives the Oracle state transition directly: a commit, then updateStartTxnTs as on a leader change, then purgeBelow with a lower checkpoint. It then checks that commit rejects the older transaction. It does not run a cluster failover. It checks only that the commit is rejected, not which error is returned: if #9747 lands first, the same commit is reported as a stale start ts.

go test -count=1 -run TestPurgeBelowDoesNotLowerStartTxnTs -v ./dgraph/cmd/zero/

It fails without the fix (the commit succeeds) and passes with it, both on its own and merged with #9747. Also checked: go test ./dgraph/cmd/zero/, the test with -race, gofmt, and golangci-lint 2.15.0 with the trunk config .trunk/configs/.golangci.json (0 issues on changed lines).

Checklist

  • The PR title follows the Conventional Commits syntax
  • Code compiles correctly and linting (via trunk) passes locally
  • Tests added for new functionality, or regression tests for bug fixes added as applicable

See it in action
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed transaction handling after checkpoint purges. Transactions that started before the retained timestamp continue to be checked for aborts, even when a later purge uses a lower checkpoint.
    • Verified that advancing the checkpoint preserves the expected behavior for transactions on either side of the retained timestamp.

When a Zero becomes leader (after an election, or when a single Zero
restarts), updateLeases sets startTxnTs to MaxTxnTs+1 and resets keyCommit.
The new leader has lost the conflict map of the previous term, so it relies
on startTxnTs to abort every transaction that started before it took over.

purgeBelow, called when Zero applies one of its own snapshot proposals, then
set startTxnTs = minTs unconditionally. minTs is the lowest checkpoint ts
reported by the Alpha groups. A snapshot proposal can carry a checkpoint below
the floor set at the leader change, for example when a group is behind, and
then startTxnTs drops below the start ts of a transaction that is still open
from before the change. That transaction then commits without a conflict
check against writes committed in the previous term.

The unconditional assignment came in with dgraph-io#7096. Only raise startTxnTs in
purgeBelow: the floor is kept until a checkpoint exceeds it.
@myk-dol
myk-dol requested a review from a team as a code owner November 28, 2026 16:12
@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

purgeBelow now prevents startTxnTs from decreasing when it receives a lower purge timestamp. A new test checks transaction outcomes after lower and higher purges.

Changes: Oracle purge threshold

Layer / File(s) Summary
Preserve and verify transaction-start threshold
dgraph/cmd/zero/oracle.go, dgraph/cmd/zero/oracle_test.go
purgeBelow sets startTxnTs to the greater of its current value and minTs. The test checks transaction outcomes after lower and higher purge timestamps.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: go, area/core

Suggested reviewers: manishrjain, matthewmcneely, mlwelles

Merge Risk: Minimal · up to 250db

This fix keeps Zero from lowering its transaction-start floor after a leadership change, so transactions opened before the change are still checked for conflicts. The change is small and covered by a regression test. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: purgeBelow now keeps startTxnTs at max(startTxnTs, minTs) instead of assigning minTs unconditionally. The raw summary and objectives both describe this change, and the…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: purgeBelow now keeps startTxnTs at max(startTxnTs, minTs) instead of assigning minTs unconditionally. The raw summary and objectives both describe this change, and the added test covers it. The title is specific, short, and in the repository's conventional-commit style.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0936fb2f-884a-4954-9e3a-f32ca53659bb
Commits

Reviewing files that changed from the base of the PR and between e95fc26 and 250db19.

Files selected for processing: 2

  • dgraph/cmd/zero/oracle.go
  • dgraph/cmd/zero/oracle_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant