public RecoveredContainerLaunch(Context context, Configuration configuration, Dispatcher
                                 dispatcher, ContainerExecutor exec, Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Application.Application
                                 app, Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                 container, LocalDirsHandlerService dirsHandler, ContainerManagerImpl containerManager
                                 )
     : base(context, configuration, dispatcher, exec, app, container, dirsHandler, containerManager
            )
 {
     this.shouldLaunchContainer.Set(true);
 }
Esempio n. 2
0
 public DelayedProcessKiller(Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                             container, string user, string pid, long delay, ContainerExecutor.Signal signal
                             , ContainerExecutor containerExecutor)
 {
     this.container         = container;
     this.user              = user;
     this.pid               = pid;
     this.delay             = delay;
     this.signal            = signal;
     this.containerExecutor = containerExecutor;
     SetName("Task killer for " + pid);
     SetDaemon(false);
 }
Esempio n. 3
0
 public ContainerLaunch(Context context, Configuration configuration, Dispatcher dispatcher
                        , ContainerExecutor exec, Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Application.Application
                        app, Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                        container, LocalDirsHandlerService dirsHandler, ContainerManagerImpl containerManager
                        )
 {
     this.context                 = context;
     this.conf                    = configuration;
     this.app                     = app;
     this.exec                    = exec;
     this.container               = container;
     this.dispatcher              = dispatcher;
     this.dirsHandler             = dirsHandler;
     this.containerManager        = containerManager;
     this.sleepDelayBeforeSigKill = conf.GetLong(YarnConfiguration.NmSleepDelayBeforeSigkillMs
                                                 , YarnConfiguration.DefaultNmSleepDelayBeforeSigkillMs);
     this.maxKillWaitTime = conf.GetLong(YarnConfiguration.NmProcessKillWaitMs, YarnConfiguration
                                         .DefaultNmProcessKillWaitMs);
 }
Esempio n. 4
0
            public virtual void Transition(ApplicationImpl app, ApplicationEvent @event)
            {
                ApplicationContainerInitEvent initEvent = (ApplicationContainerInitEvent)@event;

                Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                    = initEvent.GetContainer();
                app.containers[container.GetContainerId()] = container;
                Log.Info("Adding " + container.GetContainerId() + " to application " + app.ToString
                             ());
                switch (app.GetApplicationState())
                {
                case ApplicationState.Running:
                {
                    app.dispatcher.GetEventHandler().Handle(new ContainerInitEvent(container.GetContainerId
                                                                                       ()));
                    break;
                }

                case ApplicationState.Initing:
                case ApplicationState.New:
                {
                    // these get queued up and sent out in AppInitDoneTransition
                    break;
                }

                default:
                {
                    System.Diagnostics.Debug.Assert(false, "Invalid state for InitContainerTransition: "
                                                    + app.GetApplicationState());
                    break;
                }
                }
            }
