/// <exception cref="System.IO.IOException"/>
 /// <exception cref="System.Exception"/>
 public override void StartLocalizer(Path nmPrivateContainerTokensPath, IPEndPoint
                                     nmAddr, string user, string appId, string locId, LocalDirsHandlerService dirsHandler
                                     )
 {
     lock (this)
     {
         IList <string>     localDirs = dirsHandler.GetLocalDirs();
         IList <string>     logDirs   = dirsHandler.GetLogDirs();
         ContainerLocalizer localizer = new ContainerLocalizer(lfs, user, appId, locId, GetPaths
                                                                   (localDirs), RecordFactoryProvider.GetRecordFactory(GetConf()));
         CreateUserLocalDirs(localDirs, user);
         CreateUserCacheDirs(localDirs, user);
         CreateAppDirs(localDirs, user, appId);
         CreateAppLogDirs(appId, logDirs, user);
         // randomly choose the local directory
         Path   appStorageDir = GetWorkingDir(localDirs, user, appId);
         string tokenFn       = string.Format(ContainerLocalizer.TokenFileNameFmt, locId);
         Path   tokenDst      = new Path(appStorageDir, tokenFn);
         CopyFile(nmPrivateContainerTokensPath, tokenDst, user);
         Log.Info("Copying from " + nmPrivateContainerTokensPath + " to " + tokenDst);
         lfs.SetWorkingDirectory(appStorageDir);
         Log.Info("CWD set to " + appStorageDir + " = " + lfs.GetWorkingDirectory());
         // TODO: DO it over RPC for maintaining similarity?
         localizer.RunLocalization(nmAddr);
     }
 }
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestContainerLaunchNullImage()
        {
            string appSubmitter = "nobody";
            string appId        = "APP_ID";
            string containerId  = "CONTAINER_ID";
            string testImage    = string.Empty;

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >(Org.Mockito.Mockito.ReturnsDeepStubs);
            ContainerId cId = Org.Mockito.Mockito.Mock <ContainerId>(Org.Mockito.Mockito.ReturnsDeepStubs
                                                                     );
            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.GetApplicationAttemptId().GetApplicationId().ToString
                                         ()).ThenReturn(appId);
            Org.Mockito.Mockito.When(cId.ToString()).ThenReturn(containerId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(env);
            env[YarnConfiguration.NmDockerContainerExecutorImageName] = testImage;
            dockerContainerExecutor.GetConf().Set(YarnConfiguration.NmDockerContainerExecutorImageName
                                                  , testImage);
            Path scriptPath = new Path("file:///bin/echo");
            Path tokensPath = new Path("file:///dev/null");
            Path pidFile    = new Path(workDir, "pid.txt");

            dockerContainerExecutor.ActivateContainer(cId, pidFile);
            dockerContainerExecutor.LaunchContainer(container, scriptPath, tokensPath, appSubmitter
                                                    , appId, workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs());
        }
        /// <exception cref="System.IO.IOException"/>
        private int RunAndBlock(ContainerId cId, IDictionary <string, string> launchCtxEnv
                                , params string[] cmd)
        {
            string appId = "APP_" + Runtime.CurrentTimeMillis();

            Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container container
                = Org.Mockito.Mockito.Mock <Org.Apache.Hadoop.Yarn.Server.Nodemanager.Containermanager.Container.Container
                                            >();
            ContainerLaunchContext context = Org.Mockito.Mockito.Mock <ContainerLaunchContext>
                                                 ();

            Org.Mockito.Mockito.When(container.GetContainerId()).ThenReturn(cId);
            Org.Mockito.Mockito.When(container.GetLaunchContext()).ThenReturn(context);
            Org.Mockito.Mockito.When(cId.GetApplicationAttemptId().GetApplicationId().ToString
                                         ()).ThenReturn(appId);
            Org.Mockito.Mockito.When(context.GetEnvironment()).ThenReturn(launchCtxEnv);
            string script     = WriteScriptFile(launchCtxEnv, cmd);
            Path   scriptPath = new Path(script);
            Path   tokensPath = new Path("/dev/null");
            Path   workDir    = new Path(workSpace.GetAbsolutePath());
            Path   pidFile    = new Path(workDir, "pid.txt");

            exec.ActivateContainer(cId, pidFile);
            return(exec.LaunchContainer(container, scriptPath, tokensPath, appSubmitter, appId
                                        , workDir, dirsHandler.GetLocalDirs(), dirsHandler.GetLogDirs()));
        }
        public virtual void TestGetFullDirs()
        {
            Configuration conf = new YarnConfiguration();

            conf.Set(CommonConfigurationKeys.FsPermissionsUmaskKey, "077");
            FileContext  localFs        = FileContext.GetLocalFSFileContext(conf);
            string       localDir1      = new FilePath(testDir, "localDir1").GetPath();
            string       localDir2      = new FilePath(testDir, "localDir2").GetPath();
            string       logDir1        = new FilePath(testDir, "logDir1").GetPath();
            string       logDir2        = new FilePath(testDir, "logDir2").GetPath();
            Path         localDir1Path  = new Path(localDir1);
            Path         logDir1Path    = new Path(logDir1);
            FsPermission dirPermissions = new FsPermission((short)0x108);

            localFs.Mkdir(localDir1Path, dirPermissions, true);
            localFs.Mkdir(logDir1Path, dirPermissions, true);
            conf.Set(YarnConfiguration.NmLocalDirs, localDir1 + "," + localDir2);
            conf.Set(YarnConfiguration.NmLogDirs, logDir1 + "," + logDir2);
            conf.SetFloat(YarnConfiguration.NmMaxPerDiskUtilizationPercentage, 0.0f);
            LocalDirsHandlerService dirSvc = new LocalDirsHandlerService();

            dirSvc.Init(conf);
            NUnit.Framework.Assert.AreEqual(0, dirSvc.GetLocalDirs().Count);
            NUnit.Framework.Assert.AreEqual(0, dirSvc.GetLogDirs().Count);
            NUnit.Framework.Assert.AreEqual(1, dirSvc.GetDiskFullLocalDirs().Count);
            NUnit.Framework.Assert.AreEqual(1, dirSvc.GetDiskFullLogDirs().Count);
            FileUtils.DeleteDirectory(new FilePath(localDir1));
            FileUtils.DeleteDirectory(new FilePath(localDir2));
            FileUtils.DeleteDirectory(new FilePath(logDir1));
            FileUtils.DeleteDirectory(new FilePath(logDir1));
            dirSvc.Close();
        }
        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());
        }
