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

sql: attempt to reduce duplication in legacy schema changer txn management #91567

Merged
merged 1 commit into from
Nov 9, 2022
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
30 changes: 17 additions & 13 deletions pkg/sql/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,17 @@ func (sc *SchemaChanger) makeFixedTimestampRunner(readAsOf hlc.Timestamp) histor
func (sc *SchemaChanger) makeFixedTimestampInternalExecRunner(
readAsOf hlc.Timestamp,
) descs.HistoricalInternalExecTxnRunner {
return descs.NewHistoricalInternalExecTxnRunner(readAsOf, func(ctx context.Context, retryable descs.InternalExecFn) error {
return sc.fixedTimestampTxn(ctx, readAsOf, func(
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection,
return descs.NewHistoricalInternalExecTxnRunner(readAsOf, func(
ctx context.Context, retryable descs.InternalExecFn,
) error {
return sc.fixedTimestampTxnWithExecutor(ctx, readAsOf, func(
ctx context.Context,
txn *kv.Txn,
_ *sessiondata.SessionData,
descriptors *descs.Collection,
ie sqlutil.InternalExecutor,
) error {
// We need to re-create the evalCtx since the txn may retry.
ie := sc.ieFactory.NewInternalExecutor(NewFakeSessionData(sc.execCfg.SV()))
return retryable(ctx, txn, ie, nil /* descriptors */)
return retryable(ctx, txn, ie, descriptors)
})
})
}
Expand All @@ -188,10 +192,10 @@ func (sc *SchemaChanger) fixedTimestampTxn(
readAsOf hlc.Timestamp,
retryable func(ctx context.Context, txn *kv.Txn, descriptors *descs.Collection) error,
) error {
return sc.txn(ctx, func(ctx context.Context, txn *kv.Txn, descriptors *descs.Collection) error {
if err := txn.SetFixedTimestamp(ctx, readAsOf); err != nil {
return err
}
return sc.fixedTimestampTxnWithExecutor(ctx, readAsOf, func(
ctx context.Context, txn *kv.Txn, _ *sessiondata.SessionData,
descriptors *descs.Collection, _ sqlutil.InternalExecutor,
) error {
return retryable(ctx, txn, descriptors)
})
}
Expand All @@ -207,9 +211,9 @@ func (sc *SchemaChanger) fixedTimestampTxnWithExecutor(
ie sqlutil.InternalExecutor,
) error,
) error {
sd := NewFakeSessionData(sc.execCfg.SV())
return sc.txnWithExecutor(ctx, sd, func(
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection, ie sqlutil.InternalExecutor,
return sc.txnWithExecutor(ctx, func(
ctx context.Context, txn *kv.Txn, sd *sessiondata.SessionData,
descriptors *descs.Collection, ie sqlutil.InternalExecutor,
) error {
if err := txn.SetFixedTimestamp(ctx, readAsOf); err != nil {
return err
Expand Down
27 changes: 17 additions & 10 deletions pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2456,27 +2456,34 @@ func (*SchemaChangerTestingKnobs) ModuleTestingKnobs() {}
func (sc *SchemaChanger) txn(
ctx context.Context, f func(context.Context, *kv.Txn, *descs.Collection) error,
) error {
if fn := sc.testingKnobs.RunBeforeDescTxn; fn != nil {
if err := fn(sc.job.ID()); err != nil {
return err
}
}
return sc.execCfg.InternalExecutorFactory.DescsTxn(ctx, sc.db, f)
return sc.txnWithExecutor(ctx, func(
ctx context.Context, txn *kv.Txn, _ *sessiondata.SessionData,
collection *descs.Collection, _ sqlutil.InternalExecutor,
) error {
return f(ctx, txn, collection)
})
}

// txnWithExecutor is to run internal executor within a txn.
func (sc *SchemaChanger) txnWithExecutor(
ctx context.Context,
sd *sessiondata.SessionData,
f func(context.Context, *kv.Txn, *descs.Collection, sqlutil.InternalExecutor) error,
f func(
context.Context, *kv.Txn, *sessiondata.SessionData,
*descs.Collection, sqlutil.InternalExecutor,
) error,
) error {
if fn := sc.testingKnobs.RunBeforeDescTxn; fn != nil {
if err := fn(sc.job.ID()); err != nil {
return err
}
}
return sc.execCfg.InternalExecutorFactory.
DescsTxnWithExecutor(ctx, sc.db, sd, f)
sd := NewFakeSessionData(sc.execCfg.SV())
return sc.execCfg.InternalExecutorFactory.DescsTxnWithExecutor(ctx, sc.db, sd, func(
ctx context.Context, txn *kv.Txn, descriptors *descs.Collection,
ie sqlutil.InternalExecutor,
) error {
return f(ctx, txn, sd, descriptors, ie)
})
}

// createSchemaChangeEvalCtx creates an extendedEvalContext() to be used for backfills.
Expand Down