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: Add created_by columns to system.jobs #49444

Merged
merged 1 commit into from
May 29, 2020

Conversation

miretskiy
Copy link
Contributor

@miretskiy miretskiy commented May 22, 2020

Informs #49346

Add created_by_type and created_by_id columns, along with the index over
these columns, to the system.jobs table.

Add sql migration code to migrate old definition to the new one.

See https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20200414_scheduled_jobs.md

Release Notes: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@pbardea pbardea left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt, @lucy-zhang, @miretskiy, and @pbardea)


pkg/clusterversion/cockroach_versions.go, line 514 at r1 (raw file):

	// that wouldn't work since you'd have to go through the final 19.2 binary
	// before going to HEAD.
	binaryMinSupportedVersion = VersionByKey(Version20_1)

I think the comment above needs updating.


pkg/sql/sqlbase/system.go, line 885 at r2 (raw file):

			{Name: "progress", ID: 5, Type: types.Bytes, Nullable: true},
		},
		NextColumnID: 8,

Would this still be 6 on the old jobs table?


pkg/sqlmigrations/migrations_test.go, line 833 at r2 (raw file):

	// Check that we don't have created_by columns.
	var count int
	require.Error(t, mt.sqlDB.DB.QueryRowContext(ctx, query).Scan(&count))

Do you think it's worthwhile checking that the error we get here is the one that we expect? Alternatively we could also do something like SELECT column_name FROM [SHOW COLUMNS from system.jobs];?

Do you think it's also worth testing that the indexes have been created?

Copy link
Contributor

@pbardea pbardea 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 @miretskiy)


pkg/clusterversion/cockroach_versions.go, line 514 at r1 (raw file):

Previously, pbardea (Paul Bardea) wrote…

I think the comment above needs updating.

Nevermind - I see this has been pulled out into another PR.

@miretskiy miretskiy requested a review from pbardea May 27, 2020 16:52
Copy link
Contributor Author

@miretskiy miretskiy 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 @pbardea)


pkg/sql/sqlbase/system.go, line 885 at r2 (raw file):

Previously, pbardea (Paul Bardea) wrote…

Would this still be 6 on the old jobs table?

YES! Good Catch


pkg/sqlmigrations/migrations_test.go, line 833 at r2 (raw file):

Previously, pbardea (Paul Bardea) wrote…

Do you think it's worthwhile checking that the error we get here is the one that we expect? Alternatively we could also do something like SELECT column_name FROM [SHOW COLUMNS from system.jobs];?

Do you think it's also worth testing that the indexes have been created?

Great idea. Done. I also changed index names a bit... Basically created_by_idx is too close
to created_by_id column... I decided to use default names for the index (long, but descriptive).

Finally, I had to explicitly specify familly name when adding new columns (in the migration.go) -- not sure why, but without it, alter columns added new columns to 2 new column families.

Do you know how to check which families exist on a table?

@miretskiy miretskiy force-pushed the mod_jobs_tbl branch 2 times, most recently from 7901c34 to fb5811d Compare May 27, 2020 16:54

// OldJobsTable is the descriptor for JobsTable prior to 20.2.
// Remove when migration code is baked in (after 22.1 release).
OldJobsTable = TableDescriptor{
Copy link
Member

Choose a reason for hiding this comment

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

we could just move this to the test since that is the only usage of it, right?

Copy link
Contributor

@pbardea pbardea 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 @lucy-zhang, @miretskiy, and @pbardea)


pkg/sqlmigrations/migrations_test.go, line 833 at r2 (raw file):

Previously, miretskiy (Yevgeniy Miretskiy) wrote…

Great idea. Done. I also changed index names a bit... Basically created_by_idx is too close
to created_by_id column... I decided to use default names for the index (long, but descriptive).

Finally, I had to explicitly specify familly name when adding new columns (in the migration.go) -- not sure why, but without it, alter columns added new columns to 2 new column families.

Do you know how to check which families exist on a table?

Hmm, the only way that comes to mind would be SHOW CREATE system.jobs - you then may be able to compare it against JobsTableSchema.

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.

Reviewed 1 of 1 files at r1, 3 of 3 files at r3.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @miretskiy and @pbardea)


pkg/sql/sqlbase/system.go, line 165 at r3 (raw file):

	payload           BYTES     NOT NULL,
	progress          BYTES,
  created_by_type   STRING,

