利用 Gemini、MCP 与 Agent 技能调试无服务器 Apache Spark
使用 Gemini 调试无服务器版 Apache Spark:从将错误回溯信息粘贴到聊天窗口,到通过 MCP 服务器读取批量日志并修复 PySpark 问题。
任何大规模运行 Apache Spark 的人都知道,要查明批处理作业为何失败是一件极其繁琐的工作。真正的原因往往隐藏在数百行执行器日志、JVM 堆栈跟踪信息以及 Cloud Logging 数据流中。最终不得不在多个浏览器标签页之间切换,浏览 Cloud Storage 中的路径,并在笔记本电脑上手动编辑 PySpark 脚本。
生成式模型可以加快这一过程,但它们的实际帮助程度取决于一个关键因素:它们能够获取的上下文信息。以下内容将通过 Apache Spark 托管服务、Antigravity CLI 以及谷歌的远程 Spark 模型上下文协议(MCP)服务器,介绍从无上下文到可复用的团队操作指南这四个层面的上下文机制。
零级:直接粘贴到聊天窗口
PySpark作业会因SparkException或AttributeError之类的模糊错误而失败。人们的本能反应是将堆栈跟踪信息复制到网络聊天中。以下是一个来自名为spark_job.py的脚本中的示例:
py4j.protocol.Py4JJavaError: An error occurred while calling o80.load.
org.apache.spark.SparkException: Job aborted due to stage failure.
Traceback (most recent call last):
File "spark_job.py", line 26, in main
df_with_status = df.withColunm("status", lit("active"))
AttributeError: 'DataFrame' object has no attribute 'withColunm'
聊天模型能快速发现拼写错误,并建议将 withColunm 更改为 withColumn。但它是在“盲目”工作的:
- 它无法打开你本地的
spark_job.py,因此需要你自己找到对应行并手动编辑保存。 - 它无法查看无服务器批处理的进度或获取实时执行日志。
- 它也无法查看你的存储桶、服务账户权限或批处理设置。
如果故障原因是缺少存储桶或子网没有私有 Google 访问权限,聊天模型只能给出一般性的猜测。
第一层级:从终端获取文件上下文
作为 agy 调用的 Google Antigravity CLI 是一款以终端为核心的编程工具。它在你的工作区中运行,因此可以直接读取和修改你的文件。
涉及两层独立的身份验证机制。第一层是应用程序默认凭据:运行 gcloud auth application-default login 可让 gcloud 命令、客户端库以及 Spark MCP 服务器获得操作您项目资源的权限。第二层则是 CLI 自身的会话机制。首次启动时,agy 会要求您使用 Google 账户登录并信任该工作空间,这样它才能访问 Gemini 模型。
随后请确保已导出您的项目 ID:
export GOOGLE_CLOUD_PROJECT="$PROJECT_ID"
默认情况下,CLI在读取或写入文件前会请求权限。使用非交互式打印标志(-p)时,除非已设置自动批准,否则任何需要确认的操作都会被默默拒绝。如果将settings.json中的toolPermission设置为always-proceed,并同时指定文件操作的允许列表,那么该代理就可以查看并修复脚本:
mkdir -p ~/.gemini/antigravity-cli
cat << 'EOF' | tee ~/.gemini/antigravity-cli/settings.json ~/.gemini/jetski/cli/settings.json ~/.gemini/antigravity/settings.json ~/.gemini/settings.json
{
"toolPermission": "always-proceed",
"permissions": {
"allow": [
"read_file(*)",
"write_file(*)",
"mcp(*)"
]
}
}
EOF
请注意,为追求便利而选择始终允许交易的模式会带来安全风险,因此应缩小白名单范围,并在可信赖的工作环境中使用它。
现在让系统处理出错的脚本吧:
agy -p "examine spark_job.py, fix the DataFrame method typo, and save the file"
通过本地访问,代理程序会打开spark_job.py文件,找到withColunm这一行,将其改为withColumn,然后直接在原文件中保存。无需在不同窗口之间复制文件。
第二层:通过 Spark MCP 服务器获取基础设施上下文
语法错误属于较容易处理的情形。而大规模的流水线更常在基础设施层面出现故障:数据集缺失、权限不匹配、运行时环境问题。这时就需要 MCP 了。它是一种开放标准,能够让 AI 代理程序连接到外部 API 和数据源,谷歌则提供了远程 Spark MCP 服务器,将 Apache Spark 管理服务 API 作为可调用的工具提供出来。
- 端点:
https://dataproc-$REGION.googleapis.com/mcp - 传输协议:HTTP
list_batches、get_batch、list_jobs 和 get_job 等。在全局配置文件 ~/.gemini/config/mcp_config.json 中注册服务器:
mkdir -p ~/.gemini/config
cat << EOF | tee ~/.gemini/config/mcp_config.json
{
"mcpServers": {
"spark": {
"serverUrl": "https://dataproc-${REGION}.googleapis.com/mcp"
}
}
}
EOF
设置完成后,代理即可查看您的基础设施。当 Google Cloud 中的批处理任务失败时,请运行:
agy -p "inspect my latest failed Spark batch via MCP, identify the root cause, and fix spark_job.py so it succeeds"
接下来的流程如下:
- CLI向远程MCP服务器查询可用的工具,找到
list_batches和get_batch。 - 它调用
list_batches来定位最近的一次运行记录。 - 它调用
get_batch来获取故障状态,并从Cloud Logging中读取JVM错误信息,例如因gs://路径不存在而产生的FileNotFoundException。 - 它打开本地的
spark_job.py文件,找到读取该错误路径的代码行并删除该引用。在这个例子中,它可能会直接输出结果而非将其保存。 - 它通过
spark.stop()确保资源被正确释放,然后写入修正后的文件。
仅需一个指令,该代理就能诊断出云端的故障并更新本地代码。
第三级:将剧本作为代理技能
一旦某个故障排查流程可行,就将其封装为智能体技能。技能是一个包含指令、脚本和上下文文件的文件夹,由 SKILL.md 文件作为核心,用于教授智能体如何执行重复性的操作任务。
创建 .agents/skills/spark-troubleshooter/ 目录并编写该技能内容:
mkdir -p .agents/skills/spark-troubleshooter
cat << 'EOF' > .agents/skills/spark-troubleshooter/SKILL.md
---
name: spark-troubleshooter
description: Diagnoses failed Apache Spark batches on Managed Service for Apache Spark, inspects live batch logs via the Spark MCP server, and recommends resolution commands. Use when troubleshooting Spark job failures.
---
# Spark Troubleshooter Skill
This skill diagnoses failed Apache Spark batches on Managed Service for Apache Spark and offers rapid solutions.
## Instructions
1. Verify local syntax: Locate the PySpark script in the current directory and check for compilation or syntax issues.
2. Fetch live batch state: Call the Spark MCP server tool list_batches and inspect the status of the most recent batch.
3. Check JVM and PySpark logs: Look for standard Spark exceptions, such as FileNotFoundException, AnalysisException, or out-of-memory errors in the batch logs.
4. Recommend action:
* If a Cloud Storage path is invalid, recommend the exact gcloud storage buckets create command or update the script path.
* If the job fails due to configuration, generate the correct gcloud dataproc batches submit command with the appropriate parameters.
* If a syntax error is detected, fix the code in-place.
* For other errors, recommend a fix.
EOF
由于它位于项目的 .agents/skills/ 目录中,CLI 会自动找到它。接着:
agy -p "Diagnose why my last Spark batch failed and recommend a fix"
该代理会按照您的操作指南步骤执行,并返回一份针对性的计划:
[Spark Troubleshooter] Running diagnostic playbook...
- Local Syntax: OK (spark_job.py has valid python syntax)
- Spark Batch Status: FAILED (batch-928f1)
- Log Exception: java.io.FileNotFoundException for gs://my-missing-bucket/input.csv
Recommendation:
The bucket gs://my-missing-bucket does not exist. Run this command to create it:
gcloud storage buckets create gs://my-missing-bucket --location=$REGION
Once created, submit the batch again with:
gcloud dataproc batches submit pyspark spark_job.py \
--region=$REGION \
--deps-bucket=gs://$BUCKET_NAME
将 .agents/skills/ 文件夹提交到 Git,这样所有拉取该仓库的人都能获得相同的自动故障排查功能。
控制台选项:Cloud Logging 中的 Gemini
如果您更愿意在 Google Cloud 控制台操作,Cloud Logging 中内置了 AI 功能。当无服务器 Spark 批处理任务失败时:
- 打开 Apache Spark 托管服务的批处理页面。
- 选择失败的批处理任务,然后点击“查看日志”。
- 在日志浏览器中找到出错行。
- 点击“调查日志”或 Gemini 的火花图标。
Gemini 会直接读取日志内容,标出 PySpark 异常并给出修复建议,无需您将敏感日志信息复制到其他地方。
简要进展概述
数据工程领域的 AI 辅助功能会随着使用场景的不同而扩展功能范围:
- 在无上下文的情况下,比如网页聊天时,只能提供一般性建议,无法查看代码或验证基础设施。
- Antigravity CLI的文件上下文功能可以读取本地代码,并直接进行语法修正。
- Spark MCP的基础设施上下文功能可以查询实时任务状态、读取Cloud Logging日志轨迹,并将运行时故障追溯到源头。
- Agent Skills中的编码上下文功能可将多步骤操作流程转化为整个团队可重复使用的命令。
如需亲自动手体验,Google发布了关于结合Gemini与MCP的Apache Spark托管服务平台工程的实验课程。针对Google Kubernetes Engine也有类似的实验课程,涵盖类似的应用场景。