2021-01-29 20:01:00 +00:00
|
|
|
package blockstore
|
2020-10-14 21:02:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
2022-06-28 11:09:59 +00:00
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2020-10-23 18:46:06 +00:00
|
|
|
"github.com/raulk/clock"
|
2020-10-14 21:02:19 +00:00
|
|
|
"go.uber.org/multierr"
|
|
|
|
)
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
// TimedCacheBlockstore is a blockstore that keeps blocks for at least the
|
|
|
|
// specified caching interval before discarding them. Garbage collection must
|
|
|
|
// be started and stopped by calling Start/Stop.
|
2020-10-14 21:02:19 +00:00
|
|
|
//
|
|
|
|
// Under the covers, it's implemented with an active and an inactive blockstore
|
|
|
|
// that are rotated every cache time interval. This means all blocks will be
|
|
|
|
// stored at most 2x the cache interval.
|
2021-01-29 23:17:25 +00:00
|
|
|
//
|
|
|
|
// Create a new instance by calling the NewTimedCacheBlockstore constructor.
|
|
|
|
type TimedCacheBlockstore struct {
|
2020-10-14 21:02:19 +00:00
|
|
|
mu sync.RWMutex
|
2021-01-29 20:01:00 +00:00
|
|
|
active, inactive MemBlockstore
|
2020-10-23 18:46:06 +00:00
|
|
|
clock clock.Clock
|
|
|
|
interval time.Duration
|
|
|
|
closeCh chan struct{}
|
|
|
|
doneRotatingCh chan struct{}
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func NewTimedCacheBlockstore(interval time.Duration) *TimedCacheBlockstore {
|
|
|
|
b := &TimedCacheBlockstore{
|
2021-01-29 20:01:00 +00:00
|
|
|
active: NewMemory(),
|
|
|
|
inactive: NewMemory(),
|
2021-01-29 23:17:25 +00:00
|
|
|
interval: interval,
|
2021-01-29 20:01:00 +00:00
|
|
|
clock: clock.New(),
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
2021-01-29 23:17:25 +00:00
|
|
|
return b
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) Start(_ context.Context) error {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
if t.closeCh != nil {
|
|
|
|
return fmt.Errorf("already started")
|
|
|
|
}
|
|
|
|
t.closeCh = make(chan struct{})
|
2021-08-24 18:35:56 +00:00
|
|
|
|
|
|
|
// Create this timer before starting the goroutine. Otherwise, creating the timer will race
|
2021-08-25 00:39:28 +00:00
|
|
|
// with adding time to the mock clock, and we could add time _first_, then stall waiting for
|
2021-08-24 18:35:56 +00:00
|
|
|
// a timer that'll never fire.
|
|
|
|
ticker := t.clock.Ticker(t.interval)
|
2020-10-14 21:02:19 +00:00
|
|
|
go func() {
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
t.rotate()
|
2020-10-23 18:46:06 +00:00
|
|
|
if t.doneRotatingCh != nil {
|
|
|
|
t.doneRotatingCh <- struct{}{}
|
|
|
|
}
|
2020-10-14 21:02:19 +00:00
|
|
|
case <-t.closeCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) Stop(_ context.Context) error {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
if t.closeCh == nil {
|
2021-01-29 23:17:25 +00:00
|
|
|
return fmt.Errorf("not started")
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-t.closeCh:
|
|
|
|
// already closed
|
|
|
|
default:
|
|
|
|
close(t.closeCh)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) rotate() {
|
2021-01-29 20:01:00 +00:00
|
|
|
newBs := NewMemory()
|
2020-10-14 21:02:19 +00:00
|
|
|
|
|
|
|
t.mu.Lock()
|
|
|
|
t.inactive, t.active = t.active, newBs
|
|
|
|
t.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) Put(ctx context.Context, b blocks.Block) error {
|
2020-10-14 21:02:19 +00:00
|
|
|
// Don't check the inactive set here. We want to keep this block for at
|
|
|
|
// least one interval.
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return t.active.Put(ctx, b)
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) PutMany(ctx context.Context, bs []blocks.Block) error {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return t.active.PutMany(ctx, bs)
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) View(ctx context.Context, k cid.Cid, callback func([]byte) error) error {
|
2021-03-12 04:03:38 +00:00
|
|
|
// The underlying blockstore is always a "mem" blockstore so there's no difference,
|
|
|
|
// from a performance perspective, between view & get. So we call Get to avoid
|
|
|
|
// calling an arbitrary callback while holding a lock.
|
2021-02-28 22:39:00 +00:00
|
|
|
t.mu.RLock()
|
2021-11-19 01:50:25 +00:00
|
|
|
block, err := t.active.Get(ctx, k)
|
2022-06-28 11:09:59 +00:00
|
|
|
if ipld.IsNotFound(err) {
|
2021-11-19 01:50:25 +00:00
|
|
|
block, err = t.inactive.Get(ctx, k)
|
2021-03-12 04:03:38 +00:00
|
|
|
}
|
|
|
|
t.mu.RUnlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-29 20:01:00 +00:00
|
|
|
}
|
2021-03-12 04:03:38 +00:00
|
|
|
return callback(block.RawData())
|
2021-01-29 20:01:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
b, err := t.active.Get(ctx, k)
|
2022-06-28 11:09:59 +00:00
|
|
|
if ipld.IsNotFound(err) {
|
2021-11-19 01:50:25 +00:00
|
|
|
b, err = t.inactive.Get(ctx, k)
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) GetSize(ctx context.Context, k cid.Cid) (int, error) {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
size, err := t.active.GetSize(ctx, k)
|
2022-06-28 11:09:59 +00:00
|
|
|
if ipld.IsNotFound(err) {
|
2021-11-19 01:50:25 +00:00
|
|
|
size, err = t.inactive.GetSize(ctx, k)
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
return size, err
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) Has(ctx context.Context, k cid.Cid) (bool, error) {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
if has, err := t.active.Has(ctx, k); err != nil {
|
2020-10-14 21:02:19 +00:00
|
|
|
return false, err
|
|
|
|
} else if has {
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-11-19 01:50:25 +00:00
|
|
|
return t.inactive.Has(ctx, k)
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 12:26:59 +00:00
|
|
|
func (t *TimedCacheBlockstore) HashOnRead(_ bool) {
|
2020-10-14 21:02:19 +00:00
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) DeleteBlock(ctx context.Context, k cid.Cid) error {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return multierr.Combine(t.active.DeleteBlock(ctx, k), t.inactive.DeleteBlock(ctx, k))
|
2020-10-14 21:02:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:50:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) DeleteMany(ctx context.Context, ks []cid.Cid) error {
|
2021-03-02 14:45:45 +00:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2021-11-19 01:50:25 +00:00
|
|
|
return multierr.Combine(t.active.DeleteMany(ctx, ks), t.inactive.DeleteMany(ctx, ks))
|
2021-03-02 14:45:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 23:17:25 +00:00
|
|
|
func (t *TimedCacheBlockstore) AllKeysChan(_ context.Context) (<-chan cid.Cid, error) {
|
2020-10-14 21:02:19 +00:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
|
|
|
|
ch := make(chan cid.Cid, len(t.active)+len(t.inactive))
|
|
|
|
for c := range t.active {
|
|
|
|
ch <- c
|
|
|
|
}
|
|
|
|
for c := range t.inactive {
|
|
|
|
if _, ok := t.active[c]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ch <- c
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
return ch, nil
|
|
|
|
}
|