reuvenlax commented on code in PR #41541:
URL: https://github.com/apache/beam/pull/41541#discussion_r4743951212


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/DataflowOperationContext.java:
##########
@@ -157,6 +193,96 @@ private Closeable enter(ExecutionState state) {
     return executionStateTracker.enterState(state);
   }
 
+  private interface ProcessScope extends Closeable {
+    Closeable enter();
+  }
+
+  private static final class DirectProcessScope implements ProcessScope {
+    private final ExecutionStateTracker tracker;
+    private final ExecutionState processElementState;
+    private boolean inUse;
+    private @Nullable ExecutionState previous;
+
+    DirectProcessScope(ExecutionStateTracker tracker, ExecutionState 
processElementState) {
+      this.tracker = tracker;
+      this.processElementState = processElementState;
+    }
+
+    @Override
+    public Closeable enter() {
+      if (!inUse) {
+        inUse = true;
+        try {
+          previous = tracker.enterStateDirect(processElementState);
+          return this;
+        } catch (Throwable t) {
+          inUse = false;
+          previous = null;
+          throw t;
+        }
+      }
+      return tracker.enterState(processElementState);
+    }
+
+    @Override
+    public void close() {
+      if (!inUse) {

Review Comment:
   Done — changed to Preconditions.checkState(inUse, "ProcessScope closed 
without a matching enterProcess() call") and updated the double-close tests to 
expect IllegalStateException.
   
   



##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/DataflowOperationContext.java:
##########
@@ -157,6 +193,96 @@ private Closeable enter(ExecutionState state) {
     return executionStateTracker.enterState(state);
   }
 
+  private interface ProcessScope extends Closeable {
+    Closeable enter();
+  }
+
+  private static final class DirectProcessScope implements ProcessScope {
+    private final ExecutionStateTracker tracker;
+    private final ExecutionState processElementState;
+    private boolean inUse;
+    private @Nullable ExecutionState previous;
+
+    DirectProcessScope(ExecutionStateTracker tracker, ExecutionState 
processElementState) {
+      this.tracker = tracker;
+      this.processElementState = processElementState;
+    }
+
+    @Override
+    public Closeable enter() {
+      if (!inUse) {
+        inUse = true;
+        try {
+          previous = tracker.enterStateDirect(processElementState);
+          return this;
+        } catch (Throwable t) {
+          inUse = false;
+          previous = null;
+          throw t;
+        }
+      }
+      return tracker.enterState(processElementState);
+    }
+
+    @Override
+    public void close() {
+      if (!inUse) {
+        return;
+      }
+      ExecutionState prev = previous;
+      previous = null;
+      inUse = false;
+      tracker.exitStateDirect(prev);
+    }
+  }
+
+  private static final class StreamingProcessScope implements ProcessScope {
+    private final DataflowExecutionStateTracker tracker;
+    private final ExecutionState processElementState;
+    private final @Nullable StepProcessingStats stepStats;
+    private boolean inUse;
+    private @Nullable ExecutionState previous;
+
+    StreamingProcessScope(
+        DataflowExecutionStateTracker tracker,
+        ExecutionState processElementState,
+        @Nullable StepProcessingStats stepStats) {
+      this.tracker = tracker;
+      this.processElementState = processElementState;
+      this.stepStats = stepStats;
+    }
+
+    @Override
+    public Closeable enter() {
+      if (!inUse) {
+        inUse = true;
+        try {
+          previous =
+              stepStats != null
+                  ? tracker.enterStreamingProcessDirect(processElementState, 
stepStats)
+                  : tracker.enterStateDirect(processElementState);
+          return this;
+        } catch (Throwable t) {
+          inUse = false;
+          previous = null;
+          throw t;
+        }
+      }
+      return tracker.enterState(processElementState);
+    }
+
+    @Override
+    public void close() {
+      if (!inUse) {

Review Comment:
   Done (covered by the merged ProcessScope.close()).
   



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