allthingssecurity commented on code in PR #30910: URL: https://github.com/apache/camel/pull/30910#discussion_r4717042289
########## docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc: ########## @@ -1562,6 +1562,17 @@ file consumer no longer adds the key to the idempotent repository eagerly, and ` read lock adds it when it acquires the lock. Since Camel 4.7 the read lock was refused, and such a consumer never consumed a file. +=== camel-file - a stopping idempotent read lock releases its delayed read locks at once + +With `readLock=idempotent` or `idempotent-changed` and `readLockIdempotentReleaseAsync=true`, the read locks whose +release still waits for `readLockIdempotentReleaseDelay` are released when the consumer stops, at once. Before, with the +pool Camel creates, the stop waited for them for up to 30 seconds, and a release still pending after that was dropped: +its key stayed in the idempotent repository, so the file was never picked up again (with a persistent or clustered +repository, not even after a restart). With `readLockRemoveOnCommit=true`, the keys of the files a stopping node +committed in the last `readLockIdempotentReleaseDelay` millis are now removed before the delay is over. +This also applies with a `readLockIdempotentReleaseExecutorService` of your own, which Camel does not shut down: +there nothing was dropped before, and the pending releases now run at the stop instead of when their delay is over. Review Comment: Applied in 45c7ba23, thanks. ########## 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: Good catch, fixed in 45c7ba23: runPending now counts only the releases it ran itself, logs that count after the loop and returns it. DelayedReadLockReleaseTasksTest asserts 1, then 0 on a second stop. -- 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]
