stankiewicz opened a new issue, #41295:
URL: https://github.com/apache/beam/issues/41295

   ### What happened?
   
   In the Dataflow Java Runner v1 worker 
(`org.apache.beam.runners.dataflow.worker.windmill.state.IdTracker`), 
`OrderedListState` tracks used sub-column IDs  in an internal 
`ValueState<Map<Range<Instant>, RangeSet<Long>>>` named `<tag>IdsAvailable` 
(`idsAvailableValue`).
   
   When `OrderedListState.clearRange(minTs, limitTs)` clears all remaining 
ranges in `IdTracker.remove()`, `idsAvailable.isEmpty()` becomes `true`. 
However, `IdTracker.writeValues()` has a typo on line 242: it calls 
`idsAvailable.clear()` on the already-empty local `Map` variable instead of 
`idsAvailableValue.clear()` on the backing `ValueState` (unlike 
`subRangeDeletionsValue.clear()` five lines below). Because `idsAvailableValue` 
is never marked `modified = true`, `WindmillValue.persistDirectly()` sends no 
delete mutation to Windmill, leaking the `<tag>IdsAvailable` `ValueState` in 
persistent storage forever whenever keys drain `OrderedListState` via 
`clearRange()`.
   
   ---
   
   ### Root Cause in Code
   
   In 
`runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/state/IdTracker.java`
 (lines 238-251):
   
   ```java
     private void writeValues(
         Map<Range<Instant>, RangeSet<Long>> idsAvailable,
         Map<Range<Instant>, RangeSet<Instant>> subRangeDeletions) {
       if (idsAvailable.isEmpty()) {
         idsAvailable.clear(); // <-- BUG: clears the already-empty local Map 
instead of idsAvailableValue.clear()
       } else {
         idsAvailableValue.write(idsAvailable);
       }
       if (subRangeDeletions.isEmpty()) {
         subRangeDeletionsValue.clear(); // <-- Properly clears the backing 
ValueState
       } else {
         subRangeDeletionsValue.write(subRangeDeletions);
       }
     }
   ```
   
   And in `WindmillValue.java` (lines 131-135):
   
   ```java
       if (!modified) {
         // The value was read, but never written or cleared.
         // But nothing to update in Windmill.
         return Windmill.WorkItemCommitRequest.newBuilder().buildPartial();
       }
   ```
   
   ---
   
   ### Impact
   
   1. **Permanent `ValueState` leak in Windmill / Bigtable for `clearRange()` 
workflows**:
      - Standard event-time buffering patterns with `OrderedListState` append 
elements via `orderedList.add(...)` and drain due elements on timer firings via 
`orderedList.clearRange(minTs, fireTs)` without calling `orderedList.clear()`.
      - When the last 5-minute range in `idsAvailable` is removed by 
`IdTracker.remove()`, `idsAvailable.isEmpty()` is `true`, so 
`idsAvailableValue.write(...)` is skipped and `idsAvailableValue.clear()` is 
never called.
      - `WindmillValue.modified` stays `false`, so no `ValueUpdate` deletion is 
committed to Windmill. The last non-empty `<tag>IdsAvailable` map remains 
stored in Windmill persistent state indefinitely for every idle key.
   2. **Stale ID ranges reloaded after cache eviction or key rebalance**:
      - Because the persisted `<tag>IdsAvailable` cell in Windmill is never 
deleted when `idsAvailable` becomes empty, as soon as the key is evicted from 
`WindmillStateCache` (or reassigned to another worker), the next 
`idTracker.readIdsAvailable()` reads the stale non-empty `Map<Range<Instant>, 
RangeSet<Long>>` back from Windmill instead of starting clean.
   
   ---
   
   ### Proposed Fix
   
   Replace `idsAvailable.clear()` with `idsAvailableValue.clear()` in 
`IdTracker.writeValues()`:
   
   ```diff
   --- 
a/runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/state/IdTracker.java
   +++ 
b/runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/state/IdTracker.java
   @@ -239,7 +239,7 @@ final class IdTracker {
          Map<Range<Instant>, RangeSet<Long>> idsAvailable,
          Map<Range<Instant>, RangeSet<Instant>> subRangeDeletions) {
        if (idsAvailable.isEmpty()) {
   -      idsAvailable.clear();
   +      idsAvailableValue.clear();
        } else {
          idsAvailableValue.write(idsAvailable);
        }
   ```
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [ ] Component: Python SDK
   - [ ] 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]

Reply via email to