This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 1c206a4c0921 CAMEL-27382: camel-file - a done file kept for the files
a poll left is no longer deleted while a file of that poll still routes (#30700)
1c206a4c0921 is described below
commit 1c206a4c092110dffac53b9cb52108166fe6fec7
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 24 12:14:41 2026 +0530
CAMEL-27382: camel-file - a done file kept for the files a poll left is no
longer deleted while a file of that poll still routes (#30700)
A done file with a fixed name is kept when a poll leaves files of its
directory for the next poll. Two cases deleted it while the last file of
that poll was still routing on another thread (seda, an async route):
- Stopping the consumer cleared the kept done files in place, and the files
of the poll still routing refer to that same set (CAMEL-25960). The last
of them then deleted the done file, and after a restart the files left
were never consumed. The consumer now forgets the kept done files by
starting a new set, so the files of the last poll keep what their poll
kept.
- With recursive=true, the next complete listing does not list a file that
is still in progress, so it took the kept done file of its directory as
gone and deleted it. When that file then failed, it stayed without its
done file. The listing now records the done files of the files it skips
as in progress, and such a done file stays kept until a complete listing
finds its directory without files in progress.
Found with a TLA+ model.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/component/file/GenericFileConsumer.java | 9 ++
.../component/file/GenericFileKeptDoneFiles.java | 47 +++++-
.../file/GenericFileKeptDoneFilesTest.java | 35 +++++
...leConsumerDoneFileNameKeptWhileRoutingTest.java | 161 +++++++++++++++++++++
4 files changed, 246 insertions(+), 6 deletions(-)
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index c36d7e223ec4..0f7aa946913a 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -176,6 +176,10 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
// time how long it takes to poll
StopWatch stop = new StopWatch();
boolean limitHit;
+ GenericFileKeptDoneFiles keptDoneFilesOfPoll = getKeptDoneFiles();
+ if (keptDoneFilesOfPoll != null) {
+ keptDoneFilesOfPoll.startListing();
+ }
try {
limitHit = !pollDirectory(dynamic, name, files, 0);
} catch (Exception e) {
@@ -853,6 +857,11 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
if (LOG.isTraceEnabled()) {
LOG.trace("Skipping as file is already in progress: {}", name);
}
+ // a file of an earlier poll still routing: its done file is not
gone, if the poll kept it
+ GenericFileKeptDoneFiles kept = getKeptDoneFiles();
+ if (kept != null) {
+
kept.skippedInProgress(endpoint.createDoneFileName(absoluteFilePath));
+ }
return false;
}
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileKeptDoneFiles.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileKeptDoneFiles.java
index d3820718a525..e33b6b2e5fd5 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileKeptDoneFiles.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileKeptDoneFiles.java
@@ -33,15 +33,35 @@ import java.util.Set;
* <p/>
* The files of a poll can complete on another thread while the next poll runs
(the route continues asynchronously), so
* the last file of a poll checks the done files kept by its own poll as well
as those kept by the current poll (see
- * {@link Batch}).
+ * {@link Batch}). For the same reason a kept done file whose directory has no
file listed is not gone while a file of
+ * an earlier poll in that directory is still in progress: it stays kept until
a complete listing finds the directory
+ * without files in progress.
* <p/>
- * The state is in memory, and the done files kept by the current poll are
forgotten when the consumer stops.
+ * The state is in memory, and the done files kept by the current poll are
forgotten when the consumer stops. The files
+ * of a poll that are still routing then keep what their poll kept.
*/
final class GenericFileKeptDoneFiles {
// the done files kept by the current poll: a complete listing starts a
new set, and the set of the previous poll
// stays with the files of that poll that have not completed yet
private Set<String> kept = new HashSet<>();
+ // the done files of the files the current listing skipped as they are
still in progress (files of an earlier poll
+ // that have not completed yet)
+ private Set<String> inProgress = new HashSet<>();
+
+ /**
+ * Called before the directory is listed.
+ */
+ synchronized void startListing() {
+ inProgress = new HashSet<>();
+ }
+
+ /**
+ * The listing skipped a file of this done file as it is still in progress.
+ */
+ synchronized void skippedInProgress(String doneFileName) {
+ inProgress.add(doneFileName);
+ }
/**
* Called once the directory is listed, before the files are consumed.
When the listing was complete, forgets the
@@ -51,23 +71,32 @@ final class GenericFileKeptDoneFiles {
* files may be among those not listed, and the caller then keeps the done
file of every listed file, as the poll
* leaves files for the next one.
*
+ * A kept done file of which the listing found no file, but skipped a file
still in progress, stays kept: that file
+ * may still fail and then needs its done file to be polled again, and the
next complete listing decides again.
+ *
* @param listedDoneFiles the done files of the files this poll found
* @param listingComplete whether every file was listed
* @return the done files kept for files that are gone
(the listing was complete and found none of
- * their files), to delete
+ * their files, nor any of their files in
progress), to delete
*/
synchronized List<String> startBatch(Set<String> listedDoneFiles, boolean
listingComplete) {
List<String> answer = null;
if (listingComplete) {
+ Set<String> stillKept = new HashSet<>();
for (String doneFileName : kept) {
- if (!listedDoneFiles.contains(doneFileName)) {
+ if (listedDoneFiles.contains(doneFileName)) {
+ continue;
+ }
+ if (inProgress.contains(doneFileName)) {
+ stillKept.add(doneFileName);
+ } else {
if (answer == null) {
answer = new ArrayList<>();
}
answer.add(doneFileName);
}
}
- kept = new HashSet<>();
+ kept = stillKept;
}
return answer;
}
@@ -93,8 +122,14 @@ final class GenericFileKeptDoneFiles {
return new Batch(kept);
}
+ /**
+ * Forgets the kept done files, when the consumer stops. A new set, not
cleared in place: the files of the last poll
+ * that are still routing (handed over to another thread) refer to the set
of their poll, and must still find their
+ * done file kept, or the last of them deletes it and the files the poll
left are not consumed after a restart.
+ */
synchronized void clear() {
- kept.clear();
+ kept = new HashSet<>();
+ inProgress = new HashSet<>();
}
/**
diff --git
a/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileKeptDoneFilesTest.java
b/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileKeptDoneFilesTest.java
index 750cc4494dfe..759ea9cb96f6 100644
---
a/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileKeptDoneFilesTest.java
+++
b/components/camel-file/src/test/java/org/apache/camel/component/file/GenericFileKeptDoneFilesTest.java
@@ -77,4 +77,39 @@ class GenericFileKeptDoneFilesTest {
assertTrue(nextPoll.isKept("other/ready"));
assertTrue(lastPoll.isKept("other/ready"));
}
+
+ @Test
+ void clearLeavesTheFilesOfTheLastPollTheirKeptDoneFiles() {
+ keptDoneFiles.keep("dir/ready");
+ GenericFileKeptDoneFiles.Batch lastPoll = keptDoneFiles.batch();
+
+ // the consumer stops while a file of the last poll still routes on
another thread
+ keptDoneFiles.clear();
+ assertFalse(keptDoneFiles.isKept("dir/ready"));
+ assertTrue(lastPoll.isKept("dir/ready"));
+ }
+
+ @Test
+ void doneFileOfAFileInProgressIsNotGone() {
+ keptDoneFiles.keep("routing/ready");
+ keptDoneFiles.keep("gone/ready");
+
+ // the listing skips the file of routing/ready that is still in
progress, and finds no file of gone/ready
+ keptDoneFiles.startListing();
+ keptDoneFiles.skippedInProgress("routing/ready");
+ assertEquals(List.of("gone/ready"), keptDoneFiles.startBatch(Set.of(),
true));
+ assertTrue(keptDoneFiles.isKept("routing/ready"));
+
+ // the next listing finds no file of routing/ready in progress any
more: it is gone
+ keptDoneFiles.startListing();
+ assertEquals(List.of("routing/ready"),
keptDoneFiles.startBatch(Set.of(), true));
+ assertFalse(keptDoneFiles.isKept("routing/ready"));
+
+ // a done file listed again is decided again by the files of the poll,
in progress or not
+ keptDoneFiles.keep("dir/ready");
+ keptDoneFiles.startListing();
+ keptDoneFiles.skippedInProgress("dir/ready");
+ assertNull(keptDoneFiles.startBatch(Set.of("dir/ready"), true));
+ assertFalse(keptDoneFiles.isKept("dir/ready"));
+ }
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDoneFileNameKeptWhileRoutingTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDoneFileNameKeptWhileRoutingTest.java
new file mode 100644
index 000000000000..e88029aa4393
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDoneFileNameKeptWhileRoutingTest.java
@@ -0,0 +1,161 @@
+/*
+ * 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;
+
+import java.nio.file.Files;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.AsyncProcessorSupport;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * A done file with a fixed name that a poll kept, as it left files for the
next poll, must stay while a file of that
+ * poll is still routing on another thread: when the consumer is stopped
meanwhile, and when the next poll does not list
+ * the directory of that file because the file is still in progress.
+ */
+class FileConsumerDoneFileNameKeptWhileRoutingTest extends ContextTestSupport {
+
+ private final List<String> processed = new CopyOnWriteArrayList<>();
+ private final CountDownLatch release = new CountDownLatch(1);
+ private final AtomicReference<String> first = new AtomicReference<>();
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ @Test
+ void stopWhileTheLastFileOfThePollRoutesKeepsTheDoneFile() throws
Exception {
+ Files.writeString(testFile("a.txt"), "A");
+ Files.writeString(testFile("b.txt"), "B");
+ Files.writeString(testFile("ready"), "");
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from(fileUri("?doneFileName=ready&maxMessagesPerPoll=1&initialDelay=0&delay=600000"))
+ .routeId("files").to("seda:work");
+ // the first file waits on the seda route until the test
releases it
+ from("seda:work").routeId("work").process(e -> {
+ String name =
e.getMessage().getHeader(Exchange.FILE_NAME_ONLY, String.class);
+ processed.add(name);
+ if (first.compareAndSet(null, name)) {
+ assertTrue(release.await(20, TimeUnit.SECONDS));
+ }
+ });
+ }
+ });
+ context.start();
+
+ // the first poll takes one file, and keeps the done file for the other
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).until(() ->
first.get() != null);
+ String taken = first.get();
+ String left = "a.txt".equals(taken) ? "b.txt" : "a.txt";
+
+ // the file route has no exchange in flight (the file was handed over
to seda), so it stops at once
+ context.getRouteController().stopRoute("files", 10, TimeUnit.SECONDS);
+ release.countDown();
+ // the file is moved once its done file was handled
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).until(() ->
!Files.exists(testFile(taken)));
+ assertTrue(Files.exists(testFile("ready")),
+ "The last file of the poll must not delete the done file kept
for " + left + " after the consumer stopped");
+
+ context.getRouteController().startRoute("files");
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).untilAsserted(() -> {
+ assertEquals(List.of(taken, left), processed);
+ assertTrue(!Files.exists(testFile(left)) &&
!Files.exists(testFile("ready")),
+ "The file left and then the done file must be consumed
after the restart");
+ });
+ }
+
+ @Test
+ void doneFileOfAFileStillRoutingIsNotGoneForTheNextPoll() throws Exception
{
+ Files.createDirectories(testDirectory("d1"));
+ Files.createDirectories(testDirectory("d2"));
+ Files.writeString(testFile("d1/a.txt"), "A");
+ Files.writeString(testFile("d1/ready"), "");
+ Files.writeString(testFile("d2/b.txt"), "B");
+ Files.writeString(testFile("d2/ready"), "");
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
from(fileUri("?doneFileName=ready&recursive=true&maxMessagesPerPoll=1&initialDelay=0&delay=10"))
+ .routeId("files")
+ .process(new FailFirstFileLater());
+ }
+ });
+ context.start();
+
+ // the first poll takes one file, and keeps both done files as it
leaves the other; that file routes on another
+ // thread, so the next poll lists only the other directory, and
consumes its file
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).until(() ->
processed.size() == 2);
+ String routing = first.get();
+ String routingDir = routing.substring(0, 2);
+ String other = processed.get(1);
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).until(() ->
!Files.exists(testFile(other)));
+ assertTrue(Files.exists(testFile(routingDir + "/ready")),
+ "The done file of " + routing + ", which is still routing,
must not be deleted as gone");
+
+ // the file fails, keeps its done file, and is consumed again
+ release.countDown();
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).untilAsserted(() -> {
+ assertEquals(2,
processed.stream().filter(routing::equals).count());
+ assertTrue(!Files.exists(testFile(routing)) &&
!Files.exists(testFile(routingDir + "/ready")),
+ "The failed file and then its done file must be consumed");
+ });
+ }
+
+ /**
+ * The first file continues on another thread, and fails once the test
releases it; the others complete at once.
+ */
+ private class FailFirstFileLater extends AsyncProcessorSupport {
+
+ @Override
+ public boolean process(Exchange exchange, AsyncCallback callback) {
+ String name = exchange.getMessage().getHeader(Exchange.FILE_NAME,
String.class).replace('\\', '/');
+ processed.add(name);
+ if (!first.compareAndSet(null, name)) {
+ callback.done(true);
+ return true;
+ }
+ Thread thread = new Thread(() -> {
+ try {
+ if (release.await(20, TimeUnit.SECONDS)) {
+ exchange.setException(new
IllegalStateException("Forced failure of " + name));
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ exchange.setException(e);
+ }
+ callback.done(false);
+ });
+ thread.start();
+ return false;
+ }
+ }
+}