Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 105 additions & 5 deletions docs/en/connectors/sink/Hudi.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ Hudi data files plus `.hoodie` metadata and lets you choose between `INSERT`, `U

## Key features

- [ ] [exactly-once](../../introduction/concepts/connector-v2-features.md)
- [x] [exactly-once](../../introduction/concepts/connector-v2-features.md)
- [x] [cdc](../../introduction/concepts/connector-v2-features.md)
- [x] [support multiple table write](../../introduction/concepts/connector-v2-features.md)
- [x] [timer flush](../../introduction/concepts/connector-v2-features.md)

:::tip Exactly-once semantics

Exactly-once is opt-in, set `semantics = EXACTLY_ONCE` and enable the checkpoint. With the default
`AT_LEAST_ONCE` semantics a retried job can commit the records of a failed attempt twice, see
[Exactly-once semantics](#exactly-once-semantics).

:::

:::caution Hive Metastore synchronization

The SeaTunnel Hudi sink writes Hudi data files and `.hoodie` metadata, but it does not register the table in or synchronize the table with Hive Metastore. Options matching `hoodie.datasource.hive_sync.*` are not supported sink options and are not passed to the Hudi write client. Run Apache Hudi's `HiveSyncTool` or another table registration process separately when Hive Metastore registration is required.
Expand All @@ -40,6 +48,7 @@ Base configuration:
| schema_save_mode | enum | no | CREATE_SCHEMA_WHEN_NOT_EXIST | How to handle the target schema before the job starts. |
| data_save_mode | enum | no | APPEND_DATA | How to handle existing table data before the job starts. |
| multi_table_sink_replica | int | no | 1 | Replica number for every table writer in multi-table sink mode. |
| semantics | enum | no | AT_LEAST_ONCE | Write semantics: `AT_LEAST_ONCE` or `EXACTLY_ONCE`. `EXACTLY_ONCE` requires the checkpoint to be enabled. |
| common-options | Config | no | - | [Common sink options](../common-options/sink-common-options.md). |

Table list configuration:
Expand Down Expand Up @@ -180,10 +189,101 @@ env {
}
```

Hudi timer flush reuses the connector's synchronized batch flush and the Hudi client's auto-commit behavior. The Hudi
sink does not provide a 2PC exactly-once writer, so timer flush provides at-least-once delivery. Retries can create
additional commits. With `INSERT`, generated record keys can also produce duplicate rows after recovery; `UPSERT` with
stable `record_key_fields` limits duplicate logical records.
Hudi timer flush reuses the connector's synchronized batch flush. With the default `AT_LEAST_ONCE`
semantics the Hudi client commits every flushed batch immediately, so timer flush provides
at-least-once delivery and a retry can create additional commits. With `semantics = EXACTLY_ONCE`
timer flush only stages the records into the instant of the current checkpoint, and they become
visible when that checkpoint completes.

## Exactly-once semantics

Set `semantics = EXACTLY_ONCE` to write with the two-phase commit protocol of the engine:

```hocon
env {
checkpoint.interval = 10000
}

sink {
Hudi {
table_dfs_path = "/tmp/seatunnel_mnt/hudi"
table_name = "st_test"
semantics = "EXACTLY_ONCE"
op_type = "UPSERT"
record_key_fields = "c_bigint"
}
}
```

How it works:

1. While the job is running the writers only write the records into a Hudi instant and never commit
it. All the batches of one checkpoint are written with the same instant.
2. When a checkpoint completes, the aggregated committer commits the instant of that checkpoint.
Committing is what makes the files visible to the readers.
3. The commit info of a checkpoint is part of the checkpoint state. If the commit is lost, for
example because the job failed between the checkpoint completion and the commit, the job commits
the same instant again after it restores from that checkpoint. An instant that is already
committed is skipped, so the records are never published twice.
4. The records of a checkpoint that never completed are not committed. The source replays them after
the pipeline restarts, and the instant that the failed attempt left behind is rolled back by the
connector itself: by the writer when it closes, by the committer on the engines that call
`abort`, and on restore, where every commit instant of the table that the restored checkpoint
state does not reference is rolled back before the restored instants are committed.

With `EXACTLY_ONCE` the failed writes cleaning policy of the Hudi client is set to `NEVER`. Hudi
therefore neither rolls back an instant that waits for its checkpoint, nor starts the per instant
writer heartbeat that the `LAZY` policy relies on. Two consequences matter:

* The commit of a checkpointed instant does not depend on the heartbeat of the writer that produced
it, so the restore path works no matter how long the pipeline was down. With the `LAZY` policy the
commit would fail with `Heartbeat ... has expired`, or a cleanup of the table would roll the
instant back, once the writer had been gone for longer than
`hoodie.client.heartbeat.interval_in_ms * hoodie.client.heartbeat.tolerable.misses` (60s * 2 by
default).
* A long running job does not accumulate one heartbeat timer thread and one heartbeat file per
checkpoint, because no heartbeat is started at all.

Hudi does not clean up failed writes of an exactly-once table by itself. The connector reclaims the
instants of a failed attempt in three places: the writer rolls back the instant it still owns when it
closes, `abort` rolls back the instants of an aborted checkpoint on the engines that call it, and a
job that restores from a checkpoint rolls back every commit instant that the restored checkpoint
state does not reference. The last case is what keeps a job that restarts regularly from
accumulating orphaned instants and their files, including the instant of a checkpoint that handed
its records over but never completed.

Two cases are left to the operator: an instant left by a job that never completed a checkpoint, so
that there is no restored state to compare against, and an instant of a pipeline that is started
again from scratch instead of restored. Those instants stay on the active timeline until they are
rolled back manually, for example with Hudi's `HoodieWriteClient#rollback` or `hoodie-cli`. Because
the reclaim assumes that the restored job is the only writer of the table, do not run two
exactly-once jobs against the same table.

Requirements and trade-offs:

* The checkpoint must be enabled, otherwise no instant is ever committed and no record becomes
visible. Use `AT_LEAST_ONCE` for a batch job without checkpoint or a job that must publish the
records as soon as they are flushed.
* The records are visible only after a checkpoint completes, so the visibility latency is at least
the checkpoint interval.
* A record that Hudi rejects (for example a payload or schema conversion failure) fails the
checkpoint instead of being dropped from it, because a checkpoint that completed is never
replayed.
* The pending instants of a job are kept in the checkpoint state of the committer and in its memory
until their checkpoint completes, so a very long checkpoint interval keeps the write statuses of
that interval on the heap.
* Do not switch a running pipeline from `EXACTLY_ONCE` back to `AT_LEAST_ONCE`: the checkpoint state
of the committer is dropped in that mode, so the pending instants of the last checkpoints would
never be committed. Finish them with an `EXACTLY_ONCE` run, or roll them back manually.
* The instants are committed by the aggregated committer when the engine completes a checkpoint
(Zeta, Flink) or a micro batch (Spark), so the sink needs an engine that commits the commit info
of a successful checkpoint.
* Parallel writers still follow the Hudi single writer concurrency control, so two jobs (or two
writers) that update the same file group can overwrite each other. Keep a single writer per table,
or make sure the writers work on disjoint keys and partitions.
* A commit whose instant is not on the active timeline anymore, for example because an operator
or another writer of the same table rolled it back, fails the job with an explicit error instead
of silently publishing incomplete data.

## Examples

Expand Down
82 changes: 78 additions & 4 deletions docs/zh/connectors/sink/Hudi.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ merge-on-read 的 Hudi 表时,可以使用该连接器。它会写入 Hudi 数

## 主要特性

- [ ] [精确一次](../../introduction/concepts/connector-v2-features.md)
- [x] [精确一次](../../introduction/concepts/connector-v2-features.md)
- [x] [变更数据捕获](../../introduction/concepts/connector-v2-features.md)
- [x] [支持多表写入](../../introduction/concepts/connector-v2-features.md)
- [x] [定时刷新](../../introduction/concepts/connector-v2-features.md)

:::tip 精确一次语义

