Avoid data-race in getDisksInfo call (#8126)

This commit is contained in:
Harshavardhana 2019-08-23 17:03:15 -07:00 committed by GitHub
parent c38ada1a26
commit d6dd98e597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,11 +71,12 @@ func (d byDiskTotal) Less(i, j int) bool {
// getDisksInfo - fetch disks info across all other storage API. // getDisksInfo - fetch disks info across all other storage API.
func getDisksInfo(disks []StorageAPI) (disksInfo []DiskInfo, onlineDisks int, offlineDisks int) { func getDisksInfo(disks []StorageAPI) (disksInfo []DiskInfo, onlineDisks int, offlineDisks int) {
disksInfo = make([]DiskInfo, len(disks)) disksInfo = make([]DiskInfo, len(disks))
errs := make([]error, len(disks))
var wg sync.WaitGroup var wg sync.WaitGroup
for i, storageDisk := range disks { for i, storageDisk := range disks {
if storageDisk == nil { if storageDisk == nil {
// Storage disk is empty, perhaps ignored disk or not available. // Storage disk is empty, perhaps ignored disk or not available.
offlineDisks++ errs[i] = errDiskNotFound
continue continue
} }
wg.Add(1) wg.Add(1)
@ -87,16 +88,23 @@ func getDisksInfo(disks []StorageAPI) (disksInfo []DiskInfo, onlineDisks int, of
ctx := logger.SetReqInfo(context.Background(), reqInfo) ctx := logger.SetReqInfo(context.Background(), reqInfo)
logger.LogIf(ctx, err) logger.LogIf(ctx, err)
if IsErr(err, baseErrs...) { if IsErr(err, baseErrs...) {
offlineDisks++ errs[id] = err
return return
} }
} }
onlineDisks++
disksInfo[id] = info disksInfo[id] = info
}(i, storageDisk) }(i, storageDisk)
} }
// Wait for the routines. // Wait for the routines.
wg.Wait() wg.Wait()
for _, err := range errs {
if err != nil {
offlineDisks++
}
onlineDisks++
}
// Success. // Success.
return disksInfo, onlineDisks, offlineDisks return disksInfo, onlineDisks, offlineDisks
} }