storj/private/migrate/create.go
Jeff Wendling d20db90cff private/dbutil/txutil: create new transactions for retries
it was noticed that if you had a long lived transaction A that
was blocking some other transaction B and A was being aborted
due to retriable errors, then transaction B was never given
priority. this was due to using savepoints to do lightweight
retries.

this behavior was problematic becaue we had some queries blocked
for over 16 hours, so this commit addresses the issue with two
prongs:

    1. bound the amount of time we will retry a transaction
    2. create new transactions when a retry is needed

the first ensures that we never wait for 16 hours, and the value
chosen is 10 minutes. that should be long enough for an ample
amount of retries for small queries, and huge queries probably
shouldn't be retried, even if possible: it's more preferrable to
find a way to make them smaller.

the second ensures that even in the case of retries, queries that
are blocked on the aborted transaction gain priority to run.

between those two changes, the maximum stall time due to retries
should be bounded to around 10 minutes.

Change-Id: Icf898501ef505a89738820a3fae2580988f9f5f4
2020-02-01 18:34:28 +00:00

67 lines
1.6 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package migrate
import (
"context"
"database/sql"
"github.com/zeebo/errs"
"storj.io/storj/private/dbutil/txutil"
"storj.io/storj/private/tagsql"
)
// Error is the default migrate errs class
var Error = errs.Class("migrate")
// Create with a previous schema check
func Create(ctx context.Context, identifier string, db DBX) error {
// is this necessary? it's not immediately obvious why we roll back the transaction
// when the schemas match.
justRollbackPlease := errs.Class("only used to tell WithTx to do a rollback")
err := txutil.WithTx(ctx, db, nil, func(ctx context.Context, tx tagsql.Tx) (err error) {
schema := db.Schema()
_, err = tx.ExecContext(ctx, db.Rebind(`CREATE TABLE IF NOT EXISTS table_schemas (id text, schemaText text);`))
if err != nil {
return err
}
row := tx.QueryRow(ctx, db.Rebind(`SELECT schemaText FROM table_schemas WHERE id = ?;`), identifier)
var previousSchema string
err = row.Scan(&previousSchema)
// not created yet
if err == sql.ErrNoRows {
_, err := tx.ExecContext(ctx, schema)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, db.Rebind(`INSERT INTO table_schemas(id, schemaText) VALUES (?, ?);`), identifier, schema)
if err != nil {
return err
}
return nil
}
if err != nil {
return err
}
if schema != previousSchema {
return Error.New("schema mismatch:\nold %v\nnew %v", previousSchema, schema)
}
return justRollbackPlease.New("")
})
if justRollbackPlease.Has(err) {
err = nil
}
return Error.Wrap(err)
}