|
|
@ -17,26 +17,22 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"bytes" |
|
|
|
"encoding/hex" |
|
|
|
"encoding/hex" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
|
|
|
|
"io" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon" |
|
|
|
"github.com/klauspost/reedsolomon" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// erasureReadFile - read an entire erasure coded file at into a byte
|
|
|
|
// erasureReadFile - read bytes from erasure coded files and writes to given writer.
|
|
|
|
// array. Erasure coded parts are often few mega bytes in size and it
|
|
|
|
// Erasure coded files are read block by block as per given erasureInfo and data chunks
|
|
|
|
// is convenient to return them as byte slice. This function also
|
|
|
|
// are decoded into a data block. Data block is trimmed for given offset and length,
|
|
|
|
// supports bit-rot detection by verifying checksum of individual
|
|
|
|
// then written to given writer. This function also supports bit-rot detection by
|
|
|
|
// block's checksum.
|
|
|
|
// verifying checksum of individual block's checksum.
|
|
|
|
func erasureReadFile(disks []StorageAPI, volume string, path string, partName string, size int64, eInfos []erasureInfo) ([]byte, error) { |
|
|
|
func erasureReadFile(writer io.Writer, disks []StorageAPI, volume string, path string, partName string, eInfos []erasureInfo, offset int64, length int64) (int64, error) { |
|
|
|
// Return data buffer.
|
|
|
|
// Total bytes written to writer
|
|
|
|
var buffer []byte |
|
|
|
bytesWritten := int64(0) |
|
|
|
|
|
|
|
|
|
|
|
// Total size left
|
|
|
|
|
|
|
|
totalSizeLeft := size |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Starting offset for reading.
|
|
|
|
|
|
|
|
startOffset := int64(0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Gather previously calculated block checksums.
|
|
|
|
// Gather previously calculated block checksums.
|
|
|
|
blockCheckSums := metaPartBlockChecksums(disks, eInfos, partName) |
|
|
|
blockCheckSums := metaPartBlockChecksums(disks, eInfos, partName) |
|
|
@ -44,20 +40,10 @@ func erasureReadFile(disks []StorageAPI, volume string, path string, partName st |
|
|
|
// Pick one erasure info.
|
|
|
|
// Pick one erasure info.
|
|
|
|
eInfo := pickValidErasureInfo(eInfos) |
|
|
|
eInfo := pickValidErasureInfo(eInfos) |
|
|
|
|
|
|
|
|
|
|
|
// Write until each parts are read and exhausted.
|
|
|
|
// Get block info for given offset, length and block size.
|
|
|
|
for totalSizeLeft > 0 { |
|
|
|
startBlock, bytesToSkip, endBlock := getBlockInfo(offset, length, eInfo.BlockSize) |
|
|
|
// Calculate the proper block size.
|
|
|
|
|
|
|
|
var curBlockSize int64 |
|
|
|
|
|
|
|
if eInfo.BlockSize < totalSizeLeft { |
|
|
|
|
|
|
|
curBlockSize = eInfo.BlockSize |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
curBlockSize = totalSizeLeft |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate the current encoded block size.
|
|
|
|
|
|
|
|
curEncBlockSize := getEncodedBlockLen(curBlockSize, eInfo.DataBlocks) |
|
|
|
|
|
|
|
offsetEncOffset := getEncodedBlockLen(startOffset, eInfo.DataBlocks) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for block := startBlock; block <= endBlock; block++ { |
|
|
|
// Allocate encoded blocks up to storage disks.
|
|
|
|
// Allocate encoded blocks up to storage disks.
|
|
|
|
enBlocks := make([][]byte, len(disks)) |
|
|
|
enBlocks := make([][]byte, len(disks)) |
|
|
|
|
|
|
|
|
|
|
@ -65,6 +51,10 @@ func erasureReadFile(disks []StorageAPI, volume string, path string, partName st |
|
|
|
var successDataBlocksCount = 0 |
|
|
|
var successDataBlocksCount = 0 |
|
|
|
var noReconstruct bool // Set for no reconstruction.
|
|
|
|
var noReconstruct bool // Set for no reconstruction.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keep how many bytes are read for this block.
|
|
|
|
|
|
|
|
// In most cases, last block in the file is shorter than eInfo.BlockSize.
|
|
|
|
|
|
|
|
lastReadSize := int64(0) |
|
|
|
|
|
|
|
|
|
|
|
// Read from all the disks.
|
|
|
|
// Read from all the disks.
|
|
|
|
for index, disk := range disks { |
|
|
|
for index, disk := range disks { |
|
|
|
blockIndex := eInfo.Distribution[index] - 1 |
|
|
|
blockIndex := eInfo.Distribution[index] - 1 |
|
|
@ -74,13 +64,29 @@ func erasureReadFile(disks []StorageAPI, volume string, path string, partName st |
|
|
|
if disk == nil { |
|
|
|
if disk == nil { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
// Initialize shard slice and fill the data from each parts.
|
|
|
|
|
|
|
|
enBlocks[blockIndex] = make([]byte, curEncBlockSize) |
|
|
|
// Initialize chunk slice and fill the data from each parts.
|
|
|
|
|
|
|
|
enBlocks[blockIndex] = make([]byte, eInfo.BlockSize) |
|
|
|
|
|
|
|
|
|
|
|
// Read the necessary blocks.
|
|
|
|
// Read the necessary blocks.
|
|
|
|
_, err := disk.ReadFile(volume, path, offsetEncOffset, enBlocks[blockIndex]) |
|
|
|
n, err := disk.ReadFile(volume, path, block*eInfo.BlockSize, enBlocks[blockIndex]) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
enBlocks[blockIndex] = nil |
|
|
|
enBlocks[blockIndex] = nil |
|
|
|
|
|
|
|
} else if n < eInfo.BlockSize { |
|
|
|
|
|
|
|
// As the data we got is smaller than eInfo.BlockSize, keep only required chunk slice
|
|
|
|
|
|
|
|
enBlocks[blockIndex] = append([]byte{}, enBlocks[blockIndex][:n]...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remember bytes read at first time.
|
|
|
|
|
|
|
|
if lastReadSize == 0 { |
|
|
|
|
|
|
|
lastReadSize = n |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If bytes read is not equal to bytes read lastly, treat it as corrupted chunk.
|
|
|
|
|
|
|
|
if n != lastReadSize { |
|
|
|
|
|
|
|
return bytesWritten, errXLDataCorrupt |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Verify if we have successfully read all the data blocks.
|
|
|
|
// Verify if we have successfully read all the data blocks.
|
|
|
|
if blockIndex < eInfo.DataBlocks && enBlocks[blockIndex] != nil { |
|
|
|
if blockIndex < eInfo.DataBlocks && enBlocks[blockIndex] != nil { |
|
|
|
successDataBlocksCount++ |
|
|
|
successDataBlocksCount++ |
|
|
@ -95,38 +101,43 @@ func erasureReadFile(disks []StorageAPI, volume string, path string, partName st |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Check blocks if they are all zero in length, we have corruption return error.
|
|
|
|
|
|
|
|
if checkBlockSize(enBlocks) == 0 { |
|
|
|
|
|
|
|
return nil, errXLDataCorrupt |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify if reconstruction is needed, proceed with reconstruction.
|
|
|
|
// Verify if reconstruction is needed, proceed with reconstruction.
|
|
|
|
if !noReconstruct { |
|
|
|
if !noReconstruct { |
|
|
|
err := decodeData(enBlocks, eInfo.DataBlocks, eInfo.ParityBlocks) |
|
|
|
err := decodeData(enBlocks, eInfo.DataBlocks, eInfo.ParityBlocks) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return bytesWritten, err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get data blocks from encoded blocks.
|
|
|
|
// Get data blocks from encoded blocks.
|
|
|
|
dataBlocks, err := getDataBlocks(enBlocks, eInfo.DataBlocks, int(curBlockSize)) |
|
|
|
dataBlocks, err := getDataBlocks(enBlocks, eInfo.DataBlocks, int(lastReadSize)*eInfo.DataBlocks) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return bytesWritten, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Copy data blocks.
|
|
|
|
// Keep required bytes into buf.
|
|
|
|
buffer = append(buffer, dataBlocks...) |
|
|
|
buf := dataBlocks |
|
|
|
|
|
|
|
|
|
|
|
// Negate the 'n' size written to client.
|
|
|
|
// If this is start block, skip unwanted bytes.
|
|
|
|
totalSizeLeft -= int64(len(dataBlocks)) |
|
|
|
if block == startBlock { |
|
|
|
|
|
|
|
buf = append([]byte{}, dataBlocks[bytesToSkip:]...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Increase the offset to move forward.
|
|
|
|
// If this is end block, retain only required bytes.
|
|
|
|
startOffset += int64(len(dataBlocks)) |
|
|
|
if block == endBlock { |
|
|
|
|
|
|
|
buf = append([]byte{}, buf[:length-bytesWritten]...) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Relenquish memory.
|
|
|
|
// Copy data blocks.
|
|
|
|
dataBlocks = nil |
|
|
|
var n int64 |
|
|
|
|
|
|
|
n, err = io.Copy(writer, bytes.NewReader(buf)) |
|
|
|
|
|
|
|
bytesWritten += int64(n) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return bytesWritten, err |
|
|
|
} |
|
|
|
} |
|
|
|
return buffer, nil |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bytesWritten, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// PartObjectChecksum - returns the checksum for the part name from the checksum slice.
|
|
|
|
// PartObjectChecksum - returns the checksum for the part name from the checksum slice.
|
|
|
|