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 (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-04-07 00:42:54 -04:00
|
|
|
"hash"
|
2015-04-05 04:53:41 -04:00
|
|
|
"io"
|
2015-06-18 19:02:34 -04:00
|
|
|
"path/filepath"
|
2015-06-25 18:03:34 -04:00
|
|
|
"sort"
|
2015-04-07 00:42:54 -04:00
|
|
|
"strings"
|
2015-06-25 13:43:36 -04:00
|
|
|
"sync"
|
2015-06-24 23:42:36 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2015-04-07 00:42:54 -04:00
|
|
|
|
2015-07-03 18:17:44 -04:00
|
|
|
"github.com/minio/minio/pkg/crypto/sha512"
|
|
|
|
"github.com/minio/minio/pkg/donut/split"
|
2015-05-11 19:23:10 -04:00
|
|
|
"github.com/minio/minio/pkg/iodine"
|
2015-04-05 04:53:41 -04:00
|
|
|
)
|
|
|
|
|
2015-06-27 02:22:30 -04:00
|
|
|
const (
|
|
|
|
blockSize = 10 * 1024 * 1024
|
|
|
|
)
|
|
|
|
|
2015-06-24 23:42:36 -04:00
|
|
|
// internal struct carrying bucket specific information
|
|
|
|
type bucket struct {
|
|
|
|
name string
|
|
|
|
acl string
|
|
|
|
time time.Time
|
|
|
|
donutName string
|
2015-06-25 15:55:13 -04:00
|
|
|
nodes map[string]node
|
2015-07-03 20:11:26 -04:00
|
|
|
lock *sync.Mutex
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 00:00:28 -04:00
|
|
|
// newBucket - instantiate a new bucket
|
2015-06-26 18:32:24 -04:00
|
|
|
func newBucket(bucketName, aclType, donutName string, nodes map[string]node) (bucket, BucketMetadata, error) {
|
2015-06-24 23:42:36 -04:00
|
|
|
errParams := map[string]string{
|
|
|
|
"bucketName": bucketName,
|
|
|
|
"donutName": donutName,
|
|
|
|
"aclType": aclType,
|
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
|
2015-06-24 23:42:36 -04:00
|
|
|
if strings.TrimSpace(bucketName) == "" || strings.TrimSpace(donutName) == "" {
|
2015-06-26 18:32:24 -04:00
|
|
|
return bucket{}, BucketMetadata{}, iodine.New(InvalidArgument{}, errParams)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
2015-06-26 18:32:24 -04:00
|
|
|
|
2015-06-24 23:42:36 -04:00
|
|
|
b := bucket{}
|
2015-06-26 18:32:24 -04:00
|
|
|
t := time.Now().UTC()
|
2015-06-24 23:42:36 -04:00
|
|
|
b.name = bucketName
|
|
|
|
b.acl = aclType
|
|
|
|
b.time = t
|
|
|
|
b.donutName = donutName
|
|
|
|
b.nodes = nodes
|
2015-07-03 20:11:26 -04:00
|
|
|
b.lock = new(sync.Mutex)
|
2015-06-26 18:32:24 -04:00
|
|
|
|
|
|
|
metadata := BucketMetadata{}
|
2015-06-26 20:54:19 -04:00
|
|
|
metadata.Version = bucketMetadataVersion
|
2015-06-26 18:32:24 -04:00
|
|
|
metadata.Name = bucketName
|
2015-06-30 20:08:18 -04:00
|
|
|
metadata.ACL = BucketACL(aclType)
|
2015-06-26 18:32:24 -04:00
|
|
|
metadata.Created = t
|
2015-06-26 20:54:19 -04:00
|
|
|
metadata.Metadata = make(map[string]string)
|
2015-06-27 02:22:30 -04:00
|
|
|
metadata.BucketObjects = make(map[string]interface{})
|
2015-06-26 18:32:24 -04:00
|
|
|
|
|
|
|
return b, metadata, nil
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
|
2015-06-26 05:10:47 -04:00
|
|
|
func (b bucket) getBucketName() string {
|
|
|
|
return b.name
|
|
|
|
}
|
2015-06-26 20:54:19 -04:00
|
|
|
func (b bucket) getBucketMetadataReaders() ([]io.ReadCloser, error) {
|
|
|
|
var readers []io.ReadCloser
|
|
|
|
for _, node := range b.nodes {
|
|
|
|
disks, err := node.ListDisks()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
readers = make([]io.ReadCloser, len(disks))
|
|
|
|
for order, disk := range disks {
|
|
|
|
bucketMetaDataReader, err := disk.OpenFile(filepath.Join(b.donutName, bucketMetadataConfig))
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
readers[order] = bucketMetaDataReader
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b bucket) getBucketMetadata() (*AllBuckets, error) {
|
|
|
|
metadata := new(AllBuckets)
|
|
|
|
readers, err := b.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
|
|
|
|
}
|
|
|
|
return nil, iodine.New(InvalidArgument{}, nil)
|
2015-06-25 18:03:34 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 13:59:27 -04:00
|
|
|
// GetObjectMetadata - get metadata for an object
|
|
|
|
func (b bucket) GetObjectMetadata(objectName string) (ObjectMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2015-06-29 13:59:27 -04:00
|
|
|
return b.readObjectMetadata(objectName)
|
|
|
|
}
|
|
|
|
|
2015-06-24 23:42:36 -04:00
|
|
|
// ListObjects - list all objects
|
2015-07-02 23:31:22 -04:00
|
|
|
func (b bucket) ListObjects(prefix, marker, delimiter string, maxkeys int) (ListObjectsResults, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2015-06-25 18:03:34 -04:00
|
|
|
if maxkeys <= 0 {
|
|
|
|
maxkeys = 1000
|
|
|
|
}
|
|
|
|
var isTruncated bool
|
|
|
|
var objects []string
|
2015-06-26 20:54:19 -04:00
|
|
|
bucketMetadata, err := b.getBucketMetadata()
|
|
|
|
if err != nil {
|
2015-07-02 23:31:22 -04:00
|
|
|
return ListObjectsResults{}, iodine.New(err, nil)
|
2015-06-26 20:54:19 -04:00
|
|
|
}
|
2015-06-27 02:22:30 -04:00
|
|
|
for objectName := range bucketMetadata.Buckets[b.getBucketName()].BucketObjects {
|
2015-06-26 20:54:19 -04:00
|
|
|
if strings.HasPrefix(objectName, strings.TrimSpace(prefix)) {
|
|
|
|
if objectName > marker {
|
2015-07-01 18:49:15 -04:00
|
|
|
objects = append(objects, objectName)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-26 20:54:19 -04:00
|
|
|
if strings.TrimSpace(prefix) != "" {
|
2015-06-27 15:39:04 -04:00
|
|
|
objects = TrimPrefix(objects, prefix)
|
2015-06-26 20:54:19 -04:00
|
|
|
}
|
|
|
|
var prefixes []string
|
|
|
|
var filteredObjects []string
|
|
|
|
if strings.TrimSpace(delimiter) != "" {
|
2015-06-27 15:39:04 -04:00
|
|
|
filteredObjects = HasNoDelimiter(objects, delimiter)
|
|
|
|
prefixes = HasDelimiter(objects, delimiter)
|
|
|
|
prefixes = SplitDelimiter(prefixes, delimiter)
|
|
|
|
prefixes = SortU(prefixes)
|
2015-06-26 20:54:19 -04:00
|
|
|
} else {
|
|
|
|
filteredObjects = objects
|
|
|
|
}
|
|
|
|
var results []string
|
|
|
|
var commonPrefixes []string
|
2015-06-25 21:52:07 -04:00
|
|
|
|
2015-06-27 15:39:04 -04:00
|
|
|
for _, commonPrefix := range prefixes {
|
2015-07-01 18:49:15 -04:00
|
|
|
commonPrefixes = append(commonPrefixes, prefix+commonPrefix)
|
2015-06-27 15:39:04 -04:00
|
|
|
}
|
2015-07-01 18:49:15 -04:00
|
|
|
filteredObjects = RemoveDuplicates(filteredObjects)
|
2015-06-26 20:54:19 -04:00
|
|
|
sort.Strings(filteredObjects)
|
|
|
|
for _, objectName := range filteredObjects {
|
|
|
|
if len(results) >= maxkeys {
|
|
|
|
isTruncated = true
|
|
|
|
break
|
2015-06-25 18:03:34 -04:00
|
|
|
}
|
2015-07-01 18:49:15 -04:00
|
|
|
results = append(results, prefix+objectName)
|
2015-06-25 18:03:34 -04:00
|
|
|
}
|
2015-07-01 18:49:15 -04:00
|
|
|
results = RemoveDuplicates(results)
|
|
|
|
commonPrefixes = RemoveDuplicates(commonPrefixes)
|
2015-06-26 20:54:19 -04:00
|
|
|
sort.Strings(commonPrefixes)
|
2015-06-27 16:12:33 -04:00
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
listObjects := ListObjectsResults{}
|
2015-06-29 13:59:27 -04:00
|
|
|
listObjects.Objects = make(map[string]ObjectMetadata)
|
2015-06-27 16:12:33 -04:00
|
|
|
listObjects.CommonPrefixes = commonPrefixes
|
|
|
|
listObjects.IsTruncated = isTruncated
|
2015-06-29 13:59:27 -04:00
|
|
|
|
|
|
|
for _, objectName := range results {
|
|
|
|
objMetadata, err := b.readObjectMetadata(normalizeObjectName(objectName))
|
|
|
|
if err != nil {
|
2015-07-02 23:31:22 -04:00
|
|
|
return ListObjectsResults{}, iodine.New(err, nil)
|
2015-06-29 13:59:27 -04:00
|
|
|
}
|
|
|
|
listObjects.Objects[objectName] = objMetadata
|
|
|
|
}
|
2015-06-27 16:12:33 -04:00
|
|
|
return listObjects, nil
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadObject - open an object to read
|
|
|
|
func (b bucket) ReadObject(objectName string) (reader io.ReadCloser, size int64, err error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2015-06-24 23:42:36 -04:00
|
|
|
reader, writer := io.Pipe()
|
|
|
|
// get list of objects
|
2015-06-26 20:54:19 -04:00
|
|
|
bucketMetadata, err := b.getBucketMetadata()
|
2015-06-24 23:42:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
// check if object exists
|
2015-06-27 02:22:30 -04:00
|
|
|
if _, ok := bucketMetadata.Buckets[b.getBucketName()].BucketObjects[objectName]; !ok {
|
2015-06-24 23:42:36 -04:00
|
|
|
return nil, 0, iodine.New(ObjectNotFound{Object: objectName}, nil)
|
|
|
|
}
|
2015-06-29 13:59:27 -04:00
|
|
|
objMetadata, err := b.readObjectMetadata(normalizeObjectName(objectName))
|
2015-06-24 23:42:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
// read and reply back to GetObject() request in a go-routine
|
2015-06-29 14:42:58 -04:00
|
|
|
go b.readObjectData(normalizeObjectName(objectName), writer, objMetadata)
|
2015-06-26 20:54:19 -04:00
|
|
|
return reader, objMetadata.Size, nil
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteObject - write a new object into bucket
|
2015-06-29 14:10:52 -04:00
|
|
|
func (b bucket) WriteObject(objectName string, objectData io.Reader, expectedMD5Sum string, metadata map[string]string) (ObjectMetadata, error) {
|
2015-06-25 13:43:36 -04:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2015-06-24 23:42:36 -04:00
|
|
|
if objectName == "" || objectData == nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(InvalidArgument{}, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
2015-06-29 14:42:58 -04:00
|
|
|
writers, err := b.getWriters(normalizeObjectName(objectName), "data")
|
2015-06-24 23:42:36 -04:00
|
|
|
if err != nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
2015-06-26 05:10:47 -04:00
|
|
|
sumMD5 := md5.New()
|
|
|
|
sum512 := sha512.New()
|
2015-06-29 14:10:52 -04:00
|
|
|
objMetadata := ObjectMetadata{}
|
2015-06-26 18:32:24 -04:00
|
|
|
objMetadata.Version = objectMetadataVersion
|
2015-06-26 20:54:19 -04:00
|
|
|
objMetadata.Created = time.Now().UTC()
|
2015-06-24 23:42:36 -04:00
|
|
|
// if total writers are only '1' do not compute erasure
|
|
|
|
switch len(writers) == 1 {
|
|
|
|
case true:
|
2015-06-26 05:10:47 -04:00
|
|
|
mw := io.MultiWriter(writers[0], sumMD5, sum512)
|
2015-06-26 20:54:19 -04:00
|
|
|
totalLength, err := io.Copy(mw, objectData)
|
2015-06-24 23:42:36 -04:00
|
|
|
if err != nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
2015-06-26 18:32:24 -04:00
|
|
|
objMetadata.Size = totalLength
|
2015-06-24 23:42:36 -04:00
|
|
|
case false:
|
|
|
|
// calculate data and parity dictated by total number of writers
|
|
|
|
k, m, err := b.getDataAndParity(len(writers))
|
|
|
|
if err != nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
2015-06-29 14:42:58 -04:00
|
|
|
// write encoded data with k, m and writers
|
|
|
|
chunkCount, totalLength, err := b.writeObjectData(k, m, writers, objectData, sumMD5, sum512)
|
2015-06-24 23:42:36 -04:00
|
|
|
if err != nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
/// donutMetadata section
|
2015-06-27 02:22:30 -04:00
|
|
|
objMetadata.BlockSize = blockSize
|
2015-06-26 20:54:19 -04:00
|
|
|
objMetadata.ChunkCount = chunkCount
|
|
|
|
objMetadata.DataDisks = k
|
|
|
|
objMetadata.ParityDisks = m
|
|
|
|
objMetadata.ErasureTechnique = "Cauchy"
|
2015-06-26 18:32:24 -04:00
|
|
|
objMetadata.Size = int64(totalLength)
|
|
|
|
}
|
|
|
|
objMetadata.Bucket = b.getBucketName()
|
|
|
|
objMetadata.Object = objectName
|
2015-06-26 05:10:47 -04:00
|
|
|
dataMD5sum := sumMD5.Sum(nil)
|
|
|
|
dataSHA512sum := sum512.Sum(nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
|
2015-06-26 20:54:19 -04:00
|
|
|
objMetadata.MD5Sum = hex.EncodeToString(dataMD5sum)
|
|
|
|
objMetadata.SHA512Sum = hex.EncodeToString(dataSHA512sum)
|
2015-06-24 23:42:36 -04:00
|
|
|
|
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
2015-06-26 18:32:24 -04:00
|
|
|
if err := b.isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), objMetadata.MD5Sum); err != nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-27 02:22:30 -04:00
|
|
|
objMetadata.Metadata = metadata
|
2015-06-24 23:42:36 -04:00
|
|
|
// write object specific metadata
|
2015-06-26 20:54:19 -04:00
|
|
|
if err := b.writeObjectMetadata(normalizeObjectName(objectName), objMetadata); err != nil {
|
2015-06-29 14:10:52 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
|
|
|
// close all writers, when control flow reaches here
|
|
|
|
for _, writer := range writers {
|
|
|
|
writer.Close()
|
|
|
|
}
|
2015-06-29 14:10:52 -04:00
|
|
|
return objMetadata, nil
|
2015-06-24 23:42:36 -04:00
|
|
|
}
|
2015-04-08 19:28:14 -04:00
|
|
|
|
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, other its `nil`
|
2015-04-07 01:29:10 -04:00
|
|
|
func (b bucket) isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) error {
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" && strings.TrimSpace(actualMD5Sum) != "" {
|
2015-04-07 18:55:44 -04:00
|
|
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
2015-04-07 01:29:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
actualMD5SumBytes, err := hex.DecodeString(actualMD5Sum)
|
|
|
|
if err != nil {
|
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(expectedMD5SumBytes, actualMD5SumBytes) {
|
2015-06-23 14:44:32 -04:00
|
|
|
return iodine.New(BadDigest{}, nil)
|
2015-04-07 01:29:10 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-23 14:44:32 -04:00
|
|
|
return iodine.New(InvalidArgument{}, nil)
|
2015-04-07 01:29:10 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// writeObjectMetadata - write additional object metadata
|
2015-06-29 14:10:52 -04:00
|
|
|
func (b bucket) writeObjectMetadata(objectName string, objMetadata ObjectMetadata) error {
|
|
|
|
if objMetadata.Object == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
return iodine.New(InvalidArgument{}, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
2015-06-29 14:42:58 -04:00
|
|
|
objMetadataWriters, err := b.getWriters(objectName, objectMetadataConfig)
|
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-06-26 18:32:24 -04:00
|
|
|
for _, objMetadataWriter := range objMetadataWriters {
|
|
|
|
jenc := json.NewEncoder(objMetadataWriter)
|
2015-06-29 14:10:52 -04:00
|
|
|
if err := jenc.Encode(&objMetadata); err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 21:14:52 -04:00
|
|
|
for _, objMetadataWriter := range objMetadataWriters {
|
|
|
|
objMetadataWriter.Close()
|
|
|
|
}
|
2015-04-07 18:55:44 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 13:59:27 -04:00
|
|
|
// readObjectMetadata - read object metadata
|
|
|
|
func (b bucket) readObjectMetadata(objectName string) (ObjectMetadata, error) {
|
|
|
|
objMetadata := ObjectMetadata{}
|
|
|
|
if objectName == "" {
|
|
|
|
return ObjectMetadata{}, iodine.New(InvalidArgument{}, nil)
|
|
|
|
}
|
2015-06-29 14:42:58 -04:00
|
|
|
objMetadataReaders, err := b.getReaders(objectName, objectMetadataConfig)
|
2015-06-29 13:59:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
for _, objMetadataReader := range objMetadataReaders {
|
|
|
|
defer objMetadataReader.Close()
|
|
|
|
}
|
|
|
|
for _, objMetadataReader := range objMetadataReaders {
|
|
|
|
jdec := json.NewDecoder(objMetadataReader)
|
|
|
|
if err := jdec.Decode(&objMetadata); err != nil {
|
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return objMetadata, nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// TODO - This a temporary normalization of objectNames, need to find a better way
|
|
|
|
//
|
|
|
|
// normalizedObjectName - all objectNames with "/" get normalized to a simple objectName
|
|
|
|
//
|
|
|
|
// example:
|
|
|
|
// user provided value - "this/is/my/deep/directory/structure"
|
|
|
|
// donut normalized value - "this-is-my-deep-directory-structure"
|
|
|
|
//
|
2015-06-26 20:54:19 -04:00
|
|
|
func normalizeObjectName(objectName string) string {
|
2015-04-07 00:42:54 -04:00
|
|
|
// replace every '/' with '-'
|
|
|
|
return strings.Replace(objectName, "/", "-", -1)
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// getDataAndParity - calculate k, m (data and parity) values from number of disks
|
2015-04-05 04:53:41 -04:00
|
|
|
func (b bucket) getDataAndParity(totalWriters int) (k uint8, m uint8, err error) {
|
|
|
|
if totalWriters <= 1 {
|
2015-06-23 14:44:32 -04:00
|
|
|
return 0, 0, iodine.New(InvalidArgument{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
quotient := totalWriters / 2 // not using float or abs to let integer round off to lower value
|
|
|
|
// quotient cannot be bigger than (255 / 2) = 127
|
|
|
|
if quotient > 127 {
|
2015-06-23 14:44:32 -04:00
|
|
|
return 0, 0, iodine.New(ParityOverflow{}, nil)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
remainder := totalWriters % 2 // will be 1 for odd and 0 for even numbers
|
|
|
|
k = uint8(quotient + remainder)
|
|
|
|
m = uint8(quotient)
|
|
|
|
return k, m, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:42:58 -04:00
|
|
|
// writeObjectData -
|
|
|
|
func (b bucket) writeObjectData(k, m uint8, writers []io.WriteCloser, objectData io.Reader, sumMD5, sum512 hash.Hash) (int, int, error) {
|
2015-06-25 00:00:28 -04:00
|
|
|
encoder, err := newEncoder(k, m, "Cauchy")
|
2015-04-07 00:42:54 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
return 0, 0, iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
chunkCount := 0
|
|
|
|
totalLength := 0
|
2015-06-27 18:02:49 -04:00
|
|
|
for chunk := range split.Stream(objectData, 10*1024*1024) {
|
2015-06-29 14:42:58 -04:00
|
|
|
if chunk.Err != nil {
|
|
|
|
return 0, 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
totalLength = totalLength + len(chunk.Data)
|
|
|
|
encodedBlocks, _ := encoder.Encode(chunk.Data)
|
|
|
|
sumMD5.Write(chunk.Data)
|
|
|
|
sum512.Write(chunk.Data)
|
|
|
|
for blockIndex, block := range encodedBlocks {
|
|
|
|
_, err := io.Copy(writers[blockIndex], bytes.NewBuffer(block))
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, iodine.New(err, nil)
|
2015-04-07 00:42:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
chunkCount = chunkCount + 1
|
|
|
|
}
|
|
|
|
return chunkCount, totalLength, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:42:58 -04:00
|
|
|
// readObjectData -
|
|
|
|
func (b bucket) readObjectData(objectName string, writer *io.PipeWriter, objMetadata ObjectMetadata) {
|
|
|
|
readers, err := b.getReaders(objectName, "data")
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
writer.CloseWithError(iodine.New(err, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
2015-06-25 21:52:07 -04:00
|
|
|
for _, reader := range readers {
|
|
|
|
defer reader.Close()
|
|
|
|
}
|
2015-06-26 20:54:19 -04:00
|
|
|
expectedMd5sum, err := hex.DecodeString(objMetadata.MD5Sum)
|
|
|
|
if err != nil {
|
|
|
|
writer.CloseWithError(iodine.New(err, nil))
|
|
|
|
return
|
|
|
|
}
|
2015-04-05 04:53:41 -04:00
|
|
|
hasher := md5.New()
|
|
|
|
mwriter := io.MultiWriter(writer, hasher)
|
|
|
|
switch len(readers) == 1 {
|
|
|
|
case false:
|
2015-06-26 20:54:19 -04:00
|
|
|
if objMetadata.ErasureTechnique == "" {
|
2015-06-23 14:44:32 -04:00
|
|
|
writer.CloseWithError(iodine.New(MissingErasureTechnique{}, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
2015-06-26 20:54:19 -04:00
|
|
|
encoder, err := newEncoder(objMetadata.DataDisks, objMetadata.ParityDisks, objMetadata.ErasureTechnique)
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
writer.CloseWithError(iodine.New(err, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
2015-06-26 20:54:19 -04:00
|
|
|
totalLeft := objMetadata.Size
|
|
|
|
for i := 0; i < objMetadata.ChunkCount; i++ {
|
|
|
|
decodedData, err := b.decodeEncodedData(totalLeft, int64(objMetadata.BlockSize), readers, encoder, writer)
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
writer.CloseWithError(iodine.New(err, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
2015-07-07 00:54:00 -04:00
|
|
|
_, err = io.Copy(mwriter, bytes.NewReader(decodedData))
|
2015-04-05 04:53:41 -04:00
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
writer.CloseWithError(iodine.New(err, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
2015-06-26 20:54:19 -04:00
|
|
|
totalLeft = totalLeft - int64(objMetadata.BlockSize)
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
case true:
|
|
|
|
_, err := io.Copy(writer, readers[0])
|
|
|
|
if err != nil {
|
2015-04-07 18:55:44 -04:00
|
|
|
writer.CloseWithError(iodine.New(err, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if decodedData md5sum matches
|
|
|
|
if !bytes.Equal(expectedMd5sum, hasher.Sum(nil)) {
|
2015-06-23 14:44:32 -04:00
|
|
|
writer.CloseWithError(iodine.New(ChecksumMismatch{}, nil))
|
2015-04-05 04:53:41 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:28:14 -04:00
|
|
|
// decodeEncodedData -
|
2015-06-24 22:43:38 -04:00
|
|
|
func (b bucket) decodeEncodedData(totalLeft, blockSize int64, readers []io.ReadCloser, encoder encoder, writer *io.PipeWriter) ([]byte, error) {
|
2015-04-05 04:53:41 -04:00
|
|
|
var curBlockSize int64
|
|
|
|
if blockSize < totalLeft {
|
|
|
|
curBlockSize = blockSize
|
|
|
|
} else {
|
2015-07-07 00:54:00 -04:00
|
|
|
curBlockSize = totalLeft
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
curChunkSize, err := encoder.GetEncodedBlockLen(int(curBlockSize))
|
|
|
|
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
|
|
|
}
|
|
|
|
encodedBytes := make([][]byte, len(readers))
|
|
|
|
for i, reader := range readers {
|
|
|
|
var bytesBuffer bytes.Buffer
|
|
|
|
_, err := io.CopyN(&bytesBuffer, reader, int64(curChunkSize))
|
|
|
|
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
|
|
|
}
|
|
|
|
encodedBytes[i] = bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
decodedData, err := encoder.Decode(encodedBytes, int(curBlockSize))
|
|
|
|
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
|
|
|
}
|
|
|
|
return decodedData, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:42:58 -04:00
|
|
|
// getReaders -
|
|
|
|
func (b bucket) getReaders(objectName, objectMeta string) ([]io.ReadCloser, error) {
|
2015-04-05 04:53:41 -04:00
|
|
|
var readers []io.ReadCloser
|
|
|
|
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
|
|
|
}
|
|
|
|
readers = make([]io.ReadCloser, len(disks))
|
2015-06-24 23:34:20 -04:00
|
|
|
for order, disk := range disks {
|
|
|
|
bucketSlice := fmt.Sprintf("%s$%d$%d", b.name, nodeSlice, order)
|
2015-06-18 19:02:34 -04:00
|
|
|
objectPath := filepath.Join(b.donutName, bucketSlice, objectName, objectMeta)
|
2015-04-05 04:53:41 -04:00
|
|
|
objectSlice, err := disk.OpenFile(objectPath)
|
|
|
|
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
|
|
|
}
|
2015-06-24 23:34:20 -04:00
|
|
|
readers[order] = objectSlice
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
nodeSlice = nodeSlice + 1
|
|
|
|
}
|
|
|
|
return readers, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 14:42:58 -04:00
|
|
|
// getWriters -
|
|
|
|
func (b bucket) getWriters(objectName, objectMeta string) ([]io.WriteCloser, error) {
|
2015-04-05 04:53:41 -04:00
|
|
|
var writers []io.WriteCloser
|
|
|
|
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
|
|
|
}
|
|
|
|
writers = make([]io.WriteCloser, len(disks))
|
2015-06-24 23:34:20 -04:00
|
|
|
for order, disk := range disks {
|
|
|
|
bucketSlice := fmt.Sprintf("%s$%d$%d", b.name, nodeSlice, order)
|
2015-06-18 19:02:34 -04:00
|
|
|
objectPath := filepath.Join(b.donutName, bucketSlice, objectName, objectMeta)
|
2015-06-24 23:34:20 -04:00
|
|
|
objectSlice, err := disk.CreateFile(objectPath)
|
2015-04-05 04:53:41 -04:00
|
|
|
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
|
|
|
}
|
2015-06-24 23:34:20 -04:00
|
|
|
writers[order] = objectSlice
|
2015-04-05 04:53:41 -04:00
|
|
|
}
|
|
|
|
nodeSlice = nodeSlice + 1
|
|
|
|
}
|
|
|
|
return writers, nil
|
|
|
|
}
|