Github user chenghao-intel commented on a diff in the pull request:

    https://github.com/apache/spark/pull/8635#discussion_r39031470
  
    --- Diff: 
sql/core/src/test/scala/org/apache/spark/sql/MiniSparkSQLClusterSuite.scala ---
    @@ -0,0 +1,168 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql
    +
    +import java.io.File
    +import java.util.UUID
    +
    +import org.apache.spark.executor.{ShuffleWriteMetrics, TaskMetrics}
    +import org.apache.spark.serializer.SerializerInstance
    +import org.apache.spark.sql.catalyst.{InternalRow, CatalystTypeConverters}
    +import org.apache.spark.sql.catalyst.expressions.{UnsafeProjection, 
UnsafeRow}
    +import org.apache.spark.sql.execution.UnsafeRowSerializer
    +import org.apache.spark.sql.types.{IntegerType, DataType}
    +import org.apache.spark.storage._
    +import org.apache.spark.util.Utils
    +import org.apache.spark._
    +import org.apache.spark.util.collection.ExternalSorter
    +import org.mockito.Answers.RETURNS_SMART_NULLS
    +import org.mockito.Matchers._
    +import org.mockito.Mockito._
    +import org.mockito.invocation.InvocationOnMock
    +import org.mockito.stubbing.Answer
    +import org.mockito.{MockitoAnnotations, Mock}
    +import org.scalatest.BeforeAndAfterEach
    +
    +import scala.collection.mutable
    +import scala.collection.mutable.ArrayBuffer
    +
    +class MiniSparkSQLClusterSuite extends SparkFunSuite with 
