关注 spark技术分享,
撸spark源码 玩spark最佳实践

SQLListener Spark Listener

SQLListener Spark Listener

SQLListener is a custom SparkListener that collects information about SQL query executions for web UI (to display in SQL tab). It relies on spark.sql.execution.id key to distinguish between queries.

Internally, it uses SQLExecutionUIData data structure exclusively to record all the necessary data for a single SQL query execution. SQLExecutionUIData is tracked in the internal registries, i.e. activeExecutions, failedExecutions, and completedExecutions as well as lookup tables, i.e. _executionIdToData, _jobIdToExecutionId, and _stageIdToStageMetrics.

SQLListener starts recording a query execution by intercepting a SparkListenerSQLExecutionStart event (using onOtherEvent callback).

SQLListener stops recording information about a SQL query execution when SparkListenerSQLExecutionEnd event arrives.

It defines the other callbacks (from SparkListener interface):

Registering Job and Stages under Active Execution — onJobStart Callback

onJobStart reads the spark.sql.execution.id key, the identifiers of the job and the stages and then updates the SQLExecutionUIData for the execution id in activeExecutions internal registry.

Note
When onJobStart is executed, it is assumed that SQLExecutionUIData has already been created and available in the internal activeExecutions registry.

The job in SQLExecutionUIData is marked as running with the stages added (to stages). For each stage, a SQLStageMetrics is created in the internal _stageIdToStageMetrics registry. At the end, the execution id is recorded for the job id in the internal _jobIdToExecutionId.

onOtherEvent Callback

In onOtherEvent, SQLListener listens to the following SparkListenerEvent events:

Registering Active Execution — SparkListenerSQLExecutionStart Event

SparkListenerSQLExecutionStart events starts recording information about the executionId SQL query execution.

When a SparkListenerSQLExecutionStart event arrives, a new SQLExecutionUIData for the executionId query execution is created and stored in activeExecutions internal registry. It is also stored in _executionIdToData lookup table.

SparkListenerSQLExecutionEnd Event

SparkListenerSQLExecutionEnd event stops recording information about the executionId SQL query execution (tracked as SQLExecutionUIData). SQLListener saves the input time as completionTime.

If there are no other running jobs (registered in SQLExecutionUIData), the query execution is removed from the activeExecutions internal registry and moved to either completedExecutions or failedExecutions registry.

This is when SQLListener checks the number of SQLExecutionUIData entires in either registry — failedExecutions or completedExecutions — and removes the excess of the old entries beyond spark.sql.ui.retainedExecutions Spark property.

SparkListenerDriverAccumUpdates Event

When SparkListenerDriverAccumUpdates comes, SQLExecutionUIData for the input executionId is looked up (in _executionIdToData) and SQLExecutionUIData.driverAccumUpdates is updated with the input accumUpdates.

onJobEnd Callback

When called, onJobEnd retrieves the SQLExecutionUIData for the job and records it either successful or failed depending on the job result.

If it is the last job of the query execution (tracked as SQLExecutionUIData), the execution is removed from activeExecutions internal registry and moved to either

If the query execution has already been marked as completed (using completionTime) and there are no other running jobs (registered in SQLExecutionUIData), the query execution is removed from the activeExecutions internal registry and moved to either completedExecutions or failedExecutions registry.

This is when SQLListener checks the number of SQLExecutionUIData entires in either registry — failedExecutions or completedExecutions — and removes the excess of the old entries beyond spark.sql.ui.retainedExecutions Spark property.

Getting SQL Execution Data — getExecution Method

Getting Execution Metrics — getExecutionMetrics Method

getExecutionMetrics gets the metrics (aka accumulator updates) for executionId (by which it collects all the tasks that were used for an execution).

It is exclusively used to render the ExecutionPage page in web UI.

mergeAccumulatorUpdates Method

mergeAccumulatorUpdates is a private helper method for…​TK

It is used exclusively in getExecutionMetrics method.

SQLExecutionUIData

SQLExecutionUIData is the data abstraction of SQLListener to describe SQL query executions. It is a container for jobs, stages, and accumulator updates for a single query execution.

赞(0) 打赏
未经允许不得转载:spark技术分享 » SQLListener Spark Listener
分享到: 更多 (0)

关注公众号:spark技术分享

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