Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializable failure with recursion for DoTransaction on this error, and transaction aborted error handled #26

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion connectors/storage/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package storage
import (
"context"
"strings"
stdlibtime "time"

"github.com/cenkalti/backoff/v4"
"github.com/georgysavva/scany/v2/pgxscan"
Expand Down Expand Up @@ -38,6 +39,11 @@ func DoInTransaction(ctx context.Context, db *DB, fn func(conn QueryExecer) erro
return nil, backoff.Permanent(err) //nolint:wrapcheck // Not needed.
}
})
if err != nil && (errors.Is(err, ErrSerializationFailure) || errors.Is(err, ErrTxAborted)) {
stdlibtime.Sleep(10 * stdlibtime.Millisecond)

return DoInTransaction(ctx, db, fn)
}

return err
}
Expand Down Expand Up @@ -172,7 +178,10 @@ func IsUnexpected(err error) bool {
!IsErr(err, ErrRelationNotFound) &&
!IsErr(err, ErrNotFound) &&
!IsErr(err, ErrCheckFailed) &&
!IsErr(err, ErrRelationInUse)
!IsErr(err, ErrRelationInUse) &&
!IsErr(err, ErrSerializationFailure) &&
!IsErr(err, ErrTxAborted) &&
!IsErr(err, ErrExclusionViolation)
}

func parseDBError(err error) error { //nolint:funlen // .
Expand Down Expand Up @@ -206,6 +215,15 @@ func parseDBError(err error) error { //nolint:funlen // .

return terror.New(ErrCheckFailed, map[string]any{"column": column})
}
if dbErr.SQLState() == "40001" {
return ErrSerializationFailure
}
if dbErr.SQLState() == "25P02" {
return ErrTxAborted
}
if dbErr.SQLState() == "23P01" {
return ErrExclusionViolation
}

return err
ice-ares marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
13 changes: 8 additions & 5 deletions connectors/storage/v2/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
// Public API.

var (
ErrNotFound = errors.New("not found")
ErrRelationNotFound = errors.New("relation not found")
ErrRelationInUse = errors.New("relation in use")
ErrDuplicate = errors.New("duplicate")
ErrCheckFailed = errors.New("check failed")
ErrNotFound = errors.New("not found")
ErrRelationNotFound = errors.New("relation not found")
ErrRelationInUse = errors.New("relation in use")
ErrDuplicate = errors.New("duplicate")
ErrCheckFailed = errors.New("check failed")
ErrSerializationFailure = errors.New("serialization failure")
ErrTxAborted = errors.New("transaction aborted")
ErrExclusionViolation = errors.New("exclusion violation")
)

type (
Expand Down
15 changes: 15 additions & 0 deletions connectors/storage/v2/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ice-blockchain/wintr/terror"
)
Expand Down Expand Up @@ -140,3 +141,17 @@ $$ LANGUAGE plpgsql;`
return nil
}))
}

func TestStopWhenTxAborted(t *testing.T) {
t.Parallel()
db := MustConnect(context.Background(), "", "self")
require.NotNil(t, db)

err := DoInTransaction(context.Background(), db, func(tx QueryExecer) error {
_, gErr := Get[bool](context.Background(), tx, `SELECT $1 + $2`, 1, "2")

return gErr
})
require.ErrorIs(t, err, ErrTxAborted)
require.NoError(t, db.Close())
}
Loading