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

sqlbase,cluster: add new FK representation, upgrade/downgrade methods, and a new cluster version #39173

Merged
merged 4 commits into from
Aug 3, 2019

Conversation

jordanlewis
Copy link
Member

@jordanlewis jordanlewis commented Jul 30, 2019

This PR adds the new FK representation to the descriptor protobufs, adds a new cluster version for the FK representation upgrade and introduces methods that upgrade and downgrade descriptors between the two representations.

See individual commits for details.

@jordanlewis jordanlewis requested review from thoszhang and a team July 30, 2019 16:18
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@jordanlewis jordanlewis changed the title cluster: new version for the FK repr upgrade sqlbase,cluster: add new FK representation, upgrade/downgrade methods, and a new cluster version Jul 30, 2019
@jordanlewis
Copy link
Member Author

Note: I am going to add unit tests for upgrade/downgrade idempotency next.

Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis and @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 856 at r4 (raw file):

		}

		// XXX: Lucy, don't we want to *avoid* changing other table descriptors here?

Unless I'm missing something here, this doesn't change the other table descriptors; it puts the FK we just created on the appropriate index on the same table descriptor.


pkg/sql/sqlbase/structured.proto, line 49 at r4 (raw file):

  }

Extra newline?

@jordanlewis jordanlewis force-pushed the fk-upgrade-version branch 2 times, most recently from 8ac146a to e5cbe72 Compare August 1, 2019 01:29
Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

I added tests.

One question - @lucy-zhang do you think we should nix the new OriginIndex and ReferencedIndex fields now that we have the UpgradedFromOriginReference and UpgradedFromReferencedReference fields? :)

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 856 at r4 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Unless I'm missing something here, this doesn't change the other table descriptors; it puts the FK we just created on the appropriate index on the same table descriptor.

Resolved offline.


pkg/sql/sqlbase/structured.proto, line 49 at r4 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Extra newline?

Done.

Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

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

Would we just get the index IDs from the other struct? That seems fine.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis and @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 265 at r8 (raw file):

// ProtoGetter is a sub-interface of client.Txn that can fetch protobufs in a
// transaction.
type ProtoGetter interface {

Are there plans to start using this interface elsewhere, besides what we need to test right now? I think something like this is necessary but I'm concerned about introducing disorder if we don't use it in a principled way everywhere.


pkg/sql/sqlbase/structured.go, line 856 at r8 (raw file):

		return false, desc, nil
	}
	// TODO (lucy): add check that all fields are actually populated

I'm not sure we actually need this now. If we get into a situation where this FK is not populated (and I can't think of a plausible bug/mistake that would cause this), the later validation will (indirectly) catch it. Maybe I'm forgetting why we originally wanted to do this.


pkg/sql/sqlbase/structured_test.go, line 1369 at r8 (raw file):

// This test exercises the foreign key representation upgrade and downgrade
// methods that were introduced in 19.2 to move foriegn key descriptors from

foreign


pkg/sql/sqlbase/structured_test.go, line 1424 at r8 (raw file):

							OriginColumnIDs:     ColumnIDs{1},
							ReferencedColumnIDs: ColumnIDs{2},
							ReferencedTableID:   2,

nit: is there a reason to have the fields in this order? I would have expected ReferencedColumnIDs and ReferencedTableID to go in the opposite order.


pkg/sql/sqlbase/structured_test.go, line 1710 at r8 (raw file):

				// The upgraded proto will also have a copy of the old foreign key
				// reference attached for each foreign key. Delete that for the purposes of
				// verifying equality.

Is there any downside to just keeping these extra fields (aside from the fact that it makes the test structs even more annoying and huge)?

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

This stuff seems quite tricky :)

Reviewed 1 of 1 files at r1, 1 of 1 files at r2, 2 of 2 files at r3, 1 of 1 files at r4, 5 of 5 files at r5, 2 of 2 files at r6, 1 of 1 files at r7, 2 of 2 files at r8.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @jordanlewis and @lucy-zhang)


pkg/sql/sqlbase/structured.go, line 812 at r3 (raw file):

// maybeDowngradeForeignKeyRepresentation non-destructively downgrades the
// receiver into the old foreign key representation (the ForeignKey
// and ReferencedBy fields on IndexDescriptor if and only if the cluster version

[nit]: missing closing parenthesis.

The new descriptor format for FKs lives on TableDescriptor, not
IndexDescriptor, and doesn't pin the foreign keys to any particular
index on the origin or referenced tables. This paves the way toward
lifting the constraint that all outgoing foreign keys must have an
origin index, and that foreign keys may not overlap on their reference
columns.

Release note: None
Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

Would we just get the index IDs from the other struct? That seems fine.

Nevermind, this is not possible because the reference doesn't encode both origin and referenced index id. I left that alone for simplicity's sake.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @lucy-zhang and @yuzefovich)


