Github user vanzin commented on a diff in the pull request: https://github.com/apache/spark/pull/8880#discussion_r75553873 --- Diff: yarn/src/test/scala/org/apache/spark/deploy/yarn/YarnShuffleEncryptionSuite.scala --- @@ -0,0 +1,355 @@ +/* + * 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.deploy.yarn + +import java.io._ +import java.nio.ByteBuffer +import java.security.PrivilegedExceptionAction +import java.util.{ArrayList => JArrayList, LinkedList => JLinkedList, UUID} + +import scala.runtime.AbstractFunction1 + +import com.google.common.collect.HashMultiset +import com.google.common.io.ByteStreams +import org.apache.hadoop.security.{Credentials, UserGroupInformation} +import org.junit.Assert.assertEquals +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.invocation.InvocationOnMock +import org.mockito.stubbing.Answer +import org.mockito.Answers.RETURNS_SMART_NULLS +import org.mockito.Matchers.{eq => meq, _} +import org.mockito.Mockito._ +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers} + +import org.apache.spark._ +import org.apache.spark.crypto.{CryptoConf, CryptoStreamUtils} +import org.apache.spark.crypto.CryptoConf._ +import org.apache.spark.deploy.SparkHadoopUtil +import org.apache.spark.executor.{ShuffleWriteMetrics, TaskMetrics} +import org.apache.spark.io.CompressionCodec +import org.apache.spark.memory.{TaskMemoryManager, TestMemoryManager} +import org.apache.spark.network.buffer.NioManagedBuffer +import org.apache.spark.network.util.LimitedInputStream +import org.apache.spark.serializer.{DeserializationStream, KryoSerializer, SerializerInstance, +SerializerManager} +import org.apache.spark.shuffle.{BaseShuffleHandle, BlockStoreShuffleReader, + IndexShuffleBlockResolver, RecordingManagedBuffer} +import org.apache.spark.shuffle.sort.{SerializedShuffleHandle, UnsafeShuffleWriter} +import org.apache.spark.storage._ +import org.apache.spark.util.Utils + +private[spark] class YarnShuffleEncryptionSuite extends SparkFunSuite with Matchers with + BeforeAndAfterAll with BeforeAndAfterEach { + @Mock(answer = RETURNS_SMART_NULLS) private[this] var blockManager: BlockManager = _ + @Mock(answer = RETURNS_SMART_NULLS) private[this] var blockResolver: IndexShuffleBlockResolver = _ + @Mock(answer = RETURNS_SMART_NULLS) private[this] var diskBlockManager: DiskBlockManager = _ + @Mock(answer = RETURNS_SMART_NULLS) private[this] var serializerManager: SerializerManager = _ + @Mock(answer = RETURNS_SMART_NULLS) private[this] var taskContext: TaskContext = _ + @Mock( + answer = RETURNS_SMART_NULLS) private[this] var shuffleDep: ShuffleDependency[Int, Int, Int] = _ + + private[this] val NUM_MAPS = 1 + private[this] val NUM_PARTITITONS = 4 + private[this] val REDUCE_ID = 1 + private[this] val SHUFFLE_ID = 0 + private[this] val conf = new SparkConf() + private[this] val memoryManager = new TestMemoryManager(conf) + private[this] val hashPartitioner = new HashPartitioner(NUM_PARTITITONS) + private[this] val serializer = new KryoSerializer(conf) + private[this] val spillFilesCreated = new JLinkedList[File]() + private[this] val taskMemoryManager = new TaskMemoryManager(memoryManager, 0) + private[this] val taskMetrics = new TaskMetrics() + + private[this] var tempDir: File = _ + private[this] var mergedOutputFile: File = _ + private[this] var partitionSizesInMergedFile: Array[Long] = _ + private[this] val ugi = UserGroupInformation.createUserForTesting("testuser", Array("testgroup")) + + // Create a mocked shuffle handle to pass into HashShuffleReader. + private[this] val shuffleHandle = { + val dependency = mock(classOf[ShuffleDependency[Int, Int, Int]]) + when(dependency.serializer).thenReturn(serializer) + when(dependency.aggregator).thenReturn(None) + when(dependency.keyOrdering).thenReturn(None) + new BaseShuffleHandle(SHUFFLE_ID, NUM_MAPS, dependency) + } + + + // Make a mocked MapOutputTracker for the shuffle reader to use to determine what + // shuffle data to read. + private[this] val mapOutputTracker = mock(classOf[MapOutputTracker]) + private[this] val sparkEnv = mock(classOf[SparkEnv]) + + override def beforeAll(): Unit = { + when(sparkEnv.conf).thenReturn(conf) + SparkEnv.set(sparkEnv) + + System.setProperty("SPARK_YARN_MODE", "true") + ugi.doAs(new PrivilegedExceptionAction[Unit]() { + override def run(): Unit = { + conf.set(SPARK_SHUFFLE_ENCRYPTION_ENABLED, true.toString) + val creds = new Credentials() + CryptoConf.initSparkShuffleCredentials(conf, creds) + SparkHadoopUtil.get.addCurrentUserCredentials(creds) + } + }) + } + + override def afterAll(): Unit = { + SparkEnv.set(null) + } + + override def beforeEach(): Unit = { + super.beforeEach() + MockitoAnnotations.initMocks(this) + tempDir = Utils.createTempDir() + mergedOutputFile = File.createTempFile("mergedoutput", "", tempDir) + } + + override def afterEach(): Unit = { + super.afterEach() + conf.set("spark.shuffle.compress", false.toString) + conf.set("spark.shuffle.spill.compress", false.toString) + Utils.deleteRecursively(tempDir) + val leakedMemory = taskMemoryManager.cleanUpAllAllocatedMemory() + if (leakedMemory != 0) { --- End diff -- `assert(leakedMemory === 0)`
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org