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

br: support backup and restore column_stats_usage #54634

Merged

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Jul 15, 2024

What problem does this PR solve?

Issue Number: ref #53567

Problem Summary:

After we enabled the predicate columns feature by default, we need to support backup and restore this information.

What changed and how does it work?

  • Added the support for backuping and restoring column_stats_usage.
  • I moved some functions from pkg/statistics/handle/usage/predicate_column.go to pkg/statistics/handle/usage/predicatecolumn/predicate_column.go, otherwise it would cause a cyclic dependence issue.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

支持备份和恢复 mysql.column_stats_usage 表
Support for backing up and restoring mysql.column_stats_usage table

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 15, 2024
@Rustin170506 Rustin170506 force-pushed the rustin-patch-column_stats_usage branch from 5f3e251 to 7ac61e4 Compare July 16, 2024 07:59
Copy link

codecov bot commented Jul 16, 2024

Codecov Report

Attention: Patch coverage is 74.43182% with 45 lines in your changes missing coverage. Please review.

Project coverage is 57.2039%. Comparing base (2108661) to head (5f69217).
Report is 30 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #54634         +/-   ##
=================================================
- Coverage   74.6249%   57.2039%   -17.4211%     
=================================================
  Files          1551       1674        +123     
  Lines        362640     617732     +255092     