pkg/sql/sqlbase/structured.go, line 812 at r3 (raw file):

Previously, yuzefovich wrote…

[nit]: missing closing parenthesis.

Done.


pkg/sql/sqlbase/structured.go, line 265 at r8 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Are there plans to start using this interface elsewhere, besides what we need to test right now? I think something like this is necessary but I'm concerned about introducing disorder if we don't use it in a principled way everywhere.

No, I don't have any plans to do that. I agree that this is sketchy. I asked about it in #core and nobody had anything particularly good to say, so unless you really object, I'll leave this.


pkg/sql/sqlbase/structured.go, line 856 at r8 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

I'm not sure we actually need this now. If we get into a situation where this FK is not populated (and I can't think of a plausible bug/mistake that would cause this), the later validation will (indirectly) catch it. Maybe I'm forgetting why we originally wanted to do this.

Done. I agree that it didn't seem necessary.


pkg/sql/sqlbase/structured_test.go, line 1369 at r8 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

foreign

Done.


pkg/sql/sqlbase/structured_test.go, line 1424 at r8 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

nit: is there a reason to have the fields in this order? I would have expected ReferencedColumnIDs and ReferencedTableID to go in the opposite order.

Nope, just happenstance / carelessness - fixed everywhere.


pkg/sql/sqlbase/structured_test.go, line 1710 at r8 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Is there any downside to just keeping these extra fields (aside from the fact that it makes the test structs even more annoying and huge)?

Just the latter. It doesn't seem particularly important to me - I think messing this up would mess up the verification too.

Copy link
Member

@dt dt left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt, @jordanlewis, @lucy-zhang, and @yuzefovich)


pkg/sql/sqlbase/structured.go, line 265 at r8 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

No, I don't have any plans to do that. I agree that this is sketchy. I asked about it in #core and nobody had anything particularly good to say, so unless you really object, I'll leave this.

any reason for it to be exported?


pkg/sql/sqlbase/structured.proto, line 92 at r12 (raw file):

  // is in a mixed-version state containing VersionTopLevelForeignKeys) was done
  // without creating any accidental changes to the foreign key references.
  optional uint32 origin_index = 10 [(gogoproto.nullable) = false, (gogoproto.casttype) = "IndexID", deprecated = true];

I might name these legacy_ since otherwise OriginIndex looks like a perfect valid thing to use. AFAIK the deprecated annotation does nothing, right?

Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt, @lucy-zhang, and @yuzefovich)


pkg/sql/sqlbase/structured.go, line 265 at r8 (raw file):

Previously, dt (David Taylor) wrote…

any reason for it to be exported?

Nope. Done.


pkg/sql/sqlbase/structured.proto, line 92 at r12 (raw file):

Previously, dt (David Taylor) wrote…

I might name these legacy_ since otherwise OriginIndex looks like a perfect valid thing to use. AFAIK the deprecated annotation does nothing, right?

The deprecated annotation makes the field crossed out in my editor.

Copy link
Contributor

@thoszhang thoszhang left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt, @jordanlewis, @lucy-zhang, and @yuzefovich)


pkg/sql/sqlbase/structured.go, line 265 at r8 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

Nope. Done.

It's fine with me.


pkg/sql/sqlbase/structured.proto, line 92 at r12 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

The deprecated annotation makes the field crossed out in my editor.

Yeah, I think prefixing with legacy_ couldn't hurt.


pkg/sql/sqlbase/structured_test.go, line 1710 at r8 (raw file):

Previously, jordanlewis (Jordan Lewis) wrote…

Just the latter. It doesn't seem particularly important to me - I think messing this up would mess up the verification too.

Seems fine.

This commit adds two methods that upgrade and downgrade TableDescriptors
to and from the new, top-level FK representation.

Release note: None
Copy link
Member Author

@jordanlewis jordanlewis left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt, @jordanlewis, @lucy-zhang, and @yuzefovich)


pkg/sql/sqlbase/structured.proto, line 92 at r12 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

Yeah, I think prefixing with legacy_ couldn't hurt.

Done.

@jordanlewis
Copy link
Member Author

TFTRs!

bors r+

craig bot pushed a commit that referenced this pull request Aug 3, 2019
39173: sqlbase,cluster: add new FK representation, upgrade/downgrade methods, and a new cluster version r=jordanlewis a=jordanlewis

This PR adds the new FK representation to the descriptor protobufs, adds a new cluster version for the FK representation upgrade and introduces methods that upgrade and downgrade descriptors between the two representations.

See individual commits for details.

Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
@craig
Copy link
Contributor

craig bot commented Aug 3, 2019

Build succeeded

@craig craig bot merged commit e366076 into cockroachdb:master Aug 3, 2019
@jordanlewis jordanlewis deleted the fk-upgrade-version branch August 6, 2019 16:21
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.

5 participants