-
Notifications
You must be signed in to change notification settings - Fork 29k
SPARK-3874, Provide stable TaskContext API #2782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ef633f5
bbd9e05
7ecc2fe
facf3b1
df261d0
ed551ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,21 +39,6 @@ | |
| @DeveloperApi | ||
| public abstract class TaskContext implements Serializable { | ||
|
|
||
| private int stageId; | ||
| private int partitionId; | ||
| private long attemptId; | ||
| private boolean runningLocally; | ||
| private TaskMetrics taskMetrics; | ||
|
|
||
| TaskContext(int stageId, int partitionId, long attemptId, boolean runningLocally, | ||
| TaskMetrics taskMetrics) { | ||
| this.attemptId = attemptId; | ||
| this.partitionId = partitionId; | ||
| this.runningLocally = runningLocally; | ||
| this.stageId = stageId; | ||
| this.taskMetrics = taskMetrics; | ||
| } | ||
|
|
||
| private static ThreadLocal<TaskContext> taskContext = | ||
| new ThreadLocal<TaskContext>(); | ||
|
|
||
|
|
@@ -74,132 +59,56 @@ static void unset() { | |
| taskContext.remove(); | ||
| } | ||
|
|
||
| // List of callback functions to execute when the task completes. | ||
| private transient List<TaskCompletionListener> onCompleteCallbacks = | ||
| new ArrayList<TaskCompletionListener>(); | ||
|
|
||
| // Whether the corresponding task has been killed. | ||
| private volatile boolean interrupted = false; | ||
|
|
||
| // Whether the task has completed. | ||
| private volatile boolean completed = false; | ||
|
|
||
| /** | ||
| * Checks whether the task has completed. | ||
| */ | ||
| public boolean isCompleted() { | ||
| return completed; | ||
| } | ||
| public abstract boolean isCompleted(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. //cc @rxin - should this be "isComplete" rather than "isCompleted"?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually this looks good - we use isCompleted elsewhere already in user-facing code.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that isCompleted means it has been completed, whereas isComplete means it is "whole".
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think isCompleted is right, since we'd use isFailed instead of isFailure or isFail. |
||
|
|
||
| /** | ||
| * Checks whether the task has been killed. | ||
| */ | ||
| public boolean isInterrupted() { | ||
| return interrupted; | ||
| } | ||
| public abstract boolean isInterrupted(); | ||
|
|
||
| /** | ||
| * Add a (Java friendly) listener to be executed on task completion. | ||
| * This will be called in all situation - success, failure, or cancellation. | ||
| * <p/> | ||
| * An example use is for HadoopRDD to register a callback to close the input stream. | ||
| */ | ||
| public TaskContext addTaskCompletionListener(TaskCompletionListener listener) { | ||
| onCompleteCallbacks.add(listener); | ||
| return this; | ||
| } | ||
| public abstract TaskContext addTaskCompletionListener(TaskCompletionListener listener); | ||
|
|
||
| /** | ||
| * Add 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. | ||
| * <p/> | ||
| * An example use is for HadoopRDD to register a callback to close the input stream. | ||
| */ | ||
| public TaskContext addTaskCompletionListener(final Function1<TaskContext, Unit> f) { | ||
| onCompleteCallbacks.add(new TaskCompletionListener() { | ||
| @Override | ||
| public void onTaskCompletion(TaskContext context) { | ||
| f.apply(context); | ||
| } | ||
| }); | ||
| return this; | ||
| } | ||
| public abstract TaskContext addTaskCompletionListener(final Function1<TaskContext, Unit> f); | ||
|
|
||
| /** | ||
| * Add a callback function to be executed on task completion. An example use | ||
| * is for HadoopRDD to register a callback to close the input stream. | ||
| * Will be called in any situation - success, failure, or cancellation. | ||
| * | ||
| * Deprecated: use addTaskCompletionListener | ||
| * | ||
| * | ||
| * @param f Callback function. | ||
| */ | ||
| @Deprecated | ||
| public void addOnCompleteCallback(final Function0<Unit> f) { | ||
| onCompleteCallbacks.add(new TaskCompletionListener() { | ||
| @Override | ||
| public void onTaskCompletion(TaskContext context) { | ||
| f.apply(); | ||
| } | ||
| }); | ||
| } | ||
| public abstract void addOnCompleteCallback(final Function0<Unit> f); | ||
|
|
||
| /** | ||
| * ::Internal API:: | ||
| * Marks the task as completed and triggers the listeners. | ||
| */ | ||
| public void markTaskCompleted() throws TaskCompletionListenerException { | ||
| completed = true; | ||
| List<String> errorMsgs = new ArrayList<String>(2); | ||
| // Process complete callbacks in the reverse order of registration | ||
| List<TaskCompletionListener> revlist = | ||
| new ArrayList<TaskCompletionListener>(onCompleteCallbacks); | ||
| Collections.reverse(revlist); | ||
| for (TaskCompletionListener tcl: revlist) { | ||
| try { | ||
| tcl.onTaskCompletion(this); | ||
| } catch (Throwable e) { | ||
| errorMsgs.add(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| if (!errorMsgs.isEmpty()) { | ||
| throw new TaskCompletionListenerException(JavaConversions.asScalaBuffer(errorMsgs)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ::Internal API:: | ||
| * Marks the task for interruption, i.e. cancellation. | ||
| */ | ||
| public void markInterrupted() { | ||
| interrupted = true; | ||
| } | ||
|
|
||
| public int stageId() { | ||
| return stageId; | ||
| } | ||
| public abstract int stageId(); | ||
|
|
||
| public int partitionId() { | ||
| return partitionId; | ||
| } | ||
| public abstract int partitionId(); | ||
|
|
||
| public long attemptId() { | ||
| return attemptId; | ||
| } | ||
| public abstract long attemptId(); | ||
|
|
||
| @Deprecated | ||
| /** Deprecated: use isRunningLocally() */ | ||
| public boolean runningLocally() { | ||
| return runningLocally; | ||
| } | ||
| public abstract boolean runningLocally(); | ||
|
|
||
| public boolean isRunningLocally() { | ||
| return runningLocally; | ||
| } | ||
| public abstract boolean isRunningLocally(); | ||
|
|
||
| /** ::Internal API:: */ | ||
| public TaskMetrics taskMetrics() { | ||
| return taskMetrics; | ||
| } | ||
| public abstract TaskMetrics taskMetrics(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer this class not have a constructor (the whole idea here is that we don't want user to instantiate it). Basically we want to move all of the logic here to
TaskContextImpland only define the interface here in terms of abstract methods.