2015-04-08 19:28:14 -04:00
|
|
|
/*
|
|
|
|
* Minimalist Object Storage, (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
2015-04-07 00:42:54 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
2015-04-27 05:04:05 -04:00
|
|
|
"encoding/json"
|
2015-04-07 00:42:54 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-04-27 05:04:05 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
2015-04-07 00:42:54 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
2015-04-07 18:55:44 -04:00
|
|
|
|
2015-04-08 21:02:03 -04:00
|
|
|
"github.com/minio-io/minio/pkg/iodine"
|
2015-04-07 00:42:54 -04:00
|
|
|
)
|
|
|
|
|
2015-04-27 05:04:05 -04:00
|
|
|
/// This file contains all the internal functions used by Object interface
|
|
|
|
|
|
|
|
// getDiskWriters -
|
|
|
|
func (d donut) getBucketMetadataWriters() ([]io.WriteCloser, error) {
|
|
|
|
var writers []io.WriteCloser
|
|
|
|
for _, node := range d.nodes {
|
|
|
|
disks, err := node.ListDisks()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
writers = make([]io.WriteCloser, len(disks))
|
|
|
|
for _, disk := range disks {
|
|
|
|
bucketMetaDataWriter, err := disk.MakeFile(path.Join(d.name, bucketMetadataConfig))
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
writers[disk.GetOrder()] = bucketMetaDataWriter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return writers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d donut) getBucketMetadataReaders() ([]io.ReadCloser, error) {
|
|
|
|
var readers []io.ReadCloser
|
|
|
|
for _, node := range d.nodes {
|
|
|
|
disks, err := node.ListDisks()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
readers = make([]io.ReadCloser, len(disks))
|
|
|
|
for _, disk := range disks {
|
|
|
|
bucketMetaDataReader, err := disk.OpenFile(path.Join(d.name, bucketMetadataConfig))
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
readers[disk.GetOrder()] = bucketMetaDataReader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
func (d donut) setDonutBucketMetadata(metadata map[string]map[string]string) error {
|
|
|
|
writers, err := d.getBucketMetadataWriters()
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
for _, writer := range writers {
|
|
|
|
defer writer.Close()
|
|
|
|
}
|
|
|
|
for _, writer := range writers {
|
|
|
|
jenc := json.NewEncoder(writer)
|
|
|
|
if err := jenc.Encode(metadata); err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d donut) getDonutBucketMetadata() (map[string]map[string]string, error) {
|
|
|
|
metadata := make(map[string]map[string]string)
|
|
|
|
readers, err := d.getBucketMetadataReaders()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
for _, reader := range readers {
|
|
|
|
defer reader.Close()
|
|
|
|
}
|
|
|
|
for _, reader := range readers {
|
|
|
|
jenc := json.NewDecoder(reader)
|
|
|
|
if err := jenc.Decode(&metadata); err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
2015-04-22 21:19:53 -04:00
|
|
|
func (d donut) makeDonutBucket(bucketName, acl string) error {
|
2015-04-08 19:28:14 -04:00
|
|
|
err := d.getDonutBuckets()
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
if _, ok := d.buckets[bucketName]; ok {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(errors.New("bucket exists"), nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
bucket, bucketMetadata, err := NewBucket(bucketName, acl, d.name, d.nodes)
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
nodeNumber := 0
|
|
|
|
d.buckets[bucketName] = bucket
|
|
|
|
for _, node := range d.nodes {
|
|
|
|
disks, err := node.ListDisks()
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
for _, disk := range disks {
|
|
|
|
bucketSlice := fmt.Sprintf("%s$%d$%d", bucketName, nodeNumber, disk.GetOrder())
|
|
|
|
err := disk.MakeDir(path.Join(d.name, bucketSlice))
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nodeNumber = nodeNumber + 1
|
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
metadata, err := d.getDonutBucketMetadata()
|
|
|
|
if err != nil {
|
|
|
|
err = iodine.ToError(err)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
metadata := make(map[string]map[string]string)
|
|
|
|
metadata[bucketName] = bucketMetadata
|
|
|
|
err = d.setDonutBucketMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
metadata[bucketName] = bucketMetadata
|
|
|
|
err = d.setDonutBucketMetadata(metadata)
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
2015-04-07 00:42:54 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
func (d donut) getDonutBuckets() error {
|
2015-04-07 00:42:54 -04:00
|
|
|
for _, node := range d.nodes {
|
|
|
|
disks, err := node.ListDisks()
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
for _, disk := range disks {
|
|
|
|
dirs, err := disk.ListDir(d.name)
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
splitDir := strings.Split(dir.Name(), "$")
|
|
|
|
if len(splitDir) < 3 {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(errors.New("corrupted backend"), nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
bucketName := splitDir[0]
|
2015-04-08 19:28:14 -04:00
|
|
|
// we dont need this NewBucket once we cache from makeDonutBucket()
|
2015-04-27 05:04:05 -04:00
|
|
|
bucket, _, err := NewBucket(bucketName, "private", d.name, d.nodes)
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
d.buckets[bucketName] = bucket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|