Github user vanzin commented on a diff in the pull request:
https://github.com/apache/spark/pull/8880#discussion_r47735014
--- Diff:
yarn/src/test/scala/org/apache/spark/deploy/yarn/YarnShuffleEncryptionSuite.scala
---
@@ -0,0 +1,307 @@
+/*
+ * 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.UUID
+import java.util.{ArrayList => JArrayList, LinkedList => JLinkedList}
+
+import org.apache.spark.rpc.{RpcEnvConfig, RpcEnv}
+import org.apache.spark.rpc.akka.AkkaRpcEnvFactory
+
+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._
+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.{TestMemoryManager, TaskMemoryManager}
+import org.apache.spark.network.buffer.NioManagedBuffer
+import org.apache.spark.serializer.{KryoSerializer, SerializerInstance}
+import org.apache.spark.shuffle.{RecordingManagedBuffer, BaseShuffleHandle,
+ BlockStoreShuffleReader, IndexShuffleBlockResolver}
+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 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 env: RpcEnv = _
+ private[this] var tempDir: File = _
+ private[this] var mergedOutputFile: File = _
+ private[this] var partitionSizesInMergedFile: Array[Long] = _
+ private[this] val ugi =
UserGroupInformation.createUserForTesting("testuser", Array[String] {
+ "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(Some(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])
+
+ override def beforeAll(): Unit = {
+ env = new AkkaRpcEnvFactory().create(
+ RpcEnvConfig(conf, "local", "localhost", 0, new
SecurityManager(conf), false))
+
+ val sparkEnv = mock(classOf[SparkEnv])
+ when(sparkEnv.rpcEnv).thenReturn(env)
+ 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 = {
+ if (env != null) {
+ env.shutdown()
+ }
+ 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)
+ Utils.deleteRecursively(tempDir)
+ val leakedMemory = taskMemoryManager.cleanUpAllAllocatedMemory()
+ if (leakedMemory != 0) {
+ fail("Test leaked " + leakedMemory + " bytes of managed memory")
+ }
+ }
+
+ test("yarn shuffle encryption read and write") {
+ ugi.doAs(new PrivilegedExceptionAction[Unit] {
+ override def run(): Unit = {
+ testYarnShuffleEncryptionWriteRead()
+ }
+ })
+ }
+
+ test("yarn shuffle encryption read and write with shuffle compression
enabled") {
+ ugi.doAs(new PrivilegedExceptionAction[Unit] {
+ override def run(): Unit = {
+ conf.set("spark.shuffle.compress", true.toString)
+ testYarnShuffleEncryptionWriteRead()
+ }
+ })
+ }
+
+ private[this] def testYarnShuffleEncryptionWriteRead(): Unit = {
+ val dataToWrite = new JArrayList[Product2[Int, Int]]()
+ for (i <- 0 to NUM_PARTITITONS) dataToWrite.add((i, i))
--- End diff --
nit:
for (i <- 0 to NUM_PARTITITONS) {
dataToWrite.add((i, i))
}
---
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]