This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbloom_cache.go
231 lines (200 loc) · 5.46 KB
/
bloom_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package blockstore
import (
"context"
"fmt"
"sync/atomic"
"time"
bloom "github.com/ipfs/bbloom"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
ipld "github.com/ipfs/go-ipld-format"
metrics "github.com/ipfs/go-metrics-interface"
)
// bloomCached returns a Blockstore that caches Has requests using a Bloom
// filter. bloomSize is size of bloom filter in bytes. hashCount specifies the
// number of hashing functions in the bloom filter (usually known as k).
func bloomCached(ctx context.Context, bs Blockstore, bloomSize, hashCount int) (*bloomcache, error) {
bl, err := bloom.New(float64(bloomSize), float64(hashCount))
if err != nil {
return nil, err
}
bc := &bloomcache{
blockstore: bs,
bloom: bl,
hits: metrics.NewCtx(ctx, "bloom.hits_total",
"Number of cache hits in bloom cache").Counter(),
total: metrics.NewCtx(ctx, "bloom_total",
"Total number of requests to bloom cache").Counter(),
buildChan: make(chan struct{}),
}
if v, ok := bs.(Viewer); ok {
bc.viewer = v
}
go func() {
err := bc.build(ctx)
if err != nil {
select {
case <-ctx.Done():
logger.Warning("Cache rebuild closed by context finishing: ", err)
default:
logger.Error(err)
}
return
}
if metrics.Active() {
fill := metrics.NewCtx(ctx, "bloom_fill_ratio",
"Ratio of bloom filter fullnes, (updated once a minute)").Gauge()
t := time.NewTicker(1 * time.Minute)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
fill.Set(bc.bloom.FillRatioTS())
}
}
}
}()
return bc, nil
}
type bloomcache struct {
active int32
bloom *bloom.Bloom
buildErr error
buildChan chan struct{}
blockstore Blockstore
viewer Viewer
// Statistics
hits metrics.Counter
total metrics.Counter
}
var _ Blockstore = (*bloomcache)(nil)
var _ Viewer = (*bloomcache)(nil)
func (b *bloomcache) BloomActive() bool {
return atomic.LoadInt32(&b.active) != 0
}
func (b *bloomcache) Wait(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-b.buildChan:
return b.buildErr
}
}
func (b *bloomcache) build(ctx context.Context) error {
evt := logger.EventBegin(ctx, "bloomcache.build")
defer evt.Done()
defer close(b.buildChan)
ch, err := b.blockstore.AllKeysChan(ctx)
if err != nil {
b.buildErr = fmt.Errorf("AllKeysChan failed in bloomcache rebuild with: %v", err)
return b.buildErr
}
for {
select {
case key, ok := <-ch:
if !ok {
atomic.StoreInt32(&b.active, 1)
return nil
}
b.bloom.AddTS(key.Hash()) // Use binary key, the more compact the better
case <-ctx.Done():
b.buildErr = ctx.Err()
return b.buildErr
}
}
}
func (b *bloomcache) DeleteBlock(ctx context.Context, k cid.Cid) error {
if has, ok := b.hasCached(k); ok && !has {
return nil
}
return b.blockstore.DeleteBlock(ctx, k)
}
// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *bloomcache) hasCached(k cid.Cid) (has bool, ok bool) {
b.total.Inc()
if !k.Defined() {
logger.Error("undefined in bloom cache")
// Return cache invalid so call to blockstore
// in case of invalid key is forwarded deeper
return false, false
}
if b.BloomActive() {
blr := b.bloom.HasTS(k.Hash())
if !blr { // not contained in bloom is only conclusive answer bloom gives
b.hits.Inc()
return false, true
}
}
return false, false
}
func (b *bloomcache) Has(ctx context.Context, k cid.Cid) (bool, error) {
if has, ok := b.hasCached(k); ok {
return has, nil
}
return b.blockstore.Has(ctx, k)
}
func (b *bloomcache) GetSize(ctx context.Context, k cid.Cid) (int, error) {
if has, ok := b.hasCached(k); ok && !has {
return -1, ipld.ErrNotFound{Cid: k}
}
return b.blockstore.GetSize(ctx, k)
}
func (b *bloomcache) View(ctx context.Context, k cid.Cid, callback func([]byte) error) error {
if b.viewer == nil {
blk, err := b.Get(ctx, k)
if err != nil {
return err
}
return callback(blk.RawData())
}
if has, ok := b.hasCached(k); ok && !has {
return ipld.ErrNotFound{Cid: k}
}
return b.viewer.View(ctx, k, callback)
}
func (b *bloomcache) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
if has, ok := b.hasCached(k); ok && !has {
return nil, ipld.ErrNotFound{Cid: k}
}
return b.blockstore.Get(ctx, k)
}
func (b *bloomcache) Put(ctx context.Context, bl blocks.Block) error {
// See comment in PutMany
err := b.blockstore.Put(ctx, bl)
if err == nil {
b.bloom.AddTS(bl.Cid().Hash())
}
return err
}
func (b *bloomcache) PutMany(ctx context.Context, bs []blocks.Block) error {
// bloom cache gives only conclusive resulty if key is not contained
// to reduce number of puts we need conclusive information if block is contained
// this means that PutMany can't be improved with bloom cache so we just
// just do a passthrough.
err := b.blockstore.PutMany(ctx, bs)
if err != nil {
return err
}
for _, bl := range bs {
b.bloom.AddTS(bl.Cid().Hash())
}
return nil
}
func (b *bloomcache) HashOnRead(enabled bool) {
b.blockstore.HashOnRead(enabled)
}
func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return b.blockstore.AllKeysChan(ctx)
}
func (b *bloomcache) GCLock(ctx context.Context) Unlocker {
return b.blockstore.(GCBlockstore).GCLock(ctx)
}
func (b *bloomcache) PinLock(ctx context.Context) Unlocker {
return b.blockstore.(GCBlockstore).PinLock(ctx)
}
func (b *bloomcache) GCRequested(ctx context.Context) bool {
return b.blockstore.(GCBlockstore).GCRequested(ctx)
}