Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add shuffle for iceberg sink #77

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
72 changes: 72 additions & 0 deletions rfcs/0077-iceberg-sink-shuffle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
feature: Improve shuffle in iceberg sink
authors:
- "Renjie Liu"
start_date: "2023/11/8"
---

# Shuffle according to iceberg's partition spec

## Motivation

Apache iceberg allows users to define partition spec[1] for a table. The partition spec defines how data is partitioned and stored in the table. For example, a table can be partitioned by date and hour. Also, it's required that each data file in apache iceberg can contain only one partition value. In our current iceberg sink implementation, we don't do any shuffle. This means that if the table is partitioned by bucket, we will have a lot of small files in the table. This is not good for performance. We should shuffle the data according to the partition spec.

## Design

We will add a shuffle operator before sending data to iceberg sink. For example, let's assume the iceberg table is partitioned by following partition spec:

```sql
CREATE TABLE prod.db.sample (
id bigint,
data string,
category string,
ts timestamp)
USING iceberg
PARTITIONED BY (bucket(16, id), years(ts))
```

If we don't have the shuffle operator, the writing process will be like following:

![image](images/0077-iceberg-sink-shuffle/before_shuffle.svg)

After we add the shuffle operator, the writing process will be like following:

![image](images/0077-iceberg-sink-shuffle/after_shuffle.svg)

This way we can reduce the number of data files in the table, which helps to improve the read performance of iceberg.

### Implementation

There are two possible implementations for this feature:

1. Add a new `IcebergPartitionOperator`, which calculates the partition value of each record and adds it to the record, then asking the dispatcher executor to do hash shuffle according to the partition value. The plan is like following:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, our common sinks also have such a Shuffle when the sink's PK doesn't match its stream key. cc. @st1page

This is very similar with this design, the only problem is that previously we didn't distinguish partition key from primary key.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also cc. @xiangjinwu @tabVersion

Previously in channel #wg-new-source-ddl and risingwavelabs/risingwave#9443, we decided to use the sink property primary_key for both PK and partition key, depending on different connectors, for example,

  • Kafka upsert sink will use primary_key as partition key & PK
  • Kafka append-only sink will use primary_key as partition key only

While this RFC proposes to introduce 2 different properties: partition_key and primary_key respectively, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, our common sinks also have such a Shuffle when the sink's PK doesn't match its stream key

clarify the condition can lead to the issue: when the partition key does not contain the whole stream key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this RFC proposes to introduce 2 different properties: partition_key and primary_key respectively, right?

In fact, by primary_key here I mean stream_key of sink's input. I think for iceberg we don't need user to specify primary_key here?

Copy link
Contributor

@st1page st1page Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optimizer must know the downstream's partition requirements. in the past, we were just concerned about correctness issues such as "make sure the order of operations for the specific key" or "no multiple parallelisms modifying the same key, which can bring dead-lock". And in this RFC, if the downstream system has a stronger partition key to achieving better performance, we can use it as the distribution strategy and the correctness issues should be guaranteed by the downstream system.

About if the user needs to specify the primary_key/partition key in the CREATE SINK statement.

  • for kafka/redis, the primary key/partition key has not a table level's catalog or config, user must give it in CREATE SINK statement to define the behavior of the RW sink.
  • for MySQL/JDBC, current behavior is that requiring users to specify the primary_key in the CREATE SINK statement. It will be validated in the meta node if the user-specified pk is the same as the JDBC catalog. It is because currently, frontend can not query the JDBC's catalog without java runtime c.c. @StrikeW correct me if I am wrong
  • and for iceberg in this RFC, I think user do not need to specify any information in the CREATE SINK because our fe node can get the information from the downstream system's catalog


```mermaid
flowchart TD
A[Source] --> B[IcebergPartitionOperator]
B --> C[DispatcherExecutor]
C -->|"partition_value"| D[IcebergSink]
```

The `IcebergPartitionExecutor` will be a `StreamExecutor`, which calculates the partition value of each record and adds it to the record. The `DispatcherExecutor` doesn't need to change much, and do hash shuffle according to the partition value.

2. Extend dispatcher executor to support iceberg partition shuffle. The plan is like following:

```mermaid
flowchart TD
A[Source] --> B[DispatcherExecutor]
B -->|IcebergDispatcher| C[IcebergSink]
```

In this approach we need to add an extra `IcebergDispatcher` to dispatcher executor. The `IcebergDispatcher` will calculate the partition value of each record and do hash shuffle according to the partition value.

I prefer approach 1 since it's more extensible and does not change too much current shuffle implementation, e.g. other lakehouse sinks (delta lake) could have similar approach.
fuyufjh marked this conversation as resolved.
Show resolved Hide resolved

### Caveats

When iceberg partition spec only contains range partitions(e.g. year, month, day), we don't need to do this shuffle, otherwise all traffic will go to same sink.
fuyufjh marked this conversation as resolved.
Show resolved Hide resolved
Also we need to reject upsert queries where partition columns is not a subset of the `stream_pk`.

## References

1. https://iceberg.apache.org/spec/#partitioning
21 changes: 21 additions & 0 deletions rfcs/images/0077-iceberg-sink-shuffle/after_shuffle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading