Skip to content

Introduce BlobMailRepositoryV2 (skeleton) - #3213

Draft
prosgarz35 wants to merge 8 commits into
apache:masterfrom
KPS-T:lfb
Draft

prosgarz35 wants to merge 8 commits into
apache:masterfrom
KPS-T:lfb

Conversation

@prosgarz35

@prosgarz35 prosgarz35 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Context

This PR supersedes the earlier repository-isolation approach on this branch.
Per review discussion (@chibenwa), instead of patching BlobMailRepository
in place — which raised retro-compatibility and blob-id-format concerns — we
introduce a new, self-contained BlobMailRepositoryV2 behind a dedicated
blobv2:// protocol. This keeps the change small and reviewable, and leaves
BlobMailRepository (V1) completely untouched.

This is intentionally a skeleton: it establishes the V2 type, its own
storage format, factory, GC integration and Guice wiring. Follow-up concerns
(deprecating V1, @Disabled contract adjustments, upgrade/migration
instructions) are deliberately left out and will be handled in separate PRs,
as agreed.

What this PR does

  • BlobMailRepositoryV2 — a MailRepository backed by the blob store,
    fully independent from V1. Unlike V1, store does not read-then-delete a
    possibly-superseded MIME blob on overwrite; it always writes a fresh
    mail + metadata pair. Orphaned MIME blobs from superseded writes are left to
    the blob-store garbage collector.
  • MailMetadataV2 — V2's own on-wire metadata schema (with its own
    HeaderV2). It carries a leading version discriminator
    (CURRENT_VERSION = 2) stamped on every write, so the format can evolve
    freely without any retro-compatibility constraint against V1.
  • MailRepositoryBlobIdFactoryV2 — V2's own url-prefixed blob-id factory.
  • BlobMailRepositoryV2Factory — mirrors BlobMailRepositoryFactory,
    wired to the blobv2:// protocol.
  • BlobMailRepositoryV2BlobReferenceSource — reports the MIME parts
    referenced by every BlobMailRepositoryV2 instance to the blob-store GC, so
    live MIME blobs are not reclaimed as orphans. Registered via a
    Multibinder<BlobReferenceSource>.
  • BlobstoreMailRepositoryModule — registers the blobv2:// protocol
    (@ProvidesIntoSet factory) and the reference source.

Design notes

  • Full independence from V1. V2 shares no type, codec, blob-type or schema
    with BlobMailRepository. Dropping V1 in a later release is a pure file
    deletion with zero impact on V2.
  • Per-repository isolation. Listing is scoped to the repository's path
    prefix via listBlobs(bucket, prefix) (with a trailing /), so operations
    on one repository never touch blobs of another repository sharing the same
    bucket.
  • Metadata blob type is mailMetadataV2 (distinct from V1's
    mailMetadata).

Testing

  • BlobMailRepositoryV2Test exercises the shared MailRepositoryContract
    (same coverage as BlobMailRepositoryTest).
  • Verified locally with mvn test-compile checkstyle:check (0 Checkstyle
    violations) and a full reactor build up to the Guice mailrepository module.

Out of scope (follow-up PRs)

  • Deprecating BlobMailRepository / contract @Disabled adjustments.
  • Upgrade & migration instructions (blob:// → blobv2:// via webadmin).
  • V2-specific hardening (ownership validation, resilient decoding).
}

@Test
void removeAllShouldBeIsolatedAcrossRepositories() throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests are generic and should be moved to the contract IMO

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test
var/mail/error do not list var/mail/error-backup content ?

@chibenwa chibenwa left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the dedup/GC module, asString() becomes family_generation_ (or family/generation/ for MinIO). The ownership check would then reject every mail, and those mails would become invisible. It's not a regression: the old belongsToMailRepository used the same startsWith and was already broken there, and the new listBlobs(bucket, prefix) behaves the same way

I'd favour finding something agnostic to the blobId format. It's an important architectural property that let's us build cool feature like #3193

