2016-05-30 19:51:59 -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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-25 19:42:31 -04:00
|
|
|
|
|
|
|
import (
|
2016-07-08 10:33:21 -04:00
|
|
|
"hash/crc32"
|
2016-06-02 19:34:15 -04:00
|
|
|
"path"
|
2016-07-26 14:34:48 -04:00
|
|
|
"sync"
|
2016-09-09 01:38:18 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
2016-05-25 19:42:31 -04:00
|
|
|
)
|
|
|
|
|
2016-07-13 14:56:25 -04:00
|
|
|
// Returns number of errors that occurred the most (incl. nil) and the
|
|
|
|
// corresponding error value. N B when there is more than one error value that
|
|
|
|
// 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.
|
2016-07-09 16:01:32 -04:00
|
|
|
|
2016-07-19 22:24:32 -04:00
|
|
|
func reduceErrs(errs []error, ignoredErrs []error) error {
|
2016-07-13 14:56:25 -04:00
|
|
|
errorCounts := make(map[error]int)
|
2016-08-25 12:39:01 -04:00
|
|
|
errs = errorsCause(errs)
|
2016-07-09 16:01:32 -04:00
|
|
|
for _, err := range errs {
|
2016-07-19 22:24:32 -04:00
|
|
|
if isErrIgnored(err, ignoredErrs) {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-13 14:56:25 -04:00
|
|
|
errorCounts[err]++
|
2016-07-09 16:01:32 -04:00
|
|
|
}
|
|
|
|
max := 0
|
2016-07-13 14:56:25 -04:00
|
|
|
var errMax error
|
|
|
|
for err, count := range errorCounts {
|
|
|
|
if max < count {
|
|
|
|
max = count
|
|
|
|
errMax = err
|
2016-07-09 16:01:32 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-25 12:39:01 -04:00
|
|
|
return traceError(errMax, errs...)
|
2016-07-09 16:01:32 -04:00
|
|
|
}
|
|
|
|
|
2016-11-20 19:57:12 -05:00
|
|
|
// List of all errors which are ignored while verifying quorum.
|
|
|
|
var quorumIgnoredErrs = []error{
|
|
|
|
errFaultyDisk,
|
|
|
|
errFaultyRemoteDisk,
|
|
|
|
errDiskNotFound,
|
|
|
|
errDiskAccessDenied,
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:29:48 -04:00
|
|
|
// Validates if we have quorum based on the errors related to disk only.
|
|
|
|
// Returns 'true' if we have quorum, 'false' if we don't.
|
|
|
|
func isDiskQuorum(errs []error, minQuorumCount int) bool {
|
|
|
|
var count int
|
2016-08-25 12:39:01 -04:00
|
|
|
errs = errorsCause(errs)
|
2016-06-17 14:57:51 -04:00
|
|
|
for _, err := range errs {
|
2016-11-20 19:57:12 -05:00
|
|
|
// Check if the error can be ignored for quorum verification.
|
|
|
|
if !isErrIgnored(err, quorumIgnoredErrs) {
|
|
|
|
count++
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-13 03:29:48 -04:00
|
|
|
return count >= minQuorumCount
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to 'len(slice)' but returns the actual elements count
|
|
|
|
// skipping the unallocated elements.
|
|
|
|
func diskCount(disks []StorageAPI) int {
|
|
|
|
diskCount := 0
|
|
|
|
for _, disk := range disks {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
diskCount++
|
|
|
|
}
|
|
|
|
return diskCount
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:07:00 -04:00
|
|
|
// hashOrder - hashes input key to return returns consistent
|
|
|
|
// 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 {
|
|
|
|
if cardinality < 0 {
|
|
|
|
// Returns an empty int slice for negative cardinality.
|
|
|
|
return nil
|
2016-05-25 19:42:31 -04:00
|
|
|
}
|
2016-07-21 22:07:00 -04:00
|
|
|
nums := make([]int, cardinality)
|
|
|
|
keyCrc := crc32.Checksum([]byte(key), crc32.IEEETable)
|
2016-07-08 10:33:21 -04:00
|
|
|
|
2016-07-21 22:07:00 -04:00
|
|
|
start := int(uint32(keyCrc)%uint32(cardinality)) | 1
|
|
|
|
for i := 1; i <= cardinality; i++ {
|
|
|
|
nums[i-1] = 1 + ((start + i) % cardinality)
|
2016-05-25 19:42:31 -04:00
|
|
|
}
|
2016-07-08 10:33:21 -04:00
|
|
|
return nums
|
2016-05-25 19:42:31 -04:00
|
|
|
}
|
|
|
|
|
2016-09-09 01:38:18 -04:00
|
|
|
func parseXLStat(xlMetaBuf []byte) (statInfo, error) {
|
|
|
|
// obtain stat info.
|
|
|
|
stat := statInfo{}
|
|
|
|
// fetching modTime.
|
|
|
|
modTime, err := time.Parse(time.RFC3339, gjson.GetBytes(xlMetaBuf, "stat.modTime").String())
|
|
|
|
if err != nil {
|
|
|
|
return statInfo{}, err
|
|
|
|
}
|
|
|
|
stat.ModTime = modTime
|
|
|
|
// obtain Stat.Size .
|
|
|
|
stat.Size = gjson.GetBytes(xlMetaBuf, "stat.size").Int()
|
|
|
|
return stat, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXLVersion(xlMetaBuf []byte) string {
|
|
|
|
return gjson.GetBytes(xlMetaBuf, "version").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXLFormat(xlMetaBuf []byte) string {
|
|
|
|
return gjson.GetBytes(xlMetaBuf, "format").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXLRelease(xlMetaBuf []byte) string {
|
|
|
|
return gjson.GetBytes(xlMetaBuf, "minio.release").String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXLErasureInfo(xlMetaBuf []byte) erasureInfo {
|
|
|
|
erasure := erasureInfo{}
|
|
|
|
erasureResult := gjson.GetBytes(xlMetaBuf, "erasure")
|
|
|
|
// parse the xlV1Meta.Erasure.Distribution.
|
|
|
|
disResult := erasureResult.Get("distribution").Array()
|
|
|
|
|
|
|
|
distribution := make([]int, len(disResult))
|
|
|
|
for i, dis := range disResult {
|
|
|
|
distribution[i] = int(dis.Int())
|
|
|
|
}
|
|
|
|
erasure.Distribution = distribution
|
|
|
|
|
|
|
|
erasure.Algorithm = erasureResult.Get("algorithm").String()
|
|
|
|
erasure.DataBlocks = int(erasureResult.Get("data").Int())
|
|
|
|
erasure.ParityBlocks = int(erasureResult.Get("parity").Int())
|
|
|
|
erasure.BlockSize = erasureResult.Get("blockSize").Int()
|
|
|
|
erasure.Index = int(erasureResult.Get("index").Int())
|
|
|
|
// Pare xlMetaV1.Erasure.Checksum array.
|
|
|
|
checkSumsResult := erasureResult.Get("checksum").Array()
|
|
|
|
checkSums := make([]checkSumInfo, len(checkSumsResult))
|
|
|
|
for i, checkSumResult := range checkSumsResult {
|
|
|
|
checkSum := checkSumInfo{}
|
|
|
|
checkSum.Name = checkSumResult.Get("name").String()
|
|
|
|
checkSum.Algorithm = checkSumResult.Get("algorithm").String()
|
|
|
|
checkSum.Hash = checkSumResult.Get("hash").String()
|
|
|
|
checkSums[i] = checkSum
|
|
|
|
}
|
|
|
|
erasure.Checksum = checkSums
|
|
|
|
|
|
|
|
return erasure
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXLParts(xlMetaBuf []byte) []objectPartInfo {
|
|
|
|
// Parse the XL Parts.
|
|
|
|
partsResult := gjson.GetBytes(xlMetaBuf, "parts").Array()
|
|
|
|
partInfo := make([]objectPartInfo, len(partsResult))
|
|
|
|
for i, p := range partsResult {
|
|
|
|
info := objectPartInfo{}
|
|
|
|
info.Number = int(p.Get("number").Int())
|
|
|
|
info.Name = p.Get("name").String()
|
|
|
|
info.ETag = p.Get("etag").String()
|
|
|
|
info.Size = p.Get("size").Int()
|
|
|
|
partInfo[i] = info
|
|
|
|
}
|
|
|
|
return partInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseXLMetaMap(xlMetaBuf []byte) map[string]string {
|
|
|
|
// Get xlMetaV1.Meta map.
|
|
|
|
metaMapResult := gjson.GetBytes(xlMetaBuf, "meta").Map()
|
|
|
|
metaMap := make(map[string]string)
|
|
|
|
for key, valResult := range metaMapResult {
|
|
|
|
metaMap[key] = valResult.String()
|
|
|
|
}
|
|
|
|
return metaMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs XLMetaV1 using `gjson` lib to retrieve each field.
|
|
|
|
func xlMetaV1UnmarshalJSON(xlMetaBuf []byte) (xlMetaV1, error) {
|
|
|
|
xlMeta := xlMetaV1{}
|
|
|
|
// obtain version.
|
|
|
|
xlMeta.Version = parseXLVersion(xlMetaBuf)
|
|
|
|
// obtain format.
|
|
|
|
xlMeta.Format = parseXLFormat(xlMetaBuf)
|
|
|
|
// Parse xlMetaV1.Stat .
|
|
|
|
stat, err := parseXLStat(xlMetaBuf)
|
|
|
|
if err != nil {
|
|
|
|
return xlMetaV1{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
xlMeta.Stat = stat
|
|
|
|
// parse the xlV1Meta.Erasure fields.
|
|
|
|
xlMeta.Erasure = parseXLErasureInfo(xlMetaBuf)
|
|
|
|
|
|
|
|
// Parse the XL Parts.
|
|
|
|
xlMeta.Parts = parseXLParts(xlMetaBuf)
|
|
|
|
// Get the xlMetaV1.Realse field.
|
|
|
|
xlMeta.Minio.Release = parseXLRelease(xlMetaBuf)
|
|
|
|
// parse xlMetaV1.
|
|
|
|
xlMeta.Meta = parseXLMetaMap(xlMetaBuf)
|
|
|
|
|
|
|
|
return xlMeta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// read xl.json from the given disk, parse and return xlV1MetaV1.Parts.
|
|
|
|
func readXLMetaParts(disk StorageAPI, bucket string, object string) ([]objectPartInfo, error) {
|
|
|
|
// Reads entire `xl.json`.
|
|
|
|
xlMetaBuf, err := disk.ReadAll(bucket, path.Join(object, xlMetaJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
return nil, traceError(err)
|
|
|
|
}
|
|
|
|
// obtain xlMetaV1{}.Partsusing `github.com/tidwall/gjson`.
|
|
|
|
xlMetaParts := parseXLParts(xlMetaBuf)
|
|
|
|
|
|
|
|
return xlMetaParts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// read xl.json from the given disk and parse xlV1Meta.Stat and xlV1Meta.Meta using gjson.
|
|
|
|
func readXLMetaStat(disk StorageAPI, bucket string, object string) (statInfo, map[string]string, error) {
|
|
|
|
// Reads entire `xl.json`.
|
|
|
|
xlMetaBuf, err := disk.ReadAll(bucket, path.Join(object, xlMetaJSONFile))
|
|
|
|
if err != nil {
|
|
|
|
return statInfo{}, nil, traceError(err)
|
|
|
|
}
|
|
|
|
// obtain xlMetaV1{}.Meta using `github.com/tidwall/gjson`.
|
|
|
|
xlMetaMap := parseXLMetaMap(xlMetaBuf)
|
|
|
|
|
|
|
|
// obtain xlMetaV1{}.Stat using `github.com/tidwall/gjson`.
|
|
|
|
xlStat, err := parseXLStat(xlMetaBuf)
|
|
|
|
if err != nil {
|
|
|
|
return statInfo{}, nil, traceError(err)
|
|
|
|
}
|
|
|
|
// Return structured `xl.json`.
|
|
|
|
return xlStat, xlMetaMap, nil
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:51:06 -04:00
|
|
|
// readXLMeta reads `xl.json` and returns back XL metadata structure.
|
2016-06-24 05:06:23 -04:00
|
|
|
func readXLMeta(disk StorageAPI, bucket string, object string) (xlMeta xlMetaV1, err error) {
|
|
|
|
// Reads entire `xl.json`.
|
2016-09-09 01:38:18 -04:00
|
|
|
xlMetaBuf, err := disk.ReadAll(bucket, path.Join(object, xlMetaJSONFile))
|
2016-06-25 17:51:06 -04:00
|
|
|
if err != nil {
|
2016-08-25 12:39:01 -04:00
|
|
|
return xlMetaV1{}, traceError(err)
|
2016-06-02 19:34:15 -04:00
|
|
|
}
|
2016-09-09 01:38:18 -04:00
|
|
|
// obtain xlMetaV1{} using `github.com/tidwall/gjson`.
|
|
|
|
xlMeta, err = xlMetaV1UnmarshalJSON(xlMetaBuf)
|
|
|
|
if err != nil {
|
2016-08-25 12:39:01 -04:00
|
|
|
return xlMetaV1{}, traceError(err)
|
2016-05-25 19:42:31 -04:00
|
|
|
}
|
2016-06-24 05:06:23 -04:00
|
|
|
// Return structured `xl.json`.
|
|
|
|
return xlMeta, nil
|
2016-05-25 19:42:31 -04:00
|
|
|
}
|
2016-07-12 21:23:40 -04:00
|
|
|
|
2016-07-26 14:34:48 -04:00
|
|
|
// Reads all `xl.json` metadata as a xlMetaV1 slice.
|
|
|
|
// Returns error slice indicating the failed metadata reads.
|
|
|
|
func readAllXLMetadata(disks []StorageAPI, bucket, object string) ([]xlMetaV1, []error) {
|
|
|
|
errs := make([]error, len(disks))
|
|
|
|
metadataArray := make([]xlMetaV1, len(disks))
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
// Read `xl.json` parallelly across disks.
|
|
|
|
for index, disk := range disks {
|
|
|
|
if disk == nil {
|
|
|
|
errs[index] = errDiskNotFound
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
// Read `xl.json` in routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
var err error
|
|
|
|
metadataArray[index], err = readXLMeta(disk, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
errs[index] = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the routines to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Return all the metadata.
|
|
|
|
return metadataArray, errs
|
|
|
|
}
|
|
|
|
|
2016-07-16 11:35:30 -04:00
|
|
|
// Return ordered partsMetadata depeinding on distribution.
|
|
|
|
func getOrderedPartsMetadata(distribution []int, partsMetadata []xlMetaV1) (orderedPartsMetadata []xlMetaV1) {
|
|
|
|
orderedPartsMetadata = make([]xlMetaV1, len(partsMetadata))
|
|
|
|
for index := range partsMetadata {
|
|
|
|
blockIndex := distribution[index]
|
|
|
|
orderedPartsMetadata[blockIndex-1] = partsMetadata[index]
|
|
|
|
}
|
|
|
|
return orderedPartsMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOrderedDisks - get ordered disks from erasure distribution.
|
|
|
|
// returns ordered slice of disks from their actual distribution.
|
|
|
|
func getOrderedDisks(distribution []int, disks []StorageAPI) (orderedDisks []StorageAPI) {
|
|
|
|
orderedDisks = make([]StorageAPI, len(disks))
|
|
|
|
// From disks gets ordered disks.
|
|
|
|
for index := range disks {
|
|
|
|
blockIndex := distribution[index]
|
|
|
|
orderedDisks[blockIndex-1] = disks[index]
|
|
|
|
}
|
|
|
|
return orderedDisks
|
|
|
|
}
|