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

correctly convert the datastore not found errors #10

Merged
merged 2 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions arc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
lru "github.com/hashicorp/golang-lru"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
metrics "github.com/ipfs/go-metrics-interface"
)

type cacheHave bool
type cacheSize int

// arccache wraps a BlockStore with an Adaptive Replacement Cache (ARC) for
// block Cids. This provides block access-time improvements, allowing
// to short-cut many searches without query-ing the underlying datastore.
Expand Down Expand Up @@ -41,8 +43,8 @@ func (b *arccache) DeleteBlock(k *cid.Cid) error {
b.arc.Remove(k) // Invalidate cache before deleting.
err := b.blockstore.DeleteBlock(k)
switch err {
case nil, ds.ErrNotFound, ErrNotFound:
b.addCache(k, -1)
case nil, ErrNotFound:
b.cacheHave(k, false)
return err
default:
return err
Expand All @@ -63,32 +65,37 @@ func (b *arccache) hasCached(k *cid.Cid) (has bool, size int, ok bool) {
h, ok := b.arc.Get(k.KeyString())
if ok {
b.hits.Inc()
if h.(int) > -1 {
return true, h.(int), true
} else {
return false, h.(int), true
switch h := h.(type) {
case cacheHave:
return bool(h), -1, true
case cacheSize:
return true, int(h), true
}
}
return false, -1, false
}

func (b *arccache) Has(k *cid.Cid) (bool, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taylormike

I rushed this change through as I ended up leaving a bunch of packages semi-broken (due to ipfs/go-datastore#92).

Before, arccache.Has would always call GetSize on the underlying datastore. Now, it calls Has unless someone as already called GetSize or Get (to avoid the overhead of actually getting the value). Will this change interfere with your bitswap changes?

Copy link
Contributor

@taylormike taylormike Aug 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Stebalien
No, this will not interfere with my bitswap changes. I tested both your recent changes and my bitswap changes together. They both look good.
Thank you for your help and the quick turnaround.

blockSize, err := b.GetSize(k)
if err == ds.ErrNotFound {
return false, nil
if has, _, ok := b.hasCached(k); ok {
return has, nil
}
has, err := b.blockstore.Has(k)
if err != nil {
return false, err
}
return blockSize > -1, err
b.cacheHave(k, has)
return has, nil
}

func (b *arccache) GetSize(k *cid.Cid) (int, error) {
if _, blockSize, ok := b.hasCached(k); ok {
return blockSize, nil
}
blockSize, err := b.blockstore.GetSize(k)
if err == ds.ErrNotFound {
b.addCache(k, -1)
if err == ErrNotFound {
b.cacheHave(k, false)
} else if err == nil {
b.addCache(k, blockSize)
b.cacheSize(k, blockSize)
}
return blockSize, err
}
Expand All @@ -105,9 +112,9 @@ func (b *arccache) Get(k *cid.Cid) (blocks.Block, error) {

bl, err := b.blockstore.Get(k)
if bl == nil && err == ErrNotFound {
b.addCache(k, -1)
b.cacheHave(k, false)
} else if bl != nil {
b.addCache(k, len(bl.RawData()))
b.cacheSize(k, len(bl.RawData()))
}
return bl, err
}
Expand All @@ -119,7 +126,7 @@ func (b *arccache) Put(bl blocks.Block) error {

err := b.blockstore.Put(bl)
if err == nil {
b.addCache(bl.Cid(), len(bl.RawData()))
b.cacheSize(bl.Cid(), len(bl.RawData()))
}
return err
}
Expand All @@ -138,7 +145,7 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
return err
}
for _, block := range good {
b.addCache(block.Cid(), len(block.RawData()))
b.cacheSize(block.Cid(), len(block.RawData()))
}
return nil
}
Expand All @@ -147,8 +154,12 @@ func (b *arccache) HashOnRead(enabled bool) {
b.blockstore.HashOnRead(enabled)
}

func (b *arccache) addCache(c *cid.Cid, blockSize int) {
b.arc.Add(c.KeyString(), blockSize)
func (b *arccache) cacheHave(c *cid.Cid, have bool) {
b.arc.Add(c.KeyString(), cacheHave(have))
}

func (b *arccache) cacheSize(c *cid.Cid, blockSize int) {
b.arc.Add(c.KeyString(), cacheSize(blockSize))
}

func (b *arccache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
Expand Down
3 changes: 3 additions & 0 deletions blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func (bs *blockstore) Has(k *cid.Cid) (bool, error) {

func (bs *blockstore) GetSize(k *cid.Cid) (int, error) {
maybeData, err := bs.datastore.Get(dshelp.CidToDsKey(k))
if err == ds.ErrNotFound {
return -1, ErrNotFound
}
if err != nil {
return -1, err
}
Expand Down
2 changes: 1 addition & 1 deletion bloom_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestPutManyAddsToBloom(t *testing.T) {
t.Fatal(err)
}
blockSize, err = cachedbs.GetSize(block2.Cid())
if err != nil && err != ds.ErrNotFound {
if err != nil && err != ErrNotFound {
t.Fatal(err)
}
if blockSize > -1 || has {
Expand Down