-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
statistics: use session pool when locking or unlocking table stats #46611
statistics: use session pool when locking or unlocking table stats #46611
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #46611 +/- ##
================================================
- Coverage 73.3655% 72.7560% -0.6095%
================================================
Files 1315 1339 +24
Lines 395585 402706 +7121
================================================
+ Hits 290223 292993 +2770
- Misses 86902 91192 +4290
- Partials 18460 18521 +61
Flags with carried forward coverage won't be shown. Click here to find out more.
|
fdb1ccd
to
c4b21d2
Compare
c4b21d2
to
1a3a853
Compare
statistics: fix tests statistics: fix tests statistics: fix tests
statistics: gen mock statistics: gen mock
35db42f
to
34c1a05
Compare
create table t(a int, b int);
insert into t values (1,2), (3,4), (5,6), (7,8);
analyze table t;
show warnings;
mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1105 | Analyze use auto adjusted sample rate 1.000000 for table test.t, reason to use this rate is "use min(1, 110000/4) as the sample-rate=1" |
| Warning | 1105 | skip analyze locked table: t |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from stats_table_locked;
+----------+--------------+-------+--------------------+
| table_id | modify_count | count | version |
+----------+--------------+-------+--------------------+
| 98 | 4 | 4 | 443863948666601483 |
+----------+--------------+-------+--------------------+
1 row in set (0.00 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> lock stats t;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+-----------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------+
| Warning | 1105 | skip locking locked table: test.t |
+---------+------+-----------------------------------+
1 row in set (0.00 sec)
mysql> show stats_locked;
+---------+------------+----------------+--------+
| Db_name | Table_name | Partition_name | Status |
+---------+------------+----------------+--------+
| test | t | | locked |
+---------+------------+----------------+--------+
1 row in set (0.00 sec)
mysql> select * from stats_table_locked;
Empty set (0.00 sec)
mysql> show stats_locked;
Empty set (0.00 sec)
mysql> unlock stats t;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+---------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------+
| Warning | 1105 | skip unlocking unlocked table: test.t |
+---------+------+---------------------------------------+
1 row in set (0.00 sec) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self-check
@@ -29,9 +27,15 @@ import ( | |||
// - tables: table names of which will be locked. | |||
// Return the message of skipped tables and error. | |||
func (h *Handle) AddLockedTables(tids []int64, pids []int64, tables []*ast.TableName) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this PR (in the next few PRs), can we thoroughly detach(decouple) StatsLock
from StatsHandler
? Specifically, can we remove all Handle.XXLockedXX
methods (all methods in this file lock_stats_handler.go
) and move them into a new separate package?
Logically, if we implement these XXLockedXX
methods on Handler
, we think the management of locked stats is part of the responsibility of Handler
, although Handler
is big enough and it already has too many responsibilities.
Another way is to let the Handler
just as a consumer of locked stats info. It doesn't update locked stats info and only reads locked stats info from the storage and reacts to it accordingly.
I'm not sure whether this can simplify the design and implementation, what do you think of this? @hi-rustin
(I'm not sure whether I explain it clearly...
There was a problem hiding this comment.
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 this really helps. If we want to split the entry from the handle to stats locker. Then we need to put it in the domain or some other global structurations. I guess using the stats handle as an entry point for stats logic is fine. The problem with the handle is that we put too much business logic in the entry point.
Right now, the lock/unlock stats handler is pretty simple:
// AddLockedTables add locked tables id to store.
// - tids: table ids of which will be locked.
// - pids: partition ids of which will be locked.
// - tables: table names of which will be locked.
// Return the message of skipped tables and error.
func (h *Handle) AddLockedTables(tids []int64, pids []int64, tables []*ast.TableName) (string, error) {
se, err := h.pool.Get()
if err != nil {
return "", errors.Trace(err)
}
defer h.pool.Put(se)
exec := se.(sqlexec.RestrictedSQLExecutor)
return lockstats.AddLockedTables(exec, tids, pids, tables)
}
// RemoveLockedTables remove tables from table locked array.
// - tids: table ids of which will be unlocked.
// - pids: partition ids of which will be unlocked.
// - tables: table names of which will be unlocked.
// Return the message of skipped tables and error.
func (h *Handle) RemoveLockedTables(tids []int64, pids []int64, tables []*ast.TableName) (string, error) {
se, err := h.pool.Get()
if err != nil {
return "", errors.Trace(err)
}
defer h.pool.Put(se)
exec := se.(sqlexec.RestrictedSQLExecutor)
return lockstats.RemoveLockedTables(exec, tids, pids, tables)
}
// QueryTablesLockedStatuses query whether table is locked.
// Note: This function query locked tables from store, so please try to batch the query.
func (h *Handle) QueryTablesLockedStatuses(tableIDs ...int64) (map[int64]bool, error) {
tableLocked, err := h.queryLockedTables()
if err != nil {
return nil, err
}
return lockstats.GetTablesLockedStatuses(tableLocked, tableIDs...), nil
}
// queryLockedTables query locked tables from store.
func (h *Handle) queryLockedTables() (map[int64]struct{}, error) {
se, err := h.pool.Get()
if err != nil {
return nil, errors.Trace(err)
}
defer h.pool.Put(se)
exec := se.(sqlexec.RestrictedSQLExecutor)
return lockstats.QueryLockedTables(exec)
}
But I can try it recently to see if this helps. Do you have any suggestions on where to put the `statsLocker/statsUnlocker/statsQuerier'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then in the feature, we can break the huge StatsHandler
down to multiple smaller sub-components.
This makes our code easier to maintain (maintaining multiple smaller and simpler components
is better than maintaining a big and complex component
).
And these sub-components rely on the storage to synchronize information instead of relying on memory status, which is much safer.
@@ -25,16 +25,22 @@ import ( | |||
"go.uber.org/zap" | |||
) | |||
|
|||
var ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we can use const
for these strings.
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hawkingrei, 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 |
[LGTM Timeline notifier]Timeline:
|
What problem does this PR solve?
Issue Number: ref #46351
Problem Summary:
What is changed and how it works?
...e/handletest/statslock/stats_lcok_test.go → ...e/handletest/statslock/stats_lock_test.go
query_lock.go
. See: statistics: remove lockedTable cache #46555 (review)Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.