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

*: add infoschema client errors #22382

Merged
merged 15 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions errno/infoschema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// 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.

type errorSummary struct {
sync.Mutex
ErrorCount int
WarningCount int
FirstSeen time.Time
LastSeen time.Time
}

type globalStats struct {
sync.Mutex
errors map[uint16]*errorSummary
}

type userStats struct {
sync.Mutex
errors map[string]map[uint16]*errorSummary
}

type hostStats struct {
sync.Mutex
errors map[string]map[uint16]*errorSummary
}

var global globalStats
var users userStats
var hosts hostStats

func init() {
global.errors = make(map[uint16]*errorSummary)
users.errors = make(map[string]map[uint16]*errorSummary)
hosts.errors = make(map[string]map[uint16]*errorSummary)
}

// FlushStats resets errors and warnings across global/users/hosts
func FlushStats() {
global.Lock()
defer global.Unlock()
users.Lock()
defer users.Unlock()
hosts.Lock()
defer hosts.Unlock()

global.errors = make(map[uint16]*errorSummary)
users.errors = make(map[string]map[uint16]*errorSummary)
hosts.errors = make(map[string]map[uint16]*errorSummary)
}

// GlobalStats summarizes errors and warnings across all users/hosts
func GlobalStats() map[uint16]*errorSummary {
global.Lock()
defer global.Unlock()
return global.errors
}

// UserStats summarizes per-user
func UserStats() map[string]map[uint16]*errorSummary {
users.Lock()
defer users.Unlock()
return users.errors
}

// HostStats summarizes per remote-host
func HostStats() map[string]map[uint16]*errorSummary {
hosts.Lock()
defer hosts.Unlock()
return hosts.errors
}

func initCounters(errCode uint16, user, host string) {
global.Lock()
if _, ok := global.errors[errCode]; !ok {
global.errors[errCode] = &errorSummary{FirstSeen: time.Now()}
}
global.Unlock()
users.Lock()
if _, ok := users.errors[user]; !ok {
users.errors[user] = make(map[uint16]*errorSummary)
}
if _, ok := users.errors[user][errCode]; !ok {
users.errors[user][errCode] = &errorSummary{FirstSeen: time.Now()}
}
users.Unlock()
hosts.Lock()
if _, ok := hosts.errors[host]; !ok {
hosts.errors[host] = make(map[uint16]*errorSummary)
}
if _, ok := hosts.errors[host][errCode]; !ok {
hosts.errors[host][errCode] = &errorSummary{FirstSeen: time.Now()}
}
hosts.Unlock()
}

// IncrementError increments the global/user/host statistics for an errCode
func IncrementError(errCode uint16, user, host string) {
initCounters(errCode, user, host)
// Increment counter + update last seen
global.errors[errCode].Lock()
global.errors[errCode].ErrorCount++
global.errors[errCode].LastSeen = time.Now()
morgo marked this conversation as resolved.
Show resolved Hide resolved
global.errors[errCode].Unlock()
// Increment counter + update last seen
users.errors[user][errCode].Lock()
users.errors[user][errCode].ErrorCount++
users.errors[user][errCode].LastSeen = time.Now()
users.errors[user][errCode].Unlock()
// Increment counter + update last seen
hosts.errors[host][errCode].Lock()
hosts.errors[host][errCode].ErrorCount++
hosts.errors[host][errCode].LastSeen = time.Now()
hosts.errors[host][errCode].Unlock()
}

