diff --git a/pkg/donut/donut.go b/pkg/donut/donut.go index da093bbc..7e7af93e 100644 --- a/pkg/donut/donut.go +++ b/pkg/donut/donut.go @@ -42,6 +42,6 @@ type ObjectWriter interface { type Writer interface { ObjectWriter - GetDonutDriverMetadata() (map[string]string, error) - SetDonutDriverMetadata(map[string]string) error + GetDonutMetadata() (map[string]string, error) + SetDonutMetadata(map[string]string) error } diff --git a/pkg/donut/erasure.go b/pkg/donut/erasure.go index c5d94fd5..060d1772 100644 --- a/pkg/donut/erasure.go +++ b/pkg/donut/erasure.go @@ -6,38 +6,55 @@ import ( "strconv" "time" - "github.com/minio-io/mc/pkg/encoding/erasure" - "github.com/minio-io/mc/pkg/utils/split" + "encoding/hex" + "errors" + "github.com/minio-io/minio/pkg/encoding/erasure" + "github.com/minio-io/minio/pkg/utils/crypto/sha512" + "github.com/minio-io/minio/pkg/utils/split" ) func erasureReader(readers []io.ReadCloser, donutMetadata map[string]string, writer *io.PipeWriter) { + // TODO handle errors totalChunks, _ := strconv.Atoi(donutMetadata["chunkCount"]) totalLeft, _ := strconv.Atoi(donutMetadata["totalLength"]) blockSize, _ := strconv.Atoi(donutMetadata["blockSize"]) - params, _ := erasure.ParseEncoderParams(8, 8, erasure.Cauchy) + k, _ := strconv.Atoi(donutMetadata["erasureK"]) + m, _ := strconv.Atoi(donutMetadata["erasureM"]) + expectedSha512, _ := hex.DecodeString(donutMetadata["sha512"]) + summer := sha512.New() + // TODO select technique properly + params, _ := erasure.ParseEncoderParams(uint8(k), uint8(m), erasure.Cauchy) encoder := erasure.NewEncoder(params) for _, reader := range readers { defer reader.Close() } for i := 0; i < totalChunks; i++ { - encodedBytes := make([][]byte, 16) - for i, reader := range readers { - var bytesBuffer bytes.Buffer - io.Copy(&bytesBuffer, reader) - encodedBytes[i] = bytesBuffer.Bytes() - } curBlockSize := totalLeft if blockSize < totalLeft { curBlockSize = blockSize } + curChunkSize := erasure.GetEncodedChunkLen(curBlockSize, uint8(k)) + + encodedBytes := make([][]byte, 16) + for i, reader := range readers { + var bytesBuffer bytes.Buffer + // TODO watch for errors + io.CopyN(&bytesBuffer, reader, int64(curChunkSize)) + encodedBytes[i] = bytesBuffer.Bytes() + } decodedData, err := encoder.Decode(encodedBytes, curBlockSize) if err != nil { writer.CloseWithError(err) return } + summer.Write(decodedData) io.Copy(writer, bytes.NewBuffer(decodedData)) totalLeft = totalLeft - blockSize } + actualSha512 := summer.Sum(nil) + if bytes.Compare(expectedSha512, actualSha512) != 0 { + writer.CloseWithError(errors.New("decoded sha512 did not match")) + } writer.Close() } @@ -47,7 +64,7 @@ type erasureWriter struct { writers []Writer metadata map[string]string donutMetadata map[string]string // not exposed - writer *io.PipeWriter + erasureWriter *io.PipeWriter isClosed <-chan bool } @@ -55,10 +72,10 @@ func newErasureWriter(writers []Writer) ObjectWriter { r, w := io.Pipe() isClosed := make(chan bool) writer := erasureWriter{ - writers: writers, - metadata: make(map[string]string), - writer: w, - isClosed: isClosed, + writers: writers, + metadata: make(map[string]string), + erasureWriter: w, + isClosed: isClosed, } go erasureGoroutine(r, writer, isClosed) return writer @@ -70,16 +87,19 @@ func erasureGoroutine(r *io.PipeReader, eWriter erasureWriter, isClosed chan<- b encoder := erasure.NewEncoder(params) chunkCount := 0 totalLength := 0 + summer := sha512.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(eWriter.writers[blockIndex], bytes.NewBuffer(block)) } } chunkCount = chunkCount + 1 } + dataSha512 := summer.Sum(nil) metadata := make(map[string]string) metadata["blockSize"] = strconv.Itoa(10 * 1024 * 1024) metadata["chunkCount"] = strconv.Itoa(chunkCount) @@ -88,10 +108,11 @@ func erasureGoroutine(r *io.PipeReader, eWriter erasureWriter, isClosed chan<- b metadata["erasureM"] = "8" metadata["erasureTechnique"] = "Cauchy" metadata["totalLength"] = strconv.Itoa(totalLength) + metadata["sha512"] = hex.EncodeToString(dataSha512) for _, nodeWriter := range eWriter.writers { if nodeWriter != nil { nodeWriter.SetMetadata(eWriter.metadata) - nodeWriter.SetDonutDriverMetadata(metadata) + nodeWriter.SetDonutMetadata(metadata) nodeWriter.Close() } } @@ -99,12 +120,12 @@ func erasureGoroutine(r *io.PipeReader, eWriter erasureWriter, isClosed chan<- b } func (d erasureWriter) Write(data []byte) (int, error) { - io.Copy(d.writer, bytes.NewBuffer(data)) + io.Copy(d.erasureWriter, bytes.NewBuffer(data)) return len(data), nil } func (d erasureWriter) Close() error { - d.writer.Close() + d.erasureWriter.Close() <-d.isClosed return nil } diff --git a/pkg/donut/writer.go b/pkg/donut/writer.go index b94e4c34..3d837a2f 100644 --- a/pkg/donut/writer.go +++ b/pkg/donut/writer.go @@ -69,7 +69,7 @@ func (d donutFileWriter) GetMetadata() (map[string]string, error) { return metadata, nil } -func (d donutFileWriter) SetDonutDriverMetadata(metadata map[string]string) error { +func (d donutFileWriter) SetDonutMetadata(metadata map[string]string) error { for k := range d.donutMetadata { delete(d.donutMetadata, k) } @@ -79,7 +79,7 @@ func (d donutFileWriter) SetDonutDriverMetadata(metadata map[string]string) erro return nil } -func (d donutFileWriter) GetDonutDriverMetadata() (map[string]string, error) { +func (d donutFileWriter) GetDonutMetadata() (map[string]string, error) { donutMetadata := make(map[string]string) for k, v := range d.donutMetadata { donutMetadata[k] = v diff --git a/pkg/encoding/erasure/cauchy_test.go b/pkg/encoding/erasure/cauchy_test.go index 0dda870d..d577d97d 100644 --- a/pkg/encoding/erasure/cauchy_test.go +++ b/pkg/encoding/erasure/cauchy_test.go @@ -29,14 +29,18 @@ var _ = Suite(&MySuite{}) func Test(t *testing.T) { TestingT(t) } +const ( + k = 10 + m = 5 +) + func (s *MySuite) TestCauchyDecode(c *C) { - ep, _ := ParseEncoderParams(10, 5, Cauchy) + ep, _ := ParseEncoderParams(k, m, Cauchy) data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") e := NewEncoder(ep) - chunks, length := e.Encode(data) - c.Assert(length, Equals, len(data)) + chunks, _ := e.Encode(data) chunks[0] = nil chunks[3] = nil @@ -44,7 +48,7 @@ func (s *MySuite) TestCauchyDecode(c *C) { chunks[9] = nil chunks[13] = nil - recoveredData, err := e.Decode(chunks, length) + recoveredData, err := e.Decode(chunks, len(data)) c.Assert(err, IsNil) if !bytes.Equal(data, recoveredData) { diff --git a/pkg/encoding/erasure/erasure_decode.go b/pkg/encoding/erasure/erasure_decode.go index 8d79a510..a4de0fdf 100644 --- a/pkg/encoding/erasure/erasure_decode.go +++ b/pkg/encoding/erasure/erasure_decode.go @@ -37,12 +37,13 @@ func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) { var decode_index *C.uint32_t var source, target **C.uint8_t - k := int(e.k) - n := int(e.k + e.m) - if len(chunks) != n { + k := e.params.K + m := e.params.M + n := k + m + if len(chunks) != int(n) { return nil, errors.New(fmt.Sprintf("chunks length must be %d", n)) } - chunk_size := getChunkSize(k, length) + chunk_size := GetEncodedChunkLen(length, uint8(k)) error_index := make([]int, n+1) var err_count int = 0 @@ -58,7 +59,7 @@ func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) { err_count++ // Too many missing chunks, cannot be more than parity `m` - if err_count-1 > (n - k) { + if err_count-1 > int(n-k) { return nil, errors.New("too many erasures requested, can't decode") } @@ -70,7 +71,7 @@ func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) { } } - C.minio_init_decoder(error_index_ptr, e.k, e.k+e.m, C.int(err_count-1), + C.minio_init_decoder(error_index_ptr, C.int(k), C.int(n), C.int(err_count-1), e.encode_matrix, &decode_matrix, &decode_tbls, &decode_index) pointers := make([]*byte, n) @@ -80,18 +81,18 @@ func (e *Encoder) Decode(chunks [][]byte, length int) ([]byte, error) { data := (**C.uint8_t)(unsafe.Pointer(&pointers[0])) - ret := C.minio_get_source_target(C.int(err_count-1), e.k, e.m, error_index_ptr, + ret := C.minio_get_source_target(C.int(err_count-1), C.int(k), C.int(m), error_index_ptr, decode_index, data, &source, &target) if int(ret) == -1 { return nil, errors.New("Decoding source target failed") } - C.ec_encode_data(C.int(chunk_size), e.k, C.int(err_count-1), decode_tbls, + C.ec_encode_data(C.int(chunk_size), C.int(k), C.int(err_count-1), decode_tbls, source, target) - recovered_output := make([]byte, 0, chunk_size*k) - for i := 0; i < k; i++ { + recovered_output := make([]byte, 0, chunk_size*int(k)) + for i := 0; i < int(k); i++ { recovered_output = append(recovered_output, chunks[i]...) } diff --git a/pkg/encoding/erasure/erasure_encode.go b/pkg/encoding/erasure/erasure_encode.go index 1a8301e4..65f1313e 100644 --- a/pkg/encoding/erasure/erasure_encode.go +++ b/pkg/encoding/erasure/erasure_encode.go @@ -39,7 +39,7 @@ const ( ) const ( - SimdAlign = 32 + SIMDAlign = 32 ) // EncoderParams is a configuration set for building an encoder. It is created using ValidateParams. @@ -51,9 +51,7 @@ type EncoderParams struct { // Encoder is an object used to encode and decode data. type Encoder struct { - p *EncoderParams - k, - m C.int + params *EncoderParams encode_matrix, encode_tbls, decode_matrix, @@ -96,19 +94,17 @@ func ParseEncoderParams(k, m uint8, technique Technique) (*EncoderParams, error) // NewEncoder creates an encoder object with a given set of parameters. func NewEncoder(ep *EncoderParams) *Encoder { - var k = C.int(ep.K) - var m = C.int(ep.M) - var encode_matrix *C.uint8_t var encode_tbls *C.uint8_t + k := C.int(ep.K) + m := C.int(ep.M) + C.minio_init_encoder(C.int(ep.Technique), k, m, &encode_matrix, &encode_tbls) return &Encoder{ - p: ep, - k: k, - m: m, + params: ep, encode_matrix: encode_matrix, encode_tbls: encode_tbls, decode_matrix: nil, @@ -116,59 +112,68 @@ func NewEncoder(ep *EncoderParams) *Encoder { } } -func getChunkSize(k, split_len int) int { - var alignment, remainder, padded_len int +func GetEncodedLen(inputLen int, k, m uint8) (outputLen int) { + outputLen = GetEncodedChunkLen(inputLen, k) * int(k+m) + return outputLen +} - alignment = k * SimdAlign - remainder = split_len % alignment +func GetEncodedChunkLen(inputLen int, k uint8) (outputChunkLen int) { + alignment := int(k) * SIMDAlign + remainder := inputLen % alignment - padded_len = split_len + paddedInputLen := inputLen if remainder != 0 { - padded_len = split_len + (alignment - remainder) + paddedInputLen = inputLen + (alignment - remainder) } - return padded_len / k + outputChunkLen = paddedInputLen / int(k) + return outputChunkLen } // Encode encodes a block of data. The input is the original data. The output // is a 2 tuple containing (k + m) chunks of erasure encoded data and the // length of the original object. -func (e *Encoder) Encode(block []byte) ([][]byte, int) { - var block_len = len(block) +func (e *Encoder) Encode(input []byte) ([][]byte, error) { + inputLen := len(input) + k := C.int(e.params.K) + m := C.int(e.params.M) + n := k + m - chunk_size := getChunkSize(int(e.k), block_len) - chunk_len := chunk_size * int(e.k) - pad_len := int(chunk_len) - block_len + chunkLen := GetEncodedChunkLen(inputLen, e.params.K) + encodedDataLen := chunkLen * int(k) + paddedDataLen := int(encodedDataLen) - inputLen - if pad_len > 0 { - s := make([]byte, pad_len) + if paddedDataLen > 0 { + s := make([]byte, paddedDataLen) // Expand with new padded blocks to the byte array - block = append(block, s...) + input = append(input, s...) } - coded_len := chunk_size * int(e.p.M) - c := make([]byte, coded_len) - block = append(block, c...) + encodedParityLen := chunkLen * int(e.params.M) + c := make([]byte, encodedParityLen) + input = append(input, c...) + + // encodedOutLen := encodedDataLen + encodedParityLen // Allocate chunks - chunks := make([][]byte, e.p.K+e.p.M) - pointers := make([]*byte, e.p.K+e.p.M) + chunks := make([][]byte, k+m) + pointers := make([]*byte, k+m) var i int // Add data blocks to chunks - for i = 0; i < int(e.p.K); i++ { - chunks[i] = block[i*chunk_size : (i+1)*chunk_size] + for i = 0; i < int(k); i++ { + chunks[i] = input[i*chunkLen : (i+1)*chunkLen] pointers[i] = &chunks[i][0] } - for i = int(e.p.K); i < int(e.p.K+e.p.M); i++ { - chunks[i] = make([]byte, chunk_size) + for i = int(k); i < int(n); i++ { + chunks[i] = make([]byte, chunkLen) pointers[i] = &chunks[i][0] } - data := (**C.uint8_t)(unsafe.Pointer(&pointers[:e.p.K][0])) - coding := (**C.uint8_t)(unsafe.Pointer(&pointers[e.p.K:][0])) + data := (**C.uint8_t)(unsafe.Pointer(&pointers[:k][0])) + coding := (**C.uint8_t)(unsafe.Pointer(&pointers[k:][0])) - C.ec_encode_data(C.int(chunk_size), e.k, e.m, e.encode_tbls, data, + C.ec_encode_data(C.int(chunkLen), k, m, e.encode_tbls, data, coding) - return chunks, block_len + return chunks, nil } diff --git a/pkg/encoding/erasure/vandermonde_test.go b/pkg/encoding/erasure/vandermonde_test.go index 50ea52fe..853dd086 100644 --- a/pkg/encoding/erasure/vandermonde_test.go +++ b/pkg/encoding/erasure/vandermonde_test.go @@ -23,15 +23,14 @@ import ( ) func (s *MySuite) TestVanderMondeDecode(c *C) { - ep, _ := ParseEncoderParams(10, 5, Vandermonde) + ep, _ := ParseEncoderParams(k, m, Vandermonde) data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") e := NewEncoder(ep) - chunks, length := e.Encode(data) + chunks, err := e.Encode(data) c.Logf("chunks length: %d", len(chunks)) - c.Logf("length: %d", length) - c.Assert(length, Equals, len(data)) + c.Logf("length: %d", len(data)) chunks[0] = nil chunks[3] = nil @@ -39,7 +38,7 @@ func (s *MySuite) TestVanderMondeDecode(c *C) { chunks[9] = nil chunks[13] = nil - recoveredData, err := e.Decode(chunks, length) + recoveredData, err := e.Decode(chunks, len(data)) c.Assert(err, IsNil) if !bytes.Equal(recoveredData, data) {