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 4eeefc4
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion 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,33 @@ 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() {
log.Error("undefined cid in arc cache")
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 +81,18 @@ func (b *arccache) DeleteBlock(k cid.Cid) error {
}

func (b *arccache) Has(k cid.Cid) (bool, error) {
if !k.Defined() {
log.Error("undefined cid in arc cache")
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 +102,11 @@ func (b *arccache) Has(k cid.Cid) (bool, error) {
}

func (b *arccache) GetSize(k cid.Cid) (int, error) {
if !k.Defined() {
log.Error("undefined cid in arc cache")
return -1, ErrNotFound
}

if has, blockSize, ok := b.queryCache(k); ok {
if !has {
// don't have it, return
Expand All @@ -79,6 +118,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 Down Expand Up @@ -110,6 +153,9 @@ 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)
}

Expand All @@ -123,6 +169,9 @@ func (b *arccache) Get(k cid.Cid) (blocks.Block, error) {
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 +186,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 +197,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 4eeefc4

Please sign in to comment.