Over the past few months I rewrote our main event pipeline from Hive to Spark. It was a daily batch job that took the raw analytics events sent by our web and mobile apps, cleaned and deduplicated them, and wrote the result to the events table that most of our analysis started from. The Hive version had run for years, and a lot of reports and downstream jobs depended on exactly what it produced.
This post is about proving the new job produced the same table. Every difference between the two outputs turned out to be one of three things: a bug in the new job, a bug in the old one, or a rule the old job applied that nobody had written down.
Run both, compare everything
The Spark job ran every night next to the Hive one, reading the same input and writing to a parallel set of tables. Downstream jobs kept reading the Hive output, so nothing depended on the new tables yet.
Comparing two tables of 13 million rows a day row by row isn’t practical, so I compared aggregates first and only drilled into rows where the aggregates disagreed. Counts grouped by the columns that matter, joined with a full outer join so that a group missing on either side shows up:
WITH old_counts AS (
SELECT user_id_type, event_name, count(*) AS c
FROM events_hive
WHERE ts >= 1531353600000
GROUP BY user_id_type, event_name
),
new_counts AS (
SELECT user_id_type, event_name, count(*) AS c
FROM events_spark
WHERE ts >= 1531353600000
GROUP BY user_id_type, event_name
)
SELECT o.user_id_type, o.event_name, o.c AS old_count,
n.user_id_type, n.event_name, n.c AS new_count
FROM old_counts o
FULL OUTER JOIN new_counts n
ON o.user_id_type = n.user_id_type AND o.event_name = n.event_name
WHERE o.c IS NULL OR n.c IS NULL OR o.c != n.c;
An empty result means the two tables agree at that grain. A non-empty one tells you which slice to look at. Watch for NULLs in the join keys: two groups with a NULL user type never match each other in a join, so they show up as a difference on both sides even when the counts are equal.
We moved the pipeline over one output table at a time. The first two were smaller side outputs of the job. One matched exactly for every user type after two weeks of parallel runs. The other, the table of rejected events, had 35 more rows in the Spark version out of about 67,000. I checked those by hand, found they were real events the Hive job had dropped, and switched both tables. The events table took two more months.
Some differences in the events table were bugs in the new job, and I fixed those. Others were behaviour of the old job that nobody had chosen on purpose. For each of those we decided what the table should do, and then I changed the Hive job to match. That sounds backwards, since the Hive job was being retired, but it was the quickest way to keep comparing. Once a difference is understood and decided, making the old job agree removes it from the diff, and whatever is left is something you haven’t explained yet.
The last ten rows
Once the larger differences were settled, one day’s output came to 13,404,402 rows from Hive and 13,404,412 from Spark. I found seven of the ten extra events. They had nothing in common except that they were all sent around midnight.
Both jobs removed duplicate events, but one deduplicated seven days at a time and the other one day at a time. Near midnight, that can keep a different copy of a duplicated event, so the same event lands on one day in one table and on the next day in the other. That most likely accounts for the ten rows, and ten rows out of 13 million was small enough to accept once we knew where they came from.
Two tables that should have agreed
While backfilling one of the new job’s tables, I compared it with another table that should have held the same events, and found about 3 million rows in one that weren’t in the other, and 2.8 million going the other way. Each gap had an ordinary cause:
- one table was deduplicated and the other wasn’t;
- one table had events with a timestamp of zero;
- events with an empty or meaningless user ID were filtered out of one table and kept in the other;
- a backfill months earlier had written to one table and not the other.
After accounting for those, the gap was much smaller, and a sample of what was left all came from a single client app that was already known to send unusual user IDs.
Switching over
For the switch, we swapped the tables: the Spark job started writing to the production events table and the Hive job to the parallel one. Downstream jobs didn’t change at all, and if something had gone wrong, swapping back was the whole rollback plan. No downstream job had problems. About three weeks later we stopped the Hive job and dropped its tables.
The parallel run also turned each difference into a decision someone made on purpose and wrote down: what counts as a duplicate, over what window, and which events get filtered. Before the rewrite, those rules existed only as the behaviour of a query that had been running for years.
Leave a Reply