Skip to content

Commit b71fd2b

Browse files
rayshreytandonks
authored andcommitted
Fail deleteFile silently in case file not present in composite directory (opensearch-project#18299)
Signed-off-by: Shreyansh Ray <rayshrey@amazon.com>
1 parent 7040cb7 commit b71fd2b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

server/src/main/java/org/opensearch/index/store/CompositeDirectory.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ public void deleteFile(String name) throws IOException {
143143
if (FileTypeUtils.isTempFile(name)) {
144144
localDirectory.deleteFile(name);
145145
} else if (Arrays.asList(listAll()).contains(name) == false) {
146-
throw new NoSuchFileException("File " + name + " not found in directory");
146+
/*
147+
We can run into scenarios where stale files are evicted locally (zero refCount in FileCache) and
148+
not present in remote as well (due to metadata refresh). In such scenarios listAll() of composite directory
149+
will not show the file. Hence, we need to silently fail deleteFile operation for such files.
150+
*/
151+
return;
147152
} else {
148153
List<String> blockFiles = listBlockFiles(name);
149154
if (blockFiles.isEmpty()) {

server/src/test/java/org/opensearch/index/store/CompositeDirectoryTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ public void testDeleteFile() throws IOException {
8080
compositeDirectory.deleteFile(FILE_PRESENT_LOCALLY);
8181
assertFalse(existsInCompositeDirectory(FILE_PRESENT_LOCALLY));
8282
assertFalse(existsInCompositeDirectory(BLOCK_FILE_PRESENT_LOCALLY));
83-
// Reading deleted file from directory should result in NoSuchFileException
84-
assertThrows(NoSuchFileException.class, () -> compositeDirectory.openInput(FILE_PRESENT_LOCALLY, IOContext.DEFAULT));
83+
// Deletion of non-existent file should fail silently without throwing any error
84+
Exception exception = null;
85+
try {
86+
compositeDirectory.deleteFile(NON_EXISTENT_FILE);
87+
} catch (Exception e) {
88+
exception = e;
89+
}
90+
assertNull(exception);
8591
}
8692

8793
public void testFileLength() throws IOException {

0 commit comments

Comments
 (0)