tgravescs commented on a change in pull request #24406: [SPARK-27024] Executor interface for cluster managers to support GPU and other resources URL: https://github.com/apache/spark/pull/24406#discussion_r283010629
########## File path: core/src/test/scala/org/apache/spark/executor/CoarseGrainedExecutorBackendSuite.scala ########## @@ -0,0 +1,241 @@ +/* + * 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.executor + + +import java.io.{File, PrintWriter} +import java.net.URL +import java.nio.charset.StandardCharsets +import java.nio.file.{Files => JavaFiles} +import java.nio.file.attribute.PosixFilePermission.{OWNER_EXECUTE, OWNER_READ, OWNER_WRITE} +import java.util.EnumSet + +import com.google.common.io.Files +import org.json4s.JsonAST.{JArray, JObject, JString} +import org.json4s.JsonDSL._ +import org.json4s.jackson.JsonMethods.{compact, render} +import org.mockito.Mockito.when +import org.scalatest.mockito.MockitoSugar + +import org.apache.spark._ +import org.apache.spark.internal.config._ +import org.apache.spark.rpc.RpcEnv +import org.apache.spark.serializer.JavaSerializer +import org.apache.spark.util.Utils + +class CoarseGrainedExecutorBackendSuite extends SparkFunSuite + with LocalSparkContext with MockitoSugar { + + // scalastyle:off println + private def writeFileWithJson(dir: File, strToWrite: JArray): String = { + val f1 = File.createTempFile("test-resource-parser1", "", dir) + val writer1 = new PrintWriter(f1) + writer1.println(compact(render(strToWrite))) + writer1.close() + f1.getPath() + } + // scalastyle:on println + + test("parsing no resources") { + val conf = new SparkConf + conf.set(SPARK_TASK_RESOURCE_PREFIX + "gpu" + SPARK_RESOURCE_COUNT_POSTFIX, "2") + val serializer = new JavaSerializer(conf) + val env = createMockEnv(conf, serializer) + + // we don't really use this, just need it to get at the parser function + val backend = new CoarseGrainedExecutorBackend( env.rpcEnv, "driverurl", "1", "host1", + 4, Seq.empty[URL], env, None) + withTempDir { tmpDir => + val testResourceArgs: JObject = ("" -> "") + val ja = JArray(List(testResourceArgs)) + val f1 = writeFileWithJson(tmpDir, ja) + var error = intercept[SparkException] { + val parsedResources = backend.parseOrFindResources(Some(f1)) + }.getMessage() + + assert(error.contains("Exception parsing the resources in")) + } + } + + test("parsing one resources") { + val conf = new SparkConf + conf.set(SPARK_EXECUTOR_RESOURCE_PREFIX + "gpu" + SPARK_RESOURCE_COUNT_POSTFIX, "2") + conf.set(SPARK_TASK_RESOURCE_PREFIX + "gpu" + SPARK_RESOURCE_COUNT_POSTFIX, "2") + val serializer = new JavaSerializer(conf) + val env = createMockEnv(conf, serializer) + // we don't really use this, just need it to get at the parser function + val backend = new CoarseGrainedExecutorBackend( env.rpcEnv, "driverurl", "1", "host1", + 4, Seq.empty[URL], env, None) + withTempDir { tmpDir => + val testResourceArgs = + ("name" -> "gpu") ~ + ("addresses" -> JArray(Array("0", "1").map(JString(_)).toList)) + val ja = JArray(List(testResourceArgs)) + val f1 = writeFileWithJson(tmpDir, ja) + val parsedResources = backend.parseOrFindResources(Some(f1)) + + assert(parsedResources.size === 1) + assert(parsedResources.get("gpu").nonEmpty) + assert(parsedResources.get("gpu").get.name === "gpu") + assert(parsedResources.get("gpu").get.addresses.deep === Array("0", "1").deep) + } + } + + test("parsing multiple resources") { + val conf = new SparkConf + conf.set(SPARK_EXECUTOR_RESOURCE_PREFIX + "fpga" + SPARK_RESOURCE_COUNT_POSTFIX, "3") + conf.set(SPARK_TASK_RESOURCE_PREFIX + "fpga" + SPARK_RESOURCE_COUNT_POSTFIX, "3") + conf.set(SPARK_EXECUTOR_RESOURCE_PREFIX + "gpu" + SPARK_RESOURCE_COUNT_POSTFIX, "2") + conf.set(SPARK_TASK_RESOURCE_PREFIX + "gpu" + SPARK_RESOURCE_COUNT_POSTFIX, "2") + val serializer = new JavaSerializer(conf) + val env = createMockEnv(conf, serializer) + // we don't really use this, just need it to get at the parser function + val backend = new CoarseGrainedExecutorBackend( env.rpcEnv, "driverurl", "1", "host1", + 4, Seq.empty[URL], env, None) + + withTempDir { tmpDir => + val gpuArgs = + ("name" -> "gpu") ~ + ("addresses" -> JArray(Array("0", "1").map(JString(_)).toList)) Review comment: ah nice, that is much cleaner ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
