12-04 更新 实时
首页/ 技术栈/ 正文

Spark 声明式管道的增量回滚

技术栈 来源:Databricks Blog 2026-12-03 05:40 2 阅读 机器翻译
Spark 声明式管道的增量回滚

与其他类似方案不同,表状态以及生成这些状态的流处理过程都被一并纳入了回退(rewind)之中。

这最大程度地减少了回填的需求,有时甚至完全无需回填,从而简化了数据运维并缩短了恢复所需时间。

在数据工程中,一如在生活中,错误不可避免。错误可能来自多种不同原因:有时是源系统产出了一些坏数据,有时是对转换逻辑的新改动引入了一个隐蔽的 bug,有时是上游数据的变更先落地,而聚合处理尚未相应更新以正确处理它们。但后果都是一样的:我们的数据管道产出了错误结果,我们需要找到修复它们的策略。

如今,这种策略通常最终都落在事后的临时性修正上。像 Delta Lake 这样的开放表格式支持一类强大的操作,称为时间旅行(time travel),对此有所帮助;我们可以逐表回溯过去,以弄清(1)问题从何时开始,以及(2)哪些数据需要被删除或修正。

但往往需要修正的范围远超单张表。一个管道可能管理着几十、几百甚至几千张表的数据,而这些表都可能从第一张表接收到了坏行,或收到了由该坏行派生出的坏数据。即使所有表都被修正,坏数据也可能已经传播到管道的运行状态中,导致水位(watermark)被不当推进,或流入中间聚合缓冲区。而对于需要回填的问题,待回填的数据集相当难以计算,需要对源流式系统中的原始偏移量(offset)以及这些偏移量如何在管道中传播进行复杂的推理。

我们坐下来,从第一性原理出发重新审视这个问题,结果发现:在我们见过的许多最棘手的恢复场景中,都存在一条共同的线索。假如你能把整条数据管道——表数据与运行状态一并涵盖,覆盖整个数据流图——回退到过去的某个时间点呢?那样一来,你就可以先做简单的修复,然后继续处理,就好像什么都没出过问题一样。

于是我们构建了一项新功能,由 Spark Declarative Pipelines(SDP)驱动,正是为了实现这一点!这项新的回退功能让你能够回退到过去,查看数据当初的样子,并从该时间点开始重放流式管道,同时应用所需的修复或更正。随着图的执行,我们会定期生成所谓的“回退点”:

这些回退点是一致性的元数据快照,包含管道中每个流和数据集的一组信息。当出现问题时,我们只需挑选其中一个快照:

然后执行回退,把管道的状态带回过去的某个时刻。

回退完成后,管道即可再次执行,无需回填,也无需临时协调。在对输入数据或转换逻辑完成所需的修复之后,管道便可恢复运行,并继续向前推进,就好像从未出过任何问题一样。

当然,更难的问题是:这一切在底层究竟是如何运作的?在本文的剩余部分,我们将探讨支撑回退功能的底层技术与流程、我们为让它跑通所必须采取的步骤,以及我们未来如何把它做得更好的目标。

事务性存储与 Delta Lake

Delta Lake 拥有多级存储系统。表的核心是 Delta log,即按照 Delta Lake 协议排列的一系列事务条目。事务日志中的每一条条目都代表对表的一次变更,例如:

添加了文件 101、102、103。

文件 4、5、6 被替换为文件 104 和 105。

文件 7 被移除,且没有替代文件。

在这些事务日志条目中,每个文件都指向一个特定的 blob 存储路径,而该路径才是底层表数据实际所在的位置。

这层额外的间接寻址确实增加了系统复杂度,但正因如此,它在实际工作负载中带来了诸多优势。许多新特性,例如删除向量(deletion vectors)或 liquid clustering,都通过利用事务日志结构,在性能和可维护性上带来了显著收益。另一个主要优势是能够把事务日志当作历史视图来使用。

使用表历史记录

在事务性存储系统中,表历史不再只是一种抽象概念:它就是存放在 blob 存储中、可以被查询的另一个数据集。任何用户都可以翻阅 Delta Lake 格式的表,了解发生过哪些操作、操作发生在何时、由谁执行等等。他们可以进行时间旅行,查看表在过去某个特定时间点的内容。他们甚至可以用 RESTORE 把表恢复到之前的状态。

