2015-03-18 17:27:10 -04:00
|
|
|
package donut
|
|
|
|
|
2015-03-23 23:06:15 -04:00
|
|
|
import (
|
2015-03-23 23:40:21 -04:00
|
|
|
"errors"
|
2015-03-23 23:06:15 -04:00
|
|
|
"io"
|
2015-03-23 23:40:21 -04:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-03-25 02:54:43 -04:00
|
|
|
|
|
|
|
"github.com/minio-io/iodine"
|
2015-03-23 23:06:15 -04:00
|
|
|
)
|
2015-03-18 17:27:10 -04:00
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
type donut struct {
|
|
|
|
buckets map[string]Bucket
|
|
|
|
nodes map[string]Node
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:04:08 -04:00
|
|
|
// NewDonut - instantiate new donut driver
|
2015-03-25 22:12:39 -04:00
|
|
|
func NewDonut(root string) (Donut, error) {
|
2015-03-23 23:40:21 -04:00
|
|
|
nodes := make(map[string]Node)
|
2015-03-25 00:04:08 -04:00
|
|
|
nodes["localhost"] = &localDirectoryNode{root: root}
|
|
|
|
driver := &donut{
|
2015-03-23 23:40:21 -04:00
|
|
|
buckets: make(map[string]Bucket),
|
|
|
|
nodes: nodes,
|
|
|
|
}
|
2015-03-25 00:04:08 -04:00
|
|
|
for nodeID, node := range nodes {
|
|
|
|
bucketIDs, err := node.GetBuckets()
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, map[string]string{"root": root})
|
2015-03-25 00:04:08 -04:00
|
|
|
}
|
|
|
|
for _, bucketID := range bucketIDs {
|
|
|
|
tokens := strings.Split(bucketID, ":")
|
2015-03-25 03:17:38 -04:00
|
|
|
if _, ok := driver.buckets[tokens[0]]; !ok {
|
2015-03-25 00:04:08 -04:00
|
|
|
bucket := donutBucket{
|
|
|
|
nodes: make([]string, 16),
|
|
|
|
}
|
|
|
|
// TODO catch errors
|
|
|
|
driver.buckets[tokens[0]] = bucket
|
|
|
|
}
|
|
|
|
if err = driver.buckets[tokens[0]].AddNode(nodeID, bucketID); err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, map[string]string{"root": root})
|
2015-03-25 00:04:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return driver, nil
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
2015-03-18 17:27:10 -04:00
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// CreateBucket - create a new bucket
|
|
|
|
func (d donut) CreateBucket(bucketName string) error {
|
|
|
|
if _, ok := d.buckets[bucketName]; ok == false {
|
|
|
|
bucketName = strings.TrimSpace(bucketName)
|
|
|
|
if bucketName == "" {
|
2015-03-26 18:55:06 -04:00
|
|
|
return iodine.New(errors.New("Cannot create bucket with no name"), map[string]string{"bucket": bucketName})
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
// assign nodes
|
|
|
|
// TODO assign other nodes
|
|
|
|
nodes := make([]string, 16)
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
nodes[i] = "localhost"
|
|
|
|
if node, ok := d.nodes["localhost"]; ok {
|
2015-03-26 12:56:45 -04:00
|
|
|
err := node.CreateBucket(bucketName + ":0:" + strconv.Itoa(i))
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return iodine.New(err, map[string]string{"node": nodes[i], "bucket": bucketName})
|
2015-03-26 12:56:45 -04:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bucket := donutBucket{
|
|
|
|
nodes: nodes,
|
|
|
|
}
|
|
|
|
d.buckets[bucketName] = bucket
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-26 18:55:06 -04:00
|
|
|
return iodine.New(errors.New("Bucket exists"), map[string]string{"bucket": bucketName})
|
2015-03-18 17:27:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// ListBuckets - list all buckets
|
|
|
|
func (d donut) ListBuckets() ([]string, error) {
|
|
|
|
var buckets []string
|
|
|
|
for bucket := range d.buckets {
|
|
|
|
buckets = append(buckets, bucket)
|
|
|
|
}
|
|
|
|
sort.Strings(buckets)
|
|
|
|
return buckets, nil
|
2015-03-18 17:27:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// GetObjectWriter - get a new writer interface for a new object
|
|
|
|
func (d donut) GetObjectWriter(bucketName, objectName string) (ObjectWriter, error) {
|
|
|
|
if bucket, ok := d.buckets[bucketName]; ok == true {
|
|
|
|
writers := make([]Writer, 16)
|
|
|
|
nodes, err := bucket.GetNodes()
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, map[string]string{"bucket": bucketName, "object": objectName})
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
for i, nodeID := range nodes {
|
|
|
|
if node, ok := d.nodes[nodeID]; ok == true {
|
2015-03-26 12:56:45 -04:00
|
|
|
bucketID := bucketName + ":0:" + strconv.Itoa(i)
|
|
|
|
writer, err := node.GetWriter(bucketID, objectName)
|
2015-03-23 23:40:21 -04:00
|
|
|
if err != nil {
|
|
|
|
for _, writerToClose := range writers {
|
|
|
|
if writerToClose != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
writerToClose.CloseWithError(iodine.New(err, nil))
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, map[string]string{"bucketid": bucketID})
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
writers[i] = writer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newErasureWriter(writers), nil
|
|
|
|
}
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(errors.New("Bucket not found"), map[string]string{"bucket": bucketName})
|
2015-03-18 17:27:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// GetObjectReader - get a new reader interface for a new object
|
|
|
|
func (d donut) GetObjectReader(bucketName, objectName string) (io.ReadCloser, error) {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams := map[string]string{"bucket": bucketName, "object": objectName}
|
2015-03-23 23:40:21 -04:00
|
|
|
r, w := io.Pipe()
|
|
|
|
if bucket, ok := d.buckets[bucketName]; ok == true {
|
|
|
|
readers := make([]io.ReadCloser, 16)
|
|
|
|
nodes, err := bucket.GetNodes()
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
var metadata map[string]string
|
|
|
|
for i, nodeID := range nodes {
|
|
|
|
if node, ok := d.nodes[nodeID]; ok == true {
|
|
|
|
bucketID := bucketName + ":0:" + strconv.Itoa(i)
|
|
|
|
reader, err := node.GetReader(bucketID, objectName)
|
|
|
|
if err != nil {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams["node"] = nodeID
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
readers[i] = reader
|
|
|
|
if metadata == nil {
|
|
|
|
metadata, err = node.GetDonutMetadata(bucketID, objectName)
|
|
|
|
if err != nil {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams["node"] = nodeID
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go erasureReader(readers, metadata, w)
|
|
|
|
return r, nil
|
|
|
|
}
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(errors.New("Bucket not found"), errParams)
|
2015-03-18 17:27:10 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// GetObjectMetadata returns metadata for a given object in a bucket
|
|
|
|
func (d donut) GetObjectMetadata(bucketName, object string) (map[string]string, error) {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams := map[string]string{"bucket": bucketName, "object": object}
|
2015-03-23 23:40:21 -04:00
|
|
|
if bucket, ok := d.buckets[bucketName]; ok {
|
|
|
|
nodes, err := bucket.GetNodes()
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
if node, ok := d.nodes[nodes[0]]; ok {
|
|
|
|
bucketID := bucketName + ":0:0"
|
|
|
|
metadata, err := node.GetMetadata(bucketID, object)
|
|
|
|
if err != nil {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams["bucketID"] = bucketID
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
donutMetadata, err := node.GetDonutMetadata(bucketID, object)
|
|
|
|
if err != nil {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams["bucketID"] = bucketID
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
metadata["sys.created"] = donutMetadata["created"]
|
|
|
|
metadata["sys.md5"] = donutMetadata["md5"]
|
|
|
|
metadata["sys.size"] = donutMetadata["size"]
|
|
|
|
return metadata, nil
|
|
|
|
}
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams["node"] = nodes[0]
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(errors.New("Cannot connect to node: "+nodes[0]), errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
return nil, errors.New("Bucket not found")
|
|
|
|
}
|
2015-03-18 17:27:10 -04:00
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// ListObjects - list all the available objects in a bucket
|
|
|
|
func (d donut) ListObjects(bucketName string) ([]string, error) {
|
2015-03-26 12:56:45 -04:00
|
|
|
errParams := map[string]string{"bucket": bucketName}
|
2015-03-23 23:40:21 -04:00
|
|
|
if bucket, ok := d.buckets[bucketName]; ok {
|
|
|
|
nodes, err := bucket.GetNodes()
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
if node, ok := d.nodes[nodes[0]]; ok {
|
2015-03-26 12:56:45 -04:00
|
|
|
bucketID := bucketName + ":0:0"
|
|
|
|
objects, err := node.ListObjects(bucketID)
|
|
|
|
errParams["bucketID"] = bucketID
|
2015-03-26 18:55:06 -04:00
|
|
|
return objects, iodine.New(err, errParams)
|
2015-03-23 23:40:21 -04:00
|
|
|
}
|
|
|
|
}
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(errors.New("Bucket not found"), errParams)
|
2015-03-18 17:27:10 -04:00
|
|
|
}
|