arc_cache.go 3.34 KB
Newer Older
1 2 3
package blockstore

import (
4
	"context"
5

6 7 8
	"github.com/ipfs/go-ipfs/blocks"

	"gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
9
	lru "gx/ipfs/QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy/golang-lru"
10
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
George Antoniadis's avatar
George Antoniadis committed
11
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
12 13 14 15 16
)

type arccache struct {
	arc        *lru.ARCCache
	blockstore Blockstore
17 18 19

	hits  metrics.Counter
	total metrics.Counter
20 21
}

22
func newARCCachedBS(ctx context.Context, bs Blockstore, lruSize int) (*arccache, error) {
23 24 25 26
	arc, err := lru.NewARC(lruSize)
	if err != nil {
		return nil, err
	}
27 28 29
	c := &arccache{arc: arc, blockstore: bs}
	c.hits = metrics.NewCtx(ctx, "arc.hits_total", "Number of ARC cache hits").Counter()
	c.total = metrics.NewCtx(ctx, "arc_total", "Total number of ARC cache requests").Counter()
30

31
	return c, nil
32 33
}

34
func (b *arccache) DeleteBlock(k *cid.Cid) error {
35 36 37 38 39 40 41
	if has, ok := b.hasCached(k); ok && !has {
		return ErrNotFound
	}

	b.arc.Remove(k) // Invalidate cache before deleting.
	err := b.blockstore.DeleteBlock(k)
	switch err {
42
	case nil, ds.ErrNotFound, ErrNotFound:
43
		b.addCache(k, false)
44
		return err
45 46 47 48 49 50 51
	default:
		return err
	}
}

// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
52
func (b *arccache) hasCached(k *cid.Cid) (has bool, ok bool) {
53
	b.total.Inc()
54 55
	if k == nil {
		log.Error("nil cid in arccache")
56 57
		// Return cache invalid so the call to blockstore happens
		// in case of invalid key and correct error is created.
58 59
		return false, false
	}
60

61
	h, ok := b.arc.Get(k.KeyString())
62
	if ok {
63
		b.hits.Inc()
64
		return h.(bool), true
65
	}
66
	return false, false
67 68
}

69
func (b *arccache) Has(k *cid.Cid) (bool, error) {
70 71 72 73 74 75
	if has, ok := b.hasCached(k); ok {
		return has, nil
	}

	res, err := b.blockstore.Has(k)
	if err == nil {
76
		b.addCache(k, res)
77 78 79 80
	}
	return res, err
}

81 82 83 84 85 86
func (b *arccache) Get(k *cid.Cid) (blocks.Block, error) {
	if k == nil {
		log.Error("nil cid in arc cache")
		return nil, ErrNotFound
	}

87 88 89 90 91 92
	if has, ok := b.hasCached(k); ok && !has {
		return nil, ErrNotFound
	}

	bl, err := b.blockstore.Get(k)
	if bl == nil && err == ErrNotFound {
93
		b.addCache(k, false)
94
	} else if bl != nil {
95
		b.addCache(k, true)
96 97 98 99 100
	}
	return bl, err
}

func (b *arccache) Put(bl blocks.Block) error {
101
	if has, ok := b.hasCached(bl.Cid()); ok && has {
102 103 104 105 106
		return nil
	}

	err := b.blockstore.Put(bl)
	if err == nil {
107
		b.addCache(bl.Cid(), true)
108 109 110 111 112 113 114
	}
	return err
}

func (b *arccache) PutMany(bs []blocks.Block) error {
	var good []blocks.Block
	for _, block := range bs {
115 116
		// call put on block if result is inconclusive or we are sure that
		// the block isn't in storage
117
		if has, ok := b.hasCached(block.Cid()); !ok || (ok && !has) {
118 119 120
			good = append(good, block)
		}
	}
121
	err := b.blockstore.PutMany(good)
122 123
	if err != nil {
		return err
124
	}
125
	for _, block := range good {
126
		b.addCache(block.Cid(), true)
127 128
	}
	return nil
129 130
}

131 132 133 134 135
func (b *arccache) addCache(c *cid.Cid, has bool) {
	b.arc.Add(c.KeyString(), has)
}

func (b *arccache) AllKeysChan(ctx context.Context) (<-chan *cid.Cid, error) {
136 137 138 139 140 141 142 143 144 145 146 147 148 149
	return b.blockstore.AllKeysChan(ctx)
}

func (b *arccache) GCLock() Unlocker {
	return b.blockstore.(GCBlockstore).GCLock()
}

func (b *arccache) PinLock() Unlocker {
	return b.blockstore.(GCBlockstore).PinLock()
}

func (b *arccache) GCRequested() bool {
	return b.blockstore.(GCBlockstore).GCRequested()
}