Skip to content

sql: crdb_internal.reset_sql_stats() now resets persisted SQL Stats #69273

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
Azhng:sqlstats-reset
Aug 28, 2021
Merged

sql: crdb_internal.reset_sql_stats() now resets persisted SQL Stats #69273
craig[bot] merged 1 commit intocockroachdb:masterfrom
Azhng:sqlstats-reset

Conversation

@Azhng
Copy link
Copy Markdown
Contributor

@Azhng Azhng commented Aug 24, 2021

Depends on #68401 and #68434

Previously, crdb_internal.reset_sql_stats() builtin only resets
cluster-wide in-memory sql stats.
This patch updated the builtin to be able to reset persisted
sql stats as well.

Release justification: category 4

Release note (sql change): crdb_internal.reset_sql_stats() now resets
persisted SQL Stats.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@Azhng Azhng force-pushed the sqlstats-reset branch 2 times, most recently from c7f73ed to 7a88fb5 Compare August 24, 2021 16:16
@Azhng
Copy link
Copy Markdown
Contributor Author

Azhng commented Aug 24, 2021

Reviewer note: only the last commit is the change.

@Azhng Azhng force-pushed the sqlstats-reset branch 3 times, most recently from e6a2dbf to 4751895 Compare August 25, 2021 02:21
@Azhng Azhng requested a review from a team August 25, 2021 02:21
@Azhng Azhng marked this pull request as ready for review August 25, 2021 02:21
@Azhng Azhng requested a review from a team August 25, 2021 02:21
@Azhng Azhng requested a review from a team as a code owner August 25, 2021 02:21
@Azhng Azhng requested review from a team and dt and removed request for a team August 25, 2021 02:21
Copy link
Copy Markdown

@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, 28 of 28 files at r2, 3 of 3 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Azhng and @dt)


pkg/sql/sqlstats/persistedsqlstats/compaction_scheduling.go, line 65 at r2 (raw file):

	compactionSchedule.SetOwner(security.NodeUserName())

	any, err := pbtypes.MarshalAny(&ScheduledSQLStatsCompactorExecutionArgs{})

nit: Maybe call this args or marshalledArgs or something?


