diff options
| author | chrislusf <chrislu@Chriss-MacBook-Air.local> | 2016-04-17 12:03:45 -0700 |
|---|---|---|
| committer | chrislusf <chrislu@Chriss-MacBook-Air.local> | 2016-04-17 12:03:45 -0700 |
| commit | adcfaa5735e1bb7330b35a054e2b061c614cbcb3 (patch) | |
| tree | 65bb0d4f2575d2d1a260ab603825641a5635b897 /go/storage/needle_byte_cache.go | |
| parent | 6dd257a81f3419ba5da4cc61919ae5c6fcd6bdb3 (diff) | |
| download | seaweedfs-adcfaa5735e1bb7330b35a054e2b061c614cbcb3.tar.xz seaweedfs-adcfaa5735e1bb7330b35a054e2b061c614cbcb3.zip | |
correct cache: fix racing condition
Diffstat (limited to 'go/storage/needle_byte_cache.go')
| -rw-r--r-- | go/storage/needle_byte_cache.go | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/go/storage/needle_byte_cache.go b/go/storage/needle_byte_cache.go index 24a72e244..d39ed23c4 100644 --- a/go/storage/needle_byte_cache.go +++ b/go/storage/needle_byte_cache.go @@ -45,21 +45,24 @@ func (block *Block) increaseReference() { // 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 -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 cacheKey := fmt.Sprintf("%d:%d:%d", r.Fd(), offset>>3, readSize) if obj, found := bytesCache.Get(cacheKey); found { block = obj.(*Block) block.increaseReference() - return block, false + dataSlice = block.Bytes[0:readSize] + return dataSlice, block, nil } // get the []byte from pool b := bytesPool.Get(readSize) // refCount = 2, one by the bytesCache, one by the actual needle object block = &Block{Bytes: b, refCount: 2} + dataSlice = block.Bytes[0:readSize] + _, err = r.ReadAt(dataSlice, offset) bytesCache.Add(cacheKey, block) - return block, true + return dataSlice, block, err } func (n *Needle) ReleaseMemory() { |
