Skip to content
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

feat: put all temporary files in the same directory and clean them up #69

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ type Datastore struct {
// (see https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
diskUsage int64

path string
path string
tempPath string

shardStr string
getDir ShardFunc
Expand Down Expand Up @@ -196,7 +197,6 @@ func (o *opResult) Finish(ok bool) {
}

func Create(path string, fun *ShardIdV1) error {

err := os.Mkdir(path, 0755)
if err != nil && !os.IsExist(err) {
return err
Expand Down Expand Up @@ -238,13 +238,25 @@ func Open(path string, syncFiles bool) (*Datastore, error) {
return nil, err
}

tempPath := filepath.Join(path, ".temp")
err = os.RemoveAll(tempPath)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to remove temporary directory: %w", err)
}

err = os.Mkdir(tempPath, 0755)
if err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
}

shardId, err := ReadShardFunc(path)
if err != nil {
return nil, err
}

fs := &Datastore{
path: path,
tempPath: tempPath,
shardStr: shardId.String(),
getDir: shardId.Func(),
sync: syncFiles,
Expand Down Expand Up @@ -452,7 +464,7 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
return err
}

tmp, err := ioutil.TempFile(dir, "put-")
tmp, err := fs.tempFile()
if err != nil {
return err
}
Expand Down Expand Up @@ -528,7 +540,7 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
}
dirsToSync = append(dirsToSync, dir)

tmp, err := ioutil.TempFile(dir, "put-")
tmp, err := fs.tempFile()
if err != nil {
return err
}
Expand Down Expand Up @@ -954,7 +966,7 @@ func (fs *Datastore) checkpointLoop() {
}

func (fs *Datastore) writeDiskUsageFile(du int64, doSync bool) {
tmp, err := ioutil.TempFile(fs.path, "du-")
tmp, err := fs.tempFile()
if err != nil {
log.Warnw("could not write disk usage", "error", err)
return
Expand Down Expand Up @@ -1037,6 +1049,11 @@ func (fs *Datastore) Accuracy() string {
return string(fs.storedValue.Accuracy)
}

func (fs *Datastore) tempFile() (*os.File, error) {
file, err := ioutil.TempFile(fs.tempPath, "temp-")
return file, err
}

func (fs *Datastore) walk(path string, qrb *query.ResultBuilder) error {
dir, err := os.Open(path)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions flatfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func testStorage(p *params, t *testing.T) {
return err
}
switch path {
case ".", "..", "SHARDING", flatfs.DiskUsageFile:
case ".", "..", "SHARDING", flatfs.DiskUsageFile, ".temp":
// ignore
case "_README":
_, err := ioutil.ReadFile(absPath)
Expand Down Expand Up @@ -950,9 +950,8 @@ func TestNoCluster(t *testing.T) {
tolerance := math.Floor(idealFilesPerDir * 0.25)
count := 0
for _, dir := range dirs {
if dir.Name() == flatfs.SHARDING_FN ||
dir.Name() == flatfs.README_FN ||
dir.Name() == flatfs.DiskUsageFile {
switch dir.Name() {
case flatfs.SHARDING_FN, flatfs.README_FN, flatfs.DiskUsageFile, ".temp":
continue
}
count += 1
Expand Down