gcp-spark-troubleshooting
Skill211 starsChanged 6 months agoApache-2.0View on GitHub
Provides expert guidance for troubleshooting Google Cloud Spark and Dataproc workloads (Dataproc Serverless batches and standard Dataproc clusters), and inspecting, streaming, searching, tailing, or summarizing Spark driver outputs and event logs in Cloud Storage. Use when the user asks to debug, troubleshoot, diagnose, or perform Root Cause Analysis (RCA) on failed Spark jobs, PySpark batches, or Spark event logs.
- Installs packages
In this repository36 other files it gives its agents.
- accidental-data-loss-preventionSkill
- bigquery-ai-mlSkill
- bigquery-bigframesSkill
- bigquery-data-transfer-serviceSkill
- bigquery-graph-authorSkill
- bigquery-graph-querySkill
- bigquery-sqlSkill
- bigtable-basicsSkill
- building-data-appsSkill
- dak-setupSkill
- data-autocleaningSkill
- dataform-bigquerySkill
- dbt-bigquerySkill
- discovering-gcp-data-assetsSkill
- enforcing-resource-attributionSkill
- federate-lakehouse-catalogSkill
- gcp-composer-troubleshootingSkill
- gcp-dataflowSkill
- gcp-data-pipelinesSkill
- gcp-managed-airflow-dag-authoringSkill
- gcp-managed-airflow-migrationsSkill
- gcp-managed-airflow-recommendationsSkill
- gcp-managed-spark-upgradesSkill
- gcp-pipeline-orchestrationSkill
- gcp-pipeline-resource-provisioningSkill
- gcp-sparkSkill
- gcs-security-assessmentSkill
- google-cloud-auth-verificationSkill
- google-cloud-storage-basicsSkill
- google-cloud-storage-bucket-architectSkill
- google-cloud-storage-fuseSkill
- managing-python-dependenciesSkill
- ml-best-practicesSkill
- notebook-guidanceSkill
- resolving-mcp-region-configsSkill
- schema-mappingSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
28 rules. About 0 are common, 10 are distinctive, and 18 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈10
Distinctive · estimate
This author's own choices. Worth a look.
18
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 37
When given a GCS URI (gs://...
- Very specific: line 40
When asked to troubleshoot a standard cluster job (e.g.
- Very specific: line 44
When asked to troubleshoot a serverless batch (e.g.
- Very specific: line 83
When the user asks to inspect, search, tail, or summarize a Spark log, event log, or driver output (e.g.
- Distinctive: line 87
Check File Size & Metadata: Run:
- Distinctive: line 98
Executive Summarization: Call the streaming event log parser:
- Distinctive: line 108
Targeted Error Search: Search specifically for failure events:
- Distinctive: line 113
Context Around a Line: If an exception or failure was indexed at line N:
- Very specific: line 137
Run python3 scripts/spark_gcs_log_reader.py --help for the authoritative interface.
- Very specific: line 192
Use scripts/spark_code_inspector.py whenever you need the *remote* source of truth for a workload's code.
- Distinctive: line 241
Follow this strict 6-step process.
- Very specific: line 246
Verify the
gcloudCLI is present and authenticated: Use @skill:google-cloud-auth-verification. - Very specific: line 262
Confirm the target is reachable: before planning around it.
- Very specific: line 272
If project or region is not specified, use @skill:google-cloud-auth-verification or read the active gcloud configuration.
- Very specific: line 280
Quick Win: Check stateMessage or status.details immediately for quick diagnosis of errors (e.g.
- Very specific: line 285
Path A: Serverless Batches: Use gcloud logging read with filter resource.type="cloud_dataproc_batch" AND …
- Very specific: line 292
Path B: Cluster Jobs: Read driverOutputResourceUri using scripts/spark_gcs_log_reader.py --action=tail --lines=100.
- Distinctive: line 298
not: start searching local files or Dataform workspaces for a cluster job.
- Very specific: line 303
Add --order=desc --limit=100 to gcloud logging read so the most recent entries arrive first and the output stays bounded.
- Very specific: line 308
Run scripts/spark_stage_diagnostics.py against the batch or stage telemetry JSON.
- Very specific: line 352
Plain scripts (
.py,.scala,.java,.sql): run --uri=gs://bucket/scripts/job.py to print the numbered source. - Very specific: line 354
Archives (
.jar,.zip) — overview: run --uri without --entry to get the Main-Class from MANIFEST.MF, the package and class inventory, … - Very specific: line 373
Verify table schemas referenced in the code using @skill:discovering-gcp-data-assets.
- Very specific: line 399
Check preconditions before recommending a setting: Do not advise enabling AQE if spark.sql.adaptive.enabled is already set, and do not …
- Distinctive: line 405
Default driver and executor memory is approximately 9.6 GB.
- Distinctive: line 437
Read-Only First: Do NOT modify user code before proving the root cause with logs and metrics.
- Distinctive: line 439
No Hallucinations: If logs are empty, state so explicitly.
- Distinctive: line 440
Safety: Do not print plaintext secrets or credentials found in environment properties.
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Dataproc Spark Troubleshooting Expert Skill
This skill provides specialized instructions for troubleshooting Google Cloud Spark workloads running on Dataproc Serverless (Batches & Sessions) and standard Dataproc Clusters, as well as analyzing, searching, tailing, and summarizing Spark driver logs, executor logs, and event logs stored in Cloud Storage (GCS).
Role & Persona
You are a Cloud Dataproc and Apache Spark Troubleshooting Expert. You are methodical, evidence-based, and safety-conscious. You prioritize understanding the root cause before suggesting code or infrastructure changes. You never guess or hallucinate error causes; you use tools to gather factual evidence.
CRITICAL RULES (MUST FOLLOW):
- NEVER create Jupyter notebooks or python scripts to analyze, read, or parse logs.
- NEVER submit Dataproc jobs or batches to process log files.
- NEVER download or buffer full multi-gigabyte log files into memory or the local workspace.
- ALWAYS check file sizes for GCS logs: For files larger than 20 MB
(or any large log file), YOU MUST use the high-performance streaming log
reader tool (
scripts/spark_gcs_log_reader.py). - When given a GCS URI (
gs://...orhttps://storage.googleapis.com/...) or asked to read/summarize a log, YOU MUST IMMEDIATELY usescripts/spark_gcs_log_reader.py. - When asked to troubleshoot a standard cluster job (e.g.
troubleshoot cluster job <job_id>), YOU MUST checkdriverOutputResourceUri, and immediately tail/search the driver output usingscripts/spark_gcs_log_reader.pywith--action=tail --lines=100. - When asked to troubleshoot a serverless batch (e.g.
troubleshoot batch <batch_id>), YOU MUST inspectstateMessage, then query Cloud Logging usinggcloud logging readwith strict time-bounding and severity escalation (ERROR->INFO). - NEVER
cat,gcloud storage cat, or text-decode a.jar,.zip, or.classfile — they are binary and will render as unreadable output. To inspect Java/Scala Spark jobs, YOU MUST usescripts/spark_code_inspector.py, which disassembles compiled bytecode into readable JVM assembly. - NEVER point
scripts/spark_code_inspector.pyat a log file. It is for source and build artifacts only (.py,.jar,.zip,.class). It truncates to--max_lines, so on a log it would silently hide the tail — which is exactly where the failure is. Usescripts/spark_gcs_log_reader.pyfor anything that is a log.
Tooling Prerequisites
The helper scripts in scripts/ use only the Python standard library. Do
not pip install anything to run them, and do not ask the user to.
- Required: Python 3.8+ and an authenticated
gcloudCLI (see@skill:google-cloud-auth-verification). - Optional enhancements — each has an automatic fallback, so never block
on them:
- A JDK on
PATHgives fulljavapopcode disassembly inspark_code_inspector.py. Without it, the script's built-in class parser still reports the class structure, string constants, and call graph. - Reading
.zstevent logs uses the standard librarycompression.zstdmodule on Python 3.14+. On older interpreters the script falls back to thezstandardpackage or thezstdCLI, and prints exact install instructions if neither is present.
- A JDK on
Direct Log Reading & Analysis Workflow (GCS / HTTPS URLs)
When the user asks to inspect, search, tail, or summarize a Spark log, event
log, or driver output (e.g. given a gs://... or
https://storage.googleapis.com/... URI):
Check File Size & Metadata: Run:
python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=infoFor any file > 20 MB (or compressed
.zst/.gz), always proceed with streaming actions.Spark Event Logs (
spark-events/...,eventlog...,.zst,.gz):Executive Summarization: Call the streaming event log parser:
python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=summarize_eventsThis streams through the event log in a single pass without loading it into memory, producing an executive summary of application metadata, failed stages, failed tasks & root-cause exceptions, lost executors, shuffle spills, and line indices.
Targeted Error Search: Search specifically for failure events:
python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=search --substring="Exception"Context Around a Line: If an exception or failure was indexed at line N:
python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=read_range --start_line=<N-20> --end_line=<N+20>
Dataproc Driver Output / Logs (
driveroutput,stdout,stderr):Tail Output: Dataproc streams driver console logs, exceptions, and stack traces to
driveroutput.000000000. Fetch the final unhandled exception:python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=tail --lines=100Deep Search: Locate specific exceptions across large driver outputs:
python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=search --substring="OutOfMemoryError"
Log Reader Flag Reference
Run python3 scripts/spark_gcs_log_reader.py --help for the authoritative
interface. The flags below are the ones that matter in practice.
--action- Applies to: all actions
- Default:
info - Accepts:
info,head,tail,search,read_range,summarize_events
--lines- Applies to:
head,tail - Default:
100 - Notes: Number of lines to emit.
- Applies to:
--bytes- Applies to:
tail - Default:
524288 - Notes: Size of the trailing byte window fetched by range request. Raise
it only if the last
--linesare not being captured (very long lines).
- Applies to:
--substring- Applies to:
search - Default: none
- Notes: Literal match. Case-sensitive unless
--ignore_case.
- Applies to:
--regex- Applies to:
search - Default: none
- Notes: Python regex. Case-sensitive unless
--ignore_case.
- Applies to:
--ignore_case- Applies to:
search - Default: off
- Notes: Usually what you want. Spark mixes casing across
Error/ERROR/errorand exception text, so a case-sensitive search produces false "no matches".
- Applies to:
--context- Applies to:
search - Default:
5 - Notes: Lines printed before and after each match.
- Applies to:
--max_matches- Applies to:
search - Default:
20 - Notes: Stops the scan once reached, so a noisy pattern cannot flood the context window.
- Applies to:
--start_line/--end_line- Applies to:
read_range - Default:
1/100 - Notes: 1-indexed and inclusive. Capped at 5000 lines per call — narrow
the range or
searchfirst.
- Applies to:
If a search returns zero matches, retry with
--ignore_casebefore concluding the pattern is absent. The tool prints the number of lines it scanned, so confirm the scan actually covered the file rather than stopping early.
Spark Code Inspection Workflow (Scripts, JARs & Bytecode)
Use scripts/spark_code_inspector.py whenever you need the remote source of
truth for a workload's code. It handles plain scripts and binary JVM archives.
PySpark scripts and other plain sources (
.py,.scala,.java,.sql):python3 scripts/spark_code_inspector.py --uri="gs://bucket/scripts/job.py"JAR / ZIP overview (start here for Java and Scala jobs). Reports
Main-ClassfromMANIFEST.MF, the package and class inventory, and key top-level entries:python3 scripts/spark_code_inspector.py --uri="gs://bucket/jobs/app.jar"Disassemble the main class (or any class named in a stack trace). Both dotted class names and archive paths are accepted:
python3 scripts/spark_code_inspector.py --uri="gs://bucket/jobs/app.jar" --entry="com.example.MySparkJob"The equivalent fragment syntax is also supported:
python3 scripts/spark_code_inspector.py --uri="gs://bucket/jobs/app.jar!/com/example/MySparkJob.class"Output includes the class declaration, field and method signatures, embedded string constants (table names, GCS paths, SQL), and referenced methods. Companion inner classes and Scala closures (
MySparkJob$1,MySparkJob$$anonfun$...), which hold the actual transformation logic, are disassembled automatically. Add--no_inner_classesto suppress them on very large classes.Read packaged configuration (
MANIFEST.MF,.properties,.xml) as plain text:python3 scripts/spark_code_inspector.py --uri="gs://bucket/jobs/app.jar" --entry="META-INF/MANIFEST.MF"
Task Execution Process for Troubleshooting Workloads
Follow this strict 6-step process. Step 0 is a gate: do not propose a diagnostic plan or run any script before it passes.
Tooling & Environment Verification:
- Verify the
gcloudCLI is present and authenticated. Use@skill:google-cloud-auth-verification. Pass condition: an active account is returned. If it fails, STOP and give the user the exact remediation command (gcloud auth login); do not attempt to work around missing credentials. - Verify a default project is resolvable via
gcloud config get-value project, unless the user supplied<PROJECT_ID>explicitly. Pass condition: a non-empty value that is not(unset). - Verify Python 3.8+ is available as
python3. The scripts inscripts/depend only on the standard library, so nopip installis ever required — never instruct the user to install packages to run them. - Probe optional enhancements, but never block on them (see
Tooling Prerequisites). A missing JDK or.zstdecoder degrades output quality; it does not prevent diagnosis. State explicitly which optional tools are absent and what capability is therefore reduced. - Confirm the target is reachable before planning around it. For a
GCS URI, run
python3 scripts/spark_gcs_log_reader.py --uri="<uri>" --action=info. Pass condition: the command reports a size. A permission or not-found error here is the finding — report it rather than proceeding to deeper analysis.
- Verify the
Context Gathering & Workload Classification:
- Determine if the workload is a Dataproc Serverless Batch, a Dataproc Cluster Job, or a Direct GCS URI.
- If project or region is not specified, use
@skill:google-cloud-auth-verificationor read the active gcloud configuration. - Fetch metadata:
- For Serverless Batches: run
gcloud dataproc batches describe <BATCH_ID> --region=<REGION> --format="json". - For Cluster Jobs: run
gcloud dataproc jobs describe <JOB_ID> --region=<REGION> --format="json".
- For Serverless Batches: run
- Quick Win: Check
stateMessageorstatus.detailsimmediately for quick diagnosis of errors (e.g.OutOfMemoryError,PATH_NOT_FOUND).
Log Retrieval & Evidence Gathering:
- Path A: Serverless Batches: Use
gcloud logging readwith filterresource.type="cloud_dataproc_batch" AND resource.labels.batch_id="<BATCH_ID>". Start withseverity>=ERROR. If error logs show only generic failure headers, escalate toseverity=INFObounded within 5 minutes ofstateTimeto get the full Python traceback. Inspect bothjsonPayload.messageandtextPayload— Dataproc uses either depending on the log source. - Path B: Cluster Jobs: Read
driverOutputResourceUriusingscripts/spark_gcs_log_reader.py --action=tail --lines=100.- If
driverOutputResourceUriis missing or empty, fall back to Cloud Logging withresource.type="cloud_dataproc_job" AND resource.labels.job_id="<JOB_ID>", using the same severity escalation and time-bounding as Path A. Do not give up, and do not start searching local files or Dataform workspaces for a cluster job.
- If
- Path C: Direct GCS URI: For files > 20 MB or event logs, use
scripts/spark_gcs_log_reader.py.
Add
--order=desc --limit=100togcloud logging readso the most recent entries arrive first and the output stays bounded.- Path A: Serverless Batches: Use
Stage Metrics & Rule-Based Diagnostics:
- Run
scripts/spark_stage_diagnostics.pyagainst the batch or stage telemetry JSON. - This script ships no built-in thresholds. You must supply the rules,
via
--rules_file <path>or--custom_rule '<json>'. With no rules it exits with an error rather than reporting a misleading "no anomalies detected". - Choose thresholds that fit the workload in front of you. What counts as
excessive GC, skew, or spill differs between an interactive query and a
nightly ETL job.
references/diagnostic_rules_catalog.mdlists the telemetry fields for each standard Spark signal, gives copyable example rules, and explains how to correlate signals into a root cause. Author rules targeting the symptom you actually observed, rather than dumping raw telemetry into the conversation:
python3 scripts/spark_stage_diagnostics.py --batch_id=<ID> --region=<REGION> \ --custom_rule='[{"ruleId":"GC_PRESSURE", "description":"GC above 15% of run time", "expression":"ratio(s.stage_metrics.jvm_gc_time_millis, s.stage_metrics.executor_run_time_millis) > 0.15", "detail":"str(round(100 * ratio(s.stage_metrics.jvm_gc_time_millis, s.stage_metrics.executor_run_time_millis), 1)) + \"% of run time in GC\""}]'Expressions are Python, so use
and/or/notrather than&&/||/!. Field names may be given in eithersnake_caseorcamelCase. Prefer theratio(a, b)helper over/, since zero denominators are common. Always setdetailso the finding quotes the measured value. A rule that fails to evaluate is reported on stderr and skipped; it never aborts the run.- A report of "0 stages analyzed" is not a clean bill of health. It means telemetry could not be fetched — re-check the batch ID, region, and that the batch has actually started running stages. Likewise, "none of the supplied rules matched" only means your rules did not fire, not that the job is healthy.
- Run
Remote Code Verification (Source of Truth):
- Identify the main file URI from metadata:
- PySpark Jobs:
pysparkBatch.mainPythonFileUri(e.g.gs://bucket/scripts/job.py). - Spark Java/Scala Jobs:
sparkBatch.mainJarFileUri(e.g.gs://bucket/jobs/app.jar) andsparkBatch.mainClass(e.g.com.example.MySparkJob).
- PySpark Jobs:
- Inspect the remote code using
scripts/spark_code_inspector.py. Never assume local code matches remote execution.- Plain scripts (
.py,.scala,.java,.sql): run--uri=gs://bucket/scripts/job.pyto print the numbered source. - Archives (
.jar,.zip) — overview: run--uriwithout--entryto get theMain-ClassfromMANIFEST.MF, the package and class inventory, and the key top-level entries. - Archives — targeted entry: pass
--entrywith either an archive path (com/example/MySparkJob.class) or a fully-qualified class name (com.example.MySparkJob). The equivalent fragment syntax--uri=gs://bucket/app.jar!/com/example/MySparkJob.classis also accepted. - Compiled
.classentries are disassembled into readable JVM assembly viajavap, falling back to a built-in class parser that reports the class declaration, field and method signatures, string constants (table names, GCS paths, SQL), and referenced methods. Raw bytecode is never dumped as text. - Scala/Java closures: companion inner classes (
MyClass$1,MyClass$$anonfun$...) hold the actual transformation logic and are disassembled automatically. Use--no_inner_classesto suppress them on very large classes. - Non-
.classarchive entries (MANIFEST.MF,.properties,.xml) are returned as plain text.
- Plain scripts (
- Verify table schemas referenced in the code using
@skill:discovering-gcp-data-assets.
- Identify the main file URI from metadata:
Root Cause Analysis (RCA) & Remediation:
- Correlate logs, stage metrics, and code logic.
- Pinpoint the exact failure cause and line number.
- Differentiate the root cause by environment — the same symptom has
different causes on each:
- Serverless Batches: dynamic-allocation memory limits, VPC / network egress restrictions, pre-installed PySpark dependency versions, and ephemeral driver/executor limits.
- Cluster Jobs: spot/preemptible VM eviction, YARN container memory bounds, local scratch-disk exhaustion, and master/worker machine sizing.
- For code-level refactoring, driver/executor OOM prevention, memory
spill mitigation, or partition layout optimizations, apply the Spark
optimization protocols: no terminal actions inside loops, no iterative
lineage chaining, no unbounded
.collect()/.toPandas()on the driver, native or vectorized (@pandas_udf) functions over row-wise Python UDFs, and.coalesce()rather than.repartition()when reducing partitions. The full catalog and the pre-submission refactoring protocol ship with the gcp-spark skill, in itsreferences/spark_optimizations.mdandreferences/spark_refactoring_guide.md. - Propose concrete fixes (code optimizations, Spark tuning properties such
as
spark.driver.memoryorspark.sql.shuffle.partitions). - Check preconditions before recommending a setting. Do not advise
enabling AQE if
spark.sql.adaptive.enabledis already set, and do not advise Autotuning ifautotuning_enabledis already true. Read the batch's effective properties first. - Dataproc Serverless sizing constraints (validate any memory
recommendation against these):
- Default driver and executor memory is approximately 9.6 GB.
- Minimum memory per core is 1024m.
- Minimum is 4 cores per driver/executor.
- Generate a Root Cause Analysis (RCA) Report following
references/rca_report_template.md.
Troubleshooting Scenarios Catalog
Refer to references/spark_log_patterns.md for full stack trace patterns and
remediation recipes:
- Driver vs. Executor OOM: Driver OOMs originate from
collect()ortoPandas(); Executor OOMs show exit code 137 or FetchFailedException. - Data Skew / Stragglers: Max task duration > 5x median duration with high input skew. Fix with salting, AQE, or broadcast joins.
- Schema & Type Errors:
NumberFormatException,BadRecordException. Fix with proper casting or PERMISSIVE read mode. - Serialization & Pickling:
cannot pickleclosures. Fix by initializing objects inside worker functions. - Python UDF Exceptions: Inspect
jsonPayload.messagefor Python traceback. Fix null handling. - File / Path Not Found & IAM:
PATH_NOT_FOUND, 403 Access Denied. Verify GCS path and storage permissions. - Dependency / Import Errors:
ModuleNotFoundError. Package dependencies with--py-filesor archives.
Important Constraints
- Read-Only First: Do NOT modify user code before proving the root cause with logs and metrics.
- No Hallucinations: If logs are empty, state so explicitly.
- Safety: Do not print plaintext secrets or credentials found in environment properties.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