精确一次是可选能力,设置 `semantics = EXACTLY_ONCE` 并开启 checkpoint 后生效。默认的 `AT_LEAST_ONCE`
语义下,作业重试可能重复提交失败尝试的记录,详见[精确一次语义](#精确一次语义)。

:::

:::caution Hive Metastore 同步

SeaTunnel Hudi sink 会写入 Hudi 数据文件和 `.hoodie` 元数据,但不会在 Hive Metastore 中注册表或将表同步到 Hive Metastore。`hoodie.datasource.hive_sync.*` 配置不是受支持的 sink 选项,也不会传递给 Hudi 写入客户端。需要注册到 Hive Metastore 时,请单独运行 Apache Hudi `HiveSyncTool` 或其他表注册流程。
Expand All @@ -39,6 +46,7 @@ SeaTunnel Hudi sink 会写入 Hudi 数据文件和 `.hoodie` 元数据,但不
| schema_save_mode | enum | 否 | CREATE_SCHEMA_WHEN_NOT_EXIST | 作业启动前如何处理目标 schema。 |
| data_save_mode | enum | 否 | APPEND_DATA | 作业启动前如何处理已存在的表数据。 |
| multi_table_sink_replica | int | 否 | 1 | 多表写入模式下每张表写入器的副本数。 |
| semantics | enum | 否 | AT_LEAST_ONCE | 写入语义,`AT_LEAST_ONCE` 或 `EXACTLY_ONCE`,详见[精确一次语义](#精确一次语义)。 |
| common-options | config | 否 | - | [Sink 插件通用参数](../common-options/sink-common-options.md)。 |

表清单配置:
Expand Down Expand Up @@ -176,9 +184,75 @@ env {
}
```

Hudi 定时刷新复用连接器现有的同步批量刷新和 Hudi 客户端 auto-commit 行为。Hudi Sink 没有 2PC 精确一次
写入器,因此定时刷新提供的是至少一次语义,重试可能产生额外的 commit。使用 `INSERT` 时,自动生成的
record key 还可能在恢复后产生重复行;使用具有稳定 `record_key_fields` 的 `UPSERT` 可以减少逻辑记录重复。
Hudi 定时刷新复用连接器现有的同步批量刷新。默认的 `AT_LEAST_ONCE` 语义下,Hudi 客户端会立即提交每个刷新的
批次,因此定时刷新提供的是至少一次语义,重试可能产生额外的 commit。当 `semantics = EXACTLY_ONCE` 时,定时刷新
只会把记录写入当前 checkpoint 对应的 instant,等该 checkpoint 完成后才对读取方可见。

## 精确一次语义

设置 `semantics = EXACTLY_ONCE` 后,Sink 会使用引擎的两阶段提交协议写入:

```hocon
env {
checkpoint.interval = 10000
}

sink {
Hudi {
table_dfs_path = "/tmp/seatunnel_mnt/hudi"
table_name = "st_test"
semantics = "EXACTLY_ONCE"
op_type = "UPSERT"
record_key_fields = "c_bigint"
}
}
```

工作流程:

1. 作业运行期间,写入器只把记录写入一个 Hudi instant,不会提交它;同一个 checkpoint 的所有批次共用同一个 instant。
2. checkpoint 完成后,聚合提交器(aggregated committer)才提交该 checkpoint 对应的 instant。提交之后文件才对读取方可见。
3. checkpoint 的提交信息会作为 checkpoint 状态保存。如果提交丢失(例如作业在 checkpoint 完成之后、提交之前失败),
作业从该 checkpoint 恢复后会重新提交同一个 instant;已经提交过的 instant 会被跳过,因此记录不会重复发布。
4. 未完成的 checkpoint 的记录不会被提交:流水线重启后由 source 重放这些记录,失败尝试留下的 instant 由连接器自己回滚
(写入器关闭时回滚;在调用 `abort` 的引擎上由聚合提交器回滚;从 checkpoint 恢复时,表上所有未被恢复状态引用的
commit instant 都会在提交恢复的 instant 之前被回滚)。

`EXACTLY_ONCE` 会把 Hudi 客户端的失败写入清理策略设置为 `NEVER`:Hudi 既不会回滚等待 checkpoint 的 instant,也不会启动
`LAZY` 策略依赖的“每个 instant 一个写入器心跳”。这样做有两个关键效果:

* 已 checkpoint 的 instant 的提交不再依赖产生它的写入器心跳,因此无论流水线停机多久,恢复提交都能成功。若使用 `LAZY`
策略,写入器消失超过 `hoodie.client.heartbeat.interval_in_ms * hoodie.client.heartbeat.tolerable.misses`(默认
60s * 2)后,提交会抛出 `Heartbeat ... has expired`,或者表的清理动作会把这个 instant 回滚掉。
* 长时间运行的作业不会每个 checkpoint 累积一个心跳定时器线程和一个心跳文件,因为根本不会创建心跳。

`NEVER` 策略下 Hudi 不会自动清理失败写入,连接器会在三个位置回收失败尝试的 instant:写入器关闭时回滚仍由它持有的
instant;在调用 `abort` 的引擎上由聚合提交器回滚被中止 checkpoint 的 instant;从 checkpoint 恢复时,表上所有未被恢复状态
引用的 commit instant 都会在提交恢复的 instant 之前被回滚。最后一种情况可以避免频繁重启的作业不断累积孤儿 instant 及其
文件,包括那些已经把记录交给提交器、但 checkpoint 从未完成的 instant。

有两种情况仍需人工处理:作业从未完成过任何 checkpoint,因而没有恢复状态可供比对;以及流水线不是从 checkpoint 恢复,
而是从头启动。这些 instant 会一直留在 active timeline 上,需要手动回滚(例如使用 Hudi 的
`HoodieWriteClient#rollback` 或 `hoodie-cli`)。由于该回收逻辑假定恢复后的作业是表的唯一写入者,请不要让两个
`EXACTLY_ONCE` 作业同时写入同一张表。

约束与取舍:

* 必须开启 checkpoint,否则不会提交任何 instant,数据也不会可见。没有 checkpoint 的批处理作业或需要“刷新即可见”的
作业请使用 `AT_LEAST_ONCE`。
* 数据只在 checkpoint 完成后可见,因此可见性延迟至少是一个 checkpoint 间隔。
* 被 Hudi 拒绝的记录(例如 payload 或 schema 转换失败)会让本次 checkpoint 失败,而不是被静默丢弃——因为已完成的
checkpoint 不会再被重放。
* 待提交的 instant 会在聚合提交器的 checkpoint 状态与内存中保留到对应 checkpoint 完成,因此 checkpoint 间隔很长时,
这段时间的 write status 会占用堆内存。
* 不要在运行中把 `EXACTLY_ONCE` 改回 `AT_LEAST_ONCE`:该模式下聚合提交器的 checkpoint 状态会被丢弃,最近几个 checkpoint
的待提交 instant 将永远不会被提交。请用一次 `EXACTLY_ONCE` 运行把它们提交完,或手动回滚。
* instant 由聚合提交器在引擎完成 checkpoint(Zeta、Flink)或微批(Spark)后提交,因此需要使用会提交成功
checkpoint 对应 commit info 的引擎。
* 并行写入仍然遵循 Hudi 的单写入者并发控制,两个作业(或两个写入器)更新同一个 file group 时可能互相覆盖。请保证
同一张表只有一个写入者,或让写入器处理互不相交的 key 和分区。
* 如果提交时 instant 已经不在 active timeline 上(例如被运维人员或同一张表的其他写入器回滚),作业会以明确的错误
失败,而不是静默发布不完整的数据。

## 示例

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.connectors.seatunnel.hudi.config;

/** The write semantics supported by the Hudi sink. */
public enum HudiSemantics {

/**
* Every flushed batch is committed to Hudi immediately. Data becomes visible as soon as the
* batch is flushed, and a failed job can produce duplicated commits when the source replays
* records, so this semantics is at-least-once.
*/
AT_LEAST_ONCE,

/**
* Records are written into a Hudi instant while the job is running, but the instant is only
* committed after the checkpoint that contains it completes. The write follows the two-phase
* commit protocol of the engine, so a failed job never commits the data of a checkpoint that
* was not completed, which gives exactly-once semantics. Data is visible only after a
* checkpoint completes.
*/
EXACTLY_ONCE
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class HudiSinkConfig implements Serializable {

private DataSaveMode dataSaveMode;

private HudiSemantics semantics;

public static HudiSinkConfig of(ReadonlyConfig config) {
Builder builder = HudiSinkConfig.builder();
Optional<SchemaSaveMode> optionalSchemaSaveMode =
Expand All @@ -59,6 +61,18 @@ public static HudiSinkConfig of(ReadonlyConfig config) {
optionalSchemaSaveMode.orElseGet(HudiSinkOptions.SCHEMA_SAVE_MODE::defaultValue));
builder.dataSaveMode(
optionalDataSaveMode.orElseGet(HudiSinkOptions.DATA_SAVE_MODE::defaultValue));
builder.semantics(
config.getOptional(HudiSinkOptions.SEMANTICS)
.orElseGet(HudiSinkOptions.SEMANTICS::defaultValue));
return builder.build();
}

/**
* Whether the sink writes with the two-phase commit protocol of the engine.
*
* @return true when the sink must only commit after a checkpoint completes
*/
public boolean isExactlyOnce() {
return HudiSemantics.EXACTLY_ONCE.equals(semantics);
}
}
Loading
Loading