satellite/satellitedb: don't use crdb.ExecuteTx with postgres

crdb.ExecuteTx is great, but I don't think it will work right with
PostgreSQL. It works by way of cockroach savepoints, which allows
it to react to retryable errors, whereas tx.Commit() doesn't. But
I don't think PostgreSQL savepoints work exactly the same way. I'm not
100% sure, but it doesn't seem worth the risk.

So, I'm switching one case here to use the new dbutil.WithTx instead,
which will use crdb.ExecuteTx if appropriate. The other case doesn't
need a transaction at all.

Change-Id: I39283f3b5d8d47596db7aff5048bb74597e5918f
This commit is contained in:
paul cannon 2019-12-19 03:20:52 -06:00 committed by paul cannon
parent 0135852a0e
commit 4a26fb5bd5
2 changed files with 8 additions and 11 deletions

View File

@ -8,10 +8,10 @@ import (
"database/sql"
"time"
"github.com/cockroachdb/cockroach-go/crdb"
"github.com/zeebo/errs"
"storj.io/storj/private/currency"
"storj.io/storj/private/dbutil/txutil"
"storj.io/storj/satellite/rewards"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
@ -108,7 +108,7 @@ func (db *offersDB) Create(ctx context.Context, o *rewards.NewOffer) (*rewards.O
var id int64
err := crdb.ExecuteTx(ctx, db.db.DB.DB, nil, func(tx *sql.Tx) error {
err := txutil.WithTx(ctx, db.db.DB.DB, nil, func(ctx context.Context, tx *sql.Tx) error {
// If there's an existing current offer, update its status to Done and set its expires_at to be NOW()
switch o.Type {
case rewards.Partner:

View File

@ -7,7 +7,6 @@ import (
"context"
"database/sql"
"github.com/cockroachdb/cockroach-go/crdb"
"github.com/zeebo/errs"
"storj.io/common/pb"
@ -36,14 +35,12 @@ func (r *repairQueue) Select(ctx context.Context) (seg *pb.InjuredSegment, err e
defer mon.Task()(&ctx)(&err)
switch r.db.implementation {
case dbutil.Cockroach:
err = crdb.ExecuteTx(ctx, r.db.DB.DB, nil, func(tx *sql.Tx) error {
return tx.QueryRowContext(ctx, `
UPDATE injuredsegments SET attempted = now() AT TIME ZONE 'UTC' WHERE path = (
SELECT path FROM injuredsegments
WHERE attempted IS NULL OR attempted < now() AT TIME ZONE 'UTC' - interval '1 hour'
ORDER BY attempted LIMIT 1
) RETURNING data`).Scan(&seg)
})
err = r.db.QueryRowContext(ctx, `
UPDATE injuredsegments SET attempted = now() AT TIME ZONE 'UTC' WHERE path = (
SELECT path FROM injuredsegments
WHERE attempted IS NULL OR attempted < now() AT TIME ZONE 'UTC' - interval '1 hour'
ORDER BY attempted LIMIT 1
) RETURNING data`).Scan(&seg)
case dbutil.Postgres:
err = r.db.QueryRowContext(ctx, `
UPDATE injuredsegments SET attempted = now() AT TIME ZONE 'UTC' WHERE path = (