correct cache: fix racing condition

This commit is contained in:
chrislusf 2016-04-17 12:03:45 -07:00
parent 6dd257a81f
commit adcfaa5735
2 changed files with 7 additions and 9 deletions

View File

@ -45,21 +45,24 @@ func (block *Block) increaseReference() {
// get bytes from the LRU cache of []byte first, then from the bytes pool // get bytes from the LRU cache of []byte first, then from the bytes pool
// when []byte in LRU cache is evicted, it will be put back to the bytes pool // when []byte in LRU cache is evicted, it will be put back to the bytes pool
func getBytesForFileBlock(r *os.File, offset int64, readSize int) (block *Block, isNew bool) { func getBytesForFileBlock(r *os.File, offset int64, readSize int) (dataSlice []byte, block *Block, err error) {
// check cache, return if found // check cache, return if found
cacheKey := fmt.Sprintf("%d:%d:%d", r.Fd(), offset>>3, readSize) cacheKey := fmt.Sprintf("%d:%d:%d", r.Fd(), offset>>3, readSize)
if obj, found := bytesCache.Get(cacheKey); found { if obj, found := bytesCache.Get(cacheKey); found {
block = obj.(*Block) block = obj.(*Block)
block.increaseReference() block.increaseReference()
return block, false dataSlice = block.Bytes[0:readSize]
return dataSlice, block, nil
} }
// get the []byte from pool // get the []byte from pool
b := bytesPool.Get(readSize) b := bytesPool.Get(readSize)
// refCount = 2, one by the bytesCache, one by the actual needle object // refCount = 2, one by the bytesCache, one by the actual needle object
block = &Block{Bytes: b, refCount: 2} block = &Block{Bytes: b, refCount: 2}
dataSlice = block.Bytes[0:readSize]
_, err = r.ReadAt(dataSlice, offset)
bytesCache.Add(cacheKey, block) bytesCache.Add(cacheKey, block)
return block, true return dataSlice, block, err
} }
func (n *Needle) ReleaseMemory() { func (n *Needle) ReleaseMemory() {

View File

@ -139,12 +139,7 @@ func (n *Needle) Append(w io.Writer, version Version) (size uint32, err error) {
func ReadNeedleBlob(r *os.File, offset int64, size uint32) (dataSlice []byte, block *Block, err error) { func ReadNeedleBlob(r *os.File, offset int64, size uint32) (dataSlice []byte, block *Block, err error) {
padding := NeedlePaddingSize - ((NeedleHeaderSize + size + NeedleChecksumSize) % NeedlePaddingSize) padding := NeedlePaddingSize - ((NeedleHeaderSize + size + NeedleChecksumSize) % NeedlePaddingSize)
readSize := NeedleHeaderSize + size + NeedleChecksumSize + padding readSize := NeedleHeaderSize + size + NeedleChecksumSize + padding
block, isNew := getBytesForFileBlock(r, offset, int(readSize)) return getBytesForFileBlock(r, offset, int(readSize))
dataSlice = block.Bytes[0:int(readSize)]
if isNew {
_, err = r.ReadAt(dataSlice, offset)
}
return dataSlice, block, err
} }
func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) { func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) {