Sub topic 14
Monitoring progress and failures
This chapter assumes you can start a streaming query with writeStream, and that you know what checkpoints and triggers do. It covers how to see whether a running query is healthy, which metrics matter, and how to read failures.
What a query exposes
Every started query returns a StreamingQuery object. It reports on itself through three properties.
statusshows what the query is doing right now, for example whether a trigger is active or data is available.lastProgressis a dictionary of metrics for the most recent micro-batch.exception()returns the error that stopped the query, orNone.
print(query.status)
print(query.lastProgress)
for q in spark.streams.active:
print(q.name, q.id, q.isActive)spark.streams.active lists all queries running in the current session. Give each query a name with .queryName("orders_bronze") before you start it. Names make this list, the Spark UI, and your logs much easier to read.
Reading the progress metrics
lastProgress holds one report per micro-batch. These fields answer most health questions.
| Field | What it tells you |
|---|---|
| `numInputRows` | Rows read in the batch |
| `inputRowsPerSecond` | Rate at which data arrives |
| `processedRowsPerSecond` | Rate at which Spark processes it |
| `batchId` | Sequence number of the batch |
| `durationMs` | Time spent in each phase, such as `triggerExecution` and `addBatch` |
| `sources[].endOffset` | How far the query has read |
| `stateOperators[]` | State rows and memory for stateful queries |
Compare inputRowsPerSecond with processedRowsPerSecond. If input is consistently higher, the query is falling behind and the backlog grows. Also watch durationMs.triggerExecution against your trigger interval. A batch that takes longer than the interval means the next batch starts late.
For stateful queries, check stateOperators[].numRowsTotal. A value that rises without bound often means a missing or too-long watermark.
Where to look in Databricks
- The Streaming tab in the Spark UI charts input rate, process rate, and batch duration for each query.
- The notebook shows a live dashboard under a running streaming cell.
- A job run page shows task status, logs, and the error for a failed run.
- Many sources report backlog metrics in
sources[].metrics. For Kafka, these include offsets behind the latest.
For alerts, configure job notifications for failure and for long duration. A continuous stream that stops is a failed run, so a failure alert is your first line of defence.
Reacting in code
A StreamingQueryListener receives an event at start, at each progress update, and at termination. Use it to forward metrics to your own system.
from pyspark.sql.streaming import StreamingQueryListener
class ProgressLogger(StreamingQueryListener):
def onQueryStarted(self, event):
print("started", event.name, event.id)
def onQueryProgress(self, event):
p = event.progress
print(p.name, p.batchId, p.numInputRows, p.processedRowsPerSecond)
def onQueryIdle(self, event):
pass
def onQueryTerminated(self, event):
print("terminated", event.id, event.exception)
spark.streams.addListener(ProgressLogger())Reading failures
When a query fails, awaitTermination() raises a StreamingQueryException. In a job, this fails the task so the retry policy and alerts apply.
Common causes:
- Schema mismatch. The source schema changed and the sink or Auto Loader rejected it.
- Checkpoint conflict. A checkpoint path is shared by two queries, or the query logic changed incompatibly.
- Out-of-memory errors. State grew too large, or a partition is skewed.
- Permission errors. The job identity lost access to a volume, table, or topic.
- Duplicate keys in a MERGE. A
foreachBatchbatch had more than one row for a key.
Fix the cause, then restart with the same checkpoint. The query resumes from the last committed batch.
Exercise
A query shows inputRowsPerSecond of 4,000 and processedRowsPerSecond of 2,500 for an hour. What is happening, and what do you check next?
Show answer
The query is falling behind, so the backlog is growing. Check durationMs to see which phase is slow, then look at cluster size, partition skew, and the number of files or offsets read per batch.