2016-06-01 19:43:31 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
|
|
|
|
2016-06-02 04:49:46 -04:00
|
|
|
// erasureCreateFile - writes an entire stream by erasure coding to
|
|
|
|
// all the disks, writes also calculate individual block's checksum
|
|
|
|
// for future bit-rot protection.
|
2016-07-18 22:06:48 -04:00
|
|
|
func erasureCreateFile(disks []StorageAPI, volume, path string, reader io.Reader, blockSize int64, dataBlocks int, parityBlocks int, writeQuorum int) (bytesWritten int64, checkSums []string, err error) {
|
2016-06-17 03:17:15 -04:00
|
|
|
// Allocated blockSized buffer for reading.
|
2016-07-14 17:59:01 -04:00
|
|
|
buf := make([]byte, blockSize)
|
|
|
|
|
2016-06-17 03:17:15 -04:00
|
|
|
hashWriters := newHashWriters(len(disks))
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Read until io.EOF, erasure codes data and writes to all disks.
|
|
|
|
for {
|
2016-06-19 17:51:20 -04:00
|
|
|
var blocks [][]byte
|
2016-07-14 17:59:01 -04:00
|
|
|
n, rErr := io.ReadFull(reader, buf)
|
2016-07-01 17:33:28 -04:00
|
|
|
// FIXME: this is a bug in Golang, n == 0 and err ==
|
|
|
|
// io.ErrUnexpectedEOF for io.ReadFull function.
|
|
|
|
if n == 0 && rErr == io.ErrUnexpectedEOF {
|
2016-07-14 17:59:01 -04:00
|
|
|
return 0, nil, rErr
|
2016-07-01 17:33:28 -04:00
|
|
|
}
|
|
|
|
if rErr == io.EOF {
|
2016-06-19 17:51:20 -04:00
|
|
|
// We have reached EOF on the first byte read, io.Reader
|
|
|
|
// must be 0bytes, we don't need to erasure code
|
|
|
|
// data. Will create a 0byte file instead.
|
2016-07-18 22:06:48 -04:00
|
|
|
if bytesWritten == 0 {
|
2016-06-19 17:51:20 -04:00
|
|
|
blocks = make([][]byte, len(disks))
|
2016-07-14 17:59:01 -04:00
|
|
|
rErr = appendFile(disks, volume, path, blocks, hashWriters, writeQuorum)
|
2016-07-01 17:33:28 -04:00
|
|
|
if rErr != nil {
|
2016-07-14 17:59:01 -04:00
|
|
|
return 0, nil, rErr
|
2016-06-19 17:51:20 -04:00
|
|
|
}
|
|
|
|
} // else we have reached EOF after few reads, no need to
|
|
|
|
// add an additional 0bytes at the end.
|
2016-06-01 19:43:31 -04:00
|
|
|
break
|
|
|
|
}
|
2016-07-01 17:33:28 -04:00
|
|
|
if rErr != nil && rErr != io.ErrUnexpectedEOF {
|
2016-07-14 17:59:01 -04:00
|
|
|
return 0, nil, rErr
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
2016-07-05 04:04:50 -04:00
|
|
|
if n > 0 {
|
|
|
|
// Returns encoded blocks.
|
|
|
|
var enErr error
|
2016-07-14 17:59:01 -04:00
|
|
|
blocks, enErr = encodeData(buf[0:n], dataBlocks, parityBlocks)
|
2016-07-05 04:04:50 -04:00
|
|
|
if enErr != nil {
|
2016-07-14 17:59:01 -04:00
|
|
|
return 0, nil, enErr
|
2016-07-05 04:04:50 -04:00
|
|
|
}
|
2016-06-19 17:51:20 -04:00
|
|
|
|
2016-07-05 04:04:50 -04:00
|
|
|
// Write to all disks.
|
2016-07-14 17:59:01 -04:00
|
|
|
if err = appendFile(disks, volume, path, blocks, hashWriters, writeQuorum); err != nil {
|
|
|
|
return 0, nil, err
|
2016-07-05 04:04:50 -04:00
|
|
|
}
|
2016-07-18 22:06:48 -04:00
|
|
|
bytesWritten += int64(n)
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:59:01 -04:00
|
|
|
checkSums = make([]string, len(disks))
|
|
|
|
for i := range checkSums {
|
|
|
|
checkSums[i] = hex.EncodeToString(hashWriters[i].Sum(nil))
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
2016-07-18 22:06:48 -04:00
|
|
|
return bytesWritten, checkSums, nil
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
|
|
|
|
2016-06-02 04:49:46 -04:00
|
|
|
// encodeData - encodes incoming data buffer into
|
|
|
|
// dataBlocks+parityBlocks returns a 2 dimensional byte array.
|
|
|
|
func encodeData(dataBuffer []byte, dataBlocks, parityBlocks int) ([][]byte, error) {
|
|
|
|
rs, err := reedsolomon.New(dataBlocks, parityBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Split the input buffer into data and parity blocks.
|
|
|
|
var blocks [][]byte
|
|
|
|
blocks, err = rs.Split(dataBuffer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode parity blocks using data blocks.
|
|
|
|
err = rs.Encode(blocks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return encoded blocks.
|
|
|
|
return blocks, nil
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// appendFile - append data buffer at path.
|
2016-07-14 17:59:01 -04:00
|
|
|
func appendFile(disks []StorageAPI, volume, path string, enBlocks [][]byte, hashWriters []hash.Hash, writeQuorum int) (err error) {
|
2016-06-01 19:43:31 -04:00
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
var wErrs = make([]error, len(disks))
|
|
|
|
// Write encoded data to quorum disks in parallel.
|
|
|
|
for index, disk := range disks {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
// Write encoded data in routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-07-14 17:59:01 -04:00
|
|
|
wErr := disk.AppendFile(volume, path, enBlocks[index])
|
2016-06-01 19:43:31 -04:00
|
|
|
if wErr != nil {
|
|
|
|
wErrs[index] = wErr
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate hash for each blocks.
|
2016-07-14 17:59:01 -04:00
|
|
|
hashWriters[index].Write(enBlocks[index])
|
2016-06-01 19:43:31 -04:00
|
|
|
|
|
|
|
// Successfully wrote.
|
|
|
|
wErrs[index] = nil
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the appends to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Do we have write quorum?.
|
2016-07-13 03:29:48 -04:00
|
|
|
if !isDiskQuorum(wErrs, writeQuorum) {
|
2016-07-19 02:56:16 -04:00
|
|
|
return errXLWriteQuorum
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|
|
|
|
return nil
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|