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

import (
4
	"context"
5

6 7 8 9
	lru "github.com/hashicorp/golang-lru"
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
	metrics "github.com/ipfs/go-metrics-interface"
10 11
)

12 13 14
type cacheHave bool
type cacheSize int

15 16 17
// 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.
18
type arccache struct {
Steven Allen's avatar
Steven Allen committed
19
	arc        *lru.TwoQueueCache
20
	blockstore Blockstore
21 22 23

	hits  metrics.Counter
	total metrics.Counter
24 25
}

26
func newARCCachedBS(ctx context.Context, bs Blockstore, lruSize int) (*arccache, error) {
Steven Allen's avatar
Steven Allen committed
27
	arc, err := lru.New2Q(lruSize)
28 29 30
	if err != nil {
		return nil, err
	}
31 32 33
	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()
34

35
	return c, nil
36 37
}

38
func (b *arccache) DeleteBlock(k cid.Cid) error {
39
	if has, _, ok := b.hasCached(k); ok && !has {
Steven Allen's avatar
Steven Allen committed
40
		return nil
41 42 43 44
	}

	b.arc.Remove(k) // Invalidate cache before deleting.
	err := b.blockstore.DeleteBlock(k)
Steven Allen's avatar
Steven Allen committed
45
	if err == nil {
46
		b.cacheHave(k, false)
47
	}
Steven Allen's avatar
Steven Allen committed
48
	return err
49 50 51 52
}

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

62
	h, ok := b.arc.Get(string(k.Hash()))
63
	if ok {
64
		b.hits.Inc()
65 66 67 68 69
		switch h := h.(type) {
		case cacheHave:
			return bool(h), -1, true
		case cacheSize:
			return true, int(h), true
70
		}
71
	}
72
	return false, -1, false
73 74
}

75
func (b *arccache) Has(k cid.Cid) (bool, error) {
76 77 78 79 80 81
	if has, _, ok := b.hasCached(k); ok {
		return has, nil
	}
	has, err := b.blockstore.Has(k)
	if err != nil {
		return false, err
82
	}
83 84
	b.cacheHave(k, has)
	return has, nil
85
}
86

87
func (b *arccache) GetSize(k cid.Cid) (int, error) {
88
	if has, blockSize, ok := b.hasCached(k); ok {
89 90 91 92 93 94
		if !has {
			// don't have it, return
			return -1, ErrNotFound
		}
		if blockSize >= 0 {
			// have it and we know the size
95 96
			return blockSize, nil
		}
97
		// we have it but don't know the size, ask the datastore.
98 99
	}
	blockSize, err := b.blockstore.GetSize(k)
100
	if err == ErrNotFound {
101
		b.cacheHave(k, false)
102
	} else if err == nil {
103
		b.cacheSize(k, blockSize)
104
	}
105
	return blockSize, err
106 107
}

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

114
	if has, _, ok := b.hasCached(k); ok && !has {
115 116 117 118 119
		return nil, ErrNotFound
	}

	bl, err := b.blockstore.Get(k)
	if bl == nil && err == ErrNotFound {
120
		b.cacheHave(k, false)
121
	} else if bl != nil {
122
		b.cacheSize(k, len(bl.RawData()))
123 124 125 126 127
	}
	return bl, err
}

func (b *arccache) Put(bl blocks.Block) error {
128
	if has, _, ok := b.hasCached(bl.Cid()); ok && has {
129 130 131 132 133
		return nil
	}

	err := b.blockstore.Put(bl)
	if err == nil {
134
		b.cacheSize(bl.Cid(), len(bl.RawData()))
135 136 137 138 139 140 141
	}
	return err
}

func (b *arccache) PutMany(bs []blocks.Block) error {
	var good []blocks.Block
	for _, block := range bs {
142 143
		// call put on block if result is inconclusive or we are sure that
		// the block isn't in storage
144
		if has, _, ok := b.hasCached(block.Cid()); !ok || (ok && !has) {
145 146 147
			good = append(good, block)
		}
	}
148
	err := b.blockstore.PutMany(good)
149 150
	if err != nil {
		return err
151
	}
152
	for _, block := range good {
153
		b.cacheSize(block.Cid(), len(block.RawData()))
154 155
	}
	return nil
156 157
}

158 159 160 161
func (b *arccache) HashOnRead(enabled bool) {
	b.blockstore.HashOnRead(enabled)
}

162
func (b *arccache) cacheHave(c cid.Cid, have bool) {
163
	b.arc.Add(string(c.Hash()), cacheHave(have))
164 165
}

166
func (b *arccache) cacheSize(c cid.Cid, blockSize int) {
167
	b.arc.Add(string(c.Hash()), cacheSize(blockSize))
168 169
}

170
func (b *arccache) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184
	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()
}