Skip to content

Commit

Permalink
Merge pull request #93434 from Xiang-Gu/backport22.2-92774
Browse files Browse the repository at this point in the history
Fixes #91528
  • Loading branch information
Xiang-Gu authored Dec 12, 2022
2 parents 2a04626 + 7fdbc41 commit c50f15f
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 22 deletions.
5 changes: 5 additions & 0 deletions pkg/sql/schemachanger/scdeps/exec_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ func (d *execDeps) User() username.SQLUsername {
return d.user
}

// ClusterSettings implements the scexec.Dependencies interface.
func (d *execDeps) ClusterSettings() *cluster.Settings {
return d.settings
}

// DescriptorMetadataUpdater implements the scexec.Dependencies interface.
func (d *execDeps) DescriptorMetadataUpdater(ctx context.Context) scexec.DescriptorMetadataUpdater {
return d.metadataUpdater
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/schemachanger/scexec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
"//pkg/keys",
"//pkg/roachpb",
"//pkg/security/username",
"//pkg/settings/cluster",
"//pkg/sql/catalog",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/nstree",
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/schemachanger/scexec/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec/scmutationexec"
Expand Down Expand Up @@ -48,6 +49,7 @@ type Dependencies interface {
// Statements returns the statements behind this schema change.
Statements() []string
User() username.SQLUsername
ClusterSettings() *cluster.Settings
}

// Catalog encapsulates the catalog-related dependencies for the executor.
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/schemachanger/scexec/mocks_generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 25 additions & 19 deletions pkg/sql/schemachanger/scplan/internal/scstage/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ func BuildStages(
phase scop.Phase,
g *scgraph.Graph,
scJobIDSupplier func() jobspb.JobID,
enforcePlannerSanityChecks bool,
) []Stage {
c := buildContext{
rollback: init.InRollback,
g: g,
isRevertibilityIgnored: true,
targetState: init.TargetState,
startingStatuses: init.Current,
startingPhase: phase,
descIDs: screl.AllTargetDescIDs(init.TargetState),
rollback: init.InRollback,
g: g,
isRevertibilityIgnored: true,
targetState: init.TargetState,
startingStatuses: init.Current,
startingPhase: phase,
descIDs: screl.AllTargetDescIDs(init.TargetState),
enforcePlannerSanityChecks: enforcePlannerSanityChecks,
}
// Try building stages while ignoring revertibility constraints.
// This is fine as long as there are no post-commit stages.
Expand Down Expand Up @@ -92,14 +94,15 @@ func BuildStages(
// buildContext contains the global constants for building the stages.
// Only the BuildStages function mutates it, it's read-only everywhere else.
type buildContext struct {
rollback bool
g *scgraph.Graph
scJobID jobspb.JobID
isRevertibilityIgnored bool
targetState scpb.TargetState
startingStatuses []scpb.Status
startingPhase scop.Phase
descIDs catalog.DescriptorIDSet
rollback bool
g *scgraph.Graph
scJobID jobspb.JobID
isRevertibilityIgnored bool
targetState scpb.TargetState
startingStatuses []scpb.Status
startingPhase scop.Phase
descIDs catalog.DescriptorIDSet
enforcePlannerSanityChecks bool
}

func buildStages(bc buildContext) (stages []Stage) {
Expand Down Expand Up @@ -432,10 +435,13 @@ func (sb stageBuilder) hasUnmeetableOutboundDeps(n *screl.Node) (ret bool) {
// Mark this node as having been visited in this traversal.
sb.visited[n] = sb.visitEpoch
// Do some sanity checks.
if _, isFulfilling := sb.bs.fulfilled[n]; isFulfilling {
// This should never happen.
panic(errors.AssertionFailedf("%s should not yet be scheduled for this stage",
screl.NodeString(n)))
if _, isFulfilled := sb.bs.fulfilled[n]; isFulfilled {
if sb.bc.enforcePlannerSanityChecks {
panic(errors.AssertionFailedf("%s should not yet be scheduled for this stage",
screl.NodeString(n)))
} else {
return false
}
}
// Look up the current target state for this node, via the lookup table.
if t := sb.lut[n.Target]; t == nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/sql/schemachanger/scplan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Params struct {
// SchemaChangerJobIDSupplier is used to return the JobID for a
// job if one should exist.
SchemaChangerJobIDSupplier func() jobspb.JobID

// enforcePlannerSanityCheck, if true, strictly enforces sanity checks in the
// declarative schema changer planner.
EnforcePlannerSanityCheck bool
}

// Exported internal types
Expand Down Expand Up @@ -105,9 +109,7 @@ func makePlan(ctx context.Context, p *Plan) (err error) {
}
{
start := timeutil.Now()
p.Stages = scstage.BuildStages(
ctx, p.CurrentState, p.Params.ExecutionPhase, p.Graph, p.Params.SchemaChangerJobIDSupplier,
)
p.Stages = scstage.BuildStages(ctx, p.CurrentState, p.Params.ExecutionPhase, p.Graph, p.Params.SchemaChangerJobIDSupplier, p.Params.EnforcePlannerSanityCheck)
if log.ExpensiveLogEnabled(ctx, 2) {
log.Infof(ctx, "stage generation took %v", timeutil.Since(start))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/schemachanger/scrun/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//pkg/jobs",
"//pkg/jobs/jobspb",
"//pkg/roachpb",
"//pkg/settings",
"//pkg/settings/cluster",
"//pkg/sql/catalog",
"//pkg/sql/catalog/descpb",
Expand All @@ -21,6 +22,7 @@ go_library(
"//pkg/sql/schemachanger/scop",
"//pkg/sql/schemachanger/scpb",
"//pkg/sql/schemachanger/scplan",
"//pkg/util/buildutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_redact//:redact",
],
Expand Down
10 changes: 10 additions & 0 deletions pkg/sql/schemachanger/scrun/scrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
Expand All @@ -25,10 +26,18 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scplan"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)

var enforcePlannerSanityCheck = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.schemachanger.strict_planning_sanity_check.enabled",
"enforce strict sanity checks in the declarative schema changer planner",
buildutil.CrdbTestBuild,
)

// RunStatementPhase executes in-transaction schema changes for the targeted
// state. These are the immediate changes which take place at DDL statement
// execution time (scop.StatementPhase).
Expand Down Expand Up @@ -67,6 +76,7 @@ func runTransactionPhase(
sc, err := scplan.MakePlan(ctx, state, scplan.Params{
ExecutionPhase: phase,
SchemaChangerJobIDSupplier: deps.TransactionalJobRegistry().SchemaChangerJobID,
EnforcePlannerSanityCheck: enforcePlannerSanityCheck.Get(&deps.ClusterSettings().SV),
})
if err != nil {
return scpb.CurrentState{}, jobspb.InvalidJobID, err
Expand Down

0 comments on commit c50f15f

Please sign in to comment.