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


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/StreamingModeExecutionContext.java:
##########
@@ -270,6 +277,17 @@ public void start(
     }
   }
 
+  public void finishKey() {
+    checkState(!finishKeyCalled, "finishKey was already called");
+    checkNotNull(workExecutor, "workExecutor must be set before calling 
finishKey()");
+    try {
+      workExecutor.finishKey();
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+    this.finishKeyCalled = true;
+  }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The current implementation of `finishKey()` throws an 
`IllegalStateException` if called more than once. However, iterators often call 
`advance()` multiple times after exhaustion, which would trigger multiple calls 
to this method. Making `finishKey()` idempotent is safer and prevents 
unexpected crashes in the worker. Additionally, providing a descriptive message 
when wrapping the exception improves debuggability.
   
   ```java
     public void finishKey() {
       if (finishKeyCalled) {
         return;
       }
       checkNotNull(workExecutor, "workExecutor must be set before calling 
finishKey()");
       try {
         workExecutor.finishKey();
       } catch (Exception e) {
         throw new RuntimeException("Failed to finish key processing", e);
       }
       this.finishKeyCalled = true;
     }
   ```



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/WindmillReaderIteratorBase.java:
##########
@@ -54,9 +56,14 @@ 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;
+        context.finishKey();
         return false;
       }

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   To avoid redundant calls to `context.finishKey()` if `advance()` is invoked 
after the iterator is already exhausted, consider adding a guard. This ensures 
the lifecycle method is only triggered once per key, even if the iterator 
contract is not strictly followed by the caller.
   
   ```java
         if (bundleIndex >= work.getMessageBundlesCount()) {
           if (bundleIndex == work.getMessageBundlesCount()) {
             current = null;
             context.finishKey();
             bundleIndex++;
           }
           return false;
         }
   ```



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