Class ContainerExecutor
java.lang.Object
org.apache.hadoop.yarn.server.nodemanager.ContainerExecutor
- All Implemented Interfaces:
org.apache.hadoop.conf.Configurable
- Direct Known Subclasses:
DefaultContainerExecutor,LinuxContainerExecutor
public abstract class ContainerExecutor
extends Object
implements org.apache.hadoop.conf.Configurable
This class is abstraction of the mechanism used to launch a container on the
underlying OS. All executor implementations must extend ContainerExecutor.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis class will signal a target container after a specified delay.static enumThe container exit code.static enumThe constants for the signals. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateContainer(org.apache.hadoop.yarn.api.records.ContainerId containerId, org.apache.hadoop.fs.Path pidFilePath) Mark the container as active.voidcleanupBeforeRelaunch(Container container) Perform any cleanup before the next launch of the container.voiddeactivateContainer(org.apache.hadoop.yarn.api.records.ContainerId containerId) Mark the container as inactive.abstract voidDelete specified directories as a given user.abstract org.apache.hadoop.hdfs.protocol.datatransfer.IOStreamPairPerform interactive docker command into running container.org.apache.hadoop.conf.ConfigurationgetConf()getExposedPorts(Container container) String[]getIpAndHost(Container container) static String[]getLocalIpAndHost(Container container) getLocalResources(Container container) protected StringgetNMEnvVar(String varname) protected org.apache.hadoop.fs.PathgetPidFilePath(org.apache.hadoop.yarn.api.records.ContainerId containerId) Get the pidFile of the container.getProcessId(org.apache.hadoop.yarn.api.records.ContainerId containerID) Get the process-identifier for the container.protected String[]getRunCommand(String command, String groupId, String userName, org.apache.hadoop.fs.Path pidFile, org.apache.hadoop.conf.Configuration config) Return a command line to execute the given command in the OS shell.protected String[]getRunCommand(String command, String groupId, String userName, org.apache.hadoop.fs.Path pidFile, org.apache.hadoop.conf.Configuration config, org.apache.hadoop.yarn.api.records.Resource resource) Return a command line to execute the given command in the OS shell.protected String[]getRunCommandForOther(String command, org.apache.hadoop.conf.Configuration config) Return a command line to execute the given command in the OS shell.protected String[]getRunCommandForWindows(String command, String groupId, String userName, org.apache.hadoop.fs.Path pidFile, org.apache.hadoop.conf.Configuration config, org.apache.hadoop.yarn.api.records.Resource resource) Return a command line to execute the given command in the OS shell.abstract voidRun the executor initialization steps.protected booleanisContainerActive(org.apache.hadoop.yarn.api.records.ContainerId containerId) Return whether the container is still active.abstract booleanCheck if a container is alive.abstract intLaunch the container on the node.org.apache.hadoop.fs.PathlocalizeClasspathJar(org.apache.hadoop.fs.Path jarPath, org.apache.hadoop.fs.Path target, String owner) This function localizes the JAR file on-demand.protected voidLog each line of the output string as INFO level log messages.voidpauseContainer(Container container) Pause the container.voidPrepare the container prior to the launch environment being written.intRecover an already existing container.protected File[]readDirAsUser(String user, org.apache.hadoop.fs.Path dir) Return the files in the target directory.abstract booleanPerform the steps necessary to reap the container.abstract intRelaunch the container on the node.voidresumeContainer(Container container) Resume the container from pause state.voidsetConf(org.apache.hadoop.conf.Configuration conf) abstract booleanSignal container with the specified signal.voidstart()abstract voidPrepare the environment for containers in this application to execute.voidstop()abstract voidCreate a symlink file which points to the target.abstract voidupdateYarnSysFS(Context ctx, String user, String appId, String spec) Update cluster information inside container.voidwriteLaunchEnv(OutputStream out, Map<String, String> environment, Map<org.apache.hadoop.fs.Path, List<String>> resources, List<String> command, org.apache.hadoop.fs.Path logDir, String user, String outFilename, LinkedHashSet<String> nmVars) This method writes out the launch environment of a container to a specified path.voidwriteLaunchEnv(OutputStream out, Map<String, String> environment, Map<org.apache.hadoop.fs.Path, List<String>> resources, List<String> command, org.apache.hadoop.fs.Path logDir, String user, LinkedHashSet<String> nmVars) This method writes out the launch environment of a container to the default container launch script.
-
Field Details
-
WILDCARD
- See Also:
-
TOKEN_FILE_NAME_FMT
- See Also:
-
TASK_LAUNCH_SCRIPT_PERMISSION
public static final org.apache.hadoop.fs.permission.FsPermission TASK_LAUNCH_SCRIPT_PERMISSIONThe permissions to use when creating the launch script. -
DIRECTORY_CONTENTS
The relative path to which debug information will be written.
-
-
Constructor Details
-
ContainerExecutor
public ContainerExecutor()
-
-
Method Details
-
setConf
public void setConf(org.apache.hadoop.conf.Configuration conf) - Specified by:
setConfin interfaceorg.apache.hadoop.conf.Configurable
-
getConf
public org.apache.hadoop.conf.Configuration getConf()- Specified by:
getConfin interfaceorg.apache.hadoop.conf.Configurable
-
init
Run the executor initialization steps. Verify that the necessary configs and permissions are in place.- Parameters:
nmContext- Context of NM- Throws:
IOException- if initialization fails
-
start
public void start() -
stop
public void stop() -
localizeClasspathJar
public org.apache.hadoop.fs.Path localizeClasspathJar(org.apache.hadoop.fs.Path jarPath, org.apache.hadoop.fs.Path target, String owner) throws IOException This function localizes the JAR file on-demand. On Windows the ContainerLaunch creates a temporary special JAR manifest of other JARs to workaround the CLASSPATH length. In a secure cluster this JAR must be localized so that the container has access to it. The default implementation returns the classpath passed to it, which is expected to have been created in the node manager's fprivate folder, which will not work with secure Windows clusters.- Parameters:
jarPath- the path to the JAR to localizetarget- the directory where the JAR file should be localizedowner- the name of the user who should own the localized file- Returns:
- the path to the localized JAR file
- Throws:
IOException- if localization fails
-
startLocalizer
public abstract void startLocalizer(LocalizerStartContext ctx) throws IOException, InterruptedException, org.apache.hadoop.yarn.exceptions.ConfigurationException Prepare the environment for containers in this application to execute.For $x in local.dirs create $x/$user/$appId Copy $nmLocal/appTokens -> $N/$user/$appId For $rsrc in private resources Copy $rsrc -> $N/$user/filecache/[idef] For $rsrc in job resources Copy $rsrc -> $N/$user/$appId/filecache/idef
- Parameters:
ctx- LocalizerStartContext that encapsulates necessary information for starting a localizer.- Throws:
IOException- for most application init failuresInterruptedException- if application init thread is halted by NMorg.apache.hadoop.yarn.exceptions.ConfigurationException- if config error was found
-
prepareContainer
Prepare the container prior to the launch environment being written.- Parameters:
ctx- Encapsulates information necessary for launching containers.- Throws:
IOException- if errors occur during container preparation
-
launchContainer
public abstract int launchContainer(ContainerStartContext ctx) throws IOException, org.apache.hadoop.yarn.exceptions.ConfigurationException Launch the container on the node. This is a blocking call and returns only when the container exits.- Parameters:
ctx- Encapsulates information necessary for launching containers.- Returns:
- the return status of the launch
- Throws:
IOException- if the container launch failsorg.apache.hadoop.yarn.exceptions.ConfigurationException- if config error was found
-
relaunchContainer
public abstract int relaunchContainer(ContainerStartContext ctx) throws IOException, org.apache.hadoop.yarn.exceptions.ConfigurationException Relaunch the container on the node. This is a blocking call and returns only when the container exits.- Parameters:
ctx- Encapsulates information necessary for relaunching containers.- Returns:
- the return status of the relaunch
- Throws:
IOException- if the container relaunch failsorg.apache.hadoop.yarn.exceptions.ConfigurationException- if config error was found
-
signalContainer
Signal container with the specified signal.- Parameters:
ctx- Encapsulates information necessary for signaling containers.- Returns:
- returns true if the operation succeeded
- Throws:
IOException- if signaling the container fails
-
reapContainer
Perform the steps necessary to reap the container.- Parameters:
ctx- Encapsulates information necessary for reaping containers.- Returns:
- returns true if the operation succeeded.
- Throws:
IOException- if reaping the container fails.
-
execContainer
public abstract org.apache.hadoop.hdfs.protocol.datatransfer.IOStreamPair execContainer(ContainerExecContext ctx) throws ContainerExecutionException Perform interactive docker command into running container.- Parameters:
ctx- Encapsulates information necessary for exec containers.- Returns:
- return input/output stream if the operation succeeded.
- Throws:
ContainerExecutionException- if container exec fails.
-
deleteAsUser
public abstract void deleteAsUser(DeletionAsUserContext ctx) throws IOException, InterruptedException Delete specified directories as a given user.- Parameters:
ctx- Encapsulates information necessary for deletion.- Throws:
IOException- if delete failsInterruptedException- if interrupted while waiting for the deletion operation to complete
-
symLink
Create a symlink file which points to the target.- Parameters:
target- The target for symlinksymlink- the symlink file- Throws:
IOException- Error when creating symlinks
-
isContainerAlive
Check if a container is alive.- Parameters:
ctx- Encapsulates information necessary for container liveness check.- Returns:
- true if container is still alive
- Throws:
IOException- if there is a failure while checking the container status
-
getLocalResources
public Map<String,org.apache.hadoop.yarn.api.records.LocalResource> getLocalResources(Container container) throws IOException - Throws:
IOException
-
updateYarnSysFS
public abstract void updateYarnSysFS(Context ctx, String user, String appId, String spec) throws IOException Update cluster information inside container.- Parameters:
ctx- ContainerRuntimeContextuser- Owner of applicationappId- YARN application IDspec- Service Specification- Throws:
IOException- if there is a failure while writing spec to disk
-
reacquireContainer
public int reacquireContainer(ContainerReacquisitionContext ctx) throws IOException, InterruptedException Recover an already existing container. This is a blocking call and returns only when the container exits. Note that the container must have been activated prior to this call.- Parameters:
ctx- encapsulates information necessary to reacquire container- Returns:
- The exit code of the pre-existing container
- Throws:
IOException- if there is a failure while reacquiring the containerInterruptedException- if interrupted while waiting to reacquire the container
-
writeLaunchEnv
public void writeLaunchEnv(OutputStream out, Map<String, String> environment, Map<org.apache.hadoop.fs.Path, throws IOExceptionList<String>> resources, List<String> command, org.apache.hadoop.fs.Path logDir, String user, LinkedHashSet<String> nmVars) This method writes out the launch environment of a container to the default container launch script. For the default container script path seeContainerLaunch.CONTAINER_SCRIPT.- Parameters:
out- the output stream to which the environment is written (usually a script file which will be executed by the Launcher)environment- the environment variables and their valuesresources- the resources which have been localized for this container. Symlinks will be created to these localized resourcescommand- the command that will be runlogDir- the log dir to which to copy debugging informationuser- the username of the job ownernmVars- the set of environment vars that are explicitly set by NM- Throws:
IOException- if any errors happened writing to the OutputStream, while creating symlinks
-
writeLaunchEnv
@VisibleForTesting public void writeLaunchEnv(OutputStream out, Map<String, String> environment, Map<org.apache.hadoop.fs.Path, throws IOExceptionList<String>> resources, List<String> command, org.apache.hadoop.fs.Path logDir, String user, String outFilename, LinkedHashSet<String> nmVars) This method writes out the launch environment of a container to a specified path.- Parameters:
out- the output stream to which the environment is written (usually a script file which will be executed by the Launcher)environment- the environment variables and their valuesresources- the resources which have been localized for this container. Symlinks will be created to these localized resourcescommand- the command that will be runlogDir- the log dir to which to copy debugging informationuser- the username of the job owneroutFilename- the path to which to write the launch environmentnmVars- the set of environment vars that are explicitly set by NM- Throws:
IOException- if any errors happened writing to the OutputStream, while creating symlinks
-
readDirAsUser
Return the files in the target directory. If retrieving the list of files requires specific access rights, that access will happen as the specified user. The list will not include entries for "." or "..".- Parameters:
user- the user as whom to access the target directorydir- the target directory- Returns:
- a list of files in the target directory
-
logOutput
Log each line of the output string as INFO level log messages.- Parameters:
output- the output string to log
-
getPidFilePath
protected org.apache.hadoop.fs.Path getPidFilePath(org.apache.hadoop.yarn.api.records.ContainerId containerId) Get the pidFile of the container.- Parameters:
containerId- the container ID- Returns:
- the path of the pid-file for the given containerId.
-
getRunCommand
protected String[] getRunCommand(String command, String groupId, String userName, org.apache.hadoop.fs.Path pidFile, org.apache.hadoop.conf.Configuration config) Return a command line to execute the given command in the OS shell. On Windows, the {code}groupId{code} parameter can be used to launch and associate the given GID with a process group. On non-Windows hosts, the {code}groupId{code} parameter is ignored.- Parameters:
command- the command to executegroupId- the job owner's GIDuserName- the job owner's usernamepidFile- the path to the container's PID fileconfig- the configuration- Returns:
- the command line to execute
-
getRunCommand
protected String[] getRunCommand(String command, String groupId, String userName, org.apache.hadoop.fs.Path pidFile, org.apache.hadoop.conf.Configuration config, org.apache.hadoop.yarn.api.records.Resource resource) Return a command line to execute the given command in the OS shell. On Windows, the {code}groupId{code} parameter can be used to launch and associate the given GID with a process group. On non-Windows hosts, the {code}groupId{code} parameter is ignored.- Parameters:
command- the command to executegroupId- the job owner's GID for Windows. On other operating systems it is ignored.userName- the job owner's username for Windows. On other operating systems it is ignored.pidFile- the path to the container's PID file on Windows. On other operating systems it is ignored.config- the configurationresource- on Windows this parameter controls memory and CPU limits. If null, no limits are set. On other operating systems it is ignored.- Returns:
- the command line to execute
-
getRunCommandForWindows
protected String[] getRunCommandForWindows(String command, String groupId, String userName, org.apache.hadoop.fs.Path pidFile, org.apache.hadoop.conf.Configuration config, org.apache.hadoop.yarn.api.records.Resource resource) Return a command line to execute the given command in the OS shell. The {code}groupId{code} parameter can be used to launch and associate the given GID with a process group.- Parameters:
command- the command to executegroupId- the job owner's GIDuserName- the job owner's usernamepidFile- the path to the container's PID fileconfig- the configurationresource- this parameter controls memory and CPU limits. If null, no limits are set.- Returns:
- the command line to execute
-
getRunCommandForOther
protected String[] getRunCommandForOther(String command, org.apache.hadoop.conf.Configuration config) Return a command line to execute the given command in the OS shell.- Parameters:
command- the command to executeconfig- the configuration- Returns:
- the command line to execute
-
isContainerActive
protected boolean isContainerActive(org.apache.hadoop.yarn.api.records.ContainerId containerId) Return whether the container is still active.- Parameters:
containerId- the target container's ID- Returns:
- true if the container is active
-
getNMEnvVar
-
activateContainer
public void activateContainer(org.apache.hadoop.yarn.api.records.ContainerId containerId, org.apache.hadoop.fs.Path pidFilePath) Mark the container as active.- Parameters:
containerId- the container IDpidFilePath- the path where the executor should write the PID of the launched process
-
getIpAndHost
- Throws:
ContainerExecutionException
-
getLocalIpAndHost
-
deactivateContainer
public void deactivateContainer(org.apache.hadoop.yarn.api.records.ContainerId containerId) Mark the container as inactive. For inactive containers this method has no effect.- Parameters:
containerId- the container ID
-
pauseContainer
Pause the container. The default implementation is to raise a kill event. Specific executor implementations can override this behavior.- Parameters:
container- the Container
-
resumeContainer
Resume the container from pause state. The default implementation ignores this event. Specific implementations can override this behavior.- Parameters:
container- the Container
-
cleanupBeforeRelaunch
Perform any cleanup before the next launch of the container.- Parameters:
container- container- Throws:
IOExceptionInterruptedException
-
getProcessId
Get the process-identifier for the container.- Parameters:
containerID- the container ID- Returns:
- the process ID of the container if it has already launched, or null otherwise
-
getExposedPorts
- Throws:
ContainerExecutionException
-