2021-09-14 15:09:36 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-09-14 15:09:36 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoDefaultBucket is returned by IterateDB when default bucket for objects is missing.
|
|
|
|
var ErrNoDefaultBucket = errors.New("no default bucket")
|
|
|
|
|
2022-01-20 16:52:30 +00:00
|
|
|
// IterationPrm contains iteration parameters.
|
|
|
|
type IterationPrm struct {
|
|
|
|
handler func([]byte) error
|
|
|
|
ignoreErrors bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHandler sets a callback to be executed on every object.
|
2022-05-20 15:43:05 +00:00
|
|
|
func (p *IterationPrm) WithHandler(f func([]byte) error) {
|
2022-01-20 16:52:30 +00:00
|
|
|
p.handler = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithIgnoreErrors sets a flag indicating that errors should be ignored.
|
2022-05-20 15:43:05 +00:00
|
|
|
func (p *IterationPrm) WithIgnoreErrors(ignore bool) {
|
2022-01-20 16:52:30 +00:00
|
|
|
p.ignoreErrors = ignore
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:40:11 +00:00
|
|
|
// Iterate iterates over all objects present in write cache.
|
|
|
|
// This is very difficult to do correctly unless write-cache is put in read-only mode.
|
|
|
|
// Thus we silently fail if shard is not in read-only mode to avoid reporting misleading results.
|
2023-05-05 15:36:12 +00:00
|
|
|
func (c *Cache) Iterate(prm IterationPrm) error {
|
2022-01-18 13:40:11 +00:00
|
|
|
c.modeMtx.RLock()
|
|
|
|
defer c.modeMtx.RUnlock()
|
2022-03-17 11:55:25 +00:00
|
|
|
if !c.readOnly() {
|
2022-01-18 13:40:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
return b.ForEach(func(k, data []byte) error {
|
|
|
|
if _, ok := c.flushed.Peek(string(k)); ok {
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-20 16:52:30 +00:00
|
|
|
return prm.handler(data)
|
2022-01-18 13:40:11 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
var fsPrm common.IteratePrm
|
|
|
|
fsPrm.IgnoreErrors = prm.ignoreErrors
|
|
|
|
fsPrm.LazyHandler = func(addr oid.Address, f func() ([]byte, error)) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
if _, ok := c.flushed.Peek(addr.EncodeToString()); ok {
|
2022-01-18 13:40:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-31 14:11:48 +00:00
|
|
|
data, err := f()
|
|
|
|
if err != nil {
|
|
|
|
if prm.ignoreErrors {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 16:52:30 +00:00
|
|
|
return prm.handler(data)
|
2022-07-07 12:03:45 +00:00
|
|
|
}
|
2022-05-23 14:15:16 +00:00
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
_, err = c.fsTree.Iterate(fsPrm)
|
|
|
|
return err
|
2022-01-18 13:40:11 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 15:09:36 +00:00
|
|
|
// IterateDB iterates over all objects stored in bbolt.DB instance and passes them to f until error return.
|
|
|
|
// It is assumed that db is an underlying database of some WriteCache instance.
|
|
|
|
//
|
|
|
|
// Returns ErrNoDefaultBucket if there is no default bucket in db.
|
|
|
|
//
|
|
|
|
// DB must not be nil and should be opened.
|
2022-05-31 17:00:41 +00:00
|
|
|
func IterateDB(db *bbolt.DB, f func(oid.Address) error) error {
|
2021-09-14 15:09:36 +00:00
|
|
|
return db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
if b == nil {
|
|
|
|
return ErrNoDefaultBucket
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
2021-09-14 15:09:36 +00:00
|
|
|
|
|
|
|
return b.ForEach(func(k, v []byte) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
err := addr.DecodeString(string(k))
|
2021-09-14 15:09:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse object address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f(addr)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|