/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-common/src/main/java/org/apache/hadoop/mapreduce/v2/util/MRWebAppUtil.java

http://github.com/apache/hadoop-common · Java · 174 lines · 134 code · 21 blank · 19 comment · 16 complexity · 1e06b423d2724644ca5ff8a72866d63b MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.mapreduce.v2.util;
  19. import com.google.common.base.Joiner;
  20. import com.google.common.base.Splitter;
  21. import org.apache.hadoop.classification.InterfaceAudience.Private;
  22. import org.apache.hadoop.classification.InterfaceStability.Evolving;
  23. import org.apache.hadoop.conf.Configuration;
  24. import org.apache.hadoop.http.HttpConfig;
  25. import org.apache.hadoop.mapreduce.JobID;
  26. import org.apache.hadoop.mapreduce.TypeConverter;
  27. import org.apache.hadoop.mapreduce.v2.jobhistory.JHAdminConfig;
  28. import org.apache.hadoop.net.NetUtils;
  29. import org.apache.hadoop.yarn.api.records.ApplicationId;
  30. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  31. import org.apache.hadoop.yarn.ipc.RPCUtil;
  32. import java.net.InetAddress;
  33. import java.net.InetSocketAddress;
  34. import java.net.UnknownHostException;
  35. import java.util.Iterator;
  36. import static org.apache.hadoop.http.HttpConfig.Policy;
  37. @Private
  38. @Evolving
  39. public class MRWebAppUtil {
  40. private static final Splitter ADDR_SPLITTER = Splitter.on(':').trimResults();
  41. private static final Joiner JOINER = Joiner.on("");
  42. private static Policy httpPolicyInYarn;
  43. private static Policy httpPolicyInJHS;
  44. public static void initialize(Configuration conf) {
  45. setHttpPolicyInYARN(conf.get(
  46. YarnConfiguration.YARN_HTTP_POLICY_KEY,
  47. YarnConfiguration.YARN_HTTP_POLICY_DEFAULT));
  48. setHttpPolicyInJHS(conf.get(JHAdminConfig.MR_HS_HTTP_POLICY,
  49. JHAdminConfig.DEFAULT_MR_HS_HTTP_POLICY));
  50. }
  51. private static void setHttpPolicyInJHS(String policy) {
  52. MRWebAppUtil.httpPolicyInJHS = Policy.fromString(policy);
  53. }
  54. private static void setHttpPolicyInYARN(String policy) {
  55. MRWebAppUtil.httpPolicyInYarn = Policy.fromString(policy);
  56. }
  57. public static Policy getJHSHttpPolicy() {
  58. return MRWebAppUtil.httpPolicyInJHS;
  59. }
  60. public static Policy getYARNHttpPolicy() {
  61. return MRWebAppUtil.httpPolicyInYarn;
  62. }
  63. public static String getYARNWebappScheme() {
  64. return httpPolicyInYarn == HttpConfig.Policy.HTTPS_ONLY ? "https://"
  65. : "http://";
  66. }
  67. public static String getJHSWebappScheme() {
  68. return httpPolicyInJHS == HttpConfig.Policy.HTTPS_ONLY ? "https://"
  69. : "http://";
  70. }
  71. public static void setJHSWebappURLWithoutScheme(Configuration conf,
  72. String hostAddress) {
  73. if (httpPolicyInJHS == Policy.HTTPS_ONLY) {
  74. conf.set(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS, hostAddress);
  75. } else {
  76. conf.set(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS, hostAddress);
  77. }
  78. }
  79. public static String getJHSWebappURLWithoutScheme(Configuration conf) {
  80. if (httpPolicyInJHS == Policy.HTTPS_ONLY) {
  81. return conf.get(JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
  82. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS);
  83. } else {
  84. return conf.get(JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS,
  85. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS);
  86. }
  87. }
  88. public static String getJHSWebappURLWithScheme(Configuration conf) {
  89. return getJHSWebappScheme() + getJHSWebappURLWithoutScheme(conf);
  90. }
  91. public static InetSocketAddress getJHSWebBindAddress(Configuration conf) {
  92. if (httpPolicyInJHS == Policy.HTTPS_ONLY) {
  93. return conf.getSocketAddr(
  94. JHAdminConfig.MR_HISTORY_BIND_HOST,
  95. JHAdminConfig.MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
  96. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS,
  97. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT);
  98. } else {
  99. return conf.getSocketAddr(
  100. JHAdminConfig.MR_HISTORY_BIND_HOST,
  101. JHAdminConfig.MR_HISTORY_WEBAPP_ADDRESS,
  102. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS,
  103. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT);
  104. }
  105. }
  106. public static String getApplicationWebURLOnJHSWithoutScheme(Configuration conf,
  107. ApplicationId appId)
  108. throws UnknownHostException {
  109. //construct the history url for job
  110. String addr = getJHSWebappURLWithoutScheme(conf);
  111. Iterator<String> it = ADDR_SPLITTER.split(addr).iterator();
  112. it.next(); // ignore the bind host
  113. String port = it.next();
  114. // Use hs address to figure out the host for webapp
  115. addr = conf.get(JHAdminConfig.MR_HISTORY_ADDRESS,
  116. JHAdminConfig.DEFAULT_MR_HISTORY_ADDRESS);
  117. String host = ADDR_SPLITTER.split(addr).iterator().next();
  118. String hsAddress = JOINER.join(host, ":", port);
  119. InetSocketAddress address = NetUtils.createSocketAddr(
  120. hsAddress, getDefaultJHSWebappPort(),
  121. getDefaultJHSWebappURLWithoutScheme());
  122. StringBuffer sb = new StringBuffer();
  123. if (address.getAddress().isAnyLocalAddress() ||
  124. address.getAddress().isLoopbackAddress()) {
  125. sb.append(InetAddress.getLocalHost().getCanonicalHostName());
  126. } else {
  127. sb.append(address.getHostName());
  128. }
  129. sb.append(":").append(address.getPort());
  130. sb.append("/jobhistory/job/");
  131. JobID jobId = TypeConverter.fromYarn(appId);
  132. sb.append(jobId.toString());
  133. return sb.toString();
  134. }
  135. public static String getApplicationWebURLOnJHSWithScheme(Configuration conf,
  136. ApplicationId appId) throws UnknownHostException {
  137. return getJHSWebappScheme()
  138. + getApplicationWebURLOnJHSWithoutScheme(conf, appId);
  139. }
  140. private static int getDefaultJHSWebappPort() {
  141. return httpPolicyInJHS == Policy.HTTPS_ONLY ?
  142. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_PORT:
  143. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_PORT;
  144. }
  145. private static String getDefaultJHSWebappURLWithoutScheme() {
  146. return httpPolicyInJHS == Policy.HTTPS_ONLY ?
  147. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_HTTPS_ADDRESS :
  148. JHAdminConfig.DEFAULT_MR_HISTORY_WEBAPP_ADDRESS;
  149. }
  150. public static String getAMWebappScheme(Configuration conf) {
  151. return "http://";
  152. }
  153. }