jbonofre opened a new pull request, #41704:
URL: https://github.com/apache/beam/pull/41704
On runners that execute unbounded sources as splittable DoFns
(`UnboundedSourceAsSDFWrapperFn`), such as Dataflow Runner v2, the reader cache
can miss on every bundle and failed bundles can leave readers open.
## Cause
1. **Unstable reader cache key across serialization round trips:**
`UnboundedSourceAsSDFRestrictionTracker` keys `cachedReaders` by the
structural value (encoded bytes) of `(source, checkpoint)`. When the runner
checkpoints a residual restriction and passes it to the next bundle, the
restriction is serialized and deserialized with
`UnboundedSourceRestrictionCoder`, which uses `SerializableCoder` for the
`UnboundedSource`.
For sources whose Java serialization output is not byte-identical after a
deserialization round trip (for example `JmsIO` with
`ActiveMQConnectionFactory`, whose internal `Properties` / `Hashtable`
iteration order changes after deserialization), the deserialized
`UnboundedSource` encodes to different bytes on the next bundle. As a result,
`cachedReaders.asMap().remove(cacheKey)` misses on every bundle and a new
reader (and JMS connection) is created for every bundle.
2. **Duplicate `getCheckpointMark()` call when `processElement` returns
`resume()`:**
When a reader temporarily runs out of elements (`start()` or `advance()`
returns `false`), `processElement` calls `tracker.currentRestriction()` to
register the checkpoint with `bundleFinalizer` and returns
`ProcessContinuation.resume()`. The runner (`FnApiDoFnRunner` /
`OutputAndTimeBoundedSplittableProcessElementInvoker`) immediately calls
`tracker.trySplit(0)`, which called `currentRestriction()` and
`currentReader.getCheckpointMark()` a second time without any intervening
`tryClaim`.
3. **Unclosed readers on bundle failure or idle cache segments:**
- If `processElement` fails after `initializeCurrentReader()` (for
example when a downstream fused step throws from
`receiver.outputWithTimestamp(...)`, or `tryClaim` throws a
`RuntimeException`), `trySplit` is never called and the active reader is
neither cached nor closed.
- `cachedReaders` (`CacheBuilder.newBuilder().expireAfterWrite(1,
TimeUnit.MINUTES)`) only evicts expired entries on segment writes unless
`cleanUp()` is run, so expired readers in idle segments or idle caches could
remain open.
## Changes
- **`UnboundedSourceRestrictionCoder`:** preserve the encoded `byte[]` of
each `UnboundedSource` instance in a weak-key cache (`ENCODED_SOURCES`) on
encode and decode (via `RecordingInputStream`), since `Source` instances are
effectively immutable. Re-encoding a decoded `UnboundedSource` writes the exact
bytes it was decoded from, keeping the reader cache key invariant across runner
round trips.
- **`UnboundedSourceAsSDFRestrictionTracker`:**
- Cache the restriction computed by `currentRestriction()` until the next
`tryClaim` or `trySplit`, so `trySplit(0)` after `processElement` returns
`resume()` reuses the checkpoint registered with `bundleFinalizer` instead of
calling `getCheckpointMark()` a second time.
- Close `currentReader` on any `Throwable` in `processElement`,
`tryClaim`, `currentRestriction`, or `getProgress`.
- Call `cachedReaders.cleanUp()` when initializing a reader and schedule
periodic cache cleanup while `cachedReaders` is non-empty so expired readers
are closed even when no further writes hit their cache segment.
## Tests
-
`ReadTest#testUnboundedSdfWrapperReusesReaderWhenSourceSerializationChanges`:
verifies that `UnboundedSourceAsSDFWrapperFn` reuses the cached reader across
multiple serialized/deserialized bundle transitions even when raw Java
serialization of the source produces different bytes after every round trip.
- `ReadTest#testUnboundedSdfProcessElementTakesSingleCheckpoint`: verifies
that `processElement` followed by `tracker.trySplit(0)` takes a single
checkpoint.
- `ReadTest#testUnboundedSdfClosesReaderWhenProcessElementFails`: verifies
that the active reader is closed when downstream processing throws during
`processElement` (with `RestrictionTrackers.observe`).
--
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]