2021-02-16 07:57:43 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2021-04-15 10:58:10 +00:00
|
|
|
"bytes"
|
2022-04-19 18:00:22 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-02-16 07:57:43 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-02-16 07:57:43 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
// GarbageObject represents descriptor of the
|
|
|
|
// object that has been marked with GC.
|
|
|
|
type GarbageObject struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
// Address returns garbage object address.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (g GarbageObject) Address() oid.Address {
|
2021-02-16 07:57:43 +00:00
|
|
|
return g.addr
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
// GarbageHandler is a GarbageObject handling function.
|
|
|
|
type GarbageHandler func(GarbageObject) error
|
|
|
|
|
|
|
|
// GarbageIterationPrm groups parameters of the garbage
|
|
|
|
// iteration process.
|
|
|
|
type GarbageIterationPrm struct {
|
|
|
|
h GarbageHandler
|
2022-05-31 17:00:41 +00:00
|
|
|
offset *oid.Address
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHandler sets a handler that will be called on every
|
|
|
|
// GarbageObject.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (g *GarbageIterationPrm) SetHandler(h GarbageHandler) {
|
2021-04-15 10:58:10 +00:00
|
|
|
g.h = h
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOffset sets an offset of the iteration operation.
|
|
|
|
// The handler will be applied to the next after the
|
|
|
|
// specified offset if any are left.
|
|
|
|
//
|
|
|
|
// Note: if offset is not found in db, iteration starts
|
|
|
|
// from the element that WOULD BE the following after the
|
|
|
|
// offset if offset was presented. That means that it is
|
|
|
|
// safe to delete offset element and pass if to the
|
|
|
|
// iteration once again: iteration would start from the
|
|
|
|
// next element.
|
|
|
|
//
|
|
|
|
// Nil offset means start an integration from the beginning.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (g *GarbageIterationPrm) SetOffset(offset oid.Address) {
|
2022-05-31 17:00:41 +00:00
|
|
|
g.offset = &offset
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
2021-04-08 14:19:31 +00:00
|
|
|
|
|
|
|
// IterateOverGarbage iterates over all objects
|
|
|
|
// marked with GC mark.
|
|
|
|
//
|
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (db *DB) IterateOverGarbage(p GarbageIterationPrm) error {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
2021-04-15 10:58:10 +00:00
|
|
|
return db.iterateDeletedObj(tx, gcHandler{p.h}, p.offset)
|
2021-04-08 14:19:31 +00:00
|
|
|
})
|
|
|
|
}
|
2021-02-16 07:57:43 +00:00
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
// TombstonedObject represents descriptor of the
|
|
|
|
// object that has been covered with tombstone.
|
|
|
|
type TombstonedObject struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
|
|
|
tomb oid.Address
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns tombstoned object address.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (g TombstonedObject) Address() oid.Address {
|
2021-04-15 10:58:10 +00:00
|
|
|
return g.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tombstone returns address of a tombstone that
|
|
|
|
// covers object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (g TombstonedObject) Tombstone() oid.Address {
|
2021-04-15 10:58:10 +00:00
|
|
|
return g.tomb
|
|
|
|
}
|
|
|
|
|
|
|
|
// TombstonedHandler is a TombstonedObject handling function.
|
|
|
|
type TombstonedHandler func(object TombstonedObject) error
|
|
|
|
|
|
|
|
// GraveyardIterationPrm groups parameters of the graveyard
|
|
|
|
// iteration process.
|
|
|
|
type GraveyardIterationPrm struct {
|
|
|
|
h TombstonedHandler
|
2022-05-31 17:00:41 +00:00
|
|
|
offset *oid.Address
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHandler sets a handler that will be called on every
|
|
|
|
// TombstonedObject.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (g *GraveyardIterationPrm) SetHandler(h TombstonedHandler) {
|
2021-04-15 10:58:10 +00:00
|
|
|
g.h = h
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOffset sets an offset of the iteration operation.
|
|
|
|
// The handler will be applied to the next after the
|
|
|
|
// specified offset if any are left.
|
|
|
|
//
|
|
|
|
// Note: if offset is not found in db, iteration starts
|
|
|
|
// from the element that WOULD BE the following after the
|
|
|
|
// offset if offset was presented. That means that it is
|
|
|
|
// safe to delete offset element and pass if to the
|
|
|
|
// iteration once again: iteration would start from the
|
|
|
|
// next element.
|
|
|
|
//
|
|
|
|
// Nil offset means start an integration from the beginning.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (g *GraveyardIterationPrm) SetOffset(offset oid.Address) {
|
2022-05-31 17:00:41 +00:00
|
|
|
g.offset = &offset
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 07:57:43 +00:00
|
|
|
// IterateOverGraveyard iterates over all graves in DB.
|
|
|
|
//
|
2021-02-19 08:19:28 +00:00
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (db *DB) IterateOverGraveyard(p GraveyardIterationPrm) error {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2021-02-16 07:57:43 +00:00
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
2021-04-15 10:58:10 +00:00
|
|
|
return db.iterateDeletedObj(tx, graveyardHandler{p.h}, p.offset)
|
2021-02-16 07:57:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
type kvHandler interface {
|
|
|
|
handleKV(k, v []byte) error
|
|
|
|
}
|
2021-04-08 14:19:31 +00:00
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
type gcHandler struct {
|
|
|
|
h GarbageHandler
|
|
|
|
}
|
2021-04-08 14:19:31 +00:00
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
func (g gcHandler) handleKV(k, _ []byte) error {
|
|
|
|
o, err := garbageFromKV(k)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse garbage object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.h(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
type graveyardHandler struct {
|
|
|
|
h TombstonedHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g graveyardHandler) handleKV(k, v []byte) error {
|
|
|
|
o, err := graveFromKV(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse grave: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.h(o)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) iterateDeletedObj(tx *bbolt.Tx, h kvHandler, offset *oid.Address) error {
|
2021-04-08 14:19:31 +00:00
|
|
|
var bkt *bbolt.Bucket
|
2021-04-15 10:58:10 +00:00
|
|
|
switch t := h.(type) {
|
|
|
|
case graveyardHandler:
|
2021-04-08 14:19:31 +00:00
|
|
|
bkt = tx.Bucket(graveyardBucketName)
|
2021-04-15 10:58:10 +00:00
|
|
|
case gcHandler:
|
2021-04-08 14:19:31 +00:00
|
|
|
bkt = tx.Bucket(garbageBucketName)
|
|
|
|
default:
|
2021-04-15 10:58:10 +00:00
|
|
|
panic(fmt.Sprintf("metabase: unknown iteration object hadler: %T", t))
|
2021-04-08 14:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
c := bkt.Cursor()
|
|
|
|
var k, v []byte
|
|
|
|
|
|
|
|
if offset == nil {
|
|
|
|
k, v = c.First()
|
|
|
|
} else {
|
2022-09-08 11:54:21 +00:00
|
|
|
rawAddr := addressKey(*offset, make([]byte, addressKeySize))
|
2021-04-15 10:58:10 +00:00
|
|
|
|
|
|
|
k, v = c.Seek(rawAddr)
|
|
|
|
if bytes.Equal(k, rawAddr) {
|
|
|
|
// offset was found, move
|
|
|
|
// cursor to the next element
|
|
|
|
k, v = c.Next()
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
2021-02-16 07:57:43 +00:00
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
for ; k != nil; k, v = c.Next() {
|
|
|
|
err := h.handleKV(k, v)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrInterruptIterator) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-19 08:19:28 +00:00
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-02-19 08:19:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 10:58:10 +00:00
|
|
|
return nil
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func garbageFromKV(k []byte) (res GarbageObject, err error) {
|
|
|
|
err = decodeAddressFromKey(&res.addr, k)
|
2021-02-16 07:57:43 +00:00
|
|
|
if err != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
err = fmt.Errorf("could not parse address: %w", err)
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return
|
2021-02-16 07:57:43 +00:00
|
|
|
}
|
2021-04-15 10:58:10 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func graveFromKV(k, v []byte) (res TombstonedObject, err error) {
|
|
|
|
if err = decodeAddressFromKey(&res.addr, k); err != nil {
|
|
|
|
err = fmt.Errorf("decode tombstone target from key: %w", err)
|
|
|
|
} else if err = decodeAddressFromKey(&res.tomb, v); err != nil {
|
|
|
|
err = fmt.Errorf("decode tombstone address from value: %w", err)
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return
|
2021-04-15 10:58:10 +00:00
|
|
|
}
|
2022-04-18 18:13:35 +00:00
|
|
|
|
|
|
|
// DropGraves deletes tombstoned objects from the
|
|
|
|
// graveyard bucket.
|
|
|
|
//
|
|
|
|
// Returns any error appeared during deletion process.
|
|
|
|
func (db *DB) DropGraves(tss []TombstonedObject) error {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
buf := make([]byte, addressKeySize)
|
|
|
|
|
2022-04-18 18:13:35 +00:00
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(graveyardBucketName)
|
|
|
|
if bkt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ts := range tss {
|
2022-09-08 11:54:21 +00:00
|
|
|
err := bkt.Delete(addressKey(ts.Address(), buf))
|
2022-04-18 18:13:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|