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, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.yarn.util.timeline; 020 021import java.io.IOException; 022import java.net.InetSocketAddress; 023 024import org.apache.hadoop.classification.InterfaceAudience.Public; 025import org.apache.hadoop.classification.InterfaceStability.Evolving; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.io.Text; 028import org.apache.hadoop.security.SecurityUtil; 029import org.apache.hadoop.util.VersionInfo; 030import org.apache.hadoop.yarn.api.records.timeline.TimelineAbout; 031import org.apache.hadoop.yarn.conf.YarnConfiguration; 032import org.apache.hadoop.yarn.util.YarnVersionInfo; 033import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; 034import org.codehaus.jackson.JsonGenerationException; 035import org.codehaus.jackson.map.JsonMappingException; 036import org.codehaus.jackson.map.ObjectMapper; 037 038/** 039 * The helper class for the timeline module. 040 * 041 */ 042@Public 043@Evolving 044public class TimelineUtils { 045 046 private static ObjectMapper mapper; 047 048 static { 049 mapper = new ObjectMapper(); 050 YarnJacksonJaxbJsonProvider.configObjectMapper(mapper); 051 } 052 053 /** 054 * Serialize a POJO object into a JSON string not in a pretty format 055 * 056 * @param o 057 * an object to serialize 058 * @return a JSON string 059 * @throws IOException 060 * @throws JsonMappingException 061 * @throws JsonGenerationException 062 */ 063 public static String dumpTimelineRecordtoJSON(Object o) 064 throws JsonGenerationException, JsonMappingException, IOException { 065 return dumpTimelineRecordtoJSON(o, false); 066 } 067 068 /** 069 * Serialize a POJO object into a JSON string 070 * 071 * @param o 072 * an object to serialize 073 * @param pretty 074 * whether in a pretty format or not 075 * @return a JSON string 076 * @throws IOException 077 * @throws JsonMappingException 078 * @throws JsonGenerationException 079 */ 080 public static String dumpTimelineRecordtoJSON(Object o, boolean pretty) 081 throws JsonGenerationException, JsonMappingException, IOException { 082 if (pretty) { 083 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o); 084 } else { 085 return mapper.writeValueAsString(o); 086 } 087 } 088 089 /** 090 * Returns whether the timeline service is enabled via configuration. 091 * 092 * @param conf the configuration 093 * @return whether the timeline service is enabled. 094 */ 095 public static boolean timelineServiceEnabled(Configuration conf) { 096 return conf.getBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, 097 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_ENABLED); 098 } 099 100 /** 101 * Returns the timeline service version. It does not check whether the 102 * timeline service itself is enabled. 103 * 104 * @param conf the configuration 105 * @return the timeline service version as a float. 106 */ 107 public static float getTimelineServiceVersion(Configuration conf) { 108 return conf.getFloat(YarnConfiguration.TIMELINE_SERVICE_VERSION, 109 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_VERSION); 110 } 111 112 /** 113 * Returns whether the timeline service v.1.5 is enabled via configuration. 114 * 115 * @param conf the configuration 116 * @return whether the timeline service v.1.5 is enabled. V.1.5 refers to a 117 * version equal to 1.5. 118 */ 119 public static boolean timelineServiceV1_5Enabled(Configuration conf) { 120 return timelineServiceEnabled(conf) && 121 Math.abs(getTimelineServiceVersion(conf) - 1.5) < 0.00001; 122 } 123 124 public static TimelineAbout createTimelineAbout(String about) { 125 TimelineAbout tsInfo = new TimelineAbout(about); 126 tsInfo.setHadoopBuildVersion(VersionInfo.getBuildVersion()); 127 tsInfo.setHadoopVersion(VersionInfo.getVersion()); 128 tsInfo.setHadoopVersionBuiltOn(VersionInfo.getDate()); 129 tsInfo.setTimelineServiceBuildVersion(YarnVersionInfo.getBuildVersion()); 130 tsInfo.setTimelineServiceVersion(YarnVersionInfo.getVersion()); 131 tsInfo.setTimelineServiceVersionBuiltOn(YarnVersionInfo.getDate()); 132 return tsInfo; 133 } 134 135 public static InetSocketAddress getTimelineTokenServiceAddress( 136 Configuration conf) { 137 InetSocketAddress timelineServiceAddr = null; 138 if (YarnConfiguration.useHttps(conf)) { 139 timelineServiceAddr = conf.getSocketAddr( 140 YarnConfiguration.TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS, 141 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS, 142 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_PORT); 143 } else { 144 timelineServiceAddr = conf.getSocketAddr( 145 YarnConfiguration.TIMELINE_SERVICE_WEBAPP_ADDRESS, 146 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_ADDRESS, 147 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_PORT); 148 } 149 return timelineServiceAddr; 150 } 151 152 public static Text buildTimelineTokenService(Configuration conf) { 153 InetSocketAddress timelineServiceAddr = 154 getTimelineTokenServiceAddress(conf); 155 return SecurityUtil.buildTokenService(timelineServiceAddr); 156 } 157}