Performance. size and list now read and decode every metadata blob (N GETs), where before they only listed ids. That doesn't break anything, but it's noticeably heavier on big error repositories.

I do not know if this is structurally needed ?

@chibenwa

Copy link
Copy Markdown
Contributor

Can we also please create a JIRA for the underlying bug please ?

@chibenwa chibenwa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also storage format changed.

I'd be cautious with retro compatibility and explicitly test this: write some blobs by hand according to how they were previously stored and ensure that repository content is still redable with new code.

if we can avoid a breaking change...

@prosgarz35

Copy link
Copy Markdown
Contributor Author

Also storage format changed.

I'd be cautious with retro compatibility and explicitly test this: write some blobs by hand according to how they were previously stored and ensure that repository content is still redable with new code.

if we can avoid a breaking change...

we can avoid it for now but format needed to change in future i cant remember correctly but there problem with parsing json or something so no way in current format fully fix this problems

@chibenwa

Copy link
Copy Markdown
Contributor

You don't get it - if we break retro compatibility, we need to come up with a migration trategy which I'd like to avoid.
I would prefer to maintain retro compatibility.

Also @jeantil you might want to review this...

@chibenwa
chibenwa requested a review from jeantil September 28, 2026 15:24
@prosgarz35

Copy link
Copy Markdown
Contributor Author

You don't get it - if we break retro compatibility, we need to come up with a migration trategy which I'd like to avoid. I would prefer to maintain retro compatibility.

Also @jeantil you might want to review this...

yeah i get it for now we must have backward fully compability

@prosgarz35

Copy link
Copy Markdown
Contributor Author

Can we also please create a JIRA for the underlying bug please ?

tomorrow :) 22.30 now

