-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add deterministic redistribute sharding for KafkaIO read. #36112
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
403e2b4
Add deterministic redistribute sharding for KafkaIO read.
tomstepp a84ab49
Address PR feedback.
tomstepp 0627d94
Provide more detailed transform name for the redistribute.
tomstepp bc96165
Address spotless precommit findings.
tomstepp 65621c8
Address spotless precommit findings.
tomstepp a611d9b
Keep redistribute transform name the same.
tomstepp 1ba7206
Add deterministic sharding unit test.
tomstepp b4abac1
Refactor to specific deterministic Kafka redistribute method.
tomstepp b36e2ec
Add redistribute by key variant.
tomstepp 4496030
Add test of sharding fns.
tomstepp 99d8b78
Add bucketing to redistributeByKey and add option to redistribute by …
tomstepp 89a1244
Actually enable withRedistributeByRecordKey in KafkaIOTest.
tomstepp 760f297
Add byRecordKey property to Kafka read compatibility.
tomstepp 638feab
Fix comma formatting for mkKafkaReadTransform.
tomstepp 1447890
Fix cases where reader was not overwritten when building Kafka reader.
tomstepp 6489053
Rebase and revert method rename for debugging.
tomstepp cf907b7
Address spotless finding for makeKafkaRecord.
tomstepp 06469c5
Add tests for deterministic sharding.
tomstepp ebef848
numBuckets as UnsignedInteger to reduce conversion overhead, and clar…
tomstepp 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
124 changes: 124 additions & 0 deletions
124
sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaReadRedistribute.java
This file contains hidden or 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,124 @@ | ||
| /* | ||
| * 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.beam.sdk.io.kafka; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
|
||
| import org.apache.beam.sdk.transforms.DoFn; | ||
| import org.apache.beam.sdk.transforms.PTransform; | ||
| import org.apache.beam.sdk.transforms.ParDo; | ||
| import org.apache.beam.sdk.transforms.Redistribute; | ||
| import org.apache.beam.sdk.transforms.Values; | ||
| import org.apache.beam.sdk.values.KV; | ||
| import org.apache.beam.sdk.values.PCollection; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.hash.Hashing; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.primitives.UnsignedInteger; | ||
| import org.checkerframework.checker.nullness.qual.NonNull; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| public class KafkaReadRedistribute<K, V> | ||
| extends PTransform<PCollection<KafkaRecord<K, V>>, PCollection<KafkaRecord<K, V>>> { | ||
| public static <K, V> KafkaReadRedistribute<K, V> byOffsetShard(@Nullable Integer numBuckets) { | ||
| return new KafkaReadRedistribute<>(numBuckets, false); | ||
| } | ||
|
|
||
| public static <K, V> KafkaReadRedistribute<K, V> byRecordKey(@Nullable Integer numBuckets) { | ||
| return new KafkaReadRedistribute<>(numBuckets, true); | ||
| } | ||
|
|
||
| // The number of buckets to shard into. | ||
| private @Nullable Integer numBuckets = null; | ||
| // When redistributing, group records by the Kafka record's key instead of by offset hash. | ||
| private boolean byRecordKey = false; | ||
|
|
||
| private KafkaReadRedistribute(@Nullable Integer numBuckets, boolean byRecordKey) { | ||
| this.numBuckets = numBuckets; | ||
| this.byRecordKey = byRecordKey; | ||
| } | ||
|
|
||
| @Override | ||
| public PCollection<KafkaRecord<K, V>> expand(PCollection<KafkaRecord<K, V>> input) { | ||
|
|
||
| if (byRecordKey) { | ||
| return input | ||
| .apply("Pair with shard from key", ParDo.of(new AssignRecordKeyFn<K, V>(numBuckets))) | ||
| .apply(Redistribute.<Integer, KafkaRecord<K, V>>byKey().withAllowDuplicates(false)) | ||
| .apply(Values.create()); | ||
| } | ||
|
|
||
| return input | ||
| .apply("Pair with shard from offset", ParDo.of(new AssignOffsetShardFn<K, V>(numBuckets))) | ||
| .apply(Redistribute.<Integer, KafkaRecord<K, V>>byKey().withAllowDuplicates(false)) | ||
| .apply(Values.create()); | ||
| } | ||
|
|
||
| static class AssignOffsetShardFn<K, V> | ||
| extends DoFn<KafkaRecord<K, V>, KV<Integer, KafkaRecord<K, V>>> { | ||
| private @NonNull UnsignedInteger numBuckets; | ||
|
|
||
| public AssignOffsetShardFn(@Nullable Integer numBuckets) { | ||
| if (numBuckets != null && numBuckets > 0) { | ||
| this.numBuckets = UnsignedInteger.fromIntBits(numBuckets); | ||
| } else { | ||
| this.numBuckets = UnsignedInteger.valueOf(0); | ||
| } | ||
| } | ||
|
|
||
| @ProcessElement | ||
| public void processElement( | ||
| @Element KafkaRecord<K, V> element, | ||
| OutputReceiver<KV<Integer, KafkaRecord<K, V>>> receiver) { | ||
| int hash = Hashing.farmHashFingerprint64().hashLong(element.getOffset()).asInt(); | ||
|
|
||
| if (numBuckets != null) { | ||
| hash = UnsignedInteger.fromIntBits(hash).mod(numBuckets).intValue(); | ||
| } | ||
|
|
||
| receiver.output(KV.of(hash, element)); | ||
| } | ||
| } | ||
|
|
||
| static class AssignRecordKeyFn<K, V> | ||
| extends DoFn<KafkaRecord<K, V>, KV<Integer, KafkaRecord<K, V>>> { | ||
|
|
||
| private @NonNull UnsignedInteger numBuckets; | ||
|
|
||
| public AssignRecordKeyFn(@Nullable Integer numBuckets) { | ||
| if (numBuckets != null && numBuckets > 0) { | ||
| this.numBuckets = UnsignedInteger.fromIntBits(numBuckets); | ||
| } else { | ||
| this.numBuckets = UnsignedInteger.valueOf(0); | ||
| } | ||
| } | ||
|
|
||
| @ProcessElement | ||
| public void processElement( | ||
| @Element KafkaRecord<K, V> element, | ||
| OutputReceiver<KV<Integer, KafkaRecord<K, V>>> receiver) { | ||
| K key = element.getKV().getKey(); | ||
| String keyString = key == null ? "" : key.toString(); | ||
| int hash = Hashing.farmHashFingerprint64().hashBytes(keyString.getBytes(UTF_8)).asInt(); | ||
|
|
||
| if (numBuckets != null) { | ||
| hash = UnsignedInteger.fromIntBits(hash).mod(numBuckets).intValue(); | ||
| } | ||
|
|
||
| receiver.output(KV.of(hash, element)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.