BeforeAndAfterEach {
    +
    +  @Mock(answer = RETURNS_SMART_NULLS) private var blockManager: 
BlockManager = _
    +  @Mock(answer = RETURNS_SMART_NULLS) private var diskBlockManager: 
DiskBlockManager = _
    +  @Mock(answer = RETURNS_SMART_NULLS) private var taskContext: TaskContext 
= _
    +
    +  private var taskMetrics: TaskMetrics = _
    +  private var shuffleWriteMetrics: ShuffleWriteMetrics = _
    +  private var tempDir: File = _
    +  private var outputFile: File = _
    +  private val temporaryFilesCreated: mutable.Buffer[File] = new 
ArrayBuffer[File]()
    +  private val blockIdToFileMap: mutable.Map[BlockId, File] = new 
mutable.HashMap[BlockId, File]
    +  private val shuffleBlockId: ShuffleBlockId = new ShuffleBlockId(0, 0, 0)
    +
    +  override def beforeEach(): Unit = {
    +    tempDir = Utils.createTempDir()
    +    outputFile = File.createTempFile("shuffle", null, tempDir)
    +    shuffleWriteMetrics = new ShuffleWriteMetrics
    +    taskMetrics = new TaskMetrics
    +    taskMetrics.shuffleWriteMetrics = Some(shuffleWriteMetrics)
    +    MockitoAnnotations.initMocks(this)
    +    when(taskContext.taskMetrics()).thenReturn(taskMetrics)
    +    import InternalAccumulator._
    +    when(taskContext.internalMetricsToAccumulators).thenReturn(
    +      Map(
    +        PEAK_EXECUTION_MEMORY ->
    +        new Accumulator(
    +          0L, AccumulatorParam.LongAccumulatorParam, 
Some(PEAK_EXECUTION_MEMORY), internal = true)))
    +    when(blockManager.diskBlockManager).thenReturn(diskBlockManager)
    +    when(blockManager.getDiskWriter(
    +      any[BlockId],
    +      any[File],
    +      any[SerializerInstance],
    +      anyInt(),
    +      any[ShuffleWriteMetrics]
    +    )).thenAnswer(new Answer[DiskBlockObjectWriter] {
    +      override def answer(invocation: InvocationOnMock): 
DiskBlockObjectWriter = {
    +        val args = invocation.getArguments
    +        new DiskBlockObjectWriter(
    +          args(0).asInstanceOf[BlockId],
    +          args(1).asInstanceOf[File],
    +          args(2).asInstanceOf[SerializerInstance],
    +          args(3).asInstanceOf[Int],
    +          compressStream = identity,
    +          syncWrites = false,
    +          args(4).asInstanceOf[ShuffleWriteMetrics]
    +        )
    +      }
    +    })
    +    when(diskBlockManager.createTempShuffleBlock()).thenAnswer(
    +      new Answer[(TempShuffleBlockId, File)] {
    +        override def answer(invocation: InvocationOnMock): 
(TempShuffleBlockId, File) = {
    +          val blockId = new TempShuffleBlockId(UUID.randomUUID)
    +          val file = File.createTempFile(blockId.toString, null, tempDir)
    +          blockIdToFileMap.put(blockId, file)
    +          temporaryFilesCreated.append(file)
    +          (blockId, file)
    +        }
    +      })
    +    when(diskBlockManager.getFile(any[BlockId])).thenAnswer(
    +      new Answer[File] {
    +        override def answer(invocation: InvocationOnMock): File = {
    +          
blockIdToFileMap.get(invocation.getArguments.head.asInstanceOf[BlockId]).get
    +        }
    +      })
    +  }
    +
    +  override def afterEach(): Unit = {
    +    Utils.deleteRecursively(tempDir)
    +    blockIdToFileMap.clear()
    +    temporaryFilesCreated.clear()
    +  }
    +
    +  private def toUnsafeRow(row: Row, schema: Array[DataType]): UnsafeRow = {
    +    val internalRow = 
CatalystTypeConverters.convertToCatalyst(row).asInstanceOf[InternalRow]
    +    val converter = UnsafeProjection.create(schema)
    +    converter.apply(internalRow)
    +  }
    +
    +  /**
    +   * Create a spark context with specified configuration, and the calls `f`
    +   */
    +  protected def withSparkConf(sparkConfs: (String, String)*)(f: 
(SparkContext) => Unit): Unit = {
    +    val (keys, values) = sparkConfs.unzip
    +    val conf = new SparkConf(true)
    +      .setMaster("local[1]")
    +      .setAppName("testing")
    +
    +    var sc: SparkContext = null
    +    val env = SparkEnv.get
    +    Utils.tryWithSafeFinally {
    +      (keys, values).zipped.foreach(conf.set)
    +      sc = new SparkContext(conf)
    +      f(sc)
    +    } {
    +      sc.stop()
    +      SparkEnv.set(env)
    +    }
    +  }
    +
    +  test("SPARK-10466 external sorting for UnsafeRow with spilling") {
    +    withSparkConf(
    +      ("spark.shuffle.sort.bypassMergeThreshold", "0"),
    +      ("spark.shuffle.spill.initialMemoryThreshold", "1024"), // 1k
    +      ("spark.shuffle.memoryFraction", "0.0001")) { sc =>
    +
    +      // create unsafe rows
    +      val count = 10000
    +      val unsafeRowIt = (1 to count).iterator.map { i =>
    +        (i, toUnsafeRow(Row(i, i, i), Array(IntegerType, IntegerType, 
IntegerType)))
    +      }
    +
    +      val serializer = new UnsafeRowSerializer(3)
    +      val sorter = new ExternalSorter[Int, UnsafeRow, UnsafeRow](
    +        None, None, Some(implicitly[Ordering[Int]]), Some(serializer))
    +      sorter.insertAll(unsafeRowIt)
    +      // Make sure it spilled
    +      assert(sc.env.blockManager.diskBlockManager.getAllFiles().length > 0)
    +
    +      assert(sorter.writePartitionedFile(shuffleBlockId, taskContext, 
outputFile).sum > 0)
    --- End diff --
    
    Exception will be thrown here if we didn't change the `UnsafeRowSerializer` 
as above.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to