Skip to content

perf(flink): avoid Java String conversions in upsert shuffle serialization - #21050

Open
cshuo wants to merge 1 commit into
apache:masterfrom
cshuo:perf/flink-shuffle-string-data
Open

cshuo wants to merge 1 commit into
apache:masterfrom
cshuo:perf/flink-shuffle-string-data

Conversation

@cshuo

@cshuo cshuo commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Describe the issue this Pull Request addresses

Closes #21049

HoodieFlinkInternalRow already stores its five metadata fields as Flink StringData. During upsert shuffle, the serializer converts each field through a Java String on both write and read. These conversions allocate extra objects for every row.

Summary and Changelog

  • Write the five stored StringData values directly and retain the values returned by Flink's deserializer. This preserves the existing wire format for both data rows and index-only rows.
  • Fix serializer.copy(indexRecord) throwing a NullPointerException. The copy now preserves the metadata fields and skips the absent RowData payload.
  • Keep the Java String getters for metadata, migrate payload access from getRowData() to rowData(), and remove the four-argument data-row constructor used only by repository tests.

Impact

A single-threaded benchmark on Flink 1.20.1 and JDK 17.0.19 deserialized a row, read all five metadata fields through their Java String getters, and serialized it again. Each implementation ran in six fresh JVMs using the same 125-byte row.

Metric Baseline This PR Reduction
Median time per row 379.30 ns 285.35 ns 24.8%
Heap allocated per row 2,352–2,376 B 1,440 B 38.8–39.4%

These figures measure the serializer loop. Both implementations produced identical bytes.

Risk Level

Low. The main risks are changing the shuffle wire format or retaining bytes from a reusable input buffer. Tests compare the serialized output with independently constructed legacy bytes and overwrite the input buffer before checking the restored row. All five tests in TestHoodieFlinkInternalRowSerializer passed on Flink 1.20.1.

External code that directly uses getRowData() or the removed constructor on HoodieFlinkInternalRow must migrate and recompile.

Documentation Update

none

Contributor's checklist

  • Read through contributor's guide
  • Enough context is provided in the sections above
  • Adequate tests were added if applicable

…ation

Serialize the five metadata fields directly as StringData and retain the deserialized values. Preserve the wire format for data and index rows, and skip the absent payload when copying an index-only row.
@cshuo
cshuo requested a review from danny0405 December 3, 2026 02:51
@github-actions github-actions Bot added the size:M PR with lines of changes in (100, 300] label Dec 3, 2026
stringDataSerializer.serialize(StringData.fromString(record.getOperationType()), target);
stringDataSerializer.serialize(record.recordKey(), target);
stringDataSerializer.serialize(record.partitionPath(), target);
stringDataSerializer.serialize(record.fileId(), target);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

setFileId(null) now causes StringDataSerializer.serialize to throw NullPointerException during shuffle. I ran the same call against the base and it round-tripped as the four-character string "null" via String.valueOf. Could we preserve that behavior by normalizing the Java string inputs in the constructors and setters, and add a round-trip test that passes an actual Java null? The current compatibility test starts with the string literal.

* For serialization/deserialization {@code HoodieFlinkInternalRowTypeInfo} and {@link HoodieFlinkInternalRowSerializer} are used
* to optimize serialization/deserialization costs.
*/
@Getter

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we make the five raw StringData getters package-private? On Flink 1.20.1, starting with the key "key", row.recordKey().toBytes()[0] = 'x' changes the bytes used by this serializer. row.getRecordKey() still returns "key", while deserializing that same row yields "xey". This lets the partitioner and the downstream operator see different keys. The serializer can access package-private getters directly.

}
return record;
RowData rowData = rowDataSerializer.deserialize(source);
return new HoodieFlinkInternalRow(false, recordKey, partition, fileId, instantTime, operationType, rowData);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keeping all five deserialized fields increases the live heap of the record-index minibatch. On Flink 1.20.1/JDK 17, JOL measured 516,992 bytes reachable from a list of 1,000 freshly deserialized rows on the base and 1,044,976 bytes on this commit (one INT payload, distinct keys, one partition, and three empty metadata fields per row). Even an empty field retains its own byte array and Flink memory-segment objects until the batch is flushed. Could we share an empty StringData for zero-length metadata and remeasure the buffered batch alongside the allocation benchmark?

this(recordKey, partitionPath, "", "", "", false, rowData);
}

public HoodieFlinkInternalRow(String recordKey, String partitionPath, String operationType, RowData rowData) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we keep this four-argument constructor and the existing getRowData() getter? A caller compiled against the base throws NoSuchMethodError for each when run against this commit. Keeping both signatures preserves binary compatibility and lets the existing sink and test callers stay as they are. The serializer optimization only needs direct access to the five metadata fields.

@codecov-commenter

codecov-commenter commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.32%. Comparing base (73d12db) to head (1a6402f).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
...ache/hudi/client/model/HoodieFlinkInternalRow.java 90.90% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #21050      +/-   ##
============================================
- Coverage     81.06%   80.32%   -0.75%     
+ Complexity    37278    36923     -355     
============================================
  Files          2608     2608              
  Lines        150876   150895      +19     
  Branches      18931    18936       +5     
============================================
- Hits         122302   121199    -1103     
- Misses        20063    21224    +1161     
+ Partials       8511     8472      -39     
Components Coverage Δ
hudi-common 84.24% <ø> (-0.17%) ⬇️
hudi-client 83.60% <ø> (-0.70%) ⬇️
hudi-flink 84.78% <95.65%> (-1.56%) ⬇️
hudi-spark-datasource 72.49% <ø> (-2.21%) ⬇️
hudi-utilities 79.29% <ø> (-0.03%) ⬇️
hudi-cli 70.92% <ø> (ø)
hudi-hadoop 71.15% <ø> (-0.12%) ⬇️
hudi-sync 77.37% <ø> (ø)
hudi-io 81.66% <ø> (ø)
hudi-timeline-service 83.15% <ø> (+0.54%) ⬆️
hudi-cloud 81.01% <ø> (ø)
hudi-kafka-connect 58.40% <ø> (ø)
Flag Coverage Δ
flink-integration-tests 35.81% <95.65%> (-5.00%) ⬇️
hadoop-mr-java-client 45.15% <ø> (+0.02%) ⬆️
integration-tests 14.46% <ø> (-0.01%) ⬇️
spark-client-hadoop-common 38.14% <ø> (ø)
spark-java-tests 52.79% <ø> (+0.01%) ⬆️
spark-scala-tests 50.53% <ø> (-3.32%) ⬇️
utilities 42.27% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...client/model/HoodieFlinkInternalRowSerializer.java 92.15% <100.00%> (-1.40%) ⬇️
...java/org/apache/hudi/sink/StreamWriteFunction.java 80.93% <100.00%> (ø)
...he/hudi/sink/partitioner/BucketAssignFunction.java 95.87% <100.00%> (ø)
...ache/hudi/client/model/HoodieFlinkInternalRow.java 97.29% <90.90%> (-2.71%) ⬇️

... and 145 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hudi-bot

hudi-bot commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

CI report:

Bot commands @hudi-bot supports the following commands:
  • @hudi-bot run azure re-run the last Azure build

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M PR with lines of changes in (100, 300]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flink] Avoid Java String conversions in upsert shuffle serialization

4 participants