2015-03-22 18:14:06 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2015-03-23 14:46:19 -04:00
|
|
|
"crypto/md5"
|
2015-03-22 23:11:25 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2015-03-22 18:14:06 -04:00
|
|
|
"github.com/minio-io/minio/pkg/encoding/erasure"
|
|
|
|
"github.com/minio-io/minio/pkg/utils/split"
|
2015-03-23 14:56:16 -04:00
|
|
|
"strings"
|
2015-03-22 18:14:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func erasureReader(readers []io.ReadCloser, donutMetadata map[string]string, writer *io.PipeWriter) {
|
2015-03-22 23:11:25 -04:00
|
|
|
// TODO handle errors
|
2015-03-22 18:14:06 -04:00
|
|
|
totalChunks, _ := strconv.Atoi(donutMetadata["chunkCount"])
|
|
|
|
totalLeft, _ := strconv.Atoi(donutMetadata["totalLength"])
|
|
|
|
blockSize, _ := strconv.Atoi(donutMetadata["blockSize"])
|
2015-03-22 21:49:18 -04:00
|
|
|
k, _ := strconv.Atoi(donutMetadata["erasureK"])
|
|
|
|
m, _ := strconv.Atoi(donutMetadata["erasureM"])
|
2015-03-23 14:46:19 -04:00
|
|
|
expectedMd5sum, _ := hex.DecodeString(donutMetadata["md5"])
|
|
|
|
summer := md5.New()
|
2015-03-22 21:49:18 -04:00
|
|
|
// TODO select technique properly
|
|
|
|
params, _ := erasure.ParseEncoderParams(uint8(k), uint8(m), erasure.Cauchy)
|
2015-03-22 18:14:06 -04:00
|
|
|
encoder := erasure.NewEncoder(params)
|
|
|
|
for _, reader := range readers {
|
|
|
|
defer reader.Close()
|
|
|
|
}
|
|
|
|
for i := 0; i < totalChunks; i++ {
|
2015-03-22 21:49:18 -04:00
|
|
|
curBlockSize := totalLeft
|
|
|
|
if blockSize < totalLeft {
|
|
|
|
curBlockSize = blockSize
|
|
|
|
}
|
|
|
|
curChunkSize := erasure.GetEncodedChunkLen(curBlockSize, uint8(k))
|
|
|
|
|
2015-03-22 18:14:06 -04:00
|
|
|
encodedBytes := make([][]byte, 16)
|
|
|
|
for i, reader := range readers {
|
|
|
|
var bytesBuffer bytes.Buffer
|
2015-03-22 23:11:25 -04:00
|
|
|
// TODO watch for errors
|
2015-03-22 21:49:18 -04:00
|
|
|
io.CopyN(&bytesBuffer, reader, int64(curChunkSize))
|
2015-03-22 18:14:06 -04:00
|
|
|
encodedBytes[i] = bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
decodedData, err := encoder.Decode(encodedBytes, curBlockSize)
|
|
|
|
if err != nil {
|
|
|
|
writer.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-22 23:11:25 -04:00
|
|
|
summer.Write(decodedData)
|
2015-03-22 18:14:06 -04:00
|
|
|
io.Copy(writer, bytes.NewBuffer(decodedData))
|
|
|
|
totalLeft = totalLeft - blockSize
|
|
|
|
}
|
2015-03-23 14:46:19 -04:00
|
|
|
actualMd5sum := summer.Sum(nil)
|
|
|
|
if bytes.Compare(expectedMd5sum, actualMd5sum) != 0 {
|
|
|
|
writer.CloseWithError(errors.New("decoded md5sum did not match"))
|
|
|
|
return
|
2015-03-22 23:11:25 -04:00
|
|
|
}
|
2015-03-22 18:14:06 -04:00
|
|
|
writer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// erasure writer
|
|
|
|
|
|
|
|
type erasureWriter struct {
|
|
|
|
writers []Writer
|
|
|
|
metadata map[string]string
|
|
|
|
donutMetadata map[string]string // not exposed
|
|
|
|
erasureWriter *io.PipeWriter
|
|
|
|
isClosed <-chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newErasureWriter(writers []Writer) ObjectWriter {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
isClosed := make(chan bool)
|
|
|
|
writer := erasureWriter{
|
|
|
|
writers: writers,
|
|
|
|
metadata: make(map[string]string),
|
|
|
|
erasureWriter: w,
|
|
|
|
isClosed: isClosed,
|
|
|
|
}
|
|
|
|
go erasureGoroutine(r, writer, isClosed)
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func erasureGoroutine(r *io.PipeReader, eWriter erasureWriter, isClosed chan<- bool) {
|
|
|
|
chunks := split.Stream(r, 10*1024*1024)
|
|
|
|
params, _ := erasure.ParseEncoderParams(8, 8, erasure.Cauchy)
|
|
|
|
encoder := erasure.NewEncoder(params)
|
|
|
|
chunkCount := 0
|
|
|
|
totalLength := 0
|
2015-03-23 14:46:19 -04:00
|
|
|
summer := md5.New()
|
2015-03-22 18:14:06 -04:00
|
|
|
for chunk := range chunks {
|
|
|
|
if chunk.Err == nil {
|
|
|
|
totalLength = totalLength + len(chunk.Data)
|
|
|
|
encodedBlocks, _ := encoder.Encode(chunk.Data)
|
2015-03-22 23:11:25 -04:00
|
|
|
summer.Write(chunk.Data)
|
2015-03-22 18:14:06 -04:00
|
|
|
for blockIndex, block := range encodedBlocks {
|
|
|
|
io.Copy(eWriter.writers[blockIndex], bytes.NewBuffer(block))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chunkCount = chunkCount + 1
|
|
|
|
}
|
2015-03-23 14:46:19 -04:00
|
|
|
dataMd5sum := summer.Sum(nil)
|
2015-03-22 18:14:06 -04:00
|
|
|
metadata := make(map[string]string)
|
|
|
|
metadata["blockSize"] = strconv.Itoa(10 * 1024 * 1024)
|
|
|
|
metadata["chunkCount"] = strconv.Itoa(chunkCount)
|
|
|
|
metadata["created"] = time.Now().Format(time.RFC3339Nano)
|
|
|
|
metadata["erasureK"] = "8"
|
|
|
|
metadata["erasureM"] = "8"
|
|
|
|
metadata["erasureTechnique"] = "Cauchy"
|
2015-03-23 14:46:19 -04:00
|
|
|
metadata["md5"] = hex.EncodeToString(dataMd5sum)
|
2015-03-23 14:56:16 -04:00
|
|
|
metadata["totalLength"] = strconv.Itoa(totalLength)
|
2015-03-22 18:14:06 -04:00
|
|
|
for _, nodeWriter := range eWriter.writers {
|
|
|
|
if nodeWriter != nil {
|
|
|
|
nodeWriter.SetMetadata(eWriter.metadata)
|
|
|
|
nodeWriter.SetDonutMetadata(metadata)
|
|
|
|
nodeWriter.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isClosed <- true
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:25:15 -04:00
|
|
|
func (eWriter erasureWriter) Write(data []byte) (int, error) {
|
|
|
|
io.Copy(eWriter.erasureWriter, bytes.NewBuffer(data))
|
2015-03-22 18:14:06 -04:00
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:25:15 -04:00
|
|
|
func (eWriter erasureWriter) Close() error {
|
|
|
|
eWriter.erasureWriter.Close()
|
|
|
|
<-eWriter.isClosed
|
2015-03-22 18:14:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:25:15 -04:00
|
|
|
func (eWriter erasureWriter) CloseWithError(err error) error {
|
|
|
|
for _, writer := range eWriter.writers {
|
2015-03-22 18:14:06 -04:00
|
|
|
if writer != nil {
|
|
|
|
writer.CloseWithError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:25:15 -04:00
|
|
|
func (eWriter erasureWriter) SetMetadata(metadata map[string]string) error {
|
2015-03-23 14:56:16 -04:00
|
|
|
for k, _ := range metadata {
|
|
|
|
if strings.HasPrefix(k, "sys.") {
|
|
|
|
return errors.New("Invalid key '" + k + "', cannot start with sys.'")
|
|
|
|
}
|
|
|
|
}
|
2015-03-23 15:25:15 -04:00
|
|
|
for k := range eWriter.metadata {
|
|
|
|
delete(eWriter.metadata, k)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
for k, v := range metadata {
|
2015-03-23 15:25:15 -04:00
|
|
|
eWriter.metadata[k] = v
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:25:15 -04:00
|
|
|
func (eWriter erasureWriter) GetMetadata() (map[string]string, error) {
|
2015-03-22 18:14:06 -04:00
|
|
|
metadata := make(map[string]string)
|
2015-03-23 15:25:15 -04:00
|
|
|
for k, v := range eWriter.metadata {
|
2015-03-22 18:14:06 -04:00
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|