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.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"sync"
|
2016-11-22 18:29:39 -05:00
|
|
|
"time"
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
2016-11-22 18:29:39 -05:00
|
|
|
// updateUploadJSON - add or remove upload ID info in all `uploads.json`.
|
|
|
|
func (xl xlObjects) updateUploadJSON(bucket, object, uploadID string, initiated time.Time, isRemove bool) error {
|
2016-11-22 16:15:06 -05:00
|
|
|
uploadsPath := path.Join(bucket, object, uploadsJSONFile)
|
2016-11-22 18:29:39 -05:00
|
|
|
tmpUploadsPath := getUUID()
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-10-30 12:27:29 -04:00
|
|
|
// slice to store errors from disks
|
|
|
|
errs := make([]error, len(xl.storageDisks))
|
|
|
|
// slice to store if it is a delete operation on a disk
|
|
|
|
isDelete := make([]bool, len(xl.storageDisks))
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
2016-06-29 05:10:40 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
2016-10-30 12:27:29 -04:00
|
|
|
errs[index] = traceError(errDiskNotFound)
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-10-30 12:27:29 -04:00
|
|
|
// Update `uploads.json` in a go routine.
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-10-30 12:27:29 -04:00
|
|
|
|
|
|
|
// read and parse uploads.json on this disk
|
|
|
|
uploadsJSON, err := readUploadsJSON(bucket, object, disk)
|
|
|
|
if errorCause(err) == errFileNotFound {
|
|
|
|
// If file is not found, we assume an
|
|
|
|
// default (empty) upload info.
|
|
|
|
uploadsJSON, err = newUploadsV1("xl"), nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-10-30 12:27:29 -04:00
|
|
|
// If we have a read error, we store error and
|
|
|
|
// exit.
|
|
|
|
if err != nil {
|
2016-11-01 23:04:32 -04:00
|
|
|
errs[index] = err
|
2016-05-20 23:48:47 -04:00
|
|
|
return
|
|
|
|
}
|
2016-10-30 12:27:29 -04:00
|
|
|
|
2016-11-22 18:29:39 -05:00
|
|
|
if !isRemove {
|
2016-10-30 12:27:29 -04:00
|
|
|
// Add the uploadID
|
2016-11-22 18:29:39 -05:00
|
|
|
uploadsJSON.AddUploadID(uploadID, initiated)
|
2016-10-30 12:27:29 -04:00
|
|
|
} else {
|
|
|
|
// Remove the upload ID
|
2016-11-22 18:29:39 -05:00
|
|
|
uploadsJSON.RemoveUploadID(uploadID)
|
2016-10-30 12:27:29 -04:00
|
|
|
if len(uploadsJSON.Uploads) == 0 {
|
|
|
|
isDelete[index] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For delete, rename to tmp, for the
|
|
|
|
// possibility of recovery in case of quorum
|
|
|
|
// failure.
|
|
|
|
if !isDelete[index] {
|
|
|
|
errs[index] = writeUploadJSON(&uploadsJSON, uploadsPath, tmpUploadsPath, disk)
|
|
|
|
} else {
|
2016-11-22 16:15:06 -05:00
|
|
|
wErr := disk.RenameFile(minioMetaMultipartBucket, uploadsPath, minioMetaTmpBucket, tmpUploadsPath)
|
2016-10-30 12:27:29 -04:00
|
|
|
if wErr != nil {
|
|
|
|
errs[index] = traceError(wErr)
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
2016-10-30 12:27:29 -04:00
|
|
|
// Wait for all the writes to finish.
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Wait()
|
|
|
|
|
2016-10-30 12:27:29 -04:00
|
|
|
// Do we have write quorum?
|
2016-07-13 03:29:48 -04:00
|
|
|
if !isDiskQuorum(errs, xl.writeQuorum) {
|
2016-10-30 12:27:29 -04:00
|
|
|
// No quorum. Perform cleanup on the minority of disks
|
|
|
|
// on which the operation succeeded.
|
|
|
|
|
|
|
|
// There are two cases:
|
|
|
|
//
|
|
|
|
// 1. uploads.json file was updated -> we delete the
|
|
|
|
// file that we successfully overwrote on the
|
|
|
|
// minority of disks, so that the failed quorum
|
|
|
|
// operation is not partially visible.
|
|
|
|
//
|
|
|
|
// 2. uploads.json was deleted -> in this case since
|
|
|
|
// the delete failed, we restore from tmp.
|
2016-06-29 05:10:40 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-10-30 12:27:29 -04:00
|
|
|
if disk == nil || errs[index] != nil {
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-10-30 12:27:29 -04:00
|
|
|
if !isDelete[index] {
|
|
|
|
_ = disk.DeleteFile(
|
2016-11-22 16:15:06 -05:00
|
|
|
minioMetaMultipartBucket,
|
2016-10-30 12:27:29 -04:00
|
|
|
uploadsPath,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
_ = disk.RenameFile(
|
2016-11-20 17:25:43 -05:00
|
|
|
minioMetaTmpBucket, tmpUploadsPath,
|
2016-11-22 16:15:06 -05:00
|
|
|
minioMetaMultipartBucket, uploadsPath,
|
2016-10-30 12:27:29 -04:00
|
|
|
)
|
2016-06-02 19:34:15 -04:00
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2016-08-25 12:39:01 -04:00
|
|
|
return traceError(errXLWriteQuorum)
|
2016-06-02 19:34:15 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-10-30 12:27:29 -04:00
|
|
|
// we do have quorum, so in case of delete upload.json file
|
|
|
|
// operation, we purge from tmp.
|
2016-06-29 05:10:40 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-10-30 12:27:29 -04:00
|
|
|
if disk == nil || !isDelete[index] {
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-10-30 12:27:29 -04:00
|
|
|
// isDelete[index] = true at this point.
|
2016-11-20 17:25:43 -05:00
|
|
|
_ = disk.DeleteFile(minioMetaTmpBucket, tmpUploadsPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-11-21 04:47:26 -05:00
|
|
|
if reducedErr := reduceWriteQuorumErrs(errs, objectOpIgnoredErrs, xl.writeQuorum); reducedErr != nil {
|
|
|
|
return reducedErr
|
|
|
|
}
|
|
|
|
return nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:29:39 -05:00
|
|
|
// addUploadID - add upload ID and its initiated time to 'uploads.json'.
|
|
|
|
func (xl xlObjects) addUploadID(bucket, object string, uploadID string, initiated time.Time) error {
|
|
|
|
return xl.updateUploadJSON(bucket, object, uploadID, initiated, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeUploadID - remove upload ID in 'uploads.json'.
|
|
|
|
func (xl xlObjects) removeUploadID(bucket, object string, uploadID string) error {
|
|
|
|
return xl.updateUploadJSON(bucket, object, uploadID, time.Time{}, true)
|
|
|
|
}
|
|
|
|
|
2016-05-26 17:43:17 -04:00
|
|
|
// Returns if the prefix is a multipart upload.
|
|
|
|
func (xl xlObjects) isMultipartUpload(bucket, prefix string) bool {
|
2016-07-21 03:27:08 -04:00
|
|
|
for _, disk := range xl.getLoadBalancedDisks() {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
_, err := disk.StatFile(bucket, pathJoin(prefix, uploadsJSONFile))
|
2016-07-08 01:10:27 -04:00
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// For any reason disk was deleted or goes offline, continue
|
|
|
|
if isErrIgnored(err, objMetadataOpIgnoredErrs) {
|
|
|
|
continue
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
break
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-07-08 01:10:27 -04:00
|
|
|
return false
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
// isUploadIDExists - verify if a given uploadID exists and is valid.
|
|
|
|
func (xl xlObjects) isUploadIDExists(bucket, object, uploadID string) bool {
|
2016-11-22 16:15:06 -05:00
|
|
|
uploadIDPath := path.Join(bucket, object, uploadID)
|
|
|
|
return xl.isObject(minioMetaMultipartBucket, uploadIDPath)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-28 16:23:08 -04:00
|
|
|
|
|
|
|
// Removes part given by partName belonging to a mulitpart upload from minioMetaBucket
|
|
|
|
func (xl xlObjects) removeObjectPart(bucket, object, uploadID, partName string) {
|
2016-11-22 16:15:06 -05:00
|
|
|
curpartPath := path.Join(bucket, object, uploadID, partName)
|
2016-05-28 16:23:08 -04:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i, disk := range xl.storageDisks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-28 16:23:08 -04:00
|
|
|
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.
|
2016-11-22 16:15:06 -05:00
|
|
|
_ = disk.DeleteFile(minioMetaMultipartBucket, curpartPath)
|
2016-05-28 16:23:08 -04:00
|
|
|
}(i, disk)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2016-06-07 21:15:04 -04:00
|
|
|
|
|
|
|
// statPart - returns fileInfo structure for a successful stat on part file.
|
|
|
|
func (xl xlObjects) statPart(bucket, object, uploadID, partName string) (fileInfo FileInfo, err error) {
|
2016-11-22 16:15:06 -05:00
|
|
|
partNamePath := path.Join(bucket, object, uploadID, partName)
|
2016-07-21 03:27:08 -04:00
|
|
|
for _, disk := range xl.getLoadBalancedDisks() {
|
2016-06-07 21:15:04 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-22 16:15:06 -05:00
|
|
|
fileInfo, err = disk.StatFile(minioMetaMultipartBucket, partNamePath)
|
2016-07-08 01:10:27 -04:00
|
|
|
if err == nil {
|
|
|
|
return fileInfo, nil
|
|
|
|
}
|
2016-08-25 12:39:01 -04:00
|
|
|
err = traceError(err)
|
2016-07-24 21:08:15 -04:00
|
|
|
// For any reason disk was deleted or goes offline we continue to next disk.
|
2016-07-08 01:10:27 -04:00
|
|
|
if isErrIgnored(err, objMetadataOpIgnoredErrs) {
|
|
|
|
continue
|
2016-06-07 21:15:04 -04:00
|
|
|
}
|
2016-07-24 21:08:15 -04:00
|
|
|
|
|
|
|
// Catastrophic error, we return.
|
2016-06-07 21:15:04 -04:00
|
|
|
break
|
|
|
|
}
|
2016-07-08 01:10:27 -04:00
|
|
|
return FileInfo{}, err
|
2016-06-07 21:15:04 -04:00
|
|
|
}
|
2016-06-17 14:57:51 -04:00
|
|
|
|
2016-07-11 20:24:49 -04:00
|
|
|
// commitXLMetadata - commit `xl.json` from source prefix to destination prefix in the given slice of disks.
|
2016-11-21 15:45:02 -05:00
|
|
|
func commitXLMetadata(disks []StorageAPI, srcBucket, srcPrefix, dstBucket, dstPrefix string, quorum int) error {
|
2016-06-17 14:57:51 -04:00
|
|
|
var wg = &sync.WaitGroup{}
|
2016-07-11 20:24:49 -04:00
|
|
|
var mErrs = make([]error, len(disks))
|
2016-06-17 14:57:51 -04:00
|
|
|
|
|
|
|
srcJSONFile := path.Join(srcPrefix, xlMetaJSONFile)
|
|
|
|
dstJSONFile := path.Join(dstPrefix, xlMetaJSONFile)
|
|
|
|
|
|
|
|
// Rename `xl.json` to all disks in parallel.
|
2016-07-11 20:24:49 -04:00
|
|
|
for index, disk := range disks {
|
2016-06-17 14:57:51 -04:00
|
|
|
if disk == nil {
|
2016-08-25 12:39:01 -04:00
|
|
|
mErrs[index] = traceError(errDiskNotFound)
|
2016-06-17 14:57:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
// Rename `xl.json` in a routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-06-30 19:37:51 -04:00
|
|
|
// Delete any dangling directories.
|
2016-11-21 15:45:02 -05:00
|
|
|
defer disk.DeleteFile(srcBucket, srcPrefix)
|
2016-06-30 19:37:51 -04:00
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Renames `xl.json` from source prefix to destination prefix.
|
2016-11-21 15:45:02 -05:00
|
|
|
rErr := disk.RenameFile(srcBucket, srcJSONFile, dstBucket, dstJSONFile)
|
2016-06-17 14:57:51 -04:00
|
|
|
if rErr != nil {
|
2016-08-25 12:39:01 -04:00
|
|
|
mErrs[index] = traceError(rErr)
|
2016-06-17 14:57:51 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mErrs[index] = nil
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
// Wait for all the routines.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-07-13 16:44:33 -04:00
|
|
|
// Do we have write Quorum?.
|
2016-07-19 22:24:32 -04:00
|
|
|
if !isDiskQuorum(mErrs, quorum) {
|
2016-07-13 16:44:33 -04:00
|
|
|
// Delete all `xl.json` successfully renamed.
|
2016-11-21 15:45:02 -05:00
|
|
|
deleteAllXLMetadata(disks, dstBucket, dstPrefix, mErrs)
|
2016-08-25 12:39:01 -04:00
|
|
|
return traceError(errXLWriteQuorum)
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|
2016-07-13 03:29:48 -04:00
|
|
|
|
2016-11-21 04:47:26 -05:00
|
|
|
if reducedErr := reduceWriteQuorumErrs(mErrs, objectOpIgnoredErrs, quorum); reducedErr != nil {
|
|
|
|
return reducedErr
|
|
|
|
}
|
|
|
|
return nil
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|