Skip to content

scplan: break out stage building into scstage package and invert rule directions#73674

Merged
craig[bot] merged 6 commits intocockroachdb:masterfrom
postamar:scstage
Dec 15, 2021
Merged

scplan: break out stage building into scstage package and invert rule directions#73674
craig[bot] merged 6 commits intocockroachdb:masterfrom
postamar:scstage

Conversation

@postamar
Copy link
Copy Markdown

These 4 commits introduce a new scstage package. This was motivated by the more immediate goal of inverting dependency edge directions, to express them as precedence constraints instead of succession constraints.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@postamar
Copy link
Copy Markdown
Author

postamar commented Dec 10, 2021

This is best reviewed commit by commit. The first two are strict refactors, the third is tiny, and the fourth is dominated by test artifact noise. Ultimately, this PR doesn't really change anything substantial.

Next steps here would be to explore precedence rules with strict precedence rules, which force the nodes to be in different stages. I'm feeling uneasy about relying on the topological sort to ensure that operations are performed in the correct order, I'd rather generate a bunch of stages and collapse them together afterwards. Anyway, plenty of fertile ground to explore, as always.

edit: I've since pushed more commits to this branch, because the strict precedence change was much less disruptive than I feared. In the process I found a couple more bugs to fix, and I added extra validation to check that each stage is internally consistent and so that I can trust what I see when look at the output of EXPLAIN (DDL) .... What I wrote in the first paragraph still holds.

@postamar postamar marked this pull request as ready for review December 10, 2021 15:00
@postamar postamar requested a review from a team December 10, 2021 15:01
@postamar postamar force-pushed the scstage branch 3 times, most recently from 4acae86 to 1d57d08 Compare December 11, 2021 14:54
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is Good. One concern about a behavior change. Otherwise, :lgtm:

Reviewed 9 of 9 files at r1, 1 of 1 files at r2, 3 of 3 files at r3, 13 of 14 files at r4, 1 of 1 files at r5, 1 of 1 files at r6, 3 of 3 files at r7, 17 of 17 files at r8.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner and @postamar)


pkg/sql/schemachanger/scplan/opgen/op_gen.go, line 46 at r6 (raw file):

		edgesToAdd = edgesToAdd[:0]
		if err := t.iterateFunc(g.Database(), func(n *scpb.Node) error {
			status := n.Status

I recall this having something to do with edge cases involving nodes which were referenced by dependencies. I think this was vestigial. Thanks for cleaning it up. Cleanup passes from me around some of this code are overdue.


pkg/sql/schemachanger/scplan/scstage/stage.go, line 27 at r1 (raw file):

// the stage, reflecting the fact that any set of ops can be thought of as a
// transition from one state to another.
type Stage struct {

I could see moving this to scgraph. Ultimately we'll want the visualization stuff to have awareness of this.


pkg/sql/schemachanger/scplan/scstage/stage.go, line 114 at r9 (raw file):

	revertibleAllowed := true
	for _, stage := range stages {

Can we add a check that there is at most one PreCommit stage?


pkg/sql/schemachanger/testdata/alter_table_add_column, line 238 at r8 (raw file):

Quoted 17 lines of code…
update progress of schema change job #1
upsert descriptor #56
  ...
         version: 4
       mutationId: 2
  -    state: DELETE_AND_WRITE_ONLY
  +    state: DELETE_ONLY
     name: tbl
     newSchemaChangeJobId: "1"
  ...
       time: {}
     unexposedParentSchemaId: 55
  -  version: "4"
  +  version: "5"
commit transaction #8
begin transaction #9
## PostCommitPhase non-revertible stage 8 of 8 with 3 MutationType ops

why did this stage go away? It seems important

Copy link
Copy Markdown
Author

@postamar postamar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I'll salvage as much as I can from this PR and rework the strict precedence + collapse.

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner)


pkg/sql/schemachanger/scplan/scstage/stage.go, line 27 at r1 (raw file):

Previously, ajwerner wrote…

I could see moving this to scgraph. Ultimately we'll want the visualization stuff to have awareness of this.

I agree in the longer term.


pkg/sql/schemachanger/scplan/scstage/stage.go, line 114 at r9 (raw file):

Previously, ajwerner wrote…

Can we add a check that there is at most one PreCommit stage?

Yes! Let's.


pkg/sql/schemachanger/testdata/alter_table_add_column, line 238 at r8 (raw file):

Previously, ajwerner wrote…
update progress of schema change job #1
upsert descriptor #56
  ...
         version: 4
       mutationId: 2
  -    state: DELETE_AND_WRITE_ONLY
  +    state: DELETE_ONLY
     name: tbl
     newSchemaChangeJobId: "1"
  ...
       time: {}
     unexposedParentSchemaId: 55
  -  version: "4"
  +  version: "5"
commit transaction #8
begin transaction #9
## PostCommitPhase non-revertible stage 8 of 8 with 3 MutationType ops

why did this stage go away? It seems important

Oh, good catch. My changes basically violate the two version invariant, which is bad!

What's happening is that a bunch of stages got collapsed together when they shouldn't have. It seems that we were relying on the fact that each state transition was a separate edge and therefore (pre-this-PR at least) a separate stage.

In retrospect this strict-precedence change needs some adjusting. I'm not such a huge fan of violating it after collapsing the stages. In fact, knowing what I know now, I'd feel easier if the two-version invariant were enforced by the rules engine somehow.
I'm thinking now that perhaps it might be best to separate precedence constraints from same-stage/different-stage constraints somehow. I'll go back to the drawing board.

Also this violation should have been caught by the stage validation checks.

Marius Posta added 6 commits December 14, 2021 14:36
This commit is strictly a refactor which moves all stage planning logic
from the declarative schema changer into its own package, with no change
to the structure of the code or the logic in the function bodies.

Release note: None
This commit breaks down the logic that was previously inside the
BuildStages function's body into separate functions. Mainly, the closures
have been made into methods on a buildCtx struct which basically is
the closure's captured variables made explicit. This commit is strictly
a refactor.

Release note: None
This change was motivated by the upcoming need to introduce depEdgesTo
to complement the existing depEdgesFrom field in the graph struct.

Release note: None
This commit redefines the declarative schema changer's dependency rules
as precedence rules instead of succession rules. This has been found to
be more intuitive to reason about. It also makes for better graphviz
representations.

Release note: None
The existing graph builder logic had a bug where too many op edges were
added to the graph. This caused additional source nodes to appear which
were not in the initial state. This bug was probably harmless but made
for some confusing dependency graph renderings.

Release note: None
This commit fixes a few things in the declarative schema changer's
visualization package:
- Same-state precedence dep edges have diamond arrow heads to
  distinguish them from regular precedence edges.
- A bug was causing some ops to not be visible in the stage rendering.
- Target counters are left-padded with zeros because graphviz orders the
  graph nodes by lexicographical order. They now appear in the same
  order as in the schema changer state.
- The schema changer job ops are now visible in the stage rendering.
  They were previously not rendered, which made the ops count in the
  stage label inconsistent.

Release note: None
@postamar
Copy link
Copy Markdown
Author

I force-pushed this branch to keep only the safe and uncontroversial commits at the beginning of it, which I will bors as soon as CI gives a green light. The removed commits are still being reworked.

@postamar
Copy link
Copy Markdown
Author

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Dec 15, 2021

Build succeeded:

@craig craig bot merged commit 5e70321 into cockroachdb:master Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants