2015-04-08 20:13:25 -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.
|
|
|
|
*/
|
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
package donut
|
2015-04-08 20:13:25 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
2015-04-24 18:19:29 -04:00
|
|
|
"encoding/base64"
|
2015-04-08 20:13:25 -04:00
|
|
|
"encoding/hex"
|
2015-04-24 18:19:29 -04:00
|
|
|
"errors"
|
2015-04-30 00:21:38 -04:00
|
|
|
"io"
|
2015-06-30 20:08:18 -04:00
|
|
|
"io/ioutil"
|
2015-05-04 02:16:45 -04:00
|
|
|
"log"
|
2015-04-30 00:21:38 -04:00
|
|
|
"runtime/debug"
|
|
|
|
"sort"
|
2015-05-04 18:26:56 -04:00
|
|
|
"strconv"
|
2015-04-30 00:21:38 -04:00
|
|
|
"strings"
|
2015-06-30 20:08:18 -04:00
|
|
|
"sync"
|
2015-04-30 00:21:38 -04:00
|
|
|
"time"
|
2015-04-22 17:44:59 -04:00
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
"github.com/minio/minio/pkg/donut/cache/data"
|
|
|
|
"github.com/minio/minio/pkg/donut/cache/metadata"
|
2015-05-11 19:23:10 -04:00
|
|
|
"github.com/minio/minio/pkg/iodine"
|
2015-07-02 23:31:22 -04:00
|
|
|
"github.com/minio/minio/pkg/quick"
|
2015-04-08 20:13:25 -04:00
|
|
|
)
|
|
|
|
|
2015-06-29 23:48:23 -04:00
|
|
|
// total Number of buckets allowed
|
2015-04-27 00:15:43 -04:00
|
|
|
const (
|
|
|
|
totalBuckets = 100
|
|
|
|
)
|
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
// Config donut config
|
|
|
|
type Config struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
MaxSize uint64 `json:"max-size"`
|
|
|
|
DonutName string `json:"donut-name"`
|
|
|
|
NodeDiskMap map[string][]string `json:"node-disk-map"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// API - local variables
|
|
|
|
type API struct {
|
|
|
|
config *Config
|
2015-07-03 20:11:26 -04:00
|
|
|
lock *sync.Mutex
|
2015-07-04 00:02:31 -04:00
|
|
|
objects *data.Cache
|
|
|
|
multiPartObjects *data.Cache
|
|
|
|
storedBuckets *metadata.Cache
|
2015-07-02 23:31:22 -04:00
|
|
|
nodes map[string]node
|
|
|
|
buckets map[string]bucket
|
2015-06-30 20:08:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// storedBucket saved bucket
|
|
|
|
type storedBucket struct {
|
|
|
|
bucketMetadata BucketMetadata
|
|
|
|
objectMetadata map[string]ObjectMetadata
|
2015-07-05 21:44:55 -04:00
|
|
|
partMetadata map[int]PartMetadata
|
2015-07-04 00:02:31 -04:00
|
|
|
multiPartSession map[string]MultiPartSession
|
2015-06-30 20:08:18 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
// New instantiate a new donut
|
2015-07-06 01:17:56 -04:00
|
|
|
func New() (Interface, error) {
|
|
|
|
var conf *Config
|
|
|
|
var err error
|
|
|
|
conf, err = LoadConfig()
|
|
|
|
if err != nil {
|
|
|
|
conf = &Config{
|
|
|
|
Version: "0.0.1",
|
|
|
|
MaxSize: 512000000,
|
|
|
|
NodeDiskMap: nil,
|
|
|
|
DonutName: "",
|
|
|
|
}
|
|
|
|
if err := quick.CheckData(conf); err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
2015-07-06 01:17:56 -04:00
|
|
|
a := API{config: conf}
|
2015-07-04 00:02:31 -04:00
|
|
|
a.storedBuckets = metadata.NewCache()
|
2015-07-02 23:31:22 -04:00
|
|
|
a.nodes = make(map[string]node)
|
|
|
|
a.buckets = make(map[string]bucket)
|
2015-07-04 20:08:23 -04:00
|
|
|
a.objects = data.NewCache(a.config.MaxSize)
|
|
|
|
a.multiPartObjects = data.NewCache(0)
|
|
|
|
a.objects.OnEvicted = a.evictedObject
|
|
|
|
a.multiPartObjects.OnEvicted = a.evictedPart
|
2015-07-03 20:11:26 -04:00
|
|
|
a.lock = new(sync.Mutex)
|
2015-06-30 20:08:18 -04:00
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(a.config.NodeDiskMap) > 0 {
|
|
|
|
for k, v := range a.config.NodeDiskMap {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return nil, iodine.New(InvalidDisksArgument{}, nil)
|
|
|
|
}
|
|
|
|
err := a.AttachNode(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Initialization, populate all buckets into memory
|
|
|
|
buckets, err := a.listBuckets()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
for k, v := range buckets {
|
2015-07-06 19:18:39 -04:00
|
|
|
var newBucket = storedBucket{}
|
|
|
|
newBucket.objectMetadata = make(map[string]ObjectMetadata)
|
|
|
|
newBucket.multiPartSession = make(map[string]MultiPartSession)
|
|
|
|
newBucket.partMetadata = make(map[int]PartMetadata)
|
|
|
|
newBucket.bucketMetadata = v
|
|
|
|
a.storedBuckets.Set(k, newBucket)
|
2015-07-02 23:31:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return a, nil
|
2015-06-30 20:08:18 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// GetObject - GET object from cache buffer
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) GetObject(w io.Writer, bucket string, object string) (int64, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return 0, iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return 0, iodine.New(ObjectNameInvalid{Object: object}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return 0, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-15 15:35:23 -04:00
|
|
|
objectKey := bucket + "/" + object
|
2015-07-02 23:31:22 -04:00
|
|
|
data, ok := donut.objects.Get(objectKey)
|
2015-05-04 05:31:18 -04:00
|
|
|
if !ok {
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
reader, size, err := donut.getObject(bucket, object)
|
2015-06-30 20:08:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
2015-07-02 00:39:37 -04:00
|
|
|
// new proxy writer to capture data read from disk
|
2015-07-02 23:31:22 -04:00
|
|
|
pw := NewProxyWriter(w)
|
2015-07-02 00:39:37 -04:00
|
|
|
written, err := io.CopyN(pw, reader, size)
|
2015-06-30 20:08:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
2015-07-02 00:39:37 -04:00
|
|
|
/// cache object read from disk
|
2015-07-03 20:11:26 -04:00
|
|
|
ok := donut.objects.Set(objectKey, pw.writtenBytes)
|
|
|
|
pw.writtenBytes = nil
|
|
|
|
go debug.FreeOSMemory()
|
|
|
|
if !ok {
|
|
|
|
return 0, iodine.New(InternalError{}, nil)
|
2015-07-02 00:39:37 -04:00
|
|
|
}
|
2015-06-30 20:08:18 -04:00
|
|
|
return written, nil
|
2015-06-29 23:48:23 -04:00
|
|
|
}
|
2015-06-30 20:08:18 -04:00
|
|
|
return 0, iodine.New(ObjectNotFound{Object: object}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
written, err := io.CopyN(w, bytes.NewBuffer(data), int64(donut.objects.Len(objectKey)))
|
2015-06-29 23:48:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
return written, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// GetPartialObject - GET object from cache buffer range
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-05-04 18:26:56 -04:00
|
|
|
errParams := map[string]string{
|
|
|
|
"bucket": bucket,
|
|
|
|
"object": object,
|
|
|
|
"start": strconv.FormatInt(start, 10),
|
|
|
|
"length": strconv.FormatInt(length, 10),
|
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return 0, iodine.New(BucketNameInvalid{Bucket: bucket}, errParams)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return 0, iodine.New(ObjectNameInvalid{Object: object}, errParams)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-05-04 18:26:56 -04:00
|
|
|
if start < 0 {
|
2015-06-30 19:30:02 -04:00
|
|
|
return 0, iodine.New(InvalidRange{
|
2015-05-04 18:26:56 -04:00
|
|
|
Start: start,
|
|
|
|
Length: length,
|
|
|
|
}, errParams)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-04 18:26:56 -04:00
|
|
|
objectKey := bucket + "/" + object
|
2015-07-02 23:31:22 -04:00
|
|
|
data, ok := donut.objects.Get(objectKey)
|
2015-05-04 18:26:56 -04:00
|
|
|
if !ok {
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
reader, _, err := donut.getObject(bucket, object)
|
2015-06-30 20:08:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
if _, err := io.CopyN(ioutil.Discard, reader, start); err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
pw := NewProxyWriter(w)
|
2015-06-30 20:08:18 -04:00
|
|
|
written, err := io.CopyN(w, reader, length)
|
|
|
|
if err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
2015-07-03 20:11:26 -04:00
|
|
|
ok := donut.objects.Set(objectKey, pw.writtenBytes)
|
|
|
|
pw.writtenBytes = nil
|
|
|
|
go debug.FreeOSMemory()
|
|
|
|
if !ok {
|
|
|
|
return 0, iodine.New(InternalError{}, nil)
|
2015-07-02 00:39:37 -04:00
|
|
|
}
|
2015-06-30 20:08:18 -04:00
|
|
|
return written, nil
|
2015-06-29 23:48:23 -04:00
|
|
|
}
|
2015-06-30 20:08:18 -04:00
|
|
|
return 0, iodine.New(ObjectNotFound{Object: object}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-31 21:38:34 -04:00
|
|
|
written, err := io.CopyN(w, bytes.NewBuffer(data[start:]), length)
|
2015-06-29 23:48:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
return written, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketMetadata -
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) GetBucketMetadata(bucket string) (BucketMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return BucketMetadata{}, iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
bucketMetadata, err := donut.getBucketMetadata(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return BucketMetadata{}, iodine.New(err, nil)
|
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-07-03 20:11:26 -04:00
|
|
|
storedBucket.bucketMetadata = bucketMetadata
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-06-29 23:48:23 -04:00
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
return BucketMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
return donut.storedBuckets.Get(bucket).(storedBucket).bucketMetadata, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 06:04:29 -04:00
|
|
|
// SetBucketMetadata -
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) SetBucketMetadata(bucket string, metadata map[string]string) error {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-27 06:04:29 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-27 06:04:29 -04:00
|
|
|
}
|
2015-07-03 20:11:26 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
if err := donut.setBucketMetadata(bucket, metadata); err != nil {
|
|
|
|
return iodine.New(err, nil)
|
2015-06-29 23:48:23 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-07-03 20:11:26 -04:00
|
|
|
storedBucket.bucketMetadata.ACL = BucketACL(metadata["acl"])
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-04-27 06:04:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:19:29 -04:00
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, success its `nil`
|
|
|
|
func isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) error {
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" && strings.TrimSpace(actualMD5Sum) != "" {
|
|
|
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
|
|
|
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) {
|
|
|
|
return iodine.New(errors.New("bad digest, md5sum mismatch"), nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return iodine.New(errors.New("invalid argument"), nil)
|
|
|
|
}
|
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// CreateObject - create an object
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) CreateObject(bucket, key, expectedMD5Sum string, size int64, data io.Reader, metadata map[string]string) (ObjectMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
2015-07-06 01:17:56 -04:00
|
|
|
defer donut.lock.Unlock()
|
|
|
|
|
|
|
|
contentType := metadata["contentType"]
|
2015-07-02 23:31:22 -04:00
|
|
|
objectMetadata, err := donut.createObject(bucket, key, contentType, expectedMD5Sum, size, data)
|
2015-05-04 22:04:56 -04:00
|
|
|
// free
|
|
|
|
debug.FreeOSMemory()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
return objectMetadata, iodine.New(err, nil)
|
2015-05-04 22:04:56 -04:00
|
|
|
}
|
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// createObject - PUT object to cache buffer
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) createObject(bucket, key, contentType, expectedMD5Sum string, size int64, data io.Reader) (ObjectMetadata, error) {
|
2015-07-06 01:17:56 -04:00
|
|
|
if size > int64(donut.config.MaxSize) {
|
|
|
|
generic := GenericObjectError{Bucket: bucket, Object: key}
|
|
|
|
return ObjectMetadata{}, iodine.New(EntityTooLarge{
|
|
|
|
GenericObjectError: generic,
|
|
|
|
Size: strconv.FormatInt(size, 10),
|
|
|
|
MaxSize: strconv.FormatUint(donut.config.MaxSize, 10),
|
|
|
|
}, nil)
|
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidObjectName(key) {
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(ObjectNameInvalid{Object: key}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-04-30 02:36:30 -04:00
|
|
|
// get object key
|
2015-04-15 15:35:23 -04:00
|
|
|
objectKey := bucket + "/" + key
|
2015-04-30 02:36:30 -04:00
|
|
|
if _, ok := storedBucket.objectMetadata[objectKey]; ok == true {
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(ObjectExists{Object: key}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
2015-04-24 18:19:29 -04:00
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
|
|
|
expectedMD5SumBytes, err := base64.StdEncoding.DecodeString(strings.TrimSpace(expectedMD5Sum))
|
|
|
|
if err != nil {
|
|
|
|
// pro-actively close the connection
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(InvalidDigest{Md5: expectedMD5Sum}, nil)
|
2015-04-24 18:19:29 -04:00
|
|
|
}
|
|
|
|
expectedMD5Sum = hex.EncodeToString(expectedMD5SumBytes)
|
|
|
|
}
|
2015-05-14 02:05:48 -04:00
|
|
|
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
objMetadata, err := donut.putObject(bucket, key, expectedMD5Sum, data, map[string]string{"contentType": contentType})
|
2015-07-02 00:39:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
storedBucket.objectMetadata[objectKey] = objMetadata
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-07-02 00:39:37 -04:00
|
|
|
return objMetadata, nil
|
|
|
|
}
|
2015-05-14 02:05:48 -04:00
|
|
|
// calculate md5
|
|
|
|
hash := md5.New()
|
|
|
|
|
|
|
|
var err error
|
2015-07-02 00:39:37 -04:00
|
|
|
var totalLength int
|
2015-05-14 02:05:48 -04:00
|
|
|
for err == nil {
|
2015-07-02 00:39:37 -04:00
|
|
|
var length int
|
2015-05-14 02:05:48 -04:00
|
|
|
byteBuffer := make([]byte, 1024*1024)
|
|
|
|
length, err = data.Read(byteBuffer)
|
|
|
|
// While hash.Write() wouldn't mind a Nil byteBuffer
|
|
|
|
// It is necessary for us to verify this and break
|
|
|
|
if length == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
hash.Write(byteBuffer[0:length])
|
2015-07-02 23:31:22 -04:00
|
|
|
ok := donut.objects.Append(objectKey, byteBuffer[0:length])
|
2015-07-02 00:39:37 -04:00
|
|
|
if !ok {
|
|
|
|
return ObjectMetadata{}, iodine.New(InternalError{}, nil)
|
|
|
|
}
|
|
|
|
totalLength += length
|
|
|
|
go debug.FreeOSMemory()
|
2015-05-14 02:05:48 -04:00
|
|
|
}
|
|
|
|
if err != io.EOF {
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
|
2015-07-02 00:39:37 -04:00
|
|
|
md5SumBytes := hash.Sum(nil)
|
2015-04-24 18:19:29 -04:00
|
|
|
md5Sum := hex.EncodeToString(md5SumBytes)
|
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" {
|
|
|
|
if err := isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), md5Sum); err != nil {
|
2015-07-01 21:09:44 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BadDigest{}, nil)
|
2015-04-24 18:19:29 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
|
2015-06-30 20:08:18 -04:00
|
|
|
m := make(map[string]string)
|
|
|
|
m["contentType"] = contentType
|
2015-06-30 19:30:02 -04:00
|
|
|
newObject := ObjectMetadata{
|
2015-04-24 18:19:29 -04:00
|
|
|
Bucket: bucket,
|
2015-06-30 20:08:18 -04:00
|
|
|
Object: key,
|
2015-04-24 18:19:29 -04:00
|
|
|
|
2015-06-30 20:08:18 -04:00
|
|
|
Metadata: m,
|
|
|
|
Created: time.Now().UTC(),
|
|
|
|
MD5Sum: md5Sum,
|
|
|
|
Size: int64(totalLength),
|
2015-04-24 18:19:29 -04:00
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
|
2015-05-18 16:14:01 -04:00
|
|
|
storedBucket.objectMetadata[objectKey] = newObject
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-07-01 21:09:44 -04:00
|
|
|
return newObject, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 21:09:44 -04:00
|
|
|
// MakeBucket - create bucket in cache
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) MakeBucket(bucketName, acl string) error {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-07-04 00:02:31 -04:00
|
|
|
if donut.storedBuckets.Stats().Items == totalBuckets {
|
2015-06-30 19:30:02 -04:00
|
|
|
return iodine.New(TooManyBuckets{Bucket: bucketName}, nil)
|
2015-04-27 00:15:43 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucketName) {
|
|
|
|
return iodine.New(BucketNameInvalid{Bucket: bucketName}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucketACL(acl) {
|
|
|
|
return iodine.New(InvalidACL{ACL: acl}, nil)
|
2015-04-22 21:19:53 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if donut.storedBuckets.Exists(bucketName) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return iodine.New(BucketExists{Bucket: bucketName}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 05:04:05 -04:00
|
|
|
if strings.TrimSpace(acl) == "" {
|
|
|
|
// default is private
|
|
|
|
acl = "private"
|
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
if err := donut.makeBucket(bucketName, BucketACL(acl)); err != nil {
|
2015-06-29 23:48:23 -04:00
|
|
|
return iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 20:13:25 -04:00
|
|
|
var newBucket = storedBucket{}
|
2015-06-30 19:30:02 -04:00
|
|
|
newBucket.objectMetadata = make(map[string]ObjectMetadata)
|
2015-07-04 00:02:31 -04:00
|
|
|
newBucket.multiPartSession = make(map[string]MultiPartSession)
|
2015-07-05 21:44:55 -04:00
|
|
|
newBucket.partMetadata = make(map[int]PartMetadata)
|
2015-06-30 19:30:02 -04:00
|
|
|
newBucket.bucketMetadata = BucketMetadata{}
|
2015-04-30 02:36:30 -04:00
|
|
|
newBucket.bucketMetadata.Name = bucketName
|
2015-04-30 22:26:23 -04:00
|
|
|
newBucket.bucketMetadata.Created = time.Now().UTC()
|
2015-06-30 19:30:02 -04:00
|
|
|
newBucket.bucketMetadata.ACL = BucketACL(acl)
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucketName, newBucket)
|
2015-04-08 20:13:25 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// ListObjects - list objects from cache
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) ListObjects(bucket string, resources BucketResourcesMetadata) ([]ObjectMetadata, BucketResourcesMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return nil, BucketResourcesMetadata{IsTruncated: false}, iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-07-01 21:09:44 -04:00
|
|
|
if !IsValidPrefix(resources.Prefix) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return nil, BucketResourcesMetadata{IsTruncated: false}, iodine.New(ObjectNameInvalid{Object: resources.Prefix}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return nil, BucketResourcesMetadata{IsTruncated: false}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
var results []ObjectMetadata
|
2015-04-08 20:13:25 -04:00
|
|
|
var keys []string
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
listObjects, err := donut.listObjects(
|
2015-07-02 01:36:33 -04:00
|
|
|
bucket,
|
|
|
|
resources.Prefix,
|
|
|
|
resources.Marker,
|
|
|
|
resources.Delimiter,
|
|
|
|
resources.Maxkeys,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, BucketResourcesMetadata{IsTruncated: false}, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
resources.CommonPrefixes = listObjects.CommonPrefixes
|
|
|
|
resources.IsTruncated = listObjects.IsTruncated
|
2015-07-04 00:02:31 -04:00
|
|
|
if resources.IsTruncated && resources.Delimiter != "" {
|
2015-07-02 01:36:33 -04:00
|
|
|
resources.NextMarker = results[len(results)-1].Object
|
|
|
|
}
|
|
|
|
for key := range listObjects.Objects {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, key := range keys {
|
|
|
|
results = append(results, listObjects.Objects[key])
|
|
|
|
}
|
|
|
|
return results, resources, nil
|
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-04-30 02:36:30 -04:00
|
|
|
for key := range storedBucket.objectMetadata {
|
2015-04-15 15:35:23 -04:00
|
|
|
if strings.HasPrefix(key, bucket+"/") {
|
|
|
|
key = key[len(bucket)+1:]
|
2015-07-02 01:36:33 -04:00
|
|
|
if strings.HasPrefix(key, resources.Prefix) {
|
|
|
|
if key > resources.Marker {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
2015-05-05 17:27:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 01:36:33 -04:00
|
|
|
if strings.TrimSpace(resources.Prefix) != "" {
|
|
|
|
keys = TrimPrefix(keys, resources.Prefix)
|
|
|
|
}
|
|
|
|
var prefixes []string
|
|
|
|
var filteredKeys []string
|
|
|
|
if strings.TrimSpace(resources.Delimiter) != "" {
|
|
|
|
filteredKeys = HasNoDelimiter(keys, resources.Delimiter)
|
|
|
|
prefixes = HasDelimiter(keys, resources.Delimiter)
|
|
|
|
prefixes = SplitDelimiter(prefixes, resources.Delimiter)
|
|
|
|
prefixes = SortU(prefixes)
|
|
|
|
} else {
|
|
|
|
filteredKeys = keys
|
|
|
|
}
|
|
|
|
for _, commonPrefix := range prefixes {
|
|
|
|
resources.CommonPrefixes = append(resources.CommonPrefixes, resources.Prefix+commonPrefix)
|
|
|
|
}
|
|
|
|
filteredKeys = RemoveDuplicates(filteredKeys)
|
|
|
|
sort.Strings(filteredKeys)
|
|
|
|
|
|
|
|
for _, key := range filteredKeys {
|
2015-04-08 20:13:25 -04:00
|
|
|
if len(results) == resources.Maxkeys {
|
2015-04-29 19:45:57 -04:00
|
|
|
resources.IsTruncated = true
|
2015-07-04 00:02:31 -04:00
|
|
|
if resources.IsTruncated && resources.Delimiter != "" {
|
2015-06-30 20:08:18 -04:00
|
|
|
resources.NextMarker = results[len(results)-1].Object
|
2015-05-05 17:27:18 -04:00
|
|
|
}
|
2015-04-29 19:45:57 -04:00
|
|
|
return results, resources, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-07-02 01:36:33 -04:00
|
|
|
object := storedBucket.objectMetadata[bucket+"/"+resources.Prefix+key]
|
2015-04-30 02:36:30 -04:00
|
|
|
results = append(results, object)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-07-01 21:09:44 -04:00
|
|
|
resources.CommonPrefixes = RemoveDuplicates(resources.CommonPrefixes)
|
2015-07-02 01:36:33 -04:00
|
|
|
sort.Strings(resources.CommonPrefixes)
|
2015-04-08 20:13:25 -04:00
|
|
|
return results, resources, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// byBucketName is a type for sorting bucket metadata by bucket name
|
2015-06-30 19:30:02 -04:00
|
|
|
type byBucketName []BucketMetadata
|
2015-04-08 20:13:25 -04:00
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
func (b byBucketName) Len() int { return len(b) }
|
|
|
|
func (b byBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
func (b byBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name }
|
2015-04-08 20:13:25 -04:00
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// ListBuckets - List buckets from cache
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) ListBuckets() ([]BucketMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-06-30 19:30:02 -04:00
|
|
|
var results []BucketMetadata
|
2015-07-04 00:02:31 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
buckets, err := donut.listBuckets()
|
|
|
|
if err != nil {
|
|
|
|
return nil, iodine.New(err, nil)
|
|
|
|
}
|
|
|
|
for _, bucketMetadata := range buckets {
|
|
|
|
results = append(results, bucketMetadata)
|
|
|
|
}
|
|
|
|
sort.Sort(byBucketName(results))
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
for _, bucket := range donut.storedBuckets.GetAll() {
|
|
|
|
results = append(results, bucket.(storedBucket).bucketMetadata)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-06-29 19:02:17 -04:00
|
|
|
sort.Sort(byBucketName(results))
|
2015-04-08 20:13:25 -04:00
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2015-06-29 19:02:17 -04:00
|
|
|
// GetObjectMetadata - get object metadata from cache
|
2015-07-02 23:31:22 -04:00
|
|
|
func (donut API) GetObjectMetadata(bucket, key string) (ObjectMetadata, error) {
|
2015-07-03 20:11:26 -04:00
|
|
|
donut.lock.Lock()
|
|
|
|
defer donut.lock.Unlock()
|
2015-07-06 01:17:56 -04:00
|
|
|
|
2015-04-08 20:13:25 -04:00
|
|
|
// check if bucket exists
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidBucket(bucket) {
|
|
|
|
return ObjectMetadata{}, iodine.New(BucketNameInvalid{Bucket: bucket}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-06-30 19:30:02 -04:00
|
|
|
if !IsValidObjectName(key) {
|
|
|
|
return ObjectMetadata{}, iodine.New(ObjectNameInvalid{Object: key}, nil)
|
2015-04-23 04:20:03 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
if !donut.storedBuckets.Exists(bucket) {
|
2015-06-30 19:30:02 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(BucketNotFound{Bucket: bucket}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-07-04 00:02:31 -04:00
|
|
|
storedBucket := donut.storedBuckets.Get(bucket).(storedBucket)
|
2015-04-15 15:35:23 -04:00
|
|
|
objectKey := bucket + "/" + key
|
2015-06-29 23:48:23 -04:00
|
|
|
if objMetadata, ok := storedBucket.objectMetadata[objectKey]; ok == true {
|
|
|
|
return objMetadata, nil
|
|
|
|
}
|
2015-07-02 23:31:22 -04:00
|
|
|
if len(donut.config.NodeDiskMap) > 0 {
|
|
|
|
objMetadata, err := donut.getObjectMetadata(bucket, key)
|
2015-06-29 23:48:23 -04:00
|
|
|
if err != nil {
|
2015-06-30 19:30:02 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(err, nil)
|
2015-06-29 23:48:23 -04:00
|
|
|
}
|
|
|
|
// update
|
|
|
|
storedBucket.objectMetadata[objectKey] = objMetadata
|
2015-07-04 00:02:31 -04:00
|
|
|
donut.storedBuckets.Set(bucket, storedBucket)
|
2015-06-29 23:48:23 -04:00
|
|
|
return objMetadata, nil
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-06-30 20:08:18 -04:00
|
|
|
return ObjectMetadata{}, iodine.New(ObjectNotFound{Object: key}, nil)
|
2015-04-08 20:13:25 -04:00
|
|
|
}
|
2015-04-15 15:35:23 -04:00
|
|
|
|
2015-07-05 21:44:55 -04:00
|
|
|
// evictedObject callback function called when an item is evicted from memory
|
2015-07-04 20:08:23 -04:00
|
|
|
func (donut API) evictedObject(a ...interface{}) {
|
2015-07-02 23:31:22 -04:00
|
|
|
cacheStats := donut.objects.Stats()
|
2015-07-04 20:08:23 -04:00
|
|
|
log.Printf("CurrentSize: %d, CurrentItems: %d, TotalEvicted: %d",
|
|
|
|
cacheStats.Bytes, cacheStats.Items, cacheStats.Evicted)
|
2015-05-04 02:16:45 -04:00
|
|
|
key := a[0].(string)
|
2015-04-30 02:36:30 -04:00
|
|
|
// loop through all buckets
|
2015-07-04 00:02:31 -04:00
|
|
|
for _, bucket := range donut.storedBuckets.GetAll() {
|
|
|
|
delete(bucket.(storedBucket).objectMetadata, key)
|
2015-04-30 02:36:30 -04:00
|
|
|
}
|
2015-05-04 02:16:45 -04:00
|
|
|
debug.FreeOSMemory()
|
2015-04-15 15:35:23 -04:00
|
|
|
}
|