-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Sharding vortex #16795
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
Sharding vortex #16795
Changes from all commits
Commits
Show all changes
3 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,7 @@ public class StorageApiLoads<DestinationT, ElementT> | |
| private final BigQueryServices bqServices; | ||
| private final int numShards; | ||
| private final boolean allowInconsistentWrites; | ||
| private final boolean allowAutosharding; | ||
|
|
||
| public StorageApiLoads( | ||
| Coder<DestinationT> destinationCoder, | ||
|
|
@@ -61,7 +62,8 @@ public StorageApiLoads( | |
| Duration triggeringFrequency, | ||
| BigQueryServices bqServices, | ||
| int numShards, | ||
| boolean allowInconsistentWrites) { | ||
| boolean allowInconsistentWrites, | ||
| boolean allowAutosharding) { | ||
| this.destinationCoder = destinationCoder; | ||
| this.dynamicDestinations = dynamicDestinations; | ||
| this.createDisposition = createDisposition; | ||
|
|
@@ -70,6 +72,7 @@ public StorageApiLoads( | |
| this.bqServices = bqServices; | ||
| this.numShards = numShards; | ||
| this.allowInconsistentWrites = allowInconsistentWrites; | ||
| this.allowAutosharding = allowAutosharding; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -136,9 +139,6 @@ public WriteResult expandTriggered( | |
| // Handle triggered, low-latency loads into BigQuery. | ||
| PCollection<KV<DestinationT, ElementT>> inputInGlobalWindow = | ||
| input.apply("rewindowIntoGlobal", Window.into(new GlobalWindows())); | ||
|
|
||
| // First shard all the records. | ||
| // TODO(reuvenlax): Add autosharding support so that users don't have to pick a shard count. | ||
| PCollectionTuple result = | ||
| inputInGlobalWindow | ||
| .apply( | ||
|
|
@@ -154,43 +154,33 @@ public WriteResult expandTriggered( | |
| successfulRowsTag, | ||
| BigQueryStorageApiInsertErrorCoder.of(), | ||
| successCoder)); | ||
| PCollection<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> shardedRecords = | ||
| result | ||
| .get(successfulRowsTag) | ||
| .apply( | ||
| "AddShard", | ||
| ParDo.of( | ||
| new DoFn< | ||
| KV<DestinationT, StorageApiWritePayload>, | ||
| KV<ShardedKey<DestinationT>, StorageApiWritePayload>>() { | ||
| int shardNumber; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| shardNumber = ThreadLocalRandom.current().nextInt(numShards); | ||
| } | ||
|
|
||
| @ProcessElement | ||
| public void processElement( | ||
| @Element KV<DestinationT, StorageApiWritePayload> element, | ||
| OutputReceiver<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> o) { | ||
| DestinationT destination = element.getKey(); | ||
| ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); | ||
| buffer.putInt(++shardNumber % numShards); | ||
| o.output( | ||
| KV.of(ShardedKey.of(destination, buffer.array()), element.getValue())); | ||
| } | ||
| })) | ||
| .setCoder(KvCoder.of(ShardedKey.Coder.of(destinationCoder), payloadCoder)); | ||
|
|
||
| PCollection<KV<ShardedKey<DestinationT>, Iterable<StorageApiWritePayload>>> groupedRecords = | ||
| shardedRecords.apply( | ||
| "GroupIntoBatches", | ||
| GroupIntoBatches.<ShardedKey<DestinationT>, StorageApiWritePayload>ofByteSize( | ||
| MAX_BATCH_SIZE_BYTES, | ||
| (StorageApiWritePayload e) -> (long) e.getPayload().length) | ||
| .withMaxBufferingDuration(triggeringFrequency)); | ||
|
|
||
| PCollection<KV<ShardedKey<DestinationT>, Iterable<StorageApiWritePayload>>> groupedRecords; | ||
|
|
||
| if (this.allowAutosharding) { | ||
| groupedRecords = | ||
| result | ||
| .get(successfulRowsTag) | ||
| .apply( | ||
| "GroupIntoBatches", | ||
| GroupIntoBatches.<DestinationT, StorageApiWritePayload>ofByteSize( | ||
| MAX_BATCH_SIZE_BYTES, | ||
| (StorageApiWritePayload e) -> (long) e.getPayload().length) | ||
| .withMaxBufferingDuration(triggeringFrequency) | ||
| .withShardedKey()); | ||
|
|
||
| } else { | ||
| PCollection<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> shardedRecords = | ||
|
||
| createShardedKeyValuePairs(result) | ||
| .setCoder(KvCoder.of(ShardedKey.Coder.of(destinationCoder), payloadCoder)); | ||
| groupedRecords = | ||
| shardedRecords.apply( | ||
| "GroupIntoBatches", | ||
| GroupIntoBatches.<ShardedKey<DestinationT>, StorageApiWritePayload>ofByteSize( | ||
| MAX_BATCH_SIZE_BYTES, | ||
| (StorageApiWritePayload e) -> (long) e.getPayload().length) | ||
| .withMaxBufferingDuration(triggeringFrequency)); | ||
| } | ||
| groupedRecords.apply( | ||
| "StorageApiWriteSharded", | ||
| new StorageApiWritesShardedRecords<>( | ||
|
|
@@ -207,6 +197,35 @@ public void processElement( | |
| result.get(failedRowsTag)); | ||
| } | ||
|
|
||
| private PCollection<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> | ||
| createShardedKeyValuePairs(PCollectionTuple pCollection) { | ||
| return pCollection | ||
| .get(successfulRowsTag) | ||
| .apply( | ||
| "AddShard", | ||
| ParDo.of( | ||
| new DoFn< | ||
| KV<DestinationT, StorageApiWritePayload>, | ||
| KV<ShardedKey<DestinationT>, StorageApiWritePayload>>() { | ||
| int shardNumber; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| shardNumber = ThreadLocalRandom.current().nextInt(numShards); | ||
| } | ||
|
|
||
| @ProcessElement | ||
| public void processElement( | ||
| @Element KV<DestinationT, StorageApiWritePayload> element, | ||
| OutputReceiver<KV<ShardedKey<DestinationT>, StorageApiWritePayload>> o) { | ||
| DestinationT destination = element.getKey(); | ||
| ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); | ||
| buffer.putInt(++shardNumber % numShards); | ||
| o.output(KV.of(ShardedKey.of(destination, buffer.array()), element.getValue())); | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| public WriteResult expandUntriggered( | ||
| PCollection<KV<DestinationT, ElementT>> input, | ||
| Coder<KV<DestinationT, StorageApiWritePayload>> successCoder) { | ||
|
|
||
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
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.
Does autosharding default to true or false?
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 Write method sets autosharding as false by default:
beam/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java
Line 1768 in 0fbecde