=================================================
+ Hits         270620     353367      +82747     
- Misses        72390     240802     +168412     
- Partials      19630      23563       +3933     
Flag Coverage Δ
integration 38.3661% <26.1363%> (?)
unit 72.6265% <74.4318%> (-0.9101%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.9656% <ø> (-2.2339%) ⬇️
parser ∅ <ø> (∅)
br 62.7931% <ø> (+15.1614%) ⬆️

@Rustin170506 Rustin170506 force-pushed the rustin-patch-column_stats_usage branch from 911b542 to 739b023 Compare July 16, 2024 10:43
@Rustin170506
Copy link
Member Author

/retest

Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

🔢 Self-check (PR reviewed by myself and ready for feedback.)

colStatsUsage map[model.TableItemID]statstypes.ColStatsTimeInfo,
) error {
for colID, statsUsage := range colStatsUsage {
lastUsedAt := "NULL"
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if we have a better way to handle this. The ExecRows function cannot handle nil correctly, so I have to rely on the CONVERT_TZ to insert 'null' if it is 'NULL'.

@Rustin170506 Rustin170506 force-pushed the rustin-patch-column_stats_usage branch from 5fdf072 to 5f69217 Compare July 17, 2024 09:27
@Rustin170506 Rustin170506 changed the title WIP: br: support backup and restore column_stats_usage br: support backup and restore column_stats_usage Jul 17, 2024
@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. labels Jul 17, 2024
@Rustin170506
Copy link
Member Author

Rustin170506 commented Jul 17, 2024

Tests locally:

  1. Start the TiDB cluster with patch: tiup playground nightly --db.binpath /Volumes/t7/code/tidb/bin/tidb-server
  2. Build the BR: make build_br
  3. Create a table
USE test;

CREATE TABLE new_employees
(
    id INT NOT NULL,
    fname VARCHAR(30),
    lname VARCHAR(30),
    hired DATE NOT NULL DEFAULT '1970-01-01',
    separated DATE DEFAULT '9999-12-31',
    job_code INT,
    store_id INT NOT NULL
);
  1. Insert 3 rows
INSERT INTO new_employees (id, fname, lname, hired, separated, job_code, store_id) VALUES
(1, 'John', 'Doe', '2023-01-01', '9999-12-31', 101, 1),
(2, 'Jane', 'Doe', '2023-02-01', '9999-12-31', 102, 2),
(3, 'Jim', 'Beam', '2023-03-01', '9999-12-31', 103, 3);
  1. Select the table with column a
SELECT * from mysql.stats_meta;
SELECT * FROM new_employees WHERE  fname = 'a';
  1. Wait for dumping the predicate columns into the system table:
SELECT * FROM mysql.column_stats_usage;
+--------+---------+-------------------+----------------+
|table_id|column_id|last_used_at       |last_analyzed_at|
+--------+---------+-------------------+----------------+
|104     |2        |2024-07-17 18:06:39|null            |
+--------+---------+-------------------+----------------+
  1. Analyze table: ANALYZE table new_employees; and check the column_stats_usage again:
ANALYZE table new_employees;
SELECT * FROM mysql.column_stats_usage;
+--------+---------+-------------------+-------------------+
|table_id|column_id|last_used_at       |last_analyzed_at   |
+--------+---------+-------------------+-------------------+
|104     |2        |2024-07-17 18:06:39|2024-07-17 18:11:45|
+--------+---------+-------------------+-------------------+
  1. Backup the cluster to a local file path: br backup full --pd "127.0.0.1:2379" --storage local:///Volumes/t7/code/brtest --ignore-stats=false
❯ ./br backup full --pd "127.0.0.1:2379" --storage local:///Volumes/t7/code/brtest --ignore-stats=false
Detail BR log in /var/folders/j1/7l6snwpx6svgqxh79bz_d27m0000gn/T/br.log.2024-07-17T18.12.44+0800 
Full Backup <---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
Checksum <------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2024/07/17 18:12:50.655 +08:00] [INFO] [collector.go:77] ["Full Backup success summary"] [total-ranges=25] [ranges-succeed=25] [ranges-failed=0] [backup-checksum=48.687708ms] [backup-fast-checksum=2.691792ms] [backup-total-ranges=120] [backup-total-regions=120] [total-take=6.39242775s] [backup-data-size(after-compressed)=115.7kB] [Size=115737] [BackupTS=451205179499610114] [total-kv=1433] [total-kv-size=415.9kB] [average-speed=65.06kB/s]

  1. Create a new cluster: tiup playground nightly --db.binpath /Volumes/t7/code/tidb/bin/tidb-server
  2. Make sure no predicate columns in new cluster at all:
SELECT * FROM mysql.column_stats_usage;
  1. Restore from the local file: br restore db --pd "127.0.0.1:2379" --db test --storage local:///Volumes/t7/code/brtest
❯ ./br restore db --pd "127.0.0.1:2379" --db test --storage local:///Volumes/t7/code/brtest                                                                                                   
Detail BR log in /var/folders/j1/7l6snwpx6svgqxh79bz_d27m0000gn/T/br.log.2024-07-17T18.13.53+0800 
DataBase Restore <----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> 100.00%
[2024/07/17 18:13:57.515 +08:00] [INFO] [collector.go:77] ["DataBase Restore success summary"] [total-ranges=2] [ranges-succeed=2] [ranges-failed=0] [split-region=1.348875ms] [restore-ranges=1] [total-take=3.823280459s] [total-kv=3] [total-kv-size=216B] [average-speed=56.5B/s] [restore-data-size(after-compressed)=1.897kB] [Size=1897] [BackupTS=451205179499610114] [RestoreTS=451205197701316644]
  1. Check the column_stats_usage again:
SELECT * FROM mysql.column_stats_usage;
+--------+---------+-------------------+-------------------+
|table_id|column_id|last_used_at       |last_analyzed_at   |
+--------+---------+-------------------+-------------------+
|104     |2        |2024-07-17 18:06:39|2024-07-17 18:11:45|
+--------+---------+-------------------+-------------------+

@Rustin170506
Copy link
Member Author

/retest

@Rustin170506 Rustin170506 added component/br This issue is related to BR of TiDB. component/statistics labels Jul 17, 2024
@Rustin170506
Copy link
Member Author

/retest

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 17, 2024
Copy link

ti-chi-bot bot commented Jul 19, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hawkingrei, Leavrth

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added the approved label Jul 19, 2024
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 19, 2024
Copy link

ti-chi-bot bot commented Jul 19, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-07-17 11:05:04.312252064 +0000 UTC m=+438326.303193531: ☑️ agreed by hawkingrei.
  • 2024-07-19 05:23:35.82006073 +0000 UTC m=+590637.811002201: ☑️ agreed by Leavrth.

@ti-chi-bot ti-chi-bot bot merged commit cb6f913 into pingcap:master Jul 19, 2024
40 of 44 checks passed
@Rustin170506 Rustin170506 deleted the rustin-patch-column_stats_usage branch July 19, 2024 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved component/br This issue is related to BR of TiDB. component/statistics lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants