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

Add index and table metrics to vttablet #17570

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add index and table metrics to vttablet #17570

wants to merge 3 commits into from

Conversation

rafer
Copy link
Contributor

@rafer rafer commented Jan 18, 2025

Description

This adds the following metrics to vttablet's schema engine.

  • TableRows (by table) - The estimated number of rows in the table
  • TableClusteredIndexSize (by table) - The size of the clustered index (i.e. the size of the "row data")
  • IndexCardinality (by index) - The estimated number of unique values in the index
  • IndexBytes (by index) - The size of the index
metrics sample
curl -sS http://localhost:15100/debug/vars | jq -S '{TableRows, TableClusteredIndexSize, IndexCardinality, IndexBytes}'
{
  "IndexBytes": {
    "corder.PRIMARY": 16384,
    "customer.PRIMARY": 16384,
    "product.PRIMARY": 16384
  },
  "IndexCardinality": {
    "corder.PRIMARY": 0,
    "customer.PRIMARY": 0,
    "product.PRIMARY": 0
  },
  "TableClusteredIndexSize": {
    "corder": 16384,
    "customer": 16384,
    "product": 16384
  },
  "TableRows": {
    "corder": 0,
    "customer": 0,
    "product": 0
  }
}

These values are read from mysql system tables when Engine#Reload is called, and made available via Env#Exporter

Partition names

Since mysql implements partitions internally as one table per partition, the "table name" referenced in the stats tables mostly look like TABLE_NAME#p#PARTITION_NAME. We want to report statistics per logical table (not per partition) so we need a way to extract the underlying table name. Unfortunately, it's not as simple as splitting the composite name by #p#, because it's possible to create a table or partition with #p# as a part of the name.

The approach I took here is to load the information_schema.partitions table, which includes separate columns for the table and partition name. Then, as we read through the stats tables that contain the composite name, if we encounter a #p#, we attempt to find a matching table/partition in the loaded list, and, if it is present, we can find the base table name.

An alternative method would be to make use of information_schema.innodb_tables.name column (like we do here), which provides the table name with special characters encoded. This makes it possible to confidently split on #p#, because the # characters would be encoded if they were part of the real table or partition name. I opted for the "loading all partitions" approach instead because the joins required to get an encoded table name made the queries too slow when there are many tables, indexes or partitions.

Performance

Since performance of the queries run on #Reload has been an issue in the past, I tested the queries added in this PR with the following test setup.

  • 5,000 tables with 6 indexes each (5 secondary indexes)
  • 5 tables with 5,000 partitions each, 4 indexes each (3 secondary indexes)

Results:

  • Entire updateTableIndexMetrics method: 716ms
    • Partitions query: 37ms
    • Table stats query (table row count and clustered index size): 15ms
    • Index bytes query (byte size of each index): 205ms
    • Cardinality query: 427ms

For comparison the InnoDBTableSizes query takes 2.0s for the same test setup.

Related Issue(s)

#17569

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

None

* IndexBytes
* IndexCardinality
* TableClusteredIndexSize
* TableRows

Signed-off-by: Rafer Hazen <[email protected]>
Copy link
Contributor

vitess-bot bot commented Jan 18, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Jan 18, 2025
@github-actions github-actions bot added this to the v22.0.0 milestone Jan 18, 2025
@rafer rafer added Type: Feature Component: VTTablet and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 18, 2025
Copy link

codecov bot commented Jan 21, 2025

Codecov Report

Attention: Patch coverage is 84.03361% with 19 lines in your changes missing coverage. Please review.

Project coverage is 67.65%. Comparing base (71ccd6d) to head (8f83d68).
Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vttablet/tabletserver/schema/engine.go 84.03% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17570      +/-   ##
==========================================
- Coverage   67.71%   67.65%   -0.06%     
==========================================
  Files        1584     1585       +1     
  Lines      254721   256592    +1871     
==========================================
+ Hits       172473   173608    +1135     
- Misses      82248    82984     +736     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -432,7 +444,7 @@ func (se *Engine) reload(ctx context.Context, includeStats bool) error {
// We therefore don't want to query for table sizes in getTableData()
includeStats = false

innodbResults, err := conn.Conn.Exec(ctx, innodbTableSizesQuery, maxTableCount, false)
innodbResults, err := conn.Conn.Exec(ctx, innodbTableSizesQuery, maxTableCount*maxPartitionsPerTable, false)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Increased this because even with our limit of 10k tables, it's possible that there are more partitions than that (and this query returns one row per partition).

@@ -404,6 +404,10 @@ func TestReloadView(t *testing.T) {
db.AddQuery("SELECT TABLE_NAME, CREATE_TIME FROM _vt.`tables`", &sqltypes.Result{})
// adding query pattern for udfs
db.AddQueryPattern("SELECT name.*", &sqltypes.Result{})
db.AddQuery("select table_name, partition_name from information_schema.partitions where table_schema = database() and partition_name is not null", &sqltypes.Result{})
Copy link
Contributor Author

@rafer rafer Jan 22, 2025

Choose a reason for hiding this comment

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

I don't love that these queries have leaked out of the schema package, but I can't think of better way to make this test pass (unless we want to make the query constants public). Interested if this causes anyone concern.

@@ -466,8 +478,12 @@ func (se *Engine) reload(ctx context.Context, includeStats bool) error {
}
}
}
if err := se.updateTableIndexMetrics(ctx, conn.Conn); err != nil {
log.Errorf("Updating index/table statistics failed, error: %v", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This just logs instead of failing the method. I figure if updateTableIndexMetrics fails it's better to continue on since the only impact is missing metrics.

@deepthi deepthi removed the request for review from shlomi-noach January 22, 2025 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant