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

planner: support lock/unlock stats of partition tables #46768

Merged
merged 15 commits into from
Sep 15, 2023

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Sep 8, 2023

What problem does this PR solve?

Issue Number: ref #46351

Problem Summary:

What is changed and how it works?

  • added support for lock/unlock stats of partition tables
  • added test for lock/unlock stats of partition tables

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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.

None

@ti-chi-bot

This comment was marked as outdated.

@ti-chi-bot ti-chi-bot bot added 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. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Sep 8, 2023
@tiprow

This comment was marked as outdated.

@Rustin170506 Rustin170506 force-pushed the rustin-patch-stats-partition branch 2 times, most recently from b704873 to 29d37d9 Compare September 8, 2023 08:32
@Rustin170506 Rustin170506 marked this pull request as ready for review September 11, 2023 06:56
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 11, 2023
@codecov

This comment was marked as spam.

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

statistics/handle/lockstats/unlock_stats.go Show resolved Hide resolved
@pingcap pingcap deleted a comment from ti-chi-bot bot Sep 11, 2023
@pingcap pingcap deleted a comment from ti-chi-bot bot Sep 11, 2023
@Rustin170506
Copy link
Member Author

/retest

1 similar comment
@Rustin170506
Copy link
Member Author

/retest

parser/ast/stats.go Outdated Show resolved Hide resolved
parser/parser.y Outdated Show resolved Hide resolved
planner/core/common_plans.go Outdated Show resolved Hide resolved
time-and-fate

This comment was marked as outdated.

@Rustin170506

This comment was marked as outdated.

@Rustin170506 Rustin170506 marked this pull request as draft September 12, 2023 11:42
@ti-chi-bot ti-chi-bot bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 12, 2023
@Rustin170506 Rustin170506 marked this pull request as ready for review September 12, 2023 11:58
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Sep 12, 2023
@Rustin170506
Copy link
Member Author

Rustin170506 commented Sep 14, 2023

  1. Start the TiDB cluster: tiup playground v7.3.0 --tiflash 0 --db.binpath /Users/hi-rustin/GolandProjects/tidb/bin/tidb-server
  2. Create table and analyze it:
create table t(a int, b varchar(10), index idx_b (b)) partition by range(a) (partition p0 values less than (10), partition p1 values less than (20));
analyze table test.t;
show warnings;

+---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                              |
+---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1105 | disable dynamic pruning due to t has no global stats                                                                                                                 |
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1" |
| Warning | 1105 | disable dynamic pruning due to t has no global stats                                                                                                                 |
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1" |
+---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  1. Lock one partition: lock stats t partition p0;
  2. Check the lock result: show stats_locked;
mysql> show stats_locked;
+---------+------------+----------------+--------+
| Db_name | Table_name | Partition_name | Status |
+---------+------------+----------------+--------+
| test    | t          | p0             | locked |
+---------+------------+----------------+--------+
1 row in set (0.00 sec
  1. Insert some data: insert into t(a, b) values(1,'a'); insert into t(a, b) values(2,'b');
  2. Analyze table again: analyze table test.t;
mysql> analyze table test.t;
Query OK, 0 rows affected, 3 warnings (0.11 sec)

mysql> show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                                                                              |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "Row count in stats_meta is much smaller compared with the row count got by PD, use min(1, 15000/2) as the sample-rate=1" |
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1"                                                                 |
| Warning | 1105 | skip analyze locked table: test.t partition (p0)                                                                                                                                                                                     |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
  1. Unlock partition: unlock stats t partition p0;
  2. Analyze partition again: analyze table test.t partition p0;
mysql> analyze table test.t partition p0;
Query OK, 0 rows affected, 1 warning (0.10 sec)

mysql> show warnings;
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "use min(1, 110000/2) as the sample-rate=1" |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

@Rustin170506
Copy link
Member Author

Rustin170506 commented Sep 14, 2023

If a partition is locked before generating valid statistics, what will happen as a result?

  1. Start the TiDB cluster: tiup playground v7.3.0 --tiflash 0 --db.binpath /Users/hi-rustin/GolandProjects/tidb/bin/tidb-server
  2. Create table and lock it:
create table t(a int, b varchar(10), index idx_b (b)) partition by range(a) (partition p0 values less than (10), partition p1 values less than (20)); lock stats t partition p0;
  1. Check the lock result: show stats_locked;
mysql> show stats_locked;
+---------+------------+----------------+--------+
| Db_name | Table_name | Partition_name | Status |
+---------+------------+----------------+--------+
| test    | t          | p0             | locked |
+---------+------------+----------------+--------+
1 row in set (0.00 sec
  1. Insert some data: insert into t(a, b) values(1,'a'); insert into t(a, b) values(2,'b');
  2. Analyze table again: analyze table test.t;
mysql> analyze table test.t;
Query OK, 0 rows affected, 3 warnings (0.11 sec)

mysql> show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                                                                              |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1105 | disable dynamic pruning due to t has no global stats                                                                                                                                                                                 |
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "Row count in stats_meta is much smaller compared with the row count got by PD, use min(1, 15000/2) as the sample-rate=1" |
| Warning | 1105 | disable dynamic pruning due to t has no global stats                                                                                                                                                                                 |
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1"                                                                 |
| Warning | 1105 | skip analyze locked table: test.t partition (p0)                                                                                                                                                                                     |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)
`
6. After one minute, try it again: `analyze table test.t;`
```sql
mysql> analyze table test.t;
Query OK, 0 rows affected, 3 warnings (0.09 sec)

mysql> show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                                                                              |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "Row count in stats_meta is much smaller compared with the row count got by PD, use min(1, 15000/2) as the sample-rate=1" |
| Note    | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1"                                                                 |
| Warning | 1105 | skip analyze locked table: test.t partition (p0)                                                                                                                                                                                     |
+---------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.01 sec)
  1. Get meta stats: select * from stats_meta; and you can see p1 has been updated.
