Our data pipelines run validation checks every day: SQL queries that count rows which shouldn’t exist, with an alert when the count goes over a threshold. Last week one of them alerted, and a colleague pointed out that it couldn’t be right. The check counted payment records whose reference didn’t start with any known prefix. It reported more than a thousand of them, all with a prefix the check had accepted since November.
The alert also included the query that had failed. It was the query as it looked before November.
Ruling out the obvious
An old query usually means old code, so I checked that first. The Airflow logs showed the task running the latest build of the checks. The Spark History Server showed that build was the only one on the job’s classpath. I downloaded the same artifact from our repository and confirmed it contained the current query.
Then two lines in the Airflow log, one second apart, stood out:
08:14:37 INFO - Check is successful
08:14:38 INFO - Sending alert ...
The first is the last thing the Spark job prints. The job had run the current query, and the check had passed. The alert came from the Python side, after Spark finished.
How the result got from Spark to Airflow
The check ran as a Spark job launched from an Airflow task, and the two sides passed the result through a file. Simplified, the Airflow side did this:
result_file = f"/tmp/{check_name}_{os.getpid()}.json"
spark_submit(check_job, args=["--file", result_file])
send_alerts_from(result_file)
The Spark job wrote a message into the file when it had something to report, and the Python code read the file afterwards and sent whatever it found. The process ID was there to keep two runs of the same check from using the same file.
For this run, the file was /tmp/<check>_2531.json. A process ID of 2531 is low for a busy, long-running Airflow worker. Linux hands out process IDs in increasing order and wraps around when it reaches the limit (often 32,768), so on a machine that has been up for months, every number gets reused sooner or later.
The most likely explanation: months earlier, before the prefix was added to the check, a run of the same check got process ID 2531, failed, and wrote its message into that file. Nothing deleted it. Last week, a new run of the same check got the same process ID again, the check passed and wrote nothing, and the Python side read the old message as if it were new. That accounts for both the outdated query and the records it complained about.
The fix
We moved the result file into a temporary directory that belongs to the task run, so every run starts with an empty directory. Along these lines:
with tempfile.TemporaryDirectory() as run_dir:
result_file = os.path.join(run_dir, "result.json")
spark_submit(check_job, args=["--file", result_file])
send_alerts_from(result_file)
With a directory per run, the process ID in the file name isn’t needed at all.
What I took from it
- A process ID is unique only while the process is running. It’s fine for telling apart two things running at the same moment. It’s not an identifier for anything that outlives the process, like a file in
/tmp. - Don’t let “no file” mean success. If the job had written an explicit result on every run, pass or fail, a stale file would have been overwritten instead of read.
- Use the standard library for temporary files.
tempfilein Python,mktempin the shell: they create names that can’t collide and, used as above, clean up after themselves. - Put enough detail in alerts to check them. The only reason this alert looked wrong was that it included the query it ran. Without that, it would have looked like a real data problem.
Leave a Reply