shashank created CAMEL-24927:
--------------------------------
Summary: Sample EIP drops every message until the machine's uptime
exceeds the sample period
Key: CAMEL-24927
URL: https://issues.apache.org/jira/browse/CAMEL-24927
Project: Camel
Issue Type: Bug
Components: camel-core
Reporter: shashank
The sample EIP is documented to select a single message in a given time period,
so the first message should always pass. With a long sample period it drops
everything for a while:
{code:java}
from("direct:start").sample(Duration.ofHours(24)).to("mock:result");
{code}
On a machine that was started less than 24 hours ago, no message reaches
{{mock:result}} at all until the machine has been up for 24 hours. Measured on
a machine that had been up for about 10 minutes ({{System.nanoTime()}} = 634906
ms), sending 5 messages to a fresh route:
{noformat}
sample(PT1S): 1 passed (expected 1)
sample(PT5M17.453S): 1 passed (expected 1)
sample(PT1H10M34.906S): 0 passed (expected 1)
sample(PT8760H): 0 passed (expected 1)
{noformat}
On a JVM where {{System.nanoTime()}} has a negative origin (allowed by its
javadoc), even the default 1 second period drops the first messages.
Cause: {{SamplingThrottler}} measures time with {{System.nanoTime()}} (since
CAMEL-20267, which moved away from {{System.currentTimeMillis()}}), but
{{timeOfLastExchange}} still starts at {{0}}, and an exchange passes when {{now
>= timeOfLastExchange + periodInMillis}}. With {{currentTimeMillis}} the first
comparison always passed; with {{nanoTime}}, whose origin is arbitrary (on
Linux and macOS about the time since boot), it only passes once {{nanoTime}}
exceeds the period.
Proposed fix: remember whether an exchange has been sampled yet, and always let
the first one through.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)