Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8744#discussion_r39582635
  
    --- Diff: 
yarn/history/src/main/scala/org/apache/spark/deploy/history/yarn/YarnTimelineUtils.scala
 ---
    @@ -0,0 +1,720 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.spark.deploy.history.yarn
    +
    +import java.io.IOException
    +import java.net.{URI, URL}
    +import java.text.DateFormat
    +import java.util.concurrent.atomic.AtomicLong
    +import java.util.{ArrayList => JArrayList, Collection => JCollection, 
Date, HashMap => JHashMap, Map => JMap}
    +import java.{lang, util}
    +
    +import scala.collection.JavaConversions._
    +import scala.util.control.NonFatal
    +
    +import org.apache.hadoop.conf.Configuration
    +import org.apache.hadoop.service.Service
    +import org.apache.hadoop.yarn.api.records.{ApplicationAttemptId, 
ApplicationId}
    +import 
org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse.TimelinePutError
    +import org.apache.hadoop.yarn.api.records.timeline.{TimelineEntity, 
TimelineEvent, TimelinePutResponse}
    +import org.apache.hadoop.yarn.client.api.TimelineClient
    +import org.apache.hadoop.yarn.conf.YarnConfiguration
    +import org.json4s.JsonAST.JObject
    +import org.json4s._
    +import org.json4s.jackson.JsonMethods._
    +
    +import org.apache.spark.{SparkContext, Logging}
    +import org.apache.spark.deploy.history.yarn.YarnHistoryService._
    +import org.apache.spark.scheduler.{SparkListenerStageCompleted, 
SparkListenerStageSubmitted, SparkListenerExecutorAdded, 
SparkListenerExecutorRemoved, SparkListenerJobEnd, SparkListenerJobStart, 
SparkListenerApplicationEnd, SparkListenerApplicationStart, SparkListenerEvent}
    +import org.apache.spark.util.{JsonProtocol, Utils}
    +
    +/**
    + * Utility methods for timeline classes.
    + */
    +private[spark] object YarnTimelineUtils extends Logging {
    +
    +  /**
    +   * What attempt ID to use as the attempt ID field (not the entity ID) 
when
    +   * there is no attempt info
    +   */
    +  val SINGLE_ATTEMPT = "1"
    +
    +
    +  /**
    +   * Exception text when there is no event info data to unmarshall
    +   */
    +  val E_NO_EVENTINFO = "No 'eventinfo' entry"
    +
    +  /**
    +   * Exception text when there is event info entry in the timeline event, 
but it is empty
    +   */
    +
    +  val E_EMPTY_EVENTINFO = "Empty 'eventinfo' entry"
    +
    +  /**
    +   * counter incremented on every spark event to timeline event creation,
    +   * so guaranteeing uniqueness of event IDs across a single application 
attempt
    +   * (which is implicitly, one per JVM)
    +   */
    +  val uid = new AtomicLong(System.currentTimeMillis())
    +
    +  /**
    +   * Converts a Java object to its equivalent json4s representation.
    +   */
    +  def toJValue(obj: Object): JValue = {
    +    obj match {
    +      case str: String => JString(str)
    +      case dbl: java.lang.Double => JDouble(dbl)
    +      case dec: java.math.BigDecimal => JDecimal(dec)
    +      case int: java.lang.Integer => JInt(BigInt(int))
    +      case long: java.lang.Long => JInt(BigInt(long))
    +      case bool: java.lang.Boolean => JBool(bool)
    +      case map: JMap[_, _] =>
    +        val jmap = map.asInstanceOf[JMap[String, Object]]
    +        JObject(jmap.entrySet().map { e => (e.getKey() -> 
toJValue(e.getValue())) }.toList)
    +      case array: JCollection[_] =>
    +        JArray(array.asInstanceOf[JCollection[Object]].map(o => 
toJValue(o)).toList)
    +      case null => JNothing
    +    }
    +  }
    +
    +  /**
    +   * Converts a JValue into its Java equivalent.
    +   */
    +  def toJavaObject(v: JValue): Object = {
    +    v match {
    +      case JNothing => null
    +      case JNull => null
    +      case JString(s) => s
    +      case JDouble(num) => java.lang.Double.valueOf(num)
    +      case JDecimal(num) => num.bigDecimal
    +      case JInt(num) => java.lang.Long.valueOf(num.longValue)
    +      case JBool(value) => java.lang.Boolean.valueOf(value)
    +      case obj: JObject => toJavaMap(obj)
    +      case JArray(vals) => {
    +        val list = new JArrayList[Object]()
    +        vals.foreach(x => list.add(toJavaObject(x)))
    +        list
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Converts a json4s list of fields into a Java Map suitable for 
serialization by Jackson,
    +   * which is used by the ATS client library.
    +   */
    +  def toJavaMap(sourceObj: JObject): JHashMap[String, Object] = {
    +    val map = new JHashMap[String, Object]()
    +    sourceObj.obj.foreach(f => map.put(f._1, toJavaObject(f._2)))
    +    map
    +  }
    +
    +  /**
    +   * Convert a timeline event to a spark one. Includes
    +   * some basic checks for validity of event payload.
    +   * @param event timeline event
    +   * @return an unmarshalled event
    +   */
    +  def toSparkEvent(event: TimelineEvent): SparkListenerEvent = {
    +    val info = event.getEventInfo()
    +    if (info == null) {
    +      throw new IOException(E_NO_EVENTINFO)
    +    }
    +    if (info.size() == 0) {
    +      throw new IOException(E_EMPTY_EVENTINFO)
    +    }
    +    val payload = toJValue(info)
    +    def jsonToString: String = {
    +      val json = compact(render(payload))
    +      val limit = 256
    +      if (json.length < limit) {
    +        json
    +      } else {
    +          json.substring(0, limit) + " ... }"
    +        }
    +    }
    +    logDebug(s"payload is ${jsonToString}")
    +    val eventField = payload \ "Event"
    +    if (eventField == JNothing) {
    +      throw new IOException(s"No 'Event' entry in $jsonToString")
    +    }
    +
    +    // now the real unmarshalling
    +    try {
    +      JsonProtocol.sparkEventFromJson(payload)
    +    } catch {
    +      // failure in the marshalling; include payload in the message
    +      case ex: MappingException => {
    +        logError(s"$ex while rendering $jsonToString", ex)
    +        throw ex
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Convert a spark event to a timeline event
    +   * @param event handled spark event
    +   * @return a timeline event
    +   */
    +  def toTimelineEvent(event: HandleSparkEvent): TimelineEvent = {
    +    val tlEvent = new TimelineEvent()
    +    tlEvent.setEventType(Utils.getFormattedClassName(event.sparkEvent)
    +        + "-" + YarnTimelineUtils.uid.incrementAndGet.toString)
    +    tlEvent.setTimestamp(event.time)
    +    val kvMap = new JHashMap[String, Object]()
    +    val json = JsonProtocol.sparkEventToJson(event.sparkEvent)
    +    val jObject = json.asInstanceOf[JObject]
    +    // the timeline event wants a map of java objects for Jackson to 
serialize
    +    val hashMap = toJavaMap(jObject)
    +    tlEvent.setEventInfo(hashMap)
    +    tlEvent
    +  }
    +
    +  /**
    +   * Describe the event for logging
    +   * @param event timeline event
    +   * @return a description
    +   */
    +  def describeEvent(event: TimelineEvent): String = {
    +    var sparkEventDetails = ""
    --- End diff --
    
        `val eventDetails = try { ... `


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to