2022-07-18 06:16:36 +00:00
|
|
|
package blobovniczatree
|
|
|
|
|
|
|
|
import (
|
2023-05-24 11:09:11 +00:00
|
|
|
"context"
|
2022-07-18 06:16:36 +00:00
|
|
|
"fmt"
|
2023-09-20 14:46:10 +00:00
|
|
|
"os"
|
2022-07-18 06:16:36 +00:00
|
|
|
"path/filepath"
|
2023-09-19 15:08:38 +00:00
|
|
|
"strings"
|
2023-06-05 07:25:25 +00:00
|
|
|
"time"
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-11-15 11:18:03 +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/blobovnicza"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
2023-06-05 07:25:25 +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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2023-06-05 07:25:25 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-11-15 11:18:03 +00:00
|
|
|
"go.uber.org/zap"
|
2022-07-18 06:16:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Iterate iterates over all objects in b.
|
2023-05-28 19:37:37 +00:00
|
|
|
func (b *Blobovniczas) Iterate(ctx context.Context, prm common.IteratePrm) (common.IterateRes, error) {
|
2023-06-05 07:25:25 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
b.metrics.Iterate(time.Since(startedAt), err == nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Blobovniczas.Iterate",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("path", b.rootPath),
|
|
|
|
attribute.Bool("ignore_errors", prm.IgnoreErrors),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
err = b.iterateBlobovniczas(ctx, prm.IgnoreErrors, func(p string, blz *blobovnicza.Blobovnicza) error {
|
2022-08-23 08:43:01 +00:00
|
|
|
var subPrm blobovnicza.IteratePrm
|
|
|
|
subPrm.SetHandler(func(elem blobovnicza.IterationElement) error {
|
|
|
|
data, err := b.compression.Decompress(elem.ObjectData())
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
if prm.IgnoreErrors {
|
2023-11-15 11:18:03 +00:00
|
|
|
b.log.Warn(logs.BlobstorErrorOccurredDuringTheIteration,
|
|
|
|
zap.Stringer("address", elem.Address()),
|
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.String("storage_id", p),
|
|
|
|
zap.String("root_path", b.rootPath))
|
2022-07-18 06:16:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("could not decompress object data: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:16:35 +00:00
|
|
|
if prm.Handler != nil {
|
|
|
|
return prm.Handler(common.IterationElement{
|
|
|
|
Address: elem.Address(),
|
|
|
|
ObjectData: data,
|
2023-09-19 15:08:38 +00:00
|
|
|
StorageID: []byte(strings.TrimSuffix(p, dbExtension)),
|
2022-08-22 14:16:35 +00:00
|
|
|
})
|
|
|
|
}
|
2023-11-15 10:12:23 +00:00
|
|
|
return nil
|
2022-07-18 06:16:36 +00:00
|
|
|
})
|
2022-08-23 08:43:01 +00:00
|
|
|
subPrm.DecodeAddresses()
|
|
|
|
|
2023-05-28 19:37:37 +00:00
|
|
|
_, err := blz.Iterate(ctx, subPrm)
|
2022-08-23 08:43:01 +00:00
|
|
|
return err
|
2022-07-18 06:16:36 +00:00
|
|
|
})
|
2023-06-05 07:25:25 +00:00
|
|
|
return common.IterateRes{}, err
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// iterator over all Blobovniczas in unsorted order. Break on f's error return.
|
2023-05-28 19:37:37 +00:00
|
|
|
func (b *Blobovniczas) iterateBlobovniczas(ctx context.Context, ignoreErrors bool, f func(string, *blobovnicza.Blobovnicza) error) error {
|
2023-09-20 14:46:10 +00:00
|
|
|
return b.iterateExistingDBPaths(ctx, func(p string) (bool, error) {
|
2023-08-31 08:32:09 +00:00
|
|
|
shBlz := b.getBlobovnicza(p)
|
2023-08-30 20:36:48 +00:00
|
|
|
blz, err := shBlz.Open()
|
2022-07-18 06:16:36 +00:00
|
|
|
if err != nil {
|
|
|
|
if ignoreErrors {
|
2023-11-15 11:18:03 +00:00
|
|
|
b.log.Warn(logs.BlobstorErrorOccurredDuringTheIteration,
|
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.String("storage_id", p),
|
|
|
|
zap.String("root_path", b.rootPath))
|
2022-07-18 06:16:36 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("could not open blobovnicza %s: %w", p, err)
|
|
|
|
}
|
2023-08-30 20:36:48 +00:00
|
|
|
defer shBlz.Close()
|
2022-07-18 06:16:36 +00:00
|
|
|
|
|
|
|
err = f(p, blz)
|
|
|
|
|
|
|
|
return err != nil, err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
// iterateSortedLeaves iterates over the paths of Blobovniczas sorted by weight.
|
|
|
|
//
|
|
|
|
// Uses depth, width and leaf width for iteration.
|
2023-05-28 19:37:37 +00:00
|
|
|
func (b *Blobovniczas) iterateSortedLeaves(ctx context.Context, addr *oid.Address, f func(string) (bool, error)) error {
|
2022-07-18 06:16:36 +00:00
|
|
|
_, err := b.iterateSorted(
|
2023-05-28 19:37:37 +00:00
|
|
|
ctx,
|
2022-07-18 06:16:36 +00:00
|
|
|
addr,
|
|
|
|
make([]string, 0, b.blzShallowDepth),
|
|
|
|
b.blzShallowDepth,
|
|
|
|
func(p []string) (bool, error) { return f(filepath.Join(p...)) },
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterator over directories with Blobovniczas sorted by weight.
|
2023-05-28 19:37:37 +00:00
|
|
|
func (b *Blobovniczas) iterateDeepest(ctx context.Context, addr oid.Address, f func(string) (bool, error)) error {
|
2022-07-18 06:16:36 +00:00
|
|
|
depth := b.blzShallowDepth
|
|
|
|
if depth > 0 {
|
|
|
|
depth--
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.iterateSorted(
|
2023-05-28 19:37:37 +00:00
|
|
|
ctx,
|
2022-07-18 06:16:36 +00:00
|
|
|
&addr,
|
|
|
|
make([]string, 0, depth),
|
|
|
|
depth,
|
|
|
|
func(p []string) (bool, error) { return f(filepath.Join(p...)) },
|
|
|
|
)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterator over particular level of directories.
|
2023-05-28 19:37:37 +00:00
|
|
|
func (b *Blobovniczas) iterateSorted(ctx context.Context, addr *oid.Address, curPath []string, execDepth uint64, f func([]string) (bool, error)) (bool, error) {
|
2023-08-18 06:08:26 +00:00
|
|
|
isLeafLevel := uint64(len(curPath)) == b.blzShallowDepth
|
|
|
|
levelWidth := b.blzShallowWidth
|
|
|
|
if isLeafLevel {
|
2024-07-04 06:18:17 +00:00
|
|
|
hasDBs, maxIdx, err := getBlobovniczaMaxIndex(filepath.Join(append([]string{b.rootPath}, curPath...)...))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
levelWidth = 0
|
|
|
|
if hasDBs {
|
|
|
|
levelWidth = maxIdx + 1
|
|
|
|
}
|
2023-08-18 06:08:26 +00:00
|
|
|
}
|
|
|
|
indices := indexSlice(levelWidth)
|
2022-07-18 06:16:36 +00:00
|
|
|
|
2023-10-10 15:22:53 +00:00
|
|
|
if !isLeafLevel {
|
|
|
|
hrw.SortSliceByValue(indices, addressHash(addr, filepath.Join(curPath...)))
|
|
|
|
}
|
2022-07-18 06:16:36 +00:00
|
|
|
|
|
|
|
exec := uint64(len(curPath)) == execDepth
|
|
|
|
|
|
|
|
for i := range indices {
|
2023-05-28 19:37:37 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2023-09-19 15:08:38 +00:00
|
|
|
|
|
|
|
lastPart := u64ToHexString(indices[i])
|
|
|
|
if isLeafLevel {
|
|
|
|
lastPart = u64ToHexStringExt(indices[i])
|
|
|
|
}
|
|
|
|
|
2022-07-18 06:16:36 +00:00
|
|
|
if i == 0 {
|
2023-09-19 15:08:38 +00:00
|
|
|
curPath = append(curPath, lastPart)
|
2022-07-18 06:16:36 +00:00
|
|
|
} else {
|
2023-09-19 15:08:38 +00:00
|
|
|
curPath[len(curPath)-1] = lastPart
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if exec {
|
|
|
|
if stop, err := f(curPath); err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-05-28 19:37:37 +00:00
|
|
|
} else if stop, err := b.iterateSorted(ctx, addr, curPath, execDepth, f); err != nil {
|
2022-07-18 06:16:36 +00:00
|
|
|
return false, err
|
|
|
|
} else if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
// iterateExistingDBPaths iterates over the paths of Blobovniczas without any order.
|
|
|
|
//
|
|
|
|
// Uses existed blobovnicza files for iteration.
|
|
|
|
func (b *Blobovniczas) iterateExistingDBPaths(ctx context.Context, f func(string) (bool, error)) error {
|
2023-09-27 13:25:15 +00:00
|
|
|
b.dbFilesGuard.RLock()
|
|
|
|
defer b.dbFilesGuard.RUnlock()
|
|
|
|
|
2024-08-27 12:51:55 +00:00
|
|
|
_, err := b.iterateExistingPathsDFS(ctx, "", f, func(path string) bool { return !strings.HasSuffix(path, rebuildSuffix) })
|
2023-09-20 14:46:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-27 12:51:55 +00:00
|
|
|
func (b *Blobovniczas) iterateExistingPathsDFS(ctx context.Context, path string, f func(string) (bool, error), fileFilter func(path string) bool) (bool, error) {
|
2023-09-20 14:46:10 +00:00
|
|
|
sysPath := filepath.Join(b.rootPath, path)
|
|
|
|
entries, err := os.ReadDir(sysPath)
|
|
|
|
if os.IsNotExist(err) && b.readOnly && path == "" { // non initialized tree in read only mode
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if entry.IsDir() {
|
2024-08-27 12:51:55 +00:00
|
|
|
stop, err := b.iterateExistingPathsDFS(ctx, filepath.Join(path, entry.Name()), f, fileFilter)
|
2023-09-20 14:46:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
} else {
|
2024-08-27 12:51:55 +00:00
|
|
|
if !fileFilter(entry.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-20 14:46:10 +00:00
|
|
|
stop, err := f(filepath.Join(path, entry.Name()))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-08-27 12:51:55 +00:00
|
|
|
// iterateIncompletedRebuildDBPaths iterates over the paths of Blobovniczas with incompleted rebuild files without any order.
|
|
|
|
func (b *Blobovniczas) iterateIncompletedRebuildDBPaths(ctx context.Context, f func(string) (bool, error)) error {
|
|
|
|
b.dbFilesGuard.RLock()
|
|
|
|
defer b.dbFilesGuard.RUnlock()
|
|
|
|
|
|
|
|
_, err := b.iterateExistingPathsDFS(ctx, "", f, func(path string) bool { return strings.HasSuffix(path, rebuildSuffix) })
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
func (b *Blobovniczas) iterateSortedDBPaths(ctx context.Context, addr oid.Address, f func(string) (bool, error)) error {
|
2023-09-27 13:25:15 +00:00
|
|
|
b.dbFilesGuard.RLock()
|
|
|
|
defer b.dbFilesGuard.RUnlock()
|
|
|
|
|
2023-09-20 14:46:10 +00:00
|
|
|
_, err := b.iterateSordedDBPathsInternal(ctx, "", addr, f)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Blobovniczas) iterateSordedDBPathsInternal(ctx context.Context, path string, addr oid.Address, f func(string) (bool, error)) (bool, error) {
|
|
|
|
sysPath := filepath.Join(b.rootPath, path)
|
|
|
|
entries, err := os.ReadDir(sysPath)
|
|
|
|
if os.IsNotExist(err) && b.readOnly && path == "" { // non initialized tree in read only mode
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
var dbIdxs []uint64
|
|
|
|
var dirIdxs []uint64
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
2024-08-27 12:51:55 +00:00
|
|
|
if strings.HasSuffix(entry.Name(), rebuildSuffix) {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-20 14:46:10 +00:00
|
|
|
idx := u64FromHexString(entry.Name())
|
|
|
|
if entry.IsDir() {
|
|
|
|
dirIdxs = append(dirIdxs, idx)
|
|
|
|
} else {
|
|
|
|
dbIdxs = append(dbIdxs, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dbIdxs) > 0 {
|
|
|
|
for _, dbIdx := range dbIdxs {
|
|
|
|
dbPath := filepath.Join(path, u64ToHexStringExt(dbIdx))
|
|
|
|
stop, err := f(dbPath)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dirIdxs) > 0 {
|
|
|
|
hrw.SortSliceByValue(dirIdxs, addressHash(&addr, path))
|
|
|
|
for _, dirIdx := range dirIdxs {
|
|
|
|
dirPath := filepath.Join(path, u64ToHexString(dirIdx))
|
|
|
|
stop, err := b.iterateSordedDBPathsInternal(ctx, dirPath, addr, f)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
2022-07-18 06:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// makes slice of uint64 values from 0 to number-1.
|
|
|
|
func indexSlice(number uint64) []uint64 {
|
|
|
|
s := make([]uint64, number)
|
|
|
|
|
|
|
|
for i := range s {
|
|
|
|
s[i] = uint64(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|