gemini-code-assist[bot] commented on code in PR #38430:
URL: https://github.com/apache/beam/pull/38430#discussion_r3215954638


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/WindowingWindmillReader.java:
##########
@@ -182,6 +182,11 @@ public boolean start() throws IOException {
         @Override
         public boolean advance() throws IOException {

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The anonymous iterator in `WindowingWindmillReader` should also check 
`context.workIsFailed()` and throw `WorkItemCancelledException`, consistent 
with the changes in `WindmillReaderIteratorBase`. This ensures that cancelled 
work items are handled promptly.
   
   ```java
           @Override
           public boolean advance() throws IOException {
             if (context.workIsFailed()) {
               throw new 
WorkItemCancelledException(context.getWorkItem().getShardingKey());
             }
   ```



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/StreamingModeExecutionContext.java:
##########
@@ -240,9 +243,11 @@ public void start(
       Work work,
       WindmillStateReader stateReader,
       SideInputStateFetcher sideInputStateFetcher,
-      Windmill.WorkItemCommitRequest.Builder outputBuilder) {
+      Windmill.WorkItemCommitRequest.Builder outputBuilder,
+      WorkExecutor workExecutor) {
     this.key = key;
     this.work = work;
+    this.workExecutor = workExecutor;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Reset the `finishKeyCalled` flag when starting a new work item.
   
   ```suggestion
       this.workExecutor = workExecutor;
       this.finishKeyCalled = false;
   ```



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/StreamingModeExecutionContext.java:
##########
@@ -157,6 +158,8 @@ public class StreamingModeExecutionContext extends 
DataflowExecutionContext<Step
    */
   private @Nullable UnboundedReader<?> activeReader;
 
+  private @Nullable WorkExecutor workExecutor;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Add a boolean flag to track whether `finishKey()` has been called for the 
current work item. This helps ensure idempotency, especially if multiple 
readers are present or if `advance()` is called multiple times after exhaustion.
   
   ```java
     private @Nullable WorkExecutor workExecutor;
   
     private boolean finishKeyCalled;
   ```



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/StreamingModeExecutionContext.java:
##########
@@ -270,6 +275,11 @@ public void start(
     }
   }
 
+  public void finishKey() throws Exception {
+    checkNotNull(workExecutor, "workExecutor must be set before calling 
finishKey()");
+    workExecutor.finishKey();
+  }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Use the `finishKeyCalled` flag to ensure that `workExecutor.finishKey()` is 
only executed once per work item.
   
   ```java
     public void finishKey() throws Exception {
       if (finishKeyCalled) {
         return;
       }
       checkNotNull(workExecutor, "workExecutor must be set before calling 
finishKey()");
       finishKeyCalled = true;
       workExecutor.finishKey();
     }
   ```



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/WindmillReaderIteratorBase.java:
##########
@@ -54,9 +56,18 @@ public boolean start() throws IOException {
 
   @Override
   public boolean advance() throws IOException {
+    if (context.workIsFailed()) {
+      throw new 
WorkItemCancelledException(context.getWorkItem().getShardingKey());
+    }
+
     while (true) {
       if (bundleIndex >= work.getMessageBundlesCount()) {
         current = null;
+        try {
+          context.finishKey();
+        } catch (Exception e) {
+          throw new RuntimeException(e);
+        }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Instead of wrapping all exceptions in `RuntimeException`, consider catching 
`IOException` and `RuntimeException` separately to rethrow them directly, and 
wrapping only other checked exceptions in `IOException`. This maintains 
consistency with the `advance()` method's signature and allows callers to 
handle specific exception types.
   
   ```java
           } catch (IOException | RuntimeException e) {
             throw e;
           } catch (Exception e) {
             throw new IOException(e);
           }
   ```



-- 
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