github-actions[bot] commented on code in PR #41704:
URL: https://github.com/apache/beam/pull/41704#discussion_r4709195727
##########
sdks/java/core/src/main/java/org/apache/beam/sdk/io/Read.java:
##########
@@ -877,23 +994,51 @@ private static class
UnboundedSourceAsSDFRestrictionTracker<
extends RestrictionTracker<
UnboundedSourceRestriction<OutputT, CheckpointT>,
UnboundedSourceValue<OutputT>[]>
implements HasProgress {
+ private static final Set<Cache<Object, ?>>
CACHES_WITH_SCHEDULED_CLEAN_UP =
+ ConcurrentHashMap.newKeySet();
+
private final UnboundedSourceRestriction<OutputT, CheckpointT>
initialRestriction;
private final PipelineOptions pipelineOptions;
private final Cache<Object, CacheState<OutputT>> cachedReaders;
private final Coder<UnboundedSourceRestriction<OutputT, CheckpointT>>
restrictionCoder;
+ private final Consumer<UnboundedSourceAsSDFRestrictionTracker<OutputT,
CheckpointT>>
+ onClaimCallback;
private UnboundedSource.@Nullable UnboundedReader<OutputT> currentReader;
private boolean readerHasBeenStarted;
+ private @Nullable UnboundedSourceRestriction<OutputT, CheckpointT>
lastRestriction;
UnboundedSourceAsSDFRestrictionTracker(
UnboundedSourceRestriction<OutputT, CheckpointT> initialRestriction,
PipelineOptions pipelineOptions,
Cache<Object, CacheState<OutputT>> cachedReaders,
- Coder<UnboundedSourceRestriction<OutputT, CheckpointT>>
restrictionCoder) {
+ Coder<UnboundedSourceRestriction<OutputT, CheckpointT>>
restrictionCoder,
+ Consumer<UnboundedSourceAsSDFRestrictionTracker<OutputT,
CheckpointT>> onClaimCallback) {
this.initialRestriction = initialRestriction;
this.pipelineOptions = pipelineOptions;
this.cachedReaders = cachedReaders;
this.restrictionCoder = restrictionCoder;
+ this.onClaimCallback = onClaimCallback;
+ }
+
+ private static void scheduleCacheCleanUp(Cache<Object, ?> cachedReaders)
{
+ if (CACHES_WITH_SCHEDULED_CLEAN_UP.add(cachedReaders)) {
+ @SuppressWarnings("FutureReturnValueIgnored")
+ Object unused =
+ cacheCleanUpExecutor.schedule(
+ () -> {
+ try {
+ cachedReaders.cleanUp();
+ } finally {
+ CACHES_WITH_SCHEDULED_CLEAN_UP.remove(cachedReaders);
+ if (cachedReaders.size() > 0) {
Review Comment:
**🟡 Medium**: There is a race condition between `scheduleCacheCleanUp` and
the `finally` block here: if another thread calls `cachedReaders.put(...)` and
`scheduleCacheCleanUp(cachedReaders)` while `cachedReaders.cleanUp()` is
running (before `CACHES_WITH_SCHEDULED_CLEAN_UP.remove(cachedReaders)`
executes), `CACHES_WITH_SCHEDULED_CLEAN_UP.add(cachedReaders)` in the other
thread will return `false`. However, because Guava's `LocalCache.size()` sums
segment `count` fields that are only incremented *after* the entry is written
and may not be immediately visible across threads without synchronization,
`cachedReaders.size() > 0` could evaluate to `0` if the concurrent `put` is in
flight just after `add(cachedReaders)` returned `false` (between
`scheduleCacheCleanUp` being called if `put` and `scheduleCacheCleanUp` were
reordered, or if `cleanUp()` evicted everything just as another thread checked
`add()`). More importantly, if `remove(cachedReaders)` happens before checking
`cachedR
eaders.size() > 0`, any concurrent `put` that happens before `remove` is
guaranteed to already be visible in `cachedReaders` when `size()` is checked
after `remove`. You should also catch `Throwable` inside the scheduled task so
an unexpected exception during `cleanUp()` doesn't silently break future
scheduling.
--
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]