|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package scheduler |
| 16 | + |
| 17 | +import ( |
| 18 | + "strconv" |
| 19 | + "sync/atomic" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/pingcap/tidb/pkg/disttask/framework/proto" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +var subtaskCollector = newCollector() |
| 27 | + |
| 28 | +func init() { |
| 29 | + prometheus.MustRegister(subtaskCollector) |
| 30 | +} |
| 31 | + |
| 32 | +// Because the exec_id of a subtask may change, after all tasks |
| 33 | +// are successful, subtasks will be migrated from tidb_subtask_background |
| 34 | +// to tidb_subtask_background_history. In the above situation, |
| 35 | +// the built-in collector of Prometheus needs to delete the previously |
| 36 | +// added metrics, which is quite troublesome. |
| 37 | +// Therefore, a custom collector is used. |
| 38 | +type collector struct { |
| 39 | + subtaskInfo atomic.Pointer[[]*proto.Subtask] |
| 40 | + |
| 41 | + subtasks *prometheus.Desc |
| 42 | + subtaskDuration *prometheus.Desc |
| 43 | +} |
| 44 | + |
| 45 | +func newCollector() *collector { |
| 46 | + return &collector{ |
| 47 | + subtasks: prometheus.NewDesc( |
| 48 | + "tidb_disttask_subtasks", |
| 49 | + "Number of subtasks.", |
| 50 | + []string{"task_type", "task_id", "status", "exec_id"}, nil, |
| 51 | + ), |
| 52 | + subtaskDuration: prometheus.NewDesc( |
| 53 | + "tidb_disttask_subtask_duration", |
| 54 | + "Duration of subtasks in different states.", |
| 55 | + []string{"task_type", "task_id", "status", "subtask_id", "exec_id"}, nil, |
| 56 | + ), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Describe implements the prometheus.Collector interface. |
| 61 | +func (c *collector) Describe(ch chan<- *prometheus.Desc) { |
| 62 | + ch <- c.subtasks |
| 63 | + ch <- c.subtaskDuration |
| 64 | +} |
| 65 | + |
| 66 | +// Collect implements the prometheus.Collector interface. |
| 67 | +func (c *collector) Collect(ch chan<- prometheus.Metric) { |
| 68 | + p := c.subtaskInfo.Load() |
| 69 | + if p == nil { |
| 70 | + return |
| 71 | + } |
| 72 | + subtasks := *p |
| 73 | + |
| 74 | + // taskID => execID => state => cnt |
| 75 | + subtaskCnt := make(map[int64]map[string]map[proto.SubtaskState]int) |
| 76 | + taskType := make(map[int64]proto.TaskType) |
| 77 | + for _, subtask := range subtasks { |
| 78 | + if _, ok := subtaskCnt[subtask.TaskID]; !ok { |
| 79 | + subtaskCnt[subtask.TaskID] = make(map[string]map[proto.SubtaskState]int) |
| 80 | + } |
| 81 | + if _, ok := subtaskCnt[subtask.TaskID][subtask.ExecID]; !ok { |
| 82 | + subtaskCnt[subtask.TaskID][subtask.ExecID] = make(map[proto.SubtaskState]int) |
| 83 | + } |
| 84 | + |
| 85 | + subtaskCnt[subtask.TaskID][subtask.ExecID][subtask.State]++ |
| 86 | + taskType[subtask.TaskID] = subtask.Type |
| 87 | + |
| 88 | + c.setDistSubtaskDuration(ch, subtask) |
| 89 | + } |
| 90 | + for taskID, execIDMap := range subtaskCnt { |
| 91 | + for execID, stateMap := range execIDMap { |
| 92 | + for state, cnt := range stateMap { |
| 93 | + ch <- prometheus.MustNewConstMetric(c.subtasks, prometheus.GaugeValue, |
| 94 | + float64(cnt), |
| 95 | + taskType[taskID].String(), |
| 96 | + strconv.Itoa(int(taskID)), |
| 97 | + state.String(), |
| 98 | + execID, |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (c *collector) setDistSubtaskDuration(ch chan<- prometheus.Metric, subtask *proto.Subtask) { |
| 106 | + switch subtask.State { |
| 107 | + case proto.SubtaskStatePending: |
| 108 | + ch <- prometheus.MustNewConstMetric(c.subtaskDuration, prometheus.GaugeValue, |
| 109 | + time.Since(subtask.CreateTime).Seconds(), |
| 110 | + subtask.Type.String(), |
| 111 | + strconv.Itoa(int(subtask.TaskID)), |
| 112 | + subtask.State.String(), |
| 113 | + strconv.Itoa(int(subtask.ID)), |
| 114 | + subtask.ExecID, |
| 115 | + ) |
| 116 | + case proto.SubtaskStateRunning: |
| 117 | + ch <- prometheus.MustNewConstMetric(c.subtaskDuration, prometheus.GaugeValue, |
| 118 | + time.Since(subtask.StartTime).Seconds(), |
| 119 | + subtask.Type.String(), |
| 120 | + strconv.Itoa(int(subtask.TaskID)), |
| 121 | + subtask.State.String(), |
| 122 | + strconv.Itoa(int(subtask.ID)), |
| 123 | + subtask.ExecID, |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
0 commit comments