feat: expose transaction failure stages and conservative outcomes
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
@@ -166,11 +167,64 @@ func TestMySQLConnectorTLSAndPoolIsolation(t *testing.T) {
|
||||
return rollbackErr
|
||||
}); !errors.Is(err, rollbackErr) {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
var txErr *database.TxError
|
||||
if !errors.As(err, &txErr) || txErr.Stage != database.TxStageWork || txErr.Outcome != database.TxOutcomeRolledBack {
|
||||
t.Fatal("real MySQL rollback was not classified", err)
|
||||
}
|
||||
}
|
||||
var count int
|
||||
if err := second.QueryRowContext(ctx, "SELECT COUNT(*) FROM connector_check").Scan(&count); err != nil || count != 1 {
|
||||
t.Fatalf("transaction count=%d err=%v", count, err)
|
||||
}
|
||||
// Commit/Rollback run against real MySQL, then a driver-boundary wrapper
|
||||
// suppresses the acknowledgement. This is deterministic fault injection,
|
||||
// not a claim to reproduce every wire-level disconnect timing.
|
||||
for _, loseCommit := range []bool{true, false} {
|
||||
name, id := "commitAcknowledgementLost", 3
|
||||
if !loseCommit {
|
||||
name, id = "rollbackAcknowledgementLost", 4
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
connector, err := mysql.NewConnector(cfg.Clone())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ackErr, workErr := errors.New("injected lost acknowledgement"), errors.New("work rejected")
|
||||
fault := &ackLossConnector{Connector: connector, loseCommit: loseCommit, ackErr: ackErr}
|
||||
faultPool, err := database.OpenConnector(ctx, fault, pool)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer faultPool.Close()
|
||||
err = faultPool.InTx(ctx, nil, func(tx *sql.Tx) error {
|
||||
if _, err := tx.ExecContext(ctx, "INSERT INTO connector_check VALUES (?)", id); err != nil {
|
||||
return err
|
||||
}
|
||||
if !loseCommit {
|
||||
return workErr
|
||||
}
|
||||
return nil
|
||||
})
|
||||
var txErr *database.TxError
|
||||
if !errors.As(err, &txErr) || txErr.Outcome != database.TxOutcomeUnknown || !errors.Is(err, ackErr) {
|
||||
t.Fatal("unknown acknowledgement was treated as definite outcome", err)
|
||||
}
|
||||
wantCount := 0
|
||||
if loseCommit {
|
||||
wantCount = 1
|
||||
if txErr.Stage != database.TxStageCommit || txErr.Err != ackErr || txErr.RollbackErr != sql.ErrTxDone || fault.commits.Load() != 1 || fault.rollbacks.Load() != 0 {
|
||||
t.Fatal("commit failure was retried or misclassified", err)
|
||||
}
|
||||
} else if txErr.Stage != database.TxStageWork || !errors.Is(err, workErr) || txErr.RollbackErr != ackErr || fault.commits.Load() != 0 || fault.rollbacks.Load() != 1 {
|
||||
t.Fatal("rollback failure lost its primary/secondary causes", err)
|
||||
}
|
||||
var stored int
|
||||
if err := second.QueryRowContext(ctx, "SELECT COUNT(*) FROM connector_check WHERE id = ?", id).Scan(&stored); err != nil || stored != wantCount {
|
||||
t.Fatal("independent connection observed unexpected persistence", stored, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
if err := first.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -178,3 +232,58 @@ func TestMySQLConnectorTLSAndPoolIsolation(t *testing.T) {
|
||||
t.Fatal("closing first pool affected second:", err)
|
||||
}
|
||||
}
|
||||
|
||||
type ackLossConnector struct {
|
||||
driver.Connector
|
||||
loseCommit bool
|
||||
ackErr error
|
||||
commits, rollbacks atomic.Int32
|
||||
}
|
||||
|
||||
func (c *ackLossConnector) Connect(ctx context.Context) (driver.Conn, error) {
|
||||
conn, err := c.Connector.Connect(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ackLossConn{Conn: conn, owner: c}, nil
|
||||
}
|
||||
|
||||
type ackLossConn struct {
|
||||
driver.Conn
|
||||
owner *ackLossConnector
|
||||
}
|
||||
|
||||
func (c *ackLossConn) Ping(ctx context.Context) error { return c.Conn.(driver.Pinger).Ping(ctx) }
|
||||
func (c *ackLossConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||
tx, err := c.Conn.(driver.ConnBeginTx).BeginTx(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ackLossTx{Tx: tx, owner: c.owner}, nil
|
||||
}
|
||||
|
||||
type ackLossTx struct {
|
||||
driver.Tx
|
||||
owner *ackLossConnector
|
||||
}
|
||||
|
||||
func (tx *ackLossTx) Commit() error {
|
||||
tx.owner.commits.Add(1)
|
||||
if err := tx.Tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
if tx.owner.loseCommit {
|
||||
return tx.owner.ackErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (tx *ackLossTx) Rollback() error {
|
||||
tx.owner.rollbacks.Add(1)
|
||||
if err := tx.Tx.Rollback(); err != nil {
|
||||
return err
|
||||
}
|
||||
if !tx.owner.loseCommit {
|
||||
return tx.owner.ackErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user