Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions core/src/main/scala/org/apache/spark/ResourceDiscoverer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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

import java.io.File

import org.apache.spark.internal.Logging
import org.apache.spark.internal.config._
import org.apache.spark.util.Utils.executeAndGetOutput

/**
* Discovers resources (GPUs/FPGAs/etc).
* This class find resources by running and parses the output of the user specified script
* from the config spark.{driver/executor}.{resourceType}.discoveryScript.
* The output of the script it runs is expected to be a String that is in the format of
* count:unit:comma-separated list of addresses, where the list of addresses is
* specific for that resource type. The user is responsible for interpreting the address.
*/
private[spark] object ResourceDiscoverer extends Logging {

def findResources(sparkconf: SparkConf, isDriver: Boolean): Map[String, ResourceInformation] = {
val prefix = if (isDriver) {
SPARK_DRIVER_RESOURCE_PREFIX
} else {
SPARK_EXECUTOR_RESOURCE_PREFIX
}
// get unique resource types
val resourceTypes = sparkconf.getAllWithPrefix(prefix).map(x => x._1.split('.')(0)).toSet
resourceTypes.map{ rtype => {
val rInfo = getResourceAddrsForType(sparkconf, prefix, rtype)
(rtype -> rInfo)
}}.toMap
}

private def getResourceAddrsForType(
sparkconf: SparkConf,
prefix: String,
resourceType: String): ResourceInformation = {
val discoveryConf = prefix + resourceType + SPARK_RESOURCE_DISCOVERY_SCRIPT_POSTFIX
val script = sparkconf.getOption(discoveryConf)
val result = if (script.nonEmpty) {
val scriptFile = new File(script.get)
// check that script exists and try to execute
if (scriptFile.exists()) {
try {
val output = executeAndGetOutput(Seq(script.get), new File("."))
parseResourceTypeString(resourceType, output)
} catch {
case e @ (_: SparkException | _: NumberFormatException) =>
throw new SparkException(s"Error running the resource discovery script: $scriptFile" +
s" for $resourceType", e)
}
} else {
throw new SparkException(s"Resource script: $scriptFile to discover $resourceType" +
s" doesn't exist!")
}
} else {
throw new SparkException(s"User is expecting to use $resourceType resources but " +
s"didn't specify a script via conf: $discoveryConf, to find them!")
}
result
}

// this parses a resource information string in the format:
// count:unit:comma-separated list of addresses
// The units and addresses are optional. The idea being if the user has something like
// memory you don't have addresses to assign out.
def parseResourceTypeString(rtype: String, rInfoStr: String): ResourceInformation = {
// format should be: count:unit:addr1,addr2,addr3
val singleResourceType = rInfoStr.split(':')
if (singleResourceType.size < 3) {
throw new SparkException("Format of the resourceAddrs parameter is invalid," +
" please specify all of count, unit, and addresses in the format:" +
" count:unit:addr1,addr2,addr3")
}
// format should be: addr1,addr2,addr3
val splitAddrs = singleResourceType(2).split(',').map(_.trim())
val retAddrs = if (splitAddrs.size == 1 && splitAddrs(0).isEmpty()) {
Array.empty[String]
} else {
splitAddrs
}
new ResourceInformation(rtype, singleResourceType(1), singleResourceType(0).toLong, retAddrs)
}
}
46 changes: 46 additions & 0 deletions core/src/main/scala/org/apache/spark/ResourceInformation.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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

import org.apache.spark.annotation.Evolving