mysql> select * from stats_meta;
+--------------------+----------+--------------+-------+--------------------+
| version            | table_id | modify_count | count | snapshot           |
+--------------------+----------+--------------+-------+--------------------+
| 444245517030129674 |      101 |            0 |     0 |                  0 |
| 444245637485297674 |      102 |            0 |     0 | 444245637472190466 |
| 444245637498404890 |      100 |            0 |     0 |                  0 |
+--------------------+----------+--------------+-------+--------------------+
3 rows in set (0.01 sec)
  1. Unlock partition: unlock stats t partition p0;
  2. Analyze again: analyze table test.t;
mysql> analyze table test.t;
Query OK, 0 rows affected, 2 warnings (0.14 sec)

mysql> show warnings;
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                              |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p0, reason to use this rate is "Row count in stats_meta is much smaller compared with the row count got by PD, use min(1, 15000/2) as the sample-rate=1" |
| Note  | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t's partition p1, reason to use this rate is "TiDB assumes that the table is empty, use sample-rate=1"                                                                 |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
  1. Get meta stats again: select * from stats_meta; and p0 has been updated.
mysql> select * from stats_meta;
+--------------------+----------+--------------+-------+--------------------+
| version            | table_id | modify_count | count | snapshot           |
+--------------------+----------+--------------+-------+--------------------+
| 444245677226328080 |      101 |            0 |     2 | 444245677213220865 |
| 444245677213220879 |      102 |            0 |     0 | 444245677213220865 |
| 444245677239435306 |      100 |            0 |     2 |                  0 |
+--------------------+----------+--------------+-------+--------------------+
3 rows in set (0.00 sec)

@pingcap pingcap deleted a comment from ti-chi-bot bot Sep 14, 2023
}

return tableID.TableID
panic("unreachable")
Copy link
Contributor

@qw4990 qw4990 Sep 14, 2023

Choose a reason for hiding this comment

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

Better to remove the panic, it may cause the process to exit if we add some new task but forget to update here in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you have any suggestions on how to handle this case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe I can return an error here?

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Sep 14, 2023
@easonn7
Copy link

easonn7 commented Sep 15, 2023

/approve
There is no sysvar change.

@ti-chi-bot ti-chi-bot bot added the approved label Sep 15, 2023
Copy link
Member

@time-and-fate time-and-fate left a comment

Choose a reason for hiding this comment

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

In a locked table, locking unlocked partitions and unlocking locked partitions are both meaningless in our implementation.
We disallow the former case but allow the latter case. Is that expected?

statistics/handle/update.go Outdated Show resolved Hide resolved
for pid := range pidNames {
pids = append(pids, pid)
}
statsLogger.Info("lock partitions", zap.Int64("tableID", tid), zap.Int64s("partitionIDs", pids))
Copy link
Member

Choose a reason for hiding this comment

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

I think it'll be better if we add table names and partition names. The same for the unlock stats case and lock/unlock normal table case.

Copy link
Member

Choose a reason for hiding this comment

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

Also OK for me to improve this in another PR 😆

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 will improve it in my next PR. Because I want to change how we pass these args.

@Rustin170506
Copy link
Member Author

In a locked table, locking unlocked partitions and

Was that a typo? I think there are no unlocked partitions for a locked table. Or do you mean newly added partitions?

@Rustin170506
Copy link
Member Author

unlocking locked partitions are both meaningless in our implementation.
We disallow the former case but allow the latter case. Is that expected?

Fixed in @hi-rustin
executor: do not allow to unlock partitions of the locked table

Thanks for your review! 💚 💙 💜 💛 ❤️

@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 15, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: easonn7, qw4990, time-and-fate

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 lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 15, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 15, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-09-14 07:16:13.308453128 +0000 UTC m=+156139.276041179: ☑️ agreed by qw4990.
  • 2023-09-15 12:06:43.015294512 +0000 UTC m=+259968.982882562: ☑️ agreed by time-and-fate.

@ti-chi-bot ti-chi-bot bot merged commit 8b15111 into pingcap:master Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved component/statistics lgtm release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants