Skip to content

workaround txn commit failed but tn handled - #22139

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
iamlinjunhong:m-22115
Jul 9, 2025
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
iamlinjunhong:m-22115

Conversation

@iamlinjunhong

@iamlinjunhong iamlinjunhong commented Jul 9, 2025 •

Copy link
Copy Markdown
Contributor

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #22115

What this PR does / why we need it:

workaround txn commit failed but tn handled


PR Type

Bug fix


Description

  • Add workaround for transaction commit failures

  • Set CommitTS to now+10s when commit send fails

  • Include test coverage for the workaround behavior


Changes diagram

flowchart LR
  A["doSend method"] --> B["Check if commit failed"]
  B --> C["Set CommitTS = now+10s"]
  C --> D["Return error"]
  E["Test"] --> F["Verify CommitTS workaround"]
Loading

Changes walkthrough 📝

Relevant files
Bug fix
operator.go
Add commit failure workaround logic                                           

pkg/txn/client/operator.go

  • Add error handling in doSend method for commit failures
  • Set CommitTS to current time plus 10 seconds as workaround
  • Include TODO comment indicating temporary nature of fix
  • +7/-0     
    Tests
    operator_test.go
    Add test for commit workaround                                                     

    pkg/txn/client/operator_test.go

  • Add test TestCommitSendErrorSetsCommitTSWorkaround
  • Verify CommitTS is set to now+10s on send error
  • Use manual sender to simulate commit failure
  • +14/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @qodo-code-review

    Copy link
    Copy Markdown

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Hardcoded Value

    The 10-second offset is hardcoded as a magic number (10000000000 nanoseconds). This should be extracted to a named constant for better maintainability and clarity.

    now.PhysicalTime += 10000000000
    tc.mu.txn.CommitTS = now
    Error Handling

    The clock.Now() error is ignored with blank identifier. While this may be acceptable for a workaround, consider logging or handling potential clock errors appropriately.

    now, _ := tc.clock.Now()
    now.PhysicalTime += 10000000000
    Test Precision

    The test uses a large delta (1e9 nanoseconds = 1 second) for time comparison which may be too permissive and could mask timing issues in the workaround logic.

    	assert.InDelta(t, start+10000000000, commitTS, 1e9, "CommitTS should be set to now+10s on send error")
    })

    @qodo-code-review

    Copy link
    Copy Markdown

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Handle clock error properly

    The error from tc.clock.Now() is being ignored with blank identifier. Since this
    is a workaround for commit failures, clock errors could compound the issue and
    should be handled appropriately.

    pkg/txn/client/operator.go [1016-1018]

    -now, _ := tc.clock.Now()
    +now, err := tc.clock.Now()
    +if err != nil {
    +    tc.logger.Error("failed to get current time for CommitTS workaround", zap.Error(err))
    +    return nil, err
    +}
     now.PhysicalTime += 10000000000
     tc.mu.txn.CommitTS = now
    • Apply / Chat
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly points out an ignored error from tc.clock.Now(), and proposing to handle it improves the robustness of this error handling path.

    Medium
    Use named constant for hardcoded value

    The hardcoded 10-second offset (10000000000 nanoseconds) should be made
    configurable or use a named constant. This makes the workaround more
    maintainable and allows for easier adjustment without code changes.

    pkg/txn/client/operator.go [1013-1019]

     if commit {
         // TODO: remove this workaround
         // set tc.mu.txn.CommitTS = now+10s
    +    const commitTSWorkaroundOffset = 10 * time.Second
         now, _ := tc.clock.Now()
    -    now.PhysicalTime += 10000000000
    +    now.PhysicalTime += int64(commitTSWorkaroundOffset)
         tc.mu.txn.CommitTS = now
     }
    • Apply / Chat
    Suggestion importance[1-10]: 5

    __

    Why: The suggestion correctly identifies a magic number and proposes using a named constant, which improves readability and maintainability, even in temporary workaround code.

    Low
    • More

    @mergify

    mergify Bot commented Jul 9, 2025

    Copy link
    Copy Markdown
    Contributor

    This pull request has been removed from the queue for the following reason: checks failed.

    The merge conditions cannot be satisfied due to failing checks:

    You may have to fix your CI before adding the pull request to the queue again.
    If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
    If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio requeue comment.

    @mergify
    mergify Bot merged commit 2c96731 into matrixorigin:main Jul 9, 2025
    fengttt pushed a commit to fengttt/matrixone that referenced this pull request May 7, 2026
    workaround txn commit failed but tn handled
    
    Approved by: @zhangxu19830126
    XuPeng-SH pushed a commit that referenced this pull request Jul 15, 2026
    ## What type of PR is this?
    
    - [ ] API-change
    - [x] BUG
    - [ ] Improvement
    - [ ] Documentation
    - [ ] Feature
    - [x] Test and CI
    - [ ] Code Refactoring
    
    ## Which issue(s) this PR fixes:
    
    Fixes #25704
    
    Related: #25553, #22115, #22139
    
    ## What this PR does / why we need it:
    
    An `ErrTxnUnknown` means TN may already have accepted the Commit while
    CN lost the response. Releasing its pessimistic locks with an empty or
    synthetic timestamp can therefore allow a conflicting writer to proceed
    before TN has resolved the original Commit.
    
    This PR makes unknown-result cleanup fail closed without changing the
    normal Commit or Rollback path:
    
    - The multi-request sender classifies an ambiguous Commit `Stream.Send`
    completion failure as `ErrTxnUnknown`; known pre-send failures and
    Rollback retain their existing behavior.
    - The txn operator transfers only pessimistic `ErrTxnUnknown` lock
    cleanup to lockservice. Optimistic unknown results retain their
    workspace/status handling but do not retain lock-resolution admission
    because they own no pessimistic locks.
    - The resolver keeps the source txn registered, retries bounded remote
    cleanup, and unlocks only after `CannotCommit` returns a non-empty
    allocator fence timestamp.
    - Resolver-owned user transactions continue consuming their existing
    `MaxActiveTxn` slots until terminal lock cleanup. Allocator outages
    therefore backpressure new user transactions through the existing
    cancellable queue.
    - Internal pessimistic Commits reserve a separate finite safety slot
    before workspace preparation and before Commit can be sent to TN. The
    limit is `max(MaxActiveTxn, 1024)` in production. At saturation, a new
    internal Commit returns the existing CN-busy error and performs full
    Rollback/Unlock before any Commit request is sent. Known outcomes
    release immediately; unknown outcomes transfer the slot to the resolver
    until terminal cleanup. This bounds internal resolver pending entries,
    active holders, retained locks, and retry work without putting normal
    user transactions on the internal quota.
    - Every new Commit carries an absolute deadline and a
    source-lockservice-local monotonic sequence. TN applies its configured
    HLC max-offset before treating this CN-derived deadline as expired.
    Unknown-result fences preserve each `(sequence, expiry)` pair while the
    frontier is below 1024 entries. At saturation, they collapse to one
    conservative per-source fence using the maximum sequence and latest
    expiry.
    - Proxy holder handoff is published locally only after owner
    acknowledgement. A response-lost handoff records its replacement as
    pending, and retries preserve that representative even if later local
    shared holders join.
    - The owner holder map migrates its key together with the replacement
    value, so the replacement is recognized as holder for subsequent Unlock.
    
    This is not a transaction 2PC change. Normal user Commit, known terminal
    errors, Rollback, and optimistic lock cleanup do not enter the resolver
    or add lockservice RPCs.
    
    ## Validation:
    
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/client -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/rpc
    ./pkg/frontend/test/mock_lock -count=1`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/txn/client -run
    'Test(OptimisticCommitUnknownReleasesAdmission|CommitUnknownScheduleFailureReleasesAdmission|InternalCommitUnknownAdmissionIsBounded|CommitUnknownRetainsAdmissionUntilResolution|CommitUnknownResolvedBeforeCloseReleasesAdmissionOnce)$'
    -count=20`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/lockservice -run
    'TestResolveCommitUnknownWaitsForAllocatorFence$' -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -run
    'TestResolveCommitUnknown(WaitsForAllocatorFence|CompletesWhenTxnAlreadyUnlocked)$'
    -count=1`
    - `git diff --check`
    
    `go test ./pkg/txn/service -run '^TestCommitRequestExpired$' -count=1`
    is blocked locally before package compilation because this host lacks
    the repository CGo dependencies (`usearch.h`, `roaring.h`, and
    `xxhash.h`).
    
    ---------
    
    Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
    VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
    …gin#25722)
    
    ## What type of PR is this?
    
    - [ ] API-change
    - [x] BUG
    - [ ] Improvement
    - [ ] Documentation
    - [ ] Feature
    - [x] Test and CI
    - [ ] Code Refactoring
    
    ## Which issue(s) this PR fixes:
    
    Fixes matrixorigin#25704
    
    Related: matrixorigin#25553, matrixorigin#22115, matrixorigin#22139
    
    ## What this PR does / why we need it:
    
    An `ErrTxnUnknown` means TN may already have accepted the Commit while
    CN lost the response. Releasing its pessimistic locks with an empty or
    synthetic timestamp can therefore allow a conflicting writer to proceed
    before TN has resolved the original Commit.
    
    This PR makes unknown-result cleanup fail closed without changing the
    normal Commit or Rollback path:
    
    - The multi-request sender classifies an ambiguous Commit `Stream.Send`
    completion failure as `ErrTxnUnknown`; known pre-send failures and
    Rollback retain their existing behavior.
    - The txn operator transfers only pessimistic `ErrTxnUnknown` lock
    cleanup to lockservice. Optimistic unknown results retain their
    workspace/status handling but do not retain lock-resolution admission
    because they own no pessimistic locks.
    - The resolver keeps the source txn registered, retries bounded remote
    cleanup, and unlocks only after `CannotCommit` returns a non-empty
    allocator fence timestamp.
    - Resolver-owned user transactions continue consuming their existing
    `MaxActiveTxn` slots until terminal lock cleanup. Allocator outages
    therefore backpressure new user transactions through the existing
    cancellable queue.
    - Internal pessimistic Commits reserve a separate finite safety slot
    before workspace preparation and before Commit can be sent to TN. The
    limit is `max(MaxActiveTxn, 1024)` in production. At saturation, a new
    internal Commit returns the existing CN-busy error and performs full
    Rollback/Unlock before any Commit request is sent. Known outcomes
    release immediately; unknown outcomes transfer the slot to the resolver
    until terminal cleanup. This bounds internal resolver pending entries,
    active holders, retained locks, and retry work without putting normal
    user transactions on the internal quota.
    - Every new Commit carries an absolute deadline and a
    source-lockservice-local monotonic sequence. TN applies its configured
    HLC max-offset before treating this CN-derived deadline as expired.
    Unknown-result fences preserve each `(sequence, expiry)` pair while the
    frontier is below 1024 entries. At saturation, they collapse to one
    conservative per-source fence using the maximum sequence and latest
    expiry.
    - Proxy holder handoff is published locally only after owner
    acknowledgement. A response-lost handoff records its replacement as
    pending, and retries preserve that representative even if later local
    shared holders join.
    - The owner holder map migrates its key together with the replacement
    value, so the replacement is recognized as holder for subsequent Unlock.
    
    This is not a transaction 2PC change. Normal user Commit, known terminal
    errors, Rollback, and optimistic lock cleanup do not enter the resolver
    or add lockservice RPCs.
    
    ## Validation:
    
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/client -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/rpc
    ./pkg/frontend/test/mock_lock -count=1`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/txn/client -run
    'Test(OptimisticCommitUnknownReleasesAdmission|CommitUnknownScheduleFailureReleasesAdmission|InternalCommitUnknownAdmissionIsBounded|CommitUnknownRetainsAdmissionUntilResolution|CommitUnknownResolvedBeforeCloseReleasesAdmissionOnce)$'
    -count=20`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/lockservice -run
    'TestResolveCommitUnknownWaitsForAllocatorFence$' -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -run
    'TestResolveCommitUnknown(WaitsForAllocatorFence|CompletesWhenTxnAlreadyUnlocked)$'
    -count=1`
    - `git diff --check`
    
    `go test ./pkg/txn/service -run '^TestCommitRequestExpired$' -count=1`
    is blocked locally before package compilation because this host lacks
    the repository CGo dependencies (`usearch.h`, `roaring.h`, and
    `xxhash.h`).
    
    ---------
    
    Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
    VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
    …gin#25722)
    
    ## What type of PR is this?
    
    - [ ] API-change
    - [x] BUG
    - [ ] Improvement
    - [ ] Documentation
    - [ ] Feature
    - [x] Test and CI
    - [ ] Code Refactoring
    
    ## Which issue(s) this PR fixes:
    
    Fixes matrixorigin#25704
    
    Related: matrixorigin#25553, matrixorigin#22115, matrixorigin#22139
    
    ## What this PR does / why we need it:
    
    An `ErrTxnUnknown` means TN may already have accepted the Commit while
    CN lost the response. Releasing its pessimistic locks with an empty or
    synthetic timestamp can therefore allow a conflicting writer to proceed
    before TN has resolved the original Commit.
    
    This PR makes unknown-result cleanup fail closed without changing the
    normal Commit or Rollback path:
    
    - The multi-request sender classifies an ambiguous Commit `Stream.Send`
    completion failure as `ErrTxnUnknown`; known pre-send failures and
    Rollback retain their existing behavior.
    - The txn operator transfers only pessimistic `ErrTxnUnknown` lock
    cleanup to lockservice. Optimistic unknown results retain their
    workspace/status handling but do not retain lock-resolution admission
    because they own no pessimistic locks.
    - The resolver keeps the source txn registered, retries bounded remote
    cleanup, and unlocks only after `CannotCommit` returns a non-empty
    allocator fence timestamp.
    - Resolver-owned user transactions continue consuming their existing
    `MaxActiveTxn` slots until terminal lock cleanup. Allocator outages
    therefore backpressure new user transactions through the existing
    cancellable queue.
    - Internal pessimistic Commits reserve a separate finite safety slot
    before workspace preparation and before Commit can be sent to TN. The
    limit is `max(MaxActiveTxn, 1024)` in production. At saturation, a new
    internal Commit returns the existing CN-busy error and performs full
    Rollback/Unlock before any Commit request is sent. Known outcomes
    release immediately; unknown outcomes transfer the slot to the resolver
    until terminal cleanup. This bounds internal resolver pending entries,
    active holders, retained locks, and retry work without putting normal
    user transactions on the internal quota.
    - Every new Commit carries an absolute deadline and a
    source-lockservice-local monotonic sequence. TN applies its configured
    HLC max-offset before treating this CN-derived deadline as expired.
    Unknown-result fences preserve each `(sequence, expiry)` pair while the
    frontier is below 1024 entries. At saturation, they collapse to one
    conservative per-source fence using the maximum sequence and latest
    expiry.
    - Proxy holder handoff is published locally only after owner
    acknowledgement. A response-lost handoff records its replacement as
    pending, and retries preserve that representative even if later local
    shared holders join.
    - The owner holder map migrates its key together with the replacement
    value, so the replacement is recognized as holder for subsequent Unlock.
    
    This is not a transaction 2PC change. Normal user Commit, known terminal
    errors, Rollback, and optimistic lock cleanup do not enter the resolver
    or add lockservice RPCs.
    
    ## Validation:
    
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/client -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/rpc
    ./pkg/frontend/test/mock_lock -count=1`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/txn/client -run
    'Test(OptimisticCommitUnknownReleasesAdmission|CommitUnknownScheduleFailureReleasesAdmission|InternalCommitUnknownAdmissionIsBounded|CommitUnknownRetainsAdmissionUntilResolution|CommitUnknownResolvedBeforeCloseReleasesAdmissionOnce)$'
    -count=20`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/lockservice -run
    'TestResolveCommitUnknownWaitsForAllocatorFence$' -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -run
    'TestResolveCommitUnknown(WaitsForAllocatorFence|CompletesWhenTxnAlreadyUnlocked)$'
    -count=1`
    - `git diff --check`
    
    `go test ./pkg/txn/service -run '^TestCommitRequestExpired$' -count=1`
    is blocked locally before package compilation because this host lacks
    the repository CGo dependencies (`usearch.h`, `roaring.h`, and
    `xxhash.h`).
    
    ---------
    
    Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
    VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
    …gin#25722)
    
    ## What type of PR is this?
    
    - [ ] API-change
    - [x] BUG
    - [ ] Improvement
    - [ ] Documentation
    - [ ] Feature
    - [x] Test and CI
    - [ ] Code Refactoring
    
    ## Which issue(s) this PR fixes:
    
    Fixes matrixorigin#25704
    
    Related: matrixorigin#25553, matrixorigin#22115, matrixorigin#22139
    
    ## What this PR does / why we need it:
    
    An `ErrTxnUnknown` means TN may already have accepted the Commit while
    CN lost the response. Releasing its pessimistic locks with an empty or
    synthetic timestamp can therefore allow a conflicting writer to proceed
    before TN has resolved the original Commit.
    
    This PR makes unknown-result cleanup fail closed without changing the
    normal Commit or Rollback path:
    
    - The multi-request sender classifies an ambiguous Commit `Stream.Send`
    completion failure as `ErrTxnUnknown`; known pre-send failures and
    Rollback retain their existing behavior.
    - The txn operator transfers only pessimistic `ErrTxnUnknown` lock
    cleanup to lockservice. Optimistic unknown results retain their
    workspace/status handling but do not retain lock-resolution admission
    because they own no pessimistic locks.
    - The resolver keeps the source txn registered, retries bounded remote
    cleanup, and unlocks only after `CannotCommit` returns a non-empty
    allocator fence timestamp.
    - Resolver-owned user transactions continue consuming their existing
    `MaxActiveTxn` slots until terminal lock cleanup. Allocator outages
    therefore backpressure new user transactions through the existing
    cancellable queue.
    - Internal pessimistic Commits reserve a separate finite safety slot
    before workspace preparation and before Commit can be sent to TN. The
    limit is `max(MaxActiveTxn, 1024)` in production. At saturation, a new
    internal Commit returns the existing CN-busy error and performs full
    Rollback/Unlock before any Commit request is sent. Known outcomes
    release immediately; unknown outcomes transfer the slot to the resolver
    until terminal cleanup. This bounds internal resolver pending entries,
    active holders, retained locks, and retry work without putting normal
    user transactions on the internal quota.
    - Every new Commit carries an absolute deadline and a
    source-lockservice-local monotonic sequence. TN applies its configured
    HLC max-offset before treating this CN-derived deadline as expired.
    Unknown-result fences preserve each `(sequence, expiry)` pair while the
    frontier is below 1024 entries. At saturation, they collapse to one
    conservative per-source fence using the maximum sequence and latest
    expiry.
    - Proxy holder handoff is published locally only after owner
    acknowledgement. A response-lost handoff records its replacement as
    pending, and retries preserve that representative even if later local
    shared holders join.
    - The owner holder map migrates its key together with the replacement
    value, so the replacement is recognized as holder for subsequent Unlock.
    
    This is not a transaction 2PC change. Normal user Commit, known terminal
    errors, Rollback, and optimistic lock cleanup do not enter the resolver
    or add lockservice RPCs.
    
    ## Validation:
    
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/client -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/txn/rpc
    ./pkg/frontend/test/mock_lock -count=1`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/txn/client -run
    'Test(OptimisticCommitUnknownReleasesAdmission|CommitUnknownScheduleFailureReleasesAdmission|InternalCommitUnknownAdmissionIsBounded|CommitUnknownRetainsAdmissionUntilResolution|CommitUnknownResolvedBeforeCloseReleasesAdmissionOnce)$'
    -count=20`
    - `GOFLAGS='-mod=mod' go test -race ./pkg/lockservice -run
    'TestResolveCommitUnknownWaitsForAllocatorFence$' -count=1`
    - `GOFLAGS='-mod=mod' go test ./pkg/lockservice -run
    'TestResolveCommitUnknown(WaitsForAllocatorFence|CompletesWhenTxnAlreadyUnlocked)$'
    -count=1`
    - `git diff --check`
    
    `go test ./pkg/txn/service -run '^TestCommitRequestExpired$' -count=1`
    is blocked locally before package compilation because this host lacks
    the repository CGo dependencies (`usearch.h`, `roaring.h`, and
    `xxhash.h`).
    
    ---------
    
    Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

    This branch was previously deployed

    1 inactive deployment
    ci — ed9a6cbb Deployed Jul 9, 2025 by mergify[bot]
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Review effort 2/5 size/S Denotes a PR that changes [10,99] lines

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    3 participants