然而,虽然 RESTORE 会回滚对表的写入结果,但执行该写入的进程本身并不受影响。在它们看来,数据早已处理完毕。因此,从 RESTORE 中恢复通常需要执行临时的回填来修正表状态。视数据处理管道的规模而定,这可能只是令人烦恼,也可能完全不可行。

在我们的声明式管道框架中,这一点尤其不幸,因为这种命令式的执行管理正是我们力求避免的。但管道框架也为我们提供了 flow,它直接表示数据摄取过程及其写入的数据集。如果我们也能对 flow 执行类似 RESTORE 的操作,问题就迎刃而解了;我们不必去寻找那些已从数据集中清除的源数据、再通过单独的流程重新摄取,而只需撤销整个摄取过程,然后像它第一次发生那样重新执行一遍即可。这就带来了另外两个要求。

要执行管道级别的回退,我们必须为每个数据集和每个 flow 确定要回退到哪个版本。对于触发式执行过程,这相对直接,因为我们只需在两次更新之间拍摄快照。但对于连续执行过程,相互依赖的 flow 和数据集可能会以并行且不对齐的方式执行。

我们用两步流程解决了这个问题。在计算倒回点时,我们维护一组复杂的图不变量,其效果是确保朴素重新处理会产生从 flow 的目的地倒回的数据的超集。为了避免数据重复,我们在重放时添加额外的流式逻辑,指示 flow 在重放其源数据时跳过已经消费的数据范围。但这随后引出了第二个需求:我们如何在由 flow 表示的 Structured Streaming 过程上实现这样的倒回操作?

需求 2:Structured Streaming 倒回

Spark Structured Streaming 协议也有日志结构。这最初是出于故障恢复的考虑:将偏移量、有状态操作数据等存储在带有递增版本的元数据日志中,使 Apache Spark 系统能够确保幂等性,并在微批查询在提交其结果之前失败的情况下实现可重复的批次。但该元数据日志最终在结构上与 Delta Lake 元数据日志大体相似,并且近期工作添加了 read_state_metadata 查找,类似于 Delta Lake 中的 DESCRIBE HISTORY。

我们扩展了 Structured Streaming 检查点层,引入两项新功能来帮助倒回工作负载:

我们更新了流式协议,允许从仍处于保留期内的任意先前批次开始,向前运行流。这涉及两项重大变更:向偏移量和提交日志添加额外的源端和汇端元数据,以及创建一种新的批类型,称为控制批。与普通的流式批(数据批)不同,它表示发生了修改流属性的操作,例如回退、重新分区或协议升级。控制批还执行上文回退级联所需的跳过操作。

在 Databricks Lakeflow 上,我们引入了固定(pinning)的概念,即标记某个特定的流式版本,使其应保留最多 7 天,即使已超过正常的 100 批次间隔。固定一个批次会保留流式检查点中回退到该批次所需的全部信息,包括元数据日志、源状态、状态存储状态和有状态元数据。

Auto Loader 是上述问题的一个说明性示例。在标准设置中,它从云服务提供商系统中的瞬态文件通知中消费,在文件到达源桶时对其进行处理。因此,回退流无法生成原始源状态;文件通知已被消费,无法恢复。

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(Incremental rewind with Spark Declarative Pipelines)

Unlike with other similar things, both the table state and the streaming processes which produce them are incorporated into the rewind.

This minimizes and sometimes avoids entirely the need for backfills, simplifying data operations and reducing time to recovery.

In data engineering, as in life, mistakes are inevitable. They can come from a variety of different causes: sometimes a source system will generate a bit of bad data, or a new change to a transformation will introduce a subtle bug, or changes to upstream data will land before the aggregate processing has been properly updated to handle them. But the consequence is the same: our data pipeline has generated some bad results, and we need to find a strategy to fix them.

Today, this strategy generally ends up involving ad hoc corrections after the fact. Open table formats like Delta Lake support a powerful category of operations called time travel, which help with this; we can peek into the past on a table-by-table basis to understand (1) when the issue started, and (2) what data needs to be removed or corrected.

But corrections are often required far beyond the scope of a single table. A pipeline might manage data for dozens, hundreds, or even thousands of tables, all of which might have received a bad row from the first table or bad data derived from that row. Even when all tables are corrected, the bad data might have propagated into the pipeline’s operational state, improperly advancing a watermark or flowing into an intermediate aggregate buffer. And for issues that require backfill, the set of data to be backfilled is quite hard to compute, requiring complex reasoning about the original offsets in the source streaming system and how those offsets propagated through the pipeline.