Exemple #7
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public override void StartLocalizer(Path nmPrivateContainerTokensPath, IPEndPoint
                                            nmAddr, string user, string appId, string locId, LocalDirsHandlerService dirsHandler
                                            )
        {
            IList <string> localDirs = dirsHandler.GetLocalDirs();
            IList <string> logDirs   = dirsHandler.GetLogDirs();

            VerifyUsernamePattern(user);
            string         runAsUser = GetRunAsUser(user);
            IList <string> command   = new AList <string>();

            AddSchedPriorityCommand(command);
            Sharpen.Collections.AddAll(command, Arrays.AsList(containerExecutorExe, runAsUser
                                                              , user, Sharpen.Extensions.ToString(LinuxContainerExecutor.Commands.InitializeContainer
                                                                                                  .GetValue()), appId, nmPrivateContainerTokensPath.ToUri().GetPath().ToString(),
                                                              StringUtils.Join(",", localDirs), StringUtils.Join(",", logDirs)));
            FilePath jvm = new FilePath(new FilePath(Runtime.GetProperty("java.home"), "bin")
                                        , "java");

            // use same jvm as parent
            command.AddItem(jvm.ToString());
            command.AddItem("-classpath");
            command.AddItem(Runtime.GetProperty("java.class.path"));
            string javaLibPath = Runtime.GetProperty("java.library.path");

            if (javaLibPath != null)
            {
                command.AddItem("-Djava.library.path=" + javaLibPath);
            }
            BuildMainArgs(command, user, appId, locId, nmAddr, localDirs);
            string[] commandArray = Sharpen.Collections.ToArray(command, new string[command.Count
                                                                ]);
            Shell.ShellCommandExecutor shExec = new Shell.ShellCommandExecutor(commandArray);
            if (Log.IsDebugEnabled())
            {
                Log.Debug("initApplication: " + Arrays.ToString(commandArray));
            }
            try
            {
                shExec.Execute();
                if (Log.IsDebugEnabled())
                {
                    LogOutput(shExec.GetOutput());
                }
            }
            catch (Shell.ExitCodeException e)
            {
                int exitCode = shExec.GetExitCode();
                Log.Warn("Exit code from container " + locId + " startLocalizer is : " + exitCode
                         , e);
                LogOutput(shExec.GetOutput());
                throw new IOException("Application " + appId + " initialization failed" + " (exitCode="
                                      + exitCode + ") with output: " + shExec.GetOutput(), e);
            }
        }
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public override void StartLocalizer(Path nmPrivateContainerTokens, IPEndPoint nmAddr
                                            , string user, string appId, string locId, LocalDirsHandlerService dirsHandler)
        {
            IList <string> localDirs = dirsHandler.GetLocalDirs();
            IList <string> logDirs   = dirsHandler.GetLogDirs();
            Path           classpathJarPrivateDir = dirsHandler.GetLocalPathForWrite(ResourceLocalizationService
                                                                                     .NmPrivateDir);

            CreateUserLocalDirs(localDirs, user);
            CreateUserCacheDirs(localDirs, user);
            CreateAppDirs(localDirs, user, appId);
            CreateAppLogDirs(appId, logDirs, user);
            Path   appStorageDir = GetWorkingDir(localDirs, user, appId);
            string tokenFn       = string.Format(ContainerLocalizer.TokenFileNameFmt, locId);
            Path   tokenDst      = new Path(appStorageDir, tokenFn);

            CopyFile(nmPrivateContainerTokens, tokenDst, user);
            FilePath cwdApp = new FilePath(appStorageDir.ToString());

            if (Log.IsDebugEnabled())
            {
                Log.Debug(string.Format("cwdApp: %s", cwdApp));
            }
            IList <string> command;

            command = new AList <string>();
            //use same jvm as parent
            FilePath jvm = new FilePath(new FilePath(Runtime.GetProperty("java.home"), "bin")
                                        , "java.exe");

            command.AddItem(jvm.ToString());
            Path cwdPath = new Path(cwdApp.GetPath());
            // Build a temp classpath jar. See ContainerLaunch.sanitizeEnv().
            // Passing CLASSPATH explicitly is *way* too long for command line.
            string classPath = Runtime.GetProperty("java.class.path");
            IDictionary <string, string> env = new Dictionary <string, string>(Sharpen.Runtime.GetEnv
                                                                                   ());

            string[] jarCp = FileUtil.CreateJarWithClassPath(classPath, classpathJarPrivateDir
                                                             , cwdPath, env);
            string classPathJar = LocalizeClasspathJar(new Path(jarCp[0]), cwdPath, user).ToString
                                      ();

            command.AddItem("-classpath");
            command.AddItem(classPathJar + jarCp[1]);
            string javaLibPath = Runtime.GetProperty("java.library.path");

            if (javaLibPath != null)
            {
                command.AddItem("-Djava.library.path=" + javaLibPath);
            }
            ContainerLocalizer.BuildMainArgs(command, user, appId, locId, nmAddr, localDirs);
            string cmdLine      = StringUtils.Join(command, " ");
            string localizerPid = string.Format(LocalizerPidFormat, locId);

            WindowsSecureContainerExecutor.WintuilsProcessStubExecutor stubExecutor = new WindowsSecureContainerExecutor.WintuilsProcessStubExecutor
                                                                                          (cwdApp.GetAbsolutePath(), localizerPid, user, "nul:", cmdLine);
            try
            {
                stubExecutor.Execute();
                stubExecutor.ValidateResult();
            }
            finally
            {
                stubExecutor.Close();
                try
                {
                    KillContainer(localizerPid, ContainerExecutor.Signal.Kill);
                }
                catch (Exception e)
                {
                    Log.Warn(string.Format("An exception occured during the cleanup of localizer job %s:%n%s"
                                           , localizerPid, StringUtils.StringifyException(e)));
                }
            }
        }
