Skip to content

[SPARK-25837] [Core] Fix potential slowdown in AppStatusListener when cleaning up stages #22883

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1073,16 +1073,6 @@ private[spark] class AppStatusListener(
kvstore.delete(e.getClass(), e.id)
}

val tasks = kvstore.view(classOf[TaskDataWrapper])
.index("stage")
.first(key)
.last(key)
.asScala

tasks.foreach { t =>
kvstore.delete(t.getClass(), t.taskId)
}

// Check whether there are remaining attempts for the same stage. If there aren't, then
// also delete the RDD graph data.
val remainingAttempts = kvstore.view(classOf[StageDataWrapper])
Expand All @@ -1105,6 +1095,15 @@ private[spark] class AppStatusListener(

cleanupCachedQuantiles(key)
}

// Delete tasks for all stages in one pass, as deleting them for each stage individually is slow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @patrickbrownsync , can you explain more about why deleting them in the loop above is slower? I don't quite understand it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Take a look at the implementation of InMemoryView at spark/common/kvstore/src/main/java/org/apache/spark/util/kvstore/InMemoryStore.java line 179

specifically the implementation of iterator on line 193, here is an excerpt:

Collections.sort(sorted, (e1, e2) -> modifier * compare(e1, e2, getter));
Stream<T> stream = sorted.stream();

if (first != null) {
    stream = stream.filter(e -> modifier * compare(e, getter, first) >= 0);
}

if (last != null) {
    stream = stream.filter(e -> modifier * compare(e, getter, last) <= 0);
}

and the original, in loop deletion code:

val tasks = kvstore.view(classOf[TaskDataWrapper])	
        .index("stage")	
        .first(key)	
        .last(key)	
        .asScala	
      
tasks.foreach { t =>	
    kvstore.delete(t.getClass(), t.taskId)	
}

So you can see, if we do this each loop we actually sort the whole collection of TaskDataWrapper which are currently in the store, then go through and check each item based on the key set (the stage). Assuming we have a large number of stages and tasks this is an O(n^2) operation, which is what happens on my production application and the repro code.

If we do this in one pass for all stages, we only sort and iterate the list of tasks one time.

This same pattern happens fairly frequently using the KVStoreView interface and InMemoryView implementation. Since I am new to contributing to Spark I did not undertake a massive refactor, but I would suggest that this interface and implementation should be looked at and re-designed with efficiency in mind. The current implementation favors flexibility in terms of how the dataset is sorted and filtered, but enforcing a single sort order via something like a SortedSet would hopefully make it clear when the operation being performed was efficiently searching inside the collection, and when you were using an inefficient access pattern.

I hope that explains the reasoning, if you have any more questions let me know.

val tasks = kvstore.view(classOf[TaskDataWrapper]).asScala
val keys = stages.map { s => (s.info.stageId, s.info.attemptId) }.toSet
tasks.foreach { t =>
if (keys.contains((t.stageId, t.stageAttemptId))) {
kvstore.delete(t.getClass(), t.taskId)
}
}
}

private def cleanupTasks(stage: LiveStage): Unit = {
Expand Down