Skip to content

Commit 6139128

Browse files
authored
[Cherrypick] Fix suppressed Spotbugs issuest (#35402)
[Cherrypick] Fix suppressed Spotbugs issues
2 parents d16b677 + 6c42f85 commit 6139128

File tree

7 files changed

+20
-23
lines changed

7 files changed

+20
-23
lines changed

examples/java/src/main/java/org/apache/beam/examples/complete/datatokenization/utils/SchemasUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
2222

2323
import com.google.api.services.bigquery.model.TableSchema;
24-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2524
import java.io.IOException;
2625
import java.io.InputStream;
2726
import java.io.Reader;
@@ -88,13 +87,16 @@ private void parseJson(String jsonSchema) throws UnsupportedOperationException {
8887
jsonBeamSchema = BigQueryHelpers.toJsonString(schema.getFields());
8988
}
9089

91-
@SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
9290
private void validateSchemaTypes(TableSchema bigQuerySchema) {
91+
if (bigQuerySchema == null) {
92+
LOG.error("Provided BigQuery schema is null. Please check your input.");
93+
return;
94+
}
9395
try {
9496
beamSchema = fromTableSchema(bigQuerySchema);
9597
} catch (UnsupportedOperationException exception) {
9698
LOG.error("Check json schema, {}", exception.getMessage());
97-
} catch (NullPointerException npe) {
99+
} catch (Exception e) {
98100
LOG.error("Missing schema keywords, please check what all required fields presented");
99101
}
100102
}

examples/java/src/main/java/org/apache/beam/examples/subprocess/SubProcessPipelineOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public SubProcessConfiguration create(PipelineOptions options) {
8282
configuration.setWorkerPath(subProcessPipelineOptions.getWorkerPath());
8383
configuration.setWaitTime(subProcessPipelineOptions.getWaitTime());
8484
configuration.setOnlyUpLoadLogsOnError(subProcessPipelineOptions.getOnlyUpLoadLogsOnError());
85-
configuration.concurrency = subProcessPipelineOptions.getConcurrency();
85+
configuration.setConcurrency(subProcessPipelineOptions.getConcurrency());
8686

8787
return configuration;
8888
}

examples/java/src/main/java/org/apache/beam/examples/subprocess/configuration/SubProcessConfiguration.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,29 @@
1717
*/
1818
package org.apache.beam.examples.subprocess.configuration;
1919

20-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2120
import java.io.Serializable;
2221

2322
/**
2423
* Configuration file used to setup the Process kernel for execution of the external library Values
2524
* are copied from the Options to all them to be Serializable.
2625
*/
2726
@SuppressWarnings({"serial", "nullness"}) // TODO(https://github.com/apache/beam/issues/20497)
28-
@SuppressFBWarnings("PA_PUBLIC_PRIMITIVE_ATTRIBUTE") // TODO(#35312)
2927
public class SubProcessConfiguration implements Serializable {
3028

3129
// Source GCS directory where the C++ library is located gs://bucket/tests
32-
public String sourcePath;
30+
private String sourcePath;
3331

3432
// Working directory for the process I/O
35-
public String workerPath;
33+
private String workerPath;
3634

3735
// The maximum time to wait for the sub-process to complete
38-
public Integer waitTime;
36+
private Integer waitTime;
3937

4038
// "As sub-processes can be heavy weight match the concurrency level to num cores on the machines"
41-
public Integer concurrency;
39+
private Integer concurrency;
4240

4341
// Should log files only be uploaded if error
44-
public Boolean onlyUpLoadLogsOnError;
42+
private Boolean onlyUpLoadLogsOnError;
4543

4644
public Boolean getOnlyUpLoadLogsOnError() {
4745
return onlyUpLoadLogsOnError;

examples/kotlin/src/main/java/org/apache/beam/examples/kotlin/cookbook/TriggerExample.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,8 @@ object TriggerExample {
486486

487487
@ProcessElement
488488
@Throws(Exception::class)
489-
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE")
490489
fun processElement(c: ProcessContext) {
491490
var timestamp = Instant.now()
492-
val random = Random()
493491
if (random.nextDouble() < THRESHOLD) {
494492
val range = MAX_DELAY - MIN_DELAY
495493
val delayInMinutes = random.nextInt(range) + MIN_DELAY
@@ -504,6 +502,7 @@ object TriggerExample {
504502
// MIN_DELAY and MAX_DELAY in minutes.
505503
private const val MIN_DELAY = 1
506504
private const val MAX_DELAY = 100
505+
private val random = Random()
507506
}
508507
}
509508

sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaExactlyOnceSink.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import com.fasterxml.jackson.annotation.JsonProperty;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
26-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2726
import java.io.IOException;
2827
import java.util.HashMap;
2928
import java.util.Iterator;
@@ -238,6 +237,7 @@ public void processElement(@StateId(NEXT_ID) ValueState<Long> nextIdState, Proce
238237
private static class ExactlyOnceWriter<K, V>
239238
extends DoFn<KV<Integer, Iterable<KV<Long, TimestampedValue<ProducerRecord<K, V>>>>>, Void> {
240239

240+
private static final Random RANDOM = new Random();
241241
private static final String NEXT_ID = "nextId";
242242
private static final String MIN_BUFFERED_ID = "minBufferedId";
243243
private static final String OUT_OF_ORDER_BUFFER = "outOfOrderBuffer";
@@ -551,7 +551,6 @@ void commitTxn(long lastRecordId, Counter numTransactions) throws IOException {
551551
}
552552
}
553553

554-
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE") // TODO(#35312)
555554
private ShardWriter<K, V> initShardWriter(
556555
int shard, ValueState<String> writerIdState, long nextId) throws IOException {
557556

@@ -586,7 +585,7 @@ private ShardWriter<K, V> initShardWriter(
586585
writerId =
587586
String.format(
588587
"%X - %s",
589-
new Random().nextInt(Integer.MAX_VALUE),
588+
RANDOM.nextInt(Integer.MAX_VALUE),
590589
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
591590
.withZone(DateTimeZone.UTC)
592591
.print(DateTimeUtils.currentTimeMillis()));

sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/KafkaIOUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
2121
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState;
2222

23-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2423
import java.nio.charset.StandardCharsets;
2524
import java.util.HashMap;
2625
import java.util.Map;
@@ -40,6 +39,9 @@
4039
* KafkaIO.ReadSourceDescriptors}.
4140
*/
4241
public final class KafkaIOUtils {
42+
43+
private static final Random RANDOM = new Random();
44+
4345
// A set of config defaults.
4446
static final Map<String, Object> DEFAULT_CONSUMER_PROPERTIES =
4547
ImmutableMap.of(
@@ -99,7 +101,6 @@ static Map<String, Object> updateKafkaProperties(
99101
return config;
100102
}
101103

102-
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE") // TODO(#35312)
103104
static Map<String, Object> getOffsetConsumerConfig(
104105
String name, @Nullable Map<String, Object> offsetConfig, Map<String, Object> consumerConfig) {
105106
Map<String, Object> offsetConsumerConfig = new HashMap<>(consumerConfig);
@@ -110,7 +111,7 @@ static Map<String, Object> getOffsetConsumerConfig(
110111
String offsetGroupId =
111112
String.format(
112113
"%s_offset_consumer_%d_%s",
113-
name, new Random().nextInt(Integer.MAX_VALUE), (groupId == null ? "none" : groupId));
114+
name, RANDOM.nextInt(Integer.MAX_VALUE), (groupId == null ? "none" : groupId));
114115
offsetConsumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, offsetGroupId);
115116

116117
if (offsetConfig != null) {

sdks/java/io/synthetic/src/main/java/org/apache/beam/sdk/io/synthetic/SyntheticStep.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
2222

2323
import com.fasterxml.jackson.annotation.JsonProperty;
24-
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2524
import java.util.Random;
2625
import java.util.concurrent.TimeUnit;
2726
import org.apache.beam.sdk.io.synthetic.delay.SyntheticDelay;
@@ -53,6 +52,7 @@
5352
*/
5453
public class SyntheticStep extends DoFn<KV<byte[], byte[]>, KV<byte[], byte[]>> {
5554

55+
private static final Random RANDOM = new Random();
5656
private final Options options;
5757

5858
// used when maxWorkerThroughput is set
@@ -75,13 +75,11 @@ public RateLimiter load(KV<Long, Long> pair) {
7575
}
7676
});
7777

78-
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE") // TODO(#35312)
7978
public SyntheticStep(Options options) {
8079
options.validate();
8180
this.options = options;
82-
Random rand = new Random();
8381
// use a random id so that a pipeline could have multiple SyntheticSteps
84-
this.idAndThroughput = KV.of(rand.nextLong(), options.maxWorkerThroughput);
82+
this.idAndThroughput = KV.of(RANDOM.nextLong(), options.maxWorkerThroughput);
8583
}
8684

8785
@ProcessElement

0 commit comments

Comments
 (0)