// IncrementWarning increments the global/user/host statistics for an errCode
func IncrementWarning(errCode uint16, user, host string) {
initCounters(errCode, user, host)
// Increment counter + update last seen
global.errors[errCode].Lock()
global.errors[errCode].WarningCount++
global.errors[errCode].LastSeen = time.Now()
global.errors[errCode].Unlock()
// Increment counter + update last seen
users.errors[user][errCode].Lock()
users.errors[user][errCode].WarningCount++
users.errors[user][errCode].LastSeen = time.Now()
users.errors[user][errCode].Unlock()
// Increment counter + update last seen
hosts.errors[host][errCode].Lock()
hosts.errors[host][errCode].WarningCount++
hosts.errors[host][errCode].LastSeen = time.Now()
hosts.errors[host][errCode].Unlock()
}
6 changes: 5 additions & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,11 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
strings.ToLower(infoschema.TableStatementsSummaryHistory),
strings.ToLower(infoschema.ClusterTableStatementsSummary),
strings.ToLower(infoschema.ClusterTableStatementsSummaryHistory),
strings.ToLower(infoschema.TablePlacementPolicy):
strings.ToLower(infoschema.TablePlacementPolicy),
strings.ToLower(infoschema.TablePlacementPolicy),
morgo marked this conversation as resolved.
Show resolved Hide resolved
strings.ToLower(infoschema.TableClientErrorsSummaryGlobal),
strings.ToLower(infoschema.TableClientErrorsSummaryByUser),
strings.ToLower(infoschema.TableClientErrorsSummaryByHost):
return &MemTableReaderExec{
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ID()),
table: v.Table,
Expand Down
81 changes: 81 additions & 0 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/pingcap/tidb/ddl/placement"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/meta/autoid"
plannercore "github.com/pingcap/tidb/planner/core"
Expand Down Expand Up @@ -142,6 +143,10 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
err = e.setDataForStatementsSummary(sctx, e.table.Name.O)
case infoschema.TablePlacementPolicy:
err = e.setDataForPlacementPolicy(sctx)
case infoschema.TableClientErrorsSummaryGlobal,
infoschema.TableClientErrorsSummaryByUser,
infoschema.TableClientErrorsSummaryByHost:
err = e.setDataForClientErrorsSummary(sctx, e.table.Name.O)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -1864,6 +1869,82 @@ func (e *memtableRetriever) setDataForPlacementPolicy(ctx sessionctx.Context) er
return nil
}

func (e *memtableRetriever) setDataForClientErrorsSummary(ctx sessionctx.Context, tableName string) error {
// Seeing client errors should require the PROCESS privilege, with the exception of errors for your own user.
// This is similar to information_schema.processlist, which is the closest comparison.
var hasProcessPriv bool
loginUser := ctx.GetSessionVars().User
if pm := privilege.GetPrivilegeManager(ctx); pm != nil {
if pm.RequestVerification(ctx.GetSessionVars().ActiveRoles, "", "", "", mysql.ProcessPriv) {
hasProcessPriv = true
}
}

var rows [][]types.Datum
switch tableName {
case infoschema.TableClientErrorsSummaryGlobal:
if !hasProcessPriv {
return plannercore.ErrSpecificAccessDenied.GenWithStackByArgs("PROCESS")
}
for code, summary := range errno.GlobalStats() {
morgo marked this conversation as resolved.
Show resolved Hide resolved
firstSeen := types.NewTime(types.FromGoTime(summary.FirstSeen), mysql.TypeTimestamp, types.DefaultFsp)
lastSeen := types.NewTime(types.FromGoTime(summary.LastSeen), mysql.TypeTimestamp, types.DefaultFsp)
row := types.MakeDatums(
int(code), // ERROR_NUMBER
errno.MySQLErrName[code].Raw, // ERROR_MESSAGE
summary.ErrorCount, // ERROR_COUNT
summary.WarningCount, // WARNING_COUNT
firstSeen, // FIRST_SEEN
lastSeen, // LAST_SEEN
)
rows = append(rows, row)
}
case infoschema.TableClientErrorsSummaryByUser:
for user, agg := range errno.UserStats() {
for code, summary := range agg {
// Allow anyone to see their own errors.
if !hasProcessPriv && loginUser != nil && loginUser.Username != user {
continue
}
firstSeen := types.NewTime(types.FromGoTime(summary.FirstSeen), mysql.TypeTimestamp, types.DefaultFsp)
lastSeen := types.NewTime(types.FromGoTime(summary.LastSeen), mysql.TypeTimestamp, types.DefaultFsp)
row := types.MakeDatums(
user, // USER
int(code), // ERROR_NUMBER
errno.MySQLErrName[code].Raw, // ERROR_MESSAGE
summary.ErrorCount, // ERROR_COUNT
summary.WarningCount, // WARNING_COUNT
firstSeen, // FIRST_SEEN
lastSeen, // LAST_SEEN
)
rows = append(rows, row)
}
}
case infoschema.TableClientErrorsSummaryByHost:
if !hasProcessPriv {
return plannercore.ErrSpecificAccessDenied.GenWithStackByArgs("PROCESS")
}
for host, agg := range errno.HostStats() {
for code, summary := range agg {
firstSeen := types.NewTime(types.FromGoTime(summary.FirstSeen), mysql.TypeTimestamp, types.DefaultFsp)
lastSeen := types.NewTime(types.FromGoTime(summary.LastSeen), mysql.TypeTimestamp, types.DefaultFsp)
row := types.MakeDatums(
host, // HOST
int(code), // ERROR_NUMBER
errno.MySQLErrName[code].Raw, // ERROR_MESSAGE
summary.ErrorCount, // ERROR_COUNT
summary.WarningCount, // WARNING_COUNT
firstSeen, // FIRST_SEEN
lastSeen, // LAST_SEEN
)
rows = append(rows, row)
}
}
}
e.rows = rows
return nil
}

