From fb74524ac73eaa1f46b63c06389035ae9b4b9bed Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 8 Feb 2024 17:53:27 +0300 Subject: [PATCH] [#970] fstree: Move delete implementation to a separate file Signed-off-by: Evgenii Stratonikov --- .../blobstor/fstree/control.go | 2 +- .../blobstor/fstree/fstree.go | 22 +++----------- .../blobstor/fstree/fstree_write_generic.go | 30 +++++++++++++++++-- .../blobstor/fstree/fstree_write_linux.go | 17 +++++++++-- .../blobstor/fstree/fstree_write_specific.go | 6 ++-- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/pkg/local_object_storage/blobstor/fstree/control.go b/pkg/local_object_storage/blobstor/fstree/control.go index b126f8d29..cec531f2e 100644 --- a/pkg/local_object_storage/blobstor/fstree/control.go +++ b/pkg/local_object_storage/blobstor/fstree/control.go @@ -19,7 +19,7 @@ func (t *FSTree) Init() error { if !t.readOnly { f := newSpecificWriteData(t.fileCounter, t.RootPath, t.Permissions, t.noSync) if f != nil { - t.writeData = f + t.writer = f } } diff --git a/pkg/local_object_storage/blobstor/fstree/fstree.go b/pkg/local_object_storage/blobstor/fstree/fstree.go index fc29d4eaa..ba691b056 100644 --- a/pkg/local_object_storage/blobstor/fstree/fstree.go +++ b/pkg/local_object_storage/blobstor/fstree/fstree.go @@ -57,7 +57,7 @@ type FSTree struct { fileCounter FileCounter fileCounterEnabled bool - writeData func(string, []byte) error + writer writer } // Info groups the information about file storage. @@ -95,7 +95,7 @@ func New(opts ...Option) *FSTree { for i := range opts { opts[i](f) } - f.writeData = newGenericWriteData(f) + f.writer = newGenericWriteData(f) return f } @@ -266,21 +266,7 @@ func (t *FSTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Delet } p := t.treePath(prm.Address) - - if t.fileCounterEnabled { - t.fileGuard.Lock(p) - err = os.Remove(p) - t.fileGuard.Unlock(p) - if err == nil { - t.fileCounter.Dec() - } - } else { - err = os.Remove(p) - } - - if err != nil && os.IsNotExist(err) { - err = logicerr.Wrap(new(apistatus.ObjectNotFound)) - } + err = t.writer.removeFile(p) return common.DeleteRes{}, err } @@ -357,7 +343,7 @@ func (t *FSTree) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, err } size = len(prm.RawData) - return common.PutRes{StorageID: []byte{}}, t.writeData(p, prm.RawData) + return common.PutRes{StorageID: []byte{}}, t.writer.writeData(p, prm.RawData) } // Get returns an object from the storage by address. diff --git a/pkg/local_object_storage/blobstor/fstree/fstree_write_generic.go b/pkg/local_object_storage/blobstor/fstree/fstree_write_generic.go index 3d65808ec..e1e3a53bb 100644 --- a/pkg/local_object_storage/blobstor/fstree/fstree_write_generic.go +++ b/pkg/local_object_storage/blobstor/fstree/fstree_write_generic.go @@ -9,8 +9,15 @@ import ( "syscall" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr" + apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status" ) +type writer interface { + writeData(string, []byte) error + removeFile(string) error +} + type genericWriter struct { perm fs.FileMode flags int @@ -19,7 +26,7 @@ type genericWriter struct { suffix atomic.Uint64 } -func newGenericWriteData(t *FSTree) func(string, []byte) error { +func newGenericWriteData(t *FSTree) writer { flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_EXCL if !t.noSync { flags |= os.O_SYNC @@ -29,7 +36,7 @@ func newGenericWriteData(t *FSTree) func(string, []byte) error { flags: flags, t: t, } - return w.writeData + return w } func (w *genericWriter) writeData(p string, data []byte) error { @@ -86,3 +93,22 @@ func (w *genericWriter) writeFile(p string, data []byte) error { } return err } + +func (w *genericWriter) removeFile(p string) error { + var err error + if w.t.fileCounterEnabled { + w.t.fileGuard.Lock(p) + err = os.Remove(p) + w.t.fileGuard.Unlock(p) + if err == nil { + w.t.fileCounter.Dec() + } + } else { + err = os.Remove(p) + } + + if err != nil && os.IsNotExist(err) { + err = logicerr.Wrap(new(apistatus.ObjectNotFound)) + } + return err +} diff --git a/pkg/local_object_storage/blobstor/fstree/fstree_write_linux.go b/pkg/local_object_storage/blobstor/fstree/fstree_write_linux.go index 3416cf7ac..a77c9d1b2 100644 --- a/pkg/local_object_storage/blobstor/fstree/fstree_write_linux.go +++ b/pkg/local_object_storage/blobstor/fstree/fstree_write_linux.go @@ -8,6 +8,8 @@ import ( "strconv" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr" + apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status" "golang.org/x/sys/unix" ) @@ -19,7 +21,7 @@ type linuxWriter struct { counter FileCounter } -func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync bool) func(string, []byte) error { +func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync bool) writer { flags := unix.O_WRONLY | unix.O_TMPFILE | unix.O_CLOEXEC if !noSync { flags |= unix.O_DSYNC @@ -37,7 +39,7 @@ func newSpecificWriteData(c FileCounter, root string, perm fs.FileMode, noSync b flags: flags, counter: c, } - return w.writeData + return w } func (w *linuxWriter) writeData(p string, data []byte) error { @@ -74,3 +76,14 @@ func (w *linuxWriter) writeFile(p string, data []byte) error { } return errClose } + +func (w *linuxWriter) removeFile(p string) error { + err := unix.Unlink(p) + if err != nil && err == unix.ENOENT { + return logicerr.Wrap(new(apistatus.ObjectNotFound)) + } + if err == nil { + w.counter.Dec() + } + return err +} diff --git a/pkg/local_object_storage/blobstor/fstree/fstree_write_specific.go b/pkg/local_object_storage/blobstor/fstree/fstree_write_specific.go index 16b630b6a..9bd559366 100644 --- a/pkg/local_object_storage/blobstor/fstree/fstree_write_specific.go +++ b/pkg/local_object_storage/blobstor/fstree/fstree_write_specific.go @@ -2,8 +2,10 @@ package fstree -import "io/fs" +import ( + "io/fs" +) -func newSpecificWriteData(_ FileCounter, _ string, _ fs.FileMode, _ bool) func(string, []byte) error { +func newSpecificWriteData(_ FileCounter, _ string, _ fs.FileMode, _ bool) writer { return nil }