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

sql: fix incorrect follower read and testing knob for sql stats compaction job #69346

Merged
merged 1 commit into from
Aug 26, 2021

Conversation

Azhng
Copy link
Contributor

@Azhng Azhng commented Aug 25, 2021

Previously, the SQL Stats Compaction job did not check for nil testing
knobs and constructed its SQL query with incorrect follower read syntax.
This commit address those two bugs.

Release justification: Category 2: Bug fixes and low-risk updates
to new functionality

Release note: None

@Azhng Azhng requested a review from a team August 25, 2021 01:25
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@maryliag maryliag 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 @Azhng)


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

SELECT count(*)
FROM %[1]s
%[3]s

why it was necessary to change to order of this clause?

Copy link
Contributor

@matthewtodd matthewtodd 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 @Azhng)


-- commits, line 5 at r1:
nit: spelling / grammar: "Previously, the SQL Stats Compaction job did not check for nil testing knobs and constructed its SQL query with incorrect follower read syntax."


-- commits, line 11 at r1:
über-nit: leading space


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

SELECT count(*)
FROM %[1]s
%[3]s

Is it possible to write a test for this fix?


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

SELECT count(*)
FROM %[1]s
%[3]s

And would you want to reorder the placeholder indexes now to match their positions?

SELECT count(*)
FROM %[1]s
%[2]s
WHERE %[3]s = $1

pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 204 at r1 (raw file):

	followerReadClause := "AS OF SYSTEM TIME follower_read_timestamp()"

	if c.knobs != nil && c.knobs.DisableFollowerRead {

Is it possible to write a test for this fix? Or, better yet, is there a way to make these knobs never nil in the first place?

Copy link
Contributor

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Azhng)

Copy link
Contributor Author

@Azhng Azhng 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 @maryliag and @matthewtodd)


-- commits, line 5 at r1:

Previously, matthewtodd (Matthew Todd) wrote…

nit: spelling / grammar: "Previously, the SQL Stats Compaction job did not check for nil testing knobs and constructed its SQL query with incorrect follower read syntax."

Done.


-- commits, line 11 at r1:

Previously, matthewtodd (Matthew Todd) wrote…

über-nit: leading space

Done.


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

why it was necessary to change to order of this clause?

This is because the ASOT clause was placed in a wrong place.

Originally, it generates queries like:

SELECT count(*)
FROM system.statement_statistics
WHERE ...
AS OF SYSTEM TIME follower_read_timestamp()

but the correct syntax should be

SELECT count(*)
FROM system.statement_statistics
AS OF SYSTEM TIME follower_read_timestamp()
WHERE ...

We disabled follower read in test because this would cause historical reads into a point where system tables are not even created yet. This is a scenario where it doesn't happen outside of testing.


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Is it possible to write a test for this fix?

Would be difficult, follower read is difficult for us to test in the SQL land. The only hack would be time.Sleep() and wait for the follower read timestamp to be higher than the point where system tables are created. But we really want to avoid doing those in test.


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

And would you want to reorder the placeholder indexes now to match their positions?

SELECT count(*)
FROM %[1]s
%[2]s
WHERE %[3]s = $1

Done.


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 204 at r1 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Is it possible to write a test for this fix? Or, better yet, is there a way to make these knobs never nil in the first place?

Hmm, since this is only used for test, I hope we can avoid the extra allocation unless it's necessary.

Copy link
Contributor

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Azhng, @maryliag, and @matthewtodd)


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, Azhng (Archer Zhang) wrote…

Would be difficult, follower read is difficult for us to test in the SQL land. The only hack would be time.Sleep() and wait for the follower read timestamp to be higher than the point where system tables are created. But we really want to avoid doing those in test.

Understood. It's a shame we can't protect ourselves from bad SQL. Like maybe we'd want some infrastructure to run the query through the parser or something. But non-blocking for now.


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 204 at r1 (raw file):

Previously, Azhng (Archer Zhang) wrote…

Hmm, since this is only used for test, I hope we can avoid the extra allocation unless it's necessary.

Fair enough. :-) Thoughts on a test somehow?

Copy link
Contributor Author

@Azhng Azhng 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 @maryliag and @matthewtodd)


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 204 at r1 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Fair enough. :-) Thoughts on a test somehow?

Done.

Copy link
Contributor

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

:lgtm:

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

Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r2, 1 of 1 files at r3, 1 of 1 files at r4, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @Azhng)

@Azhng
Copy link
Contributor Author

Azhng commented Aug 26, 2021

TFTR!

bors r=maryliag,mathewtodd

Copy link
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.

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


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Understood. It's a shame we can't protect ourselves from bad SQL. Like maybe we'd want some infrastructure to run the query through the parser or something. But non-blocking for now.

