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

TaskContext

TaskContext

TaskContext is the base for contextual information about a task.

You can access the active TaskContext instance using TaskContext.get method.

TaskContext allows for registering task listeners and accessing local properties that were set on the driver.

Note
TaskContext is serializable.

Table 1. (Subset of) TaskContext Contract
Method Description

addTaskCompletionListener

Registers a TaskCompletionListener

Used when…​

addTaskFailureListener

Registers a TaskFailureListener

Used when…​

attemptNumber

Specifies how many times the task has been attempted (starting from 0).

Used when…​

getLocalProperty

Used when…​

Accesses local properties set by the driver using SparkContext.setLocalProperty.

getMetricsSources

Gives all the metrics sources by sourceName which are associated with the instance that runs the task.

isCompleted

Used when…​

isInterrupted

A flag that is enabled when a task was killed.

Used when…​

killTaskIfInterrupted

If the task was marked for interruption, i.e. cancellation, killTaskIfInterrupted (is supposed to) throws a TaskKilledException with the reason for the interrupt (that in turn kills the task).

Used (to break a task execution) when:

  • InterruptibleIterator is requested to hasNext

  • TaskRunner is requested to run

  • SortedIterator and UnsafeSorterSpillReader are requested to loadNext

  • Spark SQL’s FileScanRDD is requested to compute

partitionId

Id of the Partition computed by the task.

Used when…​

registerAccumulator

Used when…​

stageId

Id of the Stage the task belongs to.

Used when…​

taskAttemptId

Id of the attempt of the task.

Used when…​

taskMemoryManager

Used when…​

taskMetrics

TaskMetrics of the active Task.

Used when…​

Note
TaskContextImpl is the one and only known implementation of TaskContext Contract in Apache Spark.

unset Method

Caution
FIXME

setTaskContext Method

Caution
FIXME

Accessing Active TaskContext — get Method

get method returns the TaskContext instance for an active task (as a TaskContextImpl). There can only be one instance and tasks can use the object to access contextual information about themselves.

Note
TaskContext object uses ThreadLocal to keep it thread-local, i.e. to associate state with the thread of a task.

Registering Task Listeners

Using TaskContext object you can register task listeners for task completion regardless of the final state and task failures only.

addTaskCompletionListener Method

addTaskCompletionListener methods register a TaskCompletionListener listener to be executed on task completion.

Note
It will be executed regardless of the final state of a task – success, failure, or cancellation.

addTaskFailureListener Method

addTaskFailureListener methods register a TaskFailureListener listener to be executed on task failure only. It can be executed multiple times since a task can be re-attempted when it fails.

(Unused) Accessing Partition Id — getPartitionId Method

getPartitionId gets the active TaskContext and returns partitionId or 0 (if TaskContext not available).

Note
getPartitionId is not used.
赞(0) 打赏
未经允许不得转载:spark技术分享 » TaskContext
分享到: 更多 (0)

关注公众号:spark技术分享

联系我们联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