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


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/StreamingKeyedWorkItemSideInputParDoFn.java:
##########
@@ -143,16 +146,20 @@ protected void onStartKey() {
             });
       }
     }
+    activeKey = true;
   }
 
   @Override
   @SuppressWarnings("unchecked")
   public void processElement(Object untypedElem) throws Exception {
-    if (helpers.fnRunner == null) {
+    boolean initialize = !activeKey || helpers.fnRunner == null;
+    if (initialize) {
       // If we need to run reallyStartBundle in here, we need to make sure to 
switch the state
       // sampler into the start state.
       try (Closeable start = helpers.operationContext.enterStart()) {
-        helpers.reallyStartBundle();
+        if (helpers.fnRunner == null) {
+          helpers.reallyStartBundle();
+        }
         onStartKey();
       }
     }

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   In `StreamingKeyedWorkItemSideInputParDoFn`, `onStartKey()` reads the 
current key from state using `keyValue().read()`. However, when `onStartKey()` 
is called during `processElement` initialization, the new key has not yet been 
written to `keyValue()` (which normally happens later in 
`onProcessWindowedValue`). This means `keyValue().read()` will either return 
`null` (if the key is being processed for the first time) or the key from a 
previous bundle, leading to incorrect behavior or missed unblocking of 
elements/timers for the new key.
   
   To fix this, we should extract the key from `untypedElem` and write it to 
`keyValue()` before calling `onStartKey()` in `processElement`.
   
   ```suggestion
     public void processElement(Object untypedElem) throws Exception {
       boolean initialize = !activeKey || helpers.fnRunner == null;
       if (initialize) {
         // If we need to run reallyStartBundle in here, we need to make sure 
to switch the state
         // sampler into the start state.
         try (Closeable start = helpers.operationContext.enterStart()) {
           if (helpers.fnRunner == null) {
             helpers.reallyStartBundle();
           }
           WindowedValue<KeyedWorkItem<K, InputT>> elem = 
(WindowedValue<KeyedWorkItem<K, InputT>>) untypedElem;
           keyValue().write(elem.getValue().key());
           onStartKey();
         }
       }
   ```



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/StreamingSideInputDoFnRunner.java:
##########
@@ -38,6 +38,7 @@ public class StreamingSideInputDoFnRunner<InputT, OutputT, W 
extends BoundedWind
     implements DoFnRunner<InputT, OutputT> {
   private final DoFnRunner<InputT, OutputT> simpleDoFnRunner;
   private final StreamingSideInputProcessor<InputT, W> sideInputProcessor;
+  boolean activeKey = false;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The `activeKey` field has default (package-private) visibility. It should be 
declared as `private` to maintain proper encapsulation and consistency with 
other classes like `SimpleParDoFn` and `StreamingKeyedWorkItemSideInputParDoFn`.
   
   ```suggestion
     private boolean activeKey = 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