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 cc111c04cc62 CAMEL-26929: camel-cli-connector - the trace file starts
again at 10 MB, and camel trace and the TUI follow the new file (#30028)
cc111c04cc62 is described below
commit cc111c04cc624c0bfd6bb6945ab780fb27ba70ac
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 20:21:40 2026 +0100
CAMEL-26929: camel-cli-connector - the trace file starts again at 10 MB,
and camel trace and the TUI follow the new file (#30028)
While tracing is on, the connector appended every trace to
~/.camel/<pid>-trace.json without a limit: 758 KB after ten
minutes of a 2 s timer, about 15 MB a minute at 100 exchanges a second, and
camel trace reads it whole for --tail. When
the file would grow past 10 MB, the connector now deletes it and the traces
start a new file. camel trace (follow) and
the trace view of the TUI see the file become smaller and read the new file
from its start; the TUI read nothing from
it again before.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/catalog/docs/cli-connector.adoc | 4 ++
.../src/main/docs/cli-connector.adoc | 4 ++
.../cli/connector/FileCliConnectorTransport.java | 18 ++++++
.../camel/cli/connector/TraceFileSizeTest.java | 64 ++++++++++++++++++++++
.../core/commands/action/CamelTraceAction.java | 19 ++++++-
.../core/commands/action/CamelTraceActionTest.java | 9 +++
.../core/commands/tui/DataRefreshService.java | 9 +++
.../commands/tui/DataRefreshMembershipTest.java | 8 +++
8 files changed, 133 insertions(+), 2 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cli-connector.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cli-connector.adoc
index 53ed54173beb..588984657479 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cli-connector.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/cli-connector.adoc
@@ -68,6 +68,10 @@ file is added to it, and with every status it lists it
anyway. It also writes th
phase, such as when it has started, and after it answers actions (at most
every 200 millis), so the status shows what
an action did.
+While tracing is on, the connector appends the new traces to
`~/.camel/<pid>-trace.json`, one JSON object per line.
+When the file would grow past 10 MB, it starts a new file: a tool that follows
the file sees it become smaller, and
+reads the new file from its start.
+
Write the action file atomically (write a temporary file, then move it), so
the connector never reads half an action.
An action file that cannot be read yet is retried for two seconds. Deleting
the action file before the answer is
written cancels the action.
diff --git a/dsl/camel-cli-connector/src/main/docs/cli-connector.adoc
b/dsl/camel-cli-connector/src/main/docs/cli-connector.adoc
index 53ed54173beb..588984657479 100644
--- a/dsl/camel-cli-connector/src/main/docs/cli-connector.adoc
+++ b/dsl/camel-cli-connector/src/main/docs/cli-connector.adoc
@@ -68,6 +68,10 @@ file is added to it, and with every status it lists it
anyway. It also writes th
phase, such as when it has started, and after it answers actions (at most
every 200 millis), so the status shows what
an action did.
+While tracing is on, the connector appends the new traces to
`~/.camel/<pid>-trace.json`, one JSON object per line.
+When the file would grow past 10 MB, it starts a new file: a tool that follows
the file sees it become smaller, and
+reads the new file from its start.
+
Write the action file atomically (write a temporary file, then move it), so
the connector never reads half an action.
An action file that cannot be read yet is retried for two seconds. Deleting
the action file before the answer is
written cancels the action.
diff --git
a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/FileCliConnectorTransport.java
b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/FileCliConnectorTransport.java
index 21ff9008d5d5..44abea427b72 100644
---
a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/FileCliConnectorTransport.java
+++
b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/FileCliConnectorTransport.java
@@ -98,6 +98,9 @@ public class FileCliConnectorTransport extends ServiceSupport
implements CliConn
private static final long CLOCK_TICK_MS = 20;
// how often at most the status is written for the actions answered
between two snapshots, as collecting it costs
private static final long ANSWERED_STATUS_MS = 200;
+ // the trace file starts again when it would grow past this: it grew
without limit while tracing is on, about 15 MB a
+ // minute at 100 exchanges a second, and camel trace reads it whole
(CAMEL-26929)
+ static final long MAX_TRACE_FILE_SIZE = 10 * 1024 * 1024;
private record PendingAction(File output, boolean replyAlways,
CompletableFuture<CliActionOutcome> outcome) {
}
@@ -446,6 +449,20 @@ public class FileCliConnectorTransport extends
ServiceSupport implements CliConn
}
}
+ /**
+ * Deletes the file when the data to append would make it larger than the
maximum, so the data starts a new file. A
+ * reader that follows the file sees it become smaller, and reads the new
file from its start.
+ *
+ * @return whether the file was deleted
+ */
+ static boolean startNewFileIfFull(File file, long append, long max) {
+ long length = file.length();
+ if (length > 0 && length + append > max) {
+ return FileUtil.deleteFile(file);
+ }
+ return false;
+ }
+
protected void traceTask() {
try {
JsonObject json = snapshots.trace();
@@ -462,6 +479,7 @@ public class FileCliConnectorTransport extends
ServiceSupport implements CliConn
// store traces in a special file
LOG.trace("Updating trace file: {}", traceFile);
String data = json.toJson() + System.lineSeparator();
+ startNewFileIfFull(traceFile, data.length(),
MAX_TRACE_FILE_SIZE);
IOHelper.appendText(data, traceFile);
json = arr.getMap(arr.size() - 1);
traceFilePos = json.getLong("uid");
diff --git
a/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/TraceFileSizeTest.java
b/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/TraceFileSizeTest.java
new file mode 100644
index 000000000000..5a86e1a50163
--- /dev/null
+++
b/dsl/camel-cli-connector/src/test/java/org/apache/camel/cli/connector/TraceFileSizeTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.cli.connector;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * The trace file starts again when it is full, instead of growing while
tracing is on (CAMEL-26929).
+ */
+class TraceFileSizeTest {
+
+ @TempDir
+ Path dir;
+
+ @Test
+ void aFileThatWouldGrowPastTheMaximumStartsAgain() throws Exception {
+ File file = Files.writeString(dir.resolve("1-trace.json"),
"x".repeat(90)).toFile();
+
+ assertThat(FileCliConnectorTransport.startNewFileIfFull(file, 20,
100)).isTrue();
+ assertThat(file).doesNotExist();
+ }
+
+ @Test
+ void aFileWithRoomIsAppendedTo() throws Exception {
+ File file = Files.writeString(dir.resolve("1-trace.json"),
"x".repeat(50)).toFile();
+
+ assertThat(FileCliConnectorTransport.startNewFileIfFull(file, 20,
100)).isFalse();
+ assertThat(file).hasContent("x".repeat(50));
+ }
+
+ @Test
+ void anEmptyFileTakesDataLargerThanTheMaximum() throws Exception {
+ // a single snapshot larger than the maximum is still written
+ File file = dir.resolve("1-trace.json").toFile();
+
+ assertThat(FileCliConnectorTransport.startNewFileIfFull(file, 200,
100)).isFalse();
+ }
+
+ @Test
+ void theMaximumIsTenMegabytes() {
+
assertThat(FileCliConnectorTransport.MAX_TRACE_FILE_SIZE).isEqualTo(10L * 1024
* 1024);
+ }
+}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
index 078c9203c475..37cff9826fa2 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceAction.java
@@ -516,13 +516,19 @@ public class CamelTraceAction extends ActionBaseCommand {
int lines = 0;
for (Pid pid : pids.values()) {
+ Path file = getTraceFile(pid.pid);
+ long size = Files.exists(file) ? Files.size(file) : 0;
+ if (pid.reader != null && isNewFile(pid.size, size)) {
+ // the integration started a new trace file, as the old one
was full: read the new one from its start
+ IOHelper.close(pid.reader);
+ pid.reader = new
LineNumberReader(Files.newBufferedReader(file));
+ }
+ pid.size = size;
if (pid.reader == null) {
- Path file = getTraceFile(pid.pid);
if (Files.exists(file)) {
pid.reader = new
LineNumberReader(Files.newBufferedReader(file));
if (tail == 0) {
// only read new lines so forward to end of reader
- long size = Files.size(file);
pid.reader.skip(size);
}
}
@@ -551,6 +557,13 @@ public class CamelTraceAction extends ActionBaseCommand {
return lines;
}
+ /**
+ * Whether a file that a reader follows was started again: it is smaller
than when it was last read (CAMEL-26929).
+ */
+ static boolean isNewFile(long lastSize, long size) {
+ return size < lastSize;
+ }
+
private List<Row> parseTraceLine(Pid pid, String line) {
JsonObject root = null;
try {
@@ -980,6 +993,8 @@ public class CamelTraceAction extends ActionBaseCommand {
Queue<String> fifo;
int depth;
LineNumberReader reader;
+ // the size of the trace file when it was last read
+ long size;
}
private static class Row {
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceActionTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceActionTest.java
index cd10d18ab6db..97d222a43407 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceActionTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelTraceActionTest.java
@@ -278,6 +278,15 @@ class CamelTraceActionTest extends
ActionCommandTestSupport {
return root;
}
+ @Test
+ void aTraceFileThatGotSmallerIsANewFile() {
+ // CAMEL-26929: the integration starts a new trace file when it is
full, and camel trace reads it from its start
+ assertThat(CamelTraceAction.isNewFile(10_000, 120)).isTrue();
+ assertThat(CamelTraceAction.isNewFile(10_000, 10_000)).isFalse();
+ assertThat(CamelTraceAction.isNewFile(10_000, 12_000)).isFalse();
+ assertThat(CamelTraceAction.isNewFile(0, 120)).isFalse();
+ }
+
@Test
void dumpAsJsonLines() throws Exception {
// CAMEL-26929: the dump had no --json; now each trace is a JSON
object of its own line
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshService.java
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshService.java
index 8c2910d0c956..fa0ad4584e6a 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshService.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshService.java
@@ -668,6 +668,14 @@ class DataRefreshService {
traces.set(allTraces);
}
+ /**
+ * Where to read a file that grows on from: the position read up to, or
the start when the file is smaller than
+ * that, as the integration started a new trace file when it was full
(CAMEL-26929).
+ */
+ static long readFrom(long lastPos, long length) {
+ return length < lastPos ? 0 : lastPos;
+ }
+
private void readTraceFile(String pid, List<TraceEntry> allTraces) {
Path traceFile = CommandLineHelper.getCamelDir().resolve(pid +
"-trace.json");
if (!Files.exists(traceFile)) {
@@ -678,6 +686,7 @@ class DataRefreshService {
try (RandomAccessFile raf = new RandomAccessFile(traceFile.toFile(),
"r")) {
long length = raf.length();
+ lastPos = readFrom(lastPos, length);
if (length <= lastPos) {
return; // no new data
}
diff --git
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshMembershipTest.java
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshMembershipTest.java
index ac61da9bcc27..7e00b06dcf18 100644
---
a/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshMembershipTest.java
+++
b/dsl/camel-jbang/camel-jbang-plugin-tui/src/test/java/org/apache/camel/dsl/jbang/core/commands/tui/DataRefreshMembershipTest.java
@@ -169,4 +169,12 @@ class DataRefreshMembershipTest {
},
Path::of, Path::of);
}
+
+ @Test
+ void aTraceFileThatGotSmallerIsReadFromItsStart() {
+ // CAMEL-26929: the integration starts a new trace file when it is
full; the panel read nothing from it again
+ assertThat(DataRefreshService.readFrom(10_000, 120)).isZero();
+ assertThat(DataRefreshService.readFrom(10_000,
12_000)).isEqualTo(10_000);
+ assertThat(DataRefreshService.readFrom(0, 120)).isZero();
+ }
}