Ngone51 commented on a change in pull request #24856: [SPARK-27823] [CORE] 
Refactor resource handling code
URL: https://github.com/apache/spark/pull/24856#discussion_r293863096
 
 

 ##########
 File path: 
core/src/test/scala/org/apache/spark/resource/ResourceUtilsSuite.scala
 ##########
 @@ -0,0 +1,230 @@
+/*
+ * 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.resource
+
+import java.io.File
+import java.nio.file.{Files => JavaFiles}
+
+import org.apache.spark.{LocalSparkContext, SparkConf, SparkException, 
SparkFunSuite}
+import org.apache.spark.TestUtils._
+import org.apache.spark.internal.config._
+import org.apache.spark.resource.ResourceUtils._
+import org.apache.spark.resource.TestResourceIDs._
+import org.apache.spark.util.Utils
+
+class ResourceUtilsSuite extends SparkFunSuite
+    with LocalSparkContext {
+
+  test("ResourceID") {
+    val componentName = "spark.test"
+    val resourceName = "p100"
+    val id = ResourceID(componentName, resourceName)
+    val confPrefix = s"$componentName.resource.$resourceName."
+    assert(id.confPrefix === confPrefix)
+    assert(id.amountConf === s"${confPrefix}amount")
+    assert(id.discoveryScriptConf === s"${confPrefix}discoveryScript")
+    assert(id.vendorConf === s"${confPrefix}vendor")
+  }
+
+  test("Resource discoverer no addresses errors") {
+    val conf = new SparkConf
+    assume(!(Utils.isWindows))
+    withTempDir { dir =>
+      val scriptPath = createTempScriptWithExpectedOutput(dir, 
"gpuDiscoverScript",
+        """{"name": "gpu"}""")
+      conf.set(EXECUTOR_GPU_ID.amountConf, "2")
+      conf.set(EXECUTOR_GPU_ID.discoveryScriptConf, scriptPath)
+
+      val error = intercept[IllegalArgumentException] {
+        getOrDiscoverAllResources(conf, SPARK_EXECUTOR_PREFIX, None)
+      }.getMessage()
+      assert(error.contains("Resource: gpu, with " +
+        "addresses:  is less than what the user requested: 2"))
+    }
+  }
+
+  test("Resource discoverer multiple resource types") {
+    val conf = new SparkConf
+    assume(!(Utils.isWindows))
+    withTempDir { dir =>
+      val gpuDiscovery = createTempScriptWithExpectedOutput(dir, 
"gpuDiscoveryScript",
+        """{"name": "gpu", "addresses": ["0", "1"]}""")
+      conf.set(EXECUTOR_GPU_ID.amountConf, "2")
+      conf.set(EXECUTOR_GPU_ID.discoveryScriptConf, gpuDiscovery)
+
+      val fpgaDiscovery = createTempScriptWithExpectedOutput(dir, 
"fpgDiscoverScript",
+        """{"name": "fpga", "addresses": ["f1", "f2", "f3"]}""")
+      conf.set(EXECUTOR_FPGA_ID.amountConf, "2")
+      conf.set(EXECUTOR_FPGA_ID.discoveryScriptConf, fpgaDiscovery)
+
+      val resources = getOrDiscoverAllResources(conf, SPARK_EXECUTOR_PREFIX, 
None)
+      assert(resources.size === 2)
+      val gpuValue = resources.get(GPU)
+      assert(gpuValue.nonEmpty, "Should have a gpu entry")
+      assert(gpuValue.get.name == "gpu", "name should be gpu")
+      assert(gpuValue.get.addresses.size == 2, "Should have 2 indexes")
+      assert(gpuValue.get.addresses.deep == Array("0", "1").deep, "should have 
0,1 entries")
+
+      val fpgaValue = resources.get(FPGA)
+      assert(fpgaValue.nonEmpty, "Should have a gpu entry")
+      assert(fpgaValue.get.name == "fpga", "name should be fpga")
+      assert(fpgaValue.get.addresses.size == 3, "Should have 3 indexes")
+      assert(fpgaValue.get.addresses.deep == Array("f1", "f2", "f3").deep,
+        "should have f1,f2,f3 entries")
+    }
+  }
+
+  test("list resource ids") {
+    val conf = new SparkConf
+    conf.set(DRIVER_GPU_ID.amountConf, "2")
+    var resources = listResourceIds(conf, SPARK_DRIVER_PREFIX)
+    assert(resources.size === 1, "should only have GPU for resource")
+    assert(resources(0).resourceName == GPU, "name should be gpu")
+
+    conf.set(DRIVER_FPGA_ID.amountConf, "2")
+    val resourcesMap = listResourceIds(conf, SPARK_DRIVER_PREFIX)
+      .map{ rId => (rId.resourceName, 1)}.toMap
+    assert(resourcesMap.size === 2, "should only have GPU for resource")
+    assert(resourcesMap.get(GPU).nonEmpty, "should have GPU")
+    assert(resourcesMap.get(FPGA).nonEmpty, "should have FPGA")
+  }
+
+  test("parse resource request") {
+    val conf = new SparkConf
+    conf.set(DRIVER_GPU_ID.amountConf, "2")
+    var request = parseResourceRequest(conf, DRIVER_GPU_ID)
+    assert(request.id.resourceName === GPU, "should only have GPU for 
resource")
+    assert(request.amount === 2, "GPU count should be 2")
+    assert(request.discoveryScript === None, "discovery script should be 
empty")
+    assert(request.vendor === None, "vendor should be empty")
+
+    val vendor = "nvidia.com"
+    val discoveryScript = "discoveryScriptGPU"
+    conf.set(DRIVER_GPU_ID.discoveryScriptConf, discoveryScript)
+    conf.set(DRIVER_GPU_ID.vendorConf, vendor)
+    request = parseResourceRequest(conf, DRIVER_GPU_ID)
+    assert(request.id.resourceName === GPU, "should only have GPU for 
resource")
+    assert(request.amount === 2, "GPU count should be 2")
+    assert(request.discoveryScript.get === discoveryScript, "discovery script 
should be empty")
 
 Review comment:
   "discovery script should be discoveryScriptGPU"

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to