nit: the columns should be aligned


pkg/sqlmigrations/migrations_test.go, line 829 at r3 (raw file):

	mt.start(t, base.TestServerArgs{})

	// We expect to create 2 new clolumns

nit: columns

Copy link
Contributor Author

@miretskiy miretskiy 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, @miretskiy, and @pbardea)


pkg/sql/sqlbase/system.go, line 872 at r3 (raw file):

Previously, dt (David Taylor) wrote…

we could just move this to the test since that is the only usage of it, right?

Good idea. Done. actually, removed descriptor, and just used old schema.

@miretskiy miretskiy force-pushed the mod_jobs_tbl branch 3 times, most recently from 59e1ba7 to 9e465fd Compare May 27, 2020 18:52
@miretskiy miretskiy requested a review from thoszhang May 27, 2020 18:52
Copy link
Contributor Author

@miretskiy miretskiy 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, @miretskiy, and @pbardea)


pkg/sql/sqlbase/system.go, line 165 at r3 (raw file):

Previously, lucy-zhang (Lucy Zhang) wrote…

nit: the columns should be aligned

Strange.. These are aligned fined in my editor...


pkg/sql/sqlbase/system.go, line 872 at r3 (raw file):

Previously, miretskiy (Yevgeniy Miretskiy) wrote…

Good idea. Done. actually, removed descriptor, and just used old schema.

Done.

Copy link
Contributor Author

@miretskiy miretskiy 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 @pbardea)


pkg/sqlmigrations/migrations_test.go, line 833 at r2 (raw file):

Previously, pbardea (Paul Bardea) wrote…

Hmm, the only way that comes to mind would be SHOW CREATE system.jobs - you then may be able to compare it against JobsTableSchema.

it's alright.. I guess.

Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r4.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt, @lucy-zhang, and @pbardea)

Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r4.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt and @pbardea)

FAMILY (id, status, created, payload),
INDEX (created_by_type, created_by_id) STORING (status),

FAMILY "primary" (id, status, created, payload, created_by_type, created_by_id),
Copy link
Member

Choose a reason for hiding this comment

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

huh, I guess it is OK to change the name of the default family for new jobs tables but not migrate it for old ones? I donno, maybe a question for @lucy-zhang

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yeah, what is preventing us from using the existing name?

@miretskiy miretskiy requested a review from dt May 27, 2020 19:20
Copy link
Contributor Author

@miretskiy miretskiy 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, @miretskiy, and @pbardea)


pkg/sql/sqlbase/system.go, line 170 at r4 (raw file):

Previously, dt (David Taylor) wrote…

part of what worried me when I saw this is now for clusters where the jobs table was created by 20.1 and below, the family will have one name while those that are started at 20.2 and up will have another. I can't actually think of how this will cause a specific problem though, just a little bit of generalized unease -- usually the migration brings an old table up to be identical to a newly created one. Actually, now that I think a bit more I think one way this could bite us is if someone later, say in 21.1, wants to add a column to this family -- what name do they use to specify it? if they use "primary" but that cluster was originally created on 19.1 it will fail, while if they use the family_0_blah_blah name, it'll fail if it was created on 20.2. I think we should either use the current name or include a rename (if that is even possible) in the migration.

Very good point. I've modified migration script and tests to ensure we rename column family. PTAL.

Copy link
Contributor Author

@miretskiy miretskiy 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, @miretskiy, and @pbardea)


pkg/sqlmigrations/migrations_test.go, line 833 at r2 (raw file):

Previously, miretskiy (Yevgeniy Miretskiy) wrote…

it's alright.. I guess.

Done.

@miretskiy miretskiy force-pushed the mod_jobs_tbl branch 2 times, most recently from 83625bf to e0fc418 Compare May 28, 2020 00:56
pkg/sqlmigrations/migrations.go Outdated Show resolved Hide resolved
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.

LGTM except for the naming bikeshed (the best kind really) over name of the primary family and happy to drop that concern if others are fine with it.

@miretskiy miretskiy requested review from dt and jordanlewis May 28, 2020 12:42
Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

I really don't care about the name of the primary family -- just used what we had used in few other system tables.
Do you have other suggestions? Be happy to change it.

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


pkg/sqlmigrations/migrations.go, line 1861 at r5 (raw file):

