mirror of
https://github.com/minio/minio.git
synced 2025-01-12 07:23:23 -05:00
erasure: read only dataBlocks if we have enough. (#1776)
Reconstruct with parity blocks if we don't have enough data blocks.
This commit is contained in:
parent
302ec27fa2
commit
27cc8a6529
@ -44,11 +44,12 @@ func (e erasure) ReadFile(volume, path string, startOffset int64, totalSize int6
|
|||||||
go func(index int, disk StorageAPI) {
|
go func(index int, disk StorageAPI) {
|
||||||
defer rwg.Done()
|
defer rwg.Done()
|
||||||
offset := int64(0)
|
offset := int64(0)
|
||||||
if reader, err := disk.ReadFile(volume, path, offset); err == nil {
|
reader, err := disk.ReadFile(volume, path, offset)
|
||||||
|
if err == nil {
|
||||||
readers[index] = reader
|
readers[index] = reader
|
||||||
} else {
|
return
|
||||||
errs[index] = err
|
|
||||||
}
|
}
|
||||||
|
errs[index] = err
|
||||||
}(index, disk)
|
}(index, disk)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,65 +70,77 @@ func (e erasure) ReadFile(volume, path string, startOffset int64, totalSize int6
|
|||||||
var totalLeft = totalSize
|
var totalLeft = totalSize
|
||||||
// Read until EOF.
|
// Read until EOF.
|
||||||
for totalLeft > 0 {
|
for totalLeft > 0 {
|
||||||
// Figure out the right blockSize as it was encoded
|
// Figure out the right blockSize as it was encoded before.
|
||||||
// before.
|
|
||||||
var curBlockSize int64
|
var curBlockSize int64
|
||||||
if erasureBlockSize < totalLeft {
|
if erasureBlockSize < totalLeft {
|
||||||
curBlockSize = erasureBlockSize
|
curBlockSize = erasureBlockSize
|
||||||
} else {
|
} else {
|
||||||
curBlockSize = totalLeft
|
curBlockSize = totalLeft
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the current encoded block size.
|
// Calculate the current encoded block size.
|
||||||
curEncBlockSize := getEncodedBlockLen(curBlockSize, e.DataBlocks)
|
curEncBlockSize := getEncodedBlockLen(curBlockSize, e.DataBlocks)
|
||||||
|
|
||||||
|
// Allocate encoded blocks up to storage disks.
|
||||||
enBlocks := make([][]byte, len(e.storageDisks))
|
enBlocks := make([][]byte, len(e.storageDisks))
|
||||||
|
|
||||||
|
// Counter to keep success data blocks.
|
||||||
|
var successDataBlocksCount = 0
|
||||||
|
var noReconstruct bool // Set for no reconstruction.
|
||||||
|
|
||||||
// Read all the readers.
|
// Read all the readers.
|
||||||
for index, reader := range readers {
|
for index, reader := range readers {
|
||||||
blockIndex := e.distribution[index] - 1
|
blockIndex := e.distribution[index] - 1
|
||||||
// Initialize shard slice and fill the data from each parts.
|
// Initialize shard slice and fill the data from each parts.
|
||||||
enBlocks[blockIndex] = make([]byte, curEncBlockSize)
|
enBlocks[blockIndex] = make([]byte, curEncBlockSize)
|
||||||
if reader == nil {
|
if reader == nil {
|
||||||
|
enBlocks[blockIndex] = nil
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the reader when routine returns.
|
||||||
|
defer reader.Close()
|
||||||
|
|
||||||
// Read the necessary blocks.
|
// Read the necessary blocks.
|
||||||
_, rErr := io.ReadFull(reader, enBlocks[blockIndex])
|
_, rErr := io.ReadFull(reader, enBlocks[blockIndex])
|
||||||
if rErr != nil && rErr != io.ErrUnexpectedEOF {
|
if rErr != nil && rErr != io.ErrUnexpectedEOF {
|
||||||
readers[index].Close()
|
enBlocks[blockIndex] = nil
|
||||||
readers[index] = nil
|
}
|
||||||
|
|
||||||
|
// Verify if we have successfully all the data blocks.
|
||||||
|
if blockIndex < e.DataBlocks {
|
||||||
|
successDataBlocksCount++
|
||||||
|
// Set when we have all the data blocks and no
|
||||||
|
// reconstruction is needed, so that we can avoid
|
||||||
|
// erasure reconstruction.
|
||||||
|
noReconstruct = successDataBlocksCount == e.DataBlocks
|
||||||
|
if noReconstruct {
|
||||||
|
// Break out we have read all the data blocks.
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check blocks if they are all zero in length.
|
// Check blocks if they are all zero in length, we have
|
||||||
|
// corruption return error.
|
||||||
if checkBlockSize(enBlocks) == 0 {
|
if checkBlockSize(enBlocks) == 0 {
|
||||||
pipeWriter.CloseWithError(errDataCorrupt)
|
pipeWriter.CloseWithError(errDataCorrupt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the blocks.
|
// Verify if reconstruction is needed, proceed with reconstruction.
|
||||||
|
if !noReconstruct {
|
||||||
|
err := e.ReedSolomon.Reconstruct(enBlocks)
|
||||||
|
if err != nil {
|
||||||
|
pipeWriter.CloseWithError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Verify reconstructed blocks (parity).
|
||||||
ok, err := e.ReedSolomon.Verify(enBlocks)
|
ok, err := e.ReedSolomon.Verify(enBlocks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipeWriter.CloseWithError(err)
|
pipeWriter.CloseWithError(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verification failed, blocks require reconstruction.
|
|
||||||
if !ok {
|
|
||||||
for index, reader := range readers {
|
|
||||||
if reader == nil {
|
|
||||||
// Reconstruct expects missing blocks to be nil.
|
|
||||||
enBlocks[index] = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = e.ReedSolomon.Reconstruct(enBlocks)
|
|
||||||
if err != nil {
|
|
||||||
pipeWriter.CloseWithError(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Verify reconstructed blocks again.
|
|
||||||
ok, err = e.ReedSolomon.Verify(enBlocks)
|
|
||||||
if err != nil {
|
|
||||||
pipeWriter.CloseWithError(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !ok {
|
if !ok {
|
||||||
// Blocks cannot be reconstructed, corrupted data.
|
// Blocks cannot be reconstructed, corrupted data.
|
||||||
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
||||||
@ -136,7 +149,7 @@ func (e erasure) ReadFile(volume, path string, startOffset int64, totalSize int6
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get all the data blocks.
|
// Get data blocks from encoded blocks.
|
||||||
dataBlocks := getDataBlocks(enBlocks, e.DataBlocks, int(curBlockSize))
|
dataBlocks := getDataBlocks(enBlocks, e.DataBlocks, int(curBlockSize))
|
||||||
|
|
||||||
// Verify if the offset is right for the block, if not move to the next block.
|
// Verify if the offset is right for the block, if not move to the next block.
|
||||||
@ -151,8 +164,8 @@ func (e erasure) ReadFile(volume, path string, startOffset int64, totalSize int6
|
|||||||
startOffset = startOffset + int64(len(dataBlocks))
|
startOffset = startOffset + int64(len(dataBlocks))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write safely the necessary blocks.
|
// Write safely the necessary blocks to the pipe.
|
||||||
_, err = pipeWriter.Write(dataBlocks[int(startOffset):])
|
_, err := pipeWriter.Write(dataBlocks[int(startOffset):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipeWriter.CloseWithError(err)
|
pipeWriter.CloseWithError(err)
|
||||||
return
|
return
|
||||||
@ -170,14 +183,6 @@ func (e erasure) ReadFile(volume, path string, startOffset int64, totalSize int6
|
|||||||
|
|
||||||
// Cleanly end the pipe after a successful decoding.
|
// Cleanly end the pipe after a successful decoding.
|
||||||
pipeWriter.Close()
|
pipeWriter.Close()
|
||||||
|
|
||||||
// Cleanly close all the underlying data readers.
|
|
||||||
for _, reader := range readers {
|
|
||||||
if reader == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
reader.Close()
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Return the pipe for the top level caller to start reading.
|
// Return the pipe for the top level caller to start reading.
|
||||||
|
Loading…
Reference in New Issue
Block a user