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.
376 lines
11 KiB
376 lines
11 KiB
10 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
package donut
|
||
|
|
||
10 years ago
|
import (
|
||
10 years ago
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
10 years ago
|
"github.com/minio/minio/pkg/iodine"
|
||
|
)
|
||
10 years ago
|
|
||
10 years ago
|
// config files used inside Donut
|
||
10 years ago
|
const (
|
||
10 years ago
|
// donut system config
|
||
|
donutConfig = "donutConfig.json"
|
||
10 years ago
|
|
||
|
// bucket, object metadata
|
||
|
bucketMetadataConfig = "bucketMetadata.json"
|
||
|
objectMetadataConfig = "objectMetadata.json"
|
||
10 years ago
|
|
||
|
// versions
|
||
10 years ago
|
objectMetadataVersion = "1.0.0"
|
||
|
bucketMetadataVersion = "1.0.0"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// makeBucket - make a new bucket
|
||
|
func (donut API) makeBucket(bucket string, acl BucketACL) error {
|
||
|
donut.lock.Lock()
|
||
|
defer donut.lock.Unlock()
|
||
10 years ago
|
if bucket == "" || strings.TrimSpace(bucket) == "" {
|
||
|
return iodine.New(InvalidArgument{}, nil)
|
||
|
}
|
||
10 years ago
|
return donut.makeDonutBucket(bucket, acl.String())
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// getBucketMetadata - get bucket metadata
|
||
|
func (donut API) getBucketMetadata(bucketName string) (BucketMetadata, error) {
|
||
|
donut.lock.RLock()
|
||
|
defer donut.lock.RUnlock()
|
||
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return BucketMetadata{}, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
10 years ago
|
if _, ok := donut.buckets[bucketName]; !ok {
|
||
10 years ago
|
return BucketMetadata{}, iodine.New(BucketNotFound{Bucket: bucketName}, nil)
|
||
10 years ago
|
}
|
||
10 years ago
|
metadata, err := donut.getDonutBucketMetadata()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return BucketMetadata{}, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
10 years ago
|
return metadata.Buckets[bucketName], nil
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// setBucketMetadata - set bucket metadata
|
||
|
func (donut API) setBucketMetadata(bucketName string, bucketMetadata map[string]string) error {
|
||
|
donut.lock.Lock()
|
||
|
defer donut.lock.Unlock()
|
||
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
metadata, err := donut.getDonutBucketMetadata()
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
oldBucketMetadata := metadata.Buckets[bucketName]
|
||
|
acl, ok := bucketMetadata["acl"]
|
||
|
if !ok {
|
||
|
return iodine.New(InvalidArgument{}, nil)
|
||
|
}
|
||
10 years ago
|
oldBucketMetadata.ACL = BucketACL(acl)
|
||
10 years ago
|
metadata.Buckets[bucketName] = oldBucketMetadata
|
||
10 years ago
|
return donut.setDonutBucketMetadata(metadata)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// listBuckets - return list of buckets
|
||
|
func (donut API) listBuckets() (map[string]BucketMetadata, error) {
|
||
|
donut.lock.RLock()
|
||
|
defer donut.lock.RUnlock()
|
||
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
metadata, err := donut.getDonutBucketMetadata()
|
||
10 years ago
|
if err != nil {
|
||
|
// intentionally left out the error when Donut is empty
|
||
|
// but we need to revisit this area in future - since we need
|
||
|
// to figure out between acceptable and unacceptable errors
|
||
10 years ago
|
return make(map[string]BucketMetadata), nil
|
||
10 years ago
|
}
|
||
10 years ago
|
return metadata.Buckets, nil
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// listObjects - return list of objects
|
||
|
func (donut API) listObjects(bucket, prefix, marker, delimiter string, maxkeys int) (ListObjectsResults, error) {
|
||
|
donut.lock.RLock()
|
||
|
defer donut.lock.RUnlock()
|
||
10 years ago
|
errParams := map[string]string{
|
||
|
"bucket": bucket,
|
||
|
"prefix": prefix,
|
||
|
"marker": marker,
|
||
|
"delimiter": delimiter,
|
||
|
"maxkeys": strconv.Itoa(maxkeys),
|
||
|
}
|
||
10 years ago
|
if err := donut.listDonutBuckets(); err != nil {
|
||
|
return ListObjectsResults{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
if _, ok := donut.buckets[bucket]; !ok {
|
||
|
return ListObjectsResults{}, iodine.New(BucketNotFound{Bucket: bucket}, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
listObjects, err := donut.buckets[bucket].ListObjects(prefix, marker, delimiter, maxkeys)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return ListObjectsResults{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
return listObjects, nil
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// putObject - put object
|
||
|
func (donut API) putObject(bucket, object, expectedMD5Sum string, reader io.Reader, metadata map[string]string) (ObjectMetadata, error) {
|
||
|
donut.lock.Lock()
|
||
|
defer donut.lock.Unlock()
|
||
10 years ago
|
errParams := map[string]string{
|
||
|
"bucket": bucket,
|
||
|
"object": object,
|
||
|
}
|
||
|
if bucket == "" || strings.TrimSpace(bucket) == "" {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(InvalidArgument{}, errParams)
|
||
10 years ago
|
}
|
||
|
if object == "" || strings.TrimSpace(object) == "" {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(InvalidArgument{}, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
if _, ok := donut.buckets[bucket]; !ok {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
||
10 years ago
|
}
|
||
10 years ago
|
bucketMeta, err := donut.getDonutBucketMetadata()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
if _, ok := bucketMeta.Buckets[bucket].BucketObjects[object]; ok {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(ObjectExists{Object: object}, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
objMetadata, err := donut.buckets[bucket].WriteObject(object, reader, expectedMD5Sum, metadata)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
bucketMeta.Buckets[bucket].BucketObjects[object] = 1
|
||
10 years ago
|
if err := donut.setDonutBucketMetadata(bucketMeta); err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
return objMetadata, nil
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// getObject - get object
|
||
|
func (donut API) getObject(bucket, object string) (reader io.ReadCloser, size int64, err error) {
|
||
|
donut.lock.RLock()
|
||
|
defer donut.lock.RUnlock()
|
||
10 years ago
|
errParams := map[string]string{
|
||
|
"bucket": bucket,
|
||
|
"object": object,
|
||
|
}
|
||
|
if bucket == "" || strings.TrimSpace(bucket) == "" {
|
||
|
return nil, 0, iodine.New(InvalidArgument{}, errParams)
|
||
|
}
|
||
|
if object == "" || strings.TrimSpace(object) == "" {
|
||
|
return nil, 0, iodine.New(InvalidArgument{}, errParams)
|
||
|
}
|
||
10 years ago
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return nil, 0, iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
if _, ok := donut.buckets[bucket]; !ok {
|
||
10 years ago
|
return nil, 0, iodine.New(BucketNotFound{Bucket: bucket}, errParams)
|
||
|
}
|
||
10 years ago
|
return donut.buckets[bucket].ReadObject(object)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
// getObjectMetadata - get object metadata
|
||
|
func (donut API) getObjectMetadata(bucket, object string) (ObjectMetadata, error) {
|
||
|
donut.lock.RLock()
|
||
|
defer donut.lock.RUnlock()
|
||
10 years ago
|
errParams := map[string]string{
|
||
|
"bucket": bucket,
|
||
|
"object": object,
|
||
|
}
|
||
10 years ago
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
if _, ok := donut.buckets[bucket]; !ok {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
bucketMeta, err := donut.getDonutBucketMetadata()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
if _, ok := bucketMeta.Buckets[bucket].BucketObjects[object]; !ok {
|
||
|
return ObjectMetadata{}, iodine.New(ObjectNotFound{Object: object}, errParams)
|
||
10 years ago
|
}
|
||
10 years ago
|
objectMetadata, err := donut.buckets[bucket].GetObjectMetadata(object)
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
return ObjectMetadata{}, iodine.New(err, nil)
|
||
10 years ago
|
}
|
||
10 years ago
|
return objectMetadata, nil
|
||
10 years ago
|
}
|
||
|
|
||
|
// getDiskWriters -
|
||
10 years ago
|
func (donut API) getBucketMetadataWriters() ([]io.WriteCloser, error) {
|
||
10 years ago
|
var writers []io.WriteCloser
|
||
10 years ago
|
for _, node := range donut.nodes {
|
||
10 years ago
|
disks, err := node.ListDisks()
|
||
|
if err != nil {
|
||
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
|
writers = make([]io.WriteCloser, len(disks))
|
||
10 years ago
|
for order, d := range disks {
|
||
10 years ago
|
bucketMetaDataWriter, err := d.CreateFile(filepath.Join(donut.config.DonutName, bucketMetadataConfig))
|
||
10 years ago
|
if err != nil {
|
||
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
|
writers[order] = bucketMetaDataWriter
|
||
|
}
|
||
|
}
|
||
|
return writers, nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func (donut API) getBucketMetadataReaders() ([]io.ReadCloser, error) {
|
||
10 years ago
|
var readers []io.ReadCloser
|
||
10 years ago
|
for _, node := range donut.nodes {
|
||
10 years ago
|
disks, err := node.ListDisks()
|
||
|
if err != nil {
|
||
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
|
readers = make([]io.ReadCloser, len(disks))
|
||
10 years ago
|
for order, d := range disks {
|
||
10 years ago
|
bucketMetaDataReader, err := d.OpenFile(filepath.Join(donut.config.DonutName, bucketMetadataConfig))
|
||
10 years ago
|
if err != nil {
|
||
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
|
readers[order] = bucketMetaDataReader
|
||
|
}
|
||
|
}
|
||
|
return readers, nil
|
||
|
}
|
||
|
|
||
|
//
|
||
10 years ago
|
func (donut API) setDonutBucketMetadata(metadata *AllBuckets) error {
|
||
|
writers, err := donut.getBucketMetadataWriters()
|
||
10 years ago
|
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
|
||
|
}
|
||
|
|
||
10 years ago
|
func (donut API) getDonutBucketMetadata() (*AllBuckets, error) {
|
||
10 years ago
|
metadata := new(AllBuckets)
|
||
10 years ago
|
readers, err := donut.getBucketMetadataReaders()
|
||
10 years ago
|
if err != nil {
|
||
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
|
for _, reader := range readers {
|
||
|
defer reader.Close()
|
||
|
}
|
||
|
for _, reader := range readers {
|
||
|
jenc := json.NewDecoder(reader)
|
||
10 years ago
|
if err := jenc.Decode(metadata); err != nil {
|
||
10 years ago
|
return nil, iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
return metadata, nil
|
||
10 years ago
|
}
|
||
10 years ago
|
return nil, iodine.New(InvalidArgument{}, nil)
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func (donut API) makeDonutBucket(bucketName, acl string) error {
|
||
|
if err := donut.listDonutBuckets(); err != nil {
|
||
10 years ago
|
return iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
if _, ok := donut.buckets[bucketName]; ok {
|
||
10 years ago
|
return iodine.New(BucketExists{Bucket: bucketName}, nil)
|
||
|
}
|
||
10 years ago
|
bucket, bucketMetadata, err := newBucket(bucketName, acl, donut.config.DonutName, donut.nodes)
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
nodeNumber := 0
|
||
10 years ago
|
donut.buckets[bucketName] = bucket
|
||
|
for _, node := range donut.nodes {
|
||
10 years ago
|
disks, err := node.ListDisks()
|
||
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
for order, disk := range disks {
|
||
|
bucketSlice := fmt.Sprintf("%s$%d$%d", bucketName, nodeNumber, order)
|
||
10 years ago
|
err := disk.MakeDir(filepath.Join(donut.config.DonutName, bucketSlice))
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
}
|
||
|
nodeNumber = nodeNumber + 1
|
||
|
}
|
||
10 years ago
|
metadata, err := donut.getDonutBucketMetadata()
|
||
10 years ago
|
if err != nil {
|
||
10 years ago
|
if os.IsNotExist(iodine.ToError(err)) {
|
||
10 years ago
|
metadata := new(AllBuckets)
|
||
|
metadata.Buckets = make(map[string]BucketMetadata)
|
||
|
metadata.Buckets[bucketName] = bucketMetadata
|
||
10 years ago
|
err = donut.setDonutBucketMetadata(metadata)
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
metadata.Buckets[bucketName] = bucketMetadata
|
||
10 years ago
|
err = donut.setDonutBucketMetadata(metadata)
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
10 years ago
|
func (donut API) listDonutBuckets() error {
|
||
|
for _, node := range donut.nodes {
|
||
10 years ago
|
disks, err := node.ListDisks()
|
||
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
for _, disk := range disks {
|
||
10 years ago
|
dirs, err := disk.ListDir(donut.config.DonutName)
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
|
for _, dir := range dirs {
|
||
|
splitDir := strings.Split(dir.Name(), "$")
|
||
|
if len(splitDir) < 3 {
|
||
|
return iodine.New(CorruptedBackend{Backend: dir.Name()}, nil)
|
||
|
}
|
||
|
bucketName := splitDir[0]
|
||
10 years ago
|
// we dont need this once we cache from makeDonutBucket()
|
||
10 years ago
|
bucket, _, err := newBucket(bucketName, "private", donut.config.DonutName, donut.nodes)
|
||
10 years ago
|
if err != nil {
|
||
|
return iodine.New(err, nil)
|
||
|
}
|
||
10 years ago
|
donut.buckets[bucketName] = bucket
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|