srowen commented on a change in pull request #24374: [SPARK-27366][CORE] Support GPU Resources in Spark job scheduling URL: https://github.com/apache/spark/pull/24374#discussion_r275849596
########## File path: core/src/test/scala/org/apache/spark/executor/ResourceDiscovererSuite.scala ########## @@ -0,0 +1,124 @@ +/* + * 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 +import java.nio.charset.StandardCharsets +import java.nio.file.{Files => JavaFiles} +import java.nio.file.attribute.PosixFilePermission._ +import java.util.EnumSet + +import com.google.common.io.Files + +import org.apache.spark._ +import org.apache.spark.internal.config._ + +class ResourceDiscovererSuite extends SparkFunSuite + with LocalSparkContext { + + test("Resource discoverer no gpus") { + val sparkconf = new SparkConf + val resources = ResourceDiscoverer.findResources(sparkconf, false) + assert(resources.get("gpu").isEmpty, + "Should have a gpus entry that is empty") + } + + test("Resource discoverer multiple gpus") { + val sparkconf = new SparkConf + + val file1 = File.createTempFile("test", "resourceDiscoverScript1") + try { + Files.write("echo 0,1", file1, StandardCharsets.UTF_8) + JavaFiles.setPosixFilePermissions(file1.toPath(), + EnumSet.of(OWNER_READ, OWNER_EXECUTE, OWNER_WRITE)) + sparkconf.set(EXECUTOR_GPU_DISCOVERY_SCRIPT, file1.getPath()) + val resources = ResourceDiscoverer.findResources(sparkconf, false) + val gpuValue = resources.get(ResourceInformation.GPU) + assert(gpuValue.nonEmpty, "Should have a gpu entry") + assert(gpuValue.get.getCount() == 2, "Should have 2") + assert(gpuValue.get.getName() == ResourceInformation.GPU, "name should be gpu") + assert(gpuValue.get.getUnits() == "", "units should be empty") + assert(gpuValue.get.getAddresses().size == 2, "Should have 2 indexes") + assert(gpuValue.get.getAddresses().deep == Array("0", "1").deep, "should have 0,1 entries") + } finally { + JavaFiles.deleteIfExists(file1.toPath()) + } + } + + test("Resource discoverer multiple gpus driver") { + val sparkconf = new SparkConf + + val file1 = File.createTempFile("test", "resourceDiscoverScript1") + try { + Files.write("echo 0,1", file1, StandardCharsets.UTF_8) + JavaFiles.setPosixFilePermissions(file1.toPath(), + EnumSet.of(OWNER_READ, OWNER_EXECUTE, OWNER_WRITE)) + sparkconf.set(DRIVER_GPU_DISCOVERY_SCRIPT, file1.getPath()) + sparkconf.set(EXECUTOR_GPU_DISCOVERY_SCRIPT, "boguspath") + // make sure it reads from correct config, here it should use driver + val resources = ResourceDiscoverer.findResources(sparkconf, true) + val gpuValue = resources.get(ResourceInformation.GPU) + assert(gpuValue.nonEmpty, "Should have a gpu entry") + assert(gpuValue.get.getCount() == 2, "Should have 2") + assert(gpuValue.get.getName() == ResourceInformation.GPU, "name should be gpu") + assert(gpuValue.get.getUnits() == "", "units should be empty") + assert(gpuValue.get.getAddresses().size == 2, "Should have 2 indexes") + assert(gpuValue.get.getAddresses().deep == Array("0", "1").deep, "should have 0,1 entries") + } finally { + JavaFiles.deleteIfExists(file1.toPath()) + } + } + + + test("Resource discoverer script returns invalid number") { + val sparkconf = new SparkConf + + val file1 = File.createTempFile("test", "resourceDiscoverScript1") + try { + Files.write("echo foo1", file1, StandardCharsets.UTF_8) + JavaFiles.setPosixFilePermissions(file1.toPath(), + EnumSet.of(OWNER_READ, OWNER_EXECUTE, OWNER_WRITE)) + sparkconf.set(EXECUTOR_GPU_DISCOVERY_SCRIPT, file1.getPath()) + + val error = intercept[SparkException] { + ResourceDiscoverer.findResources(sparkconf, false) + }.getMessage() + + assert(error.contains("The gpu discover script threw exception")) + } finally { + JavaFiles.deleteIfExists(file1.toPath()) + } + } + + test("Resource discoverer script doesn't exist") { + val sparkconf = new SparkConf + + val file1 = new File("/tmp/bogus") Review comment: OK, that's fine, yeah. For other cases where you need a temp file I'd use Utils.withTempDir and make a file inside that dir ---------------------------------------------------------------- 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]
