-
Notifications
You must be signed in to change notification settings - Fork 381
TierFS enhancements #1008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TierFS enhancements #1008
Changes from all commits
862096c
32f4228
e0a2064
67725d5
8d3c199
be9f026
9074876
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package pyramid | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
// directory synchronizes between file operations that might change (create/delete) directories | ||
type directory struct { | ||
// ceilingDir is the root directory of the FS - shouldn't never be deleted | ||
ceilingDir string | ||
mu sync.Mutex | ||
} | ||
|
||
// deleteDirRecIfEmpty deletes the given directory if it is empty. | ||
// It will continue to delete all parents directory if they are empty, until the ceilingDir. | ||
// Passed dir path isn't checked for malicious referencing (e.g. "../../../usr") and should never be | ||
// controlled by any user input. | ||
func (d *directory) deleteDirRecIfEmpty(dir string) error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
for dir != d.ceilingDir { | ||
empty, err := isDirEmpty(dir) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return nil | ||
} | ||
return err | ||
} | ||
if !empty { | ||
return nil | ||
} | ||
|
||
parentDir := path.Dir(dir) | ||
if err := os.Remove(dir); err != nil { | ||
return err | ||
itaiad200 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
dir = parentDir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop can continue way up, above the planned location. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was planning for rooted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better, but still fragile! If I can control the inputs to E.g., if I would be happier if we at least documented that paths are not clean and must never be user-controlled. Or we could resolve to actual paths and work from there. E.g. @nopcoder suggested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding documentation for now, really can't see how the paths (other than the base directory) will be user-controlled in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I don't particularly mind "user-controlled" paths when that user is the owner of lakeFS. I'm worried about "user-controlled" paths when they come from a lakeFS user, who is supposed only to have permissions to act on files inside lakeFS. |
||
} | ||
|
||
return nil | ||
} | ||
|
||
func isDirEmpty(name string) (bool, error) { | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Readdirnames(1) | ||
if errors.Is(err, io.EOF) { | ||
return true, nil | ||
} | ||
return false, err | ||
} | ||
|
||
// createFile creates the file under the path and creates all parent dirs if missing. | ||
func (d *directory) createFile(path string) (*os.File, error) { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil { | ||
return nil, fmt.Errorf("creating dir: %w", err) | ||
} | ||
|
||
return os.Create(path) | ||
} | ||
|
||
// renameFile will move the src file to dst location and creates all parent dirs if missing. | ||
func (d *directory) renameFile(src, dst string) error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
if err := os.MkdirAll(filepath.Dir(dst), os.ModePerm); err != nil { | ||
return fmt.Errorf("creating dir: %w", err) | ||
} | ||
|
||
return os.Rename(src, dst) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package pyramid | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConcurrentCreateDeleteDir(t *testing.T) { | ||
name, err := ioutil.TempDir("", "test-dir-") | ||
require.NoError(t, err) | ||
sut := directory{ceilingDir: name} | ||
|
||
var wg sync.WaitGroup | ||
concurrency := 1000 | ||
pathDir := path.Join(name, "a/b/c/") | ||
|
||
for i := 0; i < concurrency; i++ { | ||
// create and delete | ||
filepath := path.Join(pathDir, strconv.Itoa(i)) | ||
wg.Add(2) | ||
go func() { | ||
f, err := sut.createFile(filepath) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
require.NoError(t, os.Remove(filepath)) | ||
|
||
wg.Done() | ||
}() | ||
// delete folder - this will sometime succeed if the folder is empty | ||
go func() { | ||
require.NoError(t, sut.deleteDirRecIfEmpty(pathDir)) | ||
|
||
wg.Done() | ||
}() | ||
} | ||
// It doesn't really matter if the dir exists and its content. | ||
// It's more about not panicking thru all of this | ||
wg.Wait() | ||
} | ||
|
||
func TestConcurrentRenameDeleteDir(t *testing.T) { | ||
name, err := ioutil.TempDir("", "test-dir-") | ||
require.NoError(t, err) | ||
sut := directory{ceilingDir: name} | ||
|
||
var wg sync.WaitGroup | ||
concurrency := 1000 | ||
pathDir := path.Join(name, "a/b/c/") | ||
|
||
for i := 0; i < concurrency; i++ { | ||
// create and delete | ||
originalPath := path.Join(name, strconv.Itoa(i)) | ||
require.NoError(t, ioutil.WriteFile(originalPath, []byte("some data"), os.ModePerm)) | ||
|
||
filepath := path.Join(pathDir, strconv.Itoa(i)) | ||
wg.Add(2) | ||
go func() { | ||
err := sut.renameFile(originalPath, filepath) | ||
require.NoError(t, err) | ||
require.NoError(t, os.Remove(filepath)) | ||
|
||
wg.Done() | ||
}() | ||
// delete folder - this will sometime succeed if the folder is empty | ||
go func() { | ||
require.NoError(t, sut.deleteDirRecIfEmpty(pathDir)) | ||
|
||
wg.Done() | ||
}() | ||
} | ||
// It doesn't really matter if the dir exists and its content. | ||
// It's more about not panicking thru all of this | ||
wg.Wait() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pyramid | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
// nolint: gomnd | ||
const ( | ||
kb = float64(1024) | ||
fsNameLabel = "fsName" | ||
errorTypeLabel = "type" | ||
accessStatusLabel = "status" | ||
) | ||
|
||
var cacheAccess = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "tier_fs_cache_hits_total", | ||
Help: "TierFS cache hits total count", | ||
}, []string{fsNameLabel, accessStatusLabel}) | ||
|
||
var errorsTotal = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "tier_fs_errors_total", | ||
Help: "TierFS errors by type", | ||
}, []string{fsNameLabel, errorTypeLabel}) | ||
|
||
var evictionHistograms = promauto.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "tier_fs_eviction_bytes", | ||
Help: "TierFS evicted object size by bytes", | ||
Buckets: prometheus.ExponentialBuckets(kb, 4, 7), | ||
}, | ||
[]string{fsNameLabel}) | ||
|
||
var downloadHistograms = promauto.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Name: "tier_fs_download_bytes", | ||
Help: "TierFS download from block-store object size by bytes", | ||
Buckets: prometheus.ExponentialBuckets(kb, 4, 7), | ||
}, | ||
[]string{fsNameLabel}) |
Uh oh!
There was an error while loading. Please reload this page.