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 public static TimelineAbout createTimelineAbout(String about) { 090 TimelineAbout tsInfo = new TimelineAbout(about); 091 tsInfo.setHadoopBuildVersion(VersionInfo.getBuildVersion()); 092 tsInfo.setHadoopVersion(VersionInfo.getVersion()); 093 tsInfo.setHadoopVersionBuiltOn(VersionInfo.getDate()); 094 tsInfo.setTimelineServiceBuildVersion(YarnVersionInfo.getBuildVersion()); 095 tsInfo.setTimelineServiceVersion(YarnVersionInfo.getVersion()); 096 tsInfo.setTimelineServiceVersionBuiltOn(YarnVersionInfo.getDate()); 097 return tsInfo; 098 } 099 100 public static InetSocketAddress getTimelineTokenServiceAddress( 101 Configuration conf) { 102 InetSocketAddress timelineServiceAddr = null; 103 if (YarnConfiguration.useHttps(conf)) { 104 timelineServiceAddr = conf.getSocketAddr( 105 YarnConfiguration.TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS, 106 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_ADDRESS, 107 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_HTTPS_PORT); 108 } else { 109 timelineServiceAddr = conf.getSocketAddr( 110 YarnConfiguration.TIMELINE_SERVICE_WEBAPP_ADDRESS, 111 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_ADDRESS, 112 YarnConfiguration.DEFAULT_TIMELINE_SERVICE_WEBAPP_PORT); 113 } 114 return timelineServiceAddr; 115 } 116 117 public static Text buildTimelineTokenService(Configuration conf) { 118 InetSocketAddress timelineServiceAddr = 119 getTimelineTokenServiceAddress(conf); 120 return SecurityUtil.buildTokenService(timelineServiceAddr); 121 } 122}