Class TaskContext
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
BarrierTaskContext
org.apache.spark.TaskContext.get()
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract TaskContext
Adds a (Java friendly) listener to be executed on task completion.<U> TaskContext
addTaskCompletionListener
(scala.Function1<TaskContext, U> f) Adds a listener in the form of a Scala closure to be executed on task completion.abstract TaskContext
addTaskFailureListener
(TaskFailureListener listener) Adds a listener to be executed on task failure (which includes completion listener failure, if the task body did not already fail).addTaskFailureListener
(scala.Function2<TaskContext, Throwable, scala.runtime.BoxedUnit> f) Adds a listener to be executed on task failure (which includes completion listener failure, if the task body did not already fail).abstract int
How many times this task has been attempted.abstract int
cpus()
CPUs allocated to the task.static TaskContext
get()
Return the currently active TaskContext.abstract String
getLocalProperty
(String key) Get a local property set upstream in the driver, or null if it is missing.abstract scala.collection.immutable.Seq<Source>
getMetricsSources
(String sourceName) ::DeveloperApi:: Returns all metrics sources with the given name which are associated with the instance which runs the task.static int
Returns the partition id of currently active TaskContext.abstract boolean
Returns true if the task has completed.abstract boolean
isFailed()
Returns true if the task has failed.abstract boolean
Returns true if the task has been killed.abstract int
Total number of partitions in the stage that this task belongs to.abstract int
The ID of the RDD partition that is computed by this task.abstract scala.collection.immutable.Map<String,
ResourceInformation> Resources allocated to the task.abstract Map<String,
ResourceInformation> (java-specific) Resources allocated to the task.abstract int
How many times the stage that this task belongs to has been attempted.abstract int
stageId()
The ID of the stage that this task belong to.abstract long
An ID that is unique to this task attempt (within the same SparkContext, no two task attempts will share the same attempt ID).abstract org.apache.spark.executor.TaskMetrics
-
Constructor Details
-
TaskContext
public TaskContext()
-
-
Method Details
-
get
Return the currently active TaskContext. This can be called inside of user functions to access contextual information about running tasks.- Returns:
- (undocumented)
-
getPartitionId
public static int getPartitionId()Returns the partition id of currently active TaskContext. It will return 0 if there is no active TaskContext for cases like local execution.- Returns:
- (undocumented)
-
isCompleted
public abstract boolean isCompleted()Returns true if the task has completed.- Returns:
- (undocumented)
-
isFailed
public abstract boolean isFailed()Returns true if the task has failed.- Returns:
- (undocumented)
-
isInterrupted
public abstract boolean isInterrupted()Returns true if the task has been killed.- Returns:
- (undocumented)
-
addTaskCompletionListener
Adds a (Java friendly) listener to be executed on task completion. This will be called in all situations - success, failure, or cancellation. Adding a listener to an already completed task will result in that listener being called immediately.Two listeners registered in the same thread will be invoked in reverse order of registration if the task completes after both are registered. There are no ordering guarantees for listeners registered in different threads, or for listeners registered after the task completes. Listeners are guaranteed to execute sequentially.
An example use is for HadoopRDD to register a callback to close the input stream.
Exceptions thrown by the listener will result in failure of the task.
- Parameters:
listener
- (undocumented)- Returns:
- (undocumented)
-
addTaskCompletionListener
Adds a listener in the form of a Scala closure to be executed on task completion. This will be called in all situations - success, failure, or cancellation. Adding a listener to an already completed task will result in that listener being called immediately.An example use is for HadoopRDD to register a callback to close the input stream.
Exceptions thrown by the listener will result in failure of the task.
- Parameters:
f
- (undocumented)- Returns:
- (undocumented)
-
addTaskFailureListener
Adds a listener to be executed on task failure (which includes completion listener failure, if the task body did not already fail). Adding a listener to an already failed task will result in that listener being called immediately.Note: Prior to Spark 3.4.0, failure listeners were only invoked if the main task body failed.
- Parameters:
listener
- (undocumented)- Returns:
- (undocumented)
-
addTaskFailureListener
public TaskContext addTaskFailureListener(scala.Function2<TaskContext, Throwable, scala.runtime.BoxedUnit> f) Adds a listener to be executed on task failure (which includes completion listener failure, if the task body did not already fail). Adding a listener to an already failed task will result in that listener being called immediately.Note: Prior to Spark 3.4.0, failure listeners were only invoked if the main task body failed.
- Parameters:
f
- (undocumented)- Returns:
- (undocumented)
-
stageId
public abstract int stageId()The ID of the stage that this task belong to.- Returns:
- (undocumented)
-
stageAttemptNumber
public abstract int stageAttemptNumber()How many times the stage that this task belongs to has been attempted. The first stage attempt will be assigned stageAttemptNumber = 0, and subsequent attempts will have increasing attempt numbers.- Returns:
- (undocumented)
-
partitionId
public abstract int partitionId()The ID of the RDD partition that is computed by this task.- Returns:
- (undocumented)
-
numPartitions
public abstract int numPartitions()Total number of partitions in the stage that this task belongs to.- Returns:
- (undocumented)
-
attemptNumber
public abstract int attemptNumber()How many times this task has been attempted. The first task attempt will be assigned attemptNumber = 0, and subsequent attempts will have increasing attempt numbers.- Returns:
- (undocumented)
-
taskAttemptId
public abstract long taskAttemptId()An ID that is unique to this task attempt (within the same SparkContext, no two task attempts will share the same attempt ID). This is roughly equivalent to Hadoop's TaskAttemptID.- Returns:
- (undocumented)
-
getLocalProperty
Get a local property set upstream in the driver, or null if it is missing. See alsoorg.apache.spark.SparkContext.setLocalProperty
.- Parameters:
key
- (undocumented)- Returns:
- (undocumented)
-
cpus
public abstract int cpus()CPUs allocated to the task.- Returns:
- (undocumented)
-
resources
Resources allocated to the task. The key is the resource name and the value is information about the resource. Please refer toResourceInformation
for specifics.- Returns:
- (undocumented)
-
resourcesJMap
(java-specific) Resources allocated to the task. The key is the resource name and the value is information about the resource. Please refer toResourceInformation
for specifics.- Returns:
- (undocumented)
-
taskMetrics
public abstract org.apache.spark.executor.TaskMetrics taskMetrics() -
getMetricsSources
::DeveloperApi:: Returns all metrics sources with the given name which are associated with the instance which runs the task. For more information seeorg.apache.spark.metrics.MetricsSystem
.- Parameters:
sourceName
- (undocumented)- Returns:
- (undocumented)
-