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

   ### What happened?
   
   Beam 2.76.0, Python SDK, cross-language ReadFromKafka on the portable Flink 
runner (Flink 1.20).
   
   We read from Kafka with `redistribute=True, allow_duplicates=True` and 
didn't set `redistribute_num_keys`. Over a couple of weeks checkpoint size grew 
to ~4 GB on our busiest jobs, and eventually TaskManagers started dying with 
`java.lang.OutOfMemoryError: Java heap space` (stack traces in 
`HeapPriorityQueueSnapshotRestoreWrapper.stateSnapshot` and 
`TimerDataCoderV2.decode`). After that the job never recovers, since every 
restart loads the same checkpoint and OOMs again.
   
   As far as I can tell this is what happens:
   
   - Python defaults to `redistribute_num_keys=np.int32(0)` and always sends 
it: 
https://github.com/apache/beam/blob/v2.76.0/sdks/python/apache_beam/io/kafka.py#L173
   - #36124 added `DEFAULT_REDISTRIBUTE_NUM_KEYS = 32768` on the Java side, but 
it's only applied in `Read.withRedistribute()`. The external builder just 
copies the value from the config (`setRedistributeNumKeys((int) 
config.redistributeNumKeys)`), so the 0 from Python wins and the Java default 
never kicks in.
   - With 0 we get `Insert Redistribute` without shards, and `AssignShardFn` 
gives every record its own key. KafkaIO also logs "withRedistribute without 
withRedistributeNumKeys will create a key per record, which is sub-optimal for 
most use cases."
   - The portable Flink translator handles `Reshuffle` natively but not 
`Redistribute`, so it falls back to the GroupByKey in `RedistributeByKey`. That 
runs in the global window with allowed lateness `TIMESTAMP_MAX_VALUE`, so each 
key leaves behind a cleanup timer that never fires. One key per record means 
the timer state just keeps growing.
   
   To reproduce, run something like this as a streaming job on Flink and watch 
the checkpoint size:
   
   ```python
   p | ReadFromKafka(
       consumer_config={"bootstrap.servers": "..."},
       topics=["t"],
       redistribute=True,
       allow_duplicates=True,
   )
   ```
   
   Setting `redistribute_num_keys=32768` explicitly from Python fixes it for us.
   
   I think the simplest fix is to make the Python default `None` (the schema 
field is already `Optional[np.int32]`) so the Java default applies, and to 
mention in the docstring what 0 means. It might also make sense to apply the 
default in the external builder when the value isn't set, or to translate 
`Redistribute` natively on Flink like `Reshuffle`.
   
   Related: #31347, #36124
   
   ### 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
   - [x] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Infrastructure
   - [ ] Component: Spark Runner
   - [x] 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