@@ -326,7 +326,7 @@ func (bic *BackingImageController) replenishBackingImageCopies(bi *longhorn.Back
326
326
return nil
327
327
}
328
328
329
- concurrentReplenishLimit , err := bic .ds .GetSettingAsInt (types .SettingNameConcurrentBackingImageReplenishPerNodeLimit )
329
+ concurrentReplenishLimit , err := bic .ds .GetSettingAsInt (types .SettingNameConcurrentBackingImageCopyReplenishPerNodeLimit )
330
330
if err != nil {
331
331
return err
332
332
}
@@ -337,61 +337,40 @@ func (bic *BackingImageController) replenishBackingImageCopies(bi *longhorn.Back
337
337
}
338
338
339
339
nonFailedCopies := 0
340
- usedDisks := map [string ]bool {}
341
340
for diskUUID := range bi .Spec .DiskFileSpecMap {
342
341
fileStatus , exists := bi .Status .DiskFileStatusMap [diskUUID ]
343
342
if ! exists || (fileStatus .State != longhorn .BackingImageStateFailed &&
344
343
fileStatus .State != longhorn .BackingImageStateFailedAndCleanUp &&
345
344
fileStatus .State != longhorn .BackingImageStateUnknown ) {
346
-
347
345
// Non-existing file in status could due to not being synced from backing image manager yet.
348
346
// Consider it as newly created copy and count it as non-failed copies.
349
347
// So we don't create extra copy when handling copies evictions.
350
- usedDisks [diskUUID ] = true
351
348
nonFailedCopies += 1
352
349
}
353
350
}
354
351
352
+ // Need to count the evicted copies in the nonFailedCopies then handle it in handleBackingImageCopiesEvictions
353
+ // so we can distinguish the case of "0 healthy copy" and "there is 1 copy but being evicted".
355
354
if nonFailedCopies == 0 {
356
355
return nil
357
- } else if nonFailedCopies >= bi .Spec .MinNumberOfCopies {
358
- if err := bic .handleBackingImageCopiesEvictions (nonFailedCopies , bi , usedDisks ); err != nil {
359
- return nil
360
- }
361
- } else { //nonFailedCopies < bi.Spec.MinNumberOfCopies
362
- readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi , usedDisks )
363
- logrus .Infof ("replicate the copy to node: %v, disk: %v" , readyNode , readyDiskName )
364
- if err != nil {
365
- logrus .WithError (err ).Warnf ("failed to create the backing image copy" )
366
- return nil
367
- }
368
- // BackingImageManager will then sync the BackingImage to the disk
369
- bi .Spec .DiskFileSpecMap [readyNode .Status .DiskStatus [readyDiskName ].DiskUUID ] = & longhorn.BackingImageDiskFileSpec {}
370
- }
371
-
372
- return nil
373
- }
374
-
375
- // handleBackingImageCopiesEvictions do creating one more replica for eviction, if requested
376
- func (bic * BackingImageController ) handleBackingImageCopiesEvictions (nonFailedCopies int , bi * longhorn.BackingImage , usedDisks map [string ]bool ) (err error ) {
377
- log := getLoggerForBackingImage (bic .logger , bi )
378
- NonEvictingCount := nonFailedCopies
379
-
380
- for _ , fileSpec := range bi .Spec .DiskFileSpecMap {
381
- if fileSpec .EvictionRequested {
382
- NonEvictingCount --
356
+ } else {
357
+ nonEvictingCount := nonFailedCopies
358
+ for _ , fileSpec := range bi .Spec .DiskFileSpecMap {
359
+ if fileSpec .EvictionRequested {
360
+ nonEvictingCount --
361
+ }
383
362
}
384
- }
385
363
386
- if NonEvictingCount < bi .Spec .MinNumberOfCopies {
387
- log .Infof ("Creating one more backing image copy for eviction" )
388
- readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi , usedDisks )
389
- if err != nil {
390
- logrus .WithError (err ).Warnf ("failed to create the backing image copy" )
391
- return nil
364
+ if nonEvictingCount < bi .Spec .MinNumberOfCopies {
365
+ readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi )
366
+ logrus .Infof ("replicate the copy to node: %v, disk: %v" , readyNode , readyDiskName )
367
+ if err != nil {
368
+ logrus .WithError (err ).Warnf ("failed to create the backing image copy" )
369
+ return nil
370
+ }
371
+ // BackingImageManager will then sync the BackingImage to the disk
372
+ bi .Spec .DiskFileSpecMap [readyNode .Status .DiskStatus [readyDiskName ].DiskUUID ] = & longhorn.BackingImageDiskFileSpec {}
392
373
}
393
- // BackingImageManager will then sync the BackingImage to the disk
394
- bi .Spec .DiskFileSpecMap [readyNode .Status .DiskStatus [readyDiskName ].DiskUUID ] = & longhorn.BackingImageDiskFileSpec {}
395
374
}
396
375
397
376
return nil
@@ -405,8 +384,10 @@ func (bic *BackingImageController) cleanupEvictionRequestedBackingImageCopies(bi
405
384
hasNonEvictingHealthyBackingImageCopy := false
406
385
evictingHealthyBackingImageCopyDiskUUID := ""
407
386
for diskUUID , fileSpec := range bi .Spec .DiskFileSpecMap {
408
- fileStatus , exists := bi .Status .DiskFileStatusMap [diskUUID ]
409
- if exists && fileStatus != nil {
387
+ fileStatus := bi .Status .DiskFileStatusMap [diskUUID ]
388
+ if fileStatus == nil { // it is newly added, consider it as non healthy
389
+ continue
390
+ } else {
410
391
if fileStatus .State != longhorn .BackingImageStateReady {
411
392
continue
412
393
}
@@ -429,6 +410,10 @@ func (bic *BackingImageController) cleanupEvictionRequestedBackingImageCopies(bi
429
410
bic .eventRecorder .Event (bi , corev1 .EventTypeNormal , constant .EventReasonFailedDeleting , msg )
430
411
continue
431
412
}
413
+ // Backing image controller update the spec here because
414
+ // only this controller can gather all the information of all the copies of this backing image at once.
415
+ // By deleting the disk from the spec, backing image manager controller will delete the copy on that disk.
416
+ // TODO: introduce a new CRD for the backing image copy so we can delete the copy like volume controller deletes replicas.
432
417
delete (bi .Spec .DiskFileSpecMap , diskUUID )
433
418
log .Infof ("Evicted backing image copy on disk %v" , diskUUID )
434
419
}
@@ -537,9 +522,8 @@ func (bic *BackingImageController) handleBackingImageDataSource(bi *longhorn.Bac
537
522
}
538
523
}
539
524
540
- // JackLin: BackingIamge Data Source choose node/disk
541
525
if ! foundReadyDisk {
542
- readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi , map [ string ] bool {} )
526
+ readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi )
543
527
if err != nil {
544
528
return err
545
529
}
@@ -670,7 +654,7 @@ func (bic *BackingImageController) handleBackingImageDataSource(bi *longhorn.Bac
670
654
changeNodeDisk := err != nil || node .Name != bids .Spec .NodeID || node .Spec .Disks [diskName ].Path != bids .Spec .DiskPath || node .Status .DiskStatus [diskName ].DiskUUID != bids .Spec .DiskUUID
671
655
if changeNodeDisk {
672
656
log .Warn ("Backing image data source current node and disk is not ready, need to switch to another ready node and disk" )
673
- readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi , map [ string ] bool {} )
657
+ readyNode , readyDiskName , err := bic .ds .GetReadyNodeDiskForBackingImage (bi )
674
658
if err != nil {
675
659
return err
676
660
}
@@ -1002,7 +986,7 @@ func (bic *BackingImageController) enqueueBackingImageForNodeUpdate(oldObj, curr
1002
986
}
1003
987
}
1004
988
1005
- diskBackingImageMap , err := bic .ds .GetDiskBackingImageMap (oldNode )
989
+ diskBackingImageMap , err := bic .ds .GetDiskBackingImageMap ()
1006
990
if err != nil {
1007
991
utilruntime .HandleError (fmt .Errorf ("failed to get disk backing image map when handling node update" ))
1008
992
return
0 commit comments