Make erasure Encode and Decode atomic to avoid races

This commit is contained in:
Harshavardhana 2015-10-06 22:17:52 -07:00
parent e6d935731a
commit ab5ea997ab
3 changed files with 13 additions and 28 deletions

View File

@ -38,6 +38,9 @@ import (
// //
// "dataLen" is the length of original source data // "dataLen" is the length of original source data
func (e *Erasure) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData []byte, err error) { func (e *Erasure) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData []byte, err error) {
e.mutex.Lock()
defer e.mutex.Unlock()
var source, target **C.uchar var source, target **C.uchar
k := int(e.params.K) k := int(e.params.K)

View File

@ -22,6 +22,7 @@ package erasure
import "C" import "C"
import ( import (
"errors" "errors"
"sync"
"unsafe" "unsafe"
) )
@ -42,6 +43,7 @@ type Erasure struct {
encodeMatrix, encodeTbls *C.uchar encodeMatrix, encodeTbls *C.uchar
decodeMatrix, decodeTbls *C.uchar decodeMatrix, decodeTbls *C.uchar
decodeIndex *C.uint32_t decodeIndex *C.uint32_t
mutex *sync.Mutex
} }
// ValidateParams creates an Params object. // ValidateParams creates an Params object.
@ -85,6 +87,7 @@ func NewErasure(ep *Params) *Erasure {
decodeMatrix: nil, decodeMatrix: nil,
decodeTbls: nil, decodeTbls: nil,
decodeIndex: nil, decodeIndex: nil,
mutex: new(sync.Mutex),
} }
} }
@ -110,6 +113,9 @@ func GetEncodedBlockLen(inputLen int, k uint8) (encodedOutputLen int) {
// Encode erasure codes a block of data in "k" data blocks and "m" parity blocks. // Encode erasure codes a block of data in "k" data blocks and "m" parity blocks.
// Output is [k+m][]blocks of data and parity slices. // Output is [k+m][]blocks of data and parity slices.
func (e *Erasure) Encode(inputData []byte) (encodedBlocks [][]byte, err error) { func (e *Erasure) Encode(inputData []byte) (encodedBlocks [][]byte, err error) {
e.mutex.Lock()
defer e.mutex.Unlock()
k := int(e.params.K) // "k" data blocks k := int(e.params.K) // "k" data blocks
m := int(e.params.M) // "m" parity blocks m := int(e.params.M) // "m" parity blocks
n := k + m // "n" total encoded blocks n := k + m // "n" total encoded blocks

View File

@ -42,7 +42,8 @@ func corruptChunks(chunks [][]byte, errorIndex []int) [][]byte {
} }
func (s *MySuite) TestEncodeDecodeFailure(c *C) { func (s *MySuite) TestEncodeDecodeFailure(c *C) {
ep, _ := ValidateParams(k, m) ep, err := ValidateParams(k, m)
c.Assert(err, IsNil)
data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
@ -58,7 +59,8 @@ func (s *MySuite) TestEncodeDecodeFailure(c *C) {
} }
func (s *MySuite) TestEncodeDecodeSuccess(c *C) { func (s *MySuite) TestEncodeDecodeSuccess(c *C) {
ep, _ := ValidateParams(k, m) ep, err := ValidateParams(k, m)
c.Assert(err, IsNil)
data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.") data := []byte("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
@ -76,29 +78,3 @@ func (s *MySuite) TestEncodeDecodeSuccess(c *C) {
c.Fatalf("Recovered data mismatches with original data") c.Fatalf("Recovered data mismatches with original data")
} }
} }
/*
func (s *MySuite) TestEncodeDecodeSuccessBuffer(c *C) {
ep, _ := ValidateParams(k, m)
tmpBuffer := new(bytes.Buffer)
for i := 0; i < 1024*1024; i++ {
tmpBuffer.Write([]byte("Hello world, hello world"))
}
e := NewErasure(ep)
chunks, err := e.Encode(tmpBuffer.Bytes())
c.Assert(err, IsNil)
errorIndex := []int{0, 3, 5, 9, 13}
chunks = corruptChunks(chunks, errorIndex)
recoveredData, err := e.Decode(chunks, len(tmpBuffer.Bytes()))
c.Assert(err, IsNil)
if !bytes.Equal(tmpBuffer.Bytes(), recoveredData) {
c.Fatalf("Recovered data mismatches with original data")
}
}
*/