peter-toth commented on code in PR #60256:
URL: https://github.com/apache/spark/pull/60256#discussion_r4612751072
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -863,35 +865,66 @@ 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. A group names a location only
when it has several
+ * partitions, shares none of them with another group, is not over persisted
data, and more of its
+ * partitions prefer one host than any other host. Every other group leaves
its location to the
+ * scheduler.
*
* @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")
+ // Groups that share partitions, as the slots of a key under
`REPLICATED_FOR_JOIN` do, would all
+ // name the same host when one is ahead, and a zip copies it into every
task of the key. Left to
+ // the scheduler, each task gets the locations of the zip's first side
that has any, as on
+ // master: its own split's when the split side comes first.
+ lazy val shared =
+ groupedPartitions.flatten.groupMapReduce(identity)(_ => 1)(_ +
_).filter(_._2 > 1).keySet
+ // The scheduler looks cached blocks up when it submits each stage, while
a group gets its
+ // location once, when the partitions are computed.
+ lazy val persisted =
+ (parent +: parent.getNarrowAncestors).exists(_.getStorageLevel !=
StorageLevel.NONE)
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)
- .map(_._1)
- val partitionGroup = new PartitionGroup(preferredLocation)
- partitionGroup.partitions ++= partitions
+ // A group of one leaves its location to the scheduler, which then
offers every location of
+ // its partition. The checks above run only for groups of several
partitions.
+ val votes = partitionIndices.size > 1 &&
!partitionIndices.exists(shared) && !persisted
+ val partitionGroup =
+ new PartitionGroup(if (votes) preferredLocation(parent,
partitionIndices) else None)
+ partitionGroup.partitions ++= partitionIndices.map(parent.partitions(_))
Review Comment:
**Finding 12.** The vote now runs before the group's partitions are read. So
for the first group that votes, `getPreferredLocs(parent, i)` reaches
`DAGScheduler.getCacheLocs(parent)` before anything has computed
`parent.partitions`:
- `submitJob`'s eager pass computes an RDD's partitions before it visits
that RDD's dependencies (`DAGScheduler.scala:1700-1708`), so `coalesce` runs
before the parent is visited.
- `CoalescedRDD`'s constructor skips its `prev.partitions` check when
`maxPartitions > 0`.
`getCacheLocs` then computes the partitions of the parent's narrow ancestors
while it holds `cacheLocs`. The comment on `cacheLocs`
(`DAGScheduler.scala:299-301`) asks for an RDD's lock first and the map's
second, to avoid deadlocks. At `5053efb` each group read `parent.partitions(_)`
before it voted. `DefaultPartitionCoalescer` also reads `prev.partitions`
before its lookups (`CoalescedRDD.scala:198`).
What I ran: a parent RDD whose `getPartitions` records `Thread.holdsLock` on
the scheduler's `cacheLocs`, under a map, coalesced as one group of four. At
this head its partitions are computed under the lock. With the change below
they are not, and `GroupPartitionsExecSuite` passes 44 of 44. I did not
reproduce a deadlock, which needs a second thread on one of those ancestors at
the same moment.
```suggestion
val partitions = partitionIndices.map(parent.partitions(_))
// A group of one leaves its location to the scheduler, which then
offers every location of
// its partition. The checks above run only for groups of several
partitions.
val votes = partitionIndices.size > 1 &&
!partitionIndices.exists(shared) && !persisted
val partitionGroup =
new PartitionGroup(if (votes) preferredLocation(parent,
partitionIndices) else None)
partitionGroup.partitions ++= partitions
```
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -863,35 +865,66 @@ 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. A group names a location only
when it has several
+ * partitions, shares none of them with another group, is not over persisted
data, and more of its
+ * partitions prefer one host than any other host. Every other group leaves
its location to the
+ * scheduler.
*
* @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")
+ // Groups that share partitions, as the slots of a key under
`REPLICATED_FOR_JOIN` do, would all
+ // name the same host when one is ahead, and a zip copies it into every
task of the key. Left to
+ // the scheduler, each task gets the locations of the zip's first side
that has any, as on
+ // master: its own split's when the split side comes first.
+ lazy val shared =
+ groupedPartitions.flatten.groupMapReduce(identity)(_ => 1)(_ +
_).filter(_._2 > 1).keySet
+ // The scheduler looks cached blocks up when it submits each stage, while
a group gets its
+ // location once, when the partitions are computed.
+ lazy val persisted =
+ (parent +: parent.getNarrowAncestors).exists(_.getStorageLevel !=
StorageLevel.NONE)
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)
- .map(_._1)
- val partitionGroup = new PartitionGroup(preferredLocation)
- partitionGroup.partitions ++= partitions
+ // A group of one leaves its location to the scheduler, which then
offers every location of
+ // its partition. The checks above run only for groups of several
partitions.
+ val votes = partitionIndices.size > 1 &&
!partitionIndices.exists(shared) && !persisted
+ val partitionGroup =
+ new PartitionGroup(if (votes) preferredLocation(parent,
partitionIndices) else None)
+ partitionGroup.partitions ++= partitionIndices.map(parent.partitions(_))
partitionGroup
}.toArray
}
+
+ /**
+ * The host that more of the given partitions prefer than any other host, or
the only location
+ * that they name on it, such as an executor. With no host ahead it is None:
naming one of the
+ * tied hosts would only narrow what the scheduler offers, the locations of
the first partition
+ * that has any. As in `DefaultPartitionCoalescer`, a partition's locations
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.
Review Comment:
**Finding 13.** The reason given here holds only when the first partition
that has locations names a tied host. Take a group on `h7`; `h1, h2`; `h1, h2`.
Hosts `h1` and `h2` tie at two inputs each, so the group names nothing, and
`sc.getPreferredLocs` then offers `h7`, which holds one input. Master offers
`h7` too, so this is no regression, and I would not add a rule for it. In a
group of two the reason always holds. My finding 8 made the same claim without
that condition, so the correction is mine. The scaladoc can say what happens
instead:
```suggestion
* that they name on it, such as an executor. With no host ahead it is
None, and the scheduler
* offers the locations of the first partition that has any. Naming a tied
host would mostly
* narrow that offer, though in a group of three or more that partition
can miss them. As in
* `DefaultPartitionCoalescer`, a partition's locations 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.
```
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/GroupPartitionsExec.scala:
##########
@@ -863,35 +865,66 @@ 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. A group names a location only
when it has several
+ * partitions, shares none of them with another group, is not over persisted
data, and more of its
+ * partitions prefer one host than any other host. Every other group leaves
its location to the
+ * scheduler.
Review Comment:
**Finding 14.** `persisted` checks the parent and all of its narrow
ancestors at once. So one persisted RDD anywhere in that lineage stops every
group from voting, not only the groups over it. What I ran: a union of a cached
RDD's map and a scan's map, with one group over each side. The group over the
scan's inputs, on h5, h5 and h6, names no host at this head. The same group
over the scan alone names h5. This only falls back to master's offer, so I
would keep the code and fix the scaladoc:
```suggestion
* required for storage-partitioned operations. When nothing in the parent's
narrow lineage is
* persisted, a group names a location if it has several partitions, shares
none of them with
* another group, and more of its partitions prefer one host than any other
host. Every other
* group leaves its location to the scheduler.
```
--
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]