From ab5ea997ab4ccf9c2033bd523514feb648c5d33e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 6 Oct 2015 22:17:52 -0700 Subject: [PATCH] Make erasure Encode and Decode atomic to avoid races --- pkg/erasure/erasure_decode.go | 3 +++ pkg/erasure/erasure_encode.go | 6 ++++++ pkg/erasure/erasure_test.go | 32 ++++---------------------------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/pkg/erasure/erasure_decode.go b/pkg/erasure/erasure_decode.go index ee7dcac5b..20523f7b0 100644 --- a/pkg/erasure/erasure_decode.go +++ b/pkg/erasure/erasure_decode.go @@ -38,6 +38,9 @@ import ( // // "dataLen" is the length of original source data func (e *Erasure) Decode(encodedDataBlocks [][]byte, dataLen int) (decodedData []byte, err error) { + e.mutex.Lock() + defer e.mutex.Unlock() + var source, target **C.uchar k := int(e.params.K) diff --git a/pkg/erasure/erasure_encode.go b/pkg/erasure/erasure_encode.go index 8a5a92089..752e5a2a7 100644 --- a/pkg/erasure/erasure_encode.go +++ b/pkg/erasure/erasure_encode.go @@ -22,6 +22,7 @@ package erasure import "C" import ( "errors" + "sync" "unsafe" ) @@ -42,6 +43,7 @@ type Erasure struct { encodeMatrix, encodeTbls *C.uchar decodeMatrix, decodeTbls *C.uchar decodeIndex *C.uint32_t + mutex *sync.Mutex } // ValidateParams creates an Params object. @@ -85,6 +87,7 @@ func NewErasure(ep *Params) *Erasure { decodeMatrix: nil, decodeTbls: 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. // Output is [k+m][]blocks of data and parity slices. 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 m := int(e.params.M) // "m" parity blocks n := k + m // "n" total encoded blocks diff --git a/pkg/erasure/erasure_test.go b/pkg/erasure/erasure_test.go index 85c19ca9b..0ec5d9821 100644 --- a/pkg/erasure/erasure_test.go +++ b/pkg/erasure/erasure_test.go @@ -42,7 +42,8 @@ func corruptChunks(chunks [][]byte, errorIndex []int) [][]byte { } 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.") @@ -58,7 +59,8 @@ func (s *MySuite) TestEncodeDecodeFailure(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.") @@ -76,29 +78,3 @@ func (s *MySuite) TestEncodeDecodeSuccess(c *C) { 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") - } - -} -*/