/**
* Class to hold information about a type of Resource. A resource could be a GPU, FPGA, etc.
* The array of addresses are resource specific and its up to the user to interpret the address.
* The units and addresses could be empty if they doesn't apply to that resource.
*
* One example is GPUs, where the addresses would be the indices of the GPUs, the count would be the
* number of GPUs and the units would be an empty string.
*
* @param name the name of the resource
* @param units the units of the resources, can be an empty string if units don't apply
* @param count the number of resources available
* @param addresses an optional array of strings describing the addresses of the resource
*/
@Evolving
case class ResourceInformation(
private val name: String,
private val units: String,
private val count: Long,
private val addresses: Array[String] = Array.empty) {

def getName(): String = name
def getUnits(): String = units
def getCount(): Long = count
def getAddresses(): Array[String] = addresses
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.apache.spark.TaskState.TaskState
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.deploy.worker.WorkerWatcher
import org.apache.spark.internal.Logging
import org.apache.spark.internal.config._
import org.apache.spark.rpc._
import org.apache.spark.scheduler.{ExecutorLossReason, TaskDescription}
import org.apache.spark.scheduler.cluster.CoarseGrainedClusterMessages._
Expand All @@ -44,7 +45,8 @@ private[spark] class CoarseGrainedExecutorBackend(
hostname: String,
cores: Int,
userClassPath: Seq[URL],
env: SparkEnv)
env: SparkEnv,
resourceAddrs: Option[String])
extends ThreadSafeRpcEndpoint with ExecutorBackend with Logging {

private[this] val stopping = new AtomicBoolean(false)
Expand All @@ -61,7 +63,7 @@ private[spark] class CoarseGrainedExecutorBackend(
// This is a very fast action so we can use "ThreadUtils.sameThread"
driver = Some(ref)
ref.ask[Boolean](RegisterExecutor(executorId, self, hostname, cores, extractLogUrls,
extractAttributes))
extractAttributes, parseResources(resourceAddrs)))
}(ThreadUtils.sameThread).onComplete {
// This is a very fast action so we can use "ThreadUtils.sameThread"
case Success(msg) =>
Expand All @@ -71,6 +73,46 @@ private[spark] class CoarseGrainedExecutorBackend(
}(ThreadUtils.sameThread)
}

// visible for testing
def parseResources(resourceAddrsArg: Option[String]): Map[String, ResourceInformation] = {
// only parse the resources if a task requires them
val taskConfPrefix = SPARK_TASK_RESOURCE_PREFIX
val resourceInfo = if (env.conf.getAllWithPrefix(taskConfPrefix).size > 0) {
val resources = resourceAddrsArg.map(resourceStr => {
// format here would be:
// resourceType=count:unit:addr1,addr2,addr3;resourceType2=count:unit:r2addr1,r2addr2,
// first separate out resource types
val allResourceTypes = resourceStr.split(';').map(_.trim()).map( eachResource => {
// format here should be: resourceType=count:unit:addr1,addr2,addr3
val typeAndValue = eachResource.split('=').map(_.trim)
if (typeAndValue.size < 2) {
throw new SparkException("Format of the resourceAddrs parameter is invalid," +
" please specify both resource type and the count:unit:addresses: " +
"--resourceAddrs <resourceType=count:unit:addr1,addr2,addr3;" +
"resourceType2=count:unit:r2addr1,r2addr2,...>")
}
val resType = typeAndValue(0)
// format should be: count:unit:addr1,addr2,addr3
val singleResourceInfo =
ResourceDiscoverer.parseResourceTypeString(resType, typeAndValue(1))
(resType, singleResourceInfo)
}).toMap
allResourceTypes
}).getOrElse(ResourceDiscoverer.findResources(env.conf, false))

if (resources.size == 0) {
throw new SparkException(s"User specified resources per task via: $taskConfPrefix," +
s" but can't find any resources available on the executor.")
}
logInfo(s"Executor ${executorId} using resources: ${resources.values}")
// todo - add logDebug with full output?
resources
} else {
Map.empty[String, ResourceInformation]
}
resourceInfo
}

def extractLogUrls: Map[String, String] = {
val prefix = "SPARK_LOG_URL_"
sys.env.filterKeys(_.startsWith(prefix))
Expand Down Expand Up @@ -188,13 +230,14 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
cores: Int,
appId: String,
workerUrl: Option[String],
userClassPath: mutable.ListBuffer[URL])
userClassPath: mutable.ListBuffer[URL],
resourceAddrs: Option[String])

def main(args: Array[String]): Unit = {
val createFn: (RpcEnv, Arguments, SparkEnv) =>
CoarseGrainedExecutorBackend = { case (rpcEnv, arguments, env) =>
new CoarseGrainedExecutorBackend(rpcEnv, arguments.driverUrl, arguments.executorId,
arguments.hostname, arguments.cores, arguments.userClassPath, env)
arguments.hostname, arguments.cores, arguments.userClassPath, env, arguments.resourceAddrs)
}
run(parseArguments(args, this.getClass.getCanonicalName.stripSuffix("$")), createFn)
System.exit(0)
Expand Down Expand Up @@ -255,6 +298,7 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
var executorId: String = null
var hostname: String = null
var cores: Int = 0
var resourceAddrs: Option[String] = None
var appId: String = null
var workerUrl: Option[String] = None
val userClassPath = new mutable.ListBuffer[URL]()
Expand All @@ -274,6 +318,9 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
case ("--cores") :: value :: tail =>
cores = value.toInt
argv = tail
case ("--resourceAddrs") :: value :: tail =>
resourceAddrs = Some(value)
argv = tail
case ("--app-id") :: value :: tail =>
appId = value
argv = tail
Expand All @@ -299,7 +346,7 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
}

