Update to new changes in multipart handling in objectstorage-go

This commit is contained in:
Harshavardhana
2015-05-20 14:35:31 -07:00
parent b4c4300e2c
commit 6d9872d26e
4 changed files with 85 additions and 33 deletions
+1 -1
View File
@@ -36,7 +36,7 @@
},
{
"ImportPath": "github.com/minio/objectstorage-go",
"Rev": "957a55f003f69bd669440c890b3fffe40368b11b"
"Rev": "67b1b645616fbc186425f25d914c13eb10ae96b4"
},
{
"ImportPath": "github.com/shiena/ansicolor",
+47 -14
View File
@@ -119,10 +119,6 @@ func getEndpoint(region string) string {
return Regions[region]
}
type api struct {
*lowLevelAPI
}
// Config - main configuration struct used by all to set endpoint, credentials, and other options for requests.
type Config struct {
// Standard options
@@ -179,6 +175,10 @@ const (
LibraryVersion = "0.1"
)
type api struct {
*lowLevelAPI
}
// New - instantiate a new minio api client
func New(config *Config) API {
return &api{&lowLevelAPI{config}}
@@ -215,18 +215,51 @@ func (a completedParts) Len() int { return len(a) }
func (a completedParts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a completedParts) Less(i, j int) bool { return a[i].PartNumber < a[j].PartNumber }
// DefaultPartSize - default size per object after which PutObject becomes multipart
// MinimumPartSize - minimum part size per object after which PutObject becomes multipart
// one can change this value during a library import
var DefaultPartSize uint64 = 1024 * 1024 * 5
var MinimumPartSize uint64 = 1024 * 1024 * 5
func (a *api) newObjectUpload(bucket, object string, data io.Reader) error {
// maxParts - unexported right now
var maxParts = uint64(10000)
// maxPartSize - unexported right now
var maxPartSize uint64 = 1024 * 1024 * 1024 * 5
// GetPartSize - calculate the optimal part size for the given objectSize
// NOTE: Assumption here is that for any given object upload to a S3 compatible object
// storage it will have the following parameters as constants
// ---------
// maxParts
// maximumPartSize
// minimumPartSize
// ---------
//
// if a the partSize after division with maxParts is greater than MinimumPartSize
// then choose that to be the new part size, if not return MinimumPartSize
//
// special case where it happens to be that partSize is indeed bigger than the
// maximum part size just return maxPartSize back
func GetPartSize(objectSize uint64) uint64 {
partSize := (objectSize / (maxParts - 1)) // make sure last part has enough buffer and handle this poperly
{
if partSize > MinimumPartSize {
if partSize > maxPartSize {
return maxPartSize
}
return partSize
}
return MinimumPartSize
}
}
func (a *api) newObjectUpload(bucket, object string, size uint64, data io.Reader) error {
initiateMultipartUploadResult, err := a.initiateMultipartUpload(bucket, object)
if err != nil {
return err
}
uploadID := initiateMultipartUploadResult.UploadID
completeMultipartUpload := new(completeMultipartUpload)
for part := range MultiPart(data, DefaultPartSize, nil) {
for part := range multiPart(data, GetPartSize(size), nil) {
if part.Err != nil {
return part.Err
}
@@ -292,7 +325,7 @@ func (a *api) listObjectPartsRecursiveInRoutine(bucket, object, uploadID string,
}
}
func (a *api) continueObjectUpload(bucket, object, uploadID string, data io.Reader) error {
func (a *api) continueObjectUpload(bucket, object, uploadID string, size uint64, data io.Reader) error {
var skipParts []int
completeMultipartUpload := new(completeMultipartUpload)
for part := range a.listObjectPartsRecursive(bucket, object, uploadID) {
@@ -302,7 +335,7 @@ func (a *api) continueObjectUpload(bucket, object, uploadID string, data io.Read
completeMultipartUpload.Part = append(completeMultipartUpload.Part, completedPart)
skipParts = append(skipParts, part.Data.PartNumber)
}
for part := range MultiPart(data, DefaultPartSize, skipParts) {
for part := range multiPart(data, GetPartSize(size), skipParts) {
if part.Err != nil {
return part.Err
}
@@ -382,9 +415,9 @@ func (a *api) PutObject(bucket, object string, size uint64, data io.Reader) erro
return errors.New("invalid object name, should be utf-8")
}
switch {
case size < DefaultPartSize:
case size < MinimumPartSize:
// Single Part use case, use PutObject directly
for part := range MultiPart(data, DefaultPartSize, nil) {
for part := range multiPart(data, MinimumPartSize, nil) {
if part.Err != nil {
return part.Err
}
@@ -408,9 +441,9 @@ func (a *api) PutObject(bucket, object string, size uint64, data io.Reader) erro
}
}
if !inProgress {
return a.newObjectUpload(bucket, object, data)
return a.newObjectUpload(bucket, object, size, data)
}
return a.continueObjectUpload(bucket, object, inProgressUploadID, data)
return a.continueObjectUpload(bucket, object, inProgressUploadID, size, data)
}
return errors.New("Unexpected control flow")
}
+16
View File
@@ -19,6 +19,8 @@ package objectstorage_test
import (
"net/http/httptest"
"testing"
. "github.com/minio/objectstorage-go"
)
func TestBucketOperations(t *testing.T) {
@@ -30,3 +32,17 @@ func TestObjectOperations(t *testing.T) {
ts := httptest.NewServer(robotsTxtHandler)
defer ts.Close()
}
func TestPartSize(t *testing.T) {
var maxPartSize uint64 = 1024 * 1024 * 1024 * 5
partSize := GetPartSize(5000000000000000000)
if partSize > MinimumPartSize {
if partSize != maxPartSize {
t.Fatal("invalid result, cannot be bigger than MaxPartSize 5GB")
}
}
partSize = GetPartSize(50000000000)
if partSize > MinimumPartSize {
t.Fatal("invalid result, cannot be bigger than MinimumPartSize 5MB")
}
}
+21 -18
View File
@@ -21,33 +21,35 @@ import (
"io"
)
// Part - message structure for results from the MultiPart
type Part struct {
// part - message structure for results from the MultiPart
type part struct {
Data io.ReadSeeker
Err error
Len int64
Num int // part number
}
// MultiPart reads from io.Reader, partitions the data into chunks of given chunksize, and sends
// multiPart reads from io.Reader, partitions the data into chunks of given chunksize, and sends
// each chunk as io.ReadSeeker to the caller over a channel
//
// This method runs until an EOF or error occurs. If an error occurs,
// the method sends the error over the channel and returns.
// Before returning, the channel is always closed.
func MultiPart(reader io.Reader, chunkSize uint64, skipParts []int) <-chan Part {
ch := make(chan Part)
//
// additionally this function also skips list of parts if provided
func multiPart(reader io.Reader, chunkSize uint64, skipParts []int) <-chan part {
ch := make(chan part)
go multiPartInRoutine(reader, chunkSize, skipParts, ch)
return ch
}
func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch chan Part) {
func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch chan part) {
defer close(ch)
part := make([]byte, chunkSize)
n, err := io.ReadFull(reader, part)
p := make([]byte, chunkSize)
n, err := io.ReadFull(reader, p)
if err == io.EOF || err == io.ErrUnexpectedEOF { // short read, only single part return
ch <- Part{
Data: bytes.NewReader(part[0:n]),
ch <- part{
Data: bytes.NewReader(p[0:n]),
Err: nil,
Len: int64(n),
Num: 1,
@@ -56,7 +58,7 @@ func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch
}
// catastrophic error send error and return
if err != nil {
ch <- Part{
ch <- part{
Data: nil,
Err: err,
Num: 0,
@@ -66,8 +68,8 @@ func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch
// send the first part
var num = 1
if !isPartNumberUploaded(num, skipParts) {
ch <- Part{
Data: bytes.NewReader(part),
ch <- part{
Data: bytes.NewReader(p),
Err: nil,
Len: int64(n),
Num: num,
@@ -75,11 +77,11 @@ func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch
}
for err == nil {
var n int
part := make([]byte, chunkSize)
n, err = io.ReadFull(reader, part)
p := make([]byte, chunkSize)
n, err = io.ReadFull(reader, p)
if err != nil {
if err != io.EOF && err != io.ErrUnexpectedEOF { // catastrophic error
ch <- Part{
ch <- part{
Data: nil,
Err: err,
Num: 0,
@@ -91,8 +93,8 @@ func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch
if isPartNumberUploaded(num, skipParts) {
continue
}
ch <- Part{
Data: bytes.NewReader(part[0:n]),
ch <- part{
Data: bytes.NewReader(p[0:n]),
Err: nil,
Len: int64(n),
Num: num,
@@ -101,6 +103,7 @@ func multiPartInRoutine(reader io.Reader, chunkSize uint64, skipParts []int, ch
}
}
// to verify if partNumber is part of the skip part list
func isPartNumberUploaded(partNumber int, skipParts []int) bool {
for _, part := range skipParts {
if part == partNumber {