prodriguezdefino commented on PR #41072:
URL: https://github.com/apache/beam/pull/41072#issuecomment-7171066281

   Good call on checking the SDK side first. I asked Antigravity to research 
why Python behaves differently here and how the other SDKs handle short IDs:
   
   - Java (`ShortIdMap`), Python (`SHORT_ID_CACHE`), and Go (`shortIDCache`) 
all use one worker-wide map from `(urn, type, labels) -> short_id` and send 
each bundle's delta in `ProcessBundleResponse.monitoring_data`.
   - In Python `LOOPBACK`, bundle teardown (`op.finish()`, 
`monitoring_infos()`, `processor.reset()`) runs under the GIL while 
`_request_monitoring_infos` runs directly on the control reader thread, so 
bundle 1's `MonitoringMetadata` round-trip finishes before bundle 2 queues its 
`ProcessBundleResponse`.
   - Java reproduces the same failure on Prism (`expected: 4, but was: 1` on 
`master`, and `4` on this PR).
   
   To run it, drop the test below into 
`runners/prism/java/src/test/java/org/apache/beam/runners/prism/PrismShortIdReproTest.java`
 and run `./gradlew :runners:prism:java:test --tests 
org.apache.beam.runners.prism.PrismShortIdReproTest`:
   
   <details>
   <summary>PrismShortIdReproTest.java</summary>
   
   ```java
   package org.apache.beam.runners.prism;
   
   import static com.google.common.truth.Truth.assertThat;
   
   import java.util.ArrayList;
   import java.util.List;
   import java.util.concurrent.CyclicBarrier;
   import java.util.concurrent.TimeUnit;
   import org.apache.beam.sdk.Pipeline;
   import org.apache.beam.sdk.PipelineResult;
   import org.apache.beam.sdk.metrics.Counter;
   import org.apache.beam.sdk.metrics.MetricNameFilter;
   import org.apache.beam.sdk.metrics.MetricQueryResults;
   import org.apache.beam.sdk.metrics.MetricResult;
   import org.apache.beam.sdk.metrics.Metrics;
   import org.apache.beam.sdk.metrics.MetricsFilter;
   import org.apache.beam.sdk.options.PipelineOptionsFactory;
   import org.apache.beam.sdk.transforms.DoFn;
   import org.apache.beam.sdk.transforms.Flatten;
   import org.apache.beam.sdk.transforms.Impulse;
   import org.apache.beam.sdk.transforms.ParDo;
   import org.apache.beam.sdk.values.PCollection;
   import org.apache.beam.sdk.values.PCollectionList;
   import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterables;
   import org.junit.Test;
   import org.junit.runner.RunWith;
   import org.junit.runners.JUnit4;
   
   @RunWith(JUnit4.class)
   public class PrismShortIdReproTest {
     private static final int BUNDLES = 4;
     private static final CyclicBarrier BARRIER = new CyclicBarrier(BUNDLES);
   
     static class StaggeredEmitFn extends DoFn<byte[], Integer> {
       private final int val;
       private final long delayMs;
   
       StaggeredEmitFn(int val, long delayMs) {
         this.val = val;
         this.delayMs = delayMs;
       }
   
       @ProcessElement
       public void processElement(OutputReceiver<Integer> out) throws 
InterruptedException {
         Thread.sleep(delayMs);
         out.output(val);
       }
     }
   
     static class ConcurrentCounterFn extends DoFn<Integer, Integer> {
       private final Counter counter = Metrics.counter("ns", "bundles");
   
       @ProcessElement
       public void processElement(@Element Integer element, 
OutputReceiver<Integer> out)
           throws Exception {
         BARRIER.await(5, TimeUnit.SECONDS);
         counter.inc(1L);
         out.output(element);
       }
     }
   
     @Test
     public void concurrentBundleShortIdMetrics() {
       BARRIER.reset();
       PrismPipelineOptions opts = 
PipelineOptionsFactory.create().as(PrismPipelineOptions.class);
       opts.setRunner(PrismRunner.class);
       opts.setPrismLocation(System.getProperty("prism.buildTarget"));
       opts.setEnableWebUI(false);
   
       Pipeline p = Pipeline.create(opts);
       List<PCollection<Integer>> branches = new ArrayList<>();
       for (int i = 0; i < BUNDLES; i++) {
         branches.add(
             p.apply("Impulse" + i, Impulse.create())
                 .apply("Emit" + i, ParDo.of(new StaggeredEmitFn(i, i * 15L))));
       }
       PCollectionList.of(branches)
           .apply(Flatten.pCollections())
           .apply(ParDo.of(new ConcurrentCounterFn()));
   
       PipelineResult res = p.run();
       res.waitUntilFinish();
   
       MetricQueryResults metrics =
           res.metrics()
               .queryMetrics(
                   MetricsFilter.builder()
                       .addNameFilter(MetricNameFilter.named("ns", "bundles"))
                       .build());
       MetricResult<Long> counterResult = 
Iterables.getOnlyElement(metrics.getCounters());
       assertThat(counterResult.getCommitted()).isEqualTo((long) BUNDLES);
     }
   }
   ```
   </details>
   


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