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
1 changed files with 11 additions and 3 deletions

View File

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