@@ -2667,23 +2667,25 @@ object SparkContext extends Logging {
26672667
26682668 // SPARK-26340: Ensure that executor's core num meets at least one task requirement.
26692669 def checkCpusPerTask (
2670- executorCoreNum : Int = sc.conf.get( EXECUTOR_CORES ) ,
2671- clusterMode : Boolean = true ): Unit = {
2670+ clusterMode : Boolean ,
2671+ maxCoresPerExecutor : Option [ Int ] ): Unit = {
26722672 val cpusPerTask = sc.conf.get(CPUS_PER_TASK )
2673- if (executorCoreNum < cpusPerTask) {
2674- val message = if (clusterMode) {
2675- s " ${CPUS_PER_TASK .key} must be <= ${EXECUTOR_CORES .key} when run on $master. "
2676- } else {
2677- s " Only $executorCoreNum cores available per executor when run on $master, " +
2678- s " and ${CPUS_PER_TASK .key} must be <= it. "
2673+ if (clusterMode && sc.conf.contains(EXECUTOR_CORES )) {
2674+ if (sc.conf.get(EXECUTOR_CORES ) < cpusPerTask) {
2675+ throw new SparkException (s " ${CPUS_PER_TASK .key}" +
2676+ s " must be <= ${EXECUTOR_CORES .key} when run on $master. " )
2677+ }
2678+ } else if (maxCoresPerExecutor.isDefined) {
2679+ if (maxCoresPerExecutor.get < cpusPerTask) {
2680+ throw new SparkException (s " Only ${maxCoresPerExecutor.get} cores available per executor " +
2681+ s " when run on $master, and ${CPUS_PER_TASK .key} must be <= it. " )
26792682 }
2680- throw new SparkException (message)
26812683 }
26822684 }
26832685
26842686 master match {
26852687 case " local" =>
2686- checkCpusPerTask(1 , clusterMode = false )
2688+ checkCpusPerTask(clusterMode = false , Some ( 1 ) )
26872689 val scheduler = new TaskSchedulerImpl (sc, MAX_LOCAL_TASK_FAILURES , isLocal = true )
26882690 val backend = new LocalSchedulerBackend (sc.getConf, scheduler, 1 )
26892691 scheduler.initialize(backend)
@@ -2696,7 +2698,7 @@ object SparkContext extends Logging {
26962698 if (threadCount <= 0 ) {
26972699 throw new SparkException (s " Asked to run locally with $threadCount threads " )
26982700 }
2699- checkCpusPerTask(threadCount, clusterMode = false )
2701+ checkCpusPerTask(clusterMode = false , Some (threadCount) )
27002702 val scheduler = new TaskSchedulerImpl (sc, MAX_LOCAL_TASK_FAILURES , isLocal = true )
27012703 val backend = new LocalSchedulerBackend (sc.getConf, scheduler, threadCount)
27022704 scheduler.initialize(backend)
@@ -2707,22 +2709,22 @@ object SparkContext extends Logging {
27072709 // local[*, M] means the number of cores on the computer with M failures
27082710 // local[N, M] means exactly N threads with M failures
27092711 val threadCount = if (threads == " *" ) localCpuCount else threads.toInt
2710- checkCpusPerTask(threadCount, clusterMode = false )
2712+ checkCpusPerTask(clusterMode = false , Some (threadCount) )
27112713 val scheduler = new TaskSchedulerImpl (sc, maxFailures.toInt, isLocal = true )
27122714 val backend = new LocalSchedulerBackend (sc.getConf, scheduler, threadCount)
27132715 scheduler.initialize(backend)
27142716 (backend, scheduler)
27152717
27162718 case SPARK_REGEX (sparkUrl) =>
2717- checkCpusPerTask()
2719+ checkCpusPerTask(clusterMode = true , None )
27182720 val scheduler = new TaskSchedulerImpl (sc)
27192721 val masterUrls = sparkUrl.split(" ," ).map(" spark://" + _)
27202722 val backend = new StandaloneSchedulerBackend (scheduler, sc, masterUrls)
27212723 scheduler.initialize(backend)
27222724 (backend, scheduler)
27232725
27242726 case LOCAL_CLUSTER_REGEX (numSlaves, coresPerSlave, memoryPerSlave) =>
2725- checkCpusPerTask()
2727+ checkCpusPerTask(clusterMode = true , Some (coresPerSlave.toInt) )
27262728 // Check to make sure memory requested <= memoryPerSlave. Otherwise Spark will just hang.
27272729 val memoryPerSlaveInt = memoryPerSlave.toInt
27282730 if (sc.executorMemory > memoryPerSlaveInt) {
@@ -2743,7 +2745,7 @@ object SparkContext extends Logging {
27432745 (backend, scheduler)
27442746
27452747 case masterUrl =>
2746- checkCpusPerTask()
2748+ checkCpusPerTask(clusterMode = true , None )
27472749 val cm = getClusterManager(masterUrl) match {
27482750 case Some (clusterMgr) => clusterMgr
27492751 case None => throw new SparkException (" Could not parse Master URL: '" + master + " '" )
0 commit comments