Every minute, a scheduled job sends one fake analytics event into our event pipeline: a canary, from a known user, with a known name and timestamp. Downstream, a check looks for each canary in the tables the pipeline produces, and alerts when one doesn’t arrive. It has been doing this for more than nine years, mostly without anyone noticing.
Three weeks ago, it started alerting. Some canaries were going missing. Nothing else looked wrong: the streaming job was running, and the tables were filling up as usual. This post is about finding out where they went.
The pipeline
Events arrive as HTTP requests, get logged and written to Kafka. A Spark streaming job reads Kafka, parses and validates each event, and writes the results to staging tables, which the batch jobs then process. The streaming job runs as a scheduled task that restarts every hour and resumes from the offsets it last committed.
A missing canary could have been lost at any of those steps. The useful thing about a canary is that you know exactly what you’re looking for, so you can check each step separately.
Following one missing event
Was it in Kafka? We record the Kafka offsets the streaming job processes, so I could work out the offset range for each partition around the time of a missing canary and read exactly that range back:
val offsetRanges = Array(
OffsetRange.create("events", 0, 28983662960L, 28983787653L),
OffsetRange.create("events", 1, 28984194717L, 28984286953L),
// ... one range per partition
)
KafkaUtils
.createRDD[String, String](spark.sparkContext, kafkaParams.asJava, offsetRanges, PreferConsistent)
.map(_.value())
.filter(_.contains("canary_flown"))
.collect()
.foreach(println)
The missing canaries were there, between the ones that had made it through. Whatever was losing them was downstream of Kafka.
Could the job parse it? I took the raw events from Kafka and ran them through the streaming job’s own parsing function. They parsed fine. Nothing about the events themselves was wrong.
That left two possibilities: the job was dropping some records when writing them out, or it was skipping records when it restarted. The second had a clue in its favour. The missing canaries were clustered around the times the streaming job restarted.
An experiment that predicts a number
To test the restart theory, we made each streaming run last three hours instead of one. If restarts were losing events, there would be a third as many restarts, and the number of missing canaries should drop by about two thirds. A hypothesis that predicts a number is much easier to confirm or reject than one that predicts “fewer”.
In the end, the restarts weren’t the cause, though they were part of the story. The job wasn’t skipping offsets. It was failing on some events and saying nothing about it.
The cause
For a small number of events, processing threw a ConcurrentModificationException from a cache inside the job. The failure was silent: the event was dropped, the job kept running, and nothing surfaced the error. The problem came from a recent change that looked completely harmless.
That also explained the timing. After a restart the cache starts empty, so for the first stretch of each run the job is writing to it constantly while also reading from it, and the chance of hitting a concurrent modification is at its highest. Restarts didn’t lose events; they made the bug much more likely to fire.
Cleaning up
Fixing the cache was the easy part. The other two pieces mattered as much:
- Recovering the lost events. Kafka still had everything, so we replayed the ten affected days through the fixed job, slowly, over three or four days, so it wouldn’t crowd out the cluster’s normal work. Our batch pipeline reprocesses the last 14 days every night to pick up late-arriving events, so the replayed events flowed into every downstream table without any manual backfill.
- Making the next one visible. Per-event failures in the streaming job now go to a table we can query, with the error details. The next time something fails for a fraction of a percent of events, it will show up as rows in that table, not as a canary alert two weeks later.
Takeaways
- Test the pipeline from the outside. Every component reported that it was healthy, and each one was right about itself. Only an end-to-end check that knows what should come out of the other end could see the gap.
- A known event lets you bisect. Because we knew exactly what each canary looked like and when it was sent, we could check it at every stage and narrow the problem down to one component.
- Don’t swallow per-record errors. Dropping a bad record to keep a stream running can be the right call, but only if the drop is counted and the record is kept somewhere you can look at it.
- Keep the source replayable. Kafka retention plus a pipeline that reprocesses recent days turned “we lost events for ten days” into a few days of background work.
When we found the cause, a colleague who was around when the canaries were first set up commented that they were “kind of proud” to see them still useful after nine years. Fair enough.
Leave a Reply