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

   # Description
   
   [CAMEL-27529](https://issues.apache.org/jira/browse/CAMEL-27529)
   
   `idempotentRepository` is documented as the way to cluster consuming from 
the same mailbox, and by default (`idempotentRepositoryRemoveOnCommit=true`) 
the key of a mail is removed once the mail is marked SEEN or deleted, as the 
mark prevents it from being consumed again. But consumer B can search the 
unseen mails before consumer A marks the mail, and add the key after A removed 
it: the add succeeds and B routes the mail a second time. Only the DELETED flag 
is checked before routing, so with the default `delete=false` nothing stops it. 
A single consumer whose exchanges complete after the poll (`pollEnrich`, 
`ConsumerTemplate`) has the same window between its next search and the 
completion of an earlier exchange.
   
   ## The fix
   
   After the key of a mail was added, the consumer asks the server whether the 
mail is SEEN now, and skips it if so: another consumer consumed it after this 
poll searched the unseen mails. The key it added is removed again (otherwise it 
would stay in the repository for good, as the mail is not found by the unseen 
search any more), and the skip is logged at DEBUG, like a duplicate.
   
   The question goes to the server as a `SEARCH` restricted to that one mail: 
`folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), true), new Message[] { 
mail })`. The flags of the mail object cannot be used for this: 
`IMAPMessage.getFlags()` fetches them once per mail object and then returns the 
loaded ones, and with `closeFolder=false` the folder stays open, so the search 
of the next poll returns the mail objects of the earlier poll. When the 
consumer routed the mail before and the exchange failed (or the mail was given 
back as not routed), its flags were loaded then (not SEEN), and they stay so 
even after another consumer marked the mail. I checked in angus-mail 2.0.5 
(javap) that `IMAPFolder.search(SearchTerm, Message[])` sends one `SEARCH SEEN 
<message number>` command through the folder's protocol connection; a GreenMail 
trace of the new test shows exactly one `SEARCH SEEN 1` per mail whose key was 
added, answered with `* SEARCH 1` and the current flags. It only falls back t
 o matching on the client (cached flags) when the server rejects the command 
(`NO`), which leaves such a server where it is today.
   
   Why a check and not only documentation: it fixes the documented use with the 
default options. The cost is one round trip for each mail whose key the 
consumer added, that is each mail it is about to route; duplicates and mails 
skipped before the add cost nothing.
   
   The check only applies where SEEN means "consumed by another consumer":
   - IMAP only. POP3 has no SEEN flag on the server, and its mails are found by 
every poll anyway (only the repository keeps them apart);
   - the unseen search of the consumer (`unseen=true`, the default, and no 
custom `searchTerm`): with `unseen=false` or a custom search term, a SEEN mail 
can be one the consumer is meant to consume;
   - `peek=true` (the default), so the consumer cannot have flagged the mail 
SEEN by reading it. `setPeek` is now applied before the key is generated (it 
was applied right after the add), so a custom `mailUidGenerator` that reads the 
body does not flag the mail SEEN either.
   
   For the other cases the component page gets a short section "Consuming one 
mailbox with several consumers": set 
`idempotentRepositoryRemoveOnCommit=false`, so the key of a consumed mail stays 
in the repository (the model of the audit passes with keys kept). The catalog 
copy of the page is updated (identical).
   
   Not changed here:
   - a mail that a person reads (SEEN) in a mail client between the search and 
the add is now skipped too, which is what `unseen=true` asks for;
   - the DELETED check of `createExchange` still reads the flags of the mail 
object (cached with `closeFolder=false`), as before;
   - the DELETED skip keeps the key it added, as before: a DELETED mail is 
found by the unseen search until it is expunged, and the key spares a flags 
read on each poll;
   - the commit side (marking the wrong mail, the NPE with `disconnect=true`): 
separate PR with its own JIRA; both change `MailConsumer` in different places 
and merge without conflict;
   - no upgrade guide entry: the change only drops the second routing of a mail 
that another consumer already routed.
   
   ## How it was found
   
   A TLA+ model of the search, the idempotent add and the commit with two 
consumers sharing the repository: "a mail that is SEEN is never routed" fails 
with the default `idempotentRepositoryRemoveOnCommit=true`, and passes with 
keys kept. Reproduced against the real consumer and GreenMail. The switch for 
this check was added to the model but not run yet.
   
   ## Tests
   
   New test classes (package-private, JUnit assertions as in the module, 
latches and Awaitility, no sleeps):
   
   - `MailSharedIdempotentRepositoryTest`: two routes on one GreenMail mailbox 
share a `MemoryIdempotentRepository`. Route B's `mailUidGenerator` (called 
between the search and the add) waits until route A routed the mail, marked it 
SEEN and removed its key; a `postProcessAction` on B tells when B's poll ended. 
Only A routes the mail, and B removed the key it added again.
   - `MailSharedIdempotentRepositoryCachedFlagsTest` (2): consumer B routes the 
mail and the exchange fails (key removed, mail unseen, B's mail object has 
loaded the flags). In B's next poll, A consumes the mail between B's search and 
B's add (A runs inside B's `mailUidGenerator`, no threads). B does not route 
the mail again, with `closeFolder=false` (the cached-flags case) and with the 
default `closeFolder=true`.
   
   On main both classes fail 3 runs out of 3 (B routes the mail again). 
Checking the cached flags instead (`mail.getFlags()`) fails the 
`closeFolder=false` test 3/3 and passes the other. With the fix: 20 runs in one 
JVM, 60/60; camel-mail 254 tests, 0 failures (3 skipped as before). Negative 
control: without removing the key again, `MailSharedIdempotentRepositoryTest` 
fails 3/3 on the key assertion. Merged with the companion PR: the new tests of 
both and the related idempotent and reopen tests, 3 runs, 93/93.
   
   _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