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-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-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"
|
2021-04-08 13:53:25 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
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
|
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{
|
|
|
|
Permissions: 0700,
|
|
|
|
RootPath: "./",
|
|
|
|
},
|
2022-08-19 14:29:53 +00:00
|
|
|
Config: nil,
|
2022-07-11 12:34:17 +00:00
|
|
|
Depth: 4,
|
|
|
|
DirNameLen: DirNameLen,
|
2023-06-02 13:04:02 +00:00
|
|
|
metrics: &noopMetrics{},
|
2022-07-11 12:34:17 +00:00
|
|
|
}
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](f)
|
|
|
|
}
|
|
|
|
|
|
|
|
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:], "")
|
2022-02-02 13:28:08 +00:00
|
|
|
des, err := os.ReadDir(filepath.Join(curPath...))
|
2021-04-06 10:56:06 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.IgnoreErrors {
|
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-03-21 18:31:48 +00:00
|
|
|
data, err := os.ReadFile(filepath.Join(curPath...))
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.LazyHandler != nil {
|
2023-02-10 04:59:15 +00:00
|
|
|
err = prm.LazyHandler(addr, func() ([]byte, error) {
|
2023-03-21 18:31:48 +00:00
|
|
|
return data, err
|
2022-05-31 14:11:48 +00:00
|
|
|
})
|
|
|
|
} else {
|
2022-07-08 07:09:48 +00:00
|
|
|
if err == nil {
|
|
|
|
data, err = t.Decompress(data)
|
|
|
|
}
|
2022-05-31 14:11:48 +00:00
|
|
|
if err != nil {
|
2022-07-07 12:03:45 +00:00
|
|
|
if prm.IgnoreErrors {
|
|
|
|
if prm.ErrorHandler != nil {
|
2023-02-10 04:59:15 +00:00
|
|
|
return prm.ErrorHandler(addr, err)
|
2022-06-21 07:52:03 +00:00
|
|
|
}
|
2022-05-31 14:11:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
2022-01-20 15:53:29 +00:00
|
|
|
}
|
2022-07-07 12:03:45 +00:00
|
|
|
|
|
|
|
err = prm.Handler(common.IterationElement{
|
2023-02-10 04:59:15 +00:00
|
|
|
Address: addr,
|
2022-07-07 12:03:45 +00:00
|
|
|
ObjectData: data,
|
|
|
|
StorageID: []byte{},
|
|
|
|
})
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2021-04-08 13:53:25 +00:00
|
|
|
|
2023-06-02 13:04:02 +00:00
|
|
|
err = os.Remove(p)
|
2022-08-23 12:44:51 +00:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
2023-08-04 11:14:07 +00:00
|
|
|
err = logicerr.Wrap(new(apistatus.ObjectNotFound))
|
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-02-09 16:13:51 +00:00
|
|
|
// Here is a situation:
|
|
|
|
// Feb 09 13:10:37 buky neofs-node[32445]: 2023-02-09T13:10:37.161Z info log/log.go:13 local object storage operation {"shard_id": "SkT8BfjouW6t93oLuzQ79s", "address": "7NxFz4SruSi8TqXacr2Ae22nekMhgYk1sfkddJo9PpWk/5enyUJGCyU1sfrURDnHEjZFdbGqANVhayYGfdSqtA6wA", "op": "PUT", "type": "fstree", "storage_id": ""}
|
|
|
|
// Feb 09 13:10:37 buky neofs-node[32445]: 2023-02-09T13:10:37.183Z info log/log.go:13 local object storage operation {"shard_id": "SkT8BfjouW6t93oLuzQ79s", "address": "7NxFz4SruSi8TqXacr2Ae22nekMhgYk1sfkddJo9PpWk/5enyUJGCyU1sfrURDnHEjZFdbGqANVhayYGfdSqtA6wA", "op": "metabase PUT"}
|
|
|
|
// Feb 09 13:10:37 buky neofs-node[32445]: 2023-02-09T13:10:37.862Z debug policer/check.go:231 shortage of object copies detected {"component": "Object Policer", "object": "7NxFz4SruSi8TqXacr2Ae22nekMhgYk1sfkddJo9PpWk/5enyUJGCyU1sfrURDnHEjZFdbGqANVhayYGfdSqtA6wA", "shortage": 1}
|
|
|
|
// Feb 09 13:10:37 buky neofs-node[32445]: 2023-02-09T13:10:37.862Z debug shard/get.go:124 object is missing in write-cache {"shard_id": "SkT8BfjouW6t93oLuzQ79s", "addr": "7NxFz4SruSi8TqXacr2Ae22nekMhgYk1sfkddJo9PpWk/5enyUJGCyU1sfrURDnHEjZFdbGqANVhayYGfdSqtA6wA", "skip_meta": false}
|
|
|
|
//
|
|
|
|
// 1. We put an object on node 1.
|
|
|
|
// 2. Relentless policer sees that it has only 1 copy and tries to PUT it to node 2.
|
|
|
|
// 3. PUT operation started by client at (1) also puts an object here.
|
|
|
|
// 4. Now we have concurrent writes and one of `Rename` calls will return `no such file` error.
|
|
|
|
// Even more than that, concurrent writes can corrupt data.
|
|
|
|
//
|
|
|
|
// So here is a solution:
|
|
|
|
// 1. Write a file to 'name + 1'.
|
|
|
|
// 2. If it exists, retry with temporary name being 'name + 2'.
|
|
|
|
// 3. Set some reasonable number of attempts.
|
|
|
|
//
|
|
|
|
// It is a bit kludgey, but I am unusually proud about having found this out after
|
|
|
|
// hours of research on linux kernel, dirsync mount option and ext4 FS, turned out
|
|
|
|
// to be so hecking simple.
|
|
|
|
// In a very rare situation we can have multiple partially written copies on disk,
|
|
|
|
// this will be fixed in another issue (we should remove garbage on start).
|
2023-06-02 13:04:02 +00:00
|
|
|
size = len(prm.RawData)
|
2023-02-09 16:13:51 +00:00
|
|
|
const retryCount = 5
|
|
|
|
for i := 0; i < retryCount; i++ {
|
|
|
|
tmpPath := p + "#" + strconv.FormatUint(uint64(i), 10)
|
2023-06-02 13:04:02 +00:00
|
|
|
err = t.writeAndRename(tmpPath, p, prm.RawData)
|
2023-02-09 16:13:51 +00:00
|
|
|
if err != syscall.EEXIST || i == retryCount-1 {
|
|
|
|
return common.PutRes{StorageID: []byte{}}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 13:04:02 +00:00
|
|
|
err = fmt.Errorf("couldn't read file after %d retries", retryCount)
|
2023-02-09 16:13:51 +00:00
|
|
|
// unreachable, but precaution never hurts, especially 1 day before release.
|
2023-06-02 13:04:02 +00:00
|
|
|
return common.PutRes{StorageID: []byte{}}, err
|
2023-02-09 16:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeAndRename opens tmpPath exclusively, writes data to it and renames it to p.
|
|
|
|
func (t *FSTree) writeAndRename(tmpPath, p string, data []byte) error {
|
|
|
|
err := t.writeFile(tmpPath, data)
|
2022-09-28 08:10:27 +00:00
|
|
|
if err != nil {
|
|
|
|
var pe *fs.PathError
|
2023-02-09 16:13:51 +00:00
|
|
|
if errors.As(err, &pe) {
|
|
|
|
switch pe.Err {
|
|
|
|
case syscall.ENOSPC:
|
|
|
|
err = common.ErrNoSpace
|
|
|
|
_ = os.RemoveAll(tmpPath)
|
|
|
|
case syscall.EEXIST:
|
|
|
|
return syscall.EEXIST
|
|
|
|
}
|
2022-09-28 08:10:27 +00:00
|
|
|
}
|
2023-01-31 15:27:58 +00:00
|
|
|
} else {
|
|
|
|
err = os.Rename(tmpPath, p)
|
2022-09-28 08:10:27 +00:00
|
|
|
}
|
2023-02-09 16:13:51 +00:00
|
|
|
return err
|
2021-04-08 13:53:25 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 09:57:36 +00:00
|
|
|
func (t *FSTree) writeFlags() int {
|
2023-02-09 16:13:51 +00:00
|
|
|
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_EXCL
|
2022-10-28 09:57:36 +00:00
|
|
|
if t.noSync {
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
return flags | os.O_SYNC
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeFile writes data to a file with path p.
|
|
|
|
// The code is copied from `os.WriteFile` with minor corrections for flags.
|
|
|
|
func (t *FSTree) writeFile(p string, data []byte) error {
|
|
|
|
f, err := os.OpenFile(p, t.writeFlags(), t.Permissions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = f.Write(data)
|
|
|
|
if err1 := f.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:19:03 +00:00
|
|
|
// PutStream puts executes handler on a file opened for write.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (t *FSTree) PutStream(addr oid.Address, handler func(*os.File) error) error {
|
2022-08-23 13:04:01 +00:00
|
|
|
if t.readOnly {
|
|
|
|
return common.ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:19:03 +00:00
|
|
|
p := t.treePath(addr)
|
|
|
|
|
|
|
|
if err := util.MkdirAllX(filepath.Dir(p), t.Permissions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-28 09:57:36 +00:00
|
|
|
f, err := os.OpenFile(p, t.writeFlags(), t.Permissions)
|
2022-04-28 10:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return handler(f)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if _, err := os.Stat(p); os.IsNotExist(err) {
|
2023-08-04 11:14:07 +00:00
|
|
|
return common.GetRes{}, logicerr.Wrap(new(apistatus.ObjectNotFound))
|
2022-07-08 07:09:48 +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 {
|
|
|
|
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
|
|
|
|
|
|
|
// NumberOfObjects walks the file tree rooted at FSTree's root
|
|
|
|
// and returns number of stored objects.
|
|
|
|
func (t *FSTree) NumberOfObjects() (uint64, error) {
|
|
|
|
var counter uint64
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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)
|
|
|
|
}
|