/
cleanup.go
574 lines (534 loc) · 18.5 KB
/
cleanup.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/juju/names"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
type cleanupKind string
const (
// SCHEMACHANGE: the names are expressive, the values not so much.
cleanupRelationSettings cleanupKind = "settings"
cleanupUnitsForDyingService cleanupKind = "units"
cleanupDyingUnit cleanupKind = "dyingUnit"
cleanupRemovedUnit cleanupKind = "removedUnit"
cleanupServicesForDyingModel cleanupKind = "services"
cleanupDyingMachine cleanupKind = "dyingMachine"
cleanupForceDestroyedMachine cleanupKind = "machine"
cleanupAttachmentsForDyingStorage cleanupKind = "storageAttachments"
cleanupAttachmentsForDyingVolume cleanupKind = "volumeAttachments"
cleanupAttachmentsForDyingFilesystem cleanupKind = "filesystemAttachments"
cleanupModelsForDyingController cleanupKind = "models"
cleanupMachinesForDyingModel cleanupKind = "modelMachines"
)
// cleanupDoc represents a potentially large set of documents that should be
// removed.
type cleanupDoc struct {
DocID string `bson:"_id"`
ModelUUID string `bson:"model-uuid"`
Kind cleanupKind
Prefix string
}
// newCleanupOp returns a txn.Op that creates a cleanup document with a unique
// id and the supplied kind and prefix.
func (st *State) newCleanupOp(kind cleanupKind, prefix string) txn.Op {
doc := &cleanupDoc{
DocID: st.docID(fmt.Sprint(bson.NewObjectId())),
ModelUUID: st.ModelUUID(),
Kind: kind,
Prefix: prefix,
}
return txn.Op{
C: cleanupsC,
Id: doc.DocID,
Insert: doc,
}
}
// NeedsCleanup returns true if documents previously marked for removal exist.
func (st *State) NeedsCleanup() (bool, error) {
cleanups, closer := st.getCollection(cleanupsC)
defer closer()
count, err := cleanups.Count()
if err != nil {
return false, err
}
return count > 0, nil
}
// Cleanup removes all documents that were previously marked for removal, if
// any such exist. It should be called periodically by at least one element
// of the system.
func (st *State) Cleanup() (err error) {
var doc cleanupDoc
cleanups, closer := st.getCollection(cleanupsC)
defer closer()
iter := cleanups.Find(nil).Iter()
defer closeIter(iter, &err, "reading cleanup document")
for iter.Next(&doc) {
var err error
logger.Debugf("running %q cleanup: %q", doc.Kind, doc.Prefix)
switch doc.Kind {
case cleanupRelationSettings:
err = st.cleanupRelationSettings(doc.Prefix)
case cleanupUnitsForDyingService:
err = st.cleanupUnitsForDyingService(doc.Prefix)
case cleanupDyingUnit:
err = st.cleanupDyingUnit(doc.Prefix)
case cleanupRemovedUnit:
err = st.cleanupRemovedUnit(doc.Prefix)
case cleanupServicesForDyingModel:
err = st.cleanupServicesForDyingModel()
case cleanupDyingMachine:
err = st.cleanupDyingMachine(doc.Prefix)
case cleanupForceDestroyedMachine:
err = st.cleanupForceDestroyedMachine(doc.Prefix)
case cleanupAttachmentsForDyingStorage:
err = st.cleanupAttachmentsForDyingStorage(doc.Prefix)
case cleanupAttachmentsForDyingVolume:
err = st.cleanupAttachmentsForDyingVolume(doc.Prefix)
case cleanupAttachmentsForDyingFilesystem:
err = st.cleanupAttachmentsForDyingFilesystem(doc.Prefix)
case cleanupModelsForDyingController:
err = st.cleanupModelsForDyingController()
case cleanupMachinesForDyingModel:
err = st.cleanupMachinesForDyingModel()
default:
handler, ok := cleanupHandlers[doc.Kind]
if !ok {
err = fmt.Errorf("unknown cleanup kind %q", doc.Kind)
} else {
persist := st.newPersistence()
err = handler(st, persist, doc.Prefix)
}
}
if err != nil {
logger.Warningf("cleanup failed: %v", err)
continue
}
ops := []txn.Op{{
C: cleanupsC,
Id: doc.DocID,
Remove: true,
}}
if err := st.runTransaction(ops); err != nil {
logger.Warningf("cannot remove empty cleanup document: %v", err)
}
}
return nil
}
// CleanupHandler is a function that state may call during cleanup
// to perform cleanup actions for some cleanup type.
type CleanupHandler func(st *State, persist Persistence, prefix string) error
var cleanupHandlers = map[cleanupKind]CleanupHandler{}
// RegisterCleanupHandler identifies the handler to use a given
// cleanup kind.
func RegisterCleanupHandler(kindStr string, handler CleanupHandler) error {
kind := cleanupKind(kindStr)
if _, ok := cleanupHandlers[kind]; ok {
return errors.NewAlreadyExists(nil, fmt.Sprintf("cleanup handler for %q already registered", kindStr))
}
cleanupHandlers[kind] = handler
return nil
}
func (st *State) cleanupRelationSettings(prefix string) error {
settings, closer := st.getCollection(settingsC)
defer closer()
// Documents marked for cleanup are not otherwise referenced in the
// system, and will not be under watch, and are therefore safe to
// delete directly.
settingsW := settings.Writeable()
sel := bson.D{{"_id", bson.D{{"$regex", "^" + st.docID(prefix)}}}}
if count, err := settingsW.Find(sel).Count(); err != nil {
return fmt.Errorf("cannot detect cleanup targets: %v", err)
} else if count != 0 {
if _, err := settingsW.RemoveAll(sel); err != nil {
return fmt.Errorf("cannot remove documents marked for cleanup: %v", err)
}
}
return nil
}
// cleanupModelsForDyingController sets all models to dying, if
// they are not already Dying or Dead. It's expected to be used when a
// controller is destroyed.
func (st *State) cleanupModelsForDyingController() (err error) {
models, err := st.AllModels()
if err != nil {
return errors.Trace(err)
}
for _, env := range models {
if env.Life() == Alive {
if err := env.Destroy(); err != nil {
return errors.Trace(err)
}
}
}
return nil
}
// cleanupMachinesForDyingModel sets all non-manager, non-manual
// machines to Dying, if they are not already Dying or Dead. It's expected to
// be used when a model is destroyed.
func (st *State) cleanupMachinesForDyingModel() (err error) {
// This won't miss machines, because a Dying model cannot have
// machines added to it. But we do have to remove the machines themselves
// via individual transactions, because they could be in any state at all.
machines, err := st.AllMachines()
if err != nil {
return errors.Trace(err)
}
for _, m := range machines {
if m.IsManager() {
continue
}
if _, isContainer := m.ParentId(); isContainer {
continue
}
manual, err := m.IsManual()
if err != nil {
return err
} else if manual {
continue
}
err = m.ForceDestroy()
if err != nil {
return errors.Trace(err)
}
}
return nil
}
// cleanupServicesForDyingModel sets all services to Dying, if they are
// not already Dying or Dead. It's expected to be used when a model is
// destroyed.
func (st *State) cleanupServicesForDyingModel() (err error) {
// This won't miss services, because a Dying model cannot have
// services added to it. But we do have to remove the services themselves
// via individual transactions, because they could be in any state at all.
services, closer := st.getCollection(servicesC)
defer closer()
service := Service{st: st}
sel := bson.D{{"life", Alive}}
iter := services.Find(sel).Iter()
defer closeIter(iter, &err, "reading service document")
for iter.Next(&service.doc) {
if err := service.Destroy(); err != nil {
return err
}
}
return nil
}
// cleanupUnitsForDyingService sets all units with the given prefix to Dying,
// if they are not already Dying or Dead. It's expected to be used when a
// service is destroyed.
func (st *State) cleanupUnitsForDyingService(serviceName string) (err error) {
// This won't miss units, because a Dying service cannot have units added
// to it. But we do have to remove the units themselves via individual
// transactions, because they could be in any state at all.
units, closer := st.getCollection(unitsC)
defer closer()
// TODO(mjs) - remove this post v1.21
// Older versions of the code put a trailing forward slash on the
// end of the service name. Remove it here in case a pre-upgrade
// cleanup document is seen.
serviceName = strings.TrimSuffix(serviceName, "/")
unit := Unit{st: st}
sel := bson.D{{"service", serviceName}, {"life", Alive}}
iter := units.Find(sel).Iter()
defer closeIter(iter, &err, "reading unit document")
for iter.Next(&unit.doc) {
if err := unit.Destroy(); err != nil {
return err
}
}
return nil
}
// cleanupDyingUnit marks resources owned by the unit as dying, to ensure
// they are cleaned up as well.
func (st *State) cleanupDyingUnit(name string) error {
unit, err := st.Unit(name)
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
// Mark the unit as departing from its joined relations, allowing
// related units to start converging to a state in which that unit
// is gone as quickly as possible.
relations, err := unit.RelationsJoined()
if err != nil {
return err
}
for _, relation := range relations {
relationUnit, err := relation.Unit(unit)
if errors.IsNotFound(err) {
continue
} else if err != nil {
return err
}
if err := relationUnit.PrepareLeaveScope(); err != nil {
return err
}
}
// Mark storage attachments as dying, so that they are detached
// and removed from state, allowing the unit to terminate.
storageAttachments, err := st.UnitStorageAttachments(unit.UnitTag())
if err != nil {
return err
}
for _, storageAttachment := range storageAttachments {
err := st.DestroyStorageAttachment(
storageAttachment.StorageInstance(), unit.UnitTag(),
)
if errors.IsNotFound(err) {
continue
} else if err != nil {
return err
}
}
return nil
}
// cleanupRemovedUnit takes care of all the final cleanup required when
// a unit is removed.
func (st *State) cleanupRemovedUnit(unitId string) error {
actions, err := st.matchingActionsByReceiverId(unitId)
if err != nil {
return err
}
cancelled := ActionResults{Status: ActionCancelled, Message: "unit removed"}
for _, action := range actions {
if _, err = action.Finish(cancelled); err != nil {
return err
}
}
return nil
}
// cleanupDyingMachine marks resources owned by the machine as dying, to ensure
// they are cleaned up as well.
func (st *State) cleanupDyingMachine(machineId string) error {
machine, err := st.Machine(machineId)
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
return cleanupDyingMachineResources(machine)
}
// cleanupForceDestroyedMachine systematically destroys and removes all entities
// that depend upon the supplied machine, and removes the machine from state. It's
// expected to be used in response to destroy-machine --force.
func (st *State) cleanupForceDestroyedMachine(machineId string) error {
machine, err := st.Machine(machineId)
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
if err := cleanupDyingMachineResources(machine); err != nil {
return err
}
// In an ideal world, we'd call machine.Destroy() here, and thus prevent
// new dependencies being added while we clean up the ones we know about.
// But machine destruction is unsophisticated, and doesn't allow for
// destruction while dependencies exist; so we just have to deal with that
// possibility below.
if err := st.cleanupContainers(machine); err != nil {
return err
}
for _, unitName := range machine.doc.Principals {
if err := st.obliterateUnit(unitName); err != nil {
return err
}
}
// We need to refresh the machine at this point, because the local copy
// of the document will not reflect changes caused by the unit cleanups
// above, and may thus fail immediately.
if err := machine.Refresh(); errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
// TODO(fwereade): 2013-11-11 bug 1250104
// If this fails, it's *probably* due to a race in which new dependencies
// were added while we cleaned up the old ones. If the cleanup doesn't run
// again -- which it *probably* will anyway -- the issue can be resolved by
// force-destroying the machine again; that's better than adding layer
// upon layer of complication here.
if err := machine.EnsureDead(); err != nil {
return err
}
removePortsOps, err := machine.removePortsOps()
if err != nil {
return err
}
return st.runTransaction(removePortsOps)
// Note that we do *not* remove the machine entirely: we leave it for the
// provisioner to clean up, so that we don't end up with an unreferenced
// instance that would otherwise be ignored when in provisioner-safe-mode.
}
// cleanupContainers recursively calls cleanupForceDestroyedMachine on the supplied
// machine's containers, and removes them from state entirely.
func (st *State) cleanupContainers(machine *Machine) error {
containerIds, err := machine.Containers()
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
for _, containerId := range containerIds {
if err := st.cleanupForceDestroyedMachine(containerId); err != nil {
return err
}
container, err := st.Machine(containerId)
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
if err := container.Remove(); err != nil {
return err
}
}
return nil
}
func cleanupDyingMachineResources(m *Machine) error {
volumeAttachments, err := m.st.MachineVolumeAttachments(m.MachineTag())
if err != nil {
return errors.Annotate(err, "getting machine volume attachments")
}
for _, va := range volumeAttachments {
if err := m.st.DetachVolume(va.Machine(), va.Volume()); err != nil {
if IsContainsFilesystem(err) {
// The volume will be destroyed when the
// contained filesystem is removed, whose
// destruction is initiated below.
continue
}
return errors.Trace(err)
}
}
filesystemAttachments, err := m.st.MachineFilesystemAttachments(m.MachineTag())
if err != nil {
return errors.Annotate(err, "getting machine filesystem attachments")
}
for _, fsa := range filesystemAttachments {
if err := m.st.DetachFilesystem(fsa.Machine(), fsa.Filesystem()); err != nil {
return errors.Trace(err)
}
}
return nil
}
// obliterateUnit removes a unit from state completely. It is not safe or
// sane to obliterate any unit in isolation; its only reasonable use is in
// the context of machine obliteration, in which we can be sure that unclean
// shutdown of units is not going to leave a machine in a difficult state.
func (st *State) obliterateUnit(unitName string) error {
unit, err := st.Unit(unitName)
if errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
// Unlike the machine, we *can* always destroy the unit, and (at least)
// prevent further dependencies being added. If we're really lucky, the
// unit will be removed immediately.
if err := unit.Destroy(); err != nil {
return errors.Annotatef(err, "cannot destroy unit %q", unitName)
}
if err := unit.Refresh(); errors.IsNotFound(err) {
return nil
} else if err != nil {
return err
}
for _, subName := range unit.SubordinateNames() {
if err := st.obliterateUnit(subName); err != nil {
return err
}
}
if err := unit.EnsureDead(); err != nil {
return err
}
return unit.Remove()
}
// cleanupAttachmentsForDyingStorage sets all storage attachments related
// to the specified storage instance to Dying, if they are not already Dying
// or Dead. It's expected to be used when a storage instance is destroyed.
func (st *State) cleanupAttachmentsForDyingStorage(storageId string) (err error) {
storageTag := names.NewStorageTag(storageId)
// This won't miss attachments, because a Dying storage instance cannot
// have attachments added to it. But we do have to remove the attachments
// themselves via individual transactions, because they could be in
// any state at all.
coll, closer := st.getCollection(storageAttachmentsC)
defer closer()
var doc storageAttachmentDoc
fields := bson.D{{"unitid", 1}}
iter := coll.Find(bson.D{{"storageid", storageId}}).Select(fields).Iter()
defer closeIter(iter, &err, "reading storage attachment document")
for iter.Next(&doc) {
unitTag := names.NewUnitTag(doc.Unit)
if err := st.DestroyStorageAttachment(storageTag, unitTag); err != nil {
return errors.Annotate(err, "destroying storage attachment")
}
}
return nil
}
// cleanupAttachmentsForDyingVolume sets all volume attachments related
// to the specified volume to Dying, if they are not already Dying or
// Dead. It's expected to be used when a volume is destroyed.
func (st *State) cleanupAttachmentsForDyingVolume(volumeId string) (err error) {
volumeTag := names.NewVolumeTag(volumeId)
// This won't miss attachments, because a Dying volume cannot have
// attachments added to it. But we do have to remove the attachments
// themselves via individual transactions, because they could be in
// any state at all.
coll, closer := st.getCollection(volumeAttachmentsC)
defer closer()
var doc volumeAttachmentDoc
fields := bson.D{{"machineid", 1}}
iter := coll.Find(bson.D{{"volumeid", volumeId}}).Select(fields).Iter()
defer closeIter(iter, &err, "reading volume attachment document")
for iter.Next(&doc) {
machineTag := names.NewMachineTag(doc.Machine)
if err := st.DetachVolume(machineTag, volumeTag); err != nil {
return errors.Annotate(err, "destroying volume attachment")
}
}
return nil
}
// cleanupAttachmentsForDyingFilesystem sets all filesystem attachments related
// to the specified filesystem to Dying, if they are not already Dying or
// Dead. It's expected to be used when a filesystem is destroyed.
func (st *State) cleanupAttachmentsForDyingFilesystem(filesystemId string) (err error) {
filesystemTag := names.NewFilesystemTag(filesystemId)
// This won't miss attachments, because a Dying filesystem cannot have
// attachments added to it. But we do have to remove the attachments
// themselves via individual transactions, because they could be in
// any state at all.
coll, closer := st.getCollection(filesystemAttachmentsC)
defer closer()
var doc filesystemAttachmentDoc
fields := bson.D{{"machineid", 1}}
iter := coll.Find(bson.D{{"filesystemid", filesystemId}}).Select(fields).Iter()
defer closeIter(iter, &err, "reading filesystem attachment document")
for iter.Next(&doc) {
machineTag := names.NewMachineTag(doc.Machine)
if err := st.DetachFilesystem(machineTag, filesystemTag); err != nil {
return errors.Annotate(err, "destroying filesystem attachment")
}
}
return nil
}
func closeIter(iter *mgo.Iter, errOut *error, message string) {
err := iter.Close()
if err == nil {
return
}
err = errors.Annotate(err, message)
if *errOut == nil {
*errOut = err
return
}
logger.Errorf("%v", err)
}