TaskContext (Spark 4.2.0 JavaDoc)

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)
  • withTaskContext

    public static <T> T withTaskContext(TaskContext context, scala.Function0<T> task)

  • 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

    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

    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

    (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.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. For more information see org.apache.spark.metrics.MetricsSystem.

    Parameters:
    sourceName - (undocumented)
    Returns:
    (undocumented)