Exemple #9
0
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void TestStartLocalizer()
        {
            IPEndPoint     localizationServerAddress;
            Path           firstDir  = new Path(BaseTmpPath, "localDir1");
            IList <string> localDirs = new AList <string>();
            Path           secondDir = new Path(BaseTmpPath, "localDir2");
            IList <string> logDirs   = new AList <string>();
            Path           logDir    = new Path(BaseTmpPath, "logDir");
            Path           tokenDir  = new Path(BaseTmpPath, "tokenDir");
            FsPermission   perms     = new FsPermission((short)0x1f8);
            Configuration  conf      = new Configuration();

            localizationServerAddress = conf.GetSocketAddr(YarnConfiguration.NmBindHost, YarnConfiguration
                                                           .NmLocalizerAddress, YarnConfiguration.DefaultNmLocalizerAddress, YarnConfiguration
                                                           .DefaultNmLocalizerPort);
            FileContext mockLfs = Org.Mockito.Mockito.Spy(FileContext.GetLocalFSFileContext(conf
                                                                                            ));

            FileContext.Util mockUtil = Org.Mockito.Mockito.Spy(mockLfs.Util());
            Org.Mockito.Mockito.DoAnswer(new _Answer_344(mockUtil)).When(mockLfs).Util();
            Org.Mockito.Mockito.DoAnswer(new _Answer_351(firstDir, mockLfs)).When(mockUtil).Copy
                (Matchers.Any <Path>(), Matchers.Any <Path>());
            // throw an Exception when copy token to the first local dir
            // to simulate no space on the first drive
            // copy token to the second local dir
            Org.Mockito.Mockito.DoAnswer(new _Answer_378(firstDir)).When(mockLfs).GetFsStatus
                (Matchers.Any <Path>());
            // let second local directory return more free space than
            // first local directory
            DefaultContainerExecutor mockExec = Org.Mockito.Mockito.Spy(new DefaultContainerExecutor
                                                                            (mockLfs));

            mockExec.SetConf(conf);
            localDirs.AddItem(mockLfs.MakeQualified(firstDir).ToString());
            localDirs.AddItem(mockLfs.MakeQualified(secondDir).ToString());
            logDirs.AddItem(mockLfs.MakeQualified(logDir).ToString());
            conf.SetStrings(YarnConfiguration.NmLocalDirs, Sharpen.Collections.ToArray(localDirs
                                                                                       , new string[localDirs.Count]));
            conf.Set(YarnConfiguration.NmLogDirs, logDir.ToString());
            mockLfs.Mkdir(tokenDir, perms, true);
            Path   nmPrivateCTokensPath = new Path(tokenDir, "test.tokens");
            string appSubmitter         = "nobody";
            string appId = "APP_ID";
            string locId = "LOC_ID";
            LocalDirsHandlerService dirsHandler = Org.Mockito.Mockito.Mock <LocalDirsHandlerService
                                                                            >();

            Org.Mockito.Mockito.When(dirsHandler.GetLocalDirs()).ThenReturn(localDirs);
            Org.Mockito.Mockito.When(dirsHandler.GetLogDirs()).ThenReturn(logDirs);
            try
            {
                mockExec.StartLocalizer(nmPrivateCTokensPath, localizationServerAddress, appSubmitter
                                        , appId, locId, dirsHandler);
            }
            catch (IOException e)
            {
                NUnit.Framework.Assert.Fail("StartLocalizer failed to copy token file " + e);
            }
            finally
            {
                mockExec.DeleteAsUser(appSubmitter, firstDir);
                mockExec.DeleteAsUser(appSubmitter, secondDir);
                mockExec.DeleteAsUser(appSubmitter, logDir);
                DeleteTmpFiles();
            }
        }