Skip to content

Commit

Permalink
vtgate: record warning for partially successful cross-shard commits
Browse files Browse the repository at this point in the history
Signed-off-by: Max Englander <[email protected]>
  • Loading branch information
maxenglander committed Dec 21, 2023
1 parent af6a08c commit 43f9e3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion go/mysql/sqlerror/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (e ErrorCode) ToString() string {
// See above reference for more information on each code.
const (
// Vitess specific errors, (100-999)
ERNotReplica = ErrorCode(100)
ERNotReplica = ErrorCode(100)
ERNonAtomicCommit = ErrorCode(301)

// unknown
ERUnknownError = ErrorCode(1105)
Expand Down
9 changes: 8 additions & 1 deletion go/vt/vtgate/tx_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"sync"

"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/dtids"
"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -132,9 +133,15 @@ func (txc *TxConn) commitNormal(ctx context.Context, session *SafeSession) error
}

// Retain backward compatibility on commit order for the normal session.
for _, shardSession := range session.ShardSessions {
for i, shardSession := range session.ShardSessions {
if err := txc.commitShard(ctx, shardSession, session.logging); err != nil {
_ = txc.Release(ctx, session)
if i > 0 {
session.RecordWarning(&querypb.QueryWarning{
Code: uint32(sqlerror.ERNonAtomicCommit),
Message: fmt.Sprintf("multi-db commit failed after committing to %d shards", i),
})
}
return err
}
}
Expand Down
10 changes: 9 additions & 1 deletion go/vt/vtgate/tx_conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/vt/discovery"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/srvtopo"
Expand Down Expand Up @@ -117,7 +118,14 @@ func TestTxConnCommitFailure(t *testing.T) {
rss1[0].Target)

require.ErrorContains(t, sc.txConn.Commit(ctx, session), expectErr.Error())
wantSession = vtgatepb.Session{}
wantSession = vtgatepb.Session{
Warnings: []*querypb.QueryWarning{
{
Code: uint32(sqlerror.ERNonAtomicCommit),
Message: "multi-db commit failed after committing to 1 shards",
},
},
}
utils.MustMatch(t, &wantSession, session.Session, "Session")
assert.EqualValues(t, 1, sbc0.CommitCount.Load(), "sbc0.CommitCount")
assert.EqualValues(t, 1, sbc1.CommitCount.Load(), "sbc1.CommitCount")
Expand Down

0 comments on commit 43f9e3d

Please sign in to comment.