2021-04-08 13:53:25 +00:00
|
|
|
package fstree
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2021-04-08 13:53:25 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
2021-10-19 16:20:53 +00:00
|
|
|
"fmt"
|
2021-06-28 14:01:31 +00:00
|
|
|
"io/fs"
|
2021-04-08 13:53:25 +00:00
|
|
|
"os"
|
2021-10-19 16:20:53 +00:00
|
|
|
"path/filepath"
|
2023-02-09 16:13:51 +00:00
|
|
|
"strconv"
|
2021-04-06 10:56:06 +00:00
|
|
|
"strings"
|
2022-09-28 08:10:27 +00:00
|
|
|
"syscall"
|
2023-06-02 13:04:02 +00:00
|
|
|
"time"
|
2021-04-08 13:53:25 +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/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/compression"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2023-11-15 11:18:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
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"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-11-15 11:18:03 +00:00
|
|
|
"go.uber.org/zap"
|
2021-04-08 13:53:25 +00:00
|
|
|
)
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
type keyLock interface {
|
|
|
|
Lock(string)
|
|
|
|
Unlock(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopKeyLock struct{}
|
|
|
|
|
|
|
|
func (l *noopKeyLock) Lock(string) {}
|
|
|
|
func (l *noopKeyLock) Unlock(string) {}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// FSTree represents an object storage as a filesystem tree.
|
2021-04-08 13:53:25 +00:00
|
|
|
type FSTree struct {
|
|
|
|
Info
|
|
|
|
|
2023-11-15 11:18:03 +00:00
|
|
|
log *logger.Logger
|
|
|
|
|
2022-08-19 14:29:53 +00:00
|
|
|
*compression.Config
|
2022-09-26 16:38:03 +00:00
|
|
|
Depth uint64
|
2021-04-08 13:53:25 +00:00
|
|
|
DirNameLen int
|
2022-08-23 13:04:01 +00:00
|
|
|
|
2022-10-28 09:57:36 +00:00
|
|
|
noSync bool
|
2022-08-23 13:04:01 +00:00
|
|
|
readOnly bool
|
2023-06-02 13:04:02 +00:00
|
|
|
metrics Metrics
|
2023-08-11 08:32:43 +00:00
|
|
|
|
2024-02-08 15:04:18 +00:00
|
|
|
fileCounter FileCounter
|
2023-08-11 08:32:43 +00:00
|
|
|
|
2024-02-08 14:53:27 +00:00
|
|
|
writer writer
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info groups the information about file storage.
|
|
|
|
type Info struct {
|
|
|
|
// Permission bits of the root directory.
|
2021-06-28 14:01:31 +00:00
|
|
|
Permissions fs.FileMode
|
2021-04-08 13:53:25 +00:00
|
|
|
|
|
|
|
// Full path to the root directory.
|
|
|
|
RootPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DirNameLen is how many bytes is used to group keys into directories.
|
|
|
|
DirNameLen = 1 // in bytes
|
|
|
|
// MaxDepth is maximum depth of nested directories.
|
|
|
|
MaxDepth = (sha256.Size - 1) / DirNameLen
|
|
|
|
)
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
var _ common.Storage = (*FSTree)(nil)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
func New(opts ...Option) *FSTree {
|
|
|
|
f := &FSTree{
|
|
|
|
Info: Info{
|
2023-10-31 11:56:55 +00:00
|
|
|
Permissions: 0o700,
|
2022-07-11 12:34:17 +00:00
|
|
|
RootPath: "./",
|
|
|
|
},
|
2023-08-11 08:32:43 +00:00
|
|
|
Config: nil,
|
|
|
|
Depth: 4,
|
|
|
|
DirNameLen: DirNameLen,
|
|
|
|
metrics: &noopMetrics{},
|
|
|
|
fileCounter: &noopCounter{},
|
2023-11-15 11:18:03 +00:00
|
|
|
log: &logger.Logger{Logger: zap.L()},
|
2022-07-11 12:34:17 +00:00
|
|
|
}
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](f)
|
|
|
|
}
|
2024-02-08 15:04:18 +00:00
|
|
|
f.writer = newGenericWriteData(f.fileCounter, f.Permissions, f.noSync)
|
2022-07-11 12:34:17 +00:00
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func stringifyAddress(addr oid.Address) string {
|
|
|
|
return addr.Object().EncodeToString() + "." + addr.Container().EncodeToString()
|
2021-05-19 14:53:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 04:59:15 +00:00
|
|
|
func addressFromString(s string) (oid.Address, error) {
|
2023-02-27 14:19:35 +00:00
|
|
|
before, after, found := strings.Cut(s, ".")
|
|
|
|
if !found {
|
2023-02-10 04:59:15 +00:00
|
|
|
return oid.Address{}, errors.New("invalid address")
|
2021-05-19 14:53:51 +00:00
|
|
|
}
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var obj oid.ID
|
2023-02-27 14:19:35 +00:00
|
|
|
if err := obj.DecodeString(before); err != nil {
|
2023-02-10 04:59:15 +00:00
|
|
|
return oid.Address{}, err
|
2021-05-19 14:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var cnr cid.ID
|
2023-02-27 14:19:35 +00:00
|
|
|
if err := cnr.DecodeString(after); err != nil {
|
2023-02-10 04:59:15 +00:00
|
|
|
return oid.Address{}, err
|
2021-05-19 14:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addr oid.Address
|
|
|
|
addr.SetObject(obj)
|
|
|
|
addr.SetContainer(cnr)
|
2021-05-19 14:53:51 +00:00
|
|
|
|
2023-02-10 04:59:15 +00:00
|
|
|
return addr, nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// Iterate iterates over all stored objects.
|
2023-05-24 11:09:11 +00:00
|
|
|
func (t *FSTree) Iterate(ctx context.Context, prm common.IteratePrm) (common.IterateRes, error) {
|
2023-06-02 13:04:02 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
startedAt = time.Now()
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
t.metrics.Iterate(time.Since(startedAt), err == nil)
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "FSTree.Iterate",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("path", t.RootPath),
|
|
|
|
attribute.Bool("ignore_errors", prm.IgnoreErrors),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
err = t.iterate(ctx, 0, []string{t.RootPath}, prm)
|
|
|
|
return common.IterateRes{}, err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 11:09:11 +00:00
|
|
|
func (t *FSTree) iterate(ctx context.Context, depth uint64, curPath []string, prm common.IteratePrm) error {
|
2021-04-06 10:56:06 +00:00
|
|
|
curName := strings.Join(curPath[1:], "")
|
2023-11-15 11:18:03 +00:00
|
|
|
dirPath := filepath.Join(curPath...)
|
|
|
|
des, err := os.ReadDir(dirPath)
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.IgnoreErrors {
|
2023-11-15 11:18:03 +00:00
|
|
|
t.log.Warn(logs.BlobstorErrorOccurredDuringTheIteration,
|
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.String("directory_path", dirPath))
|
2022-01-20 15:53:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isLast := depth >= t.Depth
|
|
|
|
l := len(curPath)
|
|
|
|
curPath = append(curPath, "")
|
|
|
|
|
|
|
|
for i := range des {
|
2023-05-24 11:09:11 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
curPath[l] = des[i].Name()
|
|
|
|
|
|
|
|
if !isLast && des[i].IsDir() {
|
2023-05-24 11:09:11 +00:00
|
|
|
err := t.iterate(ctx, depth+1, curPath, prm)
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-01-20 15:53:29 +00:00
|
|
|
// Must be error from handler in case errors are ignored.
|
|
|
|
// Need to report.
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 14:53:51 +00:00
|
|
|
if depth != t.Depth {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := addressFromString(curName + des[i].Name())
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-15 11:18:03 +00:00
|
|
|
path := filepath.Join(curPath...)
|
|
|
|
data, err := os.ReadFile(path)
|
2023-03-21 18:31:48 +00:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-15 10:12:23 +00:00
|
|
|
if err == nil {
|
|
|
|
data, err = t.Decompress(data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if prm.IgnoreErrors {
|
2023-11-15 11:18:03 +00:00
|
|
|
t.log.Warn(logs.BlobstorErrorOccurredDuringTheIteration,
|
|
|
|
zap.Stringer("address", addr),
|
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.String("path", path))
|
2023-11-15 10:12:23 +00:00
|
|
|
continue
|
2022-01-20 15:53:29 +00:00
|
|
|
}
|
2023-11-15 10:12:23 +00:00
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 10:12:23 +00:00
|
|
|
err = prm.Handler(common.IterationElement{
|
|
|
|
Address: addr,
|
|
|
|
ObjectData: data,
|
|
|
|
StorageID: []byte{},
|
|
|
|
})
|
2022-05-31 14:11:48 +00:00
|
|
|
if err != nil {
|
2021-04-06 10:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (t *FSTree) treePath(addr oid.Address) string {
|
2021-04-08 13:53:25 +00:00
|
|
|
sAddr := stringifyAddress(addr)
|
|
|
|
|
2023-01-25 14:05:06 +00:00
|
|
|
var sb strings.Builder
|
|
|
|
sb.Grow(len(t.RootPath) + len(sAddr) + int(t.Depth) + 1)
|
|
|
|
sb.WriteString(t.RootPath)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-09-26 16:38:03 +00:00
|
|
|
for i := 0; uint64(i) < t.Depth; i++ {
|
2023-01-25 14:05:06 +00:00
|
|
|
sb.WriteRune(filepath.Separator)
|
|
|
|
sb.WriteString(sAddr[:t.DirNameLen])
|
2021-04-08 13:53:25 +00:00
|
|
|
sAddr = sAddr[t.DirNameLen:]
|
|
|
|
}
|
|
|
|
|
2023-01-25 14:05:06 +00:00
|
|
|
sb.WriteRune(filepath.Separator)
|
|
|
|
sb.WriteString(sAddr)
|
|
|
|
return sb.String()
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Delete removes the object with the specified address from the storage.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (t *FSTree) Delete(ctx context.Context, prm common.DeletePrm) (common.DeleteRes, error) {
|
2023-06-02 13:04:02 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
startedAt = time.Now()
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
t.metrics.Delete(time.Since(startedAt), err == nil)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "FSTree.Delete",
|
|
|
|
trace.WithAttributes(
|
2023-06-02 13:04:02 +00:00
|
|
|
attribute.String("path", t.RootPath),
|
2023-04-12 14:01:29 +00:00
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-23 13:04:01 +00:00
|
|
|
if t.readOnly {
|
2023-06-02 13:04:02 +00:00
|
|
|
err = common.ErrReadOnly
|
|
|
|
return common.DeleteRes{}, err
|
2022-08-23 13:04:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 13:53:10 +00:00
|
|
|
p := t.treePath(prm.Address)
|
2024-02-08 14:53:27 +00:00
|
|
|
err = t.writer.removeFile(p)
|
2022-08-23 12:44:51 +00:00
|
|
|
return common.DeleteRes{}, err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Exists returns the path to the file with object contents if it exists in the storage
|
2021-04-08 13:53:25 +00:00
|
|
|
// and an error otherwise.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (t *FSTree) Exists(ctx context.Context, prm common.ExistsPrm) (common.ExistsRes, error) {
|
2023-06-02 13:04:02 +00:00
|
|
|
var (
|
|
|
|
success = false
|
|
|
|
startedAt = time.Now()
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
t.metrics.Exists(time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "FSTree.Exists",
|
|
|
|
trace.WithAttributes(
|
2023-06-02 13:04:02 +00:00
|
|
|
attribute.String("path", t.RootPath),
|
2023-03-13 11:37:35 +00:00
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-01-25 13:53:10 +00:00
|
|
|
p := t.treePath(prm.Address)
|
|
|
|
|
|
|
|
_, err := os.Stat(p)
|
2022-07-08 07:09:48 +00:00
|
|
|
found := err == nil
|
2021-04-08 13:53:25 +00:00
|
|
|
if os.IsNotExist(err) {
|
2022-07-08 07:09:48 +00:00
|
|
|
err = nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
2023-06-02 13:04:02 +00:00
|
|
|
success = err == nil
|
2022-07-08 07:09:48 +00:00
|
|
|
return common.ExistsRes{Exists: found}, err
|
|
|
|
}
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Put puts an object in the storage.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (t *FSTree) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, error) {
|
2023-06-02 13:04:02 +00:00
|
|
|
var (
|
|
|
|
size int
|
|
|
|
startedAt = time.Now()
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
t.metrics.Put(time.Since(startedAt), size, err == nil)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "FSTree.Put",
|
|
|
|
trace.WithAttributes(
|
2023-06-02 13:04:02 +00:00
|
|
|
attribute.String("path", t.RootPath),
|
2023-04-12 14:01:29 +00:00
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
attribute.Bool("dont_compress", prm.DontCompress),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-23 13:04:01 +00:00
|
|
|
if t.readOnly {
|
2023-06-02 13:04:02 +00:00
|
|
|
err = common.ErrReadOnly
|
|
|
|
return common.PutRes{}, err
|
2022-08-23 13:04:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 13:41:35 +00:00
|
|
|
p := t.treePath(prm.Address)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2023-06-02 13:04:02 +00:00
|
|
|
if err = util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil {
|
2023-04-05 07:43:48 +00:00
|
|
|
if errors.Is(err, syscall.ENOSPC) {
|
2023-06-02 13:04:02 +00:00
|
|
|
err = common.ErrNoSpace
|
|
|
|
return common.PutRes{}, err
|
2023-04-05 07:43:48 +00:00
|
|
|
}
|
2022-07-06 13:41:35 +00:00
|
|
|
return common.PutRes{}, err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
2022-07-08 07:09:48 +00:00
|
|
|
if !prm.DontCompress {
|
|
|
|
prm.RawData = t.Compress(prm.RawData)
|
|
|
|
}
|
2022-09-28 08:10:27 +00:00
|
|
|
|
2023-06-02 13:04:02 +00:00
|
|
|
size = len(prm.RawData)
|
2024-02-08 14:53:27 +00:00
|
|
|
return common.PutRes{StorageID: []byte{}}, t.writer.writeData(p, prm.RawData)
|
2024-02-01 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Get returns an object from the storage by address.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (t *FSTree) Get(ctx context.Context, prm common.GetPrm) (common.GetRes, error) {
|
2023-06-02 13:04:02 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
size = 0
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
t.metrics.Get(time.Since(startedAt), size, success)
|
|
|
|
}()
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "FSTree.Get",
|
|
|
|
trace.WithAttributes(
|
2023-06-02 13:04:02 +00:00
|
|
|
attribute.String("path", t.RootPath),
|
2023-03-13 11:37:35 +00:00
|
|
|
attribute.Bool("raw", prm.Raw),
|
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-05 14:07:40 +00:00
|
|
|
p := t.treePath(prm.Address)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
var data []byte
|
|
|
|
var err error
|
|
|
|
{
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "FSTree.Get.ReadFile")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
data, err = os.ReadFile(p)
|
|
|
|
if err != nil {
|
2023-08-10 15:46:24 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return common.GetRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
|
|
|
}
|
2023-03-13 11:37:35 +00:00
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
2022-07-08 07:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err = t.Decompress(data)
|
|
|
|
if err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
2023-06-02 13:04:02 +00:00
|
|
|
size = len(data)
|
2022-07-08 07:09:48 +00:00
|
|
|
|
|
|
|
obj := objectSDK.New()
|
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
|
|
return common.GetRes{}, err
|
|
|
|
}
|
2023-06-02 13:04:02 +00:00
|
|
|
success = true
|
|
|
|
return common.GetRes{Object: obj, RawData: data}, nil
|
2022-07-08 07:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRange implements common.Storage.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (t *FSTree) GetRange(ctx context.Context, prm common.GetRangePrm) (common.GetRangeRes, error) {
|
2023-06-02 13:04:02 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
size = 0
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
t.metrics.GetRange(time.Since(startedAt), size, success)
|
|
|
|
}()
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "FSTree.GetRange",
|
|
|
|
trace.WithAttributes(
|
2023-06-02 13:04:02 +00:00
|
|
|
attribute.String("path", t.RootPath),
|
2023-03-13 11:37:35 +00:00
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
attribute.String("offset", strconv.FormatUint(prm.Range.GetOffset(), 10)),
|
|
|
|
attribute.String("length", strconv.FormatUint(prm.Range.GetLength(), 10)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
res, err := t.Get(ctx, common.GetPrm{Address: prm.Address})
|
2022-07-08 07:09:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.GetRangeRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := res.Object.Payload()
|
|
|
|
from := prm.Range.GetOffset()
|
|
|
|
to := from + prm.Range.GetLength()
|
|
|
|
|
|
|
|
if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to {
|
2023-08-04 11:14:07 +00:00
|
|
|
return common.GetRangeRes{}, logicerr.Wrap(new(apistatus.ObjectOutOfRange))
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 13:04:02 +00:00
|
|
|
success = true
|
|
|
|
data := payload[from:to]
|
|
|
|
size = len(data)
|
2022-07-08 07:09:48 +00:00
|
|
|
return common.GetRangeRes{
|
2023-06-02 13:04:02 +00:00
|
|
|
Data: data,
|
2022-07-08 07:09:48 +00:00
|
|
|
}, nil
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
2021-10-19 16:20:53 +00:00
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
// initFileCounter walks the file tree rooted at FSTree's root,
|
|
|
|
// counts total items count, inits counter and returns number of stored objects.
|
|
|
|
func (t *FSTree) initFileCounter() error {
|
2024-02-08 15:04:18 +00:00
|
|
|
if !counterEnabled(t.fileCounter) {
|
2023-08-11 08:32:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-19 16:20:53 +00:00
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
counter, err := t.countFiles()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.fileCounter.Set(counter)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FSTree) countFiles() (uint64, error) {
|
|
|
|
var counter uint64
|
2021-10-19 16:20:53 +00:00
|
|
|
// it is simpler to just consider every file
|
|
|
|
// that is not directory as an object
|
|
|
|
err := filepath.WalkDir(t.RootPath,
|
|
|
|
func(_ string, d fs.DirEntry, _ error) error {
|
|
|
|
if !d.IsDir() {
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not walk through %s directory: %w", t.RootPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter, nil
|
|
|
|
}
|
2022-07-08 11:33:49 +00:00
|
|
|
|
2024-03-12 14:36:26 +00:00
|
|
|
func (t *FSTree) ObjectsCount(ctx context.Context) (uint64, error) {
|
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
t.metrics.ObjectsCount(time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "FSTree.ObjectsCount",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("path", t.RootPath),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var result uint64
|
|
|
|
|
|
|
|
err := filepath.WalkDir(t.RootPath,
|
|
|
|
func(_ string, d fs.DirEntry, _ error) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if !d.IsDir() {
|
|
|
|
result++
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not walk through %s directory: %w", t.RootPath, err)
|
|
|
|
}
|
|
|
|
success = true
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-09-20 12:42:56 +00:00
|
|
|
// Type is fstree storage type used in logs and configuration.
|
|
|
|
const Type = "fstree"
|
|
|
|
|
2022-07-08 11:33:49 +00:00
|
|
|
// Type implements common.Storage.
|
|
|
|
func (*FSTree) Type() string {
|
2022-09-20 12:42:56 +00:00
|
|
|
return Type
|
2022-07-08 11:33:49 +00:00
|
|
|
}
|
2022-07-11 12:34:17 +00:00
|
|
|
|
2022-10-05 12:11:12 +00:00
|
|
|
// Path implements common.Storage.
|
|
|
|
func (t *FSTree) Path() string {
|
|
|
|
return t.RootPath
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:34:17 +00:00
|
|
|
// SetCompressor implements common.Storage.
|
2022-08-19 14:29:53 +00:00
|
|
|
func (t *FSTree) SetCompressor(cc *compression.Config) {
|
|
|
|
t.Config = cc
|
2022-07-11 12:34:17 +00:00
|
|
|
}
|
2022-11-09 10:59:24 +00:00
|
|
|
|
2023-08-09 12:54:08 +00:00
|
|
|
func (t *FSTree) Compressor() *compression.Config {
|
|
|
|
return t.Config
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:59:24 +00:00
|
|
|
// SetReportErrorFunc implements common.Storage.
|
2023-04-26 08:24:40 +00:00
|
|
|
func (t *FSTree) SetReportErrorFunc(_ func(string, error)) {
|
2022-11-09 10:59:24 +00:00
|
|
|
// Do nothing, FSTree can encounter only one error which is returned.
|
|
|
|
}
|
2023-06-07 11:39:03 +00:00
|
|
|
|
|
|
|
func (t *FSTree) SetParentID(parentID string) {
|
|
|
|
t.metrics.SetParentID(parentID)
|
|
|
|
}
|
2023-09-21 15:18:20 +00:00
|
|
|
|
|
|
|
func (t *FSTree) Rebuild(_ context.Context, _ common.RebuildPrm) (common.RebuildRes, error) {
|
|
|
|
return common.RebuildRes{}, nil
|
|
|
|
}
|