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

   ### What happened?
   
   On Dataflow Streaming Engine (legacy Java streaming worker), 
`StreamingDataflowWorker` backs both `MultimapState` and `MapState` with 
`WindmillMultimap` 
(`setSupportMapViaMultimap(options.isEnableStreamingEngine())`).
   
   In `WindmillMultimap.persistDirectly()`, the cached state object is inserted 
into `WindmillStateCache` with a hardcoded weight of **`1` byte**:
   
   ```java
   // WindmillMultimap.java (lines 148, 203)
   cache.put(namespace, stateKey, this, 1);
   ```
   
   (`WindmillMap.java` has the same hardcoded `1`-byte weight at line 153.)
   
   Because of this:
   1. **Cache weight is severely undercounted.** `WindmillMapViaMultimap.put(k, 
v)` calls `multimap.remove(k)` (which sets `keyState.valuesCached = true`) 
followed by `multimap.put(k, v)`, so `persistDirectly()` always caches the 
deserialized value in `keyStateMap`. Single-page `get()` / `entries()` reads 
also cache decoded entries in `keyStateMap` (ignoring the 
`WeightedList.getWeight()` already computed by 
`WindmillStateReader.multimapPageValues()`).
   2. **Both cache limits are bypassed.** `WindmillStateCache` holds strong 
references with no TTL and relies solely on Guava's `maximumWeight` 
(`--workerCacheMb`, default `100 MB`) and `maxCachedEntryBytesLimit` to evict 
entries (including stale entries orphaned when `cacheToken` changes). At 
~200–300 bytes of reported wrapper overhead + `1` byte per map, a `100 MB` 
cache allows hundreds of thousands of keys — potentially tens of GB of real 
heap — before evicting anything, resulting in monotonic heap growth, GC 
thrashing, and `OutOfMemoryError`.
   
   ### Minimal Reproduction
   
   Run a 1-worker Streaming Engine Java job (`n2-standard-2`, 
`--workerCacheMb=100`, `--numWorkers=1`, `--maxNumWorkers=1`, 
`--enableStreamingEngine`):
   
   ```java
   p.apply(GenerateSequence.from(0L).withRate(500L, 
Duration.standardSeconds(1)))
    .apply(MapElements.via(new SimpleFunction<Long, KV<Long, Long>>() {
      @Override
      public KV<Long, Long> apply(Long seq) {
        return KV.of(seq % 50_000L, seq);
      }
    }))
    .apply(ParDo.of(new DoFn<KV<Long, Long>, Void>() {
      @StateId("m")
      private final StateSpec<MapState<String, byte[]>> mapSpec =
          StateSpecs.map(StringUtf8Coder.of(), ByteArrayCoder.of());
   
      @ProcessElement
      public void processElement(ProcessContext c, @StateId("m") 
MapState<String, byte[]> m) {
        byte[] payload = new byte[256 * 1024];
        payload[0] = (byte) (c.element().getValue() & 0xFF);
        m.put("latest", payload);
      }
    }));
   ```
   
   - **Observed:** Container memory climbs from ~1.1 GB to ~6.85 GB within ~60s 
of processing (~5.4 GB max heap saturated) and crashes with 
`java.lang.OutOfMemoryError: Java heap space` in 
`WindmillMultimap.persistDirectly()`, restarting in a loop.
   - **Expected:** `WindmillStateCache` evicts entries once cached state 
reaches `--workerCacheMb=100`, keeping heap bounded with zero OOMs.
   
   ### Proposed Fix
   
   Incrementally track cached byte weight in `WindmillMultimap` (matching 
`WindmillBag` / `WindmillValue`):
   - Maintain per-`KeyState` and aggregate `cachedWeight` 
(`KEY_STATE_OVERHEAD_BYTES` + encoded key size + encoded cached value size).
   - Reuse `encodedKey.size()` and `encodedValue.size()` already computed in 
`persistDirectly()`, and `((Weighted) ...).getWeight()` already returned by 
`WindmillStateReader` on `get()` / `keys()` / `entries()`.
   - Decrement `cachedWeight` on `remove()`, `clear()`, and `keyStateMap` 
pruning.
   - Pass `Math.max(1L, cachedWeight + stateKey.byteString().size())` to 
`cache.put(...)`.
   
   *(Verified with the repro above: after applying this fix, the same 1-worker 
`n2-standard-2` job with `--workerCacheMb=100` ran continuously at ~500 elem/s 
with bounded memory and 0 OOMs.)*
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: runner-dataflow
   - [x] Component: sdk-java-core
   


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

  • [I] [Bug]: WindmillMultimap ha... AMOOOMA (via GitHub) via github

Reply via email to