Skip to content

Commit 8799580

Browse files
committed
if a volume containing incomplete metadata is found during a ListVolumes call, clean up and continue
Signed-off-by: James Munnelly <[email protected]>
1 parent 9811918 commit 8799580

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

storage/filesystem.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,16 @@ func (f *Filesystem) ListVolumes() ([]string, error) {
111111
var vols []string
112112
for _, dir := range dirs {
113113
file, err := f.fs.Open(f.metadataPathForVolumeID(dir.Name()))
114-
if err != nil {
114+
switch {
115+
case errors.Is(err, fs.ErrNotExist):
116+
f.log.Info("Directory exists but does not contain a metadata file - deleting directory and its contents", "volume_id", dir.Name())
117+
if err := f.RemoveVolume(dir.Name()); err != nil {
118+
return nil, fmt.Errorf("deleting stale volume: %v", err)
119+
}
120+
// continue to skip this loop iteration
121+
continue
122+
case err != nil:
115123
// discovered a volume/directory that does not contain a metadata file
116-
// TODO: log this error to allow startup to continue
117124
return nil, err
118125
}
119126
// immediately close the file as we just need to verify it exists

storage/filesystem_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ func TestFilesystem_ListVolumes(t *testing.T) {
118118
}
119119
}
120120

121+
func TestFilesystem_ListVolumes_CleansUpCorruptVolumes(t *testing.T) {
122+
backend := &Filesystem{
123+
fs: fstest.MapFS{
124+
"inmemfs/fake-volume/metadata.json": &fstest.MapFile{Data: []byte{}},
125+
"inmemfs/fake-emptyvolume/nothing": &fstest.MapFile{Data: []byte{}},
126+
},
127+
}
128+
129+
vols, err := backend.ListVolumes()
130+
if err != nil {
131+
t.Errorf("unexpected error: %v", err)
132+
}
133+
if len(vols) != 1 {
134+
t.Errorf("expected 1 volume to be returned but got: %+v", vols)
135+
}
136+
if vols[0] != "fake-volume" {
137+
t.Errorf("expected only entry to be 'fake-volume' but got: %s", vols[0])
138+
}
139+
}
121140
func Test_fsGroupForMetadata(t *testing.T) {
122141
intPtr := func(i int64) *int64 {
123142
return &i

0 commit comments

Comments
 (0)