Skip to content

Commit 260b16a

Browse files
committed
storage/fs: use vfs.FS in CleanupTempDirs
Accept a vfs.FS as a parameter to CleanupTempDirs, easing testing. Epic: none Release note: none
1 parent 619c37c commit 260b16a

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

pkg/cli/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func initTempStorageConfig(
212212
specIdxDisk = i
213213
}
214214
recordPath := filepath.Join(spec.Path, server.TempDirsRecordFilename)
215-
if err := fs.CleanupTempDirs(recordPath); err != nil {
215+
if err := fs.CleanupTempDirs(ctx, vfs.Default, recordPath); err != nil {
216216
return base.TempStorageConfig{}, errors.Wrap(err,
217217
"could not cleanup temporary directories from record file")
218218
}
@@ -311,7 +311,7 @@ func initTempStorageConfig(
311311
// Remove temporary directory on shutdown.
312312
stopper.AddCloser(stop.CloserFn(func() {
313313
unlockDirFn()
314-
if err := fs.CleanupTempDirs(recordPath); err != nil {
314+
if err := fs.CleanupTempDirs(ctx, vfs.Default, recordPath); err != nil {
315315
log.Dev.Errorf(ctx, "could not remove temporary store directory: %v", err.Error())
316316
}
317317
}))

pkg/storage/fs/temp_dir.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type lockStruct struct {
2525
}
2626

2727
// lockFile sets a lock on the specified file, using flock.
28-
func lockFile(filename string) (lockStruct, error) {
29-
closer, err := vfs.Default.Lock(filename)
28+
func lockFile(fs vfs.FS, filename string) (lockStruct, error) {
29+
closer, err := fs.Lock(filename)
3030
if err != nil {
3131
return lockStruct{}, err
3232
}
@@ -64,7 +64,7 @@ func CreateTempDir(parentDir, prefix string) (_ string, unlockDirFn func(), _ er
6464
}
6565

6666
// Create a lock file.
67-
flock, err := lockFile(filepath.Join(absPath, lockFilename))
67+
flock, err := lockFile(vfs.Default, filepath.Join(absPath, lockFilename))
6868
if err != nil {
6969
return "", nil, errors.Wrapf(err, "could not create lock on new temporary directory")
7070
}
@@ -98,19 +98,23 @@ func RecordTempDir(recordPath, tempPath string) error {
9898
// up abandoned temporary directories.
9999
// It should also be invoked when a newly created temporary directory is no
100100
// longer needed and needs to be removed from the record file.
101-
func CleanupTempDirs(recordPath string) error {
101+
func CleanupTempDirs(ctx context.Context, fs vfs.FS, recordPath string) error {
102102
// Reading the entire file into memory shouldn't be a problem since
103103
// it is extremely rare for this record file to contain more than a few
104104
// entries.
105-
f, err := os.OpenFile(recordPath, os.O_RDWR, 0644)
105+
f, err := fs.Open(recordPath)
106106
// There is no existing record file and thus nothing to clean up.
107107
if oserror.IsNotExist(err) {
108108
return nil
109109
}
110110
if err != nil {
111111
return err
112112
}
113-
defer f.Close()
113+
defer func() {
114+
if f != nil {
115+
f.Close()
116+
}
117+
}()
114118

115119
scanner := bufio.NewScanner(f)
116120
// Iterate through each temporary directory path and remove the
@@ -122,14 +126,14 @@ func CleanupTempDirs(recordPath string) error {
122126
}
123127

124128
// Check if the temporary directory exists; if it does not, skip over it.
125-
if _, err := os.Stat(path); oserror.IsNotExist(err) {
126-
log.Dev.Warningf(context.Background(), "could not locate previous temporary directory %s, might require manual cleanup, or might have already been cleaned up.", path)
129+
if _, err := fs.Stat(path); oserror.IsNotExist(err) {
130+
log.Dev.Warningf(ctx, "could not locate previous temporary directory %s, might require manual cleanup, or might have already been cleaned up.", path)
127131
continue
128132
}
129133

130134
// Check if another Cockroach instance is using this temporary
131135
// directory i.e. has a lock on the temp dir lock file.
132-
flock, err := lockFile(filepath.Join(path, lockFilename))
136+
flock, err := lockFile(fs, fs.PathJoin(path, lockFilename))
133137
if err != nil {
134138
return errors.Wrapf(err, "could not lock temporary directory %s, may still be in use", path)
135139
}
@@ -143,15 +147,21 @@ func CleanupTempDirs(recordPath string) error {
143147
// process is dead because we were able to acquire the lock in the first
144148
// place.
145149
if err := unlockFile(flock); err != nil {
146-
log.Dev.Errorf(context.TODO(), "could not unlock file lock when removing temporary directory: %s", err.Error())
150+
log.Dev.Errorf(ctx, "could not unlock file lock when removing temporary directory: %s", err.Error())
147151
}
148152

149153
// If path/directory does not exist, error is nil.
150-
if err := os.RemoveAll(path); err != nil {
154+
if err := fs.RemoveAll(path); err != nil {
151155
return err
152156
}
153157
}
154158

155-
// Clear out the record file now that we're done.
156-
return f.Truncate(0)
159+
// Remove the record file now that we're done.
160+
err = errors.CombineErrors(fs.Remove(recordPath), f.Close())
161+
f = nil
162+
rmErr := fs.Remove(recordPath)
163+
if rmErr != nil && !oserror.IsNotExist(rmErr) {
164+
err = errors.CombineErrors(err, rmErr)
165+
}
166+
return err
157167
}

pkg/storage/fs/temp_dir_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
1616
"github.com/cockroachdb/cockroach/pkg/util/log"
1717
"github.com/cockroachdb/errors/oserror"
18+
"github.com/cockroachdb/pebble/vfs"
1819
)
1920

2021
func TestCreateTempDir(t *testing.T) {
@@ -124,7 +125,7 @@ func TestCleanupTempDirs(t *testing.T) {
124125
}
125126
}
126127

127-
if err = CleanupTempDirs(recordFile.Name()); err != nil {
128+
if err = CleanupTempDirs(t.Context(), vfs.Default, recordFile.Name()); err != nil {
128129
t.Fatal(err)
129130
}
130131

0 commit comments

Comments
 (0)