forked from cloudfoundry/bbs
/
evacuation_controller.go
460 lines (379 loc) · 14.8 KB
/
evacuation_controller.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
package controllers
import (
"context"
"code.cloudfoundry.org/auctioneer"
"code.cloudfoundry.org/bbs/db"
"code.cloudfoundry.org/bbs/events"
"code.cloudfoundry.org/bbs/events/calculator"
"code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/lager"
)
type EvacuationController struct {
db db.EvacuationDB
actualLRPDB db.ActualLRPDB
suspectLRPDB db.SuspectDB
desiredLRPDB db.DesiredLRPDB
auctioneerClient auctioneer.Client
actualHub events.Hub
actualLRPInstanceHub events.Hub
}
func NewEvacuationController(
db db.EvacuationDB,
actualLRPDB db.ActualLRPDB,
suspectLRPDB db.SuspectDB,
desiredLRPDB db.DesiredLRPDB,
auctioneerClient auctioneer.Client,
actualHub events.Hub,
actualLRPInstanceHub events.Hub,
) *EvacuationController {
return &EvacuationController{
db: db,
actualLRPDB: actualLRPDB,
suspectLRPDB: suspectLRPDB,
desiredLRPDB: desiredLRPDB,
auctioneerClient: auctioneerClient,
actualHub: actualHub,
actualLRPInstanceHub: actualLRPInstanceHub,
}
}
func (h *EvacuationController) RemoveEvacuatingActualLRP(ctx context.Context, logger lager.Logger, actualLRPKey *models.ActualLRPKey, actualLRPInstanceKey *models.ActualLRPInstanceKey) error {
actualLRPs, err := h.actualLRPDB.ActualLRPs(ctx, logger, models.ActualLRPFilter{ProcessGuid: actualLRPKey.ProcessGuid, Index: &actualLRPKey.Index})
if err != nil {
return err
}
eventCalculator := calculator.ActualLRPEventCalculator{
ActualLRPGroupHub: h.actualHub,
ActualLRPInstanceHub: h.actualLRPInstanceHub,
}
newLRPs := make([]*models.ActualLRP, len(actualLRPs))
copy(newLRPs, actualLRPs)
defer func() {
go eventCalculator.EmitEvents(actualLRPs, newLRPs)
}()
lrp := lookupLRPInSlice(actualLRPs, actualLRPInstanceKey)
if lrp == nil {
logger.Debug("actual-lrp-not-found", lager.Data{"guid": actualLRPKey.ProcessGuid, "index": actualLRPKey.Index})
return models.ErrResourceNotFound
}
if lrp.Presence != models.ActualLRP_Evacuating {
logger.Info("evacuating-lrp-is-empty")
return models.ErrResourceNotFound
}
evacuatingLRPLogData := lager.Data{
"process-guid": actualLRPKey.ProcessGuid,
"index": actualLRPKey.Index,
"instance-key": actualLRPInstanceKey,
}
instance := findWithPresence(actualLRPs, models.ActualLRP_Ordinary)
if instance != nil {
evacuatingLRPLogData["replacement-lrp-instance-key"] = instance.ActualLRPInstanceKey
evacuatingLRPLogData["replacement-state"] = instance.State
evacuatingLRPLogData["replacement-lrp-placement-error"] = instance.PlacementError
}
logger.Info("removing-stranded-evacuating-actual-lrp", evacuatingLRPLogData)
err = h.db.RemoveEvacuatingActualLRP(ctx, logger, actualLRPKey, actualLRPInstanceKey)
if err != nil {
return err
}
newLRPs = eventCalculator.RecordChange(lrp, nil, actualLRPs)
return nil
}
// removeEvacuatingOrSuspect removes an evacuating or suspect LRP if they
// exist. Returns true if the LRP was found and removed, false otherwise.
// Also returns the new lrp set and any errors encountered.
//
// This is a helper function used by all evacuating controller endpoints
// (e.g. EvacuateClaimedActualLRP) that delete the LRP because transitioning
// the LRP state wouldn't make sense if the presence is Suspect or Evacuating.
func (h *EvacuationController) removeEvacuatingOrSuspect(
ctx context.Context,
logger lager.Logger,
calculator calculator.ActualLRPEventCalculator,
lrps []*models.ActualLRP,
key *models.ActualLRPKey,
instanceKey *models.ActualLRPInstanceKey,
) (bool, []*models.ActualLRP, error) {
lrp := lookupLRPInSlice(lrps, instanceKey)
if lrp == nil {
logger.Debug("actual-lrp-not-found", lager.Data{"guid": key.ProcessGuid, "index": key.Index})
return false, lrps, models.ErrResourceNotFound
}
switch lrp.Presence {
case models.ActualLRP_Evacuating:
err := h.db.RemoveEvacuatingActualLRP(ctx, logger, key, instanceKey)
if err != nil {
logger.Error("failed-removing-evacuating-actual-lrp", err)
return false, lrps, err
}
case models.ActualLRP_Suspect:
_, err := h.suspectLRPDB.RemoveSuspectActualLRP(ctx, logger, key)
if err != nil {
logger.Error("failed-removing-suspect-actual-lrp", err)
return false, lrps, err
}
default:
return false, lrps, nil
}
lrps = calculator.RecordChange(lrp, nil, lrps)
return true, lrps, nil
}
func (h *EvacuationController) EvacuateClaimedActualLRP(ctx context.Context, logger lager.Logger, actualLRPKey *models.ActualLRPKey, actualLRPInstanceKey *models.ActualLRPInstanceKey) (bool, error) {
eventCalculator := calculator.ActualLRPEventCalculator{
ActualLRPGroupHub: h.actualHub,
ActualLRPInstanceHub: h.actualLRPInstanceHub,
}
guid := actualLRPKey.ProcessGuid
index := actualLRPKey.Index
actualLRPs, err := h.actualLRPDB.ActualLRPs(ctx, logger, models.ActualLRPFilter{ProcessGuid: guid, Index: &index})
if err != nil {
logger.Error("failed-fetching-actual-lrps", err, lager.Data{"guid": guid, "index": index})
return false, err
}
newLRPs := make([]*models.ActualLRP, len(actualLRPs))
copy(newLRPs, actualLRPs)
defer func() {
go eventCalculator.EmitEvents(actualLRPs, newLRPs)
}()
removed, newLRPs, err := h.removeEvacuatingOrSuspect(ctx, logger, eventCalculator, newLRPs, actualLRPKey, actualLRPInstanceKey)
if err != nil {
return false, err
}
if removed {
return false, nil
}
// this is an ordinary LRP
before, after, err := h.actualLRPDB.UnclaimActualLRP(ctx, logger, actualLRPKey)
bbsErr := models.ConvertError(err)
if bbsErr != nil {
if bbsErr.Type == models.Error_ResourceNotFound {
return false, nil
}
return true, bbsErr
}
newLRPs = eventCalculator.RecordChange(before, after, newLRPs)
h.requestAuction(ctx, logger, actualLRPKey)
return false, nil
}
func (h *EvacuationController) EvacuateCrashedActualLRP(ctx context.Context, logger lager.Logger, actualLRPKey *models.ActualLRPKey, actualLRPInstanceKey *models.ActualLRPInstanceKey, errorMessage string) error {
eventCalculator := calculator.ActualLRPEventCalculator{
ActualLRPGroupHub: h.actualHub,
ActualLRPInstanceHub: h.actualLRPInstanceHub,
}
guid := actualLRPKey.ProcessGuid
index := actualLRPKey.Index
actualLRPs, err := h.actualLRPDB.ActualLRPs(ctx, logger, models.ActualLRPFilter{ProcessGuid: guid, Index: &index})
if err != nil {
logger.Error("failed-fetching-actual-lrps", err)
return err
}
newLRPs := make([]*models.ActualLRP, len(actualLRPs))
copy(newLRPs, actualLRPs)
defer func() {
go eventCalculator.EmitEvents(actualLRPs, newLRPs)
}()
removed, newLRPs, err := h.removeEvacuatingOrSuspect(ctx, logger, eventCalculator, newLRPs, actualLRPKey, actualLRPInstanceKey)
if err != nil {
return err
}
if removed {
return nil
}
before, after, _, err := h.actualLRPDB.CrashActualLRP(ctx, logger, actualLRPKey, actualLRPInstanceKey, errorMessage)
if err != nil {
logger.Error("failed-to-crash-actual-lrp", err)
return err
}
newLRPs = eventCalculator.RecordChange(before, after, newLRPs)
return nil
}
// EvacuateRunningActualLRP evacuates the LRP with the given lrp keys. This
// function has to handle the following cases:
//
// 1. Create a Evacuating LRP if one doesn't already exist and this isn't the
// Ordinary LRP.
//
// 2. Do the evacuation dance if this is the Ordinary LRP
//
// 3. Remove the evacuating LRP if it is no longer needed (an Ordinary is
// running or the desired LRP was removed)
//
// Refer to
// https://github.com/cloudfoundry/diego-notes/tree/2cbd7451#harmonizing-during-evacuation
// for more details.
func (h *EvacuationController) EvacuateRunningActualLRP(
ctx context.Context,
logger lager.Logger,
actualLRPKey *models.ActualLRPKey,
actualLRPInstanceKey *models.ActualLRPInstanceKey,
netInfo *models.ActualLRPNetInfo,
internalRoutes []*models.ActualLRPInternalRoute) (bool, error) {
eventCalculator := calculator.ActualLRPEventCalculator{
ActualLRPGroupHub: h.actualHub,
ActualLRPInstanceHub: h.actualLRPInstanceHub,
}
guid := actualLRPKey.ProcessGuid
index := actualLRPKey.Index
actualLRPs, err := h.actualLRPDB.ActualLRPs(ctx, logger, models.ActualLRPFilter{ProcessGuid: guid, Index: &index})
if err != nil {
logger.Error("failed-fetching-actual-lrps", err)
return true, err
}
if len(actualLRPs) == 0 {
return false, nil
}
newLRPs := make([]*models.ActualLRP, len(actualLRPs))
copy(newLRPs, actualLRPs)
defer func() {
go eventCalculator.EmitEvents(actualLRPs, newLRPs)
}()
// the ActualLRP whose InstanceGuid, and CellId match the method
// parameters.
targetActualLRP := lookupLRPInSlice(actualLRPs, actualLRPInstanceKey)
instance := findWithPresence(actualLRPs, models.ActualLRP_Ordinary)
// `instance == nil' means the DesiredLRP has been removed and
// stopInstancesFrom deleted the Ordinary instance.
desiredLRPIsRemoved := instance == nil
// the replacement is already running or crashed. Wrapped in a function so
// we can short circuit its evaluation if instance is nil.
replacementLRPIsRunning := func() bool {
return !instance.Equal(targetActualLRP) &&
(instance.State == models.ActualLRPStateRunning ||
instance.State == models.ActualLRPStateCrashed)
}
if desiredLRPIsRemoved || replacementLRPIsRunning() {
removedEvacuating, err := h.removeEvacuating(ctx, logger, targetActualLRP)
newLRPs = eventCalculator.RecordChange(removedEvacuating, nil, newLRPs)
keepContainer := err != nil
return keepContainer, err
}
if targetActualLRP == nil || targetActualLRP.Presence == models.ActualLRP_Evacuating {
// Create a new Evacuating LRP or update an existing one
evacuating := findWithPresence(actualLRPs, models.ActualLRP_Evacuating)
if evacuating != nil && !evacuating.Equal(targetActualLRP) {
// There is already another evacuating instance. Let the Rep know
// that we don't need this instance anymore. We can't have more
// than one evacuating instance.
logger.Info("already-evacuated-by-different-cell")
return false, nil
}
// FIXME: there might be a bug when the LRP is originally in the CLAIMED
// state. db.EvacuateActualLRP always create an evacuating LRP in the
// running state regardless.
newLRP, err := h.db.EvacuateActualLRP(ctx, logger, actualLRPKey, actualLRPInstanceKey, netInfo, internalRoutes)
if err != nil {
logger.Error("failed-evacuating-actual-lrp", err)
}
if err == models.ErrResourceExists {
// nothing to do, the evacuating LRP already exists in the DB
return true, nil
}
newLRPs = eventCalculator.RecordChange(nil, newLRP, newLRPs)
return true, err
}
if (targetActualLRP.State == models.ActualLRPStateRunning) ||
(targetActualLRP.State == models.ActualLRPStateClaimed) {
// do the evacuation dance. Change the instance from Running/Ordinary
// -> Running/Evacuating and create a new Unclaimed/Ordinary LRP.
err = h.evacuateInstance(ctx, logger, actualLRPs, targetActualLRP)
return true, err
}
// for all other states, just delete the container.
return false, nil
}
func (h *EvacuationController) EvacuateStoppedActualLRP(ctx context.Context, logger lager.Logger, actualLRPKey *models.ActualLRPKey, actualLRPInstanceKey *models.ActualLRPInstanceKey) error {
eventCalculator := calculator.ActualLRPEventCalculator{
ActualLRPGroupHub: h.actualHub,
ActualLRPInstanceHub: h.actualLRPInstanceHub,
}
guid := actualLRPKey.ProcessGuid
index := actualLRPKey.Index
actualLRPs, err := h.actualLRPDB.ActualLRPs(ctx, logger, models.ActualLRPFilter{ProcessGuid: guid, Index: &index})
if err != nil {
logger.Error("failed-fetching-actual-lrps", err)
return err
}
newLRPs := make([]*models.ActualLRP, len(actualLRPs))
copy(newLRPs, actualLRPs)
defer func() {
go eventCalculator.EmitEvents(actualLRPs, newLRPs)
}()
removed, newLRPs, err := h.removeEvacuatingOrSuspect(ctx, logger, eventCalculator, newLRPs, actualLRPKey, actualLRPInstanceKey)
if err != nil {
return err
}
if removed {
return nil
}
err = h.actualLRPDB.RemoveActualLRP(ctx, logger, guid, index, actualLRPInstanceKey)
if err != nil {
logger.Error("failed-to-remove-actual-lrp", err)
return err
}
lrp := lookupLRPInSlice(actualLRPs, actualLRPInstanceKey)
newLRPs = eventCalculator.RecordChange(lrp, nil, newLRPs)
return nil
}
func (h *EvacuationController) requestAuction(ctx context.Context, logger lager.Logger, lrpKey *models.ActualLRPKey) {
desiredLRP, err := h.desiredLRPDB.DesiredLRPByProcessGuid(ctx, logger, lrpKey.ProcessGuid)
if err != nil {
logger.Error("failed-fetching-desired-lrp", err)
return
}
schedInfo := desiredLRP.DesiredLRPSchedulingInfo()
startRequest := auctioneer.NewLRPStartRequestFromSchedulingInfo(&schedInfo, int(lrpKey.Index))
err = h.auctioneerClient.RequestLRPAuctions(logger, []*auctioneer.LRPStartRequest{&startRequest})
if err != nil {
logger.Error("failed-requesting-auction", err)
}
}
func (h *EvacuationController) evacuateInstance(ctx context.Context, logger lager.Logger, allLRPs []*models.ActualLRP, actualLRP *models.ActualLRP) error {
eventCalculator := calculator.ActualLRPEventCalculator{
ActualLRPGroupHub: h.actualHub,
ActualLRPInstanceHub: h.actualLRPInstanceHub,
}
evacuating, err := h.db.EvacuateActualLRP(ctx, logger, &actualLRP.ActualLRPKey, &actualLRP.ActualLRPInstanceKey, &actualLRP.ActualLRPNetInfo, actualLRP.ActualLrpInternalRoutes)
if err != nil {
return err
}
// although EvacuateActualLRP above creates a new database record. We
// would like to record that as a change event instead, since the instance
// guid hasn't changed. This will produce a simpler instance event stream
// with a single changed event and keep the group events backward
// compatible.
newLRPs := eventCalculator.RecordChange(actualLRP, evacuating, allLRPs)
defer func() {
go eventCalculator.EmitEvents(allLRPs, newLRPs)
}()
if actualLRP.Presence == models.ActualLRP_Suspect {
_, err := h.suspectLRPDB.RemoveSuspectActualLRP(ctx, logger, &actualLRP.ActualLRPKey)
if err != nil {
logger.Error("failed-removing-suspect-actual-lrp", err)
return err
}
return nil
}
_, after, err := h.actualLRPDB.UnclaimActualLRP(ctx, logger, &actualLRP.ActualLRPKey)
if err != nil {
return err
}
// although UnclaimActualLRP above updates a database record. We would
// like to record that as a create event instead. This will produce a
// simpler instance event stream and keep the group events backward
// compatible.
newLRPs = eventCalculator.RecordChange(nil, after, newLRPs)
h.requestAuction(ctx, logger, &actualLRP.ActualLRPKey)
return nil
}
func (h *EvacuationController) removeEvacuating(ctx context.Context, logger lager.Logger, evacuating *models.ActualLRP) (*models.ActualLRP, error) {
if evacuating == nil {
return nil, nil
}
err := h.db.RemoveEvacuatingActualLRP(ctx, logger, &evacuating.ActualLRPKey, &evacuating.ActualLRPInstanceKey)
if err == nil {
return evacuating, nil
}
if err == models.ErrActualLRPCannotBeRemoved {
return nil, nil
}
return nil, err
}