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 3012cfce9d64 CAMEL-26945: camel-core - the time an inflight exchange
has been at its node is no longer garbage (#29887)
3012cfce9d64 is described below
commit 3012cfce9d640d3e25937cd8b6595a79e9f30808
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 14 09:42:54 2026 +0100
CAMEL-26945: camel-core - the time an inflight exchange has been at its
node is no longer garbage (#29887)
While an exchange is at a node, the inflight repository computes how long it
has been there with MessageHistory.getElapsedSinceCreated(), whose default
subtracted the creation time in wall clock millis from System.nanoTime().
The
result was about the uptime of the machine in nanoseconds, shown as elapsed
by the inflight dev console (camel get inflight) and the JMX browse of the
inflight repository.
DefaultMessageHistory, which the message history factories of Camel use,
measures it on its monotonic clock, as it measures the time the node took.
The default method subtracts the creation time from the current time in
millis. The descriptions of elapsed and duration in the inflight dev console
were swapped.
Signed-off-by: Claus Ibsen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/catalog/dev-consoles-openapi.json | 4 +-
.../camel/catalog/dev-consoles/inflight.json | 4 +-
.../org/apache/camel/catalog/docs/test-dst.adoc | 14 ++--
.../camel-test-dst/src/main/docs/test-dst.adoc | 14 ++--
.../main/java/org/apache/camel/MessageHistory.java | 5 +-
.../org/apache/camel/dev-console/inflight.json | 4 +-
.../apache/camel/impl/console/InflightConsole.java | 4 +-
.../camel/impl/InflightRepositoryElapsedTest.java | 98 ++++++++++++++++++++++
.../camel/util/TimeSourceDirectClockCallsTest.java | 2 +
.../camel/support/DefaultMessageHistory.java | 6 ++
10 files changed, 133 insertions(+), 22 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles-openapi.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles-openapi.json
index 1f94236c2a74..a2ad636c4a3e 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles-openapi.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles-openapi.json
@@ -2883,11 +2883,11 @@
},
"elapsed": {
"type": "integer",
-
"description": "Elapsed time in milliseconds
since the exchange started"
+
"description": "Elapsed time in milliseconds
the exchange has been at the current node"
},
"duration": {
"type": "integer",
-
"description": "Duration in milliseconds the
exchange has been at the current node"
+
"description": "Duration in milliseconds since
the exchange started"
}
},
"required": [
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/inflight.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/inflight.json
index f66ea07b31e8..80c0f6810761 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/inflight.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/dev-consoles/inflight.json
@@ -84,11 +84,11 @@
},
"elapsed": {
"type": "integer",
- "description": "Elapsed time in milliseconds since the exchange
started"
+ "description": "Elapsed time in milliseconds the exchange has
been at the current node"
},
"duration": {
"type": "integer",
- "description": "Duration in milliseconds the exchange has been
at the current node"
+ "description": "Duration in milliseconds since the exchange
started"
}
},
"required": [
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
index 9a4fe1e02844..b0b386725d6f 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/test-dst.adoc
@@ -136,12 +136,14 @@ does it when the run is created) and puts the previous
provider back when the ru
the synchronous redelivery (`RedeliveryPolicy`, `RedeliveryErrorHandler`),
`ForegroundTask`, `BackgroundTask.schedule`,
the budgets of both tasks, the wait loop of `DefaultShutdownStrategy`, the
synchronous delayer, `MulticastProcessor`,
`OptimisticLockRetryPolicy`, `DefaultStartupConditionStrategy`, `StopWatch`,
`MonotonicClock`, `BackOffTimerTask`,
-`DefaultTimeoutMap`, the clocks of the throttlers and the counters of
`ThrottlingExceptionRoutePolicy`. A sleep there is
-a virtual wait on the loop thread, traced with its call site. A test of
camel-core (`TimeSourceDirectClockCallsTest`)
-fails when one of these classes reads the system clock or sleeps directly:
`System.nanoTime()`,
-`System.currentTimeMillis()`, `Thread.sleep`, a `sleep` of a `TimeUnit`
constant, `Instant.now()` and the `now()` of the
-other `java.time` types, `new Date()`, `Calendar.getInstance()`, `new
GregorianCalendar()` and the system `Clock`s. It
-also fails when a class of a core module uses `TimeSource` and is not in its
list.
+`DefaultTimeoutMap`, the elapsed time of `MessageHistory`, the clocks of the
throttlers and the counters of
+`ThrottlingExceptionRoutePolicy`. A sleep there is a virtual wait on the loop
thread, traced with its call site.
+
+A test of camel-core (`TimeSourceDirectClockCallsTest`) fails when one of
these classes reads the system clock or sleeps
+directly: `System.nanoTime()`, `System.currentTimeMillis()`, `Thread.sleep`, a
`sleep` of a `TimeUnit` constant,
+`Instant.now()` and the `now()` of the other `java.time` types, `new Date()`,
`Calendar.getInstance()`,
+`new GregorianCalendar()` and the system `Clock`s. It also fails when a class
of a core module uses `TimeSource` and is
+not in its list.
Two waits of these classes are not virtual (see below):
the permit queue of `TotalRequestsThrottler` and the `Semaphore` of
`ConcurrentRequestsThrottler`. The sleep of the
route `delayer` option is not virtual either (see below). The half-open check
of `ThrottlingExceptionRoutePolicy` runs
diff --git a/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
b/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
index 9a4fe1e02844..b0b386725d6f 100644
--- a/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
+++ b/components/camel-test/camel-test-dst/src/main/docs/test-dst.adoc
@@ -136,12 +136,14 @@ does it when the run is created) and puts the previous
provider back when the ru
the synchronous redelivery (`RedeliveryPolicy`, `RedeliveryErrorHandler`),
`ForegroundTask`, `BackgroundTask.schedule`,
the budgets of both tasks, the wait loop of `DefaultShutdownStrategy`, the
synchronous delayer, `MulticastProcessor`,
`OptimisticLockRetryPolicy`, `DefaultStartupConditionStrategy`, `StopWatch`,
`MonotonicClock`, `BackOffTimerTask`,
-`DefaultTimeoutMap`, the clocks of the throttlers and the counters of
`ThrottlingExceptionRoutePolicy`. A sleep there is
-a virtual wait on the loop thread, traced with its call site. A test of
camel-core (`TimeSourceDirectClockCallsTest`)
-fails when one of these classes reads the system clock or sleeps directly:
`System.nanoTime()`,
-`System.currentTimeMillis()`, `Thread.sleep`, a `sleep` of a `TimeUnit`
constant, `Instant.now()` and the `now()` of the
-other `java.time` types, `new Date()`, `Calendar.getInstance()`, `new
GregorianCalendar()` and the system `Clock`s. It
-also fails when a class of a core module uses `TimeSource` and is not in its
list.
+`DefaultTimeoutMap`, the elapsed time of `MessageHistory`, the clocks of the
throttlers and the counters of
+`ThrottlingExceptionRoutePolicy`. A sleep there is a virtual wait on the loop
thread, traced with its call site.
+
+A test of camel-core (`TimeSourceDirectClockCallsTest`) fails when one of
these classes reads the system clock or sleeps
+directly: `System.nanoTime()`, `System.currentTimeMillis()`, `Thread.sleep`, a
`sleep` of a `TimeUnit` constant,
+`Instant.now()` and the `now()` of the other `java.time` types, `new Date()`,
`Calendar.getInstance()`,
+`new GregorianCalendar()` and the system `Clock`s. It also fails when a class
of a core module uses `TimeSource` and is
+not in its list.
Two waits of these classes are not virtual (see below):
the permit queue of `TotalRequestsThrottler` and the `Semaphore` of
`ConcurrentRequestsThrottler`. The sleep of the
route `delayer` option is not virtual either (see below). The half-open check
of `ThrottlingExceptionRoutePolicy` runs
diff --git a/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
b/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
index 68d79f0e5b8c..8cfea6b46267 100644
--- a/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel;
+import org.apache.camel.util.TimeSource;
import org.jspecify.annotations.Nullable;
/**
@@ -52,10 +53,10 @@ public interface MessageHistory {
long getElapsed();
/**
- * The elapsed time since created.
+ * The elapsed time in millis since created, such as how long the node has
been processing while it is not done.
*/
default long getElapsedSinceCreated() {
- return System.nanoTime() - getTime();
+ return TimeSource.currentTimeMillis() - getTime();
}
/**
diff --git
a/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/inflight.json
b/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/inflight.json
index f66ea07b31e8..80c0f6810761 100644
---
a/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/inflight.json
+++
b/core/camel-console/src/generated/resources/META-INF/org/apache/camel/dev-console/inflight.json
@@ -84,11 +84,11 @@
},
"elapsed": {
"type": "integer",
- "description": "Elapsed time in milliseconds since the exchange
started"
+ "description": "Elapsed time in milliseconds the exchange has
been at the current node"
},
"duration": {
"type": "integer",
- "description": "Duration in milliseconds the exchange has been
at the current node"
+ "description": "Duration in milliseconds since the exchange
started"
}
},
"required": [
diff --git
a/core/camel-console/src/main/java/org/apache/camel/impl/console/InflightConsole.java
b/core/camel-console/src/main/java/org/apache/camel/impl/console/InflightConsole.java
index e2624a341fbb..ec4a75949620 100644
---
a/core/camel-console/src/main/java/org/apache/camel/impl/console/InflightConsole.java
+++
b/core/camel-console/src/main/java/org/apache/camel/impl/console/InflightConsole.java
@@ -44,8 +44,8 @@ public class InflightConsole extends AbstractDevConsole {
@Metadata(description = "The route ID where the exchange currently
is") String atRouteId,
@Metadata(description = "The node ID where the exchange currently
is") String nodeId,
@Metadata(description = "Where the node is in the source, such as
orders.camel.yaml:18") String nodeSource,
- @Metadata(description = "Elapsed time in milliseconds since the
exchange started") long elapsed,
- @Metadata(description = "Duration in milliseconds the exchange has
been at the current node") long duration) {
+ @Metadata(description = "Elapsed time in milliseconds the exchange
has been at the current node") long elapsed,
+ @Metadata(description = "Duration in milliseconds since the
exchange started") long duration) {
}
public record Response(
diff --git
a/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryElapsedTest.java
b/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryElapsedTest.java
new file mode 100644
index 000000000000..7759c0e7c312
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryElapsedTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.impl;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.util.Collection;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.MessageHistory;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.InflightRepository;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * CAMEL-26945: the time an inflight exchange has been at the node it is at,
while it is still there.
+ */
+public class InflightRepositoryElapsedTest extends ContextTestSupport {
+
+ private final CountDownLatch done = new CountDownLatch(1);
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.setMessageHistory(true);
+ context.getInflightRepository().setInflightBrowseEnabled(true);
+ return context;
+ }
+
+ @Test
+ public void testElapsedAtTheNodeTheExchangeIsAt() {
+ template.asyncSendBody("direct:start", "Hello World");
+ try {
+ // the exchange waits at the node: the time it has been there grows
+ await().atMost(10, TimeUnit.SECONDS).until(() -> elapsed() >= 100);
+
+ InflightRepository.InflightExchange inflight =
context.getInflightRepository().browse().iterator().next();
+ assertEquals("wait", inflight.getNodeId());
+ long elapsed = inflight.getElapsed();
+ long duration = inflight.getDuration();
+ // and it is no longer than the exchange is old
+ assertTrue(elapsed >= 100 && elapsed <= duration,
+ "Elapsed " + elapsed + " ms at the node of an exchange
that is " + duration + " ms old");
+ } finally {
+ done.countDown();
+ }
+ }
+
+ @Test
+ public void testElapsedSinceCreatedOfAMessageHistoryWithoutAClock() {
+ // a message history of another factory, which only has the time it
was created
+ long created = System.currentTimeMillis() - 250;
+ MessageHistory history = (MessageHistory)
Proxy.newProxyInstance(MessageHistory.class.getClassLoader(),
+ new Class<?>[] { MessageHistory.class },
+ (proxy, method, args) -> "getTime".equals(method.getName())
+ ? created : InvocationHandler.invokeDefault(proxy,
method, args));
+
+ long elapsed = history.getElapsedSinceCreated();
+ assertTrue(elapsed >= 250 && elapsed < 10000, "Elapsed " + elapsed + "
ms since created 250 ms ago");
+ }
+
+ private long elapsed() {
+ Collection<InflightRepository.InflightExchange> list =
context.getInflightRepository().browse();
+ return list.isEmpty() ? 0 : list.iterator().next().getElapsed();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start").routeId("slow")
+ .process(e -> done.await(10,
TimeUnit.SECONDS)).id("wait");
+ }
+ };
+ }
+}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/util/TimeSourceDirectClockCallsTest.java
b/core/camel-core/src/test/java/org/apache/camel/util/TimeSourceDirectClockCallsTest.java
index 2cf9c791ec1f..4c96635be8dd 100644
---
a/core/camel-core/src/test/java/org/apache/camel/util/TimeSourceDirectClockCallsTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/util/TimeSourceDirectClockCallsTest.java
@@ -66,6 +66,8 @@ class TimeSourceDirectClockCallsTest {
* (and to the list of core classes in test-dst.adoc of camel-test-dst).
*/
static final List<String> CLASSES = List.of(
+ // camel-api
+ "camel-api/src/main/java/org/apache/camel/MessageHistory.java",
// camel-util
"camel-util/src/main/java/org/apache/camel/util/TimeSource.java",
"camel-util/src/main/java/org/apache/camel/util/StopWatch.java",
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
index 4a8e595c2243..0eced96b808b 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
@@ -160,6 +160,12 @@ public class DefaultMessageHistory implements
MessageHistory {
}
}
+ @Override
+ public long getElapsedSinceCreated() {
+ // the monotonic clock, as the node processing time is measured
+ return clock.elapsed();
+ }
+
@Override
public Message getMessage() {
return message;