Skip to content

Commit

Permalink
telemetry: fix panic caused by assign nil map (#25129)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Jun 4, 2021
1 parent 3e69ac2 commit fbbada3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
46 changes: 24 additions & 22 deletions telemetry/data_slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func (bucketMap SlowQueryBucket) String() string {
const slowQueryBucketNum = 29 //prometheus.ExponentialBuckets(0.001, 2, 28), and 1 more +Inf

var (
// LastSQBInfo records last statistic information of slow query buckets
LastSQBInfo SlowQueryBucket
// CurrentSQBInfo records current statitic information of slow query buckets
CurrentSQBInfo SlowQueryBucket
slowQueryLock = sync.RWMutex{}
// lastSQBInfo records last statistic information of slow query buckets
lastSQBInfo SlowQueryBucket
// currentSQBInfo records current statitic information of slow query buckets
currentSQBInfo SlowQueryBucket
slowQueryLock sync.Mutex
)

func getSlowQueryStats(ctx sessionctx.Context) (*slowQueryStats, error) {
Expand All @@ -74,7 +74,7 @@ func getSlowQueryStats(ctx sessionctx.Context) (*slowQueryStats, error) {

// getSlowQueryBucket genenrates the delta SlowQueryBucket to report
func getSlowQueryBucket(ctx sessionctx.Context) (*SlowQueryBucket, error) {
// update CurrentSQBInfo first, then gen delta
// update currentSQBInfo first, then gen delta
if err := updateCurrentSQB(ctx); err != nil {
return nil, err
}
Expand All @@ -95,7 +95,9 @@ func updateCurrentSQB(ctx sessionctx.Context) (err error) {
logutil.BgLogger().Info("querySlowQueryMetric got error")
return err
}

if value == nil {
return
}
if value.Type() != pmodel.ValVector {
return errors.New("Prom vector expected, got " + value.Type().String())
}
Expand All @@ -104,7 +106,7 @@ func updateCurrentSQB(ctx sessionctx.Context) (err error) {
for _, sample := range promVec {
metric := sample.Metric
bucketName := metric["le"] //hardcode bucket upper bound
CurrentSQBInfo[string(bucketName)] = int(sample.Value)
currentSQBInfo[string(bucketName)] = int(sample.Value)
}
slowQueryLock.Unlock()
return nil
Expand Down Expand Up @@ -151,37 +153,37 @@ func querySlowQueryMetric(sctx sessionctx.Context) (result pmodel.Value, err err
func calculateDeltaSQB() *SlowQueryBucket {
deltaMap := make(SlowQueryBucket)
slowQueryLock.Lock()
for key, value := range CurrentSQBInfo {
deltaMap[key] = value - (LastSQBInfo)[key]
for key, value := range currentSQBInfo {
deltaMap[key] = value - (lastSQBInfo)[key]
}
slowQueryLock.Unlock()
return &deltaMap
}

// initSlowQueryStats Init LastSQBInfo, follow the definition of metrics/server.go
// init Init lastSQBInfo, follow the definition of metrics/server.go
// Buckets: prometheus.ExponentialBuckets(0.001, 2, 28), // 1ms ~ 1.5days
func initSlowQueryStats() {
LastSQBInfo := make(SlowQueryBucket)
CurrentSQBInfo := make(SlowQueryBucket)
func init() {
lastSQBInfo = make(SlowQueryBucket)
currentSQBInfo = make(SlowQueryBucket)

bucketBase := 0.001 // From 0.001 to 134217.728, total 28 float number; the 29th is +Inf
for i := 0; i < slowQueryBucketNum-1; i++ {
LastSQBInfo[strconv.FormatFloat(bucketBase, 'f', 3, 32)] = 0
CurrentSQBInfo[strconv.FormatFloat(bucketBase, 'f', 3, 32)] = 0
lastSQBInfo[strconv.FormatFloat(bucketBase, 'f', 3, 32)] = 0
currentSQBInfo[strconv.FormatFloat(bucketBase, 'f', 3, 32)] = 0
bucketBase += bucketBase
}
LastSQBInfo["+Inf"] = 0
CurrentSQBInfo["+Inf"] = 0
lastSQBInfo["+Inf"] = 0
currentSQBInfo["+Inf"] = 0

logutil.BgLogger().Info("Telemetry slow query stats initialized", zap.String("CurrentSQBInfo", CurrentSQBInfo.String()), zap.String("LastSQBInfo", LastSQBInfo.String()))
logutil.BgLogger().Info("Telemetry slow query stats initialized", zap.String("currentSQBInfo", currentSQBInfo.String()), zap.String("lastSQBInfo", lastSQBInfo.String()))
}

// postReportSlowQueryStats copy CurrentSQBInfo to LastSQBInfo to be ready for next report
// postReportSlowQueryStats copy currentSQBInfo to lastSQBInfo to be ready for next report
// this function is designed for being compatible with preview telemetry
func postReportSlowQueryStats() {
slowQueryLock.Lock()
LastSQBInfo = CurrentSQBInfo
CurrentSQBInfo = make(SlowQueryBucket)
lastSQBInfo = currentSQBInfo
currentSQBInfo = make(SlowQueryBucket)
slowQueryLock.Unlock()
logutil.BgLogger().Info("Telemetry slow query stats, postReportSlowQueryStats finished")
}
2 changes: 0 additions & 2 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ func InitialRun(ctx sessionctx.Context, etcdClient *clientv3.Client) error {
if err != nil {
return err
}

initSlowQueryStats()
logutil.BgLogger().Info("Telemetry configuration", zap.String("endpoint", apiEndpoint), zap.Duration("report_interval", ReportInterval), zap.Bool("enabled", enabled))
return ReportUsageData(ctx, etcdClient)
}

0 comments on commit fbbada3

Please sign in to comment.