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

sqlstats: reuse the temporary statement stats container #123698

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

xinhaoz
Copy link
Member

@xinhaoz xinhaoz commented May 6, 2024

This commit cleans up the confusing swapping and usages of 2 fields on
the StatsCollector, ApplicationStats and flushTarget.

Originally the field flushTargetwas only used for explicit transactions.
In order to provide each statement with the correct txn fingerprint id,
statements for explicit txns were stored in a temp stats container and
flushed to the app container at the end of transaction execution.
The assumption for implicit transactions was that they only had 1
statement and so such statements could be written directly to the parent
ApplicationStats container rather than waiting for the entire transaction
to finish.

Now that we create temporary containers for both explicit and implicit
txns we should reuse the temporary container between transactions instead
of discarding and allocating a new one for each.

Summary:

  • ApplicationStats field is on sslocal.StatsCollector is renamed
    to currentTransactionStatementStats
  • FlushTarget is always defined for a stats collector. It represents
    the current application's sql stats
  • Instead of allocating a new container for each new txn, we'll clear and
    reuse currentTxnStatementStats between transactions
  • StatsCollector no longer implementats the sqlstats.ApplicationStats
    interface. There isn't a need for StatsCollector to be used as an
    interface and doing so makes working with the various containers
    within StatsCollector more difficult.

An exception is made for stats collectors belonging to an internal executor
for an outer transaction. Such executors do not start or end the transaction,
and so no temporary containers will be used in this case.

Fixes: #94650
Release note: None

Copy link

blathers-crl bot commented May 6, 2024

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@xinhaoz xinhaoz marked this pull request as ready for review May 16, 2024 20:47
@xinhaoz xinhaoz requested a review from a team as a code owner May 16, 2024 20:47
@xinhaoz xinhaoz requested review from nkodali, dhartunian and abarganier and removed request for a team May 16, 2024 20:47
@xinhaoz xinhaoz force-pushed the stats-collector branch 3 times, most recently from c81db58 to 9bfd81b Compare May 20, 2024 18:15
Copy link
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier, @nkodali, and @xinhaoz)


-- commits line 41 at r2:
nit managing


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 32 at r1 (raw file):

type StatsCollector struct {
	// currentTransactionStatements is the current transaction's statement statistics.
	// They will be flushed to flushTarget when the transaction is done.

This docstring doesn't match the field name.


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 53 at r1 (raw file):

	sendInsights bool

	flushTarget sqlstats.ApplicationStats

can you add a docstring for this field and explain its purpose?


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 131 at r1 (raw file):

	)

	discardedStats += s.flushTarget.MergeApplicationTransactionStats(

You've removed the only usage of this function so can we delete the impl as well?


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 141 at r1 (raw file):

	}

	s.ApplicationStats.Free(ctx)

was this the only usage of Free? can we delete it?

@xinhaoz xinhaoz force-pushed the stats-collector branch 2 times, most recently from c5f9c24 to 2603522 Compare May 21, 2024 15:22
Copy link
Member Author

@xinhaoz xinhaoz 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 @abarganier, @dhartunian, and @nkodali)


-- commits line 41 at r2:

Previously, dhartunian (David Hartunian) wrote…

nit managing

Done.


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 32 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

This docstring doesn't match the field name.

Fixed.


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 53 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

can you add a docstring for this field and explain its purpose?

Done.


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 131 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

You've removed the only usage of this function so can we delete the impl as well?

Done.


pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go line 141 at r1 (raw file):

Previously, dhartunian (David Hartunian) wrote…

was this the only usage of Free? can we delete it?

Done.

Copy link
Collaborator

@dhartunian dhartunian left a comment

Choose a reason for hiding this comment

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

This ends up reading better as a single diff, given the rename churn, can you squash before merge and combine the commit messages? The lines changed number is not too high so I think it's fine.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @abarganier, @kyle-a-wong, @nkodali, and @xinhaoz)


