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.
311 lines
8.7 KiB
311 lines
8.7 KiB
9 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"path"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
9 years ago
|
// updateUploadsJSON - update `uploads.json` with new uploadsJSON for all disks.
|
||
9 years ago
|
func (xl xlObjects) updateUploadsJSON(bucket, object string, uploadsJSON uploadsV1) (err error) {
|
||
9 years ago
|
uploadsPath := path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)
|
||
9 years ago
|
uniqueID := getUUID()
|
||
|
tmpUploadsPath := path.Join(tmpMetaPrefix, uniqueID)
|
||
9 years ago
|
var errs = make([]error, len(xl.storageDisks))
|
||
9 years ago
|
var wg = &sync.WaitGroup{}
|
||
|
|
||
9 years ago
|
// Update `uploads.json` for all the disks.
|
||
9 years ago
|
for index, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
errs[index] = errDiskNotFound
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
wg.Add(1)
|
||
9 years ago
|
// Update `uploads.json` in routine.
|
||
9 years ago
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
9 years ago
|
uploadsBytes, wErr := json.Marshal(uploadsJSON)
|
||
9 years ago
|
if wErr != nil {
|
||
|
errs[index] = wErr
|
||
|
return
|
||
|
}
|
||
9 years ago
|
if wErr = disk.AppendFile(minioMetaBucket, tmpUploadsPath, uploadsBytes); wErr != nil {
|
||
9 years ago
|
errs[index] = wErr
|
||
|
return
|
||
|
}
|
||
9 years ago
|
if wErr = disk.RenameFile(minioMetaBucket, tmpUploadsPath, minioMetaBucket, uploadsPath); wErr != nil {
|
||
9 years ago
|
errs[index] = wErr
|
||
|
return
|
||
|
}
|
||
|
}(index, disk)
|
||
|
}
|
||
|
|
||
9 years ago
|
// Wait for all the routines to finish updating `uploads.json`
|
||
9 years ago
|
wg.Wait()
|
||
|
|
||
9 years ago
|
// Count all the errors and validate if we have write quorum.
|
||
9 years ago
|
if !isDiskQuorum(errs, xl.writeQuorum) {
|
||
9 years ago
|
// Do we have readQuorum?.
|
||
|
if isDiskQuorum(errs, xl.readQuorum) {
|
||
|
return nil
|
||
|
}
|
||
9 years ago
|
// Rename `uploads.json` left over back to tmp location.
|
||
9 years ago
|
for index, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
// Undo rename `uploads.json` in parallel.
|
||
|
wg.Add(1)
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
if errs[index] != nil {
|
||
|
return
|
||
|
}
|
||
|
_ = disk.RenameFile(minioMetaBucket, uploadsPath, minioMetaBucket, tmpUploadsPath)
|
||
|
}(index, disk)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
return errXLWriteQuorum
|
||
|
}
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// Reads uploads.json from any of the load balanced disks.
|
||
|
func (xl xlObjects) readUploadsJSON(bucket, object string) (uploadsJSON uploadsV1, err error) {
|
||
|
for _, disk := range xl.getLoadBalancedDisks() {
|
||
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
uploadsJSON, err = readUploadsJSON(bucket, object, disk)
|
||
|
if err == nil {
|
||
|
return uploadsJSON, nil
|
||
|
}
|
||
|
if isErrIgnored(err, objMetadataOpIgnoredErrs) {
|
||
|
continue
|
||
|
}
|
||
|
break
|
||
|
}
|
||
|
return uploadsV1{}, err
|
||
|
}
|
||
|
|
||
9 years ago
|
// writeUploadJSON - create `uploads.json` or update it with new uploadID.
|
||
9 years ago
|
func (xl xlObjects) writeUploadJSON(bucket, object, uploadID string, initiated time.Time) (err error) {
|
||
9 years ago
|
uploadsPath := path.Join(mpartMetaPrefix, bucket, object, uploadsJSONFile)
|
||
9 years ago
|
uniqueID := getUUID()
|
||
|
tmpUploadsPath := path.Join(tmpMetaPrefix, uniqueID)
|
||
9 years ago
|
|
||
9 years ago
|
var errs = make([]error, len(xl.storageDisks))
|
||
9 years ago
|
var wg = &sync.WaitGroup{}
|
||
|
|
||
9 years ago
|
// Reads `uploads.json` and returns error.
|
||
|
uploadsJSON, err := xl.readUploadsJSON(bucket, object)
|
||
9 years ago
|
if err != nil {
|
||
|
if err != errFileNotFound {
|
||
|
return err
|
||
|
}
|
||
9 years ago
|
// Set uploads format to `xl` otherwise.
|
||
|
uploadsJSON = newUploadsV1("xl")
|
||
9 years ago
|
}
|
||
9 years ago
|
// Add a new upload id.
|
||
|
uploadsJSON.AddUploadID(uploadID, initiated)
|
||
9 years ago
|
|
||
9 years ago
|
// Update `uploads.json` on all disks.
|
||
9 years ago
|
for index, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
errs[index] = errDiskNotFound
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
wg.Add(1)
|
||
9 years ago
|
// Update `uploads.json` in a routine.
|
||
9 years ago
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
9 years ago
|
uploadsJSONBytes, wErr := json.Marshal(&uploadsJSON)
|
||
9 years ago
|
if wErr != nil {
|
||
|
errs[index] = wErr
|
||
|
return
|
||
|
}
|
||
9 years ago
|
// Write `uploads.json` to disk.
|
||
9 years ago
|
if wErr = disk.AppendFile(minioMetaBucket, tmpUploadsPath, uploadsJSONBytes); wErr != nil {
|
||
9 years ago
|
errs[index] = wErr
|
||
|
return
|
||
|
}
|
||
9 years ago
|
wErr = disk.RenameFile(minioMetaBucket, tmpUploadsPath, minioMetaBucket, uploadsPath)
|
||
9 years ago
|
if wErr != nil {
|
||
|
if dErr := disk.DeleteFile(minioMetaBucket, tmpUploadsPath); dErr != nil {
|
||
|
errs[index] = dErr
|
||
|
return
|
||
|
}
|
||
|
errs[index] = wErr
|
||
|
return
|
||
|
}
|
||
9 years ago
|
errs[index] = nil
|
||
9 years ago
|
}(index, disk)
|
||
|
}
|
||
|
|
||
9 years ago
|
// Wait here for all the writes to finish.
|
||
9 years ago
|
wg.Wait()
|
||
|
|
||
9 years ago
|
// Do we have write quorum?.
|
||
9 years ago
|
if !isDiskQuorum(errs, xl.writeQuorum) {
|
||
9 years ago
|
// Rename `uploads.json` left over back to tmp location.
|
||
9 years ago
|
for index, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
// Undo rename `uploads.json` in parallel.
|
||
|
wg.Add(1)
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
if errs[index] != nil {
|
||
|
return
|
||
|
}
|
||
|
_ = disk.RenameFile(minioMetaBucket, uploadsPath, minioMetaBucket, tmpUploadsPath)
|
||
|
}(index, disk)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
return errXLWriteQuorum
|
||
|
}
|
||
9 years ago
|
|
||
|
// Ignored errors list.
|
||
|
ignoredErrs := []error{
|
||
|
errDiskNotFound,
|
||
|
errFaultyDisk,
|
||
|
errDiskAccessDenied,
|
||
|
}
|
||
|
return reduceErrs(errs, ignoredErrs)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Returns if the prefix is a multipart upload.
|
||
|
func (xl xlObjects) isMultipartUpload(bucket, prefix string) bool {
|
||
9 years ago
|
for _, disk := range xl.getLoadBalancedDisks() {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
_, err := disk.StatFile(bucket, pathJoin(prefix, uploadsJSONFile))
|
||
9 years ago
|
if err == nil {
|
||
|
return true
|
||
|
}
|
||
|
// For any reason disk was deleted or goes offline, continue
|
||
|
if isErrIgnored(err, objMetadataOpIgnoredErrs) {
|
||
|
continue
|
||
9 years ago
|
}
|
||
9 years ago
|
break
|
||
9 years ago
|
}
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// isUploadIDExists - verify if a given uploadID exists and is valid.
|
||
|
func (xl xlObjects) isUploadIDExists(bucket, object, uploadID string) bool {
|
||
|
uploadIDPath := path.Join(mpartMetaPrefix, bucket, object, uploadID)
|
||
|
return xl.isObject(minioMetaBucket, uploadIDPath)
|
||
|
}
|
||
9 years ago
|
|
||
|
// Removes part given by partName belonging to a mulitpart upload from minioMetaBucket
|
||
|
func (xl xlObjects) removeObjectPart(bucket, object, uploadID, partName string) {
|
||
|
curpartPath := path.Join(mpartMetaPrefix, bucket, object, uploadID, partName)
|
||
|
wg := sync.WaitGroup{}
|
||
|
for i, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
wg.Add(1)
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
// Ignoring failure to remove parts that weren't present in CompleteMultipartUpload
|
||
|
// requests. xl.json is the authoritative source of truth on which parts constitute
|
||
|
// the object. The presence of parts that don't belong in the object doesn't affect correctness.
|
||
|
_ = disk.DeleteFile(minioMetaBucket, curpartPath)
|
||
|
}(i, disk)
|
||
|
}
|
||
|
wg.Wait()
|
||
|
}
|
||
9 years ago
|
|
||
|
// statPart - returns fileInfo structure for a successful stat on part file.
|
||
|
func (xl xlObjects) statPart(bucket, object, uploadID, partName string) (fileInfo FileInfo, err error) {
|
||
|
partNamePath := path.Join(mpartMetaPrefix, bucket, object, uploadID, partName)
|
||
9 years ago
|
for _, disk := range xl.getLoadBalancedDisks() {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
fileInfo, err = disk.StatFile(minioMetaBucket, partNamePath)
|
||
9 years ago
|
if err == nil {
|
||
|
return fileInfo, nil
|
||
|
}
|
||
9 years ago
|
|
||
|
// For any reason disk was deleted or goes offline we continue to next disk.
|
||
9 years ago
|
if isErrIgnored(err, objMetadataOpIgnoredErrs) {
|
||
|
continue
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// Catastrophic error, we return.
|
||
9 years ago
|
break
|
||
|
}
|
||
9 years ago
|
return FileInfo{}, err
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// commitXLMetadata - commit `xl.json` from source prefix to destination prefix in the given slice of disks.
|
||
9 years ago
|
func commitXLMetadata(disks []StorageAPI, srcPrefix, dstPrefix string, quorum int) error {
|
||
9 years ago
|
var wg = &sync.WaitGroup{}
|
||
9 years ago
|
var mErrs = make([]error, len(disks))
|
||
9 years ago
|
|
||
|
srcJSONFile := path.Join(srcPrefix, xlMetaJSONFile)
|
||
|
dstJSONFile := path.Join(dstPrefix, xlMetaJSONFile)
|
||
|
|
||
|
// Rename `xl.json` to all disks in parallel.
|
||
9 years ago
|
for index, disk := range disks {
|
||
9 years ago
|
if disk == nil {
|
||
|
mErrs[index] = errDiskNotFound
|
||
|
continue
|
||
|
}
|
||
|
wg.Add(1)
|
||
|
// Rename `xl.json` in a routine.
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
9 years ago
|
// Delete any dangling directories.
|
||
|
defer disk.DeleteFile(minioMetaBucket, srcPrefix)
|
||
|
|
||
9 years ago
|
// Renames `xl.json` from source prefix to destination prefix.
|
||
|
rErr := disk.RenameFile(minioMetaBucket, srcJSONFile, minioMetaBucket, dstJSONFile)
|
||
|
if rErr != nil {
|
||
|
mErrs[index] = rErr
|
||
|
return
|
||
|
}
|
||
|
mErrs[index] = nil
|
||
|
}(index, disk)
|
||
|
}
|
||
|
// Wait for all the routines.
|
||
|
wg.Wait()
|
||
|
|
||
9 years ago
|
// Do we have write Quorum?.
|
||
9 years ago
|
if !isDiskQuorum(mErrs, quorum) {
|
||
9 years ago
|
// Delete all `xl.json` successfully renamed.
|
||
|
deleteAllXLMetadata(disks, minioMetaBucket, dstPrefix, mErrs)
|
||
9 years ago
|
return errXLWriteQuorum
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// List of ignored errors.
|
||
|
ignoredErrs := []error{
|
||
9 years ago
|
errDiskNotFound,
|
||
|
errDiskAccessDenied,
|
||
|
errFaultyDisk,
|
||
9 years ago
|
}
|
||
|
return reduceErrs(mErrs, ignoredErrs)
|
||
9 years ago
|
}
|