Class TaskContext

Object
org.apache.spark.TaskContext
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
BarrierTaskContext

public abstract class TaskContext extends Object implements Serializable
Contextual information about a task which can be read or mutated during execution. To access the TaskContext for a running task, use:

   org.apache.spark.TaskContext.get()
 
See Also:
  • Constructor Details

    • TaskContext

      public TaskContext()
  • Method Details

    • get

      public static TaskContext 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

      public abstract TaskContext addTaskCompletionListener(TaskCompletionListener listener)
      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

      public <U> TaskContext addTaskCompletionListener(scala.Function1<TaskContext,U> f)
      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

      public 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). 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

      public abstract String getLocalProperty(String key)
      Get a local property set upstream in the driver, or null if it is missing. See also org.apache.spark.SparkContext.setLocalProperty.
      Parameters:
      key - (undocumented)
      Returns:
      (undocumented)
    • cpus

      public abstract int cpus()
      CPUs allocated to the task.
      Returns:
      (undocumented)
    • resources

      public abstract scala.collection.immutable.Map<String,ResourceInformation> resources()
      Resources allocated to the task. The key is the resource name and the value is information about the resource. Please refer to ResourceInformation for specifics.
      Returns:
      (undocumented)
    • resourcesJMap

      public abstract Map<String,ResourceInformation> resourcesJMap()
      (java-specific) Resources allocated to the task. The key is the resource name and the value is information about the resource. Please refer to ResourceInformation for specifics.
      Returns:
      (undocumented)
    • taskMetrics

      public abstract org.apache.spark.executor.TaskMetrics taskMetrics()
    • getMetricsSources

      public abstract scala.collection.Seq<Source> getMetricsSources(String sourceName)
      ::DeveloperApi:: Returns all metrics sources with the given name which are associated with the instance which runs the task. For more information see org.apache.spark.metrics.MetricsSystem.
      Parameters:
      sourceName - (undocumented)
      Returns:
      (undocumented)