You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
232 lines
7.2 KiB
232 lines
7.2 KiB
9 years ago
|
/*
|
||
5 years ago
|
* MinIO Cloud Storage, (C) 2016-2019 MinIO, Inc.
|
||
9 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
7 years ago
|
"context"
|
||
8 years ago
|
"errors"
|
||
9 years ago
|
"hash/crc32"
|
||
8 years ago
|
|
||
7 years ago
|
"github.com/minio/minio/cmd/logger"
|
||
5 years ago
|
"github.com/minio/minio/pkg/sync/errgroup"
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
// Returns number of errors that occurred the most (incl. nil) and the
|
||
7 years ago
|
// corresponding error value. NB When there is more than one error value that
|
||
9 years ago
|
// occurs maximum number of times, the error value returned depends on how
|
||
|
// golang's map orders keys. This doesn't affect correctness as long as quorum
|
||
|
// value is greater than or equal to simple majority, since none of the equally
|
||
|
// maximal values would occur quorum or more number of times.
|
||
8 years ago
|
func reduceErrs(errs []error, ignoredErrs []error) (maxCount int, maxErr error) {
|
||
9 years ago
|
errorCounts := make(map[error]int)
|
||
9 years ago
|
for _, err := range errs {
|
||
7 years ago
|
if IsErrIgnored(err, ignoredErrs...) {
|
||
8 years ago
|
continue
|
||
|
}
|
||
9 years ago
|
errorCounts[err]++
|
||
9 years ago
|
}
|
||
8 years ago
|
|
||
9 years ago
|
max := 0
|
||
9 years ago
|
for err, count := range errorCounts {
|
||
8 years ago
|
switch {
|
||
|
case max < count:
|
||
9 years ago
|
max = count
|
||
8 years ago
|
maxErr = err
|
||
8 years ago
|
|
||
|
// Prefer `nil` over other error values with the same
|
||
|
// number of occurrences.
|
||
|
case max == count && err == nil:
|
||
|
maxErr = err
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
return max, maxErr
|
||
|
}
|
||
|
|
||
|
// reduceQuorumErrs behaves like reduceErrs by only for returning
|
||
|
// values of maximally occurring errors validated against a generic
|
||
7 years ago
|
// quorum number that can be read or write quorum depending on usage.
|
||
7 years ago
|
func reduceQuorumErrs(ctx context.Context, errs []error, ignoredErrs []error, quorum int, quorumErr error) error {
|
||
|
maxCount, maxErr := reduceErrs(errs, ignoredErrs)
|
||
|
if maxCount >= quorum {
|
||
|
return maxErr
|
||
8 years ago
|
}
|
||
7 years ago
|
return quorumErr
|
||
8 years ago
|
}
|
||
|
|
||
|
// reduceReadQuorumErrs behaves like reduceErrs but only for returning
|
||
|
// values of maximally occurring errors validated against readQuorum.
|
||
7 years ago
|
func reduceReadQuorumErrs(ctx context.Context, errs []error, ignoredErrs []error, readQuorum int) (maxErr error) {
|
||
5 years ago
|
return reduceQuorumErrs(ctx, errs, ignoredErrs, readQuorum, errErasureReadQuorum)
|
||
8 years ago
|
}
|
||
|
|
||
|
// reduceWriteQuorumErrs behaves like reduceErrs but only for returning
|
||
|
// values of maximally occurring errors validated against writeQuorum.
|
||
7 years ago
|
func reduceWriteQuorumErrs(ctx context.Context, errs []error, ignoredErrs []error, writeQuorum int) (maxErr error) {
|
||
5 years ago
|
return reduceQuorumErrs(ctx, errs, ignoredErrs, writeQuorum, errErasureWriteQuorum)
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
// Similar to 'len(slice)' but returns the actual elements count
|
||
9 years ago
|
// skipping the unallocated elements.
|
||
|
func diskCount(disks []StorageAPI) int {
|
||
|
diskCount := 0
|
||
|
for _, disk := range disks {
|
||
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
diskCount++
|
||
|
}
|
||
|
return diskCount
|
||
|
}
|
||
|
|
||
7 years ago
|
// hashOrder - hashes input key to return consistent
|
||
8 years ago
|
// hashed integer slice. Returned integer order is salted
|
||
|
// with an input key. This results in consistent order.
|
||
|
// NOTE: collisions are fine, we are not looking for uniqueness
|
||
|
// in the slices returned.
|
||
|
func hashOrder(key string, cardinality int) []int {
|
||
7 years ago
|
if cardinality <= 0 {
|
||
|
// Returns an empty int slice for cardinality < 0.
|
||
8 years ago
|
return nil
|
||
9 years ago
|
}
|
||
7 years ago
|
|
||
8 years ago
|
nums := make([]int, cardinality)
|
||
|
keyCrc := crc32.Checksum([]byte(key), crc32.IEEETable)
|
||
9 years ago
|
|
||
7 years ago
|
start := int(keyCrc % uint32(cardinality))
|
||
8 years ago
|
for i := 1; i <= cardinality; i++ {
|
||
|
nums[i-1] = 1 + ((start + i) % cardinality)
|
||
9 years ago
|
}
|
||
9 years ago
|
return nums
|
||
9 years ago
|
}
|
||
|
|
||
5 years ago
|
// Reads all `xl.meta` metadata as a FileInfo slice.
|
||
8 years ago
|
// Returns error slice indicating the failed metadata reads.
|
||
5 years ago
|
func readAllFileInfo(ctx context.Context, disks []StorageAPI, bucket, object, versionID string) ([]FileInfo, []error) {
|
||
|
metadataArray := make([]FileInfo, len(disks))
|
||
5 years ago
|
|
||
|
g := errgroup.WithNErrs(len(disks))
|
||
5 years ago
|
// Read `xl.meta` parallelly across disks.
|
||
5 years ago
|
for index := range disks {
|
||
|
index := index
|
||
|
g.Go(func() (err error) {
|
||
|
if disks[index] == nil {
|
||
|
return errDiskNotFound
|
||
|
}
|
||
5 years ago
|
metadataArray[index], err = disks[index].ReadVersion(bucket, object, versionID)
|
||
|
if err != nil {
|
||
|
if err != errFileNotFound && err != errVolumeNotFound && err != errFileVersionNotFound {
|
||
|
logger.GetReqInfo(ctx).AppendTags("disk", disks[index].String())
|
||
|
logger.LogIf(ctx, err)
|
||
|
}
|
||
|
}
|
||
5 years ago
|
return err
|
||
|
}, index)
|
||
8 years ago
|
}
|
||
|
|
||
|
// Return all the metadata.
|
||
5 years ago
|
return metadataArray, g.Wait()
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// Return shuffled partsMetadata depending on distribution.
|
||
5 years ago
|
func shufflePartsMetadata(partsMetadata []FileInfo, distribution []int) (shuffledPartsMetadata []FileInfo) {
|
||
8 years ago
|
if distribution == nil {
|
||
|
return partsMetadata
|
||
|
}
|
||
5 years ago
|
shuffledPartsMetadata = make([]FileInfo, len(partsMetadata))
|
||
8 years ago
|
// Shuffle slice xl metadata for expected distribution.
|
||
8 years ago
|
for index := range partsMetadata {
|
||
|
blockIndex := distribution[index]
|
||
8 years ago
|
shuffledPartsMetadata[blockIndex-1] = partsMetadata[index]
|
||
8 years ago
|
}
|
||
8 years ago
|
return shuffledPartsMetadata
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
// shuffleDisks - shuffle input disks slice depending on the
|
||
7 years ago
|
// erasure distribution. Return shuffled slice of disks with
|
||
8 years ago
|
// their expected distribution.
|
||
|
func shuffleDisks(disks []StorageAPI, distribution []int) (shuffledDisks []StorageAPI) {
|
||
|
if distribution == nil {
|
||
|
return disks
|
||
|
}
|
||
|
shuffledDisks = make([]StorageAPI, len(disks))
|
||
|
// Shuffle disks for expected distribution.
|
||
8 years ago
|
for index := range disks {
|
||
|
blockIndex := distribution[index]
|
||
8 years ago
|
shuffledDisks[blockIndex-1] = disks[index]
|
||
8 years ago
|
}
|
||
8 years ago
|
return shuffledDisks
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// evalDisks - returns a new slice of disks where nil is set if
|
||
7 years ago
|
// the corresponding error in errs slice is not nil
|
||
8 years ago
|
func evalDisks(disks []StorageAPI, errs []error) []StorageAPI {
|
||
|
if len(errs) != len(disks) {
|
||
5 years ago
|
logger.LogIf(GlobalContext, errors.New("unexpected disks/errors slice length"))
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
newDisks := make([]StorageAPI, len(disks))
|
||
|
for index := range errs {
|
||
|
if errs[index] == nil {
|
||
|
newDisks[index] = disks[index]
|
||
|
} else {
|
||
|
newDisks[index] = nil
|
||
|
}
|
||
|
}
|
||
|
return newDisks
|
||
|
}
|
||
|
|
||
7 years ago
|
// Errors specifically generated by calculatePartSizeFromIdx function.
|
||
8 years ago
|
var (
|
||
|
errPartSizeZero = errors.New("Part size cannot be zero")
|
||
|
errPartSizeIndex = errors.New("Part index cannot be smaller than 1")
|
||
|
)
|
||
|
|
||
7 years ago
|
// calculatePartSizeFromIdx calculates the part size according to input index.
|
||
|
// returns error if totalSize is -1, partSize is 0, partIndex is 0.
|
||
7 years ago
|
func calculatePartSizeFromIdx(ctx context.Context, totalSize int64, partSize int64, partIndex int) (currPartSize int64, err error) {
|
||
6 years ago
|
if totalSize < -1 {
|
||
7 years ago
|
logger.LogIf(ctx, errInvalidArgument)
|
||
|
return 0, errInvalidArgument
|
||
7 years ago
|
}
|
||
8 years ago
|
if partSize == 0 {
|
||
7 years ago
|
logger.LogIf(ctx, errPartSizeZero)
|
||
|
return 0, errPartSizeZero
|
||
8 years ago
|
}
|
||
|
if partIndex < 1 {
|
||
7 years ago
|
logger.LogIf(ctx, errPartSizeIndex)
|
||
|
return 0, errPartSizeIndex
|
||
8 years ago
|
}
|
||
6 years ago
|
if totalSize == -1 {
|
||
|
return -1, nil
|
||
|
}
|
||
7 years ago
|
if totalSize > 0 {
|
||
|
// Compute the total count of parts
|
||
|
partsCount := totalSize/partSize + 1
|
||
|
// Return the part's size
|
||
|
switch {
|
||
|
case int64(partIndex) < partsCount:
|
||
|
currPartSize = partSize
|
||
|
case int64(partIndex) == partsCount:
|
||
|
// Size of last part
|
||
|
currPartSize = totalSize % partSize
|
||
|
default:
|
||
|
currPartSize = 0
|
||
|
}
|
||
8 years ago
|
}
|
||
7 years ago
|
return currPartSize, nil
|
||
8 years ago
|
}
|