We sat down to think the problem through from first principles, and we realized that there was a common thread across many of the trickiest recoveries we’d seen. What if you could rewind the entire data pipeline - table data and operational state alike, encompassing the entire dataflow graph - back to some point in the past? Then you could apply the easy fix, and resume processing as though nothing ever went wrong.

So we built a new feature, powered by Spark Declarative Pipelines (SDP), to do precisely that! The new rewind functionality enables scrolling back into the past, inspecting what the data used to look like, and replaying a streaming pipeline from that point onwards with any fixes or corrections required. As the graph executes, we periodically generate what we call “rewind points”:

image2.png

These rewind points are consistent metadata snapshots, containing a set of info for each flow and dataset in the pipeline. Then, when something goes wrong, we simply have to pick one of those snapshots:

And execute the rewind to bring the pipeline’s state back in time.

Once the rewind is complete, the pipeline is ready for execution once again, with no need for backfills or ad hoc coordination. After any required fixes have been applied to the incoming data or transformation logic, the pipeline can resume, and will simply continue onwards as though nothing was ever wrong.

The harder question, of course, is how this all works under the hood? In the remainder of this post we’ll explore the underlying technologies and processes that power the rewind, the steps we had to take to get it working, and our future goals for how to make it better.

Transactional storage and Delta Lake

Delta lake has a multi-level storage system. The core of the table is the Delta log, a sequence of transaction entries according to the Delta Lake protocol. Each entry in the transaction log represents a change to the table, such as:

Files 101, 102, 103 were added.

Files 4, 5, 6 were replaced with files 104 and 105.

File 7 was removed without replacement.

Within these transaction log entries, each file points to a particular blob storage path, and that path is where the underlying table data is actually located.

This additional layer of indirection does increase the system complexity, but in doing so it enables many advantages in practical workloads. Many new features, such as deletion vectors or liquid clustering, deliver large benefits in performance and maintainability by leveraging the transactional log structure. Another major advantage is the ability to leverage the transaction log as a historical view.

Working with table history

In a transactional storage system, the table history becomes more than an abstraction: it’s just another dataset living in blob storage which can be queried. Any user can look through a table in the Delta Lake format to understand what operations happened, when they happened, who performed them, and so on. They can time travel, seeing what the contents of the table used to be at a specific point in the past. They can even RESTORE the table to bring its contents back to that previous state.

However, while RESTORE rolls back the results of a write to the table, the processes which performed that write are unaffected. As far as they’re concerned, the data has already been processed. So recovering from a RESTORE will usually involve performing ad hoc backfills to correct the table state. Depending on the size of a data processing pipeline, this can range from annoying to outright infeasible.

In our declarative pipelines framework, this is particularly unfortunate, because this kind of imperative execution management is precisely what we aim to avoid. But the pipeline framework also provides us flows, directly representing the data ingestion processes along with the datasets they’re written into. If we could do something like a RESTORE to the flows as well, the problem would be solved; rather than having to find the source data that was cleared from the datasets and re-ingest it through a separate process, we would simply undo the entire ingestion process and then redo it just as though it was happening for the first time. This then poses two additional requirements.

To perform the pipeline-level rewind, we have to identify which version to rewind to for each dataset and each flow. For triggered execution processes, this is relatively straightforward, as we can simply take snapshots between updates. For continuous execution processes, though, flows and datasets with dependencies on each other may execute in a parallel and non-aligned manner.

We solved this problem with a two-step process. When computing a rewind point, we maintain a complex set of graph invariants, with the effect of ensuring that a naive reprocessing will produce a superset of the data which was rewound from the flow’s destination. In order to avoid data duplication, we add extra streaming logic on replay, instructing the flow to skip the range of data which was already consumed when replaying its source data. But that then leads into the second requirement: how do we implement such a rewind operation on the Structured Streaming process represented by a flow?

Requirement 2: Structured Streaming rewind

The Spark structured streaming protocol also has a log structure. This was originally motivated by failure recovery: storing the offsets, stateful operation data, et cetera in a metadata log with incrementing versions allows the Apache Spark system to ensure idempotency and achieve repeatable batches in scenarios where a microbatch query fails before committing its results. But the metadata log ended up being broadly similar in structure to the Delta Lake metadata log, and recent work has added a read_state_metadata lookup analogous to DESCRIBE HISTORY in Delta Lake.

We extended the Structured Streaming checkpoint layer to introduce two pieces of new functionality to help rewind workloads:

We updated the streaming protocol to allow running the stream forwards from any prior batch that remains in retention. This involved two major changes: adding additional source and sink metadata to the offset and commit logs, and creating a new batch type, called a control batch. Unlike a normal streaming batch (a data batch) this indicates that an operation occurred that modifies the properties of a stream, such as a rewind, repartition, or protocol upgrade. A control batch also performs the skipping that was required above for rewind cascading.

On Databricks Lakeflow, we introduced a concept of pinning, marking a particular streaming version that should be retained for up to 7 days even if the normal 100 batch interval was exceeded. Pinning a batch retains all information in the streaming checkpoint necessary to rewind to that batch, including metadata logs, source state, state store state, and stateful metadata.

Auto Loader is an illustrative example of the problem described above. In the standard setup, it consumes from transient file notifications in a cloud provider’s system, processing files as they arrive into the source bucket. Rewinding the stream thus cannot produce the original source state; the file notifications are already consumed and cannot be restored.

To tackle this efficiently without unacceptable increases in storage costs, we needed to introduce a bit of incrementalism into the Auto Loader streaming source. We added a new component called the rewind log, containing metadata for archived checkpoint files to enable fast lookup of checkpoint files containing relevant replay information. This rewind log is also used to enable on-demand loading of Auto Loader’s checkpoint during non-replay execution to address disk usage concerns with increased retention. The combination of these two functionalities let us efficiently integrate Auto Loader rewind with the Structured Streaming pinning mechanism, efficiently performing rewinds without hurting the performance of normal execution.

Our beta launch is far from the end of the story, and we have a number of ongoing efforts to improve and expand on pipeline rewinding.

Rewinding the definition

In addition to rewinding the pipeline’s execution state, we intend to also support rewinding the pipeline’s definition. This will allow one-step resolution for problems such as code changes that produced invalid data. However, the abstract concept of the definition of the pipeline includes a number of components:

The code which defines the pipeline.

The results of metaprogramming within that code.

Pipeline parameters which control the code or metaprogramming.

SQL configurations which may change engine behavior.

Table controls, administrative policies, etc. which are applied to the pipeline or datasets in it out-of-band.

Our efforts continue to investigate each of these components and how to best represent them within a rewind process.

Propagation outside the pipeline

The results of a pipeline are themselves consumed by other automated processes, which today cannot easily distinguish between a rewind and other kinds of updates. We hope to establish a protocol that will allow platform-wide consumers to understand that a rewind has been performed and react accordingly.

How is rewind different from Delta Lake time travel and RESTORE?

Delta Lake time travel and RESTORE work on a single table, while SDP rewind rolls back every table and flow in a pipeline together. RESTORE reverts a table's contents, but the processes that wrote the data still treat it as processed, so recovery usually requires ad hoc backfills. Rewind resets those processes too, so the pipeline reprocesses the affected data as though for the first time.

Do I need to backfill data after a rewind?

Not necessarily. In the next update after a rewind finishes, the pipeline will replay the rewound data. So any issues that come from problems with the transformation logic can be handled by simply correcting the transformation logic before the replay. If some data requires an out of band correction, the transformation can be adjusted to filter out the bad data and ingest the rest while the bad data awaits its backfill.

Does rewind work with continuous pipelines?

Yes, SDP rewind works with both triggered and continuous pipelines. In triggered pipelines, rewind points are taken between updates, but in continuous pipelines, flows that depend on each other can run in parallel and out of step. SDP handles this by choosing rewind points that guarantee replay covers all of the rolled-back data. On replay, a new type of streaming batch called a control batch skips the data each flow already consumed, so nothing gets duplicated.

Does rewind work with Auto Loader?

Yes, SDP rewind works with Auto Loader. The file notifications which it uses to detect new files, of course, are gone once consumed and can't be directly restored. To handle this, Auto Loader keeps a rewind log with metadata about what was ingested at each of the rewind points, so it can quickly find what it needs to replay.

How do I choose which point to rewind my pipeline to?

Choose a rewind point from before the problem started. You can explore the historical state of each dataset in the pipeline using the data preview within the rewind interface, or with the Delta LAke time travel functionality.

If you want to learn more, try out the beta! See our feature introduction or review the beta documentation.

Get the latest posts in your inbox

原文为英文,正文由自动抓取并保留原文。

本文由采集器自动同步

原文发布于 Databricks Blog(Incremental rewind with Spark Declarative Pipelines),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