Esempio n. 5
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestClearLocalDirWhenNodeReboot()
        {
            nm = new TestNodeManagerReboot.MyNodeManager(this);
            nm.Start();
            ContainerManagementProtocol containerManager = nm.GetContainerManager();

            // create files under fileCache
            CreateFiles(nmLocalDir.GetAbsolutePath(), ContainerLocalizer.Filecache, 100);
            localResourceDir.Mkdirs();
            ContainerLaunchContext containerLaunchContext = Records.NewRecord <ContainerLaunchContext
                                                                               >();
            // Construct the Container-id
            ContainerId cId = CreateContainerId();
            URL         localResourceUri = ConverterUtils.GetYarnUrlFromPath(localFS.MakeQualified(new
                                                                                                   Path(localResourceDir.GetAbsolutePath())));
            LocalResource localResource = LocalResource.NewInstance(localResourceUri, LocalResourceType
                                                                    .File, LocalResourceVisibility.Application, -1, localResourceDir.LastModified());
            string destinationFile = "dest_file";
            IDictionary <string, LocalResource> localResources = new Dictionary <string, LocalResource
                                                                                 >();

            localResources[destinationFile] = localResource;
            containerLaunchContext.SetLocalResources(localResources);
            IList <string> commands = new AList <string>();

            containerLaunchContext.SetCommands(commands);
            NodeId nodeId = nm.GetNMContext().GetNodeId();
            StartContainerRequest scRequest = StartContainerRequest.NewInstance(containerLaunchContext
                                                                                , TestContainerManager.CreateContainerToken(cId, 0, nodeId, destinationFile, nm.
                                                                                                                            GetNMContext().GetContainerTokenSecretManager()));
            IList <StartContainerRequest> list = new AList <StartContainerRequest>();

            list.AddItem(scRequest);
            StartContainersRequest allRequests = StartContainersRequest.NewInstance(list);
            UserGroupInformation   currentUser = UserGroupInformation.CreateRemoteUser(cId.GetApplicationAttemptId
                                                                                           ().ToString());
            NMTokenIdentifier nmIdentifier = new NMTokenIdentifier(cId.GetApplicationAttemptId
                                                                       (), nodeId, user, 123);

            currentUser.AddTokenIdentifier(nmIdentifier);
            currentUser.DoAs(new _PrivilegedExceptionAction_152(this, allRequests));
            IList <ContainerId> containerIds = new AList <ContainerId>();

            containerIds.AddItem(cId);
            GetContainerStatusesRequest request = GetContainerStatusesRequest.NewInstance(containerIds
                                                                                          );

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = nm.GetNMContext().GetContainers()[request.GetContainerIds()[0]];
            int MaxTries = 20;
            int numTries = 0;

            while (!container.GetContainerState().Equals(ContainerState.Done) && numTries <=
                   MaxTries)
            {
                try
                {
                    Sharpen.Thread.Sleep(500);
                }
                catch (Exception)
                {
                }
                // Do nothing
                numTries++;
            }
            NUnit.Framework.Assert.AreEqual(ContainerState.Done, container.GetContainerState(
                                                ));
            NUnit.Framework.Assert.IsTrue("The container should create a subDir named currentUser: "******"under localDir/usercache", NumOfLocalDirs(nmLocalDir.GetAbsolutePath(
                                                                                                  ), ContainerLocalizer.Usercache) > 0);
            NUnit.Framework.Assert.IsTrue("There should be files or Dirs under nm_private when "
                                          + "container is launched", NumOfLocalDirs(nmLocalDir.GetAbsolutePath(), ResourceLocalizationService
                                                                                    .NmPrivateDir) > 0);
            // restart the NodeManager
            RestartNM(MaxTries);
            CheckNumOfLocalDirs();
            Org.Mockito.Mockito.Verify(delService, Org.Mockito.Mockito.Times(1)).Delete((string
                                                                                         )Matchers.IsNull(), Matchers.ArgThat(new TestNodeManagerReboot.PathInclude(this,
                                                                                                                                                                    ResourceLocalizationService.NmPrivateDir + "_DEL_")));
            Org.Mockito.Mockito.Verify(delService, Org.Mockito.Mockito.Times(1)).Delete((string
                                                                                         )Matchers.IsNull(), Matchers.ArgThat(new TestNodeManagerReboot.PathInclude(this,
                                                                                                                                                                    ContainerLocalizer.Filecache + "_DEL_")));
            Org.Mockito.Mockito.Verify(delService, Org.Mockito.Mockito.Times(1)).ScheduleFileDeletionTask
                (Matchers.ArgThat(new TestNodeManagerReboot.FileDeletionInclude(this, user, null
                                                                                , new string[] { destinationFile })));
            Org.Mockito.Mockito.Verify(delService, Org.Mockito.Mockito.Times(1)).ScheduleFileDeletionTask
                (Matchers.ArgThat(new TestNodeManagerReboot.FileDeletionInclude(this, null, ContainerLocalizer
                                                                                .Usercache + "_DEL_", new string[] {  })));
            // restart the NodeManager again
            // this time usercache directory should be empty
            RestartNM(MaxTries);
            CheckNumOfLocalDirs();
        }
        /// <exception cref="System.IO.IOException"/>
        public override int LaunchContainer(Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            container, Path nmPrivateContainerScriptPath, Path nmPrivateTokensPath, string
                                            user, string appId, Path containerWorkDir, IList <string> localDirs, IList <string
                                                                                                                        > logDirs)
        {
            FsPermission dirPerm     = new FsPermission(AppdirPerm);
            ContainerId  containerId = container.GetContainerId();
            // create container dirs on all disks
            string containerIdStr = ConverterUtils.ToString(containerId);
            string appIdStr       = ConverterUtils.ToString(containerId.GetApplicationAttemptId().GetApplicationId
                                                                ());

            foreach (string sLocalDir in localDirs)
            {
                Path usersdir     = new Path(sLocalDir, ContainerLocalizer.Usercache);
                Path userdir      = new Path(usersdir, user);
                Path appCacheDir  = new Path(userdir, ContainerLocalizer.Appcache);
                Path appDir       = new Path(appCacheDir, appIdStr);
                Path containerDir = new Path(appDir, containerIdStr);
                CreateDir(containerDir, dirPerm, true, user);
            }
            // Create the container log-dirs on all disks
            CreateContainerLogDirs(appIdStr, containerIdStr, logDirs, user);
            Path tmpDir = new Path(containerWorkDir, YarnConfiguration.DefaultContainerTempDir
                                   );

            CreateDir(tmpDir, dirPerm, false, user);
            // copy container tokens to work dir
            Path tokenDst = new Path(containerWorkDir, ContainerLaunch.FinalContainerTokensFile
                                     );

            CopyFile(nmPrivateTokensPath, tokenDst, user);
            // copy launch script to work dir
            Path launchDst = new Path(containerWorkDir, ContainerLaunch.ContainerScript);

            CopyFile(nmPrivateContainerScriptPath, launchDst, user);
            // Create new local launch wrapper script
            DefaultContainerExecutor.LocalWrapperScriptBuilder sb = GetLocalWrapperScriptBuilder
                                                                        (containerIdStr, containerWorkDir);
            // Fail fast if attempting to launch the wrapper script would fail due to
            // Windows path length limitation.
            if (Shell.Windows && sb.GetWrapperScriptPath().ToString().Length > WinMaxPath)
            {
                throw new IOException(string.Format("Cannot launch container using script at path %s, because it exceeds "
                                                    + "the maximum supported path length of %d characters.  Consider " + "configuring shorter directories in %s."
                                                    , sb.GetWrapperScriptPath(), WinMaxPath, YarnConfiguration.NmLocalDirs));
            }
            Path pidFile = GetPidFilePath(containerId);

            if (pidFile != null)
            {
                sb.WriteLocalWrapperScript(launchDst, pidFile);
            }
            else
            {
                Log.Info("Container " + containerIdStr + " was marked as inactive. Returning terminated error"
                         );
                return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
            }
            // create log dir under app
            // fork script
            Shell.CommandExecutor shExec = null;
            try
            {
                SetScriptExecutable(launchDst, user);
                SetScriptExecutable(sb.GetWrapperScriptPath(), user);
                shExec = BuildCommandExecutor(sb.GetWrapperScriptPath().ToString(), containerIdStr
                                              , user, pidFile, container.GetResource(), new FilePath(containerWorkDir.ToUri().
                                                                                                     GetPath()), container.GetLaunchContext().GetEnvironment());
                if (IsContainerActive(containerId))
                {
                    shExec.Execute();
                }
                else
                {
                    Log.Info("Container " + containerIdStr + " was marked as inactive. Returning terminated error"
                             );
                    return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
                }
            }
            catch (IOException e)
            {
                if (null == shExec)
                {
                    return(-1);
                }
                int exitCode = shExec.GetExitCode();
                Log.Warn("Exit code from container " + containerId + " is : " + exitCode);
                // 143 (SIGTERM) and 137 (SIGKILL) exit codes means the container was
                // terminated/killed forcefully. In all other cases, log the
                // container-executor's output
                if (exitCode != ContainerExecutor.ExitCode.ForceKilled.GetExitCode() && exitCode
                    != ContainerExecutor.ExitCode.Terminated.GetExitCode())
                {
                    Log.Warn("Exception from container-launch with container ID: " + containerId + " and exit code: "
                             + exitCode, e);
                    StringBuilder builder = new StringBuilder();
                    builder.Append("Exception from container-launch.\n");
                    builder.Append("Container id: " + containerId + "\n");
                    builder.Append("Exit code: " + exitCode + "\n");
                    if (!Optional.FromNullable(e.Message).Or(string.Empty).IsEmpty())
                    {
                        builder.Append("Exception message: " + e.Message + "\n");
                    }
                    builder.Append("Stack trace: " + StringUtils.StringifyException(e) + "\n");
                    if (!shExec.GetOutput().IsEmpty())
                    {
                        builder.Append("Shell output: " + shExec.GetOutput() + "\n");
                    }
                    string diagnostics = builder.ToString();
                    LogOutput(diagnostics);
                    container.Handle(new ContainerDiagnosticsUpdateEvent(containerId, diagnostics));
                }
                else
                {
                    container.Handle(new ContainerDiagnosticsUpdateEvent(containerId, "Container killed on request. Exit code is "
                                                                         + exitCode));
                }
                return(exitCode);
            }
            finally
            {
                if (shExec != null)
                {
                    shExec.Close();
                }
            }
            return(0);
        }
