jk-shah opened a new issue, #41352:
URL: https://github.com/apache/beam/issues/41352
### What happened?
On `BundleBasedDirectRunner` (for example in `TestStream` pipelines with
multiple watermark/element bundles), user `Metrics.counter` increments that
occur only in Bundle 2 or later are lost from `result.metrics()` whenever the
same step was already entered in Bundle 1:
```python
import apache_beam as beam
from apache_beam.metrics import Metrics
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.testing.test_stream import TestStream
from apache_beam.transforms.window import TimestampedValue
class CountPerBundleFn(beam.DoFn):
def __init__(self):
self.seen = Metrics.counter('ns', 'seen')
self.second_bundle = Metrics.counter('ns', 'second_bundle')
def process(self, x):
self.seen.inc()
if x == 'b2':
self.second_bundle.inc()
p =
beam.Pipeline(options=PipelineOptions(['--runner=BundleBasedDirectRunner']))
_ = (
p
| TestStream()
.advance_watermark_to(0)
.add_elements([TimestampedValue('b1', 1)])
.advance_watermark_to(10)
.add_elements([TimestampedValue('b2', 11)])
.advance_watermark_to_infinity()
| beam.ParDo(CountPerBundleFn())
)
res = p.run()
res.wait_until_finish()
counters = {
m.key.metric.name: m.committed
for m in res.metrics().query()['counters']
}
print(counters)
# Expected: {'seen': 2, 'second_bundle': 1}
# Actual: {'seen': 1} ('second_bundle' is missing and 'seen' misses Bundle
2!)
```
### Cause
In `sdks/python/apache_beam/runners/direct/executor.py` (line `314`),
`TransformExecutor.call` allocates a fresh `MetricsContainer` for each bundle
while reusing the worker thread's `StateSampler`. However, in
`sdks/python/apache_beam/runners/worker/statesampler.py` (lines `158-165`):
```python
if counter_name in self._states_by_name:
return self._states_by_name[counter_name]
```
`scoped_state` returns the cached `ScopedState` created during Bundle 1
without updating its `metrics_container` reference to the current bundle's
`MetricsContainer`. Updating `state.metrics_container = metrics_container` on
cache hit fixes multi-bundle metric reporting.
### Issue Priority
Priority: 2 (default / most bugs should be filed as P2)
### Issue Components
- [x] Component: Python SDK
- [ ] Component: Java SDK
- [ ] Component: Go SDK
- [ ] Component: Typescript SDK
- [ ] Component: IO connector
- [ ] Component: Beam YAML
- [ ] Component: Beam ML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism Runner
- [ ] Component: Hazelcast Jet Runner
- [ ] Component: Google Cloud Dataflow Runner
--
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]