Spark Event Log

本文摘自 Spark Event Log

Spark Event Log

You can find in this note a few examples on how to read SparkEventlog files to extract SQL workload/performance metrics using Spark SQL.
Some of the topics addressed are:

  • What is Spark EventLog and what info you can find there
  • How to read them using Spark SQL reader
  • Relevant SQL to extract and run aggregation on the data, notably working with nested structures present in the Event Log

Motivations

This is useful if you want to analyze the performance of your applications by processing the eventLog data beyond
what is available using Spark history server. For example you want to process data to perform custom aggregations
and/or use notebook-style tools. Another scenario is that you want to automate the analysis of multiple eventLog files.


Some background on Spark EventLog/applicationHistory files

  • The Spark driver logs into job workload/perf metrics in the spark.evenLog.dir directory as JSON files.
  • There is one file per application, the file names contains the application id (therefore including a timestamp)
    application_1502789566015_17671.
  • While the application is running the file as a suffix .inprogress, the suffix is removed if the application gracefully stops. This means that the .inprogress suffix can stick to the file in certains cases, such as driver crashes.
  • Typically these files are read with the Web UI and the history server.
  • EventLog JSON files can also be read directly.

Config

Spark Event Log records info on processed jobs/stages/tasks. See details at [https://spark.apache.org/docs/latest/monitoring.html]
This feature is activated and configured with spark config options. This is an example:

spark.eventLog.enabled=true
spark.eventLog.dir=hdfs:///user/spark/applicationHistory

Example of how to read Event Log JSON files with Spark SQL

This is an example of how to read and do basic processing with Spark Dataframes/SQL:

val df = spark.read.json("/user/spark/applicationHistory/application_1502789566015_25541")
df.printSchema

The JSON file has many attributes and a rich schema, see df.printSchema.
One useful source of info is about task metrics and events (generated by executors and handled via the Spark Listener infrastruture).
Here an

// show the type of events and the number of entries for each
df.select("Event").groupBy("Event").count.show(20,false)

scala> df.select("Event").groupBy("Event").count.show(20,false)
+----------------------------------------------------------------+-----+
|Event                                                           |count|
+----------------------------------------------------------------+-----+
|org.apache.spark.sql.execution.ui.SparkListenerSQLExecutionEnd  |3    |
|SparkListenerTaskStart                                          |249  |
|SparkListenerBlockManagerAdded                                  |4    |
|SparkListenerJobStart                                           |2    |
|SparkListenerStageCompleted                                     |5    |
|SparkListenerJobEnd                                             |2    |
|SparkListenerLogStart                                           |1    |
|SparkListenerExecutorAdded                                      |4    |
|org.apache.spark.sql.execution.ui.SparkListenerSQLExecutionStart|3    |
|SparkListenerEnvironmentUpdate                                  |1    |
|SparkListenerStageSubmitted                                     |5    |
|SparkListenerTaskEnd                                            |249  |
|SparkListenerApplicationStart                                   |1    |
+----------------------------------------------------------------+-----+

// Note use this SQL code if you prefer
df.createOrReplaceTempView("t1")
sql("select Event,count(*) from t1 group by Event").show(30,false)

Example analysis using Stage metrics:

  • This extract Stage Info values from the event log JSON
  • Metrics are stored in an array "Accumulables"
  • the SQL lateral view explode(Accumulables) is used to join the nested
    data in Accumulates with the main dataframe
val df = spark.read.json("/user/spark/applicationHistory/application_1502789566015_25541")

val df2 = df.filter("Event='SparkListenerStageCompleted'").select("`Stage Info`.*")
df2.createOrReplaceTempView("t2")

val df4 = sql("select 'Submission Time','Completion Time', 'Number of Tasks', 'Stage ID', t3.col.* from t2 lateral view explode(Accumulables) t3")
df4.show(20,false)
df4.createOrReplaceTempView("t4")

Examples:

// aggregate stage info metrics values
scala> sql("select Name, sum(Value) as value from t4 group by Name order by Name").show(40,false)

+---------------------------------------------------+----------------+
|Name                                               |value           |
+---------------------------------------------------+----------------+
|aggregate time total (min, med, max)               |1230038.0       |
|avg hash probe (min, med, max)                     |1240.0          |
|data size total (min, med, max)                    |5.6000205E7     |
|duration total (min, med, max)                     |3202872.0       |
|internal.metrics.executorCpuTime                   |1.46231111372E11|
|internal.metrics.executorDeserializeCpuTime        |3.445626341E9   |
|internal.metrics.executorDeserializeTime           |27622.0         |
|internal.metrics.executorRunTime                   |857185.0        |
|internal.metrics.input.bytesRead                   |1.3991536E7     |
|internal.metrics.input.recordsRead                 |1000224.0       |
|internal.metrics.jvmGCTime                         |100728.0        |
|internal.metrics.peakExecutionMemory               |1.2690128896E10 |
|internal.metrics.resultSerializationTime           |103.0           |
|internal.metrics.resultSize                        |1114448.0       |
|internal.metrics.shuffle.read.fetchWaitTime        |522274.0        |
|internal.metrics.shuffle.read.localBlocksFetched   |996.0           |
|internal.metrics.shuffle.read.localBytesRead       |4224628.0       |
|internal.metrics.shuffle.read.recordsRead          |2000016.0       |
|internal.metrics.shuffle.read.remoteBlocksFetched  |2988.0          |
|internal.metrics.shuffle.read.remoteBytesRead      |1.2722302E7     |
|internal.metrics.shuffle.read.remoteBytesReadToDisk|0.0             |
|internal.metrics.shuffle.write.bytesWritten        |1.694693E7      |
|internal.metrics.shuffle.write.recordsWritten      |2000016.0       |
|internal.metrics.shuffle.write.writeTime           |5.894307225E9   |
|number of output rows                              |2.504759806E9   |
|peak memory total (min, med, max)                  |1.2690128092E10 |
|sort time total (min, med, max)                    |221.0           |
+---------------------------------------------------+----------------+

scala> df2.printSchema
root
 |-- Accumulables: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- Count Failed Values: boolean (nullable = true)
 |    |    |-- ID: long (nullable = true)
 |    |    |-- Internal: boolean (nullable = true)
 |    |    |-- Metadata: string (nullable = true)
 |    |    |-- Name: string (nullable = true)
 |    |    |-- Value: string (nullable = true)
 |-- Completion Time: long (nullable = true)
 |-- Details: string (nullable = true)
 |-- Number of Tasks: long (nullable = true)
 |-- Parent IDs: array (nullable = true)
 |    |-- element: long (containsNull = true)
 |-- RDD Info: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- Callsite: string (nullable = true)
 |    |    |-- Disk Size: long (nullable = true)
 |    |    |-- Memory Size: long (nullable = true)
 |    |    |-- Name: string (nullable = true)
 |    |    |-- Number of Cached Partitions: long (nullable = true)
 |    |    |-- Number of Partitions: long (nullable = true)
 |    |    |-- Parent IDs: array (nullable = true)
 |    |    |    |-- element: long (containsNull = true)
 |    |    |-- RDD ID: long (nullable = true)
 |    |    |-- Scope: string (nullable = true)
 |    |    |-- Storage Level: struct (nullable = true)
 |    |    |    |-- Deserialized: boolean (nullable = true)
 |    |    |    |-- Replication: long (nullable = true)
 |    |    |    |-- Use Disk: boolean (nullable = true)
 |    |    |    |-- Use Memory: boolean (nullable = true)
 |-- Stage Attempt ID: long (nullable = true)
 |-- Stage ID: long (nullable = true)
 |-- Stage Name: string (nullable = true)
 |-- Submission Time: long (nullable = true)


scala> sql("select `Submission Time`,`Completion Time`,`Number of Tasks`,`Stage ID`, t3.col.* from t2 lateral view explode(Accumulables) t3").show
+---------------+---------------+---------------+--------+-------------------+---+--------+--------+--------------------+---------+
|Submission Time|Completion Time|Number of Tasks|Stage ID|Count Failed Values| ID|Internal|Metadata|                Name|    Value|
+---------------+---------------+---------------+--------+-------------------+---+--------+--------+--------------------+---------+
|  1507552351523|  1507552352748|              4|       0|               true| 23|    true|    null|internal.metrics....|     2035|
|  1507552351523|  1507552352748|              4|       0|               true| 26|    true|    null|internal.metrics....|       44|
|  1507552351523|  1507552352748|              4|       0|               true| 20|    true|     sql|number of output ...|     1000|
|  1507552351523|  1507552352748|              4|       0|               true| 22|    true|    null|internal.metrics....|717091615|
|  1507552351523|  1507552352748|              4|       0|               true| 25|    true|    null|internal.metrics....|     6368|
|  1507552351523|  1507552352748|              4|       0|               true| 19|    true|     sql|duration total (m...|       15|


Example analysis using Task metrics:

val df = spark.read.json("/user/spark/applicationHistory/application_1502789566015_25541")
val df2 = df.filter("Event='SparkListenerTaskEnd'").select("Stage ID", "Task Info.*", "Task Metrics.*")


Examples:
scala> df2.printSchema
root
 |-- Stage ID: long (nullable = true)
 |-- Accumulables: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- Count Failed Values: boolean (nullable = true)
 |    |    |-- ID: long (nullable = true)
 |    |    |-- Internal: boolean (nullable = true)
 |    |    |-- Metadata: string (nullable = true)
 |    |    |-- Name: string (nullable = true)
 |    |    |-- Update: string (nullable = true)
 |    |    |-- Value: string (nullable = true)
 |-- Attempt: long (nullable = true)
 |-- Executor ID: string (nullable = true)
 |-- Failed: boolean (nullable = true)
 |-- Finish Time: long (nullable = true)
 |-- Getting Result Time: long (nullable = true)
 |-- Host: string (nullable = true)
 |-- Index: long (nullable = true)
 |-- Killed: boolean (nullable = true)
 |-- Launch Time: long (nullable = true)
 |-- Locality: string (nullable = true)
 |-- Speculative: boolean (nullable = true)
 |-- Task ID: long (nullable = true)
 |-- Disk Bytes Spilled: long (nullable = true)
 |-- Executor CPU Time: long (nullable = true)
 |-- Executor Deserialize CPU Time: long (nullable = true)
 |-- Executor Deserialize Time: long (nullable = true)
 |-- Executor Run Time: long (nullable = true)
 |-- Input Metrics: struct (nullable = true)
 |    |-- Bytes Read: long (nullable = true)
 |    |-- Records Read: long (nullable = true)
 |-- JVM GC Time: long (nullable = true)
 |-- Memory Bytes Spilled: long (nullable = true)
 |-- Output Metrics: struct (nullable = true)
 |    |-- Bytes Written: long (nullable = true)
 |    |-- Records Written: long (nullable = true)
 |-- Result Serialization Time: long (nullable = true)
 |-- Result Size: long (nullable = true)
 |-- Shuffle Read Metrics: struct (nullable = true)
 |    |-- Fetch Wait Time: long (nullable = true)
 |    |-- Local Blocks Fetched: long (nullable = true)
 |    |-- Local Bytes Read: long (nullable = true)
 |    |-- Remote Blocks Fetched: long (nullable = true)
 |    |-- Remote Bytes Read: long (nullable = true)
 |    |-- Total Records Read: long (nullable = true)
 |-- Shuffle Write Metrics: struct (nullable = true)
 |    |-- Shuffle Bytes Written: long (nullable = true)
 |    |-- Shuffle Records Written: long (nullable = true)
 |    |-- Shuffle Write Time: long (nullable = true)
 |-- Updated Blocks: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- Block ID: string (nullable = true)
 |    |    |-- Status: struct (nullable = true)
 |    |    |    |-- Disk Size: long (nullable = true)
 |    |    |    |-- Memory Size: long (nullable = true)
 |    |    |    |-- Storage Level: struct (nullable = true)
 |    |    |    |    |-- Deserialized: boolean (nullable = true)
 |    |    |    |    |-- Replication: long (nullable = true)
 |    |    |    |    |-- Use Disk: boolean (nullable = true)
 |    |    |    |    |-- Use Memory: boolean (nullable = true)


scala> df2.select("Input Metrics.*","Executor CPU Time","Finish Time","Locality").show
+----------+------------+-----------------+-------------+-------------+
|Bytes Read|Records Read|Executor CPU Time|  Finish Time|     Locality|
+----------+------------+-----------------+-------------+-------------+
|         0|         250|        299716929|1507552352226|PROCESS_LOCAL|
|         0|         250|         27238324|1507552352230|PROCESS_LOCAL|
|         0|         250|        470049212|1507552352739|PROCESS_LOCAL|
|         0|         250|        180483811|1507552352744|PROCESS_LOCAL|
|         0|         250|        125423947|1507552353197|PROCESS_LOCAL|
|         0|         250|         97179093|1507552353197|PROCESS_LOCAL|
|         0|         250|        211535781|1507552353505|PROCESS_LOCAL|
|         0|         250|        242424956|1507552353509|PROCESS_LOCAL|
|         0|           0|        100401996|1507552353694|   NODE_LOCAL|
+----------+------------+-----------------+-------------+-------------+
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,496评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,407评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,632评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,180评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,198评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,165评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,052评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,910评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,324评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,542评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,711评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,424评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,017评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,668评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,823评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,722评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,611评论 2 353

推荐阅读更多精彩内容