prodriguezdefino opened a new pull request, #40640:
URL: https://github.com/apache/beam/pull/40640

   Problem:
   In finalizeStage (sdks/go/pkg/beam/runners/prism/internal/preprocess.go), 
Prism sets `stage.finalize` only while it inspects the consumers of the stage's 
input PCollections. The loop skips every PCollection that a transform of the 
same stage produces:
   ```go
       for pid, plinks := range stageFacts.PcolConsumers {
           if _, ok := stageFacts.PcolProducers[pid]; ok {
               continue
           }
           ...
           if pardo.GetRequestsFinalization() {
               stg.finalize = true
           }
   ```
   So a `ParDo` that sets `requests_finalization` but is fused after another 
transform (a `Map` after `Create`, the expand step of a `Reshuffle`, any 
element-wise chain) never marks its stage. The SDK harness registers the 
callbacks and returns `requires_finalization`, but Prism never sends 
`FinalizeBundleRequest`, and the callbacks never run. This affects every SDK. 
The existing `TestParDoBundleFinalizer` cases apply the finalizing `DoFn` 
directly to Impulse, so the `DoFn` always consumes the stage input and the 
tests pass.
   
   Fix:
   Set `stage.finalize` from every ParDo-like transform of the stage (`ParDo` 
and the splittable `DoFn` stage URNs), not only from the consumers of the stage 
inputs. Statefulness, the on-window-expiration timer and side inputs still come 
from the input consumers. Add a `TestParDoBundleFinalizer` case with the 
finalizing `DoFn` fused after another `ParDo`.
   
   Reproduction (Python SDK on PrismRunner):
   ```python
   import os
   import tempfile
   
   import apache_beam as beam
   from apache_beam.options.pipeline_options import PipelineOptions
   
   class MarkFinalization(beam.DoFn):
       """Registers a bundle finalization callback that creates the file 
`path`."""
       def __init__(self, path):
           self._path = path
   
       def process(self, element, 
bundle_finalizer=beam.DoFn.BundleFinalizerParam):
           path = self._path
           bundle_finalizer.register(lambda: open(path, "w").close())
           yield element
   
   if __name__ == "__main__":
       markers = tempfile.mkdtemp()
       at_input = os.path.join(markers, "at_stage_input")
       fused = os.path.join(markers, "after_fused_map")
       options = PipelineOptions(["--runner=PrismRunner", 
"--environment_type=LOOPBACK"])
       with beam.Pipeline(options=options) as p:
           impulse = p | beam.Impulse()
           # Consumes the input of its stage: Prism finalizes the bundle.
           _ = impulse | "AtStageInput" >> 
beam.ParDo(MarkFinalization(at_input))
           # Fused after a Map in the same stage: Prism never finalizes the 
bundle.
           _ = (
               impulse
               | "Map" >> beam.Map(lambda x: x)
               | "AfterFusedMap" >> beam.ParDo(MarkFinalization(fused))
           )
       print("finalized:", sorted(os.listdir(markers)))
       assert os.path.exists(at_input), "the DoFn at the stage input was not 
finalized"
       assert os.path.exists(fused), "the DoFn fused after Map was not 
finalized"
   ```
   
   Before the fix: `finalized: ['at_stage_input']`, and the second assertion 
fails.
   After the fix: `finalized: ['after_fused_map', 'at_stage_input']`.
   
   


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