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:
Harshavardhana 2016-05-27 04:37:37 -07:00 committed by Harshavardhana
parent 302ec27fa2
commit 27cc8a6529

View File

@ -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,61 +70,73 @@ 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.
ok, err := e.ReedSolomon.Verify(enBlocks) if !noReconstruct {
if err != nil { err := e.ReedSolomon.Reconstruct(enBlocks)
pipeWriter.CloseWithError(err)
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 { if err != nil {
pipeWriter.CloseWithError(err) pipeWriter.CloseWithError(err)
return return
} }
// Verify reconstructed blocks again. // 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
@ -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.