@prosgarz35
prosgarz35 force-pushed the lfb branch 2 times, most recently from ce65ce5 to d7cd227 Compare September 29, 2026 02:12
Comment on lines +183 to +190
oldMimeId.foreach(mimeId =>
SMono.fromPublisher(mimeMessageStore.delete(mimeId))
.retryWhen(Retry.backoff(2, Duration.ofMillis(50)))
.doOnError(e => LOGGER.warn("Failed to delete superseded MIME blobs {} and {} for mail {} in {}",
mimeId.getHeaderBlobId.asString(), mimeId.getBodyBlobId.asString(), mc.getName, url.asString(), e))
.onErrorResume(_ => SMono.empty)
.block()
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have IMO a plan for dropping those exta reads.

My proposition is to:

  • Leave BlobMailRepository -> untouched but marked as deprecated and to be removed after 3.10.0 release
  • Have a BlobMailRepositoryV2 which do NOT handle retrocompatibility and adopt whatever format is most suited
  • Write a upgrade instruction saying that BlobMailRepository will be dropped next release. That users can configure both BlobMailRepository and BlobMailRepositoryV2 playing mailrepositorystore.xml config and move messages via webadmin. They shall do that prior 3.11.0.

Exact steps would be:

  • A. add blobv2:// backed by BlobMailRepositoryV2 in mailrepositorystore.xml
  • A. and Update mailet container to use blobv2://
  • A. and reboot james
  • B. move things from blob:// to blobv2:// cf webadmin API
  • C: Set blob:// to be backed by BlobMailRepositoryV2
  • C: and Update mailet container to use blob://
  • C: and remove blobv2:// section
  • C: and reboot

CF https://james.staged.apache.org/james-project/3.10.0/servers/distributed/operate/webadmin.html#_moving_mails_from_a_mail_repository_to_another

I believe it provides a clear discontinuation plan with limited compexity on our hand and limits retro compatibility concerns: our hands are free to make optimal choices.

Opinion ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I surely like idea, but not sure i can make so complicated task, i try it ! So really that needed? We have something already like v2 ?

https://james.staged.apache.org/james-project/3.10.0/servers/distributed/operate/webadmin.html#_moving_mails_from_a_mail_repository_to_another its good feature!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cut it in small parts:

  • 1 PR for contract changes, @disabled for BlobMailRepository
  • 1 PR for BlobMailRepositoryV2 alone
  • 1 PR for deprecation and upgrade procedure.

Smaller chunks makes things generally more manageable.

@prosgarz35

Copy link
Copy Markdown
Contributor Author

this PR going be V2 blueprint

@prosgarz35

Copy link
Copy Markdown
Contributor Author

What you think about it Dear @chibenwa ?

@prosgarz35 prosgarz35 changed the title BlobMailRepository: Isolate removeAll across mail repositories to prevent catastrophic data loss and cross-repository deletion Sep 30, 2026
Comment on lines +82 to +86
@Test
@Disabled("This repository removes blobs rather than mails: it can not tell which of them back a mail")
@Override
public void removeAllShouldReportTheRemovedMails() {
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a pity can't we manage to solve this one in v2

@chibenwa chibenwa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BlobMailRepositoryV2BlobReferenceSource must also list metadata blobs


@BeforeEach
void setup() {
blobIdFactory = new PlainBlobId.Factory();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test with GenerationAwareBlobId.Factory

Comment on lines +68 to +71
.onErrorResume(e => {
LOGGER.warn("Failed listing referenced MIME parts for a BlobMailRepositoryV2 instance", e)
Flux.empty()
}))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.onErrorResume(e => {
LOGGER.warn("Failed listing referenced MIME parts for a BlobMailRepositoryV2 instance", e)
Flux.empty()
}))

Swallowing failures result in data loss. We MUST propagate.

@prosgarz35

Copy link
Copy Markdown
Contributor Author

@quantranhong1999 can i ask you do this too? Thanks :3

@quantranhong1999

Copy link
Copy Markdown
Member

@quantranhong1999 can i ask you do this too? Thanks :3

done ^^

@chibenwa

chibenwa commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[6]

En échec depuis 3 builds (Depuis En échec#14 )
A duré 0.16 s.
Message d'erreur
Expecting actual:
[]
to contain exactly in any order:
[MailKey{value=1_628_var/mail/error/mailMetadata/mail18},
MailKey{value=1_628_var/mail/error/mailMetadata/mail4},
MailKey{value=1_628_var/mail/error/mailMetadata/mail10}]
but could not find the following elements:
[MailKey{value=1_628_var/mail/error/mailMetadata/mail18},
MailKey{value=1_628_var/mail/error/mailMetadata/mail4},
MailKey{value=1_628_var/mail/error/mailMetadata/mail10}]

Résultats des tests (26 échecs / ±0)
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[6]
org.apache.james.mailrepository.blob.BlobMailRepositoryV2BlobReferenceSourceTest.listReferencedBlobsShouldPropagateErrorsWhenStoreFails
org.apache.james.mailrepository.blob.BlobMailRepositoryV2Test.removeAllShouldReportTheRemovedMails
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.listShouldReturnStoredMailsKeys
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.mailRepositoriesShouldBeURLIsolated
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.removeAllShouldReportTheRemovedMails
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.removeShouldHaveNoEffectOnSizeWhenUnknownKeys
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.removeShouldnotAffectUnrelatedMails
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.removedMailShouldNotBeListed
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.removedMailsShouldNotBeListed
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.retrieveShouldReturnNullAfterRemoveAll
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.sizeShouldBeIncrementedByOneWhenDuplicates
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.sizeShouldReturnMailCount
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storeShouldHaveNoEffectOnSizeWhenAlreadyStoredMail
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[10]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[1]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[2]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[3]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[4]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[5]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[7]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[8]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingAndRemovingMessagesConcurrentlyShouldLeadToConsistentResult()[9]
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingMessageWithPerRecipientHeadersShouldAllowMultipleHeadersPerUser
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingMessageWithSameKeyTwiceShouldUpdateMessageAttributes
org.apache.james.mailrepository.blob.GenerationAwareBlobMailRepositoryV2Test.storingMessageWithSameKeyTwiceShouldUpdateMessageContent

Related I presume...

@chibenwa chibenwa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for iterating. CI is green now.

A few points before this can serve as the V2 blueprint:

  1. The description says listing uses listBlobs(bucket, prefix), but the code still lists the whole default bucket and filters in memory. size/list/removeAll/GC each trigger a full bucket scan (shared with all mailbox blobs), once per repository for the GC. Can we at least use prefixes ?
  2. contains(path + "/") breaks isolation: mails of foo/var/mail/error are listed, counted and removed by removeAll in var/mail/error. It also still depends on the blob id format. Please add isolation tests (error vs error-backup, nested/suffix paths).
  3. With GenerationAwareBlobId, re-storing the same key after a generation change creates a second metadata blob, leading to duplicate listing and partial removal. Please test with a ticking clock.
  4. Leaving superseded MIME blobs to the GC only works with deduplication. The factory builds passthrough stores, where overwrites and failed removes leak blobs forever.
  5. retrieve swallows errors (onErrorReturn(None)) and hides mails. Please propagate.
  6. version is never checked on read, and attributes are still JSON-in-JSON. If the format is free, let's fix that now.
  7. Can removeAllShouldReportTheRemovedMails be enabled for V2?

Maybe we can:

  • Use a dedicated bucket
  • Rely on S3BlobStoreDAO and forget about MimeMessageStore

Would make this:

  • Independant from the GC
  • Allow to be duplicating
@chibenwa

Copy link
Copy Markdown
Contributor

Hello @prosgarz35

Can I ask you for the state of this work?

Do you plan to further work on this topic?

Best regards,

Benoit

@prosgarz35

Copy link
Copy Markdown
Contributor Author

Hello @prosgarz35

Can I ask you for the state of this work?

Do you plan to further work on this topic?

Best regards,

Benoit

I have to admit, hearing my feedback framed as 'special needs' caught me a bit off guard and felt a bit discouraging. But looking past that, maybe it's worth following his suggestion, even if my only 'special need' was just wanting more than a single option for a non-distributed system.

@chibenwa

Copy link
Copy Markdown
Contributor

Hello @prosgarz35

It took me a while to understand the context.

I'm pretty sure "special needs" was meant as "specific requirements": we are all writing in a non-native language here, and that expression carries a connotation in English its author very likely did not intend - nor did I before I checked the dictionnary. Suggesting a fork is a common path in open source to prove a backend can be maintained over time, not a dismissal of your use case.

That discussion was about adding new storage backends upstream.
It does not change anything for this PR, which is reviewed on its own merits.

Best regards,

Benoit

@prosgarz35

Copy link
Copy Markdown
Contributor Author

Hello @prosgarz35

It took me a while to understand the context.

I'm pretty sure "special needs" was meant as "specific requirements": we are all writing in a non-native language here, and that expression carries a connotation in English its author very likely did not intend - nor did I before I checked the dictionnary. Suggesting a fork is a common path in open source to prove a backend can be maintained over time, not a dismissal of your use case.

That discussion was about adding new storage backends upstream. It does not change anything for this PR, which is reviewed on its own merits.

Best regards,

Benoit

Thank you, Benoit, for stepping in and clarifying the context. I’d certainly like to believe he meant no harm with the original comment, and I'm glad to move forward from this.
On a separate note, I have recently come down with a severe case of strep throat and am currently barely able to stand. Because of this, I need to take some time to recover, but I look forward to focusing on the technical side of the contribution as soon as I'm back on my feet. Warm regards!

@chibenwa

Copy link
Copy Markdown
Contributor

Thanks for sharing.

Health first. Take time needed for recovery, if needed we would take care of the follow ups and do not hesitate to ask for help.

Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants