Claus Ibsen created CAMEL-27198:
-----------------------------------

             Summary: camel-nats - JetStream producer ignores requestTimeout 
and jetstreamName
                 Key: CAMEL-27198
                 URL: https://issues.apache.org/jira/browse/CAMEL-27198
             Project: Camel
          Issue Type: Bug
          Components: camel-nats
            Reporter: Claus Ibsen


Three problems in {{NatsProducer.publishWithJetStream()}} with 
{{jetstreamEnabled=true}}, found while adding options to the Kamelet 
{{nats-sink}}.

h3. 1. requestTimeout does not reach jnats

The JetStream context is created with {{connection.jetStreamManagement()}} and 
{{jsm.jetStream()}}, without {{JetStreamOptions}}. jnats then uses its own 
request timeout (the connection timeout, 2 s by default) for the publish 
acknowledgement, and cancels a pending {{publishAsync}} at the next request 
cleanup run (every {{requestCleanupInterval}}, 5 s).

Camel uses {{requestTimeout}} (default 20000) only as the wait in 
{{future.get(requestTimeout, MILLISECONDS)}} for {{jetstreamAsync=true}}. So it 
can shorten the wait but never lengthen it, and it is ignored with 
{{jetstreamAsync=false}}. Measured with the NATS server paused ({{docker 
pause}}):

||requestTimeout||jetstreamAsync||publish fails after||
|500|true|0.5 s|
|unset (20000)|true|6 - 7.5 s|
|60000|true|7 s|
|500|false|2 s|

*Fix:* create the JetStream context with 
{{JetStreamOptions.builder().requestTimeout(Duration.ofMillis(config.getRequestTimeout()))}}
 (e.g. {{connection.jetStream(options)}}), once in {{doStart()}} rather than 
per message.

h3. 2. jetstreamName is never used by the producer

{code:java}
jsm = this.connection.jetStreamManagement();
js = jsm.jetStream();
if (js == null) {
    jsm.addStream(StreamConfiguration.builder()
            .name(config.getJetstreamName())
            .subjects(config.getTopic())
            .build());
    js = jsm.jetStream();
}
{code}

{{JetStreamManagement.jetStream()}} never returns null (jnats 2.26.4 creates 
the {{NatsJetStream}} on first call), so the stream is never created and 
{{jetstreamName}} has no effect on a producer. The message is stored by 
whichever stream takes in the subject, whatever its name; with 
{{jetstreamName=NOPE}} no stream {{NOPE}} is created. When no stream takes in 
the subject, each send fails with {{503 No Responders Available For Request}}. 
The option documentation says "If not specified, a default stream will be used 
if JetStream is enabled."

*Fix:* either remove the dead branch and document {{jetstreamName}} as a 
consumer option, or, when {{jetstreamName}} is set, check/create the stream in 
{{doStart()}} as {{NatsConsumer}} does ({{lookupStream}} / {{addStream}}).

h3. 3. Timeout error message is "null"

When {{requestTimeout}} expires first, {{future.get(...)}} throws a 
{{TimeoutException}} without a message, and the exchange fails with:

{noformat}
Failed to publish message asynchronously with JetStream: null
{noformat}

When jnats cancels the future first, it completes with a 
{{CancellationException}}, which is not in the {{catch (InterruptedException | 
ExecutionException | TimeoutException e)}} and is not wrapped at all.

*Fix:* say what happened, e.g. "No acknowledgement from JetStream for subject X 
within N ms", and handle {{CancellationException}} the same way.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to