github-actions[bot] commented on code in PR #41328:
URL: https://github.com/apache/beam/pull/41328#discussion_r4673107665


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIO.java:
##########
@@ -1930,9 +1930,18 @@ public void processElement(@Element PubsubMessage 
message, @Timestamp Instant ti
         // - Size validation makes no distinction between JSON and Protobuf 
encoding
         // - Accounting for HTTP to gRPC transcoding is non-trivial
         PreparePubsubWriteDoFn.validatePubsubMessage(message, 
maxPublishBatchByteSize);
-        // NOTE: The record id is always null since it will be assigned by 
Pub/Sub.
+        // If an id attribute is configured, use the id the message already
+        // carries, or generate a fresh random id for this element.
+        @Nullable String recordId = null;
+        @Nullable String idAttribute = getIdAttribute();
+        if (idAttribute != null) {
+          recordId = message.getAttribute(idAttribute);
+          if (recordId == null || recordId.isEmpty()) {
+            recordId = UUID.randomUUID().toString();

Review Comment:
   **🔴 Critical**: Generating `UUID.randomUUID()` inside 
`PubsubBoundedWriter.processElement` is not stable across bundle retries. 
`PubsubBoundedWriter` flushes batches incrementally in `processElement` (when 
batch size/byte limits are reached) and in `finishBundle`. If a bundle fails 
after earlier batches have already been published, the runner will retry the 
bundle and `processElement` will generate a **new** random UUID for the retried 
elements, preventing downstream `PubsubIO.Read.withIdAttribute(...)` consumers 
from deduplicating them.
   
   In `PubsubUnboundedSink`, random UUIDs are generated in `ShardFn` *before* a 
`GroupByKey` boundary so that the generated `recordId` is checkpointed and 
reused across `WriterFn` retries. For bounded writes to support auto-generated 
retry-safe IDs, the UUID must be assigned upstream of a checkpoint/fusion break 
(e.g., before a `Reshuffle` or `GroupByKey`), or `withIdAttribute` in bounded 
mode should rely on deterministic IDs already present on the input elements.



##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIOTest.java:
##########
@@ -1204,4 +1204,83 @@ public void testOpenTelemetryHeaderPropagator() {
     Mockito.verify(mockSpanBuilder).setSpanKind(SpanKind.PRODUCER);
     Mockito.verify(mockSpan).end();
   }
+/** Records every published message instead of sending it. */
+private static class CapturingPubsubClient extends PubsubTestClient {
+  private static final List<OutgoingMessage> PUBLISHED_MESSAGES = new 
ArrayList<>();

Review Comment:
   **🟡 Medium**: `PUBLISHED_MESSAGES` is an unsynchronized static `ArrayList`. 
Because `TestPipeline` (`DirectRunner`) executes bundles across multiple 
threads concurrently, `PUBLISHED_MESSAGES.addAll(outgoingMessages)` in 
`publish()` is prone to race conditions and `ConcurrentModificationException`. 
Use a thread-safe collection such as `Collections.synchronizedList(new 
ArrayList<>())` or `CopyOnWriteArrayList`.
   
   ```suggestion
     private static final List<OutgoingMessage> PUBLISHED_MESSAGES = 
Collections.synchronizedList(new ArrayList<>());
   ```



##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIOTest.java:
##########
@@ -1204,4 +1204,83 @@ public void testOpenTelemetryHeaderPropagator() {
     Mockito.verify(mockSpanBuilder).setSpanKind(SpanKind.PRODUCER);
     Mockito.verify(mockSpan).end();
   }
+/** Records every published message instead of sending it. */
+private static class CapturingPubsubClient extends PubsubTestClient {

Review Comment:
   **🟢 Low**: The indentation and trailing whitespace in the newly added test 
code (lines 1207–1285) do not conform to Google Java Format and will fail the 
`spotlessCheck` Gradle task. Please run `./gradlew 
:sdks:java:io:google-cloud-platform:spotlessApply`.



##########
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIOTest.java:
##########
@@ -1204,4 +1204,83 @@ public void testOpenTelemetryHeaderPropagator() {
     Mockito.verify(mockSpanBuilder).setSpanKind(SpanKind.PRODUCER);
     Mockito.verify(mockSpan).end();
   }
+/** Records every published message instead of sending it. */
+private static class CapturingPubsubClient extends PubsubTestClient {
+  private static final List<OutgoingMessage> PUBLISHED_MESSAGES = new 
ArrayList<>();
+
+    @Override
+    public int publish(TopicPath topic, List<OutgoingMessage> 
outgoingMessages) {
+      PUBLISHED_MESSAGES.addAll(outgoingMessages);
+      return outgoingMessages.size();
+    }
+  static List<OutgoingMessage> published() {
+      return PUBLISHED_MESSAGES;
+      }
+            static void clear() {
+            PUBLISHED_MESSAGES.clear();
+          }
+    }
+
+    /** A {@link PubsubClient.PubsubClientFactory} that returns a {@link 
CapturingPubsubClient}. */
+    private static class CapturingPubsubClientFactory implements 
PubsubClient.PubsubClientFactory {
+
+          @Override
+          public PubsubClient newClient(
+                    @Nullable String timestampAttribute,
+                    @Nullable String idAttribute,

Review Comment:
   **🟡 Medium**: `CapturingPubsubClientFactory.newClient` ignores the 
`idAttribute` parameter. Even if `PubsubBoundedWriter.startBundle` passed 
`null` for `idAttribute`, both new tests would still pass because they only 
check `OutgoingMessage.recordId()`. Consider capturing or asserting on the 
`idAttribute` passed to `newClient` so the change in `startBundle` (line 1920 
of `PubsubIO.java`) is actually exercised by the test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to