Skip to content

Commit

Permalink
Handle tmp file deletion
Browse files Browse the repository at this point in the history
Signed-off-by: Shreyansh Ray <[email protected]>
  • Loading branch information
rayshrey committed May 10, 2024
1 parent 3292b36 commit 5013be9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ public String[] listAll() throws IOException {
@Override
public void deleteFile(String name) throws IOException {
logger.trace("deleteFile() called {}", name);
/*
Not deleting from localDirectory directly since it causes a race condition when the localDirectory deletes a file, and it ends up in pendingDeletion state.
Meanwhile, fileCache on removal deletes the file directly via the Files class and later when the directory tries to delete the files pending for deletion (which happens before creating a new file), it causes NoSuchFileException and new file creation fails
*/
fileCache.remove(localDirectory.getDirectory().resolve(name));
if (isTempFile(name)) {
localDirectory.deleteFile(name);
} else {
/*
Not deleting from localDirectory directly since it causes a race condition when the localDirectory deletes a file, and it ends up in pendingDeletion state.
Meanwhile, fileCache on removal deletes the file directly via the Files class and later when the directory tries to delete the files pending for deletion (which happens before creating a new file), it causes NoSuchFileException and new file creation fails
*/
fileCache.remove(localDirectory.getDirectory().resolve(name));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -60,9 +61,12 @@ public void testListAll() throws IOException {
public void testDeleteFile() throws IOException {
Path basePath = mock(Path.class);
Path resolvedPath = mock(Path.class);
when(basePath.resolve("_0.si")).thenReturn(resolvedPath);
when(basePath.resolve(anyString())).thenReturn(resolvedPath);
when(localDirectory.getDirectory()).thenReturn(basePath);

compositeDirectory.deleteFile("_0.tmp");
verify(localDirectory).deleteFile("_0.tmp");

compositeDirectory.deleteFile("_0.si");
verify(fileCache).remove(resolvedPath);
}
Expand Down

0 comments on commit 5013be9

Please sign in to comment.