Class FileIoProvider
java.lang.Object
org.apache.hadoop.hdfs.server.datanode.FileIoProvider
This class abstracts out various file IO operations performed by the
DataNode and invokes profiling (for collecting stats) and fault injection
(for testing) event hooks before and after each file IO.
Behavior can be injected into these events by enabling the
profiling and/or fault injection event hooks through
DFSConfigKeys.DFS_DATANODE_FILEIO_PROFILING_SAMPLING_PERCENTAGE_KEY
and DFSConfigKeys.DFS_DATANODE_ENABLE_FILEIO_FAULT_INJECTION_KEY.
These event hooks are disabled by default.
Most functions accept an optional FsVolumeSpi parameter for
instrumentation/logging.
Some methods may look redundant, especially the multiple variations of
move/rename/list. They exist to retain behavior compatibility for existing
code.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumLists the types of file system operations. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionFileIoProvider(org.apache.hadoop.conf.Configuration conf, DataNode datanode) -
Method Summary
Modifier and TypeMethodDescriptionbooleancreateFile(FsVolumeSpi volume, File f) Create a file.booleandelete(FsVolumeSpi volume, File f) Delete a file.booleandeleteWithExistsCheck(FsVolumeSpi volume, File f) Delete a file, first checking to see if it exists.voiddirSync(FsVolumeSpi volume, File dir) Sync the given directory changes to durable device.booleanexists(FsVolumeSpi volume, File f) Check for file existence usingFile.exists().voidflush(FsVolumeSpi volume, Flushable f) SeeFlushable.flush().booleanfullyDelete(FsVolumeSpi volume, File dir) Delete the given directory usingFileUtil.fullyDelete(File).getFileInputStream(FsVolumeSpi volume, File f) Create a FileInputStream usingFileInputStream(File).getFileOutputStream(FsVolumeSpi volume, File f) Create a FileOutputStream usingFileOutputStream(File, boolean).getFileOutputStream(FsVolumeSpi volume, FileDescriptor fd) Create a FileOutputStream usingFileOutputStream(FileDescriptor).getFileOutputStream(FsVolumeSpi volume, File f, boolean append) Create a FileOutputStream usingFileOutputStream(File, boolean).intgetHardLinkCount(FsVolumeSpi volume, File f) Retrieves the number of links to the specified file.org.apache.hadoop.hdfs.server.datanode.ProfilingFileIoEventsgetRandomAccessFile(FsVolumeSpi volume, File f, String mode) Create a RandomAccessFile usingRandomAccessFile(File, String).getShareDeleteFileInputStream(FsVolumeSpi volume, File f, long offset) Create a FileInputStream usingNativeIO.getShareDeleteFileDescriptor(java.io.File, long).String[]list(FsVolumeSpi volume, File dir) Get a listing of the given directory usingFileUtil.listFiles(File).listDirectory(FsVolumeSpi volume, File dir, FilenameFilter filter) Get a listing of the given directory usingIOUtils.listDirectory(File, FilenameFilter).File[]listFiles(FsVolumeSpi volume, File dir) Get a listing of the given directory usingFileUtil.listFiles(File).booleanmkdirs(FsVolumeSpi volume, File dir) SeeFile.mkdirs().voidmkdirsWithExistsCheck(FsVolumeSpi volume, File dir) Create the target directory usingFile.mkdirs()only if it doesn't exist already.voidmove(FsVolumeSpi volume, Path src, Path target, CopyOption... options) Move the src file to the target usingFiles.move(Path, Path, CopyOption...).voidmoveFile(FsVolumeSpi volume, File src, File target) Move the src file to the target usingFileUtils.moveFile(File, File).voidnativeCopyFileUnbuffered(FsVolumeSpi volume, File src, File target, boolean preserveFileDate) openAndSeek(FsVolumeSpi volume, File f, long offset) Create a FileInputStream usingFileInputStream(File)and position it at the given offset.voidposixFadvise(FsVolumeSpi volume, String identifier, FileDescriptor outFd, long offset, long length, int flags) Call posix_fadvise on the given file descriptor.voidrename(FsVolumeSpi volume, File src, File target) Move the src file to the target usingStorage.rename(File, File).voidreplaceFile(FsVolumeSpi volume, File src, File target) Move the src file to the target usingFileUtil.replaceFile(File, File).voidsync(FsVolumeSpi volume, FileOutputStream fos) Sync the givenFileOutputStream.voidsyncFileRange(FsVolumeSpi volume, FileDescriptor outFd, long offset, long numBytes, int flags) Call sync_file_range on the given file descriptor.voidtransferToSocketFully(FsVolumeSpi volume, org.apache.hadoop.net.SocketOutputStream sockOut, FileChannel fileCh, long position, int count, org.apache.hadoop.io.LongWritable waitTime, org.apache.hadoop.io.LongWritable transferTime) Transfer data from a FileChannel to a SocketOutputStream.
-
Field Details
-
LOG
public static final org.slf4j.Logger LOG
-
-
Constructor Details
-
FileIoProvider
- Parameters:
conf- Configuration object. May be null. When null, the event handlers are no-ops.datanode- datanode that owns this FileIoProvider. Used for IO error based volume checker callback
-
-
Method Details
-
flush
SeeFlushable.flush().- Parameters:
volume- target volume. null if unavailable.- Throws:
IOException
-
sync
Sync the givenFileOutputStream.- Parameters:
volume- target volume. null if unavailable.- Throws:
IOException
-
dirSync
Sync the given directory changes to durable device.- Throws:
IOException
-
syncFileRange
public void syncFileRange(@Nullable FsVolumeSpi volume, FileDescriptor outFd, long offset, long numBytes, int flags) throws org.apache.hadoop.io.nativeio.NativeIOException Call sync_file_range on the given file descriptor.- Parameters:
volume- target volume. null if unavailable.- Throws:
org.apache.hadoop.io.nativeio.NativeIOException
-
posixFadvise
public void posixFadvise(@Nullable FsVolumeSpi volume, String identifier, FileDescriptor outFd, long offset, long length, int flags) throws org.apache.hadoop.io.nativeio.NativeIOException Call posix_fadvise on the given file descriptor.- Parameters:
volume- target volume. null if unavailable.- Throws:
org.apache.hadoop.io.nativeio.NativeIOException
-
delete
Delete a file.- Parameters:
volume- target volume. null if unavailable.f- File to delete.- Returns:
- true if the file was successfully deleted.
-
deleteWithExistsCheck
Delete a file, first checking to see if it exists.- Parameters:
volume- target volume. null if unavailable.f- File to delete- Returns:
- true if the file was successfully deleted or if it never existed.
-
transferToSocketFully
public void transferToSocketFully(@Nullable FsVolumeSpi volume, org.apache.hadoop.net.SocketOutputStream sockOut, FileChannel fileCh, long position, int count, org.apache.hadoop.io.LongWritable waitTime, org.apache.hadoop.io.LongWritable transferTime) throws IOException Transfer data from a FileChannel to a SocketOutputStream.- Parameters:
volume- target volume. null if unavailable.sockOut- SocketOutputStream to write the data.fileCh- FileChannel from which to read data.position- position within the channel where the transfer begins.count- number of bytes to transfer.waitTime- returns the nanoseconds spent waiting for the socket to become writable.transferTime- returns the nanoseconds spent transferring data.- Throws:
IOException
-
createFile
Create a file.- Parameters:
volume- target volume. null if unavailable.f- File to be created.- Returns:
- true if the file does not exist and was successfully created. false if the file already exists.
- Throws:
IOException
-
getFileInputStream
public FileInputStream getFileInputStream(@Nullable FsVolumeSpi volume, File f) throws FileNotFoundException Create a FileInputStream usingFileInputStream(File). Wraps the created input stream to intercept read calls before delegating to the wrapped stream.- Parameters:
volume- target volume. null if unavailable.f- File object.- Returns:
- FileInputStream to the given file.
- Throws:
FileNotFoundException
-
getFileOutputStream
public FileOutputStream getFileOutputStream(@Nullable FsVolumeSpi volume, File f, boolean append) throws FileNotFoundException Create a FileOutputStream usingFileOutputStream(File, boolean). Wraps the created output stream to intercept write calls before delegating to the wrapped stream.- Parameters:
volume- target volume. null if unavailable.f- File object.append- if true, then bytes will be written to the end of the file rather than the beginning.- Returns:
- FileOutputStream to the given file object.
- Throws:
FileNotFoundException
-
getFileOutputStream
public FileOutputStream getFileOutputStream(@Nullable FsVolumeSpi volume, File f) throws FileNotFoundException Create a FileOutputStream usingFileOutputStream(File, boolean). Wraps the created output stream to intercept write calls before delegating to the wrapped stream.- Parameters:
volume- target volume. null if unavailable.f- File object.- Returns:
- FileOutputStream to the given file object.
- Throws:
FileNotFoundException
-
getFileOutputStream
Create a FileOutputStream usingFileOutputStream(FileDescriptor). Wraps the created output stream to intercept write calls before delegating to the wrapped stream.- Parameters:
volume- target volume. null if unavailable.fd- File descriptor object.- Returns:
- FileOutputStream to the given file object.
-
openAndSeek
public FileInputStream openAndSeek(@Nullable FsVolumeSpi volume, File f, long offset) throws IOException Create a FileInputStream usingFileInputStream(File)and position it at the given offset. Wraps the created input stream to intercept read calls before delegating to the wrapped stream.- Parameters:
volume- target volume. null if unavailable.f- File object.offset- the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.- Throws:
FileNotFoundExceptionIOException
-
getRandomAccessFile
public RandomAccessFile getRandomAccessFile(@Nullable FsVolumeSpi volume, File f, String mode) throws FileNotFoundException Create a RandomAccessFile usingRandomAccessFile(File, String). Wraps the created input stream to intercept IO calls before delegating to the wrapped RandomAccessFile.- Parameters:
volume- target volume. null if unavailable.f- File object.mode- SeeRandomAccessFilefor a description of the mode string.- Returns:
- RandomAccessFile representing the given file.
- Throws:
FileNotFoundException
-
fullyDelete
Delete the given directory usingFileUtil.fullyDelete(File).- Parameters:
volume- target volume. null if unavailable.dir- directory to be deleted.- Returns:
- true on success false on failure.
-
replaceFile
Move the src file to the target usingFileUtil.replaceFile(File, File).- Parameters:
volume- target volume. null if unavailable.src- source path.target- target path.- Throws:
IOException
-
rename
Move the src file to the target usingStorage.rename(File, File).- Parameters:
volume- target volume. null if unavailable.src- source path.target- target path.- Throws:
IOException
-
moveFile
Move the src file to the target usingFileUtils.moveFile(File, File).- Parameters:
volume- target volume. null if unavailable.src- source path.target- target path.- Throws:
IOException
-
move
public void move(@Nullable FsVolumeSpi volume, Path src, Path target, CopyOption... options) throws IOException Move the src file to the target usingFiles.move(Path, Path, CopyOption...).- Parameters:
volume- target volume. null if unavailable.src- source path.target- target path.options- SeeFiles.move(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)for a description of the options.- Throws:
IOException
-
nativeCopyFileUnbuffered
public void nativeCopyFileUnbuffered(@Nullable FsVolumeSpi volume, File src, File target, boolean preserveFileDate) throws IOException - Parameters:
volume- target volume. null if unavailable.src- an existing file to copy, must not benulltarget- the new file, must not benullpreserveFileDate- true if the file date of the copy should be the same as the original- Throws:
IOException
-
mkdirs
SeeFile.mkdirs().- Parameters:
volume- target volume. null if unavailable.dir- directory to be created.- Returns:
- true only if the directory was created. false if the directory already exists.
- Throws:
IOException- if a directory with the given name does not exist and could not be created.
-
mkdirsWithExistsCheck
Create the target directory usingFile.mkdirs()only if it doesn't exist already.- Parameters:
volume- target volume. null if unavailable.dir- directory to be created.- Throws:
IOException- if the directory could not created
-
listFiles
Get a listing of the given directory usingFileUtil.listFiles(File).- Parameters:
volume- target volume. null if unavailable.dir- Directory to be listed.- Returns:
- array of file objects representing the directory entries.
- Throws:
IOException
-
list
Get a listing of the given directory usingFileUtil.listFiles(File).- Parameters:
volume- target volume. null if unavailable.dir- directory to be listed.- Returns:
- array of strings representing the directory entries.
- Throws:
IOException
-
listDirectory
public List<String> listDirectory(@Nullable FsVolumeSpi volume, File dir, FilenameFilter filter) throws IOException Get a listing of the given directory usingIOUtils.listDirectory(File, FilenameFilter).- Parameters:
volume- target volume. null if unavailable.dir- Directory to list.filter-FilenameFilterto filter the directory entries.- Throws:
IOException
-
getHardLinkCount
Retrieves the number of links to the specified file.- Parameters:
volume- target volume. null if unavailable.f- file whose link count is being queried.- Returns:
- number of hard-links to the given file, including the given path itself.
- Throws:
IOException
-
exists
Check for file existence usingFile.exists().- Parameters:
volume- target volume. null if unavailable.f- file object.- Returns:
- true if the file exists.
-
getProfilingEventHook
public org.apache.hadoop.hdfs.server.datanode.ProfilingFileIoEvents getProfilingEventHook()
-