[ 
https://issues.apache.org/jira/browse/NIFI-16650?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18153320#comment-18153320
 ] 

Vincenzo Lombardo commented on NIFI-16650:
------------------------------------------

I have a working implementation of the proposal and would like feedback on the 
approach before opening a pull request.

Approach:
- Subclass ImapMailReceiver and Pop3MailReceiver (CommittableImapMailReceiver, 
CommittablePop3MailReceiver)
  and add methods to receive messages without changing flags, then apply flags 
to committed messages or
  release them. The Spring receiver is still used for session, store and folder 
handling, and for the
  default IMAP search term.
- AbstractEmailProcessor transfers all messages returned by a receive in the 
same trigger and applies
  \Seen, \Deleted (with expunge on folder close) and the duplicate prevention 
flag in the
  ProcessSession.commitAsync() success callback. On commit failure, or for a 
message that could not be
  written, the folder is closed without changes and the messages are received 
again (at-least-once).
- The in-memory message queue and flushRemainingMessages() in OnStopped are 
removed.
- The duplicate prevention flag applied after commit is the same one Spring 
applies today
  ("spring-integration-mail-adapter", or \Flagged when user flags are not 
supported), so with
  Mark Messages as Read = false messages consumed by previous versions are not 
received again
  after upgrading.
- Added @TriggerSerially, since the folder stays open between receive and 
commit.

Behavior changes to note:
- Delivery changes from at-most-once to at-least-once: if updating flags fails 
after a successful
  commit, the same messages are received again.
- All messages up to Fetch Size are transferred in a single trigger instead of 
one per trigger.

Tests cover marking as read after commit, no duplicate receive without Mark 
Messages as Read, IMAP and
POP3 deletion after commit, and a failed message transfer that is not 
committed. The change is built
on top of NIFI-16649 (PR #11980), so I would rebase it after that is merged.

Does this direction look acceptable?

> ConsumeIMAP marks messages as SEEN or DELETED before FlowFiles are committed
> ----------------------------------------------------------------------------
>
>                 Key: NIFI-16650
>                 URL: https://issues.apache.org/jira/browse/NIFI-16650
>             Project: Apache NiFi
>          Issue Type: Bug
>            Reporter: Vincenzo Lombardo
>            Assignee: Vincenzo Lombardo
>            Priority: Major
>
> ConsumeIMAP relies on Spring Integration ImapMailReceiver.receive(), which 
> applies \Seen (Mark Messages as
> Read) and \Deleted (Delete Messages) to the fetched messages before returning 
> them, and closes the folder
> (expunging when deleting) before AbstractEmailProcessor writes any FlowFile.
> AbstractEmailProcessor then keeps the received messages in an in-memory queue 
> of up to Fetch Size messages
> and transfers only one message per trigger. As a result the processor 
> provides at-most-once delivery:
> - If NiFi stops abnormally, restarts, or the node fails while the queue is 
> not empty, the queued messages
>   are already flagged or deleted on the server but were never transferred.
> - If writing a message to a FlowFile fails, the exception propagates out of 
> onTrigger(), the session rolls
>   back, and the message, already removed from the queue and flagged on the 
> server, is lost.
> With Mark Messages as Read = false, the Spring receiver flags each fetched 
> message with the user flag
> "spring-integration-mail-adapter" (or \Flagged as fallback) when the server 
> does not support RECENT.
> That flag is also applied before commit.
> Proposal for discussion:
> - Disable flag handling in the Spring receiver 
> (shouldMarkMessagesAsRead/shouldDeleteMessages false,
>   autoCloseFolder false) and apply \Seen / \Deleted / expunge in the 
> ProcessSession.commitAsync() success
>   callback, so flags are only set for committed FlowFiles (at-least-once)
> - Transfer all messages returned by a receive() call in the same trigger, 
> removing the in-memory queue
> - Decide how to keep the current Mark Messages as Read = false behavior 
> (avoid re-reading the same messages):
>   apply the same marker flag after commit
> Open question for maintainers: whether at-least-once (possible duplicates 
> after a failed flag update) is
> the preferred semantic over the current at-most-once behavior.



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

Reply via email to