Previously, dt (David Taylor) wrote…

I'll happily defer to the opinions of the sql and sqlmigration experts of @lucy-zhang (and @jordanlewis), and I know I originally mentioned that maybe it'd be possible to resolve my concern by renaming, but seeing this actually worked out, personally, this feels like a lot more migration code and associated complexity/risk for the benefit of just a shorter name. ~nobody has to see / type this name except when adding columns to this table so I don't know if it is worth all this?

Yeah -- it's a bit more code, primarily because I was being extra paranoid
(comparing tableID version, family id, etc). The code itself isn't that terrible -- primarily lifted form the existing examples in this file.

I do see your point, however.

Perhaps a better way is to explicitly name primary family in the updated JobsTable using the
old name. Add a comment -- and just be done with it.

Let me know what you think ( @lucy-zhang , @jordanlewis , @dt ) -- i'd be happy to drop this rename (but I will likely keep the changes to the test -- I kinda like latest version of the test).

@jordanlewis
Copy link
Member

Thanks for the ping. Just for the sake of future-us sanity I would prefer that you not do this rename. Things like this, in my experience, are ticking time bombs - they might feel fine now, but unless we rigorously, rigorously test the migration path (including upgrades, downgrades, weird cases of which there are many), we won't feel completely confident in this and if something goes wrong in a 20.1->20.2 upgrade, this small change will now be added to the list of scary things that we need to worry about given a failure.

To be clear, this might be a totally sound change and I would believe you if you said it works. But I think we've learned our lesson that, just like in KV, we should strongly prefer to avoid unnecessary migrations because of the potential headaches they might incur down the line.

Copy link
Contributor Author

@miretskiy miretskiy left a comment

Choose a reason for hiding this comment

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

Reverted; system tables updated to use "old" family name; comments added. PTAL.

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


pkg/sql/sqlbase/system.go, line 170 at r4 (raw file):

Previously, miretskiy (Yevgeniy Miretskiy) wrote…

Very good point. I've modified migration script and tests to ensure we rename column family. PTAL.

Done.


pkg/sqlmigrations/migrations.go, line 1861 at r5 (raw file):

Previously, miretskiy (Yevgeniy Miretskiy) wrote…

Yeah -- it's a bit more code, primarily because I was being extra paranoid
(comparing tableID version, family id, etc). The code itself isn't that terrible -- primarily lifted form the existing examples in this file.

I do see your point, however.

Perhaps a better way is to explicitly name primary family in the updated JobsTable using the
old name. Add a comment -- and just be done with it.

Let me know what you think ( @lucy-zhang , @jordanlewis , @dt ) -- i'd be happy to drop this rename (but I will likely keep the changes to the test -- I kinda like latest version of the test).

Done.

@miretskiy miretskiy force-pushed the mod_jobs_tbl branch 4 times, most recently from 2307474 to ed70c7c Compare May 28, 2020 21:42
// Introduced in v20.2.
name: "add created_by columns to system.jobs",
workFn: alterSystemJobsAddCreatedByColumns,
includedInBootstrap: clusterversion.VersionByKey(clusterversion.VersionStart20_2),
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how this works but do we need to mint a new version to put here to ensure existing data dirs / clusters from master binaries prior to this patch, but from after VersionStart20_2, i.e. the last couple months, will still run the migration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should. Added.

@dt
Copy link
Member

dt commented May 29, 2020

one more nit: I'd make the title "sqlbase: add created_by columns to system.jobs" or something since just "modify system.jobs" doesn't really summarize the purpose as much.

-- Done

@miretskiy miretskiy force-pushed the mod_jobs_tbl branch 2 times, most recently from 6768c3e to bae28f6 Compare May 29, 2020 14:34
@miretskiy miretskiy changed the title sqlbase/jobs: Modify system.jobs Table Definition sqlbase: Add created_by columns to system.jobs May 29, 2020
Add created_by_type and created_by_id columns, along with the index over
these columns, to the system.jobs table.

Add sql migration code to migrate old definition to the new one.

See https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20200414_scheduled_jobs.md

Release Notes: None
@miretskiy
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented May 29, 2020

Build succeeded

@craig craig bot merged commit c52e94c into cockroachdb:master May 29, 2020
@miretskiy miretskiy deleted the mod_jobs_tbl branch July 12, 2020 13:29
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.

6 participants