forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containercollector.go
374 lines (307 loc) · 10.2 KB
/
containercollector.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package gc
import (
"errors"
"time"
"code.cloudfoundry.org/garden"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/db"
"github.com/concourse/atc/metric"
"github.com/concourse/atc/worker"
)
const HijackedContainerTimeout = 5 * time.Minute
var containerCollectorFailedErr = errors.New("container collector failed")
type containerCollector struct {
rootLogger lager.Logger
containerRepository db.ContainerRepository
jobRunner WorkerJobRunner
}
func NewContainerCollector(
logger lager.Logger,
containerRepository db.ContainerRepository,
jobRunner WorkerJobRunner,
) Collector {
return &containerCollector{
rootLogger: logger,
containerRepository: containerRepository,
jobRunner: jobRunner,
}
}
type job struct {
JobName string
RunFunc func(worker.Worker)
}
func (j *job) Name() string {
return j.JobName
}
func (j *job) Run(w worker.Worker) {
j.RunFunc(w)
}
func (c *containerCollector) Run() error {
logger := c.rootLogger.Session("run")
logger.Debug("start")
defer logger.Debug("done")
var err error
orphanedErr := c.cleanupOrphanedContainers(logger.Session("orphaned-containers"))
if orphanedErr != nil {
c.rootLogger.Error("container-collector", orphanedErr)
err = containerCollectorFailedErr
}
failedErr := c.cleanupFailedContainers(logger.Session("failed-containers"))
if failedErr != nil {
c.rootLogger.Error("container-collector", failedErr)
err = containerCollectorFailedErr
}
return err
}
func (c *containerCollector) cleanupFailedContainers(logger lager.Logger) error {
failedContainers, err := c.containerRepository.FindFailedContainers()
if err != nil {
logger.Error("failed-to-find-failed-containers-for-deletion", err)
return err
}
failedContainerHandles := []string{}
var failedContainerstoDestroy = []destroyableContainer{}
if len(failedContainers) > 0 {
for _, container := range failedContainers {
failedContainerHandles = append(failedContainerHandles, container.Handle())
failedContainerstoDestroy = append(failedContainerstoDestroy, container)
}
}
logger.Debug("found-failed-containers-for-deletion", lager.Data{
"failed-containers": failedContainerHandles,
})
metric.FailedContainersToBeGarbageCollected{
Containers: len(failedContainerHandles),
}.Emit(logger)
destroyDBContainers(logger, failedContainerstoDestroy)
return nil
}
func (c *containerCollector) cleanupOrphanedContainers(logger lager.Logger) error {
creatingContainers, createdContainers, destroyingContainers, err := c.containerRepository.FindOrphanedContainers()
if err != nil {
logger.Error("failed-to-get-orphaned-containers-for-deletion", err)
return err
}
creatingContainerHandles := []string{}
createdContainerHandles := []string{}
destroyingContainerHandles := []string{}
if len(creatingContainers) > 0 {
for _, container := range creatingContainers {
creatingContainerHandles = append(creatingContainerHandles, container.Handle())
}
}
if len(createdContainers) > 0 {
for _, container := range createdContainers {
createdContainerHandles = append(createdContainerHandles, container.Handle())
}
}
if len(destroyingContainers) > 0 {
for _, container := range destroyingContainers {
destroyingContainerHandles = append(destroyingContainerHandles, container.Handle())
}
}
logger.Debug("found-orphaned-containers-for-deletion", lager.Data{
"creating-containers": creatingContainerHandles,
"created-containers": createdContainerHandles,
"destroying-containers": destroyingContainerHandles,
})
metric.CreatingContainersToBeGarbageCollected{
Containers: len(creatingContainerHandles),
}.Emit(logger)
metric.CreatedContainersToBeGarbageCollected{
Containers: len(createdContainerHandles),
}.Emit(logger)
metric.DestroyingContainersToBeGarbageCollected{
Containers: len(destroyingContainerHandles),
}.Emit(logger)
var workerCreatedContainers = make(map[string][]db.CreatedContainer)
for _, createdContainer := range createdContainers {
containers, ok := workerCreatedContainers[createdContainer.WorkerName()]
if ok {
// update existing array
containers = append(containers, createdContainer)
workerCreatedContainers[createdContainer.WorkerName()] = containers
} else {
// create new array
workerCreatedContainers[createdContainer.WorkerName()] = []db.CreatedContainer{createdContainer}
}
}
for worker, createdContainers := range workerCreatedContainers {
// prevent closure from capturing last value of loop
c.jobRunner.Try(logger,
worker,
&job{
JobName: worker,
RunFunc: destroyCreatedContainers(logger, createdContainers),
},
)
}
var workerContainers = make(map[string][]db.DestroyingContainer)
for _, destroyingContainer := range destroyingContainers {
containers, ok := workerContainers[destroyingContainer.WorkerName()]
if ok {
// update existing array
containers = append(containers, destroyingContainer)
workerContainers[destroyingContainer.WorkerName()] = containers
} else {
// create new array
workerContainers[destroyingContainer.WorkerName()] = []db.DestroyingContainer{destroyingContainer}
}
}
for worker, destroyingContainers := range workerContainers {
c.jobRunner.Try(logger,
worker,
&job{
JobName: worker,
RunFunc: destroyDestroyingContainers(logger, destroyingContainers),
},
)
}
return nil
}
func destroyCreatedContainers(logger lager.Logger, containers []db.CreatedContainer) func(worker.Worker) {
return func(workerClient worker.Worker) {
destroyingContainers := []db.DestroyingContainer{}
for _, container := range containers {
var destroyingContainer db.DestroyingContainer
if container.IsHijacked() {
cLog := logger.Session("mark-hijacked-container", lager.Data{
"container": container.Handle(),
"worker": workerClient.Name(),
})
var err error
destroyingContainer, err = markHijackedContainerAsDestroying(cLog, container, workerClient.GardenClient())
if err != nil {
cLog.Error("failed-to-transition", err)
return
}
} else {
cLog := logger.Session("mark-created-as-destroying", lager.Data{
"container": container.Handle(),
"worker": workerClient.Name(),
})
var err error
destroyingContainer, err = container.Destroying()
if err != nil {
cLog.Error("failed-to-transition", err)
return
}
}
if destroyingContainer != nil {
destroyingContainers = append(destroyingContainers, destroyingContainer)
}
}
tryToDestroyContainers(logger.Session("destroy-containers"), destroyingContainers, workerClient)
}
}
func destroyDestroyingContainers(logger lager.Logger, containers []db.DestroyingContainer) func(worker.Worker) {
return func(workerClient worker.Worker) {
cLog := logger.Session("destroy-containers-on-worker", lager.Data{
"worker": workerClient.Name(),
})
tryToDestroyContainers(cLog, containers, workerClient)
}
}
func markHijackedContainerAsDestroying(
logger lager.Logger,
hijackedContainer db.CreatedContainer,
gardenClient garden.Client,
) (db.DestroyingContainer, error) {
gardenContainer, found, err := findContainer(gardenClient, hijackedContainer.Handle())
if err != nil {
logger.Error("failed-to-lookup-container-in-garden", err)
return nil, err
}
if !found {
logger.Debug("hijacked-container-not-found-in-garden")
destroyingContainer, err := hijackedContainer.Destroying()
if err != nil {
logger.Error("failed-to-mark-container-as-destroying", err)
return nil, err
}
return destroyingContainer, nil
}
err = gardenContainer.SetGraceTime(HijackedContainerTimeout)
if err != nil {
logger.Error("failed-to-set-grace-time-on-hijacked-container", err)
return nil, err
}
_, err = hijackedContainer.Discontinue()
if err != nil {
logger.Error("failed-to-mark-container-as-destroying", err)
return nil, err
}
return nil, nil
}
func tryToDestroyContainers(
logger lager.Logger,
containers []db.DestroyingContainer,
workerClient worker.Worker,
) {
logger.Debug("start")
defer logger.Debug("done")
gardenDeleteHandles := []string{}
gardenDeleteContainers := []destroyableContainer{}
dbDeleteContainers := []destroyableContainer{}
gardenClient := workerClient.GardenClient()
reaperClient := workerClient.ReaperClient()
for _, container := range containers {
if container.IsDiscontinued() {
cLog := logger.Session("discontinued", lager.Data{"handle": container.Handle()})
_, found, err := findContainer(gardenClient, container.Handle())
if err != nil {
cLog.Error("failed-to-lookup-container-in-garden", err)
}
if found {
cLog.Debug("still-present-in-garden")
} else {
cLog.Debug("container-no-longer-present-in-garden")
dbDeleteContainers = append(dbDeleteContainers, container)
}
} else {
gardenDeleteHandles = append(gardenDeleteHandles, container.Handle())
gardenDeleteContainers = append(gardenDeleteContainers, container)
}
}
if len(gardenDeleteHandles) > 0 {
err := reaperClient.DestroyContainers(gardenDeleteHandles)
if err != nil {
logger.Error("failed-to-destroy-garden-containers", err, lager.Data{"handlers": gardenDeleteHandles})
} else {
logger.Debug("completed-destroyed-in-garden", lager.Data{"handlers": gardenDeleteHandles})
dbDeleteContainers = append(dbDeleteContainers, gardenDeleteContainers...)
}
}
destroyDBContainers(logger, dbDeleteContainers)
logger.Debug("destroyed-in-db")
}
type destroyableContainer interface {
Destroy() (bool, error)
}
func destroyDBContainers(logger lager.Logger, dbContainers []destroyableContainer) {
logger.Debug("destroying-start-in-db", lager.Data{"length": len(dbContainers)})
defer logger.Debug("destroying-done-in-db")
for _, dbContainer := range dbContainers {
destroyed, err := dbContainer.Destroy()
if err != nil {
logger.Error("failed-to-destroy-database-container", err)
continue
}
if !destroyed {
logger.Info("could-not-destroy-database-container")
continue
}
metric.ContainersDeleted.Inc()
logger.Debug("destroyed-container-in-db")
}
}
func findContainer(gardenClient garden.Client, handle string) (garden.Container, bool, error) {
gardenContainer, err := gardenClient.Lookup(handle)
if err != nil {
if _, ok := err.(garden.ContainerNotFoundError); ok {
return nil, false, nil
}
return nil, false, err
}
return gardenContainer, true, nil
}