pkg/sql/sqlstats/ssprovider.go line 96 at r10 (raw file):

	// Clear is like Free but also prepares the container for reuse.
	Clear(context.Context)

Can you add a unit test for this new method? Or do we have coverage on that elsewhere?

@xinhaoz xinhaoz force-pushed the stats-collector branch 8 times, most recently from 076ace4 to 36a5485 Compare June 3, 2024 13:54
@xinhaoz xinhaoz requested a review from dhartunian June 3, 2024 13:59
@xinhaoz
Copy link
Member Author

xinhaoz commented Jun 3, 2024

@dhartunian Can you take another look? I added a call to Free in the conn exec cleanup and had to add an additional flag on the stats collector setup for a special ase internal statement. This additional allows us to preserve current behaviour when it comes to recording stats for internal statements from outer txns.

Copy link
Collaborator

@dhartunian dhartunian 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 @abarganier, @kyle-a-wong, @nkodali, and @xinhaoz)


-- commits line 18 at r11:
I expect to see a test that verifies that the container between transactions is cleared correctly. Do we have coverage for that?


pkg/sql/conn_executor.go line 1264 at r11 (raw file):

	// Free any memory used by the stats collector.
	ex.statsCollector.Free(ctx)

Do we need to be careful not to free the transaction container if it comes from the outer txn?

Copy link
Member Author

@xinhaoz xinhaoz 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 @abarganier, @dhartunian, @kyle-a-wong, and @nkodali)


-- commits line 18 at r11:

Previously, dhartunian (David Hartunian) wrote…

I expect to see a test that verifies that the container between transactions is cleared correctly. Do we have coverage for that?

This should be covered in other tests since we'd expect to see statements associated with certain transactions absent or incorrectly associated with other txns. One test I could add to also verify this behaviour is to assert that the stats collector's local transaction container is empty after a transaction - not sure how useful that would be in this case.


pkg/sql/conn_executor.go line 1264 at r11 (raw file):

Previously, dhartunian (David Hartunian) wrote…

Do we need to be careful not to free the transaction container if it comes from the outer txn?

Good call. I added a guard within Free.


pkg/sql/sqlstats/ssprovider.go line 96 at r10 (raw file):

Previously, dhartunian (David Hartunian) wrote…

Can you add a unit test for this new method? Or do we have coverage on that elsewhere?

We have coverage for this elsewhere.

This commit cleans up the confusing swapping and usages of 2 fields on
the StatsCollector, `ApplicationStats` and `flushTarget`.

Originally the field `flushTarget`was only used for explicit transactions.
In order to provide each statement with the correct txn fingerprint id,
statements for explicit txns were stored in a temp stats container and
flushed to the app container at the end of transaction execution.
The assumption for implicit transactions was that they only had 1
statement and so such statements could be  written directly  to the parent
`ApplicationStats` container rather than waiting for the entire transaction
to finish.

Now that we create temporary containers for both explicit and implicit
txns we should reuse the temporary container between transactions instead
of discarding and allocating a new one for each.

Summary:
- `ApplicationStats` field is on sslocal.StatsCollector is renamed
to `currentTransactionStatementStats`
- `FlushTarget` is always defined for a stats collector. It represents
the current application's sql stats
- Instead of allocating a new container for each new txn, we'll clear and
reuse `currentTxnStatementStats` between transactions
- StatsCollector no longer  implementats the `sqlstats.ApplicationStats`
interface. There isn't a need for StatsCollector to be used as an
interface and doing so  makes working with the various containers
within StatsCollector more difficult.

An exception is made for stats collectors belonging to an internal executor
for an outer transaction. Such executors do not start or end the transaction,
and so no temporary containers will be used in this case.

Fixes: cockroachdb#94650
Release note: None
@xinhaoz
Copy link
Member Author

xinhaoz commented Jun 4, 2024

TFTR!
bors r+

@craig craig bot merged commit 0ecfe25 into cockroachdb:master Jun 4, 2024
22 checks passed
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.

sqlstats: reuse stats container used by current txn and stmts
3 participants