-
Notifications
You must be signed in to change notification settings - Fork 126
/
scheduler_metrics.go
227 lines (189 loc) · 6.99 KB
/
scheduler_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package scheduler
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/armadaproject/armada/internal/scheduler/configuration"
schedulercontext "github.com/armadaproject/armada/internal/scheduler/context"
)
const (
NAMESPACE = "armada"
SUBSYSTEM = "scheduler"
)
type SchedulerMetrics struct {
// Cycle time when scheduling, as leader.
scheduleCycleTime prometheus.Histogram
// Cycle time when reconciling, as leader or follower.
reconcileCycleTime prometheus.Histogram
mostRecentSchedulingRoundData schedulingRoundData
}
var scheduledJobsDesc = prometheus.NewDesc(
fmt.Sprintf("%s_%s_%s", NAMESPACE, SUBSYSTEM, "scheduled_jobs"),
"Number of jobs scheduled each round.",
[]string{
"queue",
"priority_class",
}, nil,
)
var preemptedJobsDesc = prometheus.NewDesc(
fmt.Sprintf("%s_%s_%s", NAMESPACE, SUBSYSTEM, "preempted_jobs"),
"Number of jobs preempted each round.",
[]string{
"queue",
"priority_class",
}, nil,
)
var consideredJobsDesc = prometheus.NewDesc(
fmt.Sprintf("%s_%s_%s", NAMESPACE, SUBSYSTEM, "considered_jobs"),
"Number of jobs considered in the most recent round per queue and pool.",
[]string{
"queue",
"pool",
}, nil,
)
var fairSharePerQueueDesc = prometheus.NewDesc(
fmt.Sprintf("%s_%s_%s", NAMESPACE, SUBSYSTEM, "fair_share"),
"Fair share of each queue and pool.",
[]string{
"queue",
"pool",
}, nil,
)
var actualSharePerQueueDesc = prometheus.NewDesc(
fmt.Sprintf("%s_%s_%s", NAMESPACE, SUBSYSTEM, "actual_share"),
"Actual share of each queue and pool.",
[]string{
"queue",
"pool",
}, nil,
)
func NewSchedulerMetrics(config configuration.SchedulerMetricsConfig) *SchedulerMetrics {
scheduleCycleTime := prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: NAMESPACE,
Subsystem: SUBSYSTEM,
Name: "schedule_cycle_times",
Help: "Cycle time when in a scheduling round.",
Buckets: prometheus.ExponentialBuckets(
config.ScheduleCycleTimeHistogramSettings.Start,
config.ScheduleCycleTimeHistogramSettings.Factor,
config.ScheduleCycleTimeHistogramSettings.Count),
},
)
reconcileCycleTime := prometheus.NewHistogram(
prometheus.HistogramOpts{
Namespace: NAMESPACE,
Subsystem: SUBSYSTEM,
Name: "reconcile_cycle_times",
Help: "Cycle time when outside of a scheduling round.",
Buckets: prometheus.ExponentialBuckets(
config.ReconcileCycleTimeHistogramSettings.Start,
config.ReconcileCycleTimeHistogramSettings.Factor,
config.ReconcileCycleTimeHistogramSettings.Count),
},
)
prometheus.MustRegister(scheduleCycleTime)
prometheus.MustRegister(reconcileCycleTime)
return &SchedulerMetrics{
scheduleCycleTime: scheduleCycleTime,
reconcileCycleTime: reconcileCycleTime,
}
}
func (m *SchedulerMetrics) ReportScheduleCycleTime(cycleTime time.Duration) {
m.scheduleCycleTime.Observe(float64(cycleTime.Milliseconds()))
}
func (m *SchedulerMetrics) ReportReconcileCycleTime(cycleTime time.Duration) {
m.reconcileCycleTime.Observe(float64(cycleTime.Milliseconds()))
}
func (m *SchedulerMetrics) ReportSchedulerResult(result SchedulerResult) {
currentSchedulingMetrics := schedulingRoundData{
queuePoolData: m.calculateQueuePoolMetrics(result.SchedulingContexts),
scheduledJobData: aggregateJobContexts(m.mostRecentSchedulingRoundData.scheduledJobData, result.ScheduledJobs),
preemptedJobData: aggregateJobContexts(m.mostRecentSchedulingRoundData.preemptedJobData, result.PreemptedJobs),
}
m.mostRecentSchedulingRoundData = currentSchedulingMetrics
}
func (m *SchedulerMetrics) Describe(desc chan<- *prometheus.Desc) {
desc <- scheduledJobsDesc
desc <- preemptedJobsDesc
desc <- consideredJobsDesc
desc <- fairSharePerQueueDesc
desc <- actualSharePerQueueDesc
}
func (m *SchedulerMetrics) Collect(metrics chan<- prometheus.Metric) {
schedulingRoundData := m.mostRecentSchedulingRoundData
schedulingRoundMetrics := generateSchedulerMetrics(schedulingRoundData)
for _, m := range schedulingRoundMetrics {
metrics <- m
}
}
func generateSchedulerMetrics(schedulingRoundData schedulingRoundData) []prometheus.Metric {
result := []prometheus.Metric{}
for key, value := range schedulingRoundData.queuePoolData {
result = append(result, prometheus.MustNewConstMetric(consideredJobsDesc, prometheus.GaugeValue, float64(value.numberOfJobsConsidered), key.queue, key.pool))
result = append(result, prometheus.MustNewConstMetric(fairSharePerQueueDesc, prometheus.GaugeValue, float64(value.fairShare), key.queue, key.pool))
result = append(result, prometheus.MustNewConstMetric(actualSharePerQueueDesc, prometheus.GaugeValue, float64(value.actualShare), key.queue, key.pool))
}
for key, value := range schedulingRoundData.scheduledJobData {
result = append(result, prometheus.MustNewConstMetric(scheduledJobsDesc, prometheus.CounterValue, float64(value), key.queue, key.priorityClass))
}
for key, value := range schedulingRoundData.preemptedJobData {
result = append(result, prometheus.MustNewConstMetric(preemptedJobsDesc, prometheus.CounterValue, float64(value), key.queue, key.priorityClass))
}
return result
}
// aggregateJobContexts takes a list of jobs and counts how many there are of each queue, priorityClass pair.
func aggregateJobContexts(previousSchedulingRoundData map[queuePriorityClassKey]int, jctxs []*schedulercontext.JobSchedulingContext) map[queuePriorityClassKey]int {
result := make(map[queuePriorityClassKey]int)
for _, jctx := range jctxs {
job := jctx.Job
key := queuePriorityClassKey{queue: job.Queue(), priorityClass: job.PriorityClassName()}
result[key] += 1
}
for key, value := range previousSchedulingRoundData {
_, present := result[key]
if present {
result[key] += value
} else {
result[key] = value
}
}
return result
}
func (metrics *SchedulerMetrics) calculateQueuePoolMetrics(schedulingContexts []*schedulercontext.SchedulingContext) map[queuePoolKey]queuePoolData {
result := make(map[queuePoolKey]queuePoolData)
for _, schedContext := range schedulingContexts {
totalCost := schedContext.TotalCost()
totalWeight := schedContext.WeightSum
pool := schedContext.Pool
for queue, queueContext := range schedContext.QueueSchedulingContexts {
key := queuePoolKey{queue: queue, pool: pool}
fairShare := queueContext.Weight / totalWeight
actualShare := schedContext.FairnessCostProvider.CostFromQueue(queueContext) / totalCost
result[key] = queuePoolData{
numberOfJobsConsidered: len(queueContext.UnsuccessfulJobSchedulingContexts) + len(queueContext.SuccessfulJobSchedulingContexts),
fairShare: fairShare,
actualShare: actualShare,
}
}
}
return result
}
type schedulingRoundData struct {
queuePoolData map[queuePoolKey]queuePoolData
scheduledJobData map[queuePriorityClassKey]int
preemptedJobData map[queuePriorityClassKey]int
}
type queuePriorityClassKey struct {
queue string
priorityClass string
}
type queuePoolKey struct {
queue string
pool string
}
type queuePoolData struct {
numberOfJobsConsidered int
actualShare float64
fairShare float64
}