001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.yarn.util; 020 021import org.apache.hadoop.yarn.api.ApplicationConstants; 022import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; 023import org.apache.hadoop.yarn.api.records.ApplicationId; 024import org.apache.hadoop.yarn.api.records.ContainerId; 025import org.apache.hadoop.yarn.util.ConverterUtils; 026import org.apache.reef.annotations.audience.Private; 027 028import java.util.logging.Level; 029import java.util.logging.Logger; 030 031/** 032 * A helper class for YARN applications. 033 */ 034@Private 035public final class YarnUtilities { 036 public static final String REEF_YARN_APPLICATION_ID_ENV_VAR = "REEF_YARN_APPLICATION_ID"; 037 private static final Logger LOG = Logger.getLogger(YarnUtilities.class.getName()); 038 039 /** 040 * @return the Container ID of the running Container. 041 */ 042 public static String getContainerIdString() { 043 try { 044 return System.getenv(ApplicationConstants.Environment.CONTAINER_ID.key()); 045 } catch (final Exception e) { 046 LOG.log(Level.WARNING, "Unable to get the container ID from the environment, exception " + 047 e + " was thrown."); 048 return null; 049 } 050 } 051 052 /** 053 * @return the Application ID of the YARN application. 054 */ 055 public static ApplicationId getApplicationId() { 056 if (getAppAttemptId() == null) { 057 return null; 058 } 059 060 return getAppAttemptId().getApplicationId(); 061 } 062 063 /** 064 * @return the Application Attempt ID of the YARN application. 065 */ 066 public static ApplicationAttemptId getAppAttemptId() { 067 return getAppAttemptId(getContainerIdString()); 068 } 069 070 /** 071 * @param containerIdString the Container ID of the running Container. 072 * @return the Application Attempt ID of the YARN application. 073 */ 074 public static ApplicationAttemptId getAppAttemptId(final String containerIdString) { 075 if (containerIdString == null) { 076 return null; 077 } 078 079 try { 080 final ContainerId containerId = ConverterUtils.toContainerId(containerIdString); 081 return containerId.getApplicationAttemptId(); 082 } catch (Exception e) { 083 LOG.log(Level.WARNING, "Unable to get the applicationAttempt ID from the environment, exception " + 084 e + " was thrown."); 085 return null; 086 } 087 } 088 089 private YarnUtilities() { 090 } 091}