-
Notifications
You must be signed in to change notification settings - Fork 1
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
liurenjie1024
wants to merge
4
commits into
main
Choose a base branch
from
renjie/iceberg-sink-opt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−0
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
||
```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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. @st1pageThis is very similar with this design, the only problem is that previously we didn't distinguish partition key from primary key.
There was a problem hiding this comment.
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 propertyprimary_key
for both PK and partition key, depending on different connectors, for example,primary_key
as partition key & PKprimary_key
as partition key onlyWhile this RFC proposes to introduce 2 different properties: partition_key and primary_key respectively, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clarify the condition can lead to the issue: when the partition key does not contain the whole stream key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, by
primary_key
here I meanstream_key
of sink's input. I think for iceberg we don't need user to specifyprimary_key
here?There was a problem hiding this comment.
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.CREATE SINK
statement to define the behavior of the RW sink.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 wrongCREATE SINK
because our fe node can get the information from the downstream system's catalog