Esempio n. 7
0
 /// <summary>Finds the log file with the given filename for the given container.</summary>
 /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
 public static FilePath GetContainerLogFile(ContainerId containerId, string fileName
                                            , string remoteUser, Context context)
 {
     Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
         = context.GetContainers()[containerId];
     Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Application.Application
         application = GetApplicationForContainer(containerId, context);
     CheckAccess(remoteUser, application, context);
     if (container != null)
     {
         CheckState(container.GetContainerState());
     }
     try
     {
         LocalDirsHandlerService dirsHandler = context.GetLocalDirsHandler();
         string relativeContainerLogDir      = ContainerLaunch.GetRelativeContainerLogDir(application
                                                                                          .GetAppId().ToString(), containerId.ToString());
         Path logPath = dirsHandler.GetLogPathToRead(relativeContainerLogDir + Path.Separator
                                                     + fileName);
         URI      logPathURI = new FilePath(logPath.ToString()).ToURI();
         FilePath logFile    = new FilePath(logPathURI.GetPath());
         return(logFile);
     }
     catch (IOException e)
     {
         Log.Warn("Failed to find log file", e);
         throw new NotFoundException("Cannot find this log on the local disk.");
     }
 }
Esempio n. 8
0
        CreateMockedContainer(ApplicationId appId, int containerId)
        {
            ApplicationAttemptId appAttemptId = BuilderUtils.NewApplicationAttemptId(appId, 1
                                                                                     );
            ContainerId cId = BuilderUtils.NewContainerId(appAttemptId, containerId);

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container c =
                Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                          >();
            Org.Mockito.Mockito.When(c.GetContainerId()).ThenReturn(cId);
            ContainerLaunchContext launchContext = Org.Mockito.Mockito.Mock <ContainerLaunchContext
                                                                             >();

            Org.Mockito.Mockito.When(c.GetLaunchContext()).ThenReturn(launchContext);
            Org.Mockito.Mockito.When(launchContext.GetApplicationACLs()).ThenReturn(new Dictionary
                                                                                    <ApplicationAccessType, string>());
            return(c);
        }
Esempio n. 9
0
        /// <exception cref="System.IO.IOException"/>
        public override int LaunchContainer(Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            container, Path nmPrivateCotainerScriptPath, Path nmPrivateTokensPath, string user
                                            , string appId, Path containerWorkDir, IList <string> localDirs, IList <string> logDirs
                                            )
        {
            VerifyUsernamePattern(user);
            string      runAsUser      = GetRunAsUser(user);
            ContainerId containerId    = container.GetContainerId();
            string      containerIdStr = ConverterUtils.ToString(containerId);

            resourcesHandler.PreExecute(containerId, container.GetResource());
            string resourcesOptions = resourcesHandler.GetResourcesOption(containerId);

            Shell.ShellCommandExecutor shExec = null;
            try
            {
                Path pidFilePath = GetPidFilePath(containerId);
                if (pidFilePath != null)
                {
                    IList <string> command = new AList <string>();
                    AddSchedPriorityCommand(command);
                    Sharpen.Collections.AddAll(command, Arrays.AsList(containerExecutorExe, runAsUser
                                                                      , user, Sharpen.Extensions.ToString(LinuxContainerExecutor.Commands.LaunchContainer
                                                                                                          .GetValue()), appId, containerIdStr, containerWorkDir.ToString(), nmPrivateCotainerScriptPath
                                                                      .ToUri().GetPath().ToString(), nmPrivateTokensPath.ToUri().GetPath().ToString(),
                                                                      pidFilePath.ToString(), StringUtils.Join(",", localDirs), StringUtils.Join(",",
                                                                                                                                                 logDirs), resourcesOptions));
                    string[] commandArray = Sharpen.Collections.ToArray(command, new string[command.Count
                                                                        ]);
                    shExec = new Shell.ShellCommandExecutor(commandArray, null, container.GetLaunchContext
                                                                ().GetEnvironment());
                    // NM's cwd
                    // sanitized env
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("launchContainer: " + Arrays.ToString(commandArray));
                    }
                    shExec.Execute();
                    if (Log.IsDebugEnabled())
                    {
                        LogOutput(shExec.GetOutput());
                    }
                }
                else
                {
                    Log.Info("Container was marked as inactive. Returning terminated error");
                    return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
                }
            }
            catch (Shell.ExitCodeException e)
            {
                int exitCode = shExec.GetExitCode();
                Log.Warn("Exit code from container " + containerId + " is : " + exitCode);
                // 143 (SIGTERM) and 137 (SIGKILL) exit codes means the container was
                // terminated/killed forcefully. In all other cases, log the
                // container-executor's output
                if (exitCode != ContainerExecutor.ExitCode.ForceKilled.GetExitCode() && exitCode
                    != ContainerExecutor.ExitCode.Terminated.GetExitCode())
                {
                    Log.Warn("Exception from container-launch with container ID: " + containerId + " and exit code: "
                             + exitCode, e);
                    StringBuilder builder = new StringBuilder();
                    builder.Append("Exception from container-launch.\n");
                    builder.Append("Container id: " + containerId + "\n");
                    builder.Append("Exit code: " + exitCode + "\n");
                    if (!Optional.FromNullable(e.Message).Or(string.Empty).IsEmpty())
                    {
                        builder.Append("Exception message: " + e.Message + "\n");
                    }
                    builder.Append("Stack trace: " + StringUtils.StringifyException(e) + "\n");
                    if (!shExec.GetOutput().IsEmpty())
                    {
                        builder.Append("Shell output: " + shExec.GetOutput() + "\n");
                    }
                    string diagnostics = builder.ToString();
                    LogOutput(diagnostics);
                    container.Handle(new ContainerDiagnosticsUpdateEvent(containerId, diagnostics));
                }
                else
                {
                    container.Handle(new ContainerDiagnosticsUpdateEvent(containerId, "Container killed on request. Exit code is "
                                                                         + exitCode));
                }
                return(exitCode);
            }
            finally
            {
                resourcesHandler.PostExecute(containerId);
            }
            if (Log.IsDebugEnabled())
            {
                Log.Debug("Output from LinuxContainerExecutor's launchContainer follows:");
                LogOutput(shExec.GetOutput());
            }
            return(0);
        }
Esempio n. 10
0
            public override void Handle(LocalizationEvent @event)
            {
                switch (@event.GetType())
                {
                case LocalizationEventType.InitApplicationResources:
                {
                    Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Application.Application
                                                               app = ((ApplicationLocalizationEvent)@event).GetApplication();
                    // Simulate event from ApplicationLocalization.
                    this.dispatcher.GetEventHandler().Handle(new ApplicationInitedEvent(app.GetAppId(
                                                                                            )));
                    break;
                }

                case LocalizationEventType.InitContainerResources:
                {
                    ContainerLocalizationRequestEvent rsrcReqs = (ContainerLocalizationRequestEvent)@event;
                    // simulate localization of all requested resources
                    foreach (ICollection <LocalResourceRequest> rc in rsrcReqs.GetRequestedResources()
                             .Values)
                    {
                        foreach (LocalResourceRequest req in rc)
                        {
                            Org.Apache.Hadoop.Yarn.Server.Nodemanager.DummyContainerManager.Log.Info("DEBUG: "
                                                                                                     + req + ":" + rsrcReqs.GetContainer().GetContainerId());
                            this.dispatcher.GetEventHandler().Handle(new ContainerResourceLocalizedEvent(rsrcReqs
                                                                                                         .GetContainer().GetContainerId(), req, new Path("file:///local" + req.GetPath().
                                                                                                                                                         ToUri().GetPath())));
                        }
                    }
                    break;
                }

                case LocalizationEventType.CleanupContainerResources:
                {
                    Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                        = ((ContainerLocalizationEvent)@event).GetContainer();
                    // TODO: delete the container dir
                    this.dispatcher.GetEventHandler().Handle(new ContainerEvent(container.GetContainerId
                                                                                    (), ContainerEventType.ContainerResourcesCleanedup));
                    break;
                }

                case LocalizationEventType.DestroyApplicationResources:
                {
                    Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Application.Application
                        application = ((ApplicationLocalizationEvent)@event).GetApplication();
                    // decrement reference counts of all resources associated with this
                    // app
                    this.dispatcher.GetEventHandler().Handle(new ApplicationEvent(application.GetAppId
                                                                                      (), ApplicationEventType.ApplicationResourcesCleanedup));
                    break;
                }

                default:
                {
                    NUnit.Framework.Assert.Fail("Unexpected event: " + @event.GetType());
                    break;
                }
                }
            }
Esempio n. 11
0
 // do nothing
 /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
 protected internal override void AuthorizeGetAndStopContainerRequest(ContainerId
                                                                      containerId, Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                                                      container, bool stopRequest, NMTokenIdentifier identifier)
 {
 }
Esempio n. 12
0
            public override void Handle(ContainersLauncherEvent @event)
            {
                Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                    = @event.GetContainer();
                ContainerId containerId = container.GetContainerId();

                switch (@event.GetType())
                {
                case ContainersLauncherEventType.LaunchContainer:
                {
                    this._enclosing.dispatcher.GetEventHandler().Handle(new ContainerEvent(containerId
                                                                                           , ContainerEventType.ContainerLaunched));
                    break;
                }

                case ContainersLauncherEventType.CleanupContainer:
                {
                    this._enclosing.dispatcher.GetEventHandler().Handle(new ContainerExitEvent(containerId
                                                                                               , ContainerEventType.ContainerKilledOnRequest, 0, "Container exited with exit code 0."
                                                                                               ));
                    break;
                }
                }
            }
Esempio n. 13
0
 /// <summary>Launch the container on the node.</summary>
 /// <remarks>
 /// Launch the container on the node. This is a blocking call and returns only
 /// when the container exits.
 /// </remarks>
 /// <param name="container">the container to be launched</param>
 /// <param name="nmPrivateContainerScriptPath">the path for launch script</param>
 /// <param name="nmPrivateTokensPath">the path for tokens for the container</param>
 /// <param name="user">the user of the container</param>
 /// <param name="appId">the appId of the container</param>
 /// <param name="containerWorkDir">the work dir for the container</param>
 /// <param name="localDirs">nm-local-dirs to be used for this container</param>
 /// <param name="logDirs">nm-log-dirs to be used for this container</param>
 /// <returns>the return status of the launch</returns>
 /// <exception cref="System.IO.IOException"/>
 public abstract int LaunchContainer(Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                     container, Path nmPrivateContainerScriptPath, Path nmPrivateTokensPath, string
                                     user, string appId, Path containerWorkDir, IList <string> localDirs, IList <string
                                                                                                                 > logDirs);
        public virtual void TestContainerLaunch()
        {
            string appSubmitter = "nobody";
            string cmd          = LinuxContainerExecutor.Commands.LaunchContainer.GetValue().ToString(
                );
            string appId       = "APP_ID";
            string containerId = "CONTAINER_ID";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerId            cId     = Org.Mockito.Mockito.Mock <ContainerId>();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path workDir    = new Path("/tmp");
            Path pidFile    = new Path(workDir, "pid.txt");

            mockExec.ActivateContainer(cId, pidFile);
            int ret = mockExec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                               , appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs());

            NUnit.Framework.Assert.AreEqual(0, ret);
            NUnit.Framework.Assert.AreEqual(Arrays.AsList(YarnConfiguration.DefaultNmNonsecureModeLocalUser
                                                          , appSubmitter, cmd, appId, containerId, workDir.ToString(), "/bin/echo", "/dev/null"
                                                          , pidFile.ToString(), StringUtils.Join(",", dirsHandler.GetLocalDirs()), StringUtils
                                                          .Join(",", dirsHandler.GetLogDirs()), "cgroups=none"), ReadMockParams());
        }
        public virtual void TestContainerLaunchError()
        {
            // reinitialize executer
            FilePath f = new FilePath("./src/test/resources/mock-container-executer-with-error"
                                      );

            if (!FileUtil.CanExecute(f))
            {
                FileUtil.SetExecutable(f, true);
            }
            string        executorPath = f.GetAbsolutePath();
            Configuration conf         = new Configuration();

            conf.Set(YarnConfiguration.NmLinuxContainerExecutorPath, executorPath);
            conf.Set(YarnConfiguration.NmLocalDirs, "file:///bin/echo");
            conf.Set(YarnConfiguration.NmLogDirs, "file:///dev/null");
            mockExec = Org.Mockito.Mockito.Spy(new LinuxContainerExecutor());
            Org.Mockito.Mockito.DoAnswer(new _Answer_226()).When(mockExec).LogOutput(Matchers.Any
                                                                                     <string>());
            dirsHandler = new LocalDirsHandlerService();
            dirsHandler.Init(conf);
            mockExec.SetConf(conf);
            string appSubmitter = "nobody";
            string cmd          = LinuxContainerExecutor.Commands.LaunchContainer.GetValue().ToString(
                );
            string appId       = "APP_ID";
            string containerId = "CONTAINER_ID";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerId            cId     = Org.Mockito.Mockito.Mock <ContainerId>();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.DoAnswer(new _Answer_254()).When(container).Handle(Matchers.Any
                                                                                   <ContainerDiagnosticsUpdateEvent>());
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path workDir    = new Path("/tmp");
            Path pidFile    = new Path(workDir, "pid.txt");

            mockExec.ActivateContainer(cId, pidFile);
            int ret = mockExec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                               , appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs());

            NUnit.Framework.Assert.AreNotSame(0, ret);
            NUnit.Framework.Assert.AreEqual(Arrays.AsList(YarnConfiguration.DefaultNmNonsecureModeLocalUser
                                                          , appSubmitter, cmd, appId, containerId, workDir.ToString(), "/bin/echo", "/dev/null"
                                                          , pidFile.ToString(), StringUtils.Join(",", dirsHandler.GetLocalDirs()), StringUtils
                                                          .Join(",", dirsHandler.GetLogDirs()), "cgroups=none"), ReadMockParams());
        }
        /// <exception cref="System.IO.IOException"/>
        public override int LaunchContainer(Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            container, Path nmPrivateContainerScriptPath, Path nmPrivateTokensPath, string
                                            userName, string appId, Path containerWorkDir, IList <string> localDirs, IList <string
                                                                                                                            > logDirs)
        {
            string containerImageName = container.GetLaunchContext().GetEnvironment()[YarnConfiguration
                                                                                      .NmDockerContainerExecutorImageName];

            if (Log.IsDebugEnabled())
            {
                Log.Debug("containerImageName from launchContext: " + containerImageName);
            }
            Preconditions.CheckArgument(!Strings.IsNullOrEmpty(containerImageName), "Container image must not be null"
                                        );
            containerImageName = containerImageName.ReplaceAll("['\"]", string.Empty);
            Preconditions.CheckArgument(SaneDockerImage(containerImageName), "Image: " + containerImageName
                                        + " is not a proper docker image");
            string dockerExecutor = GetConf().Get(YarnConfiguration.NmDockerContainerExecutorExecName
                                                  , YarnConfiguration.NmDefaultDockerContainerExecutorExecName);
            FsPermission dirPerm     = new FsPermission(AppdirPerm);
            ContainerId  containerId = container.GetContainerId();
            // create container dirs on all disks
            string containerIdStr = ConverterUtils.ToString(containerId);
            string appIdStr       = ConverterUtils.ToString(containerId.GetApplicationAttemptId().GetApplicationId
                                                                ());

            foreach (string sLocalDir in localDirs)
            {
                Path usersdir     = new Path(sLocalDir, ContainerLocalizer.Usercache);
                Path userdir      = new Path(usersdir, userName);
                Path appCacheDir  = new Path(userdir, ContainerLocalizer.Appcache);
                Path appDir       = new Path(appCacheDir, appIdStr);
                Path containerDir = new Path(appDir, containerIdStr);
                CreateDir(containerDir, dirPerm, true, userName);
            }
            // Create the container log-dirs on all disks
            CreateContainerLogDirs(appIdStr, containerIdStr, logDirs, userName);
            Path tmpDir = new Path(containerWorkDir, YarnConfiguration.DefaultContainerTempDir
                                   );

            CreateDir(tmpDir, dirPerm, false, userName);
            // copy launch script to work dir
            Path launchDst = new Path(containerWorkDir, ContainerLaunch.ContainerScript);

            lfs.Util().Copy(nmPrivateContainerScriptPath, launchDst);
            // copy container tokens to work dir
            Path tokenDst = new Path(containerWorkDir, ContainerLaunch.FinalContainerTokensFile
                                     );

            lfs.Util().Copy(nmPrivateTokensPath, tokenDst);
            string localDirMount         = ToMount(localDirs);
            string logDirMount           = ToMount(logDirs);
            string containerWorkDirMount = ToMount(Sharpen.Collections.SingletonList(containerWorkDir
                                                                                     .ToUri().GetPath()));
            StringBuilder commands   = new StringBuilder();
            string        commandStr = commands.Append(dockerExecutor).Append(" ").Append("run").Append
                                           (" ").Append("--rm --net=host").Append(" ").Append(" --name " + containerIdStr).
                                       Append(localDirMount).Append(logDirMount).Append(containerWorkDirMount).Append(" "
                                                                                                                      ).Append(containerImageName).ToString();
            string dockerPidScript = "`" + dockerExecutor + " inspect --format {{.State.Pid}} "
                                     + containerIdStr + "`";

            // Create new local launch wrapper script
            DockerContainerExecutor.LocalWrapperScriptBuilder sb = new DockerContainerExecutor.UnixLocalWrapperScriptBuilder
                                                                       (this, containerWorkDir, commandStr, dockerPidScript);
            Path pidFile = GetPidFilePath(containerId);

            if (pidFile != null)
            {
                sb.WriteLocalWrapperScript(launchDst, pidFile);
            }
            else
            {
                Log.Info("Container " + containerIdStr + " was marked as inactive. Returning terminated error"
                         );
                return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
            }
            Shell.ShellCommandExecutor shExec = null;
            try
            {
                lfs.SetPermission(launchDst, ContainerExecutor.TaskLaunchScriptPermission);
                lfs.SetPermission(sb.GetWrapperScriptPath(), ContainerExecutor.TaskLaunchScriptPermission
                                  );
                // Setup command to run
                string[] command = GetRunCommand(sb.GetWrapperScriptPath().ToString(), containerIdStr
                                                 , userName, pidFile, this.GetConf());
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("launchContainer: " + commandStr + " " + Joiner.On(" ").Join(command));
                }
                shExec = new Shell.ShellCommandExecutor(command, new FilePath(containerWorkDir.ToUri
                                                                                  ().GetPath()), container.GetLaunchContext().GetEnvironment());
                // sanitized env
                if (IsContainerActive(containerId))
                {
                    shExec.Execute();
                }
                else
                {
                    Log.Info("Container " + containerIdStr + " was marked as inactive. Returning terminated error"
                             );
                    return(ContainerExecutor.ExitCode.Terminated.GetExitCode());
                }
            }
            catch (IOException e)
            {
                if (null == shExec)
                {
                    return(-1);
                }
                int exitCode = shExec.GetExitCode();
                Log.Warn("Exit code from container " + containerId + " is : " + exitCode);
                // 143 (SIGTERM) and 137 (SIGKILL) exit codes means the container was
                // terminated/killed forcefully. In all other cases, log the
                // container-executor's output
                if (exitCode != ContainerExecutor.ExitCode.ForceKilled.GetExitCode() && exitCode
                    != ContainerExecutor.ExitCode.Terminated.GetExitCode())
                {
                    Log.Warn("Exception from container-launch with container ID: " + containerId + " and exit code: "
                             + exitCode, e);
                    LogOutput(shExec.GetOutput());
                    string diagnostics = "Exception from container-launch: \n" + StringUtils.StringifyException
                                             (e) + "\n" + shExec.GetOutput();
                    container.Handle(new ContainerDiagnosticsUpdateEvent(containerId, diagnostics));
                }
                else
                {
                    container.Handle(new ContainerDiagnosticsUpdateEvent(containerId, "Container killed on request. Exit code is "
                                                                         + exitCode));
                }
                return(exitCode);
            }
            finally
            {
                if (shExec != null)
                {
                    shExec.Close();
                }
            }
            return(0);
        }
