Schedule icon
Request icon
Switch icon
SlackIncomingWebhook icon
If icon
Log icon

Apache Livy Spark Batch SLA & Orphaned Session Sentinel

Audit Apache Livy Spark batches for failure and SLA breaches, detect orphaned interactive sessions, alert on Slack, and safely reclaim cluster resources.

Categories
Data

Apache Livy is the standard open-source REST service for interacting with Apache Spark clusters, enabling easy submission of Spark batch jobs and stateful interactive sessions across multi-tenant data platforms. In production analytics environments, Spark workloads submitted through Livy frequently face two silent operational failure modes: batch jobs that crash into a dead state due to driver OutOfMemory (OOM) errors without notifying downstream owners, and long-running interactive PySpark sessions abandoned by data scientists that remain idle while permanently hoarding executor memory and CPU cores on Kubernetes or YARN. This blueprint implements an automated sentinel that continuously audits Livy endpoints, inspects batch and session registries, dispatches actionable Slack alerts, and provides guarded automated reclamation of orphaned sessions.

How it works

  1. periodic_livy_audit (io.kestra.plugin.core.trigger.Schedule) triggers every 15 minutes with configurable endpoint and threshold inputs. Shipped disabled so initial manual verification can be performed first.
  2. probe_spark_batches (io.kestra.plugin.core.http.Request) invokes GET /batches to retrieve all registered Spark batch jobs, execution states, and application IDs.
  3. probe_spark_sessions (io.kestra.plugin.core.http.Request) invokes GET /sessions to inspect active interactive Spark sessions and their idle states.
  4. classify_spark_workloads (io.kestra.plugin.core.flow.Switch) evaluates probe responses:
    • DEAD_BATCH_DETECTED: Triggered when any Spark batch enters dead or error state. Dispatches an incident alert to Slack via io.kestra.plugin.slack.notifications.SlackIncomingWebhook directing on-call engineers to driver failure diagnostics.
    • ORPHANED_SESSION_DETECTED: Triggered when interactive sessions remain idle past threshold limits. Dispatches a resource leak warning to Slack, and if auto_kill_orphaned_sessions is enabled with dry_run disabled, executes reclaim_orphaned_session (DELETE /sessions/{id}) to free executor RAM.
    • HEALTHY: Confirms all batch and session states are operating within normal parameters.
  5. log_audit_summary (io.kestra.plugin.core.log.Log) records an immutable audit trail capturing evaluated workload counts and sentinel classification.
  6. Global errors handler catches connection drops or network partitions, dispatching an immediate Slack notification if the Livy server is unreachable.

What you get

  • Instant visibility into dead or failed Spark batch jobs before business analytics pipelines breach downstream delivery SLAs.
  • Automated detection and guarded reclamation of orphaned interactive sessions, preventing memory exhaustion on shared Spark infrastructure.
  • Zero unexpected state changes: dry_run defaults to true to ensure full audit safety during rollout.
  • Direct Slack notifications with Livy host, batch IDs, and application context for fast incident triage.

Who it's for

  • Data Platform Engineers operating multi-tenant Apache Spark and Livy clusters on Kubernetes, YARN, or cloud VMs.
  • Analytics Engineers responsible for production Spark ETL job reliability and SLA compliance.
  • Site Reliability Engineers maintaining big data infrastructure health and preventing executor resource starvation.

Why orchestrate this with Kestra

Apache Livy provides raw REST endpoints but lacks built-in alerting, scheduling, or self-healing policy enforcement. Relying on ad-hoc cron jobs or custom Python scripts leads to unmonitored scripts, credential exposure, and lack of audit history. Kestra delivers a declarative, version-controlled blueprint with native scheduling, credential protection through secret masking, conditional routing, and complete execution observability.

Prerequisites

  • A running Apache Livy server (version 0.7.0+ or 0.8.0+) accessible over HTTP. Livy provides an official open-source container image (apache/livy:latest) requiring no commercial licenses.
  • An incoming Slack webhook URL for operational notifications.

Secrets

  • SLACK_WEBHOOK_URL: Slack incoming webhook URL for routing dead batch and orphaned session alerts.

Quick start

  1. Start a local Apache Livy and Spark test container:
    docker run -d --name livy-test -p 8998:8998 jahstreet/spark-livy:latest
    
  2. Configure the SLACK_WEBHOOK_URL secret in your Kestra namespace.
  3. Import this blueprint into Kestra and click Execute with default inputs (dry_run: true).
  4. Verify that log_healthy_cluster logs nominal batch and session counts.
  5. Test session detection by creating an interactive PySpark session:
    curl -X POST http://localhost:8998/sessions -H "Content-Type: application/json" -d '{"kind": "pyspark"}'
    
  6. Re-run the flow to observe alert_orphaned_session_slack firing.
  7. Enable periodic_livy_audit to activate recurring 15-minute surveillance.

How to extend

  • Add Prometheus push gateway metrics via io.kestra.plugin.prometheus to track active Spark batch and session counts over time.
  • Integrate PagerDuty alerting via io.kestra.plugin.pagerduty.notifications.PagerDutyAlert for production SLA breaches.
  • Add automated batch log dumping to S3 using io.kestra.plugin.aws.s3.Upload when a batch crashes to preserve driver logs before Livy container rotation.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.