2016-05-20 23:48:47 -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 (
|
|
|
|
"encoding/json"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-05-24 20:48:58 -04:00
|
|
|
const (
|
2016-05-29 18:38:14 -04:00
|
|
|
// Erasure related constants.
|
2016-05-24 20:48:58 -04:00
|
|
|
erasureAlgorithmKlauspost = "klauspost/reedsolomon/vandermonde"
|
|
|
|
erasureAlgorithmISAL = "isa-l/reedsolomon/cauchy"
|
|
|
|
)
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
// objectPartInfo Info of each part kept in the multipart metadata
|
|
|
|
// file after CompleteMultipartUpload() is called.
|
|
|
|
type objectPartInfo struct {
|
2016-05-25 00:24:20 -04:00
|
|
|
Number int `json:"number"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
ETag string `json:"etag"`
|
|
|
|
Size int64 `json:"size"`
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// byPartName is a collection satisfying sort.Interface.
|
|
|
|
type byPartNumber []objectPartInfo
|
|
|
|
|
|
|
|
func (t byPartNumber) Len() int { return len(t) }
|
|
|
|
func (t byPartNumber) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
|
|
|
|
func (t byPartNumber) Less(i, j int) bool { return t[i].Number < t[j].Number }
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// A xlMetaV1 represents a metadata header mapping keys to sets of values.
|
|
|
|
type xlMetaV1 struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
Stat struct {
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
ModTime time.Time `json:"modTime"`
|
|
|
|
Version int64 `json:"version"`
|
|
|
|
} `json:"stat"`
|
|
|
|
Erasure struct {
|
2016-05-24 20:48:58 -04:00
|
|
|
Algorithm string `json:"algorithm"`
|
|
|
|
DataBlocks int `json:"data"`
|
|
|
|
ParityBlocks int `json:"parity"`
|
|
|
|
BlockSize int64 `json:"blockSize"`
|
|
|
|
Index int `json:"index"`
|
|
|
|
Distribution []int `json:"distribution"`
|
|
|
|
Checksum []struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Algorithm string `json:"algorithm"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
} `json:"checksum"`
|
2016-05-20 23:48:47 -04:00
|
|
|
} `json:"erasure"`
|
|
|
|
Minio struct {
|
|
|
|
Release string `json:"release"`
|
|
|
|
} `json:"minio"`
|
|
|
|
Meta map[string]string `json:"meta"`
|
|
|
|
Parts []objectPartInfo `json:"parts,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-05-30 19:51:59 -04:00
|
|
|
// newXLMetaV1 - initializes new xlMetaV1.
|
|
|
|
func newXLMetaV1(dataBlocks, parityBlocks int) (xlMeta xlMetaV1) {
|
|
|
|
xlMeta = xlMetaV1{}
|
|
|
|
xlMeta.Version = "1"
|
|
|
|
xlMeta.Format = "xl"
|
|
|
|
xlMeta.Minio.Release = minioReleaseTag
|
|
|
|
xlMeta.Erasure.Algorithm = erasureAlgorithmKlauspost
|
|
|
|
xlMeta.Erasure.DataBlocks = dataBlocks
|
|
|
|
xlMeta.Erasure.ParityBlocks = parityBlocks
|
|
|
|
xlMeta.Erasure.BlockSize = blockSizeV1
|
|
|
|
xlMeta.Erasure.Distribution = randInts(dataBlocks + parityBlocks)
|
|
|
|
return xlMeta
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// ObjectPartIndex - returns the index of matching object part number.
|
|
|
|
func (m xlMetaV1) ObjectPartIndex(partNumber int) (index int) {
|
2016-05-20 23:48:47 -04:00
|
|
|
for i, part := range m.Parts {
|
2016-05-26 06:15:01 -04:00
|
|
|
if partNumber == part.Number {
|
|
|
|
index = i
|
|
|
|
return index
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddObjectPart - add a new object part in order.
|
2016-05-26 06:15:01 -04:00
|
|
|
func (m *xlMetaV1) AddObjectPart(partNumber int, partName string, partETag string, partSize int64) {
|
2016-05-25 00:24:20 -04:00
|
|
|
partInfo := objectPartInfo{
|
2016-05-26 06:15:01 -04:00
|
|
|
Number: partNumber,
|
|
|
|
Name: partName,
|
|
|
|
ETag: partETag,
|
|
|
|
Size: partSize,
|
2016-05-25 00:24:20 -04:00
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
|
|
|
|
// Update part info if it already exists.
|
2016-05-25 00:24:20 -04:00
|
|
|
for i, part := range m.Parts {
|
2016-05-26 06:15:01 -04:00
|
|
|
if partNumber == part.Number {
|
2016-05-25 00:24:20 -04:00
|
|
|
m.Parts[i] = partInfo
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
|
|
|
|
// Proceed to include new part info.
|
2016-05-25 00:24:20 -04:00
|
|
|
m.Parts = append(m.Parts, partInfo)
|
2016-05-26 06:15:01 -04:00
|
|
|
|
|
|
|
// Parts in xlMeta should be in sorted order by part number.
|
2016-05-25 00:24:20 -04:00
|
|
|
sort.Sort(byPartNumber(m.Parts))
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// objectToPartOffset - translate offset of an object to offset of its individual part.
|
|
|
|
func (m xlMetaV1) objectToPartOffset(offset int64) (partIndex int, partOffset int64, err error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
partOffset = offset
|
2016-05-26 06:15:01 -04:00
|
|
|
// Seek until object offset maps to a particular part offset.
|
2016-05-20 23:48:47 -04:00
|
|
|
for i, part := range m.Parts {
|
2016-05-25 00:24:20 -04:00
|
|
|
partIndex = i
|
2016-05-26 06:15:01 -04:00
|
|
|
// Last part can be of '0' bytes, treat it specially and
|
|
|
|
// return right here.
|
2016-05-20 23:48:47 -04:00
|
|
|
if part.Size == 0 {
|
2016-05-25 00:24:20 -04:00
|
|
|
return partIndex, partOffset, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
// Offset is smaller than size we have reached the proper part offset.
|
2016-05-20 23:48:47 -04:00
|
|
|
if partOffset < part.Size {
|
2016-05-25 00:24:20 -04:00
|
|
|
return partIndex, partOffset, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
// Continue to towards the next part.
|
2016-05-20 23:48:47 -04:00
|
|
|
partOffset -= part.Size
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
// Offset beyond the size of the object return InvalidRange.
|
|
|
|
return 0, 0, InvalidRange{}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// readXLMetadata - returns the object metadata `xl.json` content from
|
|
|
|
// one of the disks picked at random.
|
2016-05-25 04:33:39 -04:00
|
|
|
func (xl xlObjects) readXLMetadata(bucket, object string) (xlMeta xlMetaV1, err error) {
|
|
|
|
// Count for errors encountered.
|
|
|
|
var xlJSONErrCount = 0
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// Return the first successful lookup from a random list of disks.
|
2016-05-25 04:33:39 -04:00
|
|
|
for xlJSONErrCount < len(xl.storageDisks) {
|
2016-05-26 06:15:01 -04:00
|
|
|
disk := xl.getRandomDisk() // Choose a random disk on each attempt.
|
2016-05-30 19:51:59 -04:00
|
|
|
var buffer []byte
|
|
|
|
buffer, err = readAll(disk, bucket, path.Join(object, xlMetaJSONFile))
|
2016-05-25 04:33:39 -04:00
|
|
|
if err == nil {
|
2016-05-30 19:51:59 -04:00
|
|
|
err = json.Unmarshal(buffer, &xlMeta)
|
2016-05-25 04:33:39 -04:00
|
|
|
if err == nil {
|
|
|
|
return xlMeta, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xlJSONErrCount++ // Update error count.
|
|
|
|
}
|
|
|
|
return xlMetaV1{}, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-29 03:42:09 -04:00
|
|
|
// renameXLMetadata - renames `xl.json` from source prefix to destination prefix.
|
2016-05-28 18:13:15 -04:00
|
|
|
func (xl xlObjects) renameXLMetadata(srcBucket, srcPrefix, dstBucket, dstPrefix string) error {
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
var mErrs = make([]error, len(xl.storageDisks))
|
|
|
|
|
|
|
|
srcJSONFile := path.Join(srcPrefix, xlMetaJSONFile)
|
|
|
|
dstJSONFile := path.Join(dstPrefix, xlMetaJSONFile)
|
|
|
|
// Rename `xl.json` to all disks in parallel.
|
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
wg.Add(1)
|
|
|
|
// Rename `xl.json` in a routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-05-29 03:42:09 -04:00
|
|
|
// Renames `xl.json` from source prefix to destination prefix.
|
2016-05-28 18:13:15 -04:00
|
|
|
rErr := disk.RenameFile(srcBucket, srcJSONFile, dstBucket, dstJSONFile)
|
|
|
|
if rErr != nil {
|
|
|
|
mErrs[index] = rErr
|
|
|
|
return
|
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
// Delete any dangling directories.
|
|
|
|
dErr := disk.DeleteFile(srcBucket, srcPrefix)
|
|
|
|
if dErr != nil {
|
|
|
|
mErrs[index] = dErr
|
|
|
|
return
|
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
mErrs[index] = nil
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
// Wait for all the routines.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Return the first error.
|
|
|
|
for _, err := range mErrs {
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// writeXLMetadata - write `xl.json` on all disks in order.
|
|
|
|
func (xl xlObjects) writeXLMetadata(bucket, prefix string, xlMeta xlMetaV1) error {
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
var mErrs = make([]error, len(xl.storageDisks))
|
|
|
|
|
2016-05-28 18:13:15 -04:00
|
|
|
jsonFile := path.Join(prefix, xlMetaJSONFile)
|
2016-05-26 06:15:01 -04:00
|
|
|
// Start writing `xl.json` to all disks in parallel.
|
2016-05-20 23:48:47 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
wg.Add(1)
|
2016-05-26 06:15:01 -04:00
|
|
|
// Write `xl.json` in a routine.
|
2016-05-20 23:48:47 -04:00
|
|
|
go func(index int, disk StorageAPI, metadata xlMetaV1) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// Save the disk order index.
|
2016-05-20 23:48:47 -04:00
|
|
|
metadata.Erasure.Index = index + 1
|
2016-05-26 06:15:01 -04:00
|
|
|
|
2016-05-28 18:13:15 -04:00
|
|
|
metadataBytes, err := json.Marshal(&metadata)
|
|
|
|
if err != nil {
|
|
|
|
mErrs[index] = err
|
|
|
|
return
|
|
|
|
}
|
2016-05-30 19:51:59 -04:00
|
|
|
// Persist marshalled data.
|
2016-05-28 18:13:15 -04:00
|
|
|
n, mErr := disk.AppendFile(bucket, jsonFile, metadataBytes)
|
2016-05-20 23:48:47 -04:00
|
|
|
if mErr != nil {
|
|
|
|
mErrs[index] = mErr
|
|
|
|
return
|
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
if n != int64(len(metadataBytes)) {
|
|
|
|
mErrs[index] = errUnexpected
|
2016-05-20 23:48:47 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mErrs[index] = nil
|
|
|
|
}(index, disk, xlMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the routines.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-05-24 20:48:58 -04:00
|
|
|
// Return the first error.
|
2016-05-20 23:48:47 -04:00
|
|
|
for _, err := range mErrs {
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|