Skip to content

Commit 45b2051

Browse files
authored
HDDS-15623. Disk check may not completely read test file (#10552)
1 parent 93a626d commit 45b2051

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/DiskCheckUtil.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import java.io.SyncFailedException;
3131
import java.nio.file.Files;
3232
import java.nio.file.NoSuchFileException;
33+
import java.nio.file.Path;
3334
import java.util.Arrays;
3435
import java.util.Random;
3536
import java.util.UUID;
37+
import org.apache.commons.io.IOUtils;
3638
import org.apache.ratis.util.FileUtils;
3739
import org.slf4j.Logger;
3840
import org.slf4j.LoggerFactory;
@@ -141,9 +143,10 @@ public boolean checkPermissions(File storageDir) {
141143
public boolean checkReadWrite(File storageDir,
142144
File testFileDir, int numBytesToWrite) {
143145
File testFile = new File(testFileDir, "disk-check-" + UUID.randomUUID());
146+
Path testPath = testFile.toPath();
144147
byte[] writtenBytes = new byte[numBytesToWrite];
145148
RANDOM.nextBytes(writtenBytes);
146-
try (OutputStream fos = FileUtils.newOutputStreamForceAtClose(testFile, CREATE, TRUNCATE_EXISTING, WRITE)) {
149+
try (OutputStream fos = FileUtils.newOutputStreamForceAtClose(testPath, CREATE, TRUNCATE_EXISTING, WRITE)) {
147150
fos.write(writtenBytes);
148151
} catch (FileNotFoundException | NoSuchFileException notFoundEx) {
149152
logError(storageDir, String.format("Could not find file %s for " +
@@ -152,35 +155,41 @@ public boolean checkReadWrite(File storageDir,
152155
} catch (SyncFailedException syncEx) {
153156
logError(storageDir, String.format("Could not sync file %s to disk.",
154157
testFile.getAbsolutePath()), syncEx);
158+
FileUtils.deletePathQuietly(testPath);
155159
return false;
156160
} catch (IOException ioEx) {
157161
String msg = ioEx.getMessage();
158162
if (msg != null && msg.contains(LINUX_DISK_FULL_MESSAGE)) {
159163
LOG.warn("Could not write file {} for volume check", testFile.getAbsolutePath(), ioEx);
164+
FileUtils.deletePathQuietly(testPath);
160165
return true;
161166
}
162167
logError(storageDir, String.format("Could not write file %s " +
163168
"for volume check.", testFile.getAbsolutePath()), ioEx);
169+
FileUtils.deletePathQuietly(testPath);
164170
return false;
165171
}
166172

167173
// Read data back from the test file.
168174
byte[] readBytes = new byte[numBytesToWrite];
169-
try (InputStream fis = Files.newInputStream(testFile.toPath())) {
170-
int numBytesRead = fis.read(readBytes);
175+
try (InputStream fis = Files.newInputStream(testPath)) {
176+
int numBytesRead = IOUtils.read(fis, readBytes);
171177
if (numBytesRead != numBytesToWrite) {
172178
logError(storageDir, String.format("%d bytes written to file %s " +
173179
"but %d bytes were read back.", numBytesToWrite,
174180
testFile.getAbsolutePath(), numBytesRead));
181+
FileUtils.deletePathQuietly(testPath);
175182
return false;
176183
}
177184
} catch (FileNotFoundException | NoSuchFileException notFoundEx) {
178185
logError(storageDir, String.format("Could not find file %s " +
179186
"for volume check.", testFile.getAbsolutePath()), notFoundEx);
187+
FileUtils.deletePathQuietly(testPath);
180188
return false;
181189
} catch (IOException ioEx) {
182190
logError(storageDir, String.format("Could not read file %s " +
183191
"for volume check.", testFile.getAbsolutePath()), ioEx);
192+
FileUtils.deletePathQuietly(testPath);
184193
return false;
185194
}
186195

@@ -189,6 +198,7 @@ public boolean checkReadWrite(File storageDir,
189198
logError(storageDir, String.format("%d Bytes read from file " +
190199
"%s do not match the %d bytes that were written.",
191200
writtenBytes.length, testFile.getAbsolutePath(), readBytes.length));
201+
FileUtils.deletePathQuietly(testPath);
192202
return false;
193203
}
194204

hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/utils/TestDiskCheckUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.File;
2929
import java.nio.file.FileSystemException;
3030
import java.nio.file.OpenOption;
31+
import java.nio.file.Path;
3132
import org.apache.ratis.util.FileUtils;
3233
import org.junit.jupiter.api.Test;
3334
import org.junit.jupiter.api.io.TempDir;
@@ -84,7 +85,10 @@ public void testExistence() {
8485
@Test
8586
public void testReadWrite() {
8687
assertTrue(DiskCheckUtil.checkReadWrite(testDir, testDir, 10));
88+
assertTestFileDeleted();
89+
}
8790

91+
private void assertTestFileDeleted() {
8892
// Test file should have been deleted.
8993
File[] children = testDir.listFiles();
9094
assertNotNull(children);
@@ -95,16 +99,16 @@ public void testReadWrite() {
9599
public void testCheckReadWriteDiskFull() {
96100
try (MockedStatic<FileUtils> mockService = mockStatic(FileUtils.class)) {
97101
// fos.write(writtenBytes) also through FileSystemException with the message
98-
mockService.when(() -> FileUtils.newOutputStreamForceAtClose(any(File.class), any(OpenOption[].class)))
102+
mockService.when(() -> FileUtils.newOutputStreamForceAtClose(any(Path.class), any(OpenOption[].class)))
99103
.thenThrow(new FileSystemException("No space left on device"));
100104

101-
String path = testDir.getAbsolutePath();
102105
assertThrows(FileSystemException.class,
103-
() -> FileUtils.newOutputStreamForceAtClose(new File(path), new OpenOption[2]));
106+
() -> FileUtils.newOutputStreamForceAtClose(testDir.toPath(), new OpenOption[2]));
104107

105108
// Test that checkReadWrite returns true for the disk full case
106109
boolean result = DiskCheckUtil.checkReadWrite(testDir, testDir, 1024);
107110
assertTrue(result, "checkReadWrite should return true when disk is full");
111+
assertTestFileDeleted();
108112
}
109113
}
110114
}

0 commit comments

Comments
 (0)