gnodet-bot commented on code in PR #30910: URL: https://github.com/apache/camel/pull/30910#discussion_r4716770279
########## components/camel-file/src/main/java/org/apache/camel/component/file/strategy/DelayedReadLockReleaseTasks.java: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.file.strategy; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The read lock release tasks that an idempotent read lock strategy scheduled with readLockIdempotentReleaseAsync and + * readLockIdempotentReleaseDelay, and that have not run yet. When the strategy stops it runs them at once: shutting + * down the thread pool would drop them, and their keys would stay in the idempotent repository, so the files would + * never be picked up again. + */ +final class DelayedReadLockReleaseTasks { + + private static final Logger LOG = LoggerFactory.getLogger(DelayedReadLockReleaseTasks.class); + + private final Set<Task> pending = ConcurrentHashMap.newKeySet(); + + /** + * Schedules the release task to run after the delay. When the thread pool does not accept it, the exception is + * thrown and the task is not kept for the stop. + */ + void schedule(ScheduledExecutorService executorService, Runnable release, long delay) { + Task task = new Task(release); + pending.add(task); + try { + task.future = executorService.schedule(task, delay, TimeUnit.MILLISECONDS); + } catch (RuntimeException e) { + // not scheduled (the queue of the pool is full, or a pool of your own rejects it): do not keep it, with its + // exchange, until the stop + pending.remove(task); + throw e; + } + } + + /** + * Runs the release tasks that have not run yet in the calling thread. A task runs only once, here or in the thread + * pool. + */ + void runPending() { + if (pending.isEmpty()) { + return; + } + LOG.info("Releasing {} idempotent read locks now, before their readLockIdempotentReleaseDelay, as the read lock" + + " is stopping", + pending.size()); Review Comment: 🔍 **Logged count may overstate tasks actually run.** `pending.size()` is accurate for `ConcurrentHashMap.newKeySet()`, but tasks completed by concurrent executor threads between the `size()` call and the iteration will be counted but then skipped (they already ran). Minor cosmetic issue — the execution is correct. -- 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]
