frostfs-node/pkg/local_object_storage/metabase/iterators.go
Aleksey Savchuk e0ac3a583f
All checks were successful
Tests and linters / Run gofumpt (pull_request) Successful in 3m15s
DCO action / DCO (pull_request) Successful in 3m26s
Pre-commit hooks / Pre-commit (pull_request) Successful in 4m38s
Tests and linters / gopls check (pull_request) Successful in 4m53s
Tests and linters / Lint (pull_request) Successful in 5m16s
Build / Build Components (pull_request) Successful in 5m51s
Vulncheck / Vulncheck (pull_request) Successful in 5m47s
Tests and linters / Staticcheck (pull_request) Successful in 8m3s
Tests and linters / Tests (pull_request) Successful in 9m16s
Tests and linters / Tests with -race (pull_request) Successful in 9m19s
Tests and linters / Run gofumpt (push) Successful in 2m8s
Vulncheck / Vulncheck (push) Successful in 2m41s
Build / Build Components (push) Successful in 3m18s
Tests and linters / Staticcheck (push) Successful in 3m15s
Pre-commit hooks / Pre-commit (push) Successful in 3m49s
Tests and linters / gopls check (push) Successful in 4m14s
Tests and linters / Lint (push) Successful in 4m21s
Tests and linters / Tests (push) Successful in 4m24s
Tests and linters / Tests with -race (push) Successful in 4m36s
[#1523] metabase: Remove (*DB).IterateCoveredByTombstones
Remove this method because it isn't used anywhere since 7799f8e4c.

Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
2024-11-29 10:49:24 +00:00

140 lines
3.5 KiB
Go

package meta
import (
"bytes"
"context"
"errors"
"strconv"
"time"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"go.etcd.io/bbolt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// ExpiredObject is a descriptor of expired object from DB.
type ExpiredObject struct {
typ objectSDK.Type
addr oid.Address
}
// Type returns type of the expired object.
func (e *ExpiredObject) Type() objectSDK.Type {
return e.typ
}
// Address returns address of the expired object.
func (e *ExpiredObject) Address() oid.Address {
return e.addr
}
// ExpiredObjectHandler is an ExpiredObject handling function.
type ExpiredObjectHandler func(*ExpiredObject) error
// ErrInterruptIterator is returned by iteration handlers
// as a "break" keyword.
var ErrInterruptIterator = logicerr.New("iterator is interrupted")
// IterateExpired iterates over all objects in DB which are out of date
// relative to epoch. Locked objects are not included (do not confuse
// with objects of type LOCK).
//
// If h returns ErrInterruptIterator, nil returns immediately.
// Returns other errors of h directly.
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()
db.modeMtx.RLock()
defer db.modeMtx.RUnlock()
if db.mode.NoMetabase() {
return ErrDegradedMode
}
err := metaerr.Wrap(db.boltDB.View(func(tx *bbolt.Tx) error {
return db.iterateExpired(tx, epoch, h)
}))
success = err == nil
return err
}
func (db *DB) iterateExpired(tx *bbolt.Tx, epoch uint64, h ExpiredObjectHandler) error {
b := tx.Bucket(expEpochToObjectBucketName)
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
expiresAfter, cnr, obj, err := parseExpirationEpochKey(k)
if err != nil {
return err
}
// 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,
})
if err == nil {
continue
}
if errors.Is(err, ErrInterruptIterator) {
return nil
}
return err
}
return nil
}
func iteratePhyObjects(tx *bbolt.Tx, f func(cid.ID, oid.ID, *objectSDK.Object) error) error {
var cid cid.ID
var oid oid.ID
obj := objectSDK.New()
return tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
b58CID, postfix := parseContainerIDWithPrefix(&cid, name)
if len(b58CID) == 0 {
return nil
}
switch postfix {
case primaryPrefix,
lockersPrefix,
tombstonePrefix:
default:
return nil
}
return b.ForEach(func(k, v []byte) error {
if oid.Decode(k) == nil && obj.Unmarshal(bytes.Clone(v)) == nil {
return f(cid, oid, obj)
}
return nil
})
})
}