-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: remove lockedTable cache #46555
statistics: remove lockedTable cache #46555
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #46555 +/- ##
================================================
- Coverage 73.3411% 72.4588% -0.8824%
================================================
Files 1309 1331 +22
Lines 394560 403346 +8786
================================================
+ Hits 289375 292260 +2885
- Misses 86769 92556 +5787
- Partials 18416 18530 +114
Flags with carried forward coverage won't be shown. Click here to find out more.
|
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) |
} | ||
|
||
// IsTableLocked check whether table is locked in handle with Handle.Mutex | ||
func (h *Handle) IsTableLocked(tableID int64) bool { | ||
func (h *Handle) IsTableLocked(tableID int64) (bool, 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.
Can we allow this method to accept multiple table IDs like IsTableLocked(tableIDs ...int64) ([]bool, error)
to get a better performance? Otherwise when executing show stats locked
and there a N tables, we have to execute SELECT table_id FROM mysql.stats_table_locked
N times, which is a little slow.
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.
Good point. I tried it this morning. It makes the code complicated. I will try it again.
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.
Addressed in a6a0bf3
(#46555)
fe795da
to
a6a0bf3
Compare
statistics/handle/update.go
Outdated
checkedTableIDs, err := h.QueryTablesLockedStatuses(tids...) | ||
if err != nil { | ||
logutil.BgLogger().Error("check table lock failed", | ||
zap.String("category", "stats"), zap.Error(err)) | ||
continue | ||
} | ||
|
||
for _, tbl := range tbls { | ||
//if table locked, skip analyze | ||
if h.IsTableLocked(tbl.Meta().ID) { | ||
// If table locked, skip analyze. | ||
if checkedTableIDs[tbl.Meta().ID] { | ||
logutil.BgLogger().Info("skip analyze locked table", zap.String("category", "stats"), | ||
zap.String("db", db), zap.String("table", tbl.Meta().Name.O)) |
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.
Please add some comments here to notice this check is not complete. For example, some other TiDB node may lock or unlock this table between this check and the analyze command.
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.
Better to enhance or remove this logic in the future.
statistics: rename statistics: rename statistics: rename statistics: rename
5c971df
to
9218d19
Compare
[LGTM Timeline notifier]Timeline:
|
/retest |
1 similar comment
/retest |
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.
Why are we switching from Lock
to RLock
? Would this possibly cause problems when there are concurrent operations?
It was a mistake. I reverted it. |
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.
- A typo in filename
statistics/handle/handletest/statslock/stats_lcok_test.go
. Seems it has been there for a long time. - Personally, I think it'll be better if we extract something like
query_lock.go
fromlock_stats.go
. Feel free to update it or keep it unchanged.
} else { | ||
tbl, ok := infoSchema.TableByID(tid) | ||
if !ok { | ||
// Ignore this table because it may have been dropped. |
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.
This line can be removed because dropping this table also wouldn't make this case happen.
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hawkingrei, 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 |
/retest |
3 similar comments
/retest |
/retest |
/retest |
/test all |
I will deal with them in my next PR. |
What problem does this PR solve?
Issue Number: ref #46351
Problem Summary:
What changed and how it works?
Removed the
lockedTable
cache. The reason we're removing it is because it doesn't help. It's locked and unlocked so infrequently that it doesn't matter if we query it every time. This way we don't need to load it as often.Removed
LoadLockedTables
background taskUse the read lock to protect the SQLExecutor
Move all code to
lockstats
pkgCheck List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.