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-04-08 19:28:14 -04:00
|
|
|
|
2015-04-08 21:02:03 -04:00
|
|
|
"github.com/minio-io/minio/pkg/iodine"
|
2015-04-05 04:53:41 -04:00
|
|
|
)
|
|
|
|
|
2015-04-08 19:28:14 -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
|
2015-04-27 05:04:05 -04:00
|
|
|
func NewBucket(bucketName, aclType, donutName string, nodes map[string]Node) (Bucket, map[string]string, error) {
|
2015-04-07 18:55:44 -04:00
|
|
|
errParams := map[string]string{
|
|
|
|
"bucketName": bucketName,
|
|
|
|
"donutName": donutName,
|
2015-04-22 21:19:53 -04:00
|
|
|
"aclType": aclType,
|
2015-04-07 18:55:44 -04:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(bucketName) == "" || strings.TrimSpace(donutName) == "" {
|
2015-04-27 05:04:05 -04:00
|
|
|
return nil, nil, iodine.New(errors.New("invalid argument"), errParams)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-27 05:04:05 -04:00
|
|
|
bucketMetadata := make(map[string]string)
|
|
|
|
bucketMetadata["acl"] = aclType
|
2015-04-30 22:26:23 -04:00
|
|
|
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
|
2015-04-30 22:26:23 -04:00
|
|
|
b.time = t
|
2015-04-05 04:53:41 -04:00
|
|
|
b.donutName = donutName
|
|
|
|
b.objects = make(map[string]Object)
|
|
|
|
b.nodes = nodes
|
2015-04-27 05:04:05 -04:00
|
|
|
return b, bucketMetadata, nil
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -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 {
|
2015-04-07 18:55:44 -04:00
|
|
|
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 {
|
2015-04-07 18:55:44 -04:00
|
|
|
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 {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
newObjectMetadata, err := newObject.GetObjectMetadata()
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
objectName, ok := newObjectMetadata["object"]
|
|
|
|
if !ok {
|
2015-04-07 18:55:44 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// 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 {
|
2015-04-07 18:55:44 -04:00
|
|
|
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 {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, 0, iodine.New(os.ErrNotExist, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
// verify if objectMetadata is readable, before we serve the request
|
2015-04-07 01:29:10 -04:00
|
|
|
objectMetadata, err := object.GetObjectMetadata()
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, 0, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-07 01:29:10 -04:00
|
|
|
if objectName == "" || writer == nil || len(objectMetadata) == 0 {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, 0, iodine.New(errors.New("invalid argument"), nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-07 01:29:10 -04:00
|
|
|
size, err = strconv.ParseInt(objectMetadata["size"], 10, 64)
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil, 0, iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
// verify if donutObjectMetadata is readable, before we server the request
|
2015-04-07 18:55:44 -04:00
|
|
|
donutObjectMetadata, err := object.GetDonutObjectMetadata()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, iodine.New(err, nil)
|
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
// read and reply back to GetObject() request in a go-routine
|
2015-04-07 18:55:44 -04:00
|
|
|
go b.readEncodedData(b.normalizeObjectName(objectName), writer, donutObjectMetadata)
|
2015-04-05 04:53:41 -04:00
|
|
|
return reader, size, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// PutObject - put a new object
|
2015-04-30 06:38:11 -04:00
|
|
|
func (b bucket) PutObject(objectName string, objectData io.Reader, expectedMD5Sum string, metadata map[string]string) (string, error) {
|
2015-04-07 00:42:54 -04:00
|
|
|
if objectName == "" || objectData == nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
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 {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
summer := md5.New()
|
2015-04-07 01:29:10 -04:00
|
|
|
objectMetadata := make(map[string]string)
|
2015-04-07 18:55:44 -04:00
|
|
|
donutObjectMetadata := make(map[string]string)
|
|
|
|
objectMetadata["version"] = "1.0"
|
|
|
|
donutObjectMetadata["version"] = "1.0"
|
2015-05-04 02:16:10 -04:00
|
|
|
size := metadata["contentLength"]
|
|
|
|
sizeInt, err := strconv.ParseInt(size, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return "", iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// 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)
|
2015-05-04 02:16:10 -04:00
|
|
|
totalLength, err := io.CopyN(mw, objectData, sizeInt)
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-07 18:55:44 -04:00
|
|
|
donutObjectMetadata["sys.size"] = strconv.FormatInt(totalLength, 10)
|
2015-04-07 01:29:10 -04:00
|
|
|
objectMetadata["size"] = strconv.FormatInt(totalLength, 10)
|
2015-04-05 04:53:41 -04:00
|
|
|
case false:
|
2015-04-08 19:28:14 -04:00
|
|
|
// 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 {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
// encoded data with k, m and write
|
2015-04-07 00:42:54 -04:00
|
|
|
chunkCount, totalLength, err := b.writeEncodedData(k, m, writers, objectData, summer)
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
/// donutMetadata section
|
2015-04-07 18:55:44 -04:00
|
|
|
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)
|
2015-04-08 19:28:14 -04:00
|
|
|
// keep size inside objectMetadata as well for Object API requests
|
2015-04-07 01:29:10 -04:00
|
|
|
objectMetadata["size"] = strconv.Itoa(totalLength)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-07 18:55:44 -04:00
|
|
|
objectMetadata["bucket"] = b.name
|
|
|
|
objectMetadata["object"] = objectName
|
2015-04-08 19:28:14 -04:00
|
|
|
// store all user provided metadata
|
2015-04-07 18:55:44 -04:00
|
|
|
for k, v := range metadata {
|
|
|
|
objectMetadata[k] = v
|
|
|
|
}
|
2015-04-05 04:53:41 -04:00
|
|
|
dataMd5sum := summer.Sum(nil)
|
2015-04-30 22:26:23 -04:00
|
|
|
objectMetadata["created"] = time.Now().UTC().Format(time.RFC3339Nano)
|
2015-04-07 18:55:44 -04:00
|
|
|
|
|
|
|
// keeping md5sum for the object in two different places
|
|
|
|
// one for object storage and another is for internal use
|
2015-04-07 01:29:10 -04:00
|
|
|
objectMetadata["md5"] = hex.EncodeToString(dataMd5sum)
|
2015-04-07 18:55:44 -04:00
|
|
|
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 {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-07 01:29:10 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-07 18:55:44 -04:00
|
|
|
// write donut specific metadata
|
|
|
|
if err := b.writeDonutObjectMetadata(b.normalizeObjectName(objectName), donutObjectMetadata); err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-07 18:55:44 -04:00
|
|
|
}
|
|
|
|
// write object specific metadata
|
|
|
|
if err := b.writeObjectMetadata(b.normalizeObjectName(objectName), objectMetadata); err != nil {
|
2015-04-30 06:38:11 -04:00
|
|
|
return "", iodine.New(err, nil)
|
2015-04-07 18:55:44 -04:00
|
|
|
}
|
|
|
|
// close all writers, when control flow reaches here
|
|
|
|
for _, writer := range writers {
|
|
|
|
writer.Close()
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
2015-04-30 06:38:11 -04:00
|
|
|
return objectMetadata["md5"], nil
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|