2021-02-16 15:56:59 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2024-11-13 07:07:32 +00:00
|
|
|
"bytes"
|
2023-06-06 09:27:19 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-16 15:56:59 +00:00
|
|
|
"strconv"
|
2023-06-06 09:27:19 +00:00
|
|
|
"time"
|
2021-02-16 15:56:59 +00:00
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-06-06 09:27:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-02-16 15:56:59 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-06-06 09:27:19 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-02-16 15:56:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ExpiredObject is a descriptor of expired object from DB.
|
|
|
|
type ExpiredObject struct {
|
2023-07-06 12:36:41 +00:00
|
|
|
typ objectSDK.Type
|
2021-02-16 15:56:59 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns type of the expired object.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (e *ExpiredObject) Type() objectSDK.Type {
|
2021-02-16 15:56:59 +00:00
|
|
|
return e.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns address of the expired object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (e *ExpiredObject) Address() oid.Address {
|
2021-02-16 15:56:59 +00:00
|
|
|
return e.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpiredObjectHandler is an ExpiredObject handling function.
|
|
|
|
type ExpiredObjectHandler func(*ExpiredObject) error
|
|
|
|
|
|
|
|
// ErrInterruptIterator is returned by iteration handlers
|
|
|
|
// as a "break" keyword.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrInterruptIterator = logicerr.New("iterator is interrupted")
|
2021-02-16 15:56:59 +00:00
|
|
|
|
|
|
|
// IterateExpired iterates over all objects in DB which are out of date
|
2022-02-15 18:22:06 +00:00
|
|
|
// relative to epoch. Locked objects are not included (do not confuse
|
|
|
|
// with objects of type LOCK).
|
2021-02-16 15:56:59 +00:00
|
|
|
//
|
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
2023-06-06 09:27:19 +00:00
|
|
|
func (db *DB) IterateExpired(ctx context.Context, epoch uint64, h ExpiredObjectHandler) error {
|
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("IterateExpired", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.IterateExpired",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("epoch", strconv.FormatUint(epoch, 10)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
err := metaerr.Wrap(db.boltDB.View(func(tx *bbolt.Tx) error {
|
2021-02-16 15:56:59 +00:00
|
|
|
return db.iterateExpired(tx, epoch, h)
|
2023-06-15 10:19:36 +00:00
|
|
|
}))
|
2023-06-06 09:27:19 +00:00
|
|
|
success = err == nil
|
|
|
|
return err
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) iterateExpired(tx *bbolt.Tx, epoch uint64, h ExpiredObjectHandler) error {
|
2024-08-20 08:59:42 +00:00
|
|
|
b := tx.Bucket(expEpochToObjectBucketName)
|
|
|
|
c := b.Cursor()
|
|
|
|
for k, _ := c.First(); k != nil; k, _ = c.Next() {
|
|
|
|
expiresAfter, cnr, obj, err := parseExpirationEpochKey(k)
|
2022-09-08 11:54:21 +00:00
|
|
|
if err != nil {
|
2024-08-20 08:59:42 +00:00
|
|
|
return err
|
2022-09-08 11:54:21 +00:00
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
// bucket keys ordered by epoch, no need to continue lookup
|
|
|
|
if expiresAfter >= epoch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if objectLocked(tx, cnr, obj) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnr)
|
|
|
|
addr.SetObject(obj)
|
|
|
|
objKey := objectKey(addr.Object(), make([]byte, objectKeySize))
|
|
|
|
err = h(&ExpiredObject{
|
|
|
|
typ: firstIrregularObjectType(tx, cnr, objKey),
|
|
|
|
addr: addr,
|
2021-02-16 15:56:59 +00:00
|
|
|
})
|
2024-08-20 08:59:42 +00:00
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if errors.Is(err, ErrInterruptIterator) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
return nil
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 12:22:13 +00:00
|
|
|
// IterateCoveredByTombstones iterates over all objects in DB which are covered
|
2022-02-15 18:24:12 +00:00
|
|
|
// by tombstone with string address from tss. Locked objects are not included
|
|
|
|
// (do not confuse with objects of type LOCK).
|
2021-02-17 12:22:13 +00:00
|
|
|
//
|
|
|
|
// If h returns ErrInterruptIterator, nil returns immediately.
|
|
|
|
// Returns other errors of h directly.
|
|
|
|
//
|
|
|
|
// Does not modify tss.
|
2023-06-06 09:27:19 +00:00
|
|
|
func (db *DB) IterateCoveredByTombstones(ctx context.Context, tss map[string]oid.Address, h func(oid.Address) error) error {
|
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("IterateCoveredByTombstones", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.IterateCoveredByTombstones")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2021-02-17 12:22:13 +00:00
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
return db.iterateCoveredByTombstones(tx, tss, h)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) iterateCoveredByTombstones(tx *bbolt.Tx, tss map[string]oid.Address, h func(oid.Address) error) error {
|
2021-02-17 12:22:13 +00:00
|
|
|
bktGraveyard := tx.Bucket(graveyardBucketName)
|
|
|
|
|
|
|
|
err := bktGraveyard.ForEach(func(k, v []byte) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
var addr oid.Address
|
|
|
|
if err := decodeAddressFromKey(&addr, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, ok := tss[addr.EncodeToString()]; ok {
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
|
|
|
|
err := decodeAddressFromKey(&addr, k)
|
2021-02-17 12:22:13 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not parse address of the object under tombstone: %w", err)
|
2021-02-17 12:22:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if objectLocked(tx, addr.Container(), addr.Object()) {
|
2022-02-15 18:24:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-17 12:22:13 +00:00
|
|
|
return h(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if errors.Is(err, ErrInterruptIterator) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-08-25 15:58:18 +00:00
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
func iteratePhyObjects(tx *bbolt.Tx, f func(cid.ID, oid.ID, *objectSDK.Object) error) error {
|
2022-08-25 15:58:18 +00:00
|
|
|
var cid cid.ID
|
|
|
|
var oid oid.ID
|
2023-12-05 14:00:16 +00:00
|
|
|
obj := objectSDK.New()
|
2022-08-25 15:58:18 +00:00
|
|
|
|
|
|
|
return tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
b58CID, postfix := parseContainerIDWithPrefix(&cid, name)
|
2022-08-25 15:58:18 +00:00
|
|
|
if len(b58CID) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch postfix {
|
2022-09-08 11:54:21 +00:00
|
|
|
case primaryPrefix,
|
|
|
|
lockersPrefix,
|
|
|
|
tombstonePrefix:
|
2022-08-25 15:58:18 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.ForEach(func(k, v []byte) error {
|
2024-11-13 07:07:32 +00:00
|
|
|
if oid.Decode(k) == nil && obj.Unmarshal(bytes.Clone(v)) == nil {
|
2023-12-05 14:00:16 +00:00
|
|
|
return f(cid, oid, obj)
|
2022-08-25 15:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|