Esempio n. 17
0
        public virtual void RemoveOrTrackCompletedContainersFromContext(IList <ContainerId
                                                                               > containerIds)
        {
            ICollection <ContainerId> removedContainers = new HashSet <ContainerId>();

            Sharpen.Collections.AddAll(pendingContainersToRemove, containerIds);
            IEnumerator <ContainerId> iter = pendingContainersToRemove.GetEnumerator();

            while (iter.HasNext())
            {
                ContainerId containerId = iter.Next();
                // remove the container only if the container is at DONE state
                Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container nmContainer
                    = context.GetContainers()[containerId];
                if (nmContainer == null)
                {
                    iter.Remove();
                }
                else
                {
                    if (nmContainer.GetContainerState().Equals(ContainerState.Done))
                    {
                        Sharpen.Collections.Remove(context.GetContainers(), containerId);
                        removedContainers.AddItem(containerId);
                        iter.Remove();
                    }
                }
            }
            if (!removedContainers.IsEmpty())
            {
                Log.Info("Removed completed containers from NM context: " + removedContainers);
            }
            pendingCompletedContainers.Clear();
        }
Esempio n. 18
0
        public virtual void TestContainerLaunchError()
        {
            if (Shell.Windows)
            {
                BaseTmpPath = new Path(new FilePath("target").GetAbsolutePath(), typeof(TestDefaultContainerExecutor
                                                                                        ).Name);
            }
            Path           localDir  = new Path(BaseTmpPath, "localDir");
            IList <string> localDirs = new AList <string>();

            localDirs.AddItem(localDir.ToString());
            IList <string> logDirs = new AList <string>();
            Path           logDir  = new Path(BaseTmpPath, "logDir");

            logDirs.AddItem(logDir.ToString());
            Configuration conf = new Configuration();

            conf.Set(CommonConfigurationKeys.FsPermissionsUmaskKey, "077");
            conf.Set(YarnConfiguration.NmLocalDirs, localDir.ToString());
            conf.Set(YarnConfiguration.NmLogDirs, logDir.ToString());
            FileContext lfs = FileContext.GetLocalFSFileContext(conf);
            DefaultContainerExecutor mockExec = Org.Mockito.Mockito.Spy(new DefaultContainerExecutor
                                                                            (lfs));

            mockExec.SetConf(conf);
            Org.Mockito.Mockito.DoAnswer(new _Answer_245()).When(mockExec).LogOutput(Matchers.Any
                                                                                     <string>());
            string appSubmitter = "nobody";
            string appId        = "APP_ID";
            string containerId  = "CONTAINER_ID";

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerId            cId     = Org.Mockito.Mockito.Mock <ContainerId>();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();
            Dictionary <string, string> env = new Dictionary <string, string>();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            try
            {
                Org.Mockito.Mockito.DoAnswer(new _Answer_268()).When(container).Handle(Matchers.Any
                                                                                       <ContainerDiagnosticsUpdateEvent>());
                Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
                Org.Mockito.Mockito.When(cId.GetApplicationAttemptId()).ThenReturn(ApplicationAttemptId
                                                                                   .NewInstance(ApplicationId.NewInstance(0, 1), 0));
                Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
                mockExec.CreateUserLocalDirs(localDirs, appSubmitter);
                mockExec.CreateUserCacheDirs(localDirs, appSubmitter);
                mockExec.CreateAppDirs(localDirs, appSubmitter, appId);
                mockExec.CreateAppLogDirs(appId, logDirs, appSubmitter);
                Path scriptPath = new Path("file:///bin/echo");
                Path tokensPath = new Path("file:///dev/null");
                if (Shell.Windows)
                {
                    FilePath       tmp    = new FilePath(BaseTmpPath.ToString(), "test_echo.cmd");
                    BufferedWriter output = new BufferedWriter(new FileWriter(tmp));
                    output.Write("Exit 1");
                    output.Write("Echo No such file or directory 1>&2");
                    output.Close();
                    scriptPath = new Path(tmp.GetAbsolutePath());
                    tmp        = new FilePath(BaseTmpPath.ToString(), "tokens");
                    tmp.CreateNewFile();
                    tokensPath = new Path(tmp.GetAbsolutePath());
                }
                Path workDir = localDir;
                Path pidFile = new Path(workDir, "pid.txt");
                mockExec.Init();
                mockExec.ActivateContainer(cId, pidFile);
                int ret = mockExec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                                   , appId, workDir, localDirs, localDirs);
                NUnit.Framework.Assert.AreNotSame(0, ret);
            }
            finally
            {
                mockExec.DeleteAsUser(appSubmitter, localDir);
                mockExec.DeleteAsUser(appSubmitter, logDir);
            }
        }
