|
| 1 | +package blockstore |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ipfs/go-ipfs/blocks" |
| 5 | + key "github.com/ipfs/go-ipfs/blocks/key" |
| 6 | + lru "gx/ipfs/QmVYxfoJQiZijTgPNHCHgHELvQpbsJNTg6Crmc3dQkj3yy/golang-lru" |
| 7 | + bloom "gx/ipfs/QmWQ2SJisXwcCLsUXLwYCKSfyExXjFRW2WbBH5sqCUnwX5/bbloom" |
| 8 | + context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context" |
| 9 | + ds "gx/ipfs/QmfQzVugPq1w5shWRcLWSeiHF4a2meBX7yVD8Vw7GWJM9o/go-datastore" |
| 10 | + |
| 11 | + "sync/atomic" |
| 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, bloomSize, lruSize int) (*bloomcache, error) { |
| 17 | + bl, err := bloom.New(float64(bloomSize), float64(7)) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + arc, err := lru.NewARC(lruSize) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + bc := &bloomcache{blockstore: bs, bloom: bl, arc: arc} |
| 26 | + bc.Invalidate() |
| 27 | + go bc.Rebuild() |
| 28 | + |
| 29 | + return bc, nil |
| 30 | +} |
| 31 | + |
| 32 | +type bloomcache struct { |
| 33 | + bloom *bloom.Bloom |
| 34 | + active int32 |
| 35 | + |
| 36 | + arc *lru.ARCCache |
| 37 | + // This chan is only used for testing to wait for bloom to enable |
| 38 | + rebuildChan chan struct{} |
| 39 | + blockstore Blockstore |
| 40 | + |
| 41 | + // Statistics |
| 42 | + hits uint64 |
| 43 | + misses uint64 |
| 44 | +} |
| 45 | + |
| 46 | +func (b *bloomcache) Invalidate() { |
| 47 | + b.rebuildChan = make(chan struct{}) |
| 48 | + atomic.StoreInt32(&b.active, 0) |
| 49 | +} |
| 50 | + |
| 51 | +func (b *bloomcache) BloomActive() bool { |
| 52 | + return atomic.LoadInt32(&b.active) != 0 |
| 53 | +} |
| 54 | + |
| 55 | +func (b *bloomcache) Rebuild() { |
| 56 | + ctx := context.TODO() |
| 57 | + evt := log.EventBegin(ctx, "bloomcache.Rebuild") |
| 58 | + defer evt.Done() |
| 59 | + |
| 60 | + ch, err := b.blockstore.AllKeysChan(ctx) |
| 61 | + if err != nil { |
| 62 | + log.Errorf("AllKeysChan failed in bloomcache rebuild with: %v", err) |
| 63 | + return |
| 64 | + } |
| 65 | + for key := range ch { |
| 66 | + b.bloom.AddTS([]byte(key)) // Use binary key, the more compact the better |
| 67 | + } |
| 68 | + close(b.rebuildChan) |
| 69 | + atomic.StoreInt32(&b.active, 1) |
| 70 | +} |
| 71 | + |
| 72 | +func (b *bloomcache) DeleteBlock(k key.Key) error { |
| 73 | + if has, ok := b.hasCached(k); ok && !has { |
| 74 | + return ErrNotFound |
| 75 | + } |
| 76 | + |
| 77 | + b.arc.Remove(k) // Invalidate cache before deleting. |
| 78 | + err := b.blockstore.DeleteBlock(k) |
| 79 | + switch err { |
| 80 | + case nil: |
| 81 | + b.arc.Add(k, false) |
| 82 | + case ds.ErrNotFound, ErrNotFound: |
| 83 | + b.arc.Add(k, false) |
| 84 | + default: |
| 85 | + return err |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// if ok == false has is inconclusive |
| 91 | +// if ok == true then has respons to question: is it contained |
| 92 | +func (b *bloomcache) hasCached(k key.Key) (has bool, ok bool) { |
| 93 | + if k == "" { |
| 94 | + // Return cache invalid so call to blockstore |
| 95 | + // in case of invalid key is forwarded deeper |
| 96 | + return false, false |
| 97 | + } |
| 98 | + if b.BloomActive() { |
| 99 | + blr := b.bloom.HasTS([]byte(k)) |
| 100 | + if blr == false { // not contained in bloom is only conclusive answer bloom gives |
| 101 | + return false, true |
| 102 | + } |
| 103 | + } |
| 104 | + h, ok := b.arc.Get(k) |
| 105 | + if ok { |
| 106 | + return h.(bool), ok |
| 107 | + } else { |
| 108 | + return false, false |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (b *bloomcache) Has(k key.Key) (bool, error) { |
| 113 | + if has, ok := b.hasCached(k); ok { |
| 114 | + return has, nil |
| 115 | + } |
| 116 | + |
| 117 | + res, err := b.blockstore.Has(k) |
| 118 | + if err == nil { |
| 119 | + b.arc.Add(k, res) |
| 120 | + } |
| 121 | + return res, err |
| 122 | +} |
| 123 | + |
| 124 | +func (b *bloomcache) Get(k key.Key) (blocks.Block, error) { |
| 125 | + if has, ok := b.hasCached(k); ok && !has { |
| 126 | + return nil, ErrNotFound |
| 127 | + } |
| 128 | + |
| 129 | + bl, err := b.blockstore.Get(k) |
| 130 | + if bl == nil && err == ErrNotFound { |
| 131 | + b.arc.Add(k, false) |
| 132 | + } else if bl != nil { |
| 133 | + b.arc.Add(k, true) |
| 134 | + } |
| 135 | + return bl, err |
| 136 | +} |
| 137 | + |
| 138 | +func (b *bloomcache) Put(bl blocks.Block) error { |
| 139 | + if has, ok := b.hasCached(bl.Key()); ok && has { |
| 140 | + return nil |
| 141 | + } |
| 142 | + |
| 143 | + err := b.blockstore.Put(bl) |
| 144 | + if err == nil { |
| 145 | + b.bloom.AddTS([]byte(bl.Key())) |
| 146 | + b.arc.Add(bl.Key(), true) |
| 147 | + } |
| 148 | + return err |
| 149 | +} |
| 150 | + |
| 151 | +func (b *bloomcache) PutMany(bs []blocks.Block) error { |
| 152 | + var good []blocks.Block |
| 153 | + for _, block := range bs { |
| 154 | + if has, ok := b.hasCached(block.Key()); !ok || (ok && !has) { |
| 155 | + good = append(good, block) |
| 156 | + } |
| 157 | + } |
| 158 | + err := b.blockstore.PutMany(bs) |
| 159 | + if err == nil { |
| 160 | + for _, block := range bs { |
| 161 | + b.bloom.AddTS([]byte(block.Key())) |
| 162 | + } |
| 163 | + } |
| 164 | + return err |
| 165 | +} |
| 166 | + |
| 167 | +func (b *bloomcache) AllKeysChan(ctx context.Context) (<-chan key.Key, error) { |
| 168 | + return b.blockstore.AllKeysChan(ctx) |
| 169 | +} |
| 170 | + |
| 171 | +func (b *bloomcache) GCLock() Unlocker { |
| 172 | + return b.blockstore.(GCBlockstore).GCLock() |
| 173 | +} |
| 174 | + |
| 175 | +func (b *bloomcache) PinLock() Unlocker { |
| 176 | + return b.blockstore.(GCBlockstore).PinLock() |
| 177 | +} |
| 178 | + |
| 179 | +func (b *bloomcache) GCRequested() bool { |
| 180 | + return b.blockstore.(GCBlockstore).GCRequested() |
| 181 | +} |
0 commit comments