Arguments(driverUrl, executorId, hostname, cores, appId, workerUrl,
userClassPath)
userClassPath, resourceAddrs)
}

private def printUsageAndExit(classNameForEntry: String): Unit = {
Expand All @@ -313,6 +360,7 @@ private[spark] object CoarseGrainedExecutorBackend extends Logging {
| --executor-id <executorId>
| --hostname <hostname>
| --cores <cores>
| --resourceAddrs <rtype1=count:unit:addr1,addr2;rtype2=count:unit:r2addr1,r2addr2...>
Copy link
Contributor Author

@tgravescs tgravescs Apr 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking about this some more I think I should make this json format so its more extensible in the future and not as ugly on the command line.

| --app-id <appid>
| --worker-url <workerUrl>
| --user-class-path <url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ import org.apache.spark.util.collection.unsafe.sort.UnsafeSorterSpillReader.MAX_

package object config {

private[spark] val SPARK_DRIVER_RESOURCE_PREFIX = "spark.driver.resource."
private[spark] val SPARK_EXECUTOR_RESOURCE_PREFIX = "spark.executor.resource."
private[spark] val SPARK_TASK_RESOURCE_PREFIX = "spark.task.resource."

private[spark] val SPARK_RESOURCE_COUNT_POSTFIX = ".count"
private[spark] val SPARK_RESOURCE_DISCOVERY_SCRIPT_POSTFIX = ".discoveryScript"

private[spark] val DRIVER_CLASS_PATH =
ConfigBuilder(SparkLauncher.DRIVER_EXTRA_CLASSPATH).stringConf.createOptional

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.scheduler.cluster

import java.nio.ByteBuffer

import org.apache.spark.ResourceInformation
import org.apache.spark.TaskState.TaskState
import org.apache.spark.rpc.RpcEndpointRef
import org.apache.spark.scheduler.ExecutorLossReason
Expand Down Expand Up @@ -64,7 +65,8 @@ private[spark] object CoarseGrainedClusterMessages {
hostname: String,
cores: Int,
logUrls: Map[String, String],
attributes: Map[String, String])
attributes: Map[String, String],
resources: Map[String, ResourceInformation])
extends CoarseGrainedClusterMessage

case class StatusUpdate(executorId: String, taskId: Long, state: TaskState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class CoarseGrainedSchedulerBackend(scheduler: TaskSchedulerImpl, val rpcEnv: Rp

override def receiveAndReply(context: RpcCallContext): PartialFunction[Any, Unit] = {

case RegisterExecutor(executorId, executorRef, hostname, cores, logUrls, attributes) =>
case RegisterExecutor(executorId, executorRef, hostname, cores, logUrls,
attributes, resources) =>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this isn't used anywhere at this point, the follow on jiras for scheduler will use it, this seemed like a good point to split the functionality.

if (executorDataMap.contains(executorId)) {
executorRef.send(RegisterExecutorFailed("Duplicate executor ID: " + executorId))
context.reply(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ class HeartbeatReceiverSuite
val dummyExecutorEndpointRef1 = rpcEnv.setupEndpoint("fake-executor-1", dummyExecutorEndpoint1)
val dummyExecutorEndpointRef2 = rpcEnv.setupEndpoint("fake-executor-2", dummyExecutorEndpoint2)
fakeSchedulerBackend.driverEndpoint.askSync[Boolean](
RegisterExecutor(executorId1, dummyExecutorEndpointRef1, "1.2.3.4", 0, Map.empty, Map.empty))
RegisterExecutor(executorId1, dummyExecutorEndpointRef1, "1.2.3.4", 0, Map.empty, Map.empty,
Map.empty))
fakeSchedulerBackend.driverEndpoint.askSync[Boolean](
RegisterExecutor(executorId2, dummyExecutorEndpointRef2, "1.2.3.5", 0, Map.empty, Map.empty))
RegisterExecutor(executorId2, dummyExecutorEndpointRef2, "1.2.3.5", 0, Map.empty, Map.empty,
Map.empty))
heartbeatReceiverRef.askSync[Boolean](TaskSchedulerIsSet)
addExecutorAndVerify(executorId1)
addExecutorAndVerify(executorId2)
Expand Down
Loading