pkg/sql/sqlstats/persistedsqlstats/compaction_scheduling.go, line 142 at r2 (raw file):

	if err != nil {
		return exists, err

Would it make sense to use a literal false for this exists value, and the ones below? I'm not sure what idiomatic style is, but this confused me a bit -- I read through the function looking for a line assigning to exists but didn't find it. (I get how it works now, you're relying on the default value of bools being false.)


pkg/sql/sqlstats/persistedsqlstats/compaction_scheduling.go, line 153 at r2 (raw file):

	}

	return tree.MustBeDInt(row[0]) == 1, nil /* err */

I suspect it would be safer / more defensive to say tree.MustBeDInt(row[0]) > 0 here.

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

btw @matthewtodd the PR for the compaction schedule is #68401. This PR rebase off #68401 and the only relevant change the last commit.

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


pkg/sql/sqlstats/persistedsqlstats/compaction_scheduling.go, line 142 at r2 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Would it make sense to use a literal false for this exists value, and the ones below? I'm not sure what idiomatic style is, but this confused me a bit -- I read through the function looking for a line assigning to exists but didn't find it. (I get how it works now, you're relying on the default value of bools being false.)

Done.


pkg/sql/sqlstats/persistedsqlstats/compaction_scheduling.go, line 153 at r2 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

I suspect it would be safer / more defensive to say tree.MustBeDInt(row[0]) > 0 here.

Done.

Copy link
Copy Markdown

@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.

Oops, sorry. I'm confused by Reviewable & stacked PRs.

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

@Azhng Azhng removed request for a team August 26, 2021 04:53
@Azhng Azhng force-pushed the sqlstats-reset branch 3 times, most recently from 6353d03 to fa0fca3 Compare August 26, 2021 18:12
@Azhng Azhng requested a review from a team as a code owner August 26, 2021 18:12
@Azhng Azhng removed request for a team and dt August 26, 2021 19:45
Copy link
Copy Markdown
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 and @matthewtodd)


pkg/sql/sqlstats/persistedsqlstats/controller.go, line 62 at r4 (raw file):

// ResetClusterSQLStats implements the tree.SQLStatsController interface.
func (s *Controller) ResetClusterSQLStats(ctx context.Context) error {
	if err := s.Controller.ResetClusterSQLStats(ctx); err != nil {

is this part handling the in-memory and the remaining the disk? Can you add some comments?
I want to be clear that the reset is clearing both in-memory and on disk

Copy link
Copy Markdown
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/controller.go, line 62 at r4 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

is this part handling the in-memory and the remaining the disk? Can you add some comments?
I want to be clear that the reset is clearing both in-memory and on disk

Added comment.


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 42 at r4 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

nit: remove extra space

Done.


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 60 at r4 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

this one is suppose to reset only disk stats? If this one is also resetting in memory, can you create a test that runs a few querys -> flush -> run a few more -> reset

Done.

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

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


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 60 at r4 (raw file):

Previously, Azhng (Archer Zhang) wrote…

Done.

is it possible to check if the in-memory values are also 0 after the reset?

Copy link
Copy Markdown
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/controller_test.go, line 60 at r4 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

is it possible to check if the in-memory values are also 0 after the reset?

Since we are checking crdb_internal views, we are checking both in-memory and persisted stats.

Copy link
Copy Markdown
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 and @matthewtodd)


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 60 at r4 (raw file):

Previously, Azhng (Archer Zhang) wrote…

Since we are checking crdb_internal views, we are checking both in-memory and persisted stats.

In this case, I would prefer if you have a different count for querys in-memory and disk, and check after that, so:

execute 3 querys -> run flush -> execute 2 querys (or any other number, you can even run the tests 2 other times, so you would have 6 in-memory ) -> check count = 5 (or 9 if running tests twice) -> reset -> check count = 0


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 83 at r7 (raw file):

	for _, row := range result {
		_, found := testCases[row[0]]
		require.True(t, found, "expect %s to be found, not it was not", row[0])

nit: but it was not (or something like this)


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 106 at r7 (raw file):

	for _, row := range result {
		_, found := testCases[row[0]]
		require.True(t, found, "expect %s to be found, not it was not", row[0])

nit: but

Copy link
Copy Markdown
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/controller_test.go, line 60 at r4 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

In this case, I would prefer if you have a different count for querys in-memory and disk, and check after that, so:

execute 3 querys -> run flush -> execute 2 querys (or any other number, you can even run the tests 2 other times, so you would have 6 in-memory ) -> check count = 5 (or 9 if running tests twice) -> reset -> check count = 0

Updated tests to have different numbers of in-memory stats (3) and persisted stats (4) with overlapping fingerprints.
Also updated the test to instead of check for counts, it perform deep inspection on the statement fingerprints and fingerprint IDs.

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

Just one small nit and make sure the tests are passing, otherwise :lgtm:

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


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 60 at r4 (raw file):

Previously, Azhng (Archer Zhang) wrote…

Updated tests to have different numbers of in-memory stats (3) and persisted stats (4) with overlapping fingerprints.
Also updated the test to instead of check for counts, it perform deep inspection on the statement fingerprints and fingerprint IDs.

Great improvement, thanks!


pkg/sql/sqlstats/persistedsqlstats/controller_test.go, line 129 at r9 (raw file):

				found = true

				// Populate fingerprintID

nit: period

@Azhng Azhng force-pushed the sqlstats-reset branch 3 times, most recently from 885a0f6 to b4fea5e Compare August 27, 2021 20:46
@Azhng
Copy link
Copy Markdown
Contributor Author

Azhng commented Aug 27, 2021

@maryliag, found out the cause for the unit test timeout. The culprit is TestLogic/5node/distsql_crdb_internal. The offending query was originally introduced as a regression test for #62587.

Since the crdb_internal.reset_sql_stats() is now using TRUNCATE, it has become a lot more expensive to execute. Therefore the offending query now effectively performs 10000 truncate on our own system table 🤦 .

We created a small table so that this executes a lot faster.

PTAL

@Azhng Azhng requested a review from maryliag August 27, 2021 20:51
Copy link
Copy Markdown
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.

can this cause an issue on production or was the issue just for testing? meaning, if there is a huge number of rows in reset, could we hit the timeout there too?

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

@Azhng
Copy link
Copy Markdown
Contributor Author

Azhng commented Aug 27, 2021

I can't imagine anyone in production writing queries like that. It's like doing

SELECT * FROM table WHERE crdb_internal.reset_sql_stats()

The query in #62587 was randomly generated by SQLSmith (a sort-of random SQL query generator).

Copy link
Copy Markdown
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:

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

@Azhng Azhng force-pushed the sqlstats-reset branch 2 times, most recently from f94d850 to d0406b7 Compare August 28, 2021 03:58
Previously, crdb_internal.reset_sql_stats() builtin only resets
cluster-wide in-memory sql stats.
This patch updated the builtin to be able to reset persisted
sql stats as well.

Release justification: category 4

Release note (sql change): crdb_internal.reset_sql_stats() now resets
 persisted SQL Stats.
@Azhng
Copy link
Copy Markdown
Contributor Author

Azhng commented Aug 28, 2021

TFTR!

bors r=maryliag

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 28, 2021

Build failed (retrying...):

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 28, 2021

Build succeeded:

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