Description
The content of each prepared mail is currently serialized by FileSystemMailContentStore in the local permanent directory (<permdir>/mails/<batchId>/<messageId>), while its status is stored by DatabaseMailListener in the database. The content is kept until the mail is sent successfully, so that a failed mail can be resent later (from the mail status administration or by the Mail Resender scheduler job).
Storing that content in a blob store (as provided by XCOMMONS-3250, with the filesystem and S3 implementations) instead of directly on the filesystem would:
- make possible to run XWiki without any persistent file storage (XWIKI-22847)
- make the content of a mail accessible from any cluster member, which is currently not the case: the mail statuses are shared through the database, but the content only exists on the member which prepared the mail
- give a more consistent link between the database and the storage, since the status (database) and the content (blob store) of a mail would both be shared and live in places with the same scope
Current problem in a cluster
DatabaseMailResender loads all the mail statuses matching the filter from the shared database (MailStatus does not indicate which cluster member prepared the mail), and then tries to load each mail content from the local filesystem. For mails prepared by another cluster member this fails with Failed to load message (id [...], batch id [...]) from file [...], logged as a warning Failed to resend mail message for batchId [...], messageId [...].
In practice:
- the Mail Resender scheduler job (Mail.MailResender, enabled by default, resends all mails in prepare_success state every day) can only resend the mails which were prepared by the cluster member it runs on, and logs a warning for all the others; this is true even if the scheduler is enabled on a single cluster member
- resending a mail from the mail status administration fails when the request is handled by a cluster member different from the one which prepared the mail
- the mails prepared by a cluster member that is gone for good can never be resent
Risk of duplicate mails once the content is shared
The prepare_success state does not only cover mails which were prepared but never sent: it's also the state of a mail which is waiting in the send queue of a cluster member, or being sent. DatabaseMailResender does not (and currently cannot) make the difference, it just loads the content and sends the mail again.
Right now, a resend running on a cluster member can only find the content of the mails prepared by that same member, so the mails waiting in the queue of another cluster member just fail to be loaded. Once the content is accessible from all cluster members, the resend would also send those mails, while the member which prepared them is still going to send them too, leading to duplicate mails.
So, as part of this, we need to make sure that each mail is sent by a single cluster member.
Proposed design
- Preparation stays on any cluster member: the member which prepares a mail stores its content in the blob store and its status in the database, as now, and then sends a remote event to inform the cluster leader (NetworkChannel#getLeader()) that the mail is ready to be sent.
- Only the cluster leader sends mails: it is the only member with a send queue, which is fed by those events (and by its own prepared mails).
- The resend is done by the leader too, using Java scheduling instead of the wiki scheduler: the Mail.MailResender scheduler job is replaced by a task executed at a configurable rate (e.g. with a ScheduledExecutorService) and which only does something on the cluster leader. It resends the mails in prepare_success state which are not already in the leader send queue, so it cannot produce duplicate mails.
- Leader change: the resend is also executed when a cluster member becomes the leader (NetworkChannelLeaderChangedEvent), to take care of the mails which were waiting in the queue of the previous leader.
- Lost events: if an event informing the leader about a prepared mail is lost, the mail stays in prepare_success state and is sent by the next resend, so the resend rate should be frequent enough (a few minutes rather than once a day).
- Upgrade: the existing Mail.MailResender scheduler job page needs to be removed (or at least disabled) on upgrade, so that it does not keep running in addition to the new resend.
Open point: mail listeners
The MailSender API returns a MailStatusResult and calls a MailListener on the member which asked to send the mails, and callers may wait for the result (and some listeners, like the memory one, only exist on that member). If the mails are sent by the leader, we need to either send the results back to the original member through remote events, or only send through the leader the mails tracked by the database listener and keep sending the other ones locally.
Attachments
Issue Links
- blocks
-
XWIKI-22847 Make possible to use XWiki without any persistent file storage
-
- Open
-
- relates to
-
XCOMMONS-3250 Make as easy as possible for various features to use an object store
-
- Closed
-
-
XWIKI-25262 Run the scheduler on a single cluster member and move it when that member leaves
-
- Open
-