type hugeMemTableRetriever struct {
dummyCloser
table *model.TableInfo
Expand Down
3 changes: 3 additions & 0 deletions executor/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/distsql"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/metrics"
Expand Down Expand Up @@ -1261,6 +1262,8 @@ func (e *SimpleExec) executeFlush(s *ast.FlushStmt) error {
return err
}
}
case ast.FlushClientErrorsSummary:
errno.FlushStats()
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ require (
)

go 1.13

replace github.com/pingcap/parser => github.com/morgo/parser v0.0.0-20210113194243-aa72bd5cf999
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ github.com/montanaflynn/stats v0.0.0-20151014174947-eeaced052adb/go.mod h1:wL8QJ
github.com/montanaflynn/stats v0.0.0-20180911141734-db72e6cae808/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/montanaflynn/stats v0.5.0 h1:2EkzeTSqBB4V4bJwWrt5gIIrZmpJBcoIRGS2kWLgzmk=
github.com/montanaflynn/stats v0.5.0/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/morgo/parser v0.0.0-20210113194243-aa72bd5cf999 h1:8IfXx2ql9BTOV1yLoxV5hNrIeDj0wEdPGP+IrjLYMA8=
github.com/morgo/parser v0.0.0-20210113194243-aa72bd5cf999/go.mod h1:GbEr2PgY72/4XqPZzmzstlOU/+il/wrjeTNFs6ihsSE=
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down Expand Up @@ -739,6 +741,7 @@ github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b/go.mod h1:dA0hQrY
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44 h1:tB9NOR21++IjLyVx3/PCPhWMwqGNCMQEH96A6dMZ/gc=
github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v2.19.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v2.20.3+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
Expand Down
41 changes: 41 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ const (
TableTiFlashSegments = "TIFLASH_SEGMENTS"
// TablePlacementPolicy is the string constant of placement policy table.
TablePlacementPolicy = "PLACEMENT_POLICY"
// TableClientErrorsSummaryGlobal is the string constant of client errors table.
TableClientErrorsSummaryGlobal = "CLIENT_ERRORS_SUMMARY_GLOBAL"
// TableClientErrorsSummaryByUser is the string constant of client errors table.
TableClientErrorsSummaryByUser = "CLIENT_ERRORS_SUMMARY_BY_USER"
// TableClientErrorsSummaryByHost is the string constant of client errors table.
TableClientErrorsSummaryByHost = "CLIENT_ERRORS_SUMMARY_BY_HOST"
)

