forked from evergreen-ci/evergreen
/
task_monitor_execution_timeout.go
279 lines (247 loc) · 7.53 KB
/
task_monitor_execution_timeout.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package units
import (
"context"
"fmt"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/apimodels"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/util"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/dependency"
"github.com/mongodb/amboy/job"
"github.com/mongodb/amboy/registry"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/mongodb/grip/sometimes"
"github.com/pkg/errors"
)
const (
heartbeatTimeoutThreshold = 7 * time.Minute
taskExecutionTimeoutJobName = "task-execution-timeout"
taskExecutionTimeoutPopulationJobName = "task-execution-timeout-populate"
maxAttempts = 10
)
func init() {
registry.AddJobType(taskExecutionTimeoutJobName, func() amboy.Job {
return makeTaskExecutionTimeoutMonitorJob()
})
registry.AddJobType(taskExecutionTimeoutPopulationJobName, func() amboy.Job {
return makeTaskExecutionTimeoutPopulateJob()
})
}
type taskExecutionTimeoutJob struct {
Task string `bson:"task_id"`
Execution int `bson:"execution"`
Attempt int `bson:"attempt"`
successful bool
job.Base `bson:"metadata" json:"metadata" yaml:"metadata"`
}
func makeTaskExecutionTimeoutMonitorJob() *taskExecutionTimeoutJob {
j := &taskExecutionTimeoutJob{
Base: job.Base{
JobType: amboy.JobType{
Name: taskExecutionTimeoutJobName,
Version: 0,
},
},
}
j.SetDependency(dependency.NewAlways())
return j
}
func NewTaskExecutionMonitorJob(taskID string, execution int, attempt int, ts string) amboy.Job {
j := makeTaskExecutionTimeoutMonitorJob()
j.Task = taskID
j.Execution = execution
j.Attempt = attempt
j.SetID(fmt.Sprintf("%s.%s.%d.attempt-%d.%s", taskExecutionTimeoutJobName, taskID, execution, attempt, ts))
return j
}
func (j *taskExecutionTimeoutJob) Run(ctx context.Context) {
defer j.MarkComplete()
flags, err := evergreen.GetServiceFlags()
if err != nil {
j.AddError(err)
return
}
if flags.MonitorDisabled {
grip.InfoWhen(sometimes.Percent(evergreen.DegradedLoggingPercent), message.Fields{
"message": "monitor is disabled",
"operation": j.Type().Name,
"impact": "skipping task heartbeat cleanup job",
"mode": "degraded",
})
return
}
defer j.tryRequeue(ctx)
t, err := task.FindOneId(j.Task)
if err != nil {
j.AddError(errors.Wrap(err, "error finding task"))
return
}
if t == nil {
j.AddError(errors.New("no task found"))
return
}
// if the task has heartbeat since this job was queued, let it run
if t.LastHeartbeat.Add(heartbeatTimeoutThreshold).After(time.Now()) {
j.successful = true
return
}
msg := message.Fields{
"operation": j.Type().Name,
"id": j.ID(),
"task": t.Id,
"host": t.HostId,
}
err = cleanUpTimedOutTask(t)
if err != nil {
grip.Warning(message.WrapError(err, msg))
j.AddError(err)
} else {
j.successful = true
}
grip.Debug(msg)
}
func (j *taskExecutionTimeoutJob) tryRequeue(ctx context.Context) {
if j.successful || j.Attempt >= maxAttempts {
return
}
ts := util.RoundPartOfHour(15)
newJob := NewTaskExecutionMonitorJob(j.Task, j.Execution, j.Attempt+1, ts.Format(tsFormat))
newJob.UpdateTimeInfo(amboy.JobTimeInfo{
WaitUntil: time.Now().Add(time.Minute),
})
err := evergreen.GetEnvironment().RemoteQueue().Put(ctx, newJob)
grip.Error(message.WrapError(err, message.Fields{
"message": "failed to requeue task timeout job",
"task": j.Task,
"job": j.ID(),
"attempts": j.Attempt,
}))
j.AddError(err)
}
// function to clean up a single task
func cleanUpTimedOutTask(t *task.Task) error {
// get the host for the task
host, err := host.FindOne(host.ById(t.HostId))
if err != nil {
return errors.Wrapf(err, "error finding host %s for task %s",
t.HostId, t.Id)
}
// if there's no relevant host and the task is not a display task, something went wrong
if host == nil {
if !t.DisplayOnly {
grip.Error(message.Fields{
"message": "no entry found for host",
"task": t.Id,
"host": t.HostId,
"operation": "cleanup timed out task",
})
}
return errors.WithStack(t.MarkUnscheduled())
}
// For a single-host task group, if a task fails, block and dequeue later tasks in that group.
if t.TaskGroup != "" && t.TaskGroupMaxHosts == 1 && t.Status != evergreen.TaskSucceeded {
if err = model.BlockTaskGroupTasks(t.Id); err != nil {
grip.Error(message.WrapError(err, message.Fields{
"message": "problem blocking task group tasks",
"task_id": t.Id,
}))
}
grip.Debug(message.Fields{
"message": "blocked task group tasks for task",
"task_id": t.Id,
})
}
// if the host still has the task as its running task, clear it.
if host.RunningTask == t.Id {
// clear out the host's running task
if err = host.ClearRunningAndSetLastTask(t); err != nil {
return errors.Wrapf(err, "error clearing running task %s from host %s", t.Id, host.Id)
}
}
detail := &apimodels.TaskEndDetail{
Description: task.AgentHeartbeat,
Type: evergreen.CommandTypeSystem,
TimedOut: true,
Status: evergreen.TaskFailed,
}
// try to reset the task
if t.IsPartOfDisplay() {
if err = t.DisplayTask.SetResetWhenFinished(); err != nil {
return errors.Wrap(err, "can't mark display task for reset")
}
return errors.Wrap(model.MarkEnd(t, "monitor", time.Now(), detail, false, &model.StatusChanges{}), "error marking task ended")
}
return errors.Wrapf(model.TryResetTask(t.Id, "", "monitor", detail), "error trying to reset task %s", t.Id)
}
////////////////////////////////////////////////////////////////////////
//
// Population Job
type taskExecutionTimeoutPopulationJob struct {
job.Base `bson:"metadata" json:"metadata" yaml:"metadata"`
}
func makeTaskExecutionTimeoutPopulateJob() *taskExecutionTimeoutPopulationJob {
j := &taskExecutionTimeoutPopulationJob{
Base: job.Base{
JobType: amboy.JobType{
Name: taskExecutionTimeoutPopulationJobName,
Version: 0,
},
},
}
j.SetDependency(dependency.NewAlways())
return j
}
func NewTaskExecutionMonitorPopulateJob(id string) amboy.Job {
j := makeTaskExecutionTimeoutPopulateJob()
j.SetID(fmt.Sprintf("%s.%s", j.Type().Name, id))
return j
}
func (j *taskExecutionTimeoutPopulationJob) Run(ctx context.Context) {
defer j.MarkComplete()
flags, err := evergreen.GetServiceFlags()
if err != nil {
j.AddError(err)
return
}
if flags.MonitorDisabled {
grip.InfoWhen(sometimes.Percent(evergreen.DegradedLoggingPercent), message.Fields{
"message": "monitor is disabled",
"operation": j.Type().Name,
"impact": "skipping task heartbeat cleanup job",
"mode": "degraded",
})
return
}
queue := evergreen.GetEnvironment().RemoteQueue()
taskIDs := map[string]int{}
tasks, err := host.FindStaleRunningTasks(heartbeatTimeoutThreshold)
if err != nil {
j.AddError(errors.Wrap(err, "error finding tasks with timed-out or stale heartbeats"))
return
}
for _, t := range tasks {
taskIDs[t.Id] = t.Execution
}
tasks, err = host.StaleRunningTaskIDs(heartbeatTimeoutThreshold)
if err != nil {
j.AddError(errors.Wrap(err, "error finding tasks with timed-out or stale heartbeats"))
return
}
for _, t := range tasks {
taskIDs[t.Id] = t.Execution
}
for id, execution := range taskIDs {
ts := util.RoundPartOfHour(15)
j.AddError(queue.Put(ctx, NewTaskExecutionMonitorJob(id, execution, 1, ts.Format(tsFormat))))
}
grip.Info(message.Fields{
"operation": "task-execution-timeout-populate",
"num_tasks": len(tasks),
"errors": j.HasErrors(),
})
}