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.
305 lines
7.7 KiB
305 lines
7.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 (
|
||
9 years ago
|
"path"
|
||
9 years ago
|
"sort"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
/// Bucket operations
|
||
|
|
||
|
// MakeBucket - make a bucket.
|
||
|
func (xl xlObjects) MakeBucket(bucket string) error {
|
||
|
// Verify if bucket is valid.
|
||
|
if !IsValidBucketName(bucket) {
|
||
|
return BucketNameInvalid{Bucket: bucket}
|
||
|
}
|
||
8 years ago
|
// Verify if bucket is found.
|
||
|
if xl.isBucketExist(bucket) {
|
||
|
return toObjectErr(errVolumeExists, bucket)
|
||
|
}
|
||
9 years ago
|
|
||
|
nsMutex.Lock(bucket, "")
|
||
|
defer nsMutex.Unlock(bucket, "")
|
||
|
|
||
|
// Initialize sync waitgroup.
|
||
|
var wg = &sync.WaitGroup{}
|
||
|
|
||
|
// Initialize list of errors.
|
||
|
var dErrs = make([]error, len(xl.storageDisks))
|
||
|
|
||
|
// Make a volume entry on all underlying storage disks.
|
||
|
for index, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
dErrs[index] = errDiskNotFound
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
wg.Add(1)
|
||
|
// Make a volume inside a go-routine.
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
err := disk.MakeVol(bucket)
|
||
|
if err != nil {
|
||
|
dErrs[index] = err
|
||
|
}
|
||
|
}(index, disk)
|
||
|
}
|
||
|
|
||
|
// Wait for all make vol to finish.
|
||
|
wg.Wait()
|
||
|
|
||
9 years ago
|
// Do we have write quorum?.
|
||
9 years ago
|
if !isDiskQuorum(dErrs, xl.writeQuorum) {
|
||
9 years ago
|
// Purge successfully created buckets if we don't have writeQuorum.
|
||
|
xl.undoMakeBucket(bucket)
|
||
|
return toObjectErr(errXLWriteQuorum, bucket)
|
||
|
}
|
||
|
|
||
|
// Verify we have any other errors which should undo make bucket.
|
||
8 years ago
|
if reducedErr := reduceErrs(dErrs, []error{
|
||
|
errDiskNotFound,
|
||
|
errFaultyDisk,
|
||
|
errDiskAccessDenied,
|
||
|
}); reducedErr != nil {
|
||
|
return toObjectErr(reducedErr, bucket)
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func (xl xlObjects) undoDeleteBucket(bucket string) {
|
||
|
// Initialize sync waitgroup.
|
||
|
var wg = &sync.WaitGroup{}
|
||
|
// Undo previous make bucket entry on all underlying storage disks.
|
||
|
for index, disk := range xl.storageDisks {
|
||
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
wg.Add(1)
|
||
|
// Delete a bucket inside a go-routine.
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
_ = disk.MakeVol(bucket)
|
||
|
}(index, disk)
|
||
|
}
|
||
|
|
||
|
// Wait for all make vol to finish.
|
||
|
wg.Wait()
|
||
|
}
|
||
|
|
||
9 years ago
|
// undo make bucket operation upon quorum failure.
|
||
|
func (xl xlObjects) undoMakeBucket(bucket string) {
|
||
|
// Initialize sync waitgroup.
|
||
|
var wg = &sync.WaitGroup{}
|
||
|
// Undo previous make bucket entry on all underlying storage disks.
|
||
|
for index, disk := range xl.storageDisks {
|
||
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
wg.Add(1)
|
||
|
// Delete a bucket inside a go-routine.
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
|
_ = disk.DeleteVol(bucket)
|
||
|
}(index, disk)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// Wait for all make vol to finish.
|
||
|
wg.Wait()
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// list all errors that can be ignored in a bucket metadata operation.
|
||
|
var bucketMetadataOpIgnoredErrs = []error{
|
||
|
errDiskNotFound,
|
||
|
errDiskAccessDenied,
|
||
|
errFaultyDisk,
|
||
9 years ago
|
errVolumeNotFound,
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// getBucketInfo - returns the BucketInfo from one of the load balanced disks.
|
||
9 years ago
|
func (xl xlObjects) getBucketInfo(bucketName string) (bucketInfo BucketInfo, err error) {
|
||
8 years ago
|
for _, disk := range xl.getLoadBalancedDisks() {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
var volInfo VolInfo
|
||
|
volInfo, err = disk.StatVol(bucketName)
|
||
9 years ago
|
if err == nil {
|
||
|
bucketInfo = BucketInfo{
|
||
|
Name: volInfo.Name,
|
||
|
Created: volInfo.Created,
|
||
9 years ago
|
}
|
||
9 years ago
|
return bucketInfo, nil
|
||
9 years ago
|
}
|
||
9 years ago
|
// For any reason disk went offline continue and pick the next one.
|
||
|
if isErrIgnored(err, bucketMetadataOpIgnoredErrs) {
|
||
|
continue
|
||
9 years ago
|
}
|
||
|
break
|
||
9 years ago
|
}
|
||
9 years ago
|
return BucketInfo{}, err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Checks whether bucket exists.
|
||
9 years ago
|
func (xl xlObjects) isBucketExist(bucket string) bool {
|
||
|
nsMutex.RLock(bucket, "")
|
||
|
defer nsMutex.RUnlock(bucket, "")
|
||
|
|
||
9 years ago
|
// Check whether bucket exists.
|
||
9 years ago
|
_, err := xl.getBucketInfo(bucket)
|
||
9 years ago
|
if err != nil {
|
||
|
if err == errVolumeNotFound {
|
||
|
return false
|
||
|
}
|
||
9 years ago
|
errorIf(err, "Stat failed on bucket "+bucket+".")
|
||
9 years ago
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
9 years ago
|
// GetBucketInfo - returns BucketInfo for a bucket.
|
||
9 years ago
|
func (xl xlObjects) GetBucketInfo(bucket string) (BucketInfo, error) {
|
||
|
// Verify if bucket is valid.
|
||
|
if !IsValidBucketName(bucket) {
|
||
|
return BucketInfo{}, BucketNameInvalid{Bucket: bucket}
|
||
|
}
|
||
|
nsMutex.RLock(bucket, "")
|
||
|
defer nsMutex.RUnlock(bucket, "")
|
||
9 years ago
|
bucketInfo, err := xl.getBucketInfo(bucket)
|
||
9 years ago
|
if err != nil {
|
||
|
return BucketInfo{}, toObjectErr(err, bucket)
|
||
|
}
|
||
9 years ago
|
return bucketInfo, nil
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// listBuckets - returns list of all buckets from a disk picked at random.
|
||
|
func (xl xlObjects) listBuckets() (bucketsInfo []BucketInfo, err error) {
|
||
8 years ago
|
for _, disk := range xl.getLoadBalancedDisks() {
|
||
9 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
var volsInfo []VolInfo
|
||
|
volsInfo, err = disk.ListVols()
|
||
|
if err == nil {
|
||
|
// NOTE: The assumption here is that volumes across all disks in
|
||
|
// readQuorum have consistent view i.e they all have same number
|
||
|
// of buckets. This is essentially not verified since healing
|
||
|
// should take care of this.
|
||
|
var bucketsInfo []BucketInfo
|
||
|
for _, volInfo := range volsInfo {
|
||
|
// StorageAPI can send volume names which are incompatible
|
||
|
// with buckets, handle it and skip them.
|
||
|
if !IsValidBucketName(volInfo.Name) {
|
||
|
continue
|
||
|
}
|
||
8 years ago
|
// Ignore the volume special bucket.
|
||
|
if volInfo.Name == minioMetaBucket {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
bucketsInfo = append(bucketsInfo, BucketInfo{
|
||
|
Name: volInfo.Name,
|
||
|
Created: volInfo.Created,
|
||
|
})
|
||
9 years ago
|
}
|
||
8 years ago
|
// For buckets info empty, loop once again to check
|
||
|
// if we have, can happen if disks are down.
|
||
|
if len(bucketsInfo) == 0 {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
return bucketsInfo, nil
|
||
9 years ago
|
}
|
||
9 years ago
|
// Ignore any disks not found.
|
||
|
if isErrIgnored(err, bucketMetadataOpIgnoredErrs) {
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
break
|
||
9 years ago
|
}
|
||
9 years ago
|
return nil, err
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// ListBuckets - lists all the buckets, sorted by its name.
|
||
9 years ago
|
func (xl xlObjects) ListBuckets() ([]BucketInfo, error) {
|
||
|
bucketInfos, err := xl.listBuckets()
|
||
|
if err != nil {
|
||
|
return nil, toObjectErr(err)
|
||
|
}
|
||
9 years ago
|
// Sort by bucket name before returning.
|
||
9 years ago
|
sort.Sort(byBucketName(bucketInfos))
|
||
|
return bucketInfos, nil
|
||
|
}
|
||
|
|
||
9 years ago
|
// DeleteBucket - deletes a bucket.
|
||
9 years ago
|
func (xl xlObjects) DeleteBucket(bucket string) error {
|
||
|
// Verify if bucket is valid.
|
||
|
if !IsValidBucketName(bucket) {
|
||
|
return BucketNameInvalid{Bucket: bucket}
|
||
|
}
|
||
8 years ago
|
// Verify if bucket is found.
|
||
|
if !xl.isBucketExist(bucket) {
|
||
|
return BucketNotFound{Bucket: bucket}
|
||
|
}
|
||
9 years ago
|
|
||
|
nsMutex.Lock(bucket, "")
|
||
9 years ago
|
defer nsMutex.Unlock(bucket, "")
|
||
9 years ago
|
|
||
|
// Collect if all disks report volume not found.
|
||
|
var wg = &sync.WaitGroup{}
|
||
|
var dErrs = make([]error, len(xl.storageDisks))
|
||
|
|
||
|
// Remove a volume entry on all underlying storage disks.
|
||
|
for index, disk := range xl.storageDisks {
|
||
9 years ago
|
if disk == nil {
|
||
|
dErrs[index] = errDiskNotFound
|
||
|
continue
|
||
|
}
|
||
9 years ago
|
wg.Add(1)
|
||
|
// Delete volume inside a go-routine.
|
||
|
go func(index int, disk StorageAPI) {
|
||
|
defer wg.Done()
|
||
9 years ago
|
// Attempt to delete bucket.
|
||
9 years ago
|
err := disk.DeleteVol(bucket)
|
||
9 years ago
|
if err != nil {
|
||
|
dErrs[index] = err
|
||
|
return
|
||
|
}
|
||
|
// Cleanup all the previously incomplete multiparts.
|
||
|
err = cleanupDir(disk, path.Join(minioMetaBucket, mpartMetaPrefix), bucket)
|
||
9 years ago
|
if err != nil && err != errVolumeNotFound {
|
||
9 years ago
|
dErrs[index] = err
|
||
|
}
|
||
|
}(index, disk)
|
||
|
}
|
||
|
|
||
|
// Wait for all the delete vols to finish.
|
||
|
wg.Wait()
|
||
|
|
||
8 years ago
|
if !isDiskQuorum(dErrs, xl.writeQuorum) {
|
||
|
xl.undoDeleteBucket(bucket)
|
||
|
return toObjectErr(errXLWriteQuorum, bucket)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if reducedErr := reduceErrs(dErrs, []error{
|
||
|
errFaultyDisk,
|
||
|
errDiskNotFound,
|
||
|
errDiskAccessDenied,
|
||
|
}); reducedErr != nil {
|
||
|
return toObjectErr(reducedErr, bucket)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
|
}
|