Skip to content

Commit

Permalink
incr insdie lcache to avoid race condition, pl being evicted before c…
Browse files Browse the repository at this point in the history
…alling incr. Fixes #1171
  • Loading branch information
Janardhan Reddy committed Jul 11, 2017
1 parent 686d8f8 commit 2e48afc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
8 changes: 2 additions & 6 deletions posting/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ func GetOrCreate(key []byte, group uint32) (rlist *List, decr func()) {
lp := lcache.Get(fp)
if lp != nil {
x.CacheHit.Add(1)
lp.incr()
return lp, lp.decr
}
x.CacheMiss.Add(1)
Expand All @@ -275,12 +274,10 @@ func GetOrCreate(key []byte, group uint32) (rlist *List, decr func()) {
l := getNew(key, pstore) // This retrieves a new *List and sets refcount to 1.
l.water = marks.Get(group)

// We are always going to return lp to caller, whether it is l or not
// lcache increments the ref counter
lp = lcache.PutIfMissing(fp, l)

// We are always going to return lp to caller, whether it is l or not. So, let's
// increment its reference counter.
lp.incr()

if lp != l {
x.CacheRace.Add(1)
// Undo the increment in getNew() call above.
Expand All @@ -303,7 +300,6 @@ func Get(key []byte, gid uint32) (rlist *List, decr func()) {
lp := lcache.Get(fp)

if lp != nil {
lp.incr()
return lp, lp.decr
}

Expand Down
3 changes: 3 additions & 0 deletions posting/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (c *listCache) PutIfMissing(key uint64, pl *List) (res *List) {
if ee, ok := c.cache[key]; ok {
c.ll.MoveToFront(ee)
res = ee.Value.(*entry).pl
res.incr()
return res
}

Expand All @@ -92,6 +93,7 @@ func (c *listCache) PutIfMissing(key uint64, pl *List) (res *List) {
c.cache[key] = ele
c.removeOldest()

e.pl.incr()
return e.pl
}

Expand Down Expand Up @@ -127,6 +129,7 @@ func (c *listCache) Get(key uint64) (pl *List) {
est := uint64(e.pl.EstimatedSize())
c.curSize += est - e.size
e.size = est
e.pl.incr()
return e.pl
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion x/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {
DirtyMapSize = expvar.NewInt("dirtyMapSize")
LCacheSize = expvar.NewInt("lcacheSize")
LCacheLen = expvar.NewInt("lcacheLen")
lCacheCapacity = expvar.NewInt("lCacheCapacity")
LCacheCapacity = expvar.NewInt("lCacheCapacity")
NumGoRoutines = expvar.NewInt("numGoRoutines")
MemoryInUse = expvar.NewInt("memoryInUse")
HeapIdle = expvar.NewInt("heapIdle")
Expand Down

0 comments on commit 2e48afc

Please sign in to comment.