diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 4cc176f7..8c23629f 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -28,7 +28,7 @@ }, { "ImportPath": "github.com/minio-io/donut", - "Rev": "a2932a1269212cb6aed1851b3e95a0e2094f4a7a" + "Rev": "e31777817d0808395d6ce01fca5673929c8f7cc9" }, { "ImportPath": "github.com/minio-io/erasure", diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/bucket.go b/Godeps/_workspace/src/github.com/minio-io/donut/bucket.go index beda9ace..cc2efe40 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/bucket.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/bucket.go @@ -1,3 +1,19 @@ +/* + * 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 ( @@ -59,7 +75,7 @@ func (b bucket) ListObjects() (map[string]Object, error) { return nil, err } for _, object := range objects { - b.objects[object.Name()], err = NewObject(object.Name(), path.Join(disk.GetName(), bucketPath)) + b.objects[object.Name()], err = NewObject(object.Name(), path.Join(disk.GetPath(), bucketPath)) if err != nil { return nil, err } @@ -70,58 +86,31 @@ func (b bucket) ListObjects() (map[string]Object, error) { return b.objects, nil } -func (b bucket) GetObject(objectName string, writer *io.PipeWriter, donutObjectMetadata map[string]string) { - if objectName == "" || writer == nil || len(donutObjectMetadata) == 0 { - writer.CloseWithError(errors.New("invalid argument")) - return - } - expectedMd5sum, err := hex.DecodeString(donutObjectMetadata["md5"]) +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 { - writer.CloseWithError(err) - return + return nil, 0, err } - totalChunks, totalLeft, blockSize, k, m, err := b.metadata2Values(donutObjectMetadata) - if err != nil { - writer.CloseWithError(err) - return - } - technique, ok := donutObjectMetadata["erasureTechnique"] + // check if object exists + object, ok := objects[objectName] if !ok { - writer.CloseWithError(errors.New("missing erasure Technique")) - return + return nil, 0, errors.New("object does not exist") } - hasher := md5.New() - mwriter := io.MultiWriter(writer, hasher) - encoder, err := NewEncoder(uint8(k), uint8(m), technique) + donutObjectMetadata, err := object.GetDonutObjectMetadata() if err != nil { - writer.CloseWithError(err) - return + return nil, 0, err } - readers, err := b.getDiskReaders(objectName, "data") + if objectName == "" || writer == nil || len(donutObjectMetadata) == 0 { + return nil, 0, errors.New("invalid argument") + } + size, err = strconv.ParseInt(donutObjectMetadata["size"], 10, 64) if err != nil { - writer.CloseWithError(err) - return + return nil, 0, err } - for i := 0; i < totalChunks; i++ { - decodedData, err := b.decodeData(totalLeft, blockSize, readers, encoder, writer) - if err != nil { - writer.CloseWithError(err) - return - } - _, err = io.Copy(mwriter, bytes.NewBuffer(decodedData)) - if err != nil { - writer.CloseWithError(err) - return - } - totalLeft = totalLeft - int64(blockSize) - } - actualMd5sum := hasher.Sum(nil) - if bytes.Compare(expectedMd5sum, actualMd5sum) != 0 { - writer.CloseWithError(errors.New("checksum mismatch")) - return - } - writer.Close() - return + go b.getObject(objectName, writer, donutObjectMetadata) + return reader, size, nil } func (b bucket) WriteObjectMetadata(objectName string, objectMetadata map[string]string) error { @@ -178,37 +167,49 @@ func (b bucket) PutObject(objectName string, objectData io.Reader) error { for _, writer := range writers { defer writer.Close() } - - chunks := split.Stream(objectData, 10*1024*1024) - encoder, err := NewEncoder(8, 8, "Cauchy") - if err != nil { - return err - } - chunkCount := 0 - totalLength := 0 summer := md5.New() - for chunk := range chunks { - if chunk.Err == nil { - totalLength = totalLength + len(chunk.Data) - encodedBlocks, _ := encoder.Encode(chunk.Data) - summer.Write(chunk.Data) - for blockIndex, block := range encodedBlocks { - io.Copy(writers[blockIndex], bytes.NewBuffer(block)) - } - } - chunkCount = chunkCount + 1 - } - - dataMd5sum := summer.Sum(nil) donutObjectMetadata := make(map[string]string) - donutObjectMetadata["blockSize"] = strconv.Itoa(10 * 1024 * 1024) - donutObjectMetadata["chunkCount"] = strconv.Itoa(chunkCount) + switch len(writers) == 1 { + case true: + mw := io.MultiWriter(writers[0], summer) + totalLength, err := io.Copy(mw, objectData) + if err != nil { + return err + } + donutObjectMetadata["size"] = strconv.FormatInt(totalLength, 10) + case false: + k, m, err := b.getDataAndParity(len(writers)) + if err != nil { + return err + } + chunks := split.Stream(objectData, 10*1024*1024) + encoder, err := NewEncoder(k, m, "Cauchy") + if err != nil { + return err + } + chunkCount := 0 + totalLength := 0 + for chunk := range chunks { + if chunk.Err == nil { + totalLength = totalLength + len(chunk.Data) + encodedBlocks, _ := encoder.Encode(chunk.Data) + summer.Write(chunk.Data) + for blockIndex, block := range encodedBlocks { + io.Copy(writers[blockIndex], bytes.NewBuffer(block)) + } + } + chunkCount = chunkCount + 1 + } + donutObjectMetadata["blockSize"] = strconv.Itoa(10 * 1024 * 1024) + donutObjectMetadata["chunkCount"] = strconv.Itoa(chunkCount) + donutObjectMetadata["erasureK"] = strconv.FormatUint(uint64(k), 10) + donutObjectMetadata["erasureM"] = strconv.FormatUint(uint64(m), 10) + donutObjectMetadata["erasureTechnique"] = "Cauchy" + donutObjectMetadata["size"] = strconv.Itoa(totalLength) + } + dataMd5sum := summer.Sum(nil) donutObjectMetadata["created"] = time.Now().Format(time.RFC3339Nano) - donutObjectMetadata["erasureK"] = "8" - donutObjectMetadata["erasureM"] = "8" - donutObjectMetadata["erasureTechnique"] = "Cauchy" donutObjectMetadata["md5"] = hex.EncodeToString(dataMd5sum) - donutObjectMetadata["size"] = strconv.Itoa(totalLength) if err := b.WriteDonutObjectMetadata(objectName, donutObjectMetadata); err != nil { return err } diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/bucket_internal.go b/Godeps/_workspace/src/github.com/minio-io/donut/bucket_internal.go index e2d2cdab..39fd604f 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/bucket_internal.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/bucket_internal.go @@ -1,13 +1,107 @@ +/* + * 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" + "crypto/md5" + "encoding/hex" + "errors" "fmt" "io" "path" "strconv" ) +func (b bucket) getDataAndParity(totalWriters int) (k uint8, m uint8, err error) { + if totalWriters <= 1 { + return 0, 0, errors.New("invalid argument") + } + 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 { + return 0, 0, errors.New("parity over flow") + } + remainder := totalWriters % 2 // will be 1 for odd and 0 for even numbers + k = uint8(quotient + remainder) + m = uint8(quotient) + return k, m, nil +} + +func (b bucket) getObject(objectName string, writer *io.PipeWriter, donutObjectMetadata map[string]string) { + readers, err := b.getDiskReaders(objectName, "data") + if err != nil { + writer.CloseWithError(err) + return + } + hasher := md5.New() + mwriter := io.MultiWriter(writer, hasher) + switch len(readers) == 1 { + case false: + totalChunks, totalLeft, blockSize, k, m, err := b.metadata2Values(donutObjectMetadata) + if err != nil { + writer.CloseWithError(err) + return + } + technique, ok := donutObjectMetadata["erasureTechnique"] + if !ok { + writer.CloseWithError(errors.New("missing erasure Technique")) + return + } + encoder, err := NewEncoder(uint8(k), uint8(m), technique) + if err != nil { + writer.CloseWithError(err) + return + } + for i := 0; i < totalChunks; i++ { + decodedData, err := b.decodeData(totalLeft, blockSize, readers, encoder, writer) + if err != nil { + writer.CloseWithError(err) + return + } + _, err = io.Copy(mwriter, bytes.NewBuffer(decodedData)) + if err != nil { + writer.CloseWithError(err) + return + } + totalLeft = totalLeft - int64(blockSize) + } + case true: + _, err := io.Copy(writer, readers[0]) + if err != nil { + writer.CloseWithError(err) + return + } + } + expectedMd5sum, err := hex.DecodeString(donutObjectMetadata["md5"]) + if err != nil { + writer.CloseWithError(err) + return + } + + actualMd5sum := hasher.Sum(nil) + if bytes.Compare(expectedMd5sum, actualMd5sum) != 0 { + writer.CloseWithError(errors.New("checksum mismatch")) + return + } + writer.Close() + return +} + func (b bucket) decodeData(totalLeft, blockSize int64, readers []io.ReadCloser, encoder Encoder, writer *io.PipeWriter) ([]byte, error) { var curBlockSize int64 if blockSize < totalLeft { diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/disk.go b/Godeps/_workspace/src/github.com/minio-io/donut/disk.go index 7cd8c3db..923a8eb5 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/disk.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/disk.go @@ -1,3 +1,19 @@ +/* + * 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 ( @@ -10,9 +26,9 @@ import ( ) type disk struct { - root string - order int - fsType string + root string + order int + filesystem map[string]string } // NewDisk - instantiate new disk @@ -20,6 +36,11 @@ func NewDisk(diskPath string, diskOrder int) (Disk, error) { if diskPath == "" || diskOrder < 0 { return nil, errors.New("invalid argument") } + s := syscall.Statfs_t{} + err := syscall.Statfs(diskPath, &s) + if err != nil { + return nil, err + } st, err := os.Stat(diskPath) if err != nil { return nil, err @@ -28,14 +49,19 @@ func NewDisk(diskPath string, diskOrder int) (Disk, error) { return nil, syscall.ENOTDIR } d := disk{ - root: diskPath, - order: diskOrder, - fsType: "", + root: diskPath, + order: diskOrder, + filesystem: make(map[string]string), } - return d, nil + if fsType := d.getFSType(s.Type); fsType != "UNKNOWN" { + d.filesystem["FSType"] = fsType + d.filesystem["MountPoint"] = d.root + return d, nil + } + return nil, errors.New("unsupported filesystem") } -func (d disk) GetName() string { +func (d disk) GetPath() string { return d.root } @@ -43,8 +69,15 @@ func (d disk) GetOrder() int { return d.order } -func (d disk) GetFileSystemType() string { - return d.fsType +func (d disk) GetFSInfo() map[string]string { + s := syscall.Statfs_t{} + err := syscall.Statfs(d.root, &s) + if err != nil { + return nil + } + d.filesystem["Total"] = d.formatBytes(s.Bsize * int64(s.Blocks)) + d.filesystem["Free"] = d.formatBytes(s.Bsize * int64(s.Bfree)) + return d.filesystem } func (d disk) MakeDir(dirname string) error { diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/disk_internal.go b/Godeps/_workspace/src/github.com/minio-io/donut/disk_internal.go new file mode 100644 index 00000000..0f78abc6 --- /dev/null +++ b/Godeps/_workspace/src/github.com/minio-io/donut/disk_internal.go @@ -0,0 +1,63 @@ +/* + * 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 ( + "fmt" + "strconv" + "strings" +) + +// Convert bytes to human readable string. Like a 2 MB, 64.2 KB, 52 B +func (d disk) formatBytes(i int64) (result string) { + switch { + case i > (1024 * 1024 * 1024 * 1024): + result = fmt.Sprintf("%.02f TB", float64(i)/1024/1024/1024/1024) + case i > (1024 * 1024 * 1024): + result = fmt.Sprintf("%.02f GB", float64(i)/1024/1024/1024) + case i > (1024 * 1024): + result = fmt.Sprintf("%.02f MB", float64(i)/1024/1024) + case i > 1024: + result = fmt.Sprintf("%.02f KB", float64(i)/1024) + default: + result = fmt.Sprintf("%d B", i) + } + result = strings.Trim(result, " ") + return +} + +var fsType2StringMap = map[string]string{ + "137d": "EXT", + "ef51": "EXT2OLD", + "ef53": "EXT4", + "4244": "HFS", + "5346544e": "NTFS", + "4d44": "MSDOS", + "52654973": "REISERFS", + "6969": "NFS", + "01021994": "TMPFS", + "58465342": "XFS", +} + +func (d disk) getFSType(fsType int64) string { + fsTypeHex := strconv.FormatInt(fsType, 16) + fsTypeString, ok := fsType2StringMap[fsTypeHex] + if ok == false { + return "UNKNOWN" + } + return fsTypeString +} diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/donut.go b/Godeps/_workspace/src/github.com/minio-io/donut/donut.go index ae3cfc12..446223ac 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/donut.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/donut.go @@ -1,3 +1,19 @@ +/* + * 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 ( diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/erasure.go b/Godeps/_workspace/src/github.com/minio-io/donut/erasure.go index e6288d74..9eb41dca 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/erasure.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/erasure.go @@ -1,3 +1,19 @@ +/* + * 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 ( diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/interfaces.go b/Godeps/_workspace/src/github.com/minio-io/donut/interfaces.go index da66c54d..5f613db0 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/interfaces.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/interfaces.go @@ -1,3 +1,19 @@ +/* + * 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 ( @@ -37,7 +53,7 @@ type Bucket interface { ListNodes() (map[string]Node, error) ListObjects() (map[string]Object, error) - GetObject(object string, writer *io.PipeWriter, donutObjectMetadata map[string]string) + GetObject(object string) (io.ReadCloser, int64, error) PutObject(object string, contents io.Reader) error WriteDonutObjectMetadata(object string, donutMetadata map[string]string) error @@ -72,9 +88,9 @@ type Disk interface { MakeFile(path string) (*os.File, error) OpenFile(path string) (*os.File, error) - GetName() string + GetPath() string GetOrder() int - GetFileSystemType() string + GetFSInfo() map[string]string SaveConfig() ([]byte, error) LoadConfig([]byte) error } diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/node.go b/Godeps/_workspace/src/github.com/minio-io/donut/node.go index 2bf067f6..cab56fd1 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/node.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/node.go @@ -1,3 +1,19 @@ +/* + * 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 ( @@ -34,12 +50,12 @@ func (n node) AttachDisk(disk Disk) error { if disk == nil { return errors.New("Invalid argument") } - n.disks[disk.GetName()] = disk + n.disks[disk.GetPath()] = disk return nil } func (n node) DetachDisk(disk Disk) error { - delete(n.disks, disk.GetName()) + delete(n.disks, disk.GetPath()) return nil } diff --git a/Godeps/_workspace/src/github.com/minio-io/donut/object.go b/Godeps/_workspace/src/github.com/minio-io/donut/object.go index 949ca296..481d0b55 100644 --- a/Godeps/_workspace/src/github.com/minio-io/donut/object.go +++ b/Godeps/_workspace/src/github.com/minio-io/donut/object.go @@ -1,3 +1,19 @@ +/* + * 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 ( diff --git a/pkg/client/donut/donut.go b/pkg/client/donut/donut.go index f2f22c8f..f973304c 100644 --- a/pkg/client/donut/donut.go +++ b/pkg/client/donut/donut.go @@ -111,7 +111,6 @@ func (d *donutDriver) Get(bucketName, objectName string) (body io.ReadCloser, si if objectName == "" || strings.TrimSpace(objectName) == "" { return nil, 0, errors.New("invalid argument") } - reader, writer := io.Pipe() buckets, err := d.donut.ListBuckets() if err != nil { return nil, 0, err @@ -119,20 +118,7 @@ func (d *donutDriver) Get(bucketName, objectName string) (body io.ReadCloser, si if _, ok := buckets[bucketName]; !ok { return nil, 0, errors.New("bucket does not exist") } - objects, err := buckets[bucketName].ListObjects() - if _, ok := objects[objectName]; !ok { - return nil, 0, errors.New("object does not exist") - } - donutObjectMetadata, err := objects[objectName].GetDonutObjectMetadata() - if err != nil { - return nil, 0, err - } - size, err = strconv.ParseInt(donutObjectMetadata["size"], 10, 64) - if err != nil { - return nil, 0, err - } - go buckets[bucketName].GetObject(objectName, writer, donutObjectMetadata) - return reader, size, nil + return buckets[bucketName].GetObject(objectName) } // Put creates a new object @@ -163,7 +149,6 @@ func (d *donutDriver) GetPartial(bucketName, objectName string, offset, length i if offset < 0 { return nil, 0, errors.New("invalid argument") } - reader, writer := io.Pipe() buckets, err := d.donut.ListBuckets() if err != nil { return nil, 0, err @@ -171,22 +156,13 @@ func (d *donutDriver) GetPartial(bucketName, objectName string, offset, length i if _, ok := buckets[bucketName]; !ok { return nil, 0, errors.New("bucket does not exist") } - objects, err := buckets[bucketName].ListObjects() - if _, ok := objects[objectName]; !ok { - return nil, 0, errors.New("object does not exist") - } - donutObjectMetadata, err := objects[objectName].GetDonutObjectMetadata() - if err != nil { - return nil, 0, err - } - size, err = strconv.ParseInt(donutObjectMetadata["size"], 10, 64) + reader, size, err := buckets[bucketName].GetObject(objectName) if err != nil { return nil, 0, err } if offset > size || (offset+length-1) > size { return nil, 0, errors.New("invalid range") } - go buckets[bucketName].GetObject(objectName, writer, donutObjectMetadata) n, err := io.CopyN(ioutil.Discard, reader, offset) if err != nil { return nil, 0, err