allthingssecurity opened a new pull request, #30699:
URL: https://github.com/apache/camel/pull/30699

   # Description
   
   [CAMEL-27358](https://issues.apache.org/jira/browse/CAMEL-27358)
   
   When the server deleted the ephemeral consumer of a JetStream push 
subscription, `NatsJetStreamPushConsumerKeeper` subscribes again from the first 
delivered message that is not settled, or after the highest one delivered 
(CAMEL-25845). Two cases made it start too late, so messages were skipped 
without an error:
   
   - The stream was created again (for example a memory stream after a server 
restart). The keeper cleared what it tracked, but jnats keeps the queue of its 
dispatcher across a reconnect and keeps dispatching the messages of a 
subscription until it is unsubscribed (`NatsConnection.reconnectImpl`, 
`NatsDispatcher.run`). An old-stream message dispatched after the clear (while 
the keeper worked out the start, or while it waited for the server to create 
the new consumer) set `highestDelivered` back to an old-stream sequence and was 
tracked as unsettled. The new consumer then started at that sequence or, more 
often, the next consumer loss (an ordinary disconnect of more than 5 s) resumed 
after it, and every new-stream message below it was skipped. On a busy stream 
that can be a large range. `ackPolicy=None` was affected the same way.
   - An unsettled message was forgotten too early. Right after the new 
subscribe returned, the keeper dropped every tracked message of the old 
subscription ("the new consumer delivers those again") without waiting for that 
delivery. When the new consumer was lost too before its copy reached the route 
(a second disconnect; its queued copy is dropped with its subscription), and 
the first delivery then failed (its nak goes to the deleted consumer), the next 
resubscribe started after the message.
   
   Changes (`NatsJetStreamPushConsumerKeeper` only):
   
   - The dispatcher thread tracks a delivered message under a small lock 
(`trackingLock`), and the resubscribe clears the tracking under the same lock 
when the stream was created again. It also remembers the subscriptions made on 
the old stream (the current and the previous one), whose messages are no longer 
tracked: a message is either cleared or not tracked. The resubscribe reads the 
tracked messages and `highestDelivered` together under the lock. The lock is 
never held across a call to the server.
   - A message of the previous subscription that is not settled stays tracked 
until it is settled or the new consumer delivers it again (the entry is 
replaced by sequence, as before). The new consumer starts at or below every 
such entry and delivers each message for the first time in stream order (a 
redelivery is of a message it delivered before, so its lower sequence only 
drops less). So an entry of an earlier subscription below a sequence that the 
current consumer delivered is dropped: the message is no longer in the stream 
(a limit of the stream removed it), and it must not hold the resume back for 
the life of the route.
   - A message that the new consumer delivers before `subscribe` returns (jnats 
makes the subscription before it creates the consumer, 
`NatsJetStream.createSubscription`) is tracked as before.
   
   What users can notice: after a server restart that created a memory stream 
again, and a later consumer loss, the push consumer no longer skips messages of 
the new stream. No option changed.
   
   Found with a TLA+ model of the keeper (jnats dispatcher queue that survives 
reconnects, the route, the RESUBSCRIBED handler, the keeper's check and 
resubscribe, the stop, and a server that pushes in order, redelivers naks, 
deletes the ephemeral consumer and can create the stream again). I adapted it 
to the fix. On main it finds both skips (with 1 disconnect and 1 re-creation, 
and with 2 disconnects). With the fix nothing is skipped and the resume never 
starts after an unsettled message: 2 messages, 1 disconnect, 1 re-creation, 1 
nak (244,891 states); 2 disconnects (2,823,370); 2 disconnects and 1 
re-creation (8,895,329). TLC gives the same counts on the first two. A first 
version that remembered only the subscription current at the re-creation failed 
the last scope: a message of the subscription before it, taken from the queue 
before the first resubscribe and handed over after the re-creation was found.
   
   Tests: new `NatsJetStreamPushConsumerKeeperResumeTest` (6 tests). It drives 
the keeper with fakes of the jnats interfaces it calls, which answer as jnats 
2.26.4 does (I read its sources): the dispatcher hands a queued message over 
only while its subscription is active and does not clear its queue on a 
reconnect; `subscribe` makes the subscription before it creates the consumer, 
so the new consumer can deliver before the call returns; `getConsumerInfo` 
answers `null` for a deleted consumer (api error 10014); `lastAck` is set after 
the ack is published, and a terminal ack is never replaced 
(`NatsJetStreamMessage.ackReply`); the metadata come from the reply subject. 
The test thread plays the dispatcher thread, the route and the server.
   
   - an old-stream message dispatched while the keeper subscribes on the 
re-created stream: the next consumer loss must resume after the new-stream 
messages 1 and 2 (main: start 6 with 5 messages in the stream);
   - an old-stream message dispatched on another thread while the keeper works 
out the start: the new consumer must use the consumer configuration (main: 
start 5);
   - an old-stream message taken from the queue before an earlier resubscribe 
and handed over after the re-creation was found: the consumer configuration 
again (main: start 3);
   - an unsettled message whose copy from the new consumer is still queued when 
the new consumer is lost too, and whose processing then fails: the next 
resubscribe must start at it (main: 3 instead of 2);
   - controls (pass on main): a message the new consumer delivers before 
`subscribe` returns is tracked; an unsettled message that is no longer in the 
stream does not hold the resume back.
   
   On main the 4 tests above fail with the starts given there; the 2 controls 
pass. With the change the camel-nats unit tests pass: 1,055 run, 0 failures 
(1,000 of them the DST seeds of `NatsConsumerDstTest`). 
`NatsJetStreamPushConsumerKeeperResumeTest` and 
`NatsConsumerPushConsumerDeletedTest` 20 times in one JVM: 380 run, 0 failures.
   
   Not changed:
   
   - If the stream is created again between the keeper's stream lookup and its 
subscribe (the server restarts within that window), the start is worked out on 
the old stream and the consumer is created on the new one. The model shows it; 
the keeper makes no call to the server in between, and jnats has no way to tie 
the subscribe to the stream it looked up.
   - The pull consumer (`NatsConsumer.NatsConsumingTask.subscribeAgain`) has 
the same patterns (an `unacknowledged.clear()` right after the subscribe, and 
the re-creation reset while an async route still processes old-stream 
messages). It is single-threaded for a synchronous route, so it needs an async 
route or manual ack to be exposed; there an acknowledgement of an old-stream 
message also removes the new-stream sequence with the same number. I will file 
a follow-up JIRA for it and link it from this one.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27358](https://issues.apache.org/jira/browse/CAMEL-27358).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built and tested camel-nats, including the formatter and import-sort 
plugins. No option and no doc changed. The integration tests need a container 
and were not run locally (CI runs them). I did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to