stankiewicz opened a new issue, #41036: URL: https://github.com/apache/beam/issues/41036
### What happened? ### What happened? In `org.apache.beam.runners.dataflow.worker.util.common.worker.OutputObjectAndByteCounter`, `update(Object elem)` decides whether to observe an element's byte size and sets the inverse-sampling-probability scaling factor on `byteCountObserver`: https://github.com/apache/beam/blob/master/runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/util/common/worker/OutputObjectAndByteCounter.java#L120-L128 ```java // Increment byte counter. if ((byteCountObserver != null || meanByteCountObserver != null) && (sampleElement() || elementByteSizeObservable.isRegisterByteSizeObserverCheap(elem))) { if (byteCountObserver != null) { byteCountObserver.setScalingFactor( Math.max(samplingToken, SAMPLING_CUTOFF) / (double) SAMPLING_CUTOFF); elementByteSizeObservable.registerByteSizeObserver(elem, byteCountObserver); } ``` where `sampleElement()` increments `samplingToken` up to `SAMPLING_TOKEN_UPPER_BOUND` (`1,000,000`) and samples with probability `SAMPLING_CUTOFF / samplingToken` (`10 / samplingToken`): ```java protected boolean sampleElement() { samplingToken = Math.min(samplingToken + 1, samplingTokenUpperBound); return getRandom().nextInt(samplingToken) < SAMPLING_CUTOFF; } ``` When `elementByteSizeObservable.isRegisterByteSizeObserverCheap(elem)` returns `true`: 1. `sampleElement()` is on the left-hand side of `||`, so it still executes on every element, incrementing `samplingToken` up to `1,000,000` (and performing an unnecessary `Random.nextInt()` call per element). 2. Even when `sampleElement()` returns `false`, the right-hand side `isRegisterByteSizeObserverCheap(elem)` returns `true`, so **100% of elements enter the `if` block**. 3. Inside the `if` block, `byteCountObserver.setScalingFactor(Math.max(samplingToken, SAMPLING_CUTOFF) / (double) SAMPLING_CUTOFF)` scales every element's observed byte size by `samplingToken / 10.0` (up to `100,000x`), which is the inverse-probability weight intended only when subsampling at rate `10 / samplingToken`. ### Impact For any `ElementByteSizeObservable` / `Coder` where `isRegisterByteSizeObserverCheap(elem)` returns `true`: - **100% of elements are observed**, and - **Each element's byte size is multiplied by `max(N, 10) / 10` (capped at `100,000x` after 1,000,000 elements)**. Examples of affected counters in Dataflow Runner v1: - Streaming `ReadStream.out0` on every `GroupByKey` / stateful `ParDo` step: `WindmillKeyedWorkItem.FakeKeyedWorkItemCoder.isRegisterByteSizeObserverCheap()` returns `true` (`WindmillKeyedWorkItem.java:286-289`). In a production streaming job, a step processing ~3.4 TB of actual data reported **131.45 TB** on `ReadStream.out0-ByteCount`. - Any `PCollection` whose coder implements `isRegisterByteSizeObserverCheap() == true` (e.g. `ByteArrayCoder`, `StringUtf8Coder`, or `WindowedValueCoder` wrapping them). (`meanByteCountObserver` is unaffected because `OutputObjectAndByteCounter` does not call `setScalingFactor` on `meanByteCountObserver`.) ### Proposed Fix Check `elementByteSizeObservable.isRegisterByteSizeObserverCheap(elem)` before calling `sampleElement()`, and set the scaling factor to `1.0` when `isCheap` is `true`: ```java // Increment byte counter. if (byteCountObserver != null || meanByteCountObserver != null) { boolean isCheap = elementByteSizeObservable.isRegisterByteSizeObserverCheap(elem); if (isCheap || sampleElement()) { if (byteCountObserver != null) { double scalingFactor = isCheap ? 1.0 : Math.max(samplingToken, SAMPLING_CUTOFF) / (double) SAMPLING_CUTOFF; byteCountObserver.setScalingFactor(scalingFactor); elementByteSizeObservable.registerByteSizeObserver(elem, byteCountObserver); } if (meanByteCountObserver != null) { elementByteSizeObservable.registerByteSizeObserver(elem, meanByteCountObserver); } if (byteCountObserver != null && !byteCountObserver.getIsLazy()) { byteCountObserver.advance(); } if (meanByteCountObserver != null && !meanByteCountObserver.getIsLazy()) { meanByteCountObserver.advance(); } } } ``` ### Issue Priority Priority: 2 (default / most bugs should be filed as P2) ### Issue Components - [ ] Component: Python SDK - [x] Component: Java SDK - [ ] Component: Go SDK - [ ] Component: Typescript SDK - [ ] Component: IO connector - [ ] Component: Beam YAML - [ ] 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]
