2020-11-25 09:53:17 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
2023-05-24 11:09:11 +00:00
|
|
|
"context"
|
2021-09-13 11:51:17 +00:00
|
|
|
"fmt"
|
2023-06-06 06:05:52 +00:00
|
|
|
"time"
|
2021-09-13 11:51:17 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
2023-06-06 06:05:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-06-06 06:05:52 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-06-21 07:52:03 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-25 09:53:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Iterate traverses the storage over the stored objects and calls the handler
|
|
|
|
// on each element.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely iterate over the storage.
|
|
|
|
//
|
2021-09-13 11:51:17 +00:00
|
|
|
// If handler returns an error, method wraps and returns it immediately.
|
2023-05-24 11:09:11 +00:00
|
|
|
func (b *BlobStor) Iterate(ctx context.Context, prm common.IteratePrm) (common.IterateRes, error) {
|
2023-06-06 06:05:52 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
b.metrics.Iterate(time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "BlobStor.Iterate",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Bool("ignore_errors", prm.IgnoreErrors),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-15 12:33:48 +00:00
|
|
|
b.modeMtx.RLock()
|
|
|
|
defer b.modeMtx.RUnlock()
|
|
|
|
|
2022-07-08 11:33:49 +00:00
|
|
|
for i := range b.storage {
|
2023-05-24 11:09:11 +00:00
|
|
|
_, err := b.storage[i].Storage.Iterate(ctx, prm)
|
2023-11-15 11:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
if prm.IgnoreErrors {
|
|
|
|
b.log.Warn(logs.BlobstorErrorOccurredDuringTheIteration,
|
|
|
|
zap.String("storage_path", b.storage[i].Storage.Path()),
|
|
|
|
zap.String("storage_type", b.storage[i].Storage.Type()),
|
|
|
|
zap.String("err", err.Error()))
|
|
|
|
continue
|
|
|
|
}
|
2022-07-11 12:34:17 +00:00
|
|
|
return common.IterateRes{}, fmt.Errorf("blobstor iterator failure: %w", err)
|
2022-07-08 11:33:49 +00:00
|
|
|
}
|
2022-07-07 12:03:45 +00:00
|
|
|
}
|
2023-06-06 06:05:52 +00:00
|
|
|
success = true
|
2022-07-07 12:03:45 +00:00
|
|
|
return common.IterateRes{}, nil
|
2021-09-13 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateBinaryObjects is a helper function which iterates over BlobStor and passes binary objects to f.
|
2022-06-21 07:52:03 +00:00
|
|
|
// Errors related to object reading and unmarshaling are logged and skipped.
|
2023-05-24 11:09:11 +00:00
|
|
|
func IterateBinaryObjects(ctx context.Context, blz *BlobStor, f func(addr oid.Address, data []byte, descriptor []byte) error) error {
|
2022-07-07 12:03:45 +00:00
|
|
|
var prm common.IteratePrm
|
2021-09-13 11:51:17 +00:00
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
prm.Handler = func(elem common.IterationElement) error {
|
|
|
|
return f(elem.Address, elem.ObjectData, elem.StorageID)
|
|
|
|
}
|
|
|
|
prm.IgnoreErrors = true
|
2021-09-13 11:51:17 +00:00
|
|
|
|
2023-05-24 11:09:11 +00:00
|
|
|
_, err := blz.Iterate(ctx, prm)
|
2021-09-13 11:51:17 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|