|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark.sql.comet.execution.shuffle |
| 21 | + |
| 22 | +import org.apache.spark.HashPartitioner |
| 23 | +import org.apache.spark.serializer.JavaSerializer |
| 24 | +import org.apache.spark.sql.CometTestBase |
| 25 | +import org.apache.spark.sql.comet.{CometMetricNode, NativeExecContext} |
| 26 | +import org.apache.spark.sql.execution.metric.SQLMetrics |
| 27 | +import org.apache.spark.sql.vectorized.ColumnarBatch |
| 28 | + |
| 29 | +import org.apache.comet.serde.OperatorOuterClass.Operator |
| 30 | + |
| 31 | +/** |
| 32 | + * Ensure the serialized shuffle-map-stage task binary does not grow with the number of |
| 33 | + * partitions. |
| 34 | + * |
| 35 | + * `DAGScheduler.submitMissingTasks` broadcasts the serialized `(stage.rdd, stage.shuffleDep)` |
| 36 | + * pair, which must fit in ~2GB. On the native-shuffle path the scan plan data is one serialized |
| 37 | + * blob per map partition; the leak lived under |
| 38 | + * `CometShuffleDependency.nativeShuffleSpec.execContext`, not on the thin RDD. So this builds |
| 39 | + * both carriers and serializes the pair, which catches a regression in either the RDD or the |
| 40 | + * dependency guards without needing a real 2GB allocation. |
| 41 | + * |
| 42 | + * Lives in the `execution.shuffle` package so it can construct the `private[shuffle]` |
| 43 | + * [[CometNativeShuffleInputRDD]] and the `private[comet]` [[NativeExecContext]] directly. |
| 44 | + */ |
| 45 | +class CometNativeShuffleInputRDDSuite extends CometTestBase { |
| 46 | + |
| 47 | + test("serialized (rdd, dep) task binary size is independent of partition count") { |
| 48 | + val sc = spark.sparkContext |
| 49 | + val ser = new JavaSerializer(sc.getConf).newInstance() |
| 50 | + |
| 51 | + // One scan key with a 1KB blob per map partition -- the per-partition plan data. Build both |
| 52 | + // objects the DAGScheduler serializes for a shuffle-map stage: the thin RDD, and the |
| 53 | + // CometShuffleDependency whose NativeShuffleSpec holds a NativeExecContext with this map. |
| 54 | + def build(numPartitions: Int): ( |
| 55 | + CometNativeShuffleInputRDD, |
| 56 | + CometShuffleDependency[Int, ColumnarBatch, ColumnarBatch]) = { |
| 57 | + val perPartitionByKey = |
| 58 | + Map("scan-0" -> Array.fill(numPartitions)(new Array[Byte](1024))) |
| 59 | + val rdd = new CometNativeShuffleInputRDD( |
| 60 | + sc, |
| 61 | + inputRDDs = Seq.empty, |
| 62 | + numPartitionsParam = numPartitions, |
| 63 | + shuffleScanIndices = Set.empty, |
| 64 | + perPartitionByKey = perPartitionByKey) |
| 65 | + val execContext = NativeExecContext( |
| 66 | + inputs = Seq.empty, |
| 67 | + numPartitions = numPartitions, |
| 68 | + subqueries = Seq.empty, |
| 69 | + broadcastedHadoopConfForEncryption = None, |
| 70 | + encryptedFilePaths = Seq.empty, |
| 71 | + commonByKey = Map.empty, |
| 72 | + perPartitionByKey = perPartitionByKey, |
| 73 | + shuffleScanIndices = Set.empty, |
| 74 | + hasScanInput = false) |
| 75 | + val spec = |
| 76 | + NativeShuffleSpec(Operator.getDefaultInstance, CometMetricNode(Map.empty), execContext) |
| 77 | + val dep = new CometShuffleDependency[Int, ColumnarBatch, ColumnarBatch]( |
| 78 | + _rdd = rdd, |
| 79 | + partitioner = new HashPartitioner(numPartitions), |
| 80 | + decodeTime = SQLMetrics.createMetric(sc, "decode time"), |
| 81 | + nativeShuffleSpec = Some(spec)) |
| 82 | + (rdd, dep) |
| 83 | + } |
| 84 | + |
| 85 | + // Pre-fix this pair grew from ~13KB to ~10MB between 10 and 10000 partitions. With the map held |
| 86 | + // @transient on both the RDD and the NativeExecContext, the pair stays roughly constant. |
| 87 | + val (_, smallDep) = build(10) |
| 88 | + val (largeRdd, largeDep) = build(10000) |
| 89 | + val smallPair = ser.serialize((smallDep.rdd, smallDep)).limit() |
| 90 | + val largePair = ser.serialize((largeDep.rdd, largeDep)).limit() |
| 91 | + assert( |
| 92 | + math.abs(largePair - smallPair) < 100 * 1024, |
| 93 | + s"serialized (rdd, dep) grew with partition count (small=$smallPair, large=$largePair); " + |
| 94 | + "the per-partition plan-data map is leaking into the broadcast task binary") |
| 95 | + |
| 96 | + // Each task's Partition object still carries its own slice so the writer can inject plan data. |
| 97 | + val part = largeRdd.partitions(7).asInstanceOf[CometNativeShuffleInputPartition] |
| 98 | + assert(part.planDataByKey.keySet == Set("scan-0")) |
| 99 | + assert(part.planDataByKey("scan-0").length == 1024) |
| 100 | + } |
| 101 | +} |
0 commit comments