bloom_cache.go 4.02 KB
Newer Older
1 2 3
package blockstore

import (
4 5
	"sync/atomic"

6
	"github.com/ipfs/go-ipfs/blocks"
George Antoniadis's avatar
George Antoniadis committed
7
	key "gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
8

9
	"gx/ipfs/QmVWBQQAz4Cd2XgW9KgQoqXXrU8KJoCb9WCrhWRFVBKvFe/go-metrics-interface"
10
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jakub Sztandera's avatar
Jakub Sztandera committed
11
	bloom "gx/ipfs/QmeiMCBkYHxkDkDfnDadzz4YxY5ruL5Pj499essE4vRsGM/bbloom"
12 13
)

14
// bloomCached returns Blockstore that caches Has requests using Bloom filter
15
// Size is size of bloom filter in bytes
16
func bloomCached(bs Blockstore, ctx context.Context, bloomSize, hashCount int) (*bloomcache, error) {
17
	bl, err := bloom.New(float64(bloomSize), float64(hashCount))
18 19 20
	if err != nil {
		return nil, err
	}
21
	bc := &bloomcache{blockstore: bs, bloom: bl}
22 23 24 25
	bc.hits = metrics.NewCtx(ctx, "bloom.hits_total",
		"Number of cache hits in bloom cache").Counter()
	bc.total = metrics.NewCtx(ctx, "bloom_total",
		"Total number of requests to bloom cache").Counter()
26
	bc.Invalidate()
27
	go bc.Rebuild(ctx)
28 29 30 31 32 33

	return bc, nil
}

type bloomcache struct {
	bloom  *bloom.Bloom
34
	active int32
35 36 37 38 39 40

	// This chan is only used for testing to wait for bloom to enable
	rebuildChan chan struct{}
	blockstore  Blockstore

	// Statistics
41 42
	hits  metrics.Counter
	total metrics.Counter
43 44 45 46
}

func (b *bloomcache) Invalidate() {
	b.rebuildChan = make(chan struct{})
47
	atomic.StoreInt32(&b.active, 0)
48 49 50
}

func (b *bloomcache) BloomActive() bool {
51
	return atomic.LoadInt32(&b.active) != 0
52 53
}

54
func (b *bloomcache) Rebuild(ctx context.Context) {
55 56 57 58 59 60 61 62
	evt := log.EventBegin(ctx, "bloomcache.Rebuild")
	defer evt.Done()

	ch, err := b.blockstore.AllKeysChan(ctx)
	if err != nil {
		log.Errorf("AllKeysChan failed in bloomcache rebuild with: %v", err)
		return
	}
63 64 65 66 67 68 69 70 71 72 73 74 75
	finish := false
	for !finish {
		select {
		case key, ok := <-ch:
			if ok {
				b.bloom.AddTS([]byte(key)) // Use binary key, the more compact the better
			} else {
				finish = true
			}
		case <-ctx.Done():
			log.Warning("Cache rebuild closed by context finishing.")
			return
		}
76 77
	}
	close(b.rebuildChan)
78
	atomic.StoreInt32(&b.active, 1)
79 80 81 82 83 84 85
}

func (b *bloomcache) DeleteBlock(k key.Key) error {
	if has, ok := b.hasCached(k); ok && !has {
		return ErrNotFound
	}

86
	return b.blockstore.DeleteBlock(k)
87 88 89 90 91
}

// if ok == false has is inconclusive
// if ok == true then has respons to question: is it contained
func (b *bloomcache) hasCached(k key.Key) (has bool, ok bool) {
92
	b.total.Inc()
93
	if k == "" {
94 95 96
		// Return cache invalid so call to blockstore
		// in case of invalid key is forwarded deeper
		return false, false
97
	}
98
	if b.BloomActive() {
99 100
		blr := b.bloom.HasTS([]byte(k))
		if blr == false { // not contained in bloom is only conclusive answer bloom gives
101
			b.hits.Inc()
102
			return false, true
103 104
		}
	}
105
	return false, false
106 107 108 109 110 111 112
}

func (b *bloomcache) Has(k key.Key) (bool, error) {
	if has, ok := b.hasCached(k); ok {
		return has, nil
	}

113
	return b.blockstore.Has(k)
114 115 116 117 118 119 120
}

func (b *bloomcache) Get(k key.Key) (blocks.Block, error) {
	if has, ok := b.hasCached(k); ok && !has {
		return nil, ErrNotFound
	}

121
	return b.blockstore.Get(k)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

func (b *bloomcache) Put(bl blocks.Block) error {
	if has, ok := b.hasCached(bl.Key()); ok && has {
		return nil
	}

	err := b.blockstore.Put(bl)
	if err == nil {
		b.bloom.AddTS([]byte(bl.Key()))
	}
	return err
}

func (b *bloomcache) PutMany(bs []blocks.Block) error {
137 138 139 140
	// bloom cache gives only conclusive resulty if key is not contained
	// to reduce number of puts we need conclusive infomration if block is contained
	// this means that PutMany can't be improved with bloom cache so we just
	// just do a passthrough.
141
	err := b.blockstore.PutMany(bs)
142 143
	if err != nil {
		return err
144
	}
145 146 147 148
	for _, bl := range bs {
		b.bloom.AddTS([]byte(bl.Key()))
	}
	return nil
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
	return b.blockstore.AllKeysChan(ctx)
}

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

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

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