Skip to content

vtctl support for vschema ddls#4385

Merged
sougou merged 10 commits intovitessio:masterfrom
tinyspeck:vtctl-support-vschema-ddls
Dec 6, 2018
Merged

vtctl support for vschema ddls#4385
sougou merged 10 commits intovitessio:masterfrom
tinyspeck:vtctl-support-vschema-ddls

Conversation

@demmer
Copy link
Copy Markdown
Member

@demmer demmer commented Nov 25, 2018

Description

Add support for vschema DDL statements to be executed as vtctl commands.

Motivation

Making vschema changes through the DDL interface is safer and more granular than hand-editing the vschema json file. As such it is less likely that operators run into problems like #4288.

However, there are cases in which it is useful for these statements to be executed at vtctl / vtctld, so it would be beneficial to have the DDL syntax supported both at vtgate and as a vtctl command.

Details

The crux of the change adds new options to the ApplyVschema command to take a sql statement instead of the json specification, e.g.:

# vtctl ApplyVSchema -sql 'alter table test add vindex hash(id)' test_keyspace`

To implement this support, refactor the vschema ddl operations out of the vtgate executor and into a new topotools utility function to share it with vtctl.

Also add a -dry_run option to the ApplyVschema command to be able to see what the change will do to the vschema without applying it.

Finally, add a feature to set the Sharded: true flag on the vschema the first time a vindex is added to address a bootstrapping problem for new keyspaces. At some point we will want vschema ddl support for unsharded keyspaces, but that would necessarily involve a different syntax so we can cross that bridge later.

@demmer demmer requested a review from sougou as a code owner November 25, 2018 03:27
Copy link
Copy Markdown
Member

@rafael rafael left a comment

Choose a reason for hiding this comment

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

This looks good to me. Added couple comments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was thinking that it would make sense this to be driven by a flag. That way we can be explicit with the intent. If the flag is not provided and the Vindexes len is 0, adding a vindex should fail.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

By definition, vindexes are only present in Sharded keyspaces, so I think the act of creating a vindex expresses the intent of whether we want

I have ideas about how we can extend the vschema ddl syntax to support unsharded keyspaces, but even then we won't have vindexes and I think above will hold.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think a nice feature here is to delete the vindex altogether if no other tables are using it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it's better to add an explicit syntax for dropping the vindex rather than implicit by dropping the last reference

Copy link
Copy Markdown
Collaborator

@brirams brirams left a comment

Choose a reason for hiding this comment

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

this all seems reasonable to me but I don't know anything yet so take my review with a grain of salt.

provided some nits for consideration but no showstoppers

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

there seems to be a convention for a similarly named option(sql-file) in other spots. should we maintain that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was trying to be consistent with vschema_file but you're right we have this inconsistency unfortunately:

[SFO-M-MDEMMER03] -> vtctl --help | grep -- -file
  -db-credentials-file string
  -file_backup_storage_root string
  ApplySchema [-allow_long_unavailability] [-wait_slave_timeout=10s] {-sql=<sql> || -sql-file=<filename>} <keyspace>

[SFO-M-MDEMMER03] -> vtctl --help | grep -- _file
  -consul_auth_static_file string
  -grpc_auth_static_password_file string
  -mysql_auth_server_static_file string
  -pid_file string
  ApplyVSchema {-vschema=<vschema> || -vschema_file=<vschema file> || -sql=<sql> || -sql_file=<sql file>} [-cells=c1,c2,...] [-skip_rebuild] <keyspace>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah good point. This one I will change.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

do you need to add these options to the commands slice here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes I do. Thanks for pointing this out.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could you squash these two lines:

vs = &vschemapb.Keyspace{}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks. fixed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: rm : after parsing to match error format above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

TIOLI. big if/else blocks make my eyes cross:

	// apparently golang doesn't have an xor operator
	ddlMode := (*sql != "") != (*sqlFile != "")
	vschemaMode := (*vschema != "") != (*vschemaFile != "")

	switch {
	case ddlMode && vschemaMode:
		// both specified. return err
	case !ddlMode && !vschemaMode:
		// neither specified, return err
	}

	if ddlMode {

	}

	if vschemaMode {

	}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

good suggestion. changed the names a bit but this is a much more readable cleanup

@demmer demmer mentioned this pull request Dec 3, 2018
@demmer demmer force-pushed the vtctl-support-vschema-ddls branch from 03758b8 to 6f48f91 Compare December 3, 2018 20:39
demmer added 10 commits December 3, 2018 21:56
Pull in the topotools.RebuildVschema implementation into the vtgate
VschemaManager to share the same helper function that vtctl uses to
find all cells and propagate the changed vschema to all of them.

This changes the UpdateVschema interface to take in a single modified
keyspace and not the whole SrvVschema.

Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Abstract out the execution of the vschema ddl statements into a new
function in topotools.

Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Add new flags to ApplyVschema to be able to take a sql ddl statement
either on the command line or in a file to enable vschema changes
without having to edit the whole json file.

Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Make sure to set the Sharded bit to true when creating the first
vindex in a keyspace, otherwise the vschema ddl statements can't be
used to bootstrap a new sharded keyspace.

Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
The fact that the vschema ddl needs to go through the normal srvtopo
interface to get to the underlying topo server when applying vschema
ddl operations doesn't play well with the SandboxTopo used in some of
the unit tests.

So restore the previous implementation which edits the SrvVschema in
place and then reapplies it to all cells, rather than trying to reuse
topotools.RebuildVschema from the executor.

Signed-off-by: Michael Demmer <mdemmer@slack-corp.com>
@demmer demmer force-pushed the vtctl-support-vschema-ddls branch from 6f48f91 to e47bed0 Compare December 4, 2018 05:56
Copy link
Copy Markdown
Member

@rafael rafael left a comment

Choose a reason for hiding this comment

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

All comments have been addressed. This change looks good to me.

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.

4 participants