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.ApplicationId; 031import org.apache.hadoop.yarn.api.records.timeline.TimelineAbout; 032import org.apache.hadoop.yarn.conf.YarnConfiguration; 033import org.apache.hadoop.yarn.util.YarnVersionInfo; 034import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider; 035import org.codehaus.jackson.JsonGenerationException; 036import org.codehaus.jackson.map.JsonMappingException; 037import org.codehaus.jackson.map.ObjectMapper; 038 039/** 040 * The helper class for the timeline module. 041 * 042 */ 043@Public 044@Evolving 045public class TimelineUtils { 046 047 public static final String FLOW_NAME_TAG_PREFIX = "TIMELINE_FLOW_NAME_TAG"; 048 public static final String FLOW_VERSION_TAG_PREFIX = 049 "TIMELINE_FLOW_VERSION_TAG"; 050 public static final String FLOW_RUN_ID_TAG_PREFIX = 051 "TIMELINE_FLOW_RUN_ID_TAG"; 052 public final static String DEFAULT_FLOW_VERSION = "1"; 053 054 private static ObjectMapper mapper; 055 056 static { 057 mapper = new ObjectMapper(); 058 YarnJacksonJaxbJsonProvider.configObjectMapper(mapper); 059 } 060 061 /** 062 * Serialize a POJO object into a JSON string not in a pretty format 063 * 064 * @param o 065 * an object to serialize 066 * @return a JSON string 067 * @throws IOException 068 * @throws JsonMappingException 069 * @throws JsonGenerationException 070 */ 071 public static String dumpTimelineRecordtoJSON(Object o) 072 throws JsonGenerationException, JsonMappingException, IOException { 073 return dumpTimelineRecordtoJSON(o, false); 074 } 075 076 /** 077 * Serialize a POJO object into a JSON string 078 * 079 * @param o 080 * an object to serialize 081 * @param pretty 082 * whether in a pretty format or not 083 * @return a JSON string 084 * @throws IOException 085 * @throws JsonMappingException 086 * @throws JsonGenerationException 087 */ 088 public static String dumpTimelineRecordtoJSON(Object o, boolean pretty) 089 throws JsonGenerationException, JsonMappingException, IOException { 090 if (pretty) { 091 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o); 092 } else { 093 return mapper.writeValueAsString(o); 094 } 095 } 096 097 /** 098 * Returns whether the timeline service is enabled via configuration. 099 * 100 * @param conf the configuration 101 * @return whether the timeline service is enabled. 102 */ 103 public static boolean timelineServiceEnabled(Configuration conf) { 104 return conf.getBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, 105 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_ENABLED); 106 } 107 108 /** 109 * Returns the timeline service version. It does not check whether the 110 * timeline service itself is enabled. 111 * 112 * @param conf the configuration 113 * @return the timeline service version as a float. 114 */ 115 public static float getTimelineServiceVersion(Configuration conf) { 116 return conf.getFloat(YarnConfiguration.TIMELINE_SERVICE_VERSION, 117 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_VERSION); 118 } 119 120 /** 121 * Returns whether the timeline service v.1.5 is enabled via configuration. 122 * 123 * @param conf the configuration 124 * @return whether the timeline service v.1.5 is enabled. V.1.5 refers to a 125 * version equal to 1.5. 126 */ 127 public static boolean timelineServiceV1_5Enabled(Configuration conf) { 128 return timelineServiceEnabled(conf) && 129 Math.abs(getTimelineServiceVersion(conf) - 1.5) < 0.00001; 130 } 131 132 public static TimelineAbout createTimelineAbout(String about) { 133 TimelineAbout tsInfo = new TimelineAbout(about); 134 tsInfo.setHadoopBuildVersion(VersionInfo.getBuildVersion()); 135 tsInfo.setHadoopVersion(VersionInfo.getVersion()); 136 tsInfo.setHadoopVersionBuiltOn(VersionInfo.getDate()); 137 tsInfo.setTimelineServiceBuildVersion(YarnVersionInfo.getBuildVersion()); 138 tsInfo.setTimelineServiceVersion(YarnVersionInfo.getVersion()); 139 tsInfo.setTimelineServiceVersionBuiltOn(YarnVersionInfo.getDate()); 140 return tsInfo; 141 } 142 143 public static InetSocketAddress getTimelineTokenServiceAddress( 144 Configuration conf) { 145 InetSocketAddress timelineServiceAddr = null; 146 if (YarnConfiguration.useHttps(conf)) { 147 timelineServiceAddr = conf.getSocketAddr( 148 YarnConfiguration.TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS, 149 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS, 150 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_PORT); 151 } else { 152 timelineServiceAddr = conf.getSocketAddr( 153 YarnConfiguration.TIMELINE_SERVICE_WEBAPP_ADDRESS, 154 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_ADDRESS, 155 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_PORT); 156 } 157 return timelineServiceAddr; 158 } 159 160 public static Text buildTimelineTokenService(Configuration conf) { 161 InetSocketAddress timelineServiceAddr = 162 getTimelineTokenServiceAddress(conf); 163 return SecurityUtil.buildTokenService(timelineServiceAddr); 164 } 165 166 public static String generateDefaultFlowName(String appName, 167 ApplicationId appId) { 168 return (appName != null && 169 !appName.equals(YarnConfiguration.DEFAULT_APPLICATION_NAME)) ? 170 appName : 171 "flow_" + appId.getClusterTimestamp() + "_" + appId.getId(); 172 } 173 174 /** 175 * Generate flow name tag. 176 * 177 * @param flowName flow name that identifies a distinct flow application which 178 * can be run repeatedly over time 179 * @return flow name tag. 180 */ 181 public static String generateFlowNameTag(String flowName) { 182 return FLOW_NAME_TAG_PREFIX + ":" + flowName; 183 } 184 185 /** 186 * Generate flow version tag. 187 * 188 * @param flowVersion flow version that keeps track of the changes made to the 189 * flow 190 * @return flow version tag. 191 */ 192 public static String generateFlowVersionTag(String flowVersion) { 193 return FLOW_VERSION_TAG_PREFIX + ":" + flowVersion; 194 } 195 196 /** 197 * Generate flow run ID tag. 198 * 199 * @param flowRunId flow run ID that identifies one instance (or specific 200 * execution) of that flow 201 * @return flow run id tag. 202 */ 203 public static String generateFlowRunIdTag(long flowRunId) { 204 return FLOW_RUN_ID_TAG_PREFIX + ":" + flowRunId; 205 } 206}