var tableIDMap = map[string]int64{
Expand Down Expand Up @@ -224,6 +230,9 @@ var tableIDMap = map[string]int64{
TableTiFlashTables: autoid.InformationSchemaDBID + 64,
TableTiFlashSegments: autoid.InformationSchemaDBID + 65,
TablePlacementPolicy: autoid.InformationSchemaDBID + 66,
TableClientErrorsSummaryGlobal: autoid.InformationSchemaDBID + 67,
TableClientErrorsSummaryByUser: autoid.InformationSchemaDBID + 68,
TableClientErrorsSummaryByHost: autoid.InformationSchemaDBID + 69,
}

type columnInfo struct {
Expand Down Expand Up @@ -1289,6 +1298,35 @@ var tablePlacementPolicyCols = []columnInfo{
{name: "CONSTRAINTS", tp: mysql.TypeVarchar, size: 1024},
}

var tableClientErrorsSummaryGlobalCols = []columnInfo{
{name: "ERROR_NUMBER", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "ERROR_MESSAGE", tp: mysql.TypeVarchar, size: 1024, flag: mysql.NotNullFlag},
{name: "ERROR_COUNT", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "WARNING_COUNT", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "FIRST_SEEN", tp: mysql.TypeTimestamp, size: 26},
morgo marked this conversation as resolved.
Show resolved Hide resolved
{name: "LAST_SEEN", tp: mysql.TypeTimestamp, size: 26},
}

var tableClientErrorsSummaryByUserCols = []columnInfo{
{name: "USER", tp: mysql.TypeVarchar, size: 64, flag: mysql.NotNullFlag},
{name: "ERROR_NUMBER", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "ERROR_MESSAGE", tp: mysql.TypeVarchar, size: 1024, flag: mysql.NotNullFlag},
{name: "ERROR_COUNT", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "WARNING_COUNT", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "FIRST_SEEN", tp: mysql.TypeTimestamp, size: 26},
{name: "LAST_SEEN", tp: mysql.TypeTimestamp, size: 26},
}

var tableClientErrorsSummaryByHostCols = []columnInfo{
{name: "HOST", tp: mysql.TypeVarchar, size: 255, flag: mysql.NotNullFlag},
{name: "ERROR_NUMBER", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "ERROR_MESSAGE", tp: mysql.TypeVarchar, size: 1024, flag: mysql.NotNullFlag},
{name: "ERROR_COUNT", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "WARNING_COUNT", tp: mysql.TypeLonglong, size: 64, flag: mysql.NotNullFlag},
{name: "FIRST_SEEN", tp: mysql.TypeTimestamp, size: 26},
{name: "LAST_SEEN", tp: mysql.TypeTimestamp, size: 26},
}

// GetShardingInfo returns a nil or description string for the sharding information of given TableInfo.
// The returned description string may be:
// - "NOT_SHARDED": for tables that SHARD_ROW_ID_BITS is not specified.
Expand Down Expand Up @@ -1655,6 +1693,9 @@ var tableNameToColumns = map[string][]columnInfo{
TableTiFlashTables: tableTableTiFlashTablesCols,
TableTiFlashSegments: tableTableTiFlashSegmentsCols,
TablePlacementPolicy: tablePlacementPolicyCols,
TableClientErrorsSummaryGlobal: tableClientErrorsSummaryGlobalCols,
TableClientErrorsSummaryByUser: tableClientErrorsSummaryByUserCols,
TableClientErrorsSummaryByHost: tableClientErrorsSummaryByHostCols,
}

func createInfoSchemaTable(_ autoid.Allocators, meta *model.TableInfo) (table.Table, error) {
Expand Down
Loading