rekbun commented on code in PR #60256:
URL: https://github.com/apache/spark/pull/60256#discussion_r4601277430
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -863,30 +865,33 @@ private[sql] object GroupPartitionsExec {
*
* Unlike Spark's default coalescer which tries to minimize data movement,
this coalescer
* groups partitions based on their partition keys to maintain the grouping
semantics
- * required for storage-partitioned operations.
+ * required for storage-partitioned operations. Each group prefers the host
that most of its
+ * partitions prefer.
*
* @param groupedPartitions Sequence where each element is a sequence of input
partition
- * indices that should be grouped together
+ * indices that should be grouped together. Only the
driver reads it,
+ * so it is not serialized into tasks.
*/
class GroupedPartitionCoalescer(
- val groupedPartitions: Seq[Seq[Int]]
+ @transient val groupedPartitions: Seq[Seq[Int]]
) extends PartitionCoalescer with Serializable {
override def coalesce(maxPartitions: Int, parent: RDD[_]):
Array[PartitionGroup] = {
+ require(groupedPartitions.size == maxPartitions,
s"GroupedPartitionCoalescer has " +
+ s"${groupedPartitions.size} groups but was asked for $maxPartitions
partitions")
+ val assigned = mutable.Map.empty[String, Int].withDefaultValue(0)
groupedPartitions.map { partitionIndices =>
- val partitions = new ArrayBuffer[Partition](partitionIndices.size)
- val preferredLocations = new ArrayBuffer[String](partitionIndices.size)
- partitionIndices.foreach { partitionIndex =>
- val partition = parent.partitions(partitionIndex)
- partitions += partition
- preferredLocations ++= parent.preferredLocations(partition)
- }
- // Select the most common location as the preferred location
- val preferredLocation = preferredLocations
- .groupBy(identity)
- .view.mapValues(_.size)
- .maxByOption(_._2)
+ val partitions = partitionIndices.map(parent.partitions(_))
+ // Prefer the host that the largest number of the group's partitions
prefer, and spread ties
+ // across groups rather than sending them all to one host. As in
`DefaultPartitionCoalescer`,
+ // a partition's hosts are the ones the scheduler would use for it,
found through the
+ // parent's lineage, since a parent such as a scan's mapped RDD reports
none of its own.
+ val preferredLocation = partitionIndices
Review Comment:
Thanks for measuring this. Done in 5053efb, as you proposed, with one
addition: `persistedLineage` keeps the ids of the RDDs it has visited, so a
lineage that reaches an RDD by more than one path walks it once.
Your four rows now give master's result:
- The three tests that pinned a host for a group of one now check
`sc.getPreferredLocs` and expect every location of the input.
- A new test computes the grouped partitions before a parent's cache exists,
then caches it. After the next job is submitted, the scheduler offers the
executor that holds the block.
- Another new test covers executor and HDFS-cache locations. For a group of
one, the scheduler offers them unchanged. A group of two on `executor_h9_7`
keeps that location, and one with an `executor_h9_7` input and an `h9` input
gets `h9`.
The new tests fail at 4d1dbdf. I also added the zip trade-off to the
description.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -863,30 +865,33 @@ private[sql] object GroupPartitionsExec {
*
* Unlike Spark's default coalescer which tries to minimize data movement,
this coalescer
* groups partitions based on their partition keys to maintain the grouping
semantics
- * required for storage-partitioned operations.
+ * required for storage-partitioned operations. Each group prefers the host
that most of its
+ * partitions prefer.
*
* @param groupedPartitions Sequence where each element is a sequence of input
partition
- * indices that should be grouped together
+ * indices that should be grouped together. Only the
driver reads it,
+ * so it is not serialized into tasks.
*/
class GroupedPartitionCoalescer(
- val groupedPartitions: Seq[Seq[Int]]
+ @transient val groupedPartitions: Seq[Seq[Int]]
) extends PartitionCoalescer with Serializable {
override def coalesce(maxPartitions: Int, parent: RDD[_]):
Array[PartitionGroup] = {
+ require(groupedPartitions.size == maxPartitions,
s"GroupedPartitionCoalescer has " +
+ s"${groupedPartitions.size} groups but was asked for $maxPartitions
partitions")
+ val assigned = mutable.Map.empty[String, Int].withDefaultValue(0)
groupedPartitions.map { partitionIndices =>
- val partitions = new ArrayBuffer[Partition](partitionIndices.size)
- val preferredLocations = new ArrayBuffer[String](partitionIndices.size)
- partitionIndices.foreach { partitionIndex =>
- val partition = parent.partitions(partitionIndex)
- partitions += partition
- preferredLocations ++= parent.preferredLocations(partition)
- }
- // Select the most common location as the preferred location
- val preferredLocation = preferredLocations
- .groupBy(identity)
- .view.mapValues(_.size)
- .maxByOption(_._2)
+ val partitions = partitionIndices.map(parent.partitions(_))
+ // Prefer the host that the largest number of the group's partitions
prefer, and spread ties
+ // across groups rather than sending them all to one host. As in
`DefaultPartitionCoalescer`,
+ // a partition's hosts are the ones the scheduler would use for it,
found through the
+ // parent's lineage, since a parent such as a scan's mapped RDD reports
none of its own.
+ val preferredLocation = partitionIndices
+ .flatMap(parent.context.getPreferredLocs(parent,
_).map(_.host).distinct)
Review Comment:
Done in 5053efb, as you suggested. A new test has three groups that repeat
the same two inputs and checks that each input's locations are looked up once.
Before the change it counted six lookups.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]