-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Wire error handling into PubSubIO and add initial tests #30372
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d06203
Wire error handling into PubSubIO and add initial tests
johnjcasey 5219920
Merge remote-tracking branch 'origin/master' into feature/pubsubio-dlq
johnjcasey 183c7de
update comments to reflect that schema mismatches are not routed to DLQs
johnjcasey ecc1b68
spotless
johnjcasey ef9df67
address nits
johnjcasey 4ba2682
Merge branch 'master' into feature/pubsubio-dlq
johnjcasey 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
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 |
---|---|---|
|
@@ -20,10 +20,13 @@ | |
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import javax.naming.SizeLimitExceededException; | ||
import org.apache.beam.sdk.coders.Coder; | ||
import org.apache.beam.sdk.transforms.DoFn; | ||
import org.apache.beam.sdk.transforms.SerializableFunction; | ||
import org.apache.beam.sdk.transforms.errorhandling.BadRecordRouter; | ||
import org.apache.beam.sdk.transforms.windowing.BoundedWindow; | ||
import org.apache.beam.sdk.transforms.windowing.PaneInfo; | ||
import org.apache.beam.sdk.values.TupleTag; | ||
import org.apache.beam.sdk.values.ValueInSingleWindow; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.joda.time.Instant; | ||
|
@@ -41,6 +44,12 @@ public class PreparePubsubWriteDoFn<InputT> extends DoFn<InputT, PubsubMessage> | |
private SerializableFunction<ValueInSingleWindow<InputT>, PubsubMessage> formatFunction; | ||
@Nullable SerializableFunction<ValueInSingleWindow<InputT>, PubsubIO.PubsubTopic> topicFunction; | ||
|
||
private final BadRecordRouter badRecordRouter; | ||
|
||
private final Coder<InputT> inputCoder; | ||
|
||
private final TupleTag<PubsubMessage> outputTag; | ||
|
||
static int validatePubsubMessageSize(PubsubMessage message, int maxPublishBatchSize) | ||
throws SizeLimitExceededException { | ||
int payloadSize = message.getPayload().length; | ||
|
@@ -113,10 +122,16 @@ static int validatePubsubMessageSize(PubsubMessage message, int maxPublishBatchS | |
SerializableFunction<ValueInSingleWindow<InputT>, PubsubMessage> formatFunction, | ||
@Nullable | ||
SerializableFunction<ValueInSingleWindow<InputT>, PubsubIO.PubsubTopic> topicFunction, | ||
int maxPublishBatchSize) { | ||
int maxPublishBatchSize, | ||
BadRecordRouter badRecordRouter, | ||
Coder<InputT> inputCoder, | ||
TupleTag<PubsubMessage> outputTag) { | ||
this.formatFunction = formatFunction; | ||
this.topicFunction = topicFunction; | ||
this.maxPublishBatchSize = maxPublishBatchSize; | ||
this.badRecordRouter = badRecordRouter; | ||
this.inputCoder = inputCoder; | ||
this.outputTag = outputTag; | ||
} | ||
|
||
@ProcessElement | ||
|
@@ -125,18 +140,42 @@ public void process( | |
@Timestamp Instant ts, | ||
BoundedWindow window, | ||
PaneInfo paneInfo, | ||
OutputReceiver<PubsubMessage> o) { | ||
MultiOutputReceiver o) | ||
throws Exception { | ||
ValueInSingleWindow<InputT> valueInSingleWindow = | ||
ValueInSingleWindow.of(element, ts, window, paneInfo); | ||
PubsubMessage message = formatFunction.apply(valueInSingleWindow); | ||
PubsubMessage message; | ||
try { | ||
message = formatFunction.apply(valueInSingleWindow); | ||
} catch (Exception e) { | ||
badRecordRouter.route( | ||
o, | ||
element, | ||
inputCoder, | ||
e, | ||
"Failed to serialize PubSub message with provided format function"); | ||
return; | ||
} | ||
if (topicFunction != null) { | ||
message = message.withTopic(topicFunction.apply(valueInSingleWindow).asPath()); | ||
try { | ||
message = message.withTopic(topicFunction.apply(valueInSingleWindow).asPath()); | ||
} catch (Exception e) { | ||
badRecordRouter.route( | ||
o, element, inputCoder, e, "Failed to determine PubSub topic using topic function"); | ||
return; | ||
} | ||
} | ||
try { | ||
validatePubsubMessageSize(message, maxPublishBatchSize); | ||
} catch (SizeLimitExceededException e) { | ||
throw new IllegalArgumentException(e); | ||
badRecordRouter.route( | ||
o, | ||
element, | ||
inputCoder, | ||
new IllegalArgumentException(e), | ||
"PubSub message size limit exceeded"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: to avoid confusion, can we make this message more generic? Looking at the validation code, it can fail for reasons other than message size (e.g. attribute key/value size too large) |
||
return; | ||
} | ||
o.output(message); | ||
o.get(outputTag).output(message); | ||
} | ||
} |
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.
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.
Should we just add valueInSingleWindow into the error message here so its easier to see what went wrong?
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 this case, I'm adding the input element, which is what should be meaningful to the user