-
Notifications
You must be signed in to change notification settings - Fork 6.2k
*: add infoschema client errors #22382
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c2afa14
*: add infoschema client errors
morgo b8b8079
Merge remote-tracking branch 'upstream/master' into add-is-client-errors
morgo 1ec7698
Update for parser PR merging
morgo aa10cb7
Run go mod tidy
morgo 6350242
Merge branch 'master' into add-is-client-errors
AilinKid 8da3eb6
Merge remote-tracking branch 'upstream/master' into add-is-client-errors
morgo 3608c9e
Address reviewer feedback
morgo ba137ef
change to single mutex + use deep copy
morgo 3c960e9
change to single mutex
morgo aa6d46c
Fix deep copy, add tests
morgo 0a1f635
Improve test coverage to 100%
morgo c62a2ea
Update errno/infoschema.go
morgo acf1f51
Update errno/infoschema.go
morgo 2dca1bc
Merge branch 'master' into add-is-client-errors
ti-chi-bot 80c9b75
Merge branch 'master' into add-is-client-errors
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package errno | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // The error summary is protected by a mutex for simplicity. | ||
| // It is not expected to be hot unless there are concurrent workloads | ||
| // that are generating high error/warning counts, in which case | ||
| // the system probably has other issues already. | ||
|
|
||
| // ErrorSummary summarizes errors and warnings | ||
| type ErrorSummary struct { | ||
| ErrorCount int | ||
| WarningCount int | ||
| FirstSeen time.Time | ||
| LastSeen time.Time | ||
| } | ||
|
|
||
| // instanceStatistics provide statistics for a tidb-server instance. | ||
| type instanceStatistics struct { | ||
| sync.Mutex | ||
| global map[uint16]*ErrorSummary | ||
| users map[string]map[uint16]*ErrorSummary | ||
| hosts map[string]map[uint16]*ErrorSummary | ||
| } | ||
|
|
||
| var stats instanceStatistics | ||
|
|
||
| func init() { | ||
| FlushStats() | ||
| } | ||
|
|
||
| // FlushStats resets errors and warnings across global/users/hosts | ||
| func FlushStats() { | ||
| stats.Lock() | ||
| defer stats.Unlock() | ||
| stats.global = make(map[uint16]*ErrorSummary) | ||
| stats.users = make(map[string]map[uint16]*ErrorSummary) | ||
| stats.hosts = make(map[string]map[uint16]*ErrorSummary) | ||
| } | ||
|
|
||
| func copyMap(oldMap map[uint16]*ErrorSummary) map[uint16]*ErrorSummary { | ||
| newMap := make(map[uint16]*ErrorSummary, len(oldMap)) | ||
| for k, v := range oldMap { | ||
| newMap[k] = &ErrorSummary{ | ||
| ErrorCount: v.ErrorCount, | ||
| WarningCount: v.WarningCount, | ||
| FirstSeen: v.FirstSeen, | ||
| LastSeen: v.LastSeen, | ||
| } | ||
| } | ||
| return newMap | ||
| } | ||
|
|
||
| // GlobalStats summarizes errors and warnings across all users/hosts | ||
| func GlobalStats() map[uint16]*ErrorSummary { | ||
| stats.Lock() | ||
| defer stats.Unlock() | ||
| return copyMap(stats.global) | ||
| } | ||
|
|
||
| // UserStats summarizes per-user | ||
| func UserStats() map[string]map[uint16]*ErrorSummary { | ||
| stats.Lock() | ||
| defer stats.Unlock() | ||
| newMap := make(map[string]map[uint16]*ErrorSummary, len(stats.users)) | ||
| for k, v := range stats.users { | ||
| newMap[k] = copyMap(v) | ||
| } | ||
| return newMap | ||
| } | ||
|
|
||
| // HostStats summarizes per remote-host | ||
| func HostStats() map[string]map[uint16]*ErrorSummary { | ||
| stats.Lock() | ||
| defer stats.Unlock() | ||
| newMap := make(map[string]map[uint16]*ErrorSummary, len(stats.hosts)) | ||
| for k, v := range stats.hosts { | ||
| newMap[k] = copyMap(v) | ||
| } | ||
| return newMap | ||
| } | ||
|
|
||
| func initCounters(errCode uint16, user, host string) { | ||
| seen := time.Now() | ||
| stats.Lock() | ||
| defer stats.Unlock() | ||
|
|
||
| if _, ok := stats.global[errCode]; !ok { | ||
| stats.global[errCode] = &ErrorSummary{FirstSeen: seen} | ||
| } | ||
| if _, ok := stats.users[user]; !ok { | ||
| stats.users[user] = make(map[uint16]*ErrorSummary) | ||
| } | ||
| if _, ok := stats.users[user][errCode]; !ok { | ||
| stats.users[user][errCode] = &ErrorSummary{FirstSeen: seen} | ||
| } | ||
| if _, ok := stats.hosts[host]; !ok { | ||
| stats.hosts[host] = make(map[uint16]*ErrorSummary) | ||
| } | ||
| if _, ok := stats.hosts[host][errCode]; !ok { | ||
| stats.hosts[host][errCode] = &ErrorSummary{FirstSeen: seen} | ||
| } | ||
| } | ||
|
|
||
| // IncrementError increments the global/user/host statistics for an errCode | ||
| func IncrementError(errCode uint16, user, host string) { | ||
| seen := time.Now() | ||
| initCounters(errCode, user, host) | ||
|
|
||
| stats.Lock() | ||
| defer stats.Unlock() | ||
|
|
||
| // Increment counter + update last seen | ||
| stats.global[errCode].ErrorCount++ | ||
| stats.global[errCode].LastSeen = seen | ||
| // Increment counter + update last seen | ||
| stats.users[user][errCode].ErrorCount++ | ||
| stats.users[user][errCode].LastSeen = seen | ||
| // Increment counter + update last seen | ||
| stats.hosts[host][errCode].ErrorCount++ | ||
| stats.hosts[host][errCode].LastSeen = seen | ||
| } | ||
|
|
||
| // IncrementWarning increments the global/user/host statistics for an errCode | ||
| func IncrementWarning(errCode uint16, user, host string) { | ||
| seen := time.Now() | ||
| initCounters(errCode, user, host) | ||
|
|
||
| stats.Lock() | ||
| defer stats.Unlock() | ||
|
|
||
| // Increment counter + update last seen | ||
| stats.global[errCode].WarningCount++ | ||
| stats.global[errCode].LastSeen = seen | ||
| // Increment counter + update last seen | ||
| stats.users[user][errCode].WarningCount++ | ||
| stats.users[user][errCode].LastSeen = seen | ||
| // Increment counter + update last seen | ||
| stats.hosts[host][errCode].WarningCount++ | ||
| stats.hosts[host][errCode].LastSeen = seen | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package errno | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| . "github.com/pingcap/check" | ||
| ) | ||
|
|
||
| func TestT(t *testing.T) { | ||
| TestingT(t) | ||
| } | ||
|
|
||
| var _ = Suite(&testErrno{}) | ||
|
|
||
| type testErrno struct{} | ||
|
|
||
| func (s *testErrno) TestCopySafety(c *C) { | ||
|
|
||
| IncrementError(123, "user", "host") | ||
| IncrementError(321, "user2", "host2") | ||
| IncrementWarning(123, "user", "host") | ||
| IncrementWarning(999, "user", "host") | ||
| IncrementWarning(222, "u", "h") | ||
|
|
||
| globalCopy := GlobalStats() | ||
| userCopy := UserStats() | ||
| hostCopy := HostStats() | ||
|
|
||
| IncrementError(123, "user", "host") | ||
| IncrementError(999, "user2", "host2") | ||
| IncrementError(123, "user3", "host") | ||
| IncrementWarning(123, "user", "host") | ||
| IncrementWarning(222, "u", "h") | ||
| IncrementWarning(222, "a", "b") | ||
| IncrementWarning(333, "c", "d") | ||
|
|
||
| // global stats | ||
| c.Assert(stats.global[123].ErrorCount, Equals, 3) | ||
| c.Assert(globalCopy[123].ErrorCount, Equals, 1) | ||
|
|
||
| // user stats | ||
| c.Assert(len(stats.users), Equals, 6) | ||
| c.Assert(len(userCopy), Equals, 3) | ||
| c.Assert(stats.users["user"][123].ErrorCount, Equals, 2) | ||
| c.Assert(stats.users["user"][123].WarningCount, Equals, 2) | ||
| c.Assert(userCopy["user"][123].ErrorCount, Equals, 1) | ||
| c.Assert(userCopy["user"][123].WarningCount, Equals, 1) | ||
|
|
||
| // ensure there is no user3 in userCopy | ||
| _, ok := userCopy["user3"] | ||
| c.Assert(ok, IsFalse) | ||
| _, ok = stats.users["user3"] | ||
| c.Assert(ok, IsTrue) | ||
| _, ok = userCopy["a"] | ||
| c.Assert(ok, IsFalse) | ||
| _, ok = stats.users["a"] | ||
| c.Assert(ok, IsTrue) | ||
|
|
||
| // host stats | ||
| c.Assert(len(stats.hosts), Equals, 5) | ||
| c.Assert(len(hostCopy), Equals, 3) | ||
| IncrementError(123, "user3", "newhost") | ||
| c.Assert(len(stats.hosts), Equals, 6) | ||
| c.Assert(len(hostCopy), Equals, 3) | ||
|
|
||
| // ensure there is no newhost in hostCopy | ||
| _, ok = hostCopy["newhost"] | ||
| c.Assert(ok, IsFalse) | ||
| _, ok = stats.hosts["newhost"] | ||
| c.Assert(ok, IsTrue) | ||
| _, ok = hostCopy["b"] | ||
| c.Assert(ok, IsFalse) | ||
| _, ok = stats.hosts["b"] | ||
| c.Assert(ok, IsTrue) | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.