2015-03-14 14:34:26 -04:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
2015-03-14 14:34:26 -04:00
|
|
|
*
|
|
|
|
* 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-03-18 15:25:37 -04:00
|
|
|
package donutstorage
|
2015-03-14 14:34:26 -04:00
|
|
|
|
|
|
|
import (
|
2015-03-15 22:02:00 -04:00
|
|
|
"bytes"
|
2015-03-14 14:34:26 -04:00
|
|
|
"errors"
|
2015-03-16 21:20:01 -04:00
|
|
|
"io"
|
2015-03-16 23:41:00 -04:00
|
|
|
"sort"
|
2015-03-16 21:20:01 -04:00
|
|
|
"strconv"
|
2015-03-16 23:41:00 -04:00
|
|
|
"strings"
|
2015-03-16 21:20:01 -04:00
|
|
|
"time"
|
|
|
|
|
2015-03-17 16:32:10 -04:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
|
2015-03-14 21:02:19 -04:00
|
|
|
"github.com/minio-io/minio/pkg/donutbox"
|
2015-03-15 20:30:42 -04:00
|
|
|
"github.com/minio-io/minio/pkg/encoding/erasure"
|
2015-03-14 14:34:26 -04:00
|
|
|
"github.com/minio-io/minio/pkg/storage"
|
2015-03-15 20:30:42 -04:00
|
|
|
"github.com/minio-io/minio/pkg/utils/split"
|
2015-03-14 14:34:26 -04:00
|
|
|
)
|
|
|
|
|
2015-03-18 15:25:37 -04:00
|
|
|
// DonutDriver creates a new single disk storage driver using donut without encoding.
|
|
|
|
type DonutDriver struct {
|
2015-03-14 21:02:19 -04:00
|
|
|
donutBox donutbox.DonutBox
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
2015-03-16 21:20:01 -04:00
|
|
|
const (
|
|
|
|
blockSize = 10 * 1024 * 1024
|
|
|
|
)
|
|
|
|
|
2015-03-14 14:34:26 -04:00
|
|
|
// Start a single disk subsystem
|
2015-03-15 03:41:06 -04:00
|
|
|
func Start(donutBox donutbox.DonutBox) (chan<- string, <-chan error, storage.Storage) {
|
2015-03-14 14:34:26 -04:00
|
|
|
ctrlChannel := make(chan string)
|
|
|
|
errorChannel := make(chan error)
|
2015-03-18 15:25:37 -04:00
|
|
|
s := new(DonutDriver)
|
2015-03-15 03:41:06 -04:00
|
|
|
s.donutBox = donutBox
|
2015-03-14 14:34:26 -04:00
|
|
|
go start(ctrlChannel, errorChannel, s)
|
|
|
|
return ctrlChannel, errorChannel, s
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:25:37 -04:00
|
|
|
func start(ctrlChannel <-chan string, errorChannel chan<- error, s *DonutDriver) {
|
2015-03-14 14:34:26 -04:00
|
|
|
close(errorChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBuckets returns a list of buckets
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) ListBuckets() (results []storage.BucketMetadata, err error) {
|
|
|
|
buckets, err := donutStorage.donutBox.ListBuckets()
|
2015-03-17 02:41:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sort.Strings(buckets)
|
|
|
|
for _, bucket := range buckets {
|
2015-03-18 15:25:37 -04:00
|
|
|
metadata, err := donutStorage.donutBox.GetBucketMetadata(bucket)
|
2015-03-17 02:41:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-17 03:29:19 -04:00
|
|
|
|
|
|
|
created, err := time.Parse(time.RFC3339Nano, metadata["created"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bucketMetadata := storage.BucketMetadata{
|
|
|
|
Name: bucket,
|
|
|
|
Created: created,
|
|
|
|
}
|
2015-03-17 02:41:16 -04:00
|
|
|
results = append(results, bucketMetadata)
|
|
|
|
}
|
2015-03-17 03:29:19 -04:00
|
|
|
return results, err
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucket creates a new bucket
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) CreateBucket(bucket string) error {
|
|
|
|
err := donutStorage.donutBox.CreateBucket(bucket)
|
2015-03-17 02:41:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
metadataBucket := storage.BucketMetadata{
|
|
|
|
Name: bucket,
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
metadata := createBucketMetadata(metadataBucket)
|
2015-03-18 15:25:37 -04:00
|
|
|
err = donutStorage.donutBox.SetBucketMetadata(bucket, metadata)
|
2015-03-17 02:41:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketMetadata retrieves an bucket's metadata
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) GetBucketMetadata(bucket string) (storage.BucketMetadata, error) {
|
|
|
|
metadata, err := donutStorage.donutBox.GetBucketMetadata(bucket)
|
2015-03-17 02:41:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return storage.BucketMetadata{}, err
|
|
|
|
}
|
2015-03-17 03:45:50 -04:00
|
|
|
created, err := time.Parse(time.RFC3339Nano, metadata["created"])
|
2015-03-17 02:41:16 -04:00
|
|
|
bucketMetadata := storage.BucketMetadata{
|
|
|
|
Name: bucket,
|
|
|
|
Created: created,
|
|
|
|
}
|
|
|
|
return bucketMetadata, nil
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucketPolicy sets a bucket's access policy
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) CreateBucketPolicy(bucket string, p storage.BucketPolicy) error {
|
2015-03-14 14:34:26 -04:00
|
|
|
return errors.New("Not Implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketPolicy returns a bucket's access policy
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) GetBucketPolicy(bucket string) (storage.BucketPolicy, error) {
|
2015-03-14 14:34:26 -04:00
|
|
|
return storage.BucketPolicy{}, errors.New("Not Implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject retrieves an object and writes it to a writer
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) GetObject(target io.Writer, bucket, key string) (int64, error) {
|
|
|
|
metadata, err := donutStorage.donutBox.GetObjectMetadata(bucket, key, 0)
|
2015-03-17 03:29:19 -04:00
|
|
|
if err != nil {
|
|
|
|
// TODO strongly type and properly handle error cases
|
2015-03-17 02:41:16 -04:00
|
|
|
return 0, storage.ObjectNotFound{Bucket: bucket, Object: key}
|
|
|
|
}
|
2015-03-16 20:40:59 -04:00
|
|
|
k, err := strconv.Atoi(metadata["erasureK"])
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Cannot parse erasureK")
|
|
|
|
}
|
|
|
|
m, err := strconv.Atoi(metadata["erasureM"])
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Cannot parse erasureM")
|
|
|
|
}
|
|
|
|
columnCount := k + m
|
2015-03-16 21:20:01 -04:00
|
|
|
bs, err := strconv.Atoi(metadata["blockSize"])
|
2015-03-15 22:02:00 -04:00
|
|
|
if err != nil {
|
2015-03-16 20:40:59 -04:00
|
|
|
return 0, errors.New("Cannot parse blockSize")
|
2015-03-15 22:02:00 -04:00
|
|
|
}
|
2015-03-16 20:40:59 -04:00
|
|
|
size, err := strconv.Atoi(metadata["size"])
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("Cannot parse length")
|
|
|
|
}
|
2015-03-16 21:20:01 -04:00
|
|
|
chunkCount := size/bs + 1
|
2015-03-15 22:02:00 -04:00
|
|
|
var readers []io.Reader
|
|
|
|
for column := 0; column < columnCount; column++ {
|
2015-03-18 15:25:37 -04:00
|
|
|
reader, err := donutStorage.donutBox.GetObjectReader(bucket, key, uint(column))
|
2015-03-15 22:02:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
readers = append(readers, reader)
|
|
|
|
}
|
|
|
|
|
2015-03-16 20:40:59 -04:00
|
|
|
totalWritten := int64(size)
|
|
|
|
totalRemaining := int64(size)
|
2015-03-15 22:02:00 -04:00
|
|
|
if err != err {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
params, err := erasure.ParseEncoderParams(8, 8, erasure.Cauchy)
|
|
|
|
decoder := erasure.NewEncoder(params)
|
|
|
|
for chunk := 0; chunk < chunkCount; chunk++ {
|
|
|
|
blocks := make([][]byte, columnCount)
|
|
|
|
for column := 0; column < columnCount; column++ {
|
|
|
|
var block bytes.Buffer
|
|
|
|
limitReader := io.LimitReader(readers[column], int64(blockSize))
|
|
|
|
_, err := io.Copy(&block, limitReader)
|
|
|
|
if err != nil {
|
|
|
|
return totalWritten, err
|
|
|
|
}
|
|
|
|
blocks[column] = block.Bytes()
|
|
|
|
}
|
|
|
|
curBlockSize := blockSize
|
|
|
|
if totalRemaining < int64(blockSize) {
|
|
|
|
curBlockSize = int(totalRemaining)
|
|
|
|
}
|
|
|
|
original, err := decoder.Decode(blocks, curBlockSize)
|
|
|
|
if err != nil {
|
|
|
|
return totalWritten, err
|
|
|
|
}
|
|
|
|
curWritten, err := io.Copy(target, bytes.NewBuffer(original))
|
|
|
|
totalRemaining = totalRemaining - curWritten
|
|
|
|
if err != nil {
|
|
|
|
return totalWritten, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalWritten, nil
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPartialObject retrieves an object and writes it to a writer
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) {
|
2015-03-14 14:34:26 -04:00
|
|
|
return 0, errors.New("Not Implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectMetadata retrieves an object's metadata
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) GetObjectMetadata(bucket, key string, prefix string) (storage.ObjectMetadata, error) {
|
|
|
|
metadata, err := donutStorage.donutBox.GetObjectMetadata(bucket, key, 0)
|
2015-03-16 20:40:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return storage.ObjectMetadata{}, err
|
|
|
|
}
|
2015-03-17 03:47:17 -04:00
|
|
|
created, err := time.Parse(time.RFC3339Nano, metadata["created"])
|
2015-03-16 20:40:59 -04:00
|
|
|
size, err := strconv.ParseInt(metadata["size"], 10, 64)
|
|
|
|
objectMetadata := storage.ObjectMetadata{
|
|
|
|
Bucket: bucket,
|
|
|
|
Key: key,
|
|
|
|
ContentType: metadata["contentType"],
|
|
|
|
Created: created,
|
2015-03-16 21:03:03 -04:00
|
|
|
Md5: metadata["md5"],
|
2015-03-16 20:40:59 -04:00
|
|
|
Size: size,
|
|
|
|
}
|
|
|
|
return objectMetadata, nil
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjects lists objects
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) ListObjects(bucket string, resources storage.BucketResourcesMetadata) ([]storage.ObjectMetadata, storage.BucketResourcesMetadata, error) {
|
|
|
|
objects, err := donutStorage.donutBox.ListObjectsInBucket(bucket, resources.Prefix)
|
2015-03-16 23:41:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, storage.BucketResourcesMetadata{}, err
|
|
|
|
}
|
|
|
|
var results []storage.ObjectMetadata
|
|
|
|
sort.Strings(objects)
|
|
|
|
for _, object := range withoutDelimiter(objects, resources.Prefix, resources.Delimiter) {
|
|
|
|
if len(results) < resources.Maxkeys {
|
2015-03-18 15:25:37 -04:00
|
|
|
objectMetadata, err := donutStorage.GetObjectMetadata(bucket, object, "")
|
2015-03-16 23:41:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, storage.BucketResourcesMetadata{}, err
|
|
|
|
}
|
|
|
|
results = append(results, objectMetadata)
|
|
|
|
} else {
|
|
|
|
resources.IsTruncated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if resources.Delimiter != "" {
|
|
|
|
objects = trimPrefixWithDelimiter(objects, resources.Prefix, resources.Delimiter)
|
|
|
|
objects = beforeDelimiter(objects, resources.Delimiter)
|
|
|
|
resources.CommonPrefixes = objects
|
|
|
|
}
|
|
|
|
return results, resources, nil
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:41:16 -04:00
|
|
|
func appendUniq(slice []string, i string) []string {
|
|
|
|
for _, ele := range slice {
|
|
|
|
if ele == i {
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(slice, i)
|
|
|
|
}
|
|
|
|
|
2015-03-16 23:41:00 -04:00
|
|
|
func withoutDelimiter(inputs []string, prefix, delim string) (results []string) {
|
|
|
|
if delim == "" {
|
|
|
|
return inputs
|
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
|
|
|
input = strings.TrimPrefix(input, prefix)
|
|
|
|
if !strings.Contains(input, delim) {
|
2015-03-17 02:41:16 -04:00
|
|
|
results = appendUniq(results, prefix+input)
|
2015-03-16 23:41:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimPrefixWithDelimiter(inputs []string, prefix, delim string) (results []string) {
|
|
|
|
for _, input := range inputs {
|
|
|
|
input = strings.TrimPrefix(input, prefix)
|
|
|
|
if strings.Contains(input, delim) {
|
2015-03-17 02:41:16 -04:00
|
|
|
results = appendUniq(results, input)
|
2015-03-16 23:41:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func beforeDelimiter(inputs []string, delim string) (results []string) {
|
|
|
|
for _, input := range inputs {
|
2015-03-17 02:41:16 -04:00
|
|
|
results = appendUniq(results, strings.Split(input, delim)[0]+delim)
|
2015-03-16 23:41:00 -04:00
|
|
|
}
|
|
|
|
return results
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateObject creates a new object
|
2015-03-18 15:25:37 -04:00
|
|
|
func (donutStorage DonutDriver) CreateObject(bucketKey, objectKey, contentType, md5sum string, reader io.Reader) error {
|
2015-03-17 03:29:19 -04:00
|
|
|
// set defaults
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
2015-03-15 20:30:42 -04:00
|
|
|
// split stream
|
|
|
|
splitStream := split.Stream(reader, uint64(blockSize))
|
|
|
|
writers := make([]*donutbox.NewObject, 16)
|
|
|
|
for i := 0; i < 16; i++ {
|
2015-03-18 15:25:37 -04:00
|
|
|
newWriter, err := donutStorage.donutBox.GetObjectWriter(bucketKey, objectKey, uint(i))
|
2015-03-15 20:30:42 -04:00
|
|
|
if err != nil {
|
|
|
|
closeAllWritersWithError(writers, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writers[i] = newWriter
|
|
|
|
}
|
|
|
|
totalLength := uint64(0)
|
2015-03-15 22:02:00 -04:00
|
|
|
chunkCount := 0
|
2015-03-16 23:41:00 -04:00
|
|
|
hasher := md5.New()
|
2015-03-15 20:30:42 -04:00
|
|
|
for chunk := range splitStream {
|
|
|
|
params, err := erasure.ParseEncoderParams(8, 8, erasure.Cauchy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-16 23:41:00 -04:00
|
|
|
hasher.Write(chunk.Data)
|
2015-03-15 20:30:42 -04:00
|
|
|
totalLength = totalLength + uint64(len(chunk.Data))
|
2015-03-15 22:02:00 -04:00
|
|
|
chunkCount = chunkCount + 1
|
2015-03-15 20:30:42 -04:00
|
|
|
encoder := erasure.NewEncoder(params)
|
|
|
|
if chunk.Err == nil {
|
|
|
|
parts, _ := encoder.Encode(chunk.Data)
|
|
|
|
for index, part := range parts {
|
|
|
|
if _, err := writers[index].Write(part); err != nil {
|
|
|
|
closeAllWritersWithError(writers, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
closeAllWritersWithError(writers, chunk.Err)
|
|
|
|
return chunk.Err
|
|
|
|
}
|
|
|
|
// encode data
|
|
|
|
// write
|
|
|
|
}
|
|
|
|
// close connections
|
|
|
|
|
2015-03-16 20:40:59 -04:00
|
|
|
metadataObj := storage.ObjectMetadata{
|
|
|
|
Bucket: bucketKey,
|
|
|
|
Key: objectKey,
|
|
|
|
|
2015-03-17 03:47:15 -04:00
|
|
|
ContentType: contentType,
|
2015-03-16 20:40:59 -04:00
|
|
|
Created: time.Now(),
|
2015-03-16 23:41:00 -04:00
|
|
|
Md5: hex.EncodeToString(hasher.Sum(nil)),
|
2015-03-16 20:40:59 -04:00
|
|
|
Size: int64(totalLength),
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:41:16 -04:00
|
|
|
metadata := createObjectMetadata(metadataObj, blockSize, 8, 8, "Cauchy")
|
2015-03-16 20:40:59 -04:00
|
|
|
|
2015-03-15 20:30:42 -04:00
|
|
|
for column := uint(0); column < 16; column++ {
|
|
|
|
writers[column].SetMetadata(metadata)
|
|
|
|
}
|
|
|
|
|
2015-03-15 22:02:00 -04:00
|
|
|
// TODO capture errors in writers, enough should pass before returning
|
|
|
|
closeAllWriters(writers)
|
|
|
|
|
2015-03-15 20:30:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeAllWriters(writers []*donutbox.NewObject) {
|
|
|
|
for _, writer := range writers {
|
|
|
|
if writer != nil {
|
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeAllWritersWithError(writers []*donutbox.NewObject, err error) {
|
|
|
|
for _, writer := range writers {
|
|
|
|
if writer != nil {
|
|
|
|
writer.CloseWithError(err)
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
2015-03-16 20:40:59 -04:00
|
|
|
|
2015-03-17 02:41:16 -04:00
|
|
|
func createBucketMetadata(metadataBucket storage.BucketMetadata) map[string]string {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
metadata["bucket"] = metadataBucket.Name
|
2015-03-17 03:45:50 -04:00
|
|
|
metadata["created"] = metadataBucket.Created.Format(time.RFC3339Nano)
|
2015-03-17 02:41:16 -04:00
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
func createObjectMetadata(metadataObject storage.ObjectMetadata, blockSize int, k, m uint8, technique string) map[string]string {
|
2015-03-16 20:40:59 -04:00
|
|
|
metadata := make(map[string]string)
|
|
|
|
metadata["bucket"] = metadataObject.Bucket
|
|
|
|
metadata["key"] = metadataObject.Key
|
|
|
|
metadata["contentType"] = metadataObject.ContentType
|
2015-03-17 03:45:50 -04:00
|
|
|
metadata["created"] = metadataObject.Created.Format(time.RFC3339Nano)
|
2015-03-16 21:03:03 -04:00
|
|
|
metadata["md5"] = metadataObject.Md5
|
2015-03-16 20:40:59 -04:00
|
|
|
metadata["size"] = strconv.FormatInt(metadataObject.Size, 10)
|
|
|
|
metadata["blockSize"] = strconv.FormatUint(uint64(blockSize), 10)
|
|
|
|
metadata["erasureK"] = strconv.FormatUint(uint64(k), 10)
|
|
|
|
metadata["erasureM"] = strconv.FormatUint(uint64(m), 10)
|
|
|
|
metadata["erasureTechnique"] = technique
|
|
|
|
return metadata
|
|
|
|
}
|