Why your Spark app gets no YARN resources
Do not start with the settings. Start with the symptom. Run one command and read the state of the app. The state tells you which of five cases you have. Each case has one diagram, one cause and one fix.
Summary
- Each container that Spark asks for must pass four limits: the size of one container, the AM pool, the share of the queue for one user, and the free memory on one node.
- Each limit fails with a different symptom. The symptom tells you which setting to look at. You do not have to learn all the settings at once.
- In
yarn-clustermode the Spark driver is the ApplicationMaster (AM). AMs use a separate, small pool: 10% of the queue by default. - An Oozie Spark action uses two AMs: the Oozie launcher and the Spark driver. The launcher keeps its AM for the full life of the Spark job. This can lock the AM pool.
Start here
Read the spark-submit output first. If spark-submit did not fail, run yarn application -status <appId> and read State and Diagnostics. Then select your case in the diagram.
Case 1: One container is too big
fails at onceWhat you see
spark-submit stops before the app starts. The error is Required executor memory (...) is above the max threshold (... MB) of this cluster! The same check applies to the driver AM.
Why
YARN does not give one container more than the cap. Spark asks for memory plus overhead, so --executor-memory must be smaller than the cap.
Fix
Make the executor or driver smaller, and use more executors. Or ask the admin to increase the cap.
yarn.scheduler.maximum-allocation-mb, yarn.scheduler.capacity.<queue-path>.maximum-allocation-mb
Case 2: The AM pool is full
ACCEPTEDWhat you see
State is ACCEPTED for a long time. Diagnostics: Queue's AM resource limit exceeded or User's AM resource limit exceeded. The queue does not look full.
Why
All AMs in a queue share a small pool: 10% of the queue by default. Normal containers cannot use this pool, and AMs cannot use the rest of the queue.
Fix
- Find and kill old or stuck apps in the queue.
- Use a smaller
spark.driver.memory. - Ask the admin to increase
yarn.scheduler.capacity.<queue-path>.maximum-am-resource-percent(for example to 0.3).
Case 3: Oozie launchers lock the AM pool
ACCEPTED, foreverWhat you see
The Oozie launcher app is RUNNING. Its Spark app is ACCEPTED with "AM resource limit exceeded". It often starts when several workflows start at the same time.
Why
One Oozie Spark action uses two AMs. The launcher (2 GB by default) keeps its AM until the Spark job ends. In yarn-cluster mode the Spark driver is the second AM.
Fix
- Send the launchers to a different queue:
<launcher><queue>oozie</queue></launcher>in the action. - Make the launcher smaller:
oozie.launcher.memory.mb(older name:oozie.launcher.yarn.app.mapreduce.am.resource.mb). It only runs spark-submit. - Increase the AM percent of the queue (Case 2).
Case 4: You reached your user limit
RUNNING, few executorsWhat you see
The app is RUNNING with fewer executors than you asked for. Your usage stops at the queue capacity. Other parts of the cluster are idle.
Why
With user-limit-factor = 1, one user can never use more than the queue capacity, "irrespective of how idle the cluster is". On EMR most jobs run as the user hadoop, so all of them share this one limit.
Fix
First, make sure that you did not set the limit yourself: check spark.executor.instances and spark.dynamicAllocation.maxExecutors.
Then ask the admin to increase yarn.scheduler.capacity.<queue-path>.user-limit-factor (for example to 2), or use a queue with more capacity. Check that maximum-capacity is also larger than capacity.
Case 5: No single node has room
RUNNING, executors waitWhat you see
The app is RUNNING. Some executors start, others wait. The queue and the user are below their limits.
Why
Each node gives YARN only yarn.nodemanager.resource.memory-mb. Large containers need one node with that much free memory. The free memory is in small parts on many nodes.
Fix
Use smaller executors (more of them), wait for other apps to end, or add nodes. Run yarn node -list -showDetails to see the free memory on each node.
How the five cases fit together
Each container request goes through four checks in this order. The Oozie case is Check 2 with two AMs for each job.
Commands to find the case
# 1. State and Diagnostics of one app (Cases 2, 3) yarn application -status <appId> # 2. All apps that wait or run, to see who uses the queue (Cases 2, 3, 4) yarn application -list -appStates ACCEPTED,RUNNING # 3. Capacity and usage of a queue (Case 4) yarn queue -status <queue> # 4. Free memory on each node (Case 5) yarn node -list -showDetails
The ResourceManager web UI shows the same data. The Scheduler page shows each queue with its AM limit and user limit.
See all these settings on your EMR cluster
The script yarn_resource_config.py reads yarn-site.xml, capacity-scheduler.xml, spark-defaults.conf and oozie-site.xml. It shows each setting with its value, its meaning and its case. On EMR it also shows the cluster ID and the instance groups, and it reads the AM pool, the queue usage and the free memory on each node from the ResourceManager. It uses only Python 3 and changes nothing.
Option 1: on the primary node. Connect with SSM or SSH, then run:
aws ssm start-session --target <primary-node-instance-id> # or: ssh hadoop@<primary-node-dns>
curl -fsSLO https://github.com/ryandam9/yarn-resource-config/releases/latest/download/yarn_resource_config.py
python3 yarn_resource_config.py | less
If the node has no internet access, copy the script through S3 with aws s3 cp.
Option 2: as an EMR step. The step runs on the primary node. Read the output in the console (Steps, then stdout) or in the step's log folder in S3.
aws s3 cp yarn_resource_config.py s3://<bucket>/tools/ aws emr add-steps --cluster-id j-XXXXXXXX --steps \ 'Type=CUSTOM_JAR,Name=yarn-resource-config,ActionOnFailure=CONTINUE,Jar=command-runner.jar,Args=[bash,-c,"aws s3 cp s3://<bucket>/tools/yarn_resource_config.py /tmp/ && python3 /tmp/yarn_resource_config.py"]'
All related settings, by case
Look at a setting only when a case points to it. On EMR, read the real values in /etc/hadoop/conf/. EMR sets the node memory for each instance type, so the Hadoop defaults do not apply.
| Setting | What it limits | File | Case |
|---|---|---|---|
| Spark: what your app asks for (all cases) | |||
spark.executor.memoryspark.driver.memory | Heap of each executor and of the driver. In yarn-cluster mode the driver memory is the AM size. | spark-submit, spark-defaults.conf | 1, 2, 5 |
spark.executor.memoryOverheadspark.driver.memoryOverheadspark.executor.memoryOverheadFactor | Memory that Spark adds to each container. Default: max(384 MB, 0.10 × memory). | spark-submit | 1 |
spark.executor.pyspark.memory | Memory for Python workers. Spark adds it to the executor request when you set it. Not set by default. | spark-submit | 1 |
spark.executor.coresspark.driver.cores | vcores for each container. They count only when the scheduler uses DominantResourceCalculator. | spark-submit | 1 |
spark.executor.instancesspark.dynamicAllocation.maxExecutors | Your own limit on the number of executors. Check these first when you see few executors. Default for maxExecutors: no limit. | spark-submit | 4 |
spark.yarn.am.memory | AM size in yarn-client mode only (default 512m). The driver then runs outside YARN. | spark-submit | 2 |
spark.yarn.queue | The YARN queue for the app. Default: default. | spark-submit | 2, 4 |
| Case 1: size of one container | |||
yarn.scheduler.maximum-allocation-mbyarn.scheduler.maximum-allocation-vcores | Largest one container in the cluster. Hadoop defaults: 8192 MB, 4 vcores. | yarn-site.xml | 1 |
yarn.scheduler.capacity.<queue-path>.maximum-allocation-mbyarn.scheduler.capacity.<queue-path>.maximum-allocation-vcores | Largest one container in this queue. Must not be more than the cluster cap. | capacity-scheduler.xml | 1 |
yarn.scheduler.minimum-allocation-mbyarn.scheduler.minimum-allocation-vcores | Smallest container. YARN rounds each request up to a multiple of this value. Defaults: 1024 MB, 1 vcore. | yarn-site.xml | 1, 5 |
yarn.scheduler.capacity.maximum-applicationsyarn.scheduler.capacity.<queue-path>.maximum-applications | Number of running and waiting apps. Above it, YARN rejects the new app at submit. Default 10000. | capacity-scheduler.xml | 1 |
| Cases 2 and 3: the AM pool and the number of apps | |||
yarn.scheduler.capacity.maximum-am-resource-percent | Share of each queue for all AMs. Default 0.1 (10%). | capacity-scheduler.xml | 2, 3 |
yarn.scheduler.capacity.<queue-path>.maximum-am-resource-percent | The same share, for one queue. | capacity-scheduler.xml | 2, 3 |
yarn.scheduler.capacity.max-parallel-appsyarn.scheduler.capacity.<queue-path>.max-parallel-appsyarn.scheduler.capacity.user.max-parallel-apps | Number of apps that run at the same time. More apps stay in ACCEPTED. Default: no limit. | capacity-scheduler.xml | 2 |
yarn.node-labels.am.default-node-label-expressionspark.yarn.am.nodeLabelExpression | Nodes where AMs can run. Some EMR releases set it to CORE, so AMs run only on core nodes. Check your yarn-site.xml. | yarn-site.xml, spark-submit | 2 |
oozie.launcher.memory.mboozie.launcher.yarn.app.mapreduce.am.resource.mb (older name) | Memory of the Oozie launcher AM for one action. | workflow.xml (action configuration) | 3 |
oozie.launcher.vcores | vcores of the Oozie launcher AM for one action. | workflow.xml (action configuration) | 3 |
oozie.launcher.queue or <launcher><queue> | Queue of the Oozie launcher. Use a different queue from the Spark job. | workflow.xml | 3 |
oozie.launcher.default.memory.mboozie.launcher.default.vcoresoozie.launcher.default.queue | Server defaults for all launchers: 2048 MB, 1 vcore, queue default. | oozie-site.xml | 3 |
| Case 4: share of the queue | |||
yarn.scheduler.capacity.<queue-path>.capacity | Guaranteed share of the parent queue, in %. The AM pool and the user limit are calculated from it. | capacity-scheduler.xml | 2, 4 |
yarn.scheduler.capacity.<queue-path>.maximum-capacity | Largest share the queue can grow to when other queues are idle. Default: no limit (100%). | capacity-scheduler.xml | 4 |
yarn.scheduler.capacity.<queue-path>.user-limit-factor | How many times the queue capacity one user can use. Default 1. | capacity-scheduler.xml | 4 |
yarn.scheduler.capacity.<queue-path>.minimum-user-limit-percent | Smallest share for each user when many users wait. With 25 and two users, each user gets at most 50%. Default 100. | capacity-scheduler.xml | 4 |
| Case 5: size of each node | |||
yarn.nodemanager.resource.memory-mbyarn.nodemanager.resource.cpu-vcores | Memory and vcores that each node gives to YARN. | yarn-site.xml | 5 |
yarn.scheduler.capacity.resource-calculator | DefaultResourceCalculator (default) uses memory only. DominantResourceCalculator also uses vcores. | capacity-scheduler.xml | 1, 5 |
To change a capacity-scheduler.xml value without a restart, edit the file on the primary node and run yarn rmadmin -refreshQueues. On EMR you can also set the values with the yarn-site and capacity-scheduler configuration classifications.
Checked against: Hadoop 3.3.6 yarn-default.xml, CapacityScheduler.md and LeafQueue.java (AM limit, "at least one application" rule); Spark 3.5.1 Client.scala (max threshold check); Oozie oozie-default.xml, JavaActionExecutor.java and LauncherAM.java (launcher memory and queue). The EMR notes come from general EMR knowledge.