You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
4.3 KiB
189 lines
4.3 KiB
9 years ago
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
9 years ago
|
import (
|
||
7 years ago
|
"context"
|
||
9 years ago
|
"io"
|
||
7 years ago
|
|
||
7 years ago
|
"github.com/minio/minio/cmd/logger"
|
||
9 years ago
|
)
|
||
9 years ago
|
|
||
6 years ago
|
// Reads in parallel from bitrotReaders.
|
||
|
type parallelReader struct {
|
||
|
readers []*bitrotReader
|
||
|
dataBlocks int
|
||
|
offset int64
|
||
|
shardSize int64
|
||
|
shardFileSize int64
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// newParallelReader returns parallelReader.
|
||
|
func newParallelReader(readers []*bitrotReader, dataBlocks int, offset int64, fileSize int64, blocksize int64) *parallelReader {
|
||
|
shardSize := ceilFrac(blocksize, int64(dataBlocks))
|
||
|
shardFileSize := getErasureShardFileSize(blocksize, fileSize, dataBlocks)
|
||
|
return ¶llelReader{
|
||
|
readers,
|
||
|
dataBlocks,
|
||
|
(offset / blocksize) * shardSize,
|
||
|
shardSize,
|
||
|
shardFileSize,
|
||
7 years ago
|
}
|
||
6 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
// Returns if buf can be erasure decoded.
|
||
|
func (p *parallelReader) canDecode(buf [][]byte) bool {
|
||
|
bufCount := 0
|
||
|
for _, b := range buf {
|
||
|
if b != nil {
|
||
|
bufCount++
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
return bufCount >= p.dataBlocks
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// Read reads from bitrotReaders in parallel. Returns p.dataBlocks number of bufs.
|
||
|
func (p *parallelReader) Read() ([][]byte, error) {
|
||
|
type errIdx struct {
|
||
|
idx int
|
||
|
buf []byte
|
||
|
err error
|
||
9 years ago
|
}
|
||
|
|
||
6 years ago
|
errCh := make(chan errIdx)
|
||
|
currReaderIndex := 0
|
||
|
newBuf := make([][]byte, len(p.readers))
|
||
7 years ago
|
|
||
6 years ago
|
if p.offset+p.shardSize > p.shardFileSize {
|
||
|
p.shardSize = p.shardFileSize - p.offset
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
read := func(currReaderIndex int) {
|
||
|
b, err := p.readers[currReaderIndex].ReadChunk(p.offset, p.shardSize)
|
||
|
errCh <- errIdx{currReaderIndex, b, err}
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
readerCount := 0
|
||
|
for _, r := range p.readers {
|
||
|
if r != nil {
|
||
|
readerCount++
|
||
|
}
|
||
|
}
|
||
|
if readerCount < p.dataBlocks {
|
||
|
return nil, errXLReadQuorum
|
||
9 years ago
|
}
|
||
|
|
||
6 years ago
|
readerCount = 0
|
||
|
for i, r := range p.readers {
|
||
|
if r == nil {
|
||
|
continue
|
||
|
}
|
||
|
go read(i)
|
||
|
readerCount++
|
||
|
if readerCount == p.dataBlocks {
|
||
|
currReaderIndex = i + 1
|
||
|
break
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
for errVal := range errCh {
|
||
|
if errVal.err == nil {
|
||
|
newBuf[errVal.idx] = errVal.buf
|
||
|
if p.canDecode(newBuf) {
|
||
|
p.offset += int64(p.shardSize)
|
||
|
return newBuf, nil
|
||
7 years ago
|
}
|
||
6 years ago
|
continue
|
||
9 years ago
|
}
|
||
6 years ago
|
p.readers[errVal.idx] = nil
|
||
|
for currReaderIndex < len(p.readers) {
|
||
|
if p.readers[currReaderIndex] != nil {
|
||
|
break
|
||
7 years ago
|
}
|
||
6 years ago
|
currReaderIndex++
|
||
9 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
if currReaderIndex == len(p.readers) {
|
||
|
break
|
||
7 years ago
|
}
|
||
6 years ago
|
go read(currReaderIndex)
|
||
|
currReaderIndex++
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
return nil, errXLReadQuorum
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
// Decode reads from readers, reconstructs data if needed and writes the data to the writer.
|
||
|
func (e Erasure) Decode(ctx context.Context, writer io.Writer, readers []*bitrotReader, offset, length, totalLength int64) error {
|
||
6 years ago
|
if offset < 0 || length < 0 {
|
||
|
logger.LogIf(ctx, errInvalidArgument)
|
||
|
return errInvalidArgument
|
||
|
}
|
||
|
if offset+length > totalLength {
|
||
|
logger.LogIf(ctx, errInvalidArgument)
|
||
|
return errInvalidArgument
|
||
|
}
|
||
|
if length == 0 {
|
||
|
return nil
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
reader := newParallelReader(readers, e.dataBlocks, offset, totalLength, e.blockSize)
|
||
6 years ago
|
|
||
6 years ago
|
startBlock := offset / e.blockSize
|
||
|
endBlock := (offset + length) / e.blockSize
|
||
6 years ago
|
|
||
|
var bytesWritten int64
|
||
|
for block := startBlock; block <= endBlock; block++ {
|
||
|
var blockOffset, blockLength int64
|
||
|
switch {
|
||
|
case startBlock == endBlock:
|
||
6 years ago
|
blockOffset = offset % e.blockSize
|
||
6 years ago
|
blockLength = length
|
||
|
case block == startBlock:
|
||
6 years ago
|
blockOffset = offset % e.blockSize
|
||
|
blockLength = e.blockSize - blockOffset
|
||
6 years ago
|
case block == endBlock:
|
||
|
blockOffset = 0
|
||
6 years ago
|
blockLength = (offset + length) % e.blockSize
|
||
6 years ago
|
default:
|
||
|
blockOffset = 0
|
||
6 years ago
|
blockLength = e.blockSize
|
||
9 years ago
|
}
|
||
6 years ago
|
if blockLength == 0 {
|
||
|
break
|
||
|
}
|
||
|
bufs, err := reader.Read()
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
return err
|
||
7 years ago
|
}
|
||
6 years ago
|
if err = e.DecodeDataBlocks(bufs); err != nil {
|
||
6 years ago
|
logger.LogIf(ctx, err)
|
||
|
return err
|
||
9 years ago
|
}
|
||
6 years ago
|
n, err := writeDataBlocks(ctx, writer, bufs, e.dataBlocks, blockOffset, blockLength)
|
||
6 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
bytesWritten += n
|
||
|
}
|
||
|
if bytesWritten != length {
|
||
|
logger.LogIf(ctx, errLessData)
|
||
|
return errLessData
|
||
9 years ago
|
}
|
||
6 years ago
|
return nil
|
||
9 years ago
|
}
|