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


##########
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/control/ExecutionStateSamplerTest.java:
##########
@@ -898,4 +905,241 @@ public void testDefaultElementProcessingTimeoutMinutes() {
     assertThat(sampler.getUserSpecifiedLullTimeMsForRestart(), equalTo(0L));
     assertThat(sampler.getUserSpecifiedTimeoutForRestart(), equalTo(false));
   }
+
+  @Test
+  public void testRepeatedResetAndReuse() {
+    AtomicLong currentTimeMillis = new AtomicLong(100L);
+    ExecutionStateSampler sampler =
+        new ExecutionStateSampler(
+            PipelineOptionsFactory.create(),
+            () -> currentTimeMillis.getAndAdd(10L),
+            mockOnTimeoutExceededCallback);
+    try {
+      ExecutionStateTracker tracker = sampler.create();
+      ExecutionState state =
+          tracker.create("shortId", "ptransformId", "ptransformIdName", 
"process");
+
+      // Repeated reset before start should be a safe no-op.
+      tracker.reset();
+      tracker.reset();
+      assertNull(tracker.getStatus());
+      assertNull(BeamFnLoggingMDC.getStateTracker());
+
+      // First bundle lifecycle + redundant reset calls (as done by 
ProcessBundleHandler).
+      tracker.start("bundle1");
+      assertSame(tracker, BeamFnLoggingMDC.getStateTracker());
+      state.activate();
+      assertEquals("ptransformId", tracker.getCurrentThreadsPTransformId());
+      Counter counter = 
tracker.getMetricsContainer().getCounter(MetricName.named("ns", "c"));
+      counter.inc(7L);
+      state.deactivate();
+      Map<String, ByteString> finalData = new HashMap<>();
+      tracker.updateFinalMonitoringData(finalData);
+      assertTrue(finalData.containsKey("shortId"));
+      assertEquals(
+          7L,
+          tracker
+              .getMetricsContainerRegistry()
+              .getContainer("ptransformId")
+              .getCounter(MetricName.named("ns", "c"))
+              .getCumulative()
+              .longValue());
+
+      tracker.reset();
+      tracker.reset();
+      tracker.reset();
+      assertNull(tracker.getStatus());
+      assertNull(tracker.getCurrentThreadsPTransformId());
+      assertNull(BeamFnLoggingMDC.getStateTracker());
+      assertEquals(
+          0L,
+          tracker
+              .getMetricsContainerRegistry()
+              .getContainer("ptransformId")
+              .getCounter(MetricName.named("ns", "c"))
+              .getCumulative()
+              .longValue());
+
+      // Second bundle lifecycle on the same tracker should work identically.
+      tracker.start("bundle2");
+      state.activate();
+      assertEquals("ptransformId", tracker.getCurrentThreadsPTransformId());
+      state.deactivate();
+      tracker.reset();
+      assertNull(tracker.getStatus());
+      assertNull(BeamFnLoggingMDC.getStateTracker());
+    } finally {
+      sampler.stop();
+    }
+  }
+
+  @Test
+  public void testResetWithRetainedMetricHandlesAndRegistry() {
+    AtomicLong clockMillis = new AtomicLong(100L);
+    ExecutionStateSampler sampler =
+        new ExecutionStateSampler(
+            PipelineOptionsFactory.create(), clockMillis::get, 
mockOnTimeoutExceededCallback);
+    try {
+      ExecutionStateTracker tracker = sampler.create();
+      ExecutionState state =
+          tracker.create("shortId", "ptransformId", "ptransformIdName", 
"process");
+      MetricsContainerStepMap registry = tracker.getMetricsContainerRegistry();
+
+      state.activate();
+      Counter retainedCounter =
+          tracker.getMetricsContainer().getCounter(MetricName.named("ns", 
"retained"));
+      Distribution retainedDist =
+          tracker.getMetricsContainer().getDistribution(MetricName.named("ns", 
"dist"));
+      Gauge retainedGauge = 
tracker.getMetricsContainer().getGauge(MetricName.named("ns", "gauge"));
+      StringSet retainedSet =
+          tracker.getMetricsContainer().getStringSet(MetricName.named("ns", 
"set"));
+      BoundedTrie retainedTrie =
+          tracker.getMetricsContainer().getBoundedTrie(MetricName.named("ns", 
"trie"));
+      HistogramData.BucketType bucketType = HistogramData.LinearBuckets.of(0, 
1, 5);
+      Histogram retainedHist =
+          tracker.getMetricsContainer().getHistogram(MetricName.named("ns", 
"hist"), bucketType);
+      state.deactivate();
+
+      retainedCounter.inc(10L);
+      tracker.reset();
+      assertEquals(
+          0L,
+          registry
+              .getContainer("ptransformId")
+              .getCounter(MetricName.named("ns", "retained"))
+              .getCumulative()
+              .longValue());
+
+      // Mutate retained handles directly AFTER reset() without calling 
getCounter()/getRegistry().
+      retainedCounter.inc(42L);
+      retainedDist.update(5L);
+      retainedGauge.set(9L);
+      retainedSet.add("x");
+      retainedTrie.add("a");
+      retainedHist.update(1.0);
+      registry.getUnboundContainer().getCounter(MetricName.named("ns", 
"unbound")).inc(99L);
+
+      tracker.reset();

Review Comment:
   They are not distinct: MetricsContainerImpl.reset resets cells in place.
   Added assertions in testResetWithRetainedMetricHandlesAndRegistryverifying 
all 7 mutated values are reflected in registry immediately before calling 
tracker.reset().



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