/// <exception cref="System.IO.IOException"/> /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/> public virtual void LaunchAM(ApplicationAttemptId attemptId) { Credentials credentials = new Credentials(); Org.Apache.Hadoop.Security.Token.Token <AMRMTokenIdentifier> token = rmClient.GetAMRMToken (attemptId.GetApplicationId()); // Service will be empty but that's okay, we are just passing down only // AMRMToken down to the real AM which eventually sets the correct // service-address. credentials.AddToken(token.GetService(), token); FilePath tokenFile = FilePath.CreateTempFile("unmanagedAMRMToken", string.Empty, new FilePath(Runtime.GetProperty("user.dir"))); try { FileUtil.Chmod(tokenFile.GetAbsolutePath(), "600"); } catch (Exception ex) { throw new RuntimeException(ex); } tokenFile.DeleteOnExit(); DataOutputStream os = new DataOutputStream(new FileOutputStream(tokenFile, true)); credentials.WriteTokenStorageToStream(os); os.Close(); IDictionary <string, string> env = Sharpen.Runtime.GetEnv(); AList <string> envAMList = new AList <string>(); bool setClasspath = false; foreach (KeyValuePair <string, string> entry in env) { string key = entry.Key; string value = entry.Value; if (key.Equals("CLASSPATH")) { setClasspath = true; if (classpath != null) { value = value + FilePath.pathSeparator + classpath; } } envAMList.AddItem(key + "=" + value); } if (!setClasspath && classpath != null) { envAMList.AddItem("CLASSPATH=" + classpath); } ContainerId containerId = ContainerId.NewContainerId(attemptId, 0); string hostname = Sharpen.Runtime.GetLocalHost().GetHostName(); envAMList.AddItem(ApplicationConstants.Environment.ContainerId.ToString() + "=" + containerId); envAMList.AddItem(ApplicationConstants.Environment.NmHost.ToString() + "=" + hostname ); envAMList.AddItem(ApplicationConstants.Environment.NmHttpPort.ToString() + "=0"); envAMList.AddItem(ApplicationConstants.Environment.NmPort.ToString() + "=0"); envAMList.AddItem(ApplicationConstants.Environment.LocalDirs.ToString() + "= /tmp" ); envAMList.AddItem(ApplicationConstants.AppSubmitTimeEnv + "=" + Runtime.CurrentTimeMillis ()); envAMList.AddItem(ApplicationConstants.ContainerTokenFileEnvName + "=" + tokenFile .GetAbsolutePath()); string[] envAM = new string[envAMList.Count]; SystemProcess amProc = Runtime.GetRuntime().Exec(amCmd, Sharpen.Collections.ToArray (envAMList, envAM)); BufferedReader errReader = new BufferedReader(new InputStreamReader(amProc.GetErrorStream (), Sharpen.Extensions.GetEncoding("UTF-8"))); BufferedReader inReader = new BufferedReader(new InputStreamReader(amProc.GetInputStream (), Sharpen.Extensions.GetEncoding("UTF-8"))); // read error and input streams as this would free up the buffers // free the error stream buffer Sharpen.Thread errThread = new _Thread_244(errReader); Sharpen.Thread outThread = new _Thread_258(inReader); try { errThread.Start(); outThread.Start(); } catch (InvalidOperationException) { } // wait for the process to finish and check the exit code try { int exitCode = amProc.WaitFor(); Log.Info("AM process exited with value: " + exitCode); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } finally { amCompleted = true; } try { // make sure that the error thread exits // on Windows these threads sometimes get stuck and hang the execution // timeout and join later after destroying the process. errThread.Join(); outThread.Join(); errReader.Close(); inReader.Close(); } catch (Exception ie) { Log.Info("ShellExecutor: Interrupted while reading the error/out stream", ie); } catch (IOException ioe) { Log.Warn("Error while closing the error/out stream", ioe); } amProc.Destroy(); }