Skip to content

Commit 9c82e05

Browse files
Avoid output inside try-catch in Java IO (#39124)
* Avoid output inside try-catch in Java IO * Preserve null handling when moving output * Apply suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Apply Spotless formatting --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 9ae08d5 commit 9c82e05

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,9 +2105,13 @@ public static <T> void readSource(
21052105
// the same order.
21062106
BoundedSource.BoundedReader<T> reader = streamSource.createReader(options);
21072107

2108+
T current = null;
2109+
boolean hasCurrent = false;
21082110
try {
21092111
if (reader.start()) {
2110-
outputReceiver.get(rowTag).output(reader.getCurrent());
2112+
current =
2113+
java.util.Objects.requireNonNull(reader.getCurrent(), "Reader returned null element");
2114+
hasCurrent = true;
21112115
} else {
21122116
return;
21132117
}
@@ -2120,11 +2124,17 @@ public static <T> void readSource(
21202124
(Exception) e.getCause(),
21212125
"Unable to parse record reading from BigQuery");
21222126
}
2127+
if (hasCurrent) {
2128+
outputReceiver.get(rowTag).output(current);
2129+
}
21232130

21242131
while (true) {
2132+
current = null;
2133+
hasCurrent = false;
21252134
try {
21262135
if (reader.advance()) {
2127-
outputReceiver.get(rowTag).output(reader.getCurrent());
2136+
current = reader.getCurrent();
2137+
hasCurrent = true;
21282138
} else {
21292139
return;
21302140
}
@@ -2137,6 +2147,9 @@ public static <T> void readSource(
21372147
(Exception) e.getCause(),
21382148
"Unable to parse record reading from BigQuery");
21392149
}
2150+
if (hasCurrent) {
2151+
outputReceiver.get(rowTag).output(current);
2152+
}
21402153
}
21412154
}
21422155

sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/healthcare/FhirIO.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,9 @@ public void instantiateHealthcareClient() throws IOException {
602602
@ProcessElement
603603
public void processElement(ProcessContext context) {
604604
String resourceId = context.element();
605+
String resource = null;
605606
try {
606-
context.output(fetchResource(this.client, resourceId));
607+
resource = java.util.Objects.requireNonNull(fetchResource(this.client, resourceId));
607608
} catch (Exception e) {
608609
READ_RESOURCE_ERRORS.inc();
609610
LOG.warn(
@@ -612,6 +613,9 @@ public void processElement(ProcessContext context) {
612613
e);
613614
context.output(FhirIO.Read.DEAD_LETTER, HealthcareIOError.of(resourceId, e));
614615
}
616+
if (resource != null) {
617+
context.output(resource);
618+
}
615619
}
616620

617621
private String fetchResource(HealthcareApiClient client, String resourceName)

sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/healthcare/HL7v2IO.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,15 @@ public void instantiateHealthcareClient() throws IOException {
365365
@ProcessElement
366366
public void processElement(ProcessContext context) {
367367
String msgId = context.element();
368+
HL7v2Message message = null;
368369
try {
369-
context.output(client.fetchMessage(msgId));
370+
message = java.util.Objects.requireNonNull(client.fetchMessage(msgId));
370371
} catch (Exception e) {
371372
context.output(HL7v2IO.Read.DEAD_LETTER, HealthcareIOError.of(msgId, e));
372373
}
374+
if (message != null) {
375+
context.output(message);
376+
}
373377
}
374378
}
375379
}
@@ -487,15 +491,20 @@ public void instantiateHealthcareClient() throws IOException {
487491
@ProcessElement
488492
public void processElement(ProcessContext context) {
489493
String msgId = context.element().getHl7v2MessageId();
494+
HL7v2ReadResponse response = null;
490495
try {
491-
HL7v2ReadResponse response =
492-
HL7v2ReadResponse.of(context.element().getMetadata(), client.fetchMessage(msgId));
493-
context.output(response);
496+
response =
497+
java.util.Objects.requireNonNull(
498+
HL7v2ReadResponse.of(
499+
context.element().getMetadata(), client.fetchMessage(msgId)));
494500
} catch (Exception e) {
495501
HealthcareIOError<HL7v2ReadParameter> error =
496502
HealthcareIOError.of(context.element(), e);
497503
context.output(HL7v2IO.HL7v2Read.DEAD_LETTER, error);
498504
}
505+
if (response != null) {
506+
context.output(response);
507+
}
499508
}
500509
}
501510
}

0 commit comments

Comments
 (0)