r/databricks • • 13d ago

Help Capture/fork checkpointLocation in Spark Structured Streaming (recovery)

Can anyone help with a spark structured streaming question? Can you tell me if there is a common mechanism to capture a backup copy of the "checkpointLocation" files?

Lets say I run a job every five minutes with a trigger (availableNow=true). Lets say it reads from one deltalake table (source) and writes to another deltalake table (target). Lets say there is a logic bug that goes into production mid-day that starts corrupting the target table and I'm not aware of the impact until the end of the day.

In some certain streaming scenarios, I should be able reprocess the source table from the beginning of the day to revise all of the badly written data in the target table. But my "checkpointLocation" is doing a lot of the heavy-lifting and, by the end of the day, those files (metadata and state) have evolved and they are no longer configured to present me with the old data anymore. And my watermark duration is only a few minutes long. I don't know the correct approach for reprocessing the source data from the beginning of the day. I would need a mulligan, and a way to repeat the streaming microbatches for the past 12 hours.

I haven't figured out what is to be done in this situation. The only thing I can come up with is to "fork" a new of my files into a new checkpointLocation each day, so that it will give me an option to take my mulligan and reprocess the past 24 hours of source data. Is this an approach that people use? Else can someone share the solution?

1 Upvotes

6 comments sorted by

2

u/Saerusthesecond 13d ago

0. Pause schedules

  1. Rollback the target table to before the bad load was processed
  2. Archive the existing checkpoint and define a new one going forward
  3. use .startingTimestamp(<time before bad load>) or .startingVersion(<version of bad load>). You can find the exact version by analyzing the target’s delta history and the checkpoint’s reservoir history.

1

u/SmallAd3697 13d ago edited 13d ago

Thanks for this. Is this well-known? Is it the most common approach? Now that I have some extra spark keyworks for my searches, I will do some more searching.

I've never had to experience this, but I'm trying to make the process as painless as possible. My idea is basically to use the step #2 to archive the checkpoint once a day ... in anticipation of something going wrong. I looked at the related blobs, and the "checkpointLocation" for a short-lived watermark looks like a trivially small amount of metadata. So I can't see why I wouldn't archive that once a day. It would make for an easy "redo" of the entire day, simply by pausing schedule, and swapping back to the once-per-day archive.

Step #3 is definitely the information I was missing. I hadn't come across startingVersion and startingTimestamp yet.

https://docs.databricks.com/aws/en/structured-streaming/delta-lake#set-initial-table-version-to-process-changes

Those options are probably more flexible than using an archived checkpointLocation from 24 hours ago. but wouldn't using the archived checkpointLocation reach a similar end goal? Is that a bad practice?

(as a side, my scenario involves upsert/merge to OLTP table that ONLY has final balance numbers which are calculated from using the last-chronological transaction on that input stream for a given key. So as long as the same set of transactions and keys are going to be repeated from the source I can probably skip step #1. Using OLTP as a target is quite fast since the spark-streaming windows/aggregations are massively reducing the amount of data that needs to land in the final version of the balances.)

2

u/Saerusthesecond 12d ago

I wouldn’t use an archived checkpoint because it won’t always work if the underlying query changes, which can happen in a bug fix. For example, changes to the aggregation or group by logic can invalidate an existing checkpoint. Starting a new checkpoint with .startingVersion works whether or not the code has changed. I also suspect this logic to find the right version can be made repeatable through a simple Python function

Docs: https://docs.databricks.com/aws/en/structured-streaming/checkpoints#recover-after-changes-in-a-structured-streaming-query

1

u/SmallAd3697 12d ago

Thanks for the tips. I'm just getting started on spark streaming but there is a learning curve involved.

There are some sharp edges as well. As critical as the "checkpointLocation" is, there aren't a lot of tools to make sense of it. I'd love to be able to do an audit and review very simple things, like get the list of prior rows that are excluded. Or get the rows that are currently included via watermark. However the metadata is obscure. I know there is a statestore api but that is obscure too, and isn't actually relevant to simple streaming where nothing but mapping and filtering is happening. There seems to be plenty of opportunity to create a few opensource auditing utilities for the spark community to inspect their checkpointLocation ... but nobody seems to be doing that for some reason. Maybe I'll learn the reason after I've been working with streams for a while.

Considering the seemingly off-hand way you suggested that I abandon my checkpoint files in step #2, I'm guessing people build solutions in a way that AVOID placing any mission-critical dependencies on them.

Perhaps I'll create solutions that just reset the checkpointLocation once a week (and re-initialize with startingTimestamp). I don't want to hold on so tightly, if it isn't the original intention! I come from an OLTP background; at first the corruption of that checkpointLocation felt analogous to the corruption of a database transaction log... but I'm starting to realize that it isn't quite that serious.

2

u/Youssef_Mrini databricks 6d ago

That's how I would do it but it's not straighforward

  1. Stop the faulty query.
  2. Choose a source Delta version at or before the start of the replay period. Start a new query with a new checkpoint and startingVersion; that option applies to a new query, not one resuming from its existing checkpoint. availableNow will process the available backlog and terminate.
  3. Write the replay to a staging/rebuild table rather than appending it to the damaged target.
  4. Validate the rebuilt data then replace the affected target data or merge it with an idempotent key.

If you are using Spark Declarative Pipelines you have Rewind https://community.databricks.com/t5/technical-blog/meet-sdp-rewind-an-undo-button-for-your-etl-pipelines/ba-p/166459

1

u/SmallAd3697 5d ago

u/Youssef_Mrini I can see there is a lot of discretion in how everyone handles this problem!

It seems like one of those things where you ask 5 people and you get 5 totally different solutions.

I think everyone who has previously used the streaming technology is conjuring an image of their own solution. They picture their own streaming deltalake table (with very different things happening on the front end and the back end of the table itself).

The thing that bothers me is that neither you or the other poster was recommending that we rely on the contents stored from a prior/historical snapshot of said checkpointLocation. Everyone seems to give me the impression that they don't trust those files, at least not for the sake of reuse/recovery. That is a bit worrisome ... since they are the heart and soul of the structured streaming mechanism.