[#1367] fstree: Add size hint for Delete
All checks were successful
DCO action / DCO (pull_request) Successful in 51s
Vulncheck / Vulncheck (pull_request) Successful in 1m29s
Tests and linters / Run gofumpt (pull_request) Successful in 1m43s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m4s
Build / Build Components (pull_request) Successful in 2m16s
Tests and linters / gopls check (pull_request) Successful in 2m32s
Tests and linters / Staticcheck (pull_request) Successful in 2m37s
Tests and linters / Lint (pull_request) Successful in 3m27s
Tests and linters / Tests (pull_request) Successful in 4m2s
Tests and linters / Tests with -race (pull_request) Successful in 6m26s
All checks were successful
DCO action / DCO (pull_request) Successful in 51s
Vulncheck / Vulncheck (pull_request) Successful in 1m29s
Tests and linters / Run gofumpt (pull_request) Successful in 1m43s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m4s
Build / Build Components (pull_request) Successful in 2m16s
Tests and linters / gopls check (pull_request) Successful in 2m32s
Tests and linters / Staticcheck (pull_request) Successful in 2m37s
Tests and linters / Lint (pull_request) Successful in 3m27s
Tests and linters / Tests (pull_request) Successful in 4m2s
Tests and linters / Tests with -race (pull_request) Successful in 6m26s
This allow to not to call `os.Stat` if caller already knows data size. Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
6c172ce30c
commit
95dfb724cc
7 changed files with 92 additions and 47 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
type DeletePrm struct {
|
||||
Address oid.Address
|
||||
StorageID []byte
|
||||
Size uint64
|
||||
}
|
||||
|
||||
// DeleteRes groups the resulting values of Delete operation.
|
||||
|
|
|
@ -339,7 +339,7 @@ func (t *FSTree) Delete(ctx context.Context, prm common.DeletePrm) (common.Delet
|
|||
}
|
||||
|
||||
p := t.treePath(prm.Address)
|
||||
err = t.writer.removeFile(p)
|
||||
err = t.writer.removeFile(p, prm.Size)
|
||||
return common.DeleteRes{}, err
|
||||
}
|
||||
|
||||
|
|
|
@ -68,6 +68,7 @@ func TestObjectCounter(t *testing.T) {
|
|||
var delPrm common.DeletePrm
|
||||
delPrm.Address = addr
|
||||
|
||||
t.Run("without size hint", func(t *testing.T) {
|
||||
eg, egCtx := errgroup.WithContext(context.Background())
|
||||
|
||||
eg.Go(func() error {
|
||||
|
@ -98,4 +99,39 @@ func TestObjectCounter(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
require.Equal(t, realCount, count, "real %d, actual %d", realCount, count)
|
||||
require.Equal(t, realSize, size, "real %d, actual %d", realSize, size)
|
||||
})
|
||||
|
||||
t.Run("with size hint", func(t *testing.T) {
|
||||
delPrm.Size = uint64(len(putPrm.RawData))
|
||||
eg, egCtx := errgroup.WithContext(context.Background())
|
||||
|
||||
eg.Go(func() error {
|
||||
for range 1_000 {
|
||||
_, err := fst.Put(egCtx, putPrm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
eg.Go(func() error {
|
||||
var le logicerr.Logical
|
||||
for range 1_000 {
|
||||
_, err := fst.Delete(egCtx, delPrm)
|
||||
if err != nil && !errors.As(err, &le) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
require.NoError(t, eg.Wait())
|
||||
|
||||
count, size = counter.CountSize()
|
||||
realCount, realSize, err := fst.countFiles()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, realCount, count, "real %d, actual %d", realCount, count)
|
||||
require.Equal(t, realSize, size, "real %d, actual %d", realSize, size)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
|
||||
type writer interface {
|
||||
writeData(string, []byte) error
|
||||
removeFile(string) error
|
||||
removeFile(string, uint64) error
|
||||
}
|
||||
|
||||
type genericWriter struct {
|
||||
|
@ -107,10 +107,10 @@ func (w *genericWriter) writeFile(p string, data []byte) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (w *genericWriter) removeFile(p string) error {
|
||||
func (w *genericWriter) removeFile(p string, size uint64) error {
|
||||
var err error
|
||||
if w.fileCounterEnabled {
|
||||
err = w.removeWithCounter(p)
|
||||
err = w.removeWithCounter(p, size)
|
||||
} else {
|
||||
err = os.Remove(p)
|
||||
}
|
||||
|
@ -120,18 +120,21 @@ func (w *genericWriter) removeFile(p string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (w *genericWriter) removeWithCounter(p string) error {
|
||||
func (w *genericWriter) removeWithCounter(p string, size uint64) error {
|
||||
w.fileGuard.Lock(p)
|
||||
defer w.fileGuard.Unlock(p)
|
||||
|
||||
if size == 0 {
|
||||
stat, err := os.Stat(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
size = uint64(stat.Size())
|
||||
}
|
||||
|
||||
if err := os.Remove(p); err != nil {
|
||||
return err
|
||||
}
|
||||
w.fileCounter.Dec(uint64(stat.Size()))
|
||||
w.fileCounter.Dec(uint64(size))
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -91,11 +91,12 @@ func (w *linuxWriter) writeFile(p string, data []byte) error {
|
|||
return errClose
|
||||
}
|
||||
|
||||
func (w *linuxWriter) removeFile(p string) error {
|
||||
func (w *linuxWriter) removeFile(p string, size uint64) error {
|
||||
if w.fileCounterEnabled {
|
||||
w.fileGuard.Lock(p)
|
||||
defer w.fileGuard.Unlock(p)
|
||||
}
|
||||
|
||||
if size == 0 {
|
||||
var stat unix.Stat_t
|
||||
err := unix.Stat(p, &stat)
|
||||
if err != nil {
|
||||
|
@ -104,12 +105,16 @@ func (w *linuxWriter) removeFile(p string) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
err = unix.Unlink(p)
|
||||
size = uint64(stat.Size)
|
||||
}
|
||||
}
|
||||
|
||||
err := unix.Unlink(p)
|
||||
if err != nil && err == unix.ENOENT {
|
||||
return logicerr.Wrap(new(apistatus.ObjectNotFound))
|
||||
}
|
||||
if err == nil {
|
||||
w.fileCounter.Dec(uint64(stat.Size))
|
||||
w.fileCounter.Dec(uint64(size))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ func (c *cache) flushIfAnObjectExistsWorker(ctx context.Context, objInfo objectI
|
|||
return
|
||||
}
|
||||
|
||||
c.deleteFromDisk(ctx, objInfo.addr)
|
||||
c.deleteFromDisk(ctx, objInfo.addr, uint64(len(res.RawData)))
|
||||
}
|
||||
|
||||
func (c *cache) reportFlushError(msg string, addr string, err error) {
|
||||
|
@ -157,7 +157,7 @@ func (c *cache) flushFSTree(ctx context.Context, ignoreErrors bool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
c.deleteFromDisk(ctx, e.Address)
|
||||
c.deleteFromDisk(ctx, e.Address, uint64(len(e.ObjectData)))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -40,8 +40,8 @@ func (c *cache) openStore(mod mode.ComponentMode) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *cache) deleteFromDisk(ctx context.Context, addr oid.Address) {
|
||||
_, err := c.fsTree.Delete(ctx, common.DeletePrm{Address: addr})
|
||||
func (c *cache) deleteFromDisk(ctx context.Context, addr oid.Address, size uint64) {
|
||||
_, err := c.fsTree.Delete(ctx, common.DeletePrm{Address: addr, Size: size})
|
||||
if err != nil && !client.IsErrObjectNotFound(err) {
|
||||
c.log.Error(logs.WritecacheCantRemoveObjectFromWritecache, zap.Error(err))
|
||||
} else if err == nil {
|
||||
|
|
Loading…
Reference in a new issue