-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20638][Core]Optimize the CartesianRDD to reduce repeatedly data fetching #17936
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
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1802ff0
optimize rdd cartesian with caching the block in local
ConeyLiu 08e25c9
add test case
ConeyLiu 0f812d9
put block into local with read block
ConeyLiu 08c1849
follow the code style and add some comments
ConeyLiu 89a22ef
release the hold lock
ConeyLiu 397dd90
Merge branch 'master' into cartesian
ConeyLiu 697ba33
address comments
ConeyLiu c8222f4
address the unit test error
ConeyLiu f29a9dc
address comments
ConeyLiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,8 @@ import java.io.{IOException, ObjectOutputStream} | |
| import scala.reflect.ClassTag | ||
|
|
||
| import org.apache.spark._ | ||
| import org.apache.spark.util.Utils | ||
| import org.apache.spark.storage.{RDDBlockId, StorageLevel} | ||
| import org.apache.spark.util.{CompletionIterator, Utils} | ||
|
|
||
| private[spark] | ||
| class CartesianPartition( | ||
|
|
@@ -71,9 +72,85 @@ class CartesianRDD[T: ClassTag, U: ClassTag]( | |
| } | ||
|
|
||
| override def compute(split: Partition, context: TaskContext): Iterator[(T, U)] = { | ||
| val blockManager = SparkEnv.get.blockManager | ||
| val currSplit = split.asInstanceOf[CartesianPartition] | ||
| for (x <- rdd1.iterator(currSplit.s1, context); | ||
| y <- rdd2.iterator(currSplit.s2, context)) yield (x, y) | ||
| val blockId2 = RDDBlockId(rdd2.id, currSplit.s2.index) | ||
| var cachedInLocal = false | ||
| var holdReadLock = false | ||
|
|
||
| // Try to get data from the local, otherwise it will be cached to the local. | ||
| def getOrElseCache( | ||
| rdd: RDD[U], | ||
| partition: Partition, | ||
| context: TaskContext, | ||
| level: StorageLevel): Iterator[U] = { | ||
| getLocalValues() match { | ||
| case Some(result) => | ||
| return result | ||
| case None => // do nothing | ||
| } | ||
|
|
||
| val iterator = rdd.iterator(partition, context) | ||
| // Keep read lock, because next we need read it. And don't tell master. | ||
| blockManager.putIterator[U](blockId2, iterator, level, false, true) match { | ||
| case true => | ||
|
||
| cachedInLocal = true | ||
| // After we cached the block, we also hold the block read lock until this task finished. | ||
| holdReadLock = true | ||
| case false => | ||
| // There shouldn't a error caused by put in memory, because we use MEMORY_AND_DISK to | ||
| // cache it. | ||
| throw new SparkException(s"Cache block $blockId2 in local failed even though it's $level") | ||
| } | ||
|
|
||
| logInfo(s"Cache the block $blockId2 to local successful.") | ||
| getLocalValues() match { | ||
| // We don't need release the read lock, it will release after the iterator completion. | ||
| case Some(result) => result | ||
| case None => | ||
| throw new SparkException(s"Block $blockId2 was not found even though it's read-locked") | ||
| } | ||
| } | ||
|
|
||
| // Get block from local, and update the metrics. | ||
| def getLocalValues(): Option[Iterator[U]] = { | ||
| blockManager.getLocalValues(blockId2) match { | ||
| case Some(result) => | ||
| val existingMetrics = context.taskMetrics().inputMetrics | ||
| existingMetrics.incBytesRead(result.bytes) | ||
| val localIter = | ||
| new InterruptibleIterator[U](context, result.data.asInstanceOf[Iterator[U]]) { | ||
| override def next(): U = { | ||
| existingMetrics.incRecordsRead(1) | ||
| delegate.next() | ||
| } | ||
| } | ||
| Some(localIter) | ||
| case None => | ||
| None | ||
| } | ||
| } | ||
|
|
||
| def removeCachedBlock(): Unit = { | ||
| val blockManager = SparkEnv.get.blockManager | ||
| if (holdReadLock) { | ||
| // If hold the read lock, we need release it. | ||
| blockManager.releaseLock(blockId2) | ||
| } | ||
| // Whether the block it persisted by the user. | ||
| val persistedInLocal = | ||
| blockManager.master.getLocations(blockId2).contains(blockManager.blockManagerId) | ||
| if (!persistedInLocal && (cachedInLocal || blockManager.isRemovable(blockId2))) { | ||
| blockManager.removeOrMarkAsRemovable(blockId2, false) | ||
| } | ||
| } | ||
|
|
||
| val resultIter = | ||
| for (x <- rdd1.iterator(currSplit.s1, context); | ||
| y <- getOrElseCache(rdd2, currSplit.s2, context, StorageLevel.MEMORY_AND_DISK)) | ||
| yield (x, y) | ||
|
|
||
| CompletionIterator[(T, U), Iterator[(T, U)]](resultIter, removeCachedBlock()) | ||
| } | ||
|
|
||
| override def getDependencies: Seq[Dependency[_]] = List( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Btw, can we move those functions out of
compute? Too many nested functions here and makingcomputetoo big.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.
Ok, I will change it too.