Skip to content

Commit

Permalink
fifocache: ensure Unlock/RUnlock will be called if panic occurs (#19615)
Browse files Browse the repository at this point in the history
ensure Unlock/RUnlock will be called if panic occurs

Approved by: @fengttt
  • Loading branch information
reusee authored Oct 28, 2024
1 parent 1ef31ac commit 5c6909f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
24 changes: 14 additions & 10 deletions pkg/fileservice/fifocache/data_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ func (d *DataCache) Capacity() int64 {
func (d *DataCache) DeletePaths(ctx context.Context, paths []string) {
for _, path := range paths {
for i := 0; i < len(d.fifo.shards); i++ {
shard := &d.fifo.shards[i]
shard.Lock()
for key, item := range shard.values {
if key.Path == path {
delete(shard.values, key)
if d.fifo.postEvict != nil {
d.fifo.postEvict(item.key, item.value)
}
}
d.deletePath(i, path)
}
}
}

func (d *DataCache) deletePath(shardIndex int, path string) {
shard := &d.fifo.shards[shardIndex]
shard.Lock()
defer shard.Unlock()
for key, item := range shard.values {
if key.Path == path {
delete(shard.values, key)
if d.fifo.postEvict != nil {
d.fifo.postEvict(item.key, item.value)
}
shard.Unlock()
}
}
}
Expand Down
51 changes: 27 additions & 24 deletions pkg/fileservice/fifocache/fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ func New[K comparable, V any](
return ret
}

func (c *Cache[K, V]) Set(key K, value V, size int64) {
func (c *Cache[K, V]) set(key K, value V, size int64) *_CacheItem[K, V] {
shard := &c.shards[c.keyShardFunc(key)]
shard.Lock()
defer shard.Unlock()
_, ok := shard.values[key]
if ok {
// existed
shard.Unlock()
return
return nil
}

item := &_CacheItem[K, V]{
Expand All @@ -121,14 +121,19 @@ func (c *Cache[K, V]) Set(key K, value V, size int64) {
if c.postSet != nil {
c.postSet(key, value)
}
shard.Unlock()

c.queueLock.Lock()
defer c.queueLock.Unlock()
c.queue1.enqueue(item)
c.used1 += size
if c.used1+c.used2 > c.capacity() {
c.evict(nil, 0)
return item
}

func (c *Cache[K, V]) Set(key K, value V, size int64) {
if item := c.set(key, value, size); item != nil {
c.queueLock.Lock()
defer c.queueLock.Unlock()
c.queue1.enqueue(item)
c.used1 += size
if c.used1+c.used2 > c.capacity() {
c.evict(nil, 0)
}
}
}

Expand Down Expand Up @@ -205,19 +210,23 @@ func (c *Cache[K, V]) evict1() {
c.used2 += item.size
} else {
// evict
shard := &c.shards[c.keyShardFunc(item.key)]
shard.Lock()
delete(shard.values, item.key)
if c.postEvict != nil {
c.postEvict(item.key, item.value)
}
shard.Unlock()
c.deleteItem(item)
c.used1 -= item.size
return
}
}
}

func (c *Cache[K, V]) deleteItem(item *_CacheItem[K, V]) {
shard := &c.shards[c.keyShardFunc(item.key)]
shard.Lock()
defer shard.Unlock()
delete(shard.values, item.key)
if c.postEvict != nil {
c.postEvict(item.key, item.value)
}
}

func (c *Cache[K, V]) evict2() {
// queue 2
for {
Expand All @@ -232,13 +241,7 @@ func (c *Cache[K, V]) evict2() {
item.dec()
} else {
// evict
shard := &c.shards[c.keyShardFunc(item.key)]
shard.Lock()
delete(shard.values, item.key)
if c.postEvict != nil {
c.postEvict(item.key, item.value)
}
shard.Unlock()
c.deleteItem(item)
c.used2 -= item.size
return
}
Expand Down

0 comments on commit 5c6909f

Please sign in to comment.