Skip to content

Commit

Permalink
DB: Remove AtomicContextWithRetryClearFn, rename to AtomicContext (#6231
Browse files Browse the repository at this point in the history
)
  • Loading branch information
cce authored Jan 23, 2025
1 parent d52e3dd commit a81d54f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
12 changes: 6 additions & 6 deletions ledger/store/trackerdb/sqlitedriver/sqlitedriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *trackerSQLStore) Batch(fn trackerdb.BatchFn) (err error) {
func (s *trackerSQLStore) BatchContext(ctx context.Context, fn trackerdb.BatchFn) (err error) {
return wrapIOError(s.pair.Wdb.AtomicContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
return fn(ctx, &sqlBatchScope{tx, false, &sqlWriter{tx}})
}))
}, nil))
}

func (s *trackerSQLStore) BeginBatch(ctx context.Context) (trackerdb.Batch, error) {
Expand All @@ -89,7 +89,7 @@ func (s *trackerSQLStore) Snapshot(fn trackerdb.SnapshotFn) (err error) {
func (s *trackerSQLStore) SnapshotContext(ctx context.Context, fn trackerdb.SnapshotFn) (err error) {
return wrapIOError(s.pair.Rdb.AtomicContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
return fn(ctx, &sqlSnapshotScope{tx, &sqlReader{tx}})
}))
}, nil))
}

func (s *trackerSQLStore) BeginSnapshot(ctx context.Context) (trackerdb.Snapshot, error) {
Expand All @@ -111,11 +111,11 @@ func (s *trackerSQLStore) TransactionWithRetryClearFn(fn trackerdb.TransactionFn
func (s *trackerSQLStore) TransactionContext(ctx context.Context, fn trackerdb.TransactionFn) (err error) {
return wrapIOError(s.pair.Wdb.AtomicContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
return fn(ctx, &sqlTransactionScope{tx, false, &sqlReader{tx}, &sqlWriter{tx}, &sqlCatchpoint{tx}})
}))
}, nil))
}

func (s *trackerSQLStore) TransactionContextWithRetryClearFn(ctx context.Context, fn trackerdb.TransactionFn, rollbackFn trackerdb.RetryClearFn) (err error) {
return wrapIOError(s.pair.Wdb.AtomicContextWithRetryClearFn(ctx, func(ctx context.Context, tx *sql.Tx) error {
return wrapIOError(s.pair.Wdb.AtomicContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
return fn(ctx, &sqlTransactionScope{tx, false, &sqlReader{tx}, &sqlWriter{tx}, &sqlCatchpoint{tx}})
}, rollbackFn))
}
Expand All @@ -132,7 +132,7 @@ func (s trackerSQLStore) RunMigrations(ctx context.Context, params trackerdb.Par
err = wrapIOError(s.pair.Wdb.AtomicContext(ctx, func(ctx context.Context, tx *sql.Tx) error {
mgr, err = RunMigrations(ctx, tx, params, log, targetVersion)
return err
}))
}, nil))
return
}

Expand All @@ -159,7 +159,7 @@ func (s *trackerSQLStore) ResetToV6Test(ctx context.Context) error {
}
}
return nil
})
}, nil)
}

func (s *trackerSQLStore) Close() {
Expand Down
14 changes: 4 additions & 10 deletions util/db/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,15 @@ func (db *Accessor) IsSharedCacheConnection() bool {
// The return error of fn should be a native sqlite3.Error type or an error wrapping it.
// DO NOT return a custom error - the internal logic of Atomic expects an sqlite error and uses that value.
func (db *Accessor) Atomic(fn idemFn, extras ...interface{}) (err error) {
return db.AtomicContext(context.Background(), fn, extras...)
return db.AtomicContext(context.Background(), fn, nil, extras...)
}

// AtomicContext executes a piece of code with respect to the database atomically.
// For transactions where readOnly is false, sync determines whether or not to wait for the result.
// Like for Atomic, the return error of fn should be a native sqlite3.Error type or an error wrapping it.
func (db *Accessor) AtomicContext(ctx context.Context, fn idemFn, extras ...interface{}) (err error) {

return db.AtomicContextWithRetryClearFn(ctx, fn, nil, extras...)
}

// AtomicContextWithRetryClearFn is like AtomicContext, but calls retryClearFn if the database
// txn was rolled back, due to error or in between retries. This helps a caller that
// might change in-memory state inside fn.
func (db *Accessor) AtomicContextWithRetryClearFn(ctx context.Context, fn idemFn, retryClearFn func(context.Context), extras ...interface{}) (err error) {
// If retryClearFn is provided, it will be called in between retries of calls to fn, if the error is a
// temporary error that will be retried. This helps a caller that might change in-memory state inside fn.
func (db *Accessor) AtomicContext(ctx context.Context, fn idemFn, retryClearFn func(context.Context), extras ...interface{}) (err error) {
atomicDeadline := time.Now().Add(time.Second)

// note that the sql library will drop panics inside an active transaction
Expand Down

0 comments on commit a81d54f

Please sign in to comment.