-
Notifications
You must be signed in to change notification settings - Fork 132
/
job_utilisation_reporter.go
164 lines (148 loc) · 4.28 KB
/
job_utilisation_reporter.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
package utilisation
import (
"sync"
"time"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
clusterContext "github.com/armadaproject/armada/internal/executor/context"
"github.com/armadaproject/armada/internal/executor/domain"
"github.com/armadaproject/armada/internal/executor/reporter"
"github.com/armadaproject/armada/internal/executor/util"
)
type UtilisationEventReporter struct {
clusterContext clusterContext.ClusterContext
podUtilisation PodUtilisationService
eventReporter reporter.EventReporter
podInfo map[string]*podUtilisationInfo
dataAccessMutex sync.Mutex
reportingInterval time.Duration
legacyMode bool
}
type podUtilisationInfo struct {
lastReported time.Time
pod *v1.Pod
utilisationMax *domain.UtilisationData
}
func NewUtilisationEventReporter(
clusterContext clusterContext.ClusterContext,
podUtilisation PodUtilisationService,
eventReporter reporter.EventReporter,
reportingPeriod time.Duration,
legacyMode bool,
) *UtilisationEventReporter {
r := &UtilisationEventReporter{
clusterContext: clusterContext,
podUtilisation: podUtilisation,
eventReporter: eventReporter,
reportingInterval: reportingPeriod,
podInfo: map[string]*podUtilisationInfo{},
legacyMode: legacyMode,
}
clusterContext.AddPodEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
log.Errorf("Failed to process pod event due to it being an unexpected type. Failed to process %+v", obj)
return
}
if util.IsLegacyManagedPod(pod) != legacyMode {
return
}
go r.updatePod(pod)
},
UpdateFunc: func(oldObj, newObj interface{}) {
newPod, ok := newObj.(*v1.Pod)
if !ok {
log.Errorf("Failed to process pod event due to it being an unexpected type. Failed to process %+v", newObj)
return
}
if util.IsLegacyManagedPod(newPod) != legacyMode {
return
}
go r.updatePod(newPod)
},
DeleteFunc: func(obj interface{}) {
pod, ok := obj.(*v1.Pod)
if !ok {
log.Errorf("Failed to process pod event due to it being an unexpected type. Failed to process %+v", obj)
return
}
if util.IsLegacyManagedPod(pod) != legacyMode {
return
}
go r.deletePod(pod)
},
})
return r
}
func (r *UtilisationEventReporter) ReportUtilisationEvents() {
r.dataAccessMutex.Lock()
defer r.dataAccessMutex.Unlock()
now := time.Now()
reportingTime := now.Add(-r.reportingInterval)
for _, info := range r.podInfo {
currentUtilisation := r.podUtilisation.GetPodUtilisation(info.pod)
info.utilisationMax.Max(currentUtilisation)
if info.lastReported.Before(reportingTime) {
reported := r.reportUsage(info)
if reported {
info.lastReported = now
info.utilisationMax = domain.EmptyUtilisationData()
}
}
}
}
func (r *UtilisationEventReporter) updatePod(pod *v1.Pod) {
if !util.IsManagedPod(pod) {
return
}
r.dataAccessMutex.Lock()
defer r.dataAccessMutex.Unlock()
if pod.Status.Phase == v1.PodRunning {
_, exists := r.podInfo[pod.Name]
if !exists {
r.podInfo[pod.Name] = &podUtilisationInfo{
lastReported: time.Now(),
pod: pod,
utilisationMax: r.podUtilisation.GetPodUtilisation(pod),
}
}
}
if util.IsInTerminalState(pod) {
podInfo, exists := r.podInfo[pod.Name]
if exists {
r.reportUsage(podInfo)
delete(r.podInfo, pod.Name)
}
}
}
func (r *UtilisationEventReporter) deletePod(pod *v1.Pod) {
if !util.IsManagedPod(pod) {
return
}
r.dataAccessMutex.Lock()
defer r.dataAccessMutex.Unlock()
delete(r.podInfo, pod.Name)
}
func (r *UtilisationEventReporter) reportUsage(info *podUtilisationInfo) bool {
if info.utilisationMax.IsEmpty() {
return false
}
event := reporter.CreateJobUtilisationEvent(info.pod, info.utilisationMax, r.clusterContext.GetClusterId())
r.queueEventWithRetry(reporter.EventMessage{Event: event, JobRunId: util.ExtractJobRunId(info.pod)}, 3)
return true
}
func (r *UtilisationEventReporter) queueEventWithRetry(event reporter.EventMessage, retry int) {
var callback func(e error)
callback = func(e error) {
if e != nil {
log.Errorf("failed to report utilisation: %v", e)
retry--
if retry > 0 {
r.eventReporter.QueueEvent(event, callback)
}
}
}
r.eventReporter.QueueEvent(event, callback)
}