Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
fix(arc): striped locking on last byte of CID
Browse files Browse the repository at this point in the history
- fixes #64
  • Loading branch information
frrist committed May 4, 2021
1 parent fb07d7b commit ab78397
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 additions & 3 deletions arc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blockstore

import (
"context"
"sync"

lru "github.com/hashicorp/golang-lru"
blocks "github.com/ipfs/go-block-format"
Expand All @@ -17,7 +18,9 @@ type cacheSize int
// size. This provides block access-time improvements, allowing
// to short-cut many searches without querying the underlying datastore.
type arccache struct {
cache *lru.TwoQueueCache
cache *lru.TwoQueueCache
lks [256]sync.RWMutex

blockstore Blockstore
viewer Viewer

Expand All @@ -42,11 +45,32 @@ func newARCCachedBS(ctx context.Context, bs Blockstore, lruSize int) (*arccache,
return c, nil
}

func (b *arccache) rLock(k cid.Cid) func() {
b.lks[k.KeyString()[len(k.KeyString())-1]].RLock()
return func() {
b.lks[k.KeyString()[len(k.KeyString())-1]].RUnlock()
}
}

func (b *arccache) wLock(k cid.Cid) func() {
b.lks[k.KeyString()[len(k.KeyString())-1]].Lock()
return func() {
b.lks[k.KeyString()[len(k.KeyString())-1]].Unlock()
}
}

func (b *arccache) DeleteBlock(k cid.Cid) error {
if !k.Defined() {
return ErrNotFound
}

if has, _, ok := b.queryCache(k); ok && !has {
return nil
}

unlock := b.wLock(k)
defer unlock()

b.cache.Remove(k) // Invalidate cache before deleting.
err := b.blockstore.DeleteBlock(k)
if err == nil {
Expand All @@ -56,9 +80,17 @@ func (b *arccache) DeleteBlock(k cid.Cid) error {
}

func (b *arccache) Has(k cid.Cid) (bool, error) {
if !k.Defined() {
return false, ErrNotFound
}

if has, _, ok := b.queryCache(k); ok {
return has, nil
}

unlock := b.rLock(k)
defer unlock()

has, err := b.blockstore.Has(k)
if err != nil {
return false, err
Expand All @@ -68,6 +100,10 @@ func (b *arccache) Has(k cid.Cid) (bool, error) {
}

func (b *arccache) GetSize(k cid.Cid) (int, error) {
if !k.Defined() {
return -1, ErrNotFound
}

if has, blockSize, ok := b.queryCache(k); ok {
if !has {
// don't have it, return
Expand All @@ -79,6 +115,10 @@ func (b *arccache) GetSize(k cid.Cid) (int, error) {
}
// we have it but don't know the size, ask the datastore.
}

unlock := b.rLock(k)
defer unlock()

blockSize, err := b.blockstore.GetSize(k)
if err == ErrNotFound {
b.cacheHave(k, false)
Expand All @@ -100,7 +140,6 @@ func (b *arccache) View(k cid.Cid, callback func([]byte) error) error {
}

if !k.Defined() {
log.Error("undefined cid in arc cache")
return ErrNotFound
}

Expand All @@ -110,19 +149,24 @@ func (b *arccache) View(k cid.Cid, callback func([]byte) error) error {
return ErrNotFound
}

unlock := b.rLock(k)
defer unlock()

return b.viewer.View(k, callback)
}

func (b *arccache) Get(k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
log.Error("undefined cid in arc cache")
return nil, ErrNotFound
}

if has, _, ok := b.queryCache(k); ok && !has {
return nil, ErrNotFound
}

unlock := b.rLock(k)
defer unlock()

bl, err := b.blockstore.Get(k)
if bl == nil && err == ErrNotFound {
b.cacheHave(k, false)
Expand All @@ -137,6 +181,9 @@ func (b *arccache) Put(bl blocks.Block) error {
return nil
}

unlock := b.wLock(bl.Cid())
defer unlock()

err := b.blockstore.Put(bl)
if err == nil {
b.cacheSize(bl.Cid(), len(bl.RawData()))
Expand All @@ -145,20 +192,35 @@ func (b *arccache) Put(bl blocks.Block) error {
}

func (b *arccache) PutMany(bs []blocks.Block) error {
mxs := [256]*sync.Mutex{}
var good []blocks.Block
for _, block := range bs {
// call put on block if result is inconclusive or we are sure that
// the block isn't in storage
if has, _, ok := b.queryCache(block.Cid()); !ok || (ok && !has) {
if !block.Cid().Defined() {
log.Error("undefined cid in arc cache")
continue
}

good = append(good, block)
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]] = &sync.Mutex{}
}
}

for _, mx := range mxs {
if mx != nil {
mx.Lock()
}
}

err := b.blockstore.PutMany(good)
if err != nil {
return err
}
for _, block := range good {
b.cacheSize(block.Cid(), len(block.RawData()))
mxs[block.Cid().KeyString()[len(block.Cid().KeyString())-1]].Unlock()
}
return nil
}
Expand Down

0 comments on commit ab78397

Please sign in to comment.