2021-07-22 09:11:49 +00:00
|
|
|
package splitstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/dgraph-io/badger/v2"
|
2021-07-22 09:27:59 +00:00
|
|
|
"github.com/dgraph-io/badger/v2/options"
|
2021-07-22 09:11:49 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BadgerMarkSetEnv struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ MarkSetEnv = (*BadgerMarkSetEnv)(nil)
|
|
|
|
|
|
|
|
type BadgerMarkSet struct {
|
2021-07-22 10:54:50 +00:00
|
|
|
mx sync.RWMutex
|
|
|
|
cond sync.Cond
|
|
|
|
pend map[string]struct{}
|
2021-07-22 11:43:00 +00:00
|
|
|
writing map[int]map[string]struct{}
|
2021-07-22 10:54:50 +00:00
|
|
|
writers int
|
2021-07-22 11:43:00 +00:00
|
|
|
seqno int
|
2021-07-22 09:11:49 +00:00
|
|
|
|
|
|
|
db *badger.DB
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ MarkSet = (*BadgerMarkSet)(nil)
|
|
|
|
|
2021-07-22 12:24:30 +00:00
|
|
|
var badgerMarkSetBatchSize = 16384
|
2021-07-22 09:11:49 +00:00
|
|
|
|
|
|
|
func NewBadgerMarkSetEnv(path string) (MarkSetEnv, error) {
|
|
|
|
msPath := filepath.Join(path, "markset.badger")
|
|
|
|
err := os.MkdirAll(msPath, 0755) //nolint:gosec
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error creating markset directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BadgerMarkSetEnv{path: msPath}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BadgerMarkSetEnv) Create(name string, sizeHint int64) (MarkSet, error) {
|
|
|
|
path := filepath.Join(e.path, name)
|
|
|
|
|
|
|
|
// clean up first
|
|
|
|
err := os.RemoveAll(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error clearing markset directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(path, 0755) //nolint:gosec
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error creating markset directory: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := badger.DefaultOptions(path)
|
|
|
|
opts.SyncWrites = false
|
2021-07-22 09:27:59 +00:00
|
|
|
opts.Compression = options.None
|
2021-07-22 09:11:49 +00:00
|
|
|
opts.Logger = &badgerLogger{
|
|
|
|
SugaredLogger: log.Desugar().WithOptions(zap.AddCallerSkip(1)).Sugar(),
|
|
|
|
skip2: log.Desugar().WithOptions(zap.AddCallerSkip(2)).Sugar(),
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := badger.Open(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error creating badger markset: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:54:50 +00:00
|
|
|
ms := &BadgerMarkSet{
|
2021-07-22 11:43:00 +00:00
|
|
|
pend: make(map[string]struct{}),
|
|
|
|
writing: make(map[int]map[string]struct{}),
|
|
|
|
db: db,
|
|
|
|
path: path,
|
2021-07-22 10:54:50 +00:00
|
|
|
}
|
|
|
|
ms.cond.L = &ms.mx
|
|
|
|
|
|
|
|
return ms, nil
|
2021-07-22 09:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BadgerMarkSetEnv) Close() error {
|
|
|
|
return os.RemoveAll(e.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BadgerMarkSet) Mark(c cid.Cid) error {
|
|
|
|
s.mx.Lock()
|
|
|
|
|
|
|
|
if s.pend == nil {
|
2021-07-22 10:47:40 +00:00
|
|
|
s.mx.Unlock()
|
2021-07-22 09:11:49 +00:00
|
|
|
return errMarkSetClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
s.pend[string(c.Hash())] = struct{}{}
|
|
|
|
|
|
|
|
if len(s.pend) < badgerMarkSetBatchSize {
|
2021-07-22 10:47:40 +00:00
|
|
|
s.mx.Unlock()
|
2021-07-22 09:11:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pend := s.pend
|
2021-07-22 11:43:00 +00:00
|
|
|
seqno := s.seqno
|
|
|
|
s.seqno++
|
|
|
|
s.writing[seqno] = pend
|
2021-07-22 09:11:49 +00:00
|
|
|
s.pend = make(map[string]struct{})
|
2021-07-22 10:54:50 +00:00
|
|
|
s.writers++
|
2021-07-22 10:47:40 +00:00
|
|
|
s.mx.Unlock()
|
2021-07-22 09:11:49 +00:00
|
|
|
|
2021-07-22 10:54:50 +00:00
|
|
|
defer func() {
|
|
|
|
s.mx.Lock()
|
2021-07-22 12:24:30 +00:00
|
|
|
defer s.mx.Unlock()
|
2021-07-22 11:43:00 +00:00
|
|
|
|
|
|
|
delete(s.writing, seqno)
|
2021-07-22 10:54:50 +00:00
|
|
|
s.writers--
|
|
|
|
if s.writers == 0 {
|
|
|
|
s.cond.Broadcast()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-07-22 09:11:49 +00:00
|
|
|
empty := []byte{} // not nil
|
|
|
|
|
2021-07-22 10:54:50 +00:00
|
|
|
batch := s.db.NewWriteBatch()
|
2021-07-22 09:11:49 +00:00
|
|
|
defer batch.Cancel()
|
|
|
|
|
|
|
|
for k := range pend {
|
|
|
|
if err := batch.Set([]byte(k), empty); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := batch.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error flushing batch to badger markset: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BadgerMarkSet) Has(c cid.Cid) (bool, error) {
|
|
|
|
s.mx.RLock()
|
|
|
|
defer s.mx.RUnlock()
|
|
|
|
|
|
|
|
if s.pend == nil {
|
|
|
|
return false, errMarkSetClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
key := c.Hash()
|
2021-07-22 11:43:00 +00:00
|
|
|
pendKey := string(key)
|
|
|
|
_, ok := s.pend[pendKey]
|
2021-07-22 09:11:49 +00:00
|
|
|
if ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:43:00 +00:00
|
|
|
for _, wr := range s.writing {
|
|
|
|
_, ok := wr[pendKey]
|
|
|
|
if ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:11:49 +00:00
|
|
|
err := s.db.View(func(txn *badger.Txn) error {
|
|
|
|
_, err := txn.Get(key)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return true, nil
|
|
|
|
|
|
|
|
case badger.ErrKeyNotFound:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false, xerrors.Errorf("error checking badger markset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BadgerMarkSet) Close() error {
|
|
|
|
s.mx.Lock()
|
|
|
|
defer s.mx.Unlock()
|
|
|
|
|
|
|
|
if s.pend == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 10:54:50 +00:00
|
|
|
for s.writers > 0 {
|
|
|
|
s.cond.Wait()
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:11:49 +00:00
|
|
|
s.pend = nil
|
|
|
|
db := s.db
|
|
|
|
s.db = nil
|
|
|
|
|
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error closing badger markset: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.RemoveAll(s.path)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error deleting badger markset: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BadgerMarkSet) SetConcurrent() {}
|
|
|
|
|
|
|
|
// badger logging through go-log
|
|
|
|
type badgerLogger struct {
|
|
|
|
*zap.SugaredLogger
|
|
|
|
skip2 *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:27:59 +00:00
|
|
|
func (b *badgerLogger) Warningf(format string, args ...interface{}) {}
|
|
|
|
func (b *badgerLogger) Infof(format string, args ...interface{}) {}
|
|
|
|
func (b *badgerLogger) Debugf(format string, args ...interface{}) {}
|