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.util.VersionInfo; 022import org.apache.hadoop.yarn.api.records.ApplicationId; 023import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; 024import org.apache.hadoop.yarn.api.records.LocalResource; 025import org.apache.hadoop.yarn.util.Records; 026import org.apache.reef.annotations.audience.Private; 027 028import java.nio.ByteBuffer; 029import java.util.HashMap; 030import java.util.List; 031import java.util.Map; 032import java.util.logging.Level; 033import java.util.logging.Logger; 034 035/** 036 * Helper class that creates the various records in the YARN API. 037 */ 038@Private 039public final class YarnTypes { 040 041 // TODO[REEF-537]: Remove once the hadoop version is updated. 042 public static final String MIN_VERSION_KEEP_CONTAINERS_AVAILABLE = "2.4.0"; 043 private static final Logger LOG = Logger.getLogger(YarnTypes.class.getName()); 044 private YarnTypes() { 045 } 046 047 /** 048 * @return a ContainerLaunchContext with the given commands and LocalResources. 049 */ 050 public static ContainerLaunchContext getContainerLaunchContext( 051 final List<String> commands, 052 final Map<String, LocalResource> localResources, 053 final byte[] securityTokenBuffer) { 054 return getContainerLaunchContext(commands, localResources, securityTokenBuffer, null); 055 } 056 057 /** 058 * Gets a LaunchContext and sets the environment variable 059 * {@link YarnUtilities#REEF_YARN_APPLICATION_ID_ENV_VAR} for REEF Evaluators. 060 * @return a ContainerLaunchContext with the given commands and LocalResources. 061 */ 062 public static ContainerLaunchContext getContainerLaunchContext( 063 final List<String> commands, 064 final Map<String, LocalResource> localResources, 065 final byte[] securityTokenBuffer, 066 final ApplicationId applicationId) { 067 final ContainerLaunchContext context = Records.newRecord(ContainerLaunchContext.class); 068 context.setLocalResources(localResources); 069 context.setCommands(commands); 070 final Map<String, String> envMap = new HashMap<>(); 071 if (applicationId != null) { 072 envMap.put(YarnUtilities.REEF_YARN_APPLICATION_ID_ENV_VAR, applicationId.toString()); 073 } 074 075 context.setEnvironment(envMap); 076 if (securityTokenBuffer != null) { 077 context.setTokens(ByteBuffer.wrap(securityTokenBuffer)); 078 LOG.log(Level.INFO, "Added tokens to container launch context"); 079 } 080 return context; 081 } 082 083 public static boolean isAtOrAfterVersion(final String version) { 084 final String hadoopVersion = VersionInfo.getVersion(); 085 086 if (hadoopVersion == null || hadoopVersion.length() < version.length()) { 087 throw new RuntimeException("unsupported or incomplete hadoop version number provided for comparison: " + 088 hadoopVersion); 089 } 090 091 return hadoopVersion.substring(0, version.length()).compareTo(version) >= 0; 092 } 093}