Esempio n. 19
0
 /// <summary>
 /// Finds the local directories that logs for the given container are stored
 /// on.
 /// </summary>
 /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/>
 public static IList <FilePath> GetContainerLogDirs(ContainerId containerId, string
                                                    remoteUser, Context context)
 {
     Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
         = context.GetContainers()[containerId];
     Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Application.Application
         application = GetApplicationForContainer(containerId, context);
     CheckAccess(remoteUser, application, context);
     // It is not required to have null check for container ( container == null )
     // and throw back exception.Because when container is completed, NodeManager
     // remove container information from its NMContext.Configuring log
     // aggregation to false, container log view request is forwarded to NM. NM
     // does not have completed container information,but still NM serve request for
     // reading container logs.
     if (container != null)
     {
         CheckState(container.GetContainerState());
     }
     return(GetContainerLogDirs(containerId, context.GetLocalDirsHandler()));
 }
Esempio n. 20
0
            internal WrappedApplication(TestApplication _enclosing, int id, long timestamp, string
                                        user, int numContainers)
            {
                this._enclosing = _enclosing;
                Configuration conf = new Configuration();

                this.dispatcher = new DrainDispatcher();
                this.containerTokenIdentifierMap = new Dictionary <ContainerId, ContainerTokenIdentifier
                                                                   >();
                this.dispatcher.Init(conf);
                this.localizerBus      = Org.Mockito.Mockito.Mock <EventHandler>();
                this.launcherBus       = Org.Mockito.Mockito.Mock <EventHandler>();
                this.monitorBus        = Org.Mockito.Mockito.Mock <EventHandler>();
                this.auxBus            = Org.Mockito.Mockito.Mock <EventHandler>();
                this.containerBus      = Org.Mockito.Mockito.Mock <EventHandler>();
                this.logAggregationBus = Org.Mockito.Mockito.Mock <EventHandler>();
                this.dispatcher.Register(typeof(LocalizationEventType), this.localizerBus);
                this.dispatcher.Register(typeof(ContainersLauncherEventType), this.launcherBus);
                this.dispatcher.Register(typeof(ContainersMonitorEventType), this.monitorBus);
                this.dispatcher.Register(typeof(AuxServicesEventType), this.auxBus);
                this.dispatcher.Register(typeof(ContainerEventType), this.containerBus);
                this.dispatcher.Register(typeof(LogHandlerEventType), this.logAggregationBus);
                this.nmTokenSecretMgr = Org.Mockito.Mockito.Mock <NMTokenSecretManagerInNM>();
                this.context          = Org.Mockito.Mockito.Mock <Context>();
                Org.Mockito.Mockito.When(this.context.GetContainerTokenSecretManager()).ThenReturn
                    (new NMContainerTokenSecretManager(conf));
                Org.Mockito.Mockito.When(this.context.GetApplicationACLsManager()).ThenReturn(new
                                                                                              ApplicationACLsManager(conf));
                Org.Mockito.Mockito.When(this.context.GetNMTokenSecretManager()).ThenReturn(this.
                                                                                            nmTokenSecretMgr);
                // Setting master key
                MasterKey masterKey = new MasterKeyPBImpl();

                masterKey.SetKeyId(123);
                masterKey.SetBytes(ByteBuffer.Wrap(new byte[] { (123) }));
                this.context.GetContainerTokenSecretManager().SetMasterKey(masterKey);
                this.user  = user;
                this.appId = BuilderUtils.NewApplicationId(timestamp, id);
                this.app   = new ApplicationImpl(this.dispatcher, this.user, this.appId, null, this
                                                 .context);
                this.containers = new AList <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                             >();
                for (int i = 0; i < numContainers; i++)
                {
                    Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                        = this._enclosing.CreateMockedContainer(this.appId, i);
                    this.containers.AddItem(container);
                    long currentTime = Runtime.CurrentTimeMillis();
                    ContainerTokenIdentifier identifier = new ContainerTokenIdentifier(container.GetContainerId
                                                                                           (), string.Empty, string.Empty, null, currentTime + 2000, masterKey.GetKeyId(),
                                                                                       currentTime, Priority.NewInstance(0), 0);
                    this.containerTokenIdentifierMap[identifier.GetContainerID()] = identifier;
                    this.context.GetContainerTokenSecretManager().StartContainerSuccessful(identifier
                                                                                           );
                    NUnit.Framework.Assert.IsFalse(this.context.GetContainerTokenSecretManager().IsValidStartContainerRequest
                                                       (identifier));
                }
                this.dispatcher.Start();
            }