minio/pkg/storage/donut/donut_bucket.go

225 lines
7.2 KiB
Go
Raw Normal View History

2015-04-05 04:53:41 -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.
*/
package donut
import (
"errors"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"time"
"crypto/md5"
"encoding/hex"
2015-05-11 19:23:10 -04:00
"github.com/minio/minio/pkg/iodine"
2015-04-05 04:53:41 -04:00
)
// internal struct carrying bucket specific information
2015-04-05 04:53:41 -04:00
type bucket struct {
name string
2015-04-22 21:19:53 -04:00
acl string
time time.Time
2015-04-05 04:53:41 -04:00
donutName string
nodes map[string]Node
objects map[string]Object
}
// NewBucket - instantiate a new bucket
func NewBucket(bucketName, aclType, donutName string, nodes map[string]Node) (Bucket, map[string]string, error) {
errParams := map[string]string{
"bucketName": bucketName,
"donutName": donutName,
2015-04-22 21:19:53 -04:00
"aclType": aclType,
}
if strings.TrimSpace(bucketName) == "" || strings.TrimSpace(donutName) == "" {
return nil, nil, iodine.New(errors.New("invalid argument"), errParams)
2015-04-05 04:53:41 -04:00
}
bucketMetadata := make(map[string]string)
bucketMetadata["acl"] = aclType
t := time.Now().UTC()
bucketMetadata["created"] = t.Format(time.RFC3339Nano)
2015-04-05 04:53:41 -04:00
b := bucket{}
b.name = bucketName
2015-04-22 21:19:53 -04:00
b.acl = aclType
b.time = t
2015-04-05 04:53:41 -04:00
b.donutName = donutName
b.objects = make(map[string]Object)
b.nodes = nodes
return b, bucketMetadata, nil
2015-04-05 04:53:41 -04:00
}
// ListObjects - list all objects
2015-04-05 04:53:41 -04:00
func (b bucket) ListObjects() (map[string]Object, error) {
nodeSlice := 0
for _, node := range b.nodes {
disks, err := node.ListDisks()
if err != nil {
return nil, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
for _, disk := range disks {
bucketSlice := fmt.Sprintf("%s$%d$%d", b.name, nodeSlice, disk.GetOrder())
bucketPath := path.Join(b.donutName, bucketSlice)
objects, err := disk.ListDir(bucketPath)
if err != nil {
return nil, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
for _, object := range objects {
newObject, err := NewObject(object.Name(), path.Join(disk.GetPath(), bucketPath))
if err != nil {
return nil, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
newObjectMetadata, err := newObject.GetObjectMetadata()
if err != nil {
return nil, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
objectName, ok := newObjectMetadata["object"]
if !ok {
return nil, iodine.New(errors.New("object corrupted"), nil)
2015-04-05 04:53:41 -04:00
}
b.objects[objectName] = newObject
}
}
nodeSlice = nodeSlice + 1
}
return b.objects, nil
}
// GetObject - get object
2015-04-05 04:53:41 -04:00
func (b bucket) GetObject(objectName string) (reader io.ReadCloser, size int64, err error) {
reader, writer := io.Pipe()
// get list of objects
objects, err := b.ListObjects()
if err != nil {
return nil, 0, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
// check if object exists
object, ok := objects[objectName]
if !ok {
return nil, 0, iodine.New(os.ErrNotExist, nil)
2015-04-05 04:53:41 -04:00
}
// verify if objectMetadata is readable, before we serve the request
objectMetadata, err := object.GetObjectMetadata()
2015-04-05 04:53:41 -04:00
if err != nil {
return nil, 0, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
if objectName == "" || writer == nil || len(objectMetadata) == 0 {
return nil, 0, iodine.New(errors.New("invalid argument"), nil)
2015-04-05 04:53:41 -04:00
}
size, err = strconv.ParseInt(objectMetadata["size"], 10, 64)
2015-04-05 04:53:41 -04:00
if err != nil {
return nil, 0, iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
// verify if donutObjectMetadata is readable, before we server the request
donutObjectMetadata, err := object.GetDonutObjectMetadata()
if err != nil {
return nil, 0, iodine.New(err, nil)
}
// read and reply back to GetObject() request in a go-routine
go b.readEncodedData(b.normalizeObjectName(objectName), writer, donutObjectMetadata)
2015-04-05 04:53:41 -04:00
return reader, size, nil
}
// PutObject - put a new object
func (b bucket) PutObject(objectName string, objectData io.Reader, expectedMD5Sum string, metadata map[string]string) (string, error) {
if objectName == "" || objectData == nil {
return "", iodine.New(errors.New("invalid argument"), nil)
2015-04-05 04:53:41 -04:00
}
writers, err := b.getDiskWriters(b.normalizeObjectName(objectName), "data")
if err != nil {
return "", iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
summer := md5.New()
objectMetadata := make(map[string]string)
donutObjectMetadata := make(map[string]string)
2015-05-19 21:32:50 -04:00
objectMetadata["version"] = objectMetadataVersion
donutObjectMetadata["version"] = donutObjectMetadataVersion
size := metadata["contentLength"]
sizeInt, err := strconv.ParseInt(size, 10, 64)
if err != nil {
return "", iodine.New(err, nil)
}
// if total writers are only '1' do not compute erasure
2015-04-05 04:53:41 -04:00
switch len(writers) == 1 {
case true:
mw := io.MultiWriter(writers[0], summer)
totalLength, err := io.CopyN(mw, objectData, sizeInt)
2015-04-05 04:53:41 -04:00
if err != nil {
return "", iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
donutObjectMetadata["sys.size"] = strconv.FormatInt(totalLength, 10)
objectMetadata["size"] = strconv.FormatInt(totalLength, 10)
2015-04-05 04:53:41 -04:00
case false:
// calculate data and parity dictated by total number of writers
2015-04-05 04:53:41 -04:00
k, m, err := b.getDataAndParity(len(writers))
if err != nil {
return "", iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
// encoded data with k, m and write
chunkCount, totalLength, err := b.writeEncodedData(k, m, writers, objectData, summer)
2015-04-05 04:53:41 -04:00
if err != nil {
return "", iodine.New(err, nil)
2015-04-05 04:53:41 -04:00
}
/// donutMetadata section
donutObjectMetadata["sys.blockSize"] = strconv.Itoa(10 * 1024 * 1024)
donutObjectMetadata["sys.chunkCount"] = strconv.Itoa(chunkCount)
donutObjectMetadata["sys.erasureK"] = strconv.FormatUint(uint64(k), 10)
donutObjectMetadata["sys.erasureM"] = strconv.FormatUint(uint64(m), 10)
donutObjectMetadata["sys.erasureTechnique"] = "Cauchy"
donutObjectMetadata["sys.size"] = strconv.Itoa(totalLength)
// keep size inside objectMetadata as well for Object API requests
objectMetadata["size"] = strconv.Itoa(totalLength)
2015-04-05 04:53:41 -04:00
}
objectMetadata["bucket"] = b.name
objectMetadata["object"] = objectName
// store all user provided metadata
for k, v := range metadata {
objectMetadata[k] = v
}
2015-04-05 04:53:41 -04:00
dataMd5sum := summer.Sum(nil)
objectMetadata["created"] = time.Now().UTC().Format(time.RFC3339Nano)
// keeping md5sum for the object in two different places
// one for object storage and another is for internal use
objectMetadata["md5"] = hex.EncodeToString(dataMd5sum)
donutObjectMetadata["sys.md5"] = hex.EncodeToString(dataMd5sum)
// Verify if the written object is equal to what is expected, only if it is requested as such
if strings.TrimSpace(expectedMD5Sum) != "" {
if err := b.isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), objectMetadata["md5"]); err != nil {
return "", iodine.New(err, nil)
}
}
// write donut specific metadata
if err := b.writeDonutObjectMetadata(b.normalizeObjectName(objectName), donutObjectMetadata); err != nil {
return "", iodine.New(err, nil)
}
// write object specific metadata
if err := b.writeObjectMetadata(b.normalizeObjectName(objectName), objectMetadata); err != nil {
return "", iodine.New(err, nil)
}
// close all writers, when control flow reaches here
for _, writer := range writers {
writer.Close()
2015-04-05 04:53:41 -04:00
}
return objectMetadata["md5"], nil
2015-04-05 04:53:41 -04:00
}