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

jobs: deflake TestRegistryLifecycle #92005

Merged
merged 1 commit into from
Nov 16, 2022
Merged
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
86 changes: 66 additions & 20 deletions pkg/jobs/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"runtime/pprof"
"sort"
"strings"
Expand Down Expand Up @@ -624,6 +625,53 @@ func TestRegistryLifecycle(t *testing.T) {
rts.resumeCheckCh <- struct{}{}
rts.check(t, jobs.StatusRunning)

r, err := regexp.Compile("retry txn")
require.NoError(t, err)

executeWithRetriableTxn := func(db *gosql.DB, fn func(txn *gosql.Tx) error) error {
txn, err := db.Begin()
if err != nil {
return err
}
defer func() {
if err != nil {
_ = txn.Rollback()
}

}()

_, err = txn.Exec("SAVEPOINT cockroach_restart")
if err != nil {
return err
}

maxRetries := 10
retryCount := 0
for {
err = fn(txn)
if err == nil {
_, err = txn.Exec("RELEASE SAVEPOINT cockroach_restart")
if err == nil {
return txn.Commit()
}
}

if !r.MatchString(err.Error()) {
return err
}

_, rollbackErr := txn.Exec("ROLLBACK TO SAVEPOINT cockroach_restart")
if rollbackErr != nil {
return errors.CombineErrors(rollbackErr, err)
}

retryCount++
if retryCount > maxRetries {
return errors.Wrap(err, "retries exhausted")
}
}
}

// Rollback a CANCEL.
{
txn, err := rts.outerDB.Begin()
Expand Down Expand Up @@ -661,19 +709,18 @@ func TestRegistryLifecycle(t *testing.T) {
}
// Now pause it for reals.
{
txn, err := rts.outerDB.Begin()
err := executeWithRetriableTxn(rts.outerDB, func(txn *gosql.Tx) error {
if _, err := txn.Exec("PAUSE JOB $1", job.ID()); err != nil {
return err
}
// Not committed yet, so state shouldn't have changed.
// Don't check status in txn.
rts.check(t, "")
return nil
})
if err != nil {
t.Fatal(err)
}
if _, err := txn.Exec("PAUSE JOB $1", job.ID()); err != nil {
t.Fatal(err)
}
// Not committed yet, so state shouldn't have changed.
// Don't check status in txn.
rts.check(t, "")
if err := txn.Commit(); err != nil {
t.Fatal(err)
}
rts.check(t, jobs.StatusPaused)
}
// Rollback a RESUME.
Expand All @@ -692,19 +739,18 @@ func TestRegistryLifecycle(t *testing.T) {
}
// Commit a RESUME.
{
txn, err := rts.outerDB.Begin()
err := executeWithRetriableTxn(rts.outerDB, func(txn *gosql.Tx) error {
if _, err := txn.Exec("RESUME JOB $1", job.ID()); err != nil {
return err
}
// Not committed yet, so state shouldn't have changed.
// Don't check status in txn.
rts.check(t, "")
return nil
})
if err != nil {
t.Fatal(err)
}
if _, err := txn.Exec("RESUME JOB $1", job.ID()); err != nil {
t.Fatal(err)
}
// Not committed yet, so state shouldn't have changed.
// Don't check status in txn.
rts.check(t, "")
if err := txn.Commit(); err != nil {
t.Fatal(err)
}
}
rts.mu.e.ResumeStart = true
rts.check(t, jobs.StatusRunning)
Expand Down