-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23040][CORE]: Returns interruptible iterator for shuffle reader #20449
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 2 commits
acca0e3
ddeffd8
88e86e0
ba2f355
d6ed9a1
8c15c56
756e0b7
2061d0a
a3d8ad5
28119e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ private[spark] class BlockStoreShuffleReader[K, C]( | |
| } | ||
|
|
||
| // Sort the output if there is a sort ordering defined. | ||
| dep.keyOrdering match { | ||
| val resultIter = dep.keyOrdering match { | ||
| case Some(keyOrd: Ordering[K]) => | ||
| // Create an ExternalSorter to sort the data. | ||
| val sorter = | ||
|
|
@@ -104,9 +104,16 @@ private[spark] class BlockStoreShuffleReader[K, C]( | |
| context.taskMetrics().incMemoryBytesSpilled(sorter.memoryBytesSpilled) | ||
| context.taskMetrics().incDiskBytesSpilled(sorter.diskBytesSpilled) | ||
| context.taskMetrics().incPeakExecutionMemory(sorter.peakMemoryUsedBytes) | ||
| // Use completion callback to stop sorter if task was completed(either finished/cancelled). | ||
| context.addTaskCompletionListener(_ => { | ||
| sorter.stop() | ||
| }) | ||
| CompletionIterator[Product2[K, C], Iterator[Product2[K, C]]](sorter.iterator, sorter.stop()) | ||
| case None => | ||
| aggregatedIter | ||
| } | ||
| // Use another interruptible iterator here to support task cancellation as aggregator or(and) | ||
| // sorter may have consumed previous interruptible iterator. | ||
| new InterruptibleIterator[Product2[K, C]](context, resultIter) | ||
|
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. there is a chance that
Contributor
Author
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. Will do |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,15 +18,14 @@ | |
| package org.apache.spark | ||
|
|
||
| import java.util.concurrent.Semaphore | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| import scala.concurrent.ExecutionContext.Implicits.global | ||
| import scala.concurrent.Future | ||
| import scala.concurrent.duration._ | ||
|
|
||
| import org.scalatest.BeforeAndAfter | ||
| import org.scalatest.Matchers | ||
|
|
||
|
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. this will break the style check |
||
| import org.apache.spark.scheduler.{SparkListener, SparkListenerTaskStart} | ||
| import org.apache.spark.scheduler.{SparkListener, SparkListenerStageCompleted, SparkListenerTaskEnd, SparkListenerTaskStart} | ||
| import org.apache.spark.util.ThreadUtils | ||
|
|
||
| /** | ||
|
|
@@ -320,6 +319,55 @@ class JobCancellationSuite extends SparkFunSuite with Matchers with BeforeAndAft | |
| f2.get() | ||
| } | ||
|
|
||
| test("Interruptible iterator of shuffle reader") { | ||
|
||
| import JobCancellationSuite._ | ||
| val numSlice = 2 | ||
| sc = new SparkContext(s"local[$numSlice]", "test") | ||
|
|
||
| val f = sc.parallelize(1 to 1000, numSlice).map { i => (i, i) } | ||
| .repartitionAndSortWithinPartitions(new HashPartitioner(2)) | ||
| .mapPartitions { iter => | ||
| taskStartedSemaphore.release() | ||
|
||
| iter | ||
| }.foreachAsync { x => | ||
| if ( x._1 >= 10) { // this block of code is partially executed. | ||
|
||
| taskCancelledSemaphore.acquire() | ||
| } | ||
| executionOfInterruptibleCounter.getAndIncrement() | ||
|
||
| } | ||
|
|
||
| val sem = new Semaphore(0) | ||
| val taskCompletedSem = new Semaphore(0) | ||
| Future { | ||
| taskStartedSemaphore.acquire() | ||
| f.cancel() | ||
|
||
| sem.release() | ||
| } | ||
|
|
||
| sc.addSparkListener(new SparkListener { | ||
| override def onStageCompleted(stageCompleted: SparkListenerStageCompleted): Unit = { | ||
| // release taskCancelledSemaphore when cancelTasks event has been posted | ||
| if (stageCompleted.stageInfo.stageId == 1) { | ||
| taskCancelledSemaphore.release(1000) | ||
| } | ||
| } | ||
|
|
||
| override def onTaskEnd(taskEnd: SparkListenerTaskEnd): Unit = { | ||
| if (taskEnd.stageId == 1) { // make sure task ends | ||
| taskCompletedSem.release() | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| sem.acquire() | ||
| val e = intercept[SparkException] { f.get() }.getCause | ||
|
||
| assert(e.getMessage.contains("cancelled") || e.getMessage.contains("killed")) | ||
|
|
||
| taskCompletedSem.acquire(numSlice) | ||
| // 11 as |1..10| + |501|(another partition) | ||
| assert(executionOfInterruptibleCounter.get() <= 11) | ||
|
||
| } | ||
|
|
||
| def testCount() { | ||
| // Cancel before launching any tasks | ||
| { | ||
|
|
@@ -384,4 +432,5 @@ object JobCancellationSuite { | |
| val taskStartedSemaphore = new Semaphore(0) | ||
| val taskCancelledSemaphore = new Semaphore(0) | ||
| val twoJobsSharingStageSemaphore = new Semaphore(0) | ||
| val executionOfInterruptibleCounter = new AtomicInteger(0) | ||
| } | ||
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.
To fit the 100 chars limitation,
oris replaced by/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.
then we can just write
if task was finished/cancelled.