2016-06-02 01:43:31 +02: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.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-06-02 01:43:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
|
|
|
|
2016-06-02 10:49:46 +02: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-28 11:20:34 +02:00
|
|
|
func erasureCreateFile(disks []StorageAPI, volume, path string, reader io.Reader, blockSize int64, dataBlocks int, parityBlocks int, algo string, writeQuorum int) (bytesWritten int64, checkSums []string, err error) {
|
2016-06-17 09:17:15 +02:00
|
|
|
// Allocated blockSized buffer for reading.
|
2016-07-14 23:59:01 +02:00
|
|
|
buf := make([]byte, blockSize)
|
|
|
|
|
2016-07-28 11:20:34 +02:00
|
|
|
hashWriters := newHashWriters(len(disks), algo)
|
2016-06-17 09:17:15 +02:00
|
|
|
|
2016-06-02 01:43:31 +02:00
|
|
|
// Read until io.EOF, erasure codes data and writes to all disks.
|
|
|
|
for {
|
2016-06-19 23:51:20 +02:00
|
|
|
var blocks [][]byte
|
2016-07-14 23:59:01 +02:00
|
|
|
n, rErr := io.ReadFull(reader, buf)
|
2016-07-01 23:33:28 +02: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-08-25 18:39:01 +02:00
|
|
|
return 0, nil, traceError(rErr)
|
2016-07-01 23:33:28 +02:00
|
|
|
}
|
|
|
|
if rErr == io.EOF {
|
2016-06-19 23:51:20 +02: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-19 04:06:48 +02:00
|
|
|
if bytesWritten == 0 {
|
2016-06-19 23:51:20 +02:00
|
|
|
blocks = make([][]byte, len(disks))
|
2016-07-14 23:59:01 +02:00
|
|
|
rErr = appendFile(disks, volume, path, blocks, hashWriters, writeQuorum)
|
2016-07-01 23:33:28 +02:00
|
|
|
if rErr != nil {
|
2016-07-14 23:59:01 +02:00
|
|
|
return 0, nil, rErr
|
2016-06-19 23:51:20 +02:00
|
|
|
}
|
|
|
|
} // else we have reached EOF after few reads, no need to
|
|
|
|
// add an additional 0bytes at the end.
|
2016-06-02 01:43:31 +02:00
|
|
|
break
|
|
|
|
}
|
2016-07-01 23:33:28 +02:00
|
|
|
if rErr != nil && rErr != io.ErrUnexpectedEOF {
|
2016-08-25 18:39:01 +02:00
|
|
|
return 0, nil, traceError(rErr)
|
2016-06-02 01:43:31 +02:00
|
|
|
}
|
2016-07-05 10:04:50 +02:00
|
|
|
if n > 0 {
|
|
|
|
// Returns encoded blocks.
|
|
|
|
var enErr error
|
2016-07-14 23:59:01 +02:00
|
|
|
blocks, enErr = encodeData(buf[0:n], dataBlocks, parityBlocks)
|
2016-07-05 10:04:50 +02:00
|
|
|
if enErr != nil {
|
2016-07-14 23:59:01 +02:00
|
|
|
return 0, nil, enErr
|
2016-07-05 10:04:50 +02:00
|
|
|
}
|
2016-06-19 23:51:20 +02:00
|
|
|
|
2016-07-05 10:04:50 +02:00
|
|
|
// Write to all disks.
|
2016-07-14 23:59:01 +02:00
|
|
|
if err = appendFile(disks, volume, path, blocks, hashWriters, writeQuorum); err != nil {
|
|
|
|
return 0, nil, err
|
2016-07-05 10:04:50 +02:00
|
|
|
}
|
2016-07-19 04:06:48 +02:00
|
|
|
bytesWritten += int64(n)
|
2016-06-02 01:43:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 23:59:01 +02:00
|
|
|
checkSums = make([]string, len(disks))
|
|
|
|
for i := range checkSums {
|
|
|
|
checkSums[i] = hex.EncodeToString(hashWriters[i].Sum(nil))
|
2016-06-02 01:43:31 +02:00
|
|
|
}
|
2016-07-19 04:06:48 +02:00
|
|
|
return bytesWritten, checkSums, nil
|
2016-06-02 01:43:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-02 10:49:46 +02: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 {
|
2016-08-25 18:39:01 +02:00
|
|
|
return nil, traceError(err)
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
|
|
|
// Split the input buffer into data and parity blocks.
|
|
|
|
var blocks [][]byte
|
|
|
|
blocks, err = rs.Split(dataBuffer)
|
|
|
|
if err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return nil, traceError(err)
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode parity blocks using data blocks.
|
|
|
|
err = rs.Encode(blocks)
|
|
|
|
if err != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
return nil, traceError(err)
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return encoded blocks.
|
|
|
|
return blocks, nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 01:43:31 +02:00
|
|
|
// appendFile - append data buffer at path.
|
2016-07-14 23:59:01 +02:00
|
|
|
func appendFile(disks []StorageAPI, volume, path string, enBlocks [][]byte, hashWriters []hash.Hash, writeQuorum int) (err error) {
|
2016-06-02 01:43:31 +02: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 23:59:01 +02:00
|
|
|
wErr := disk.AppendFile(volume, path, enBlocks[index])
|
2016-06-02 01:43:31 +02:00
|
|
|
if wErr != nil {
|
2016-08-25 18:39:01 +02:00
|
|
|
wErrs[index] = traceError(wErr)
|
2016-06-02 01:43:31 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate hash for each blocks.
|
2016-07-14 23:59:01 +02:00
|
|
|
hashWriters[index].Write(enBlocks[index])
|
2016-06-02 01:43:31 +02:00
|
|
|
|
|
|
|
// Successfully wrote.
|
|
|
|
wErrs[index] = nil
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the appends to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-06-17 20:57:51 +02:00
|
|
|
// Do we have write quorum?.
|
2016-07-13 09:29:48 +02:00
|
|
|
if !isDiskQuorum(wErrs, writeQuorum) {
|
2016-08-25 18:39:01 +02:00
|
|
|
return traceError(errXLWriteQuorum)
|
2016-06-17 20:57:51 +02:00
|
|
|
}
|
2016-11-21 10:47:26 +01:00
|
|
|
return reduceWriteQuorumErrs(wErrs, objectOpIgnoredErrs, writeQuorum)
|
2016-06-02 01:43:31 +02:00
|
|
|
}
|