Package org.apache.hadoop.fs
Class LocalDirAllocator
java.lang.Object
org.apache.hadoop.fs.LocalDirAllocator
An implementation of a round-robin scheme for disk allocation for creating
files. The way it works is that it is kept track what disk was last
allocated for a file write. For the current request, the next disk from
the set of disks would be allocated if the free space on the disk is
sufficient enough to accommodate the file that is being considered for
creation. If the space requirements cannot be met, the next disk in order
would be tried and so on till a disk is found with sufficient capacity.
Once a disk with sufficient space is identified, a check is done to make
sure that the disk is writable. Also, there is an API provided that doesn't
take the space requirements into consideration but just checks whether the
disk under consideration is writable (this should be used for cases where
the file size is not known apriori). An API is provided to read a path that
was created earlier. That API works by doing a scan of all the disks for the
input pathname.
This implementation also provides the functionality of having multiple
allocators per JVM (one for each unique functionality or context, like
mapred, dfs-client, etc.). It ensures that there is only one instance of
an allocator per context per JVM.
Note:
1. The contexts referred above are actually the configuration items defined
in the Configuration class like "mapred.local.dir" (for which we want to
control the dir allocations). The context-strings are exactly those
configuration items.
2. This implementation does not take into consideration cases where
a disk becomes read-only or goes out of space while a file is being written
to (disks are shared between multiple processes, and so the latter situation
is probable).
3. In the class implementation, "Disk" is referred to as "Dir", which
actually points to the configured directory on the Disk which will be the
parent for all file write/read allocations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intUsed when size of file to be allocated is unknown. -
Constructor Summary
ConstructorsConstructorDescriptionLocalDirAllocator(String contextCfgItemName) Create an allocator object.LocalDirAllocator(String contextCfgItemName, DiskValidator diskValidator) -
Method Summary
Modifier and TypeMethodDescriptioncreateTmpFileForWrite(String pathStr, long size, Configuration conf) Creates a temporary file in the local FS.getAllLocalPathsToRead(String pathStr, Configuration conf) Get all of the paths that currently exist in the working directories.getLocalPathForWrite(String pathStr, long size, Configuration conf) Get a path from the local FS.getLocalPathForWrite(String pathStr, long size, Configuration conf, boolean checkWrite) Get a path from the local FS.getLocalPathForWrite(String pathStr, Configuration conf) Get a path from the local FS.getLocalPathToRead(String pathStr, Configuration conf) Get a path from the local FS for reading.booleanifExists(String pathStr, Configuration conf) We search through all the configured dirs for the file's existence and return true when we find.static booleanisContextValid(String contextCfgItemName) Method to check whether a context is valid.static voidremoveContext(String contextCfgItemName) Deprecated.
-
Field Details
-
SIZE_UNKNOWN
public static final int SIZE_UNKNOWNUsed when size of file to be allocated is unknown.- See Also:
-
-
Constructor Details
-
LocalDirAllocator
Create an allocator object.- Parameters:
contextCfgItemName- contextCfgItemName.
-
LocalDirAllocator
-
-
Method Details
-
getLocalPathForWrite
Get a path from the local FS. This method should be used if the size of the file is not known apriori. We go round-robin over the set of disks (via the configured dirs) and return the first complete path where we could create the parent directory of the passed path.- Parameters:
pathStr- the requested path (this will be created on the first available disk)conf- the Configuration object- Returns:
- the complete path to the file on a local disk
- Throws:
IOException- raised on errors performing I/O.
-
getLocalPathForWrite
Get a path from the local FS. Pass size as SIZE_UNKNOWN if not known apriori. We round-robin over the set of disks (via the configured dirs) and return the first complete path which has enough space- Parameters:
pathStr- the requested path (this will be created on the first available disk)size- the size of the file that is going to be writtenconf- the Configuration object- Returns:
- the complete path to the file on a local disk
- Throws:
IOException- raised on errors performing I/O.
-
getLocalPathForWrite
public Path getLocalPathForWrite(String pathStr, long size, Configuration conf, boolean checkWrite) throws IOException Get a path from the local FS. Pass size as SIZE_UNKNOWN if not known apriori. We round-robin over the set of disks (via the configured dirs) and return the first complete path which has enough space- Parameters:
pathStr- the requested path (this will be created on the first available disk)size- the size of the file that is going to be writtenconf- the Configuration objectcheckWrite- ensure that the path is writable- Returns:
- the complete path to the file on a local disk
- Throws:
IOException- raised on errors performing I/O.
-
getLocalPathToRead
Get a path from the local FS for reading. We search through all the configured dirs for the file's existence and return the complete path to the file when we find one- Parameters:
pathStr- the requested file (this will be searched)conf- the Configuration object- Returns:
- the complete path to the file on a local disk
- Throws:
IOException- raised on errors performing I/O.
-
getAllLocalPathsToRead
Get all of the paths that currently exist in the working directories.- Parameters:
pathStr- the path underneath the rootsconf- the configuration to look up the roots in- Returns:
- all of the paths that exist under any of the roots
- Throws:
IOException- raised on errors performing I/O.
-
createTmpFileForWrite
Creates a temporary file in the local FS. Pass size as -1 if not known apriori. We round-robin over the set of disks (via the configured dirs) and select the first complete path which has enough space. A file is created on this directory. The file is guaranteed to go away when the JVM exits.- Parameters:
pathStr- prefix for the temporary filesize- the size of the file that is going to be writtenconf- the Configuration object- Returns:
- a unique temporary file
- Throws:
IOException- raised on errors performing I/O.
-
isContextValid
Method to check whether a context is valid.- Parameters:
contextCfgItemName- contextCfgItemName.- Returns:
- true/false
-
removeContext
@Deprecated @LimitedPrivate("MapReduce") public static void removeContext(String contextCfgItemName) Deprecated.Removes the context from the context config items.- Parameters:
contextCfgItemName- contextCfgItemName.
-
ifExists
We search through all the configured dirs for the file's existence and return true when we find.- Parameters:
pathStr- the requested file (this will be searched)conf- the Configuration object- Returns:
- true if files exist. false otherwise
-