You could pass a different expression in the follower read clause like '-1us' and then sleep for 1 microsecond. I prefer that to no testing.

Copy link
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.

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


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, ajwerner wrote…

You could pass a different expression in the follower read clause like '-1us' and then sleep for 1 microsecond. I prefer that to no testing.

In fact, maybe that's the answer here. Maybe we get rid of the knob to disable follower reads and replace it with a knob to set up the fixed timestamp to '-1us'. I don't think you need to sleep 1us.

@Azhng
Copy link
Contributor Author

Azhng commented Aug 26, 2021

bors r-

@craig
Copy link
Contributor

craig bot commented Aug 26, 2021

Canceled.

Copy link
Contributor Author

@Azhng Azhng 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 (and 2 stale) (waiting on @maryliag)


pkg/sql/sqlstats/persistedsqlstats/compaction_exec.go, line 199 at r1 (raw file):

Previously, ajwerner wrote…

In fact, maybe that's the answer here. Maybe we get rid of the knob to disable follower reads and replace it with a knob to set up the fixed timestamp to '-1us'. I don't think you need to sleep 1us.

Done.

Copy link
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.

:lgtm: 🚢 it

Reviewed 2 of 6 files at r5, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 2 stale) (waiting on @Azhng and @maryliag)


pkg/sql/sqlstats/persistedsqlstats/reader_test.go, line 57 at r5 (raw file):

	// Wait for the system table to be created by ASOT timestamp.
	time.Sleep(time.Microsecond)

Do you actually need these? I have a feeling the answer is no.

Copy link
Contributor

@matthewtodd matthewtodd 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! 1 of 0 LGTMs obtained (and 2 stale) (waiting on @Azhng and @maryliag)


pkg/sql/sqlstats/persistedsqlstats/test_utils.go, line 27 at r5 (raw file):

	// ASOTClause overrides the AS OF SYSTEM TIME clause in queries used in
	// persistedsqlstats.
	ASOTClause string

nit: Do you want this to be AOSTClause instead?

@ajwerner
Copy link
Contributor

I don't know what it is about the acronym AOST but it has been written accidentally as ASOT a lot, including by myself. My brain, for whatever reason, doesn't see the transposition. A simple slack search shows ASOT having been written 21 times by 7 different people 🤯

@matthewtodd
Copy link
Contributor

Amazing. 🤯

Copy link
Contributor Author

@Azhng Azhng left a comment

Choose a reason for hiding this comment

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

Human brain is interesting 🤯

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 3 stale) (waiting on @ajwerner and @maryliag)


pkg/sql/sqlstats/persistedsqlstats/reader_test.go, line 57 at r5 (raw file):

Previously, ajwerner wrote…
	// Wait for the system table to be created by ASOT timestamp.
	time.Sleep(time.Microsecond)

Do you actually need these? I have a feeling the answer is no.

Removed.


pkg/sql/sqlstats/persistedsqlstats/test_utils.go, line 27 at r5 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

nit: Do you want this to be AOSTClause instead?

Done.

@Azhng Azhng requested a review from a team as a code owner August 26, 2021 18:10
@Azhng Azhng removed the request for review from a team August 26, 2021 19:13
compaction job

Previously, the SQL Stats Compaction job did not check for nil
testing knobs and constructed its SQL query with
incorrect follower read syntax
This commit address those two bugs.

Release justification: Category 2: Bug fixes and low-risk updates
to new functionality

Release note: None
@Azhng
Copy link
Contributor Author

Azhng commented Aug 26, 2021

TFTR!

bors r=ajwerner,maryliag,matthewtodd

@craig
Copy link
Contributor

craig bot commented Aug 26, 2021

Build succeeded:

@craig craig bot merged commit 7d94aa8 into cockroachdb:master Aug 26, 2021
craig bot pushed a commit that referenced this pull request Aug 27, 2021
68401: sql: create sql stats compaction scheduled job r=miretskiy,maryliag a=Azhng

Depends on #68434, #69346

sql: create sql stats compaction scheduled job

This commit introduces SQL Stats Compaction Scheduled job, where it runs
in an hourly schedule and invokes the SQL Stats Compaction Job that was
created in the previous commit.
This commit also introduces `crdb_internal.schedule_sql_stats_compaction()`
builtin as a way to manually create SQL Stats compaction schedule.
`SHOW SCHEDULES` command'syntax is also extended to support
`SHOW SCHEDUELS FOR SQL STATISTICS`.

Release justification: category 4

Release note (sql change): introduce
crdb_internal.schedule_sql_stats_compaction() to manually create SQL Stats
compaction schedule. Extend SHOW SCHEDULES command to support
SHOW SCHEDULES FOR SQL STATISTICS.

Co-authored-by: Azhng <archer.xn@gmail.com>
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