2023-03-28 08:17:15 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-20 08:59:42 +00:00
|
|
|
"encoding/binary"
|
2023-03-28 08:17:15 +00:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2023-06-06 09:27:19 +00:00
|
|
|
"time"
|
2023-03-28 08:17:15 +00:00
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-06-06 09:27:19 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-28 08:17:15 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"go.etcd.io/bbolt"
|
2023-06-06 09:27:19 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-03-28 08:17:15 +00:00
|
|
|
)
|
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
var errInvalidEpochValueLength = errors.New("could not parse expiration epoch: invalid data length")
|
|
|
|
|
2023-03-28 08:17:15 +00:00
|
|
|
// FilterExpired return expired items from addresses.
|
|
|
|
// Address considered expired if metabase does contain information about expiration and
|
|
|
|
// expiration epoch is less than epoch.
|
|
|
|
func (db *DB) FilterExpired(ctx context.Context, epoch uint64, addresses []oid.Address) ([]oid.Address, error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = true
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("FilterExpired", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.FilterExpired",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("epoch", strconv.FormatUint(epoch, 10)),
|
|
|
|
attribute.Int("addr_count", len(addresses)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-03-28 08:17:15 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return nil, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]oid.Address, 0, len(addresses))
|
|
|
|
containerIDToObjectIDs := make(map[cid.ID][]oid.ID)
|
|
|
|
for _, addr := range addresses {
|
|
|
|
containerIDToObjectIDs[addr.Container()] = append(containerIDToObjectIDs[addr.Container()], addr.Object())
|
|
|
|
}
|
|
|
|
|
|
|
|
err := db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
for containerID, objectIDs := range containerIDToObjectIDs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ErrInterruptIterator
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
expired, err := selectExpiredObjects(tx, epoch, containerID, objectIDs)
|
2023-03-28 08:17:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
result = append(result, expired...)
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-06-15 10:19:36 +00:00
|
|
|
return nil, metaerr.Wrap(err)
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
2023-06-06 09:27:19 +00:00
|
|
|
success = true
|
2023-03-28 08:17:15 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
func isExpired(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) (bool, error) {
|
|
|
|
bucketName := make([]byte, bucketKeySize)
|
|
|
|
bucketName = objectToExpirationEpochBucketName(addr.Container(), bucketName)
|
|
|
|
b := tx.Bucket(bucketName)
|
|
|
|
if b == nil {
|
|
|
|
return false, nil
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
key := make([]byte, objectKeySize)
|
|
|
|
addr.Object().Encode(key)
|
|
|
|
val := b.Get(key)
|
|
|
|
if len(val) == 0 {
|
|
|
|
return false, nil
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
if len(val) != epochSize {
|
|
|
|
return false, errInvalidEpochValueLength
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
expEpoch := binary.LittleEndian.Uint64(val)
|
|
|
|
return expEpoch < currEpoch, nil
|
|
|
|
}
|
2023-03-28 08:17:15 +00:00
|
|
|
|
2024-08-20 08:59:42 +00:00
|
|
|
func selectExpiredObjects(tx *bbolt.Tx, epoch uint64, containerID cid.ID, objectIDs []oid.ID) ([]oid.Address, error) {
|
|
|
|
result := make([]oid.Address, 0)
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(containerID)
|
|
|
|
for _, objID := range objectIDs {
|
|
|
|
addr.SetObject(objID)
|
|
|
|
expired, err := isExpired(tx, addr, epoch)
|
2023-03-28 08:17:15 +00:00
|
|
|
if err != nil {
|
2024-08-20 08:59:42 +00:00
|
|
|
return nil, err
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
2024-08-20 08:59:42 +00:00
|
|
|
if expired {
|
|
|
|
result = append(result, addr)
|
2023-03-28 08:17:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|