2016-04-22 13:24:04 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
slashpath "path"
|
2016-05-10 02:51:05 -04:00
|
|
|
"sync"
|
2016-04-22 13:24:04 -04:00
|
|
|
)
|
|
|
|
|
2016-04-26 16:03:37 -04:00
|
|
|
// Get the highest integer from a given integer slice.
|
|
|
|
func highestInt(intSlice []int64) (highestInteger int64) {
|
|
|
|
highestInteger = int64(0)
|
|
|
|
for _, integer := range intSlice {
|
|
|
|
if highestInteger < integer {
|
|
|
|
highestInteger = integer
|
2016-04-22 13:24:04 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 16:03:37 -04:00
|
|
|
return highestInteger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts file versions from partsMetadata slice and returns version slice.
|
2016-04-30 06:21:54 -04:00
|
|
|
func listFileVersions(partsMetadata []xlMetaV1, errs []error) (versions []int64) {
|
2016-04-26 16:03:37 -04:00
|
|
|
versions = make([]int64, len(partsMetadata))
|
2016-04-22 13:24:04 -04:00
|
|
|
for index, metadata := range partsMetadata {
|
|
|
|
if errs[index] == nil {
|
2016-04-28 22:27:02 -04:00
|
|
|
versions[index] = metadata.Stat.Version
|
2016-04-22 13:24:04 -04:00
|
|
|
} else {
|
|
|
|
versions[index] = -1
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 06:21:54 -04:00
|
|
|
return versions
|
2016-04-26 16:03:37 -04:00
|
|
|
}
|
|
|
|
|
2016-05-18 00:22:27 -04:00
|
|
|
// reduceError - convert collection of errors into a single
|
2016-05-13 00:01:11 -04:00
|
|
|
// error based on total errors and read quorum.
|
2016-05-18 00:22:27 -04:00
|
|
|
func (xl XL) reduceError(errs []error) error {
|
|
|
|
fileNotFoundCount := 0
|
2016-05-11 21:58:32 -04:00
|
|
|
diskNotFoundCount := 0
|
2016-05-18 00:22:27 -04:00
|
|
|
volumeNotFoundCount := 0
|
2016-05-13 00:01:11 -04:00
|
|
|
diskAccessDeniedCount := 0
|
2016-04-26 16:03:37 -04:00
|
|
|
for _, err := range errs {
|
2016-05-13 00:01:11 -04:00
|
|
|
if err == errFileNotFound {
|
2016-05-18 00:22:27 -04:00
|
|
|
fileNotFoundCount++
|
2016-05-13 00:01:11 -04:00
|
|
|
} else if err == errDiskNotFound {
|
2016-05-11 21:58:32 -04:00
|
|
|
diskNotFoundCount++
|
2016-05-13 00:01:11 -04:00
|
|
|
} else if err == errVolumeAccessDenied {
|
|
|
|
diskAccessDeniedCount++
|
2016-05-18 00:22:27 -04:00
|
|
|
} else if err == errVolumeNotFound {
|
|
|
|
volumeNotFoundCount++
|
2016-05-11 21:58:32 -04:00
|
|
|
}
|
2016-04-26 16:03:37 -04:00
|
|
|
}
|
2016-05-13 00:01:11 -04:00
|
|
|
// If we have errors with 'file not found' greater than
|
2016-05-18 00:22:27 -04:00
|
|
|
// readQuorum, return as errFileNotFound.
|
|
|
|
// else if we have errors with 'volume not found' greater than
|
|
|
|
// readQuorum, return as errVolumeNotFound.
|
|
|
|
if fileNotFoundCount > len(xl.storageDisks)-xl.readQuorum {
|
2016-05-13 00:01:11 -04:00
|
|
|
return errFileNotFound
|
2016-05-18 00:22:27 -04:00
|
|
|
} else if volumeNotFoundCount > len(xl.storageDisks)-xl.readQuorum {
|
|
|
|
return errVolumeNotFound
|
2016-05-13 00:01:11 -04:00
|
|
|
}
|
|
|
|
// If we have errors with disk not found equal to the
|
|
|
|
// number of disks, return as errDiskNotFound.
|
|
|
|
if diskNotFoundCount == len(xl.storageDisks) {
|
|
|
|
return errDiskNotFound
|
|
|
|
} else if diskNotFoundCount > len(xl.storageDisks)-xl.readQuorum {
|
|
|
|
// If we have errors with 'disk not found' greater than
|
2016-05-18 00:22:27 -04:00
|
|
|
// readQuorum, return as errFileNotFound.
|
2016-05-13 00:01:11 -04:00
|
|
|
return errFileNotFound
|
|
|
|
}
|
|
|
|
// If we have errors with disk not found equal to the
|
|
|
|
// number of disks, return as errDiskNotFound.
|
|
|
|
if diskAccessDeniedCount == len(xl.storageDisks) {
|
|
|
|
return errVolumeAccessDenied
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-10 03:10:34 -04:00
|
|
|
|
2016-05-13 00:01:11 -04:00
|
|
|
// Returns slice of online disks needed.
|
|
|
|
// - slice returing readable disks.
|
|
|
|
// - xlMetaV1
|
|
|
|
// - bool value indicating if healing is needed.
|
|
|
|
// - error if any.
|
|
|
|
func (xl XL) listOnlineDisks(volume, path string) (onlineDisks []StorageAPI, mdata xlMetaV1, heal bool, err error) {
|
|
|
|
partsMetadata, errs := xl.getPartsMetadata(volume, path)
|
2016-05-18 00:22:27 -04:00
|
|
|
if err = xl.reduceError(errs); err != nil {
|
2016-05-13 00:01:11 -04:00
|
|
|
return nil, xlMetaV1{}, false, err
|
|
|
|
}
|
2016-04-26 16:03:37 -04:00
|
|
|
highestVersion := int64(0)
|
|
|
|
onlineDisks = make([]StorageAPI, len(xl.storageDisks))
|
|
|
|
// List all the file versions from partsMetadata list.
|
2016-04-30 06:21:54 -04:00
|
|
|
versions := listFileVersions(partsMetadata, errs)
|
2016-04-26 16:03:37 -04:00
|
|
|
|
|
|
|
// Get highest file version.
|
|
|
|
highestVersion = highestInt(versions)
|
|
|
|
|
|
|
|
// Pick online disks with version set to highestVersion.
|
|
|
|
onlineDiskCount := 0
|
2016-04-22 13:24:04 -04:00
|
|
|
for index, version := range versions {
|
|
|
|
if version == highestVersion {
|
2016-04-26 16:03:37 -04:00
|
|
|
mdata = partsMetadata[index]
|
|
|
|
onlineDisks[index] = xl.storageDisks[index]
|
|
|
|
onlineDiskCount++
|
2016-04-22 13:24:04 -04:00
|
|
|
} else {
|
2016-04-26 16:03:37 -04:00
|
|
|
onlineDisks[index] = nil
|
2016-04-22 13:24:04 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 16:03:37 -04:00
|
|
|
|
|
|
|
// If online disks count is lesser than configured disks, most
|
|
|
|
// probably we need to heal the file, additionally verify if the
|
|
|
|
// count is lesser than readQuorum, if not we throw an error.
|
|
|
|
if onlineDiskCount < len(xl.storageDisks) {
|
|
|
|
// Online disks lesser than total storage disks, needs to be
|
|
|
|
// healed. unless we do not have readQuorum.
|
|
|
|
heal = true
|
|
|
|
// Verify if online disks count are lesser than readQuorum
|
|
|
|
// threshold, return an error if yes.
|
|
|
|
if onlineDiskCount < xl.readQuorum {
|
2016-04-28 22:27:02 -04:00
|
|
|
return nil, xlMetaV1{}, false, errReadQuorum
|
2016-04-22 13:24:04 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-26 16:03:37 -04:00
|
|
|
return onlineDisks, mdata, heal, nil
|
2016-04-22 13:24:04 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 20:42:01 -04:00
|
|
|
// Get file.json metadata as a map slice.
|
2016-04-22 13:24:04 -04:00
|
|
|
// Returns error slice indicating the failed metadata reads.
|
|
|
|
// Read lockNS() should be done by caller.
|
2016-04-28 22:27:02 -04:00
|
|
|
func (xl XL) getPartsMetadata(volume, path string) ([]xlMetaV1, []error) {
|
2016-04-22 13:24:04 -04:00
|
|
|
errs := make([]error, len(xl.storageDisks))
|
2016-04-28 22:27:02 -04:00
|
|
|
metadataArray := make([]xlMetaV1, len(xl.storageDisks))
|
|
|
|
xlMetaV1FilePath := slashpath.Join(path, xlMetaV1File)
|
2016-05-10 02:51:05 -04:00
|
|
|
var wg = &sync.WaitGroup{}
|
2016-04-22 13:24:04 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-05-10 02:51:05 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
offset := int64(0)
|
|
|
|
metadataReader, err := disk.ReadFile(volume, xlMetaV1FilePath, offset)
|
|
|
|
if err != nil {
|
|
|
|
errs[index] = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer metadataReader.Close()
|
2016-04-22 13:24:04 -04:00
|
|
|
|
2016-05-10 02:51:05 -04:00
|
|
|
metadata, err := xlMetaV1Decode(metadataReader)
|
|
|
|
if err != nil {
|
|
|
|
// Unable to parse file.json, set error.
|
|
|
|
errs[index] = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
metadataArray[index] = metadata
|
|
|
|
}(index, disk)
|
2016-04-22 13:24:04 -04:00
|
|
|
}
|
2016-05-10 02:51:05 -04:00
|
|
|
wg.Wait()
|
2016-04-22 13:24:04 -04:00
|
|
|
return metadataArray, errs
|
|
|
|
}
|
|
|
|
|
2016-05-02 20:42:01 -04:00
|
|
|
// Writes/Updates `file.json` for given file. updateParts carries
|
|
|
|
// index of disks where `file.json` needs to be updated.
|
2016-04-22 13:24:04 -04:00
|
|
|
//
|
|
|
|
// Returns collection of errors, indexed in accordance with input
|
|
|
|
// updateParts order.
|
|
|
|
// Write lockNS() should be done by caller.
|
2016-05-05 04:39:26 -04:00
|
|
|
func (xl XL) updatePartsMetadata(volume, path string, metadata xlMetaV1, updateParts []bool) []error {
|
|
|
|
xlMetaV1FilePath := pathJoin(path, xlMetaV1File)
|
2016-04-22 13:24:04 -04:00
|
|
|
errs := make([]error, len(xl.storageDisks))
|
|
|
|
|
|
|
|
for index := range updateParts {
|
|
|
|
errs[index] = errors.New("Metadata not updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, shouldUpdate := range updateParts {
|
|
|
|
if !shouldUpdate {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-28 22:27:02 -04:00
|
|
|
writer, err := xl.storageDisks[index].CreateFile(volume, xlMetaV1FilePath)
|
2016-04-22 13:24:04 -04:00
|
|
|
errs[index] = err
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-28 22:27:02 -04:00
|
|
|
err = metadata.Write(writer)
|
2016-04-22 13:24:04 -04:00
|
|
|
if err != nil {
|
|
|
|
errs[index] = err
|
|
|
|
safeCloseAndRemove(writer)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|