Skip to content

Skip caching a notification configuration that changed during its lookup - #2939

Open
delthas wants to merge 1 commit into
development/9.6from
improvement/BB-939/skip-caching-racing-lookups
Open

delthas wants to merge 1 commit into
development/9.6from
improvement/BB-939/skip-caching-racing-lookups

Conversation

@delthas

@delthas delthas commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Hand written, please read 🙂

The bucket notification handler receives a stream of events and sends notifications according to what the bucket notification configuration says.

So when it receives an event, it must lookup the bucket notification configuration. We have added (and recently improved) a cache for the configurations.

But there is a subtle race when the two happen at once:

  • the configuration changes (and we detect that through a change stream event and drop the entry)
  • we fetch the configuration on a cache miss (and store the result)

When we're unlucky, we cache miss, fetch, during the fetch the value changes from A to B, but we store A in the cache and never update it again. The code that's supposed to delete the cached value when we receive B does it before we store A. Race lost.

This fixes it by having a map of pending configuration lookups by bucket. When a bucket changes while lookups are ongoing, we tell the lookups not to store the value they fetched, because it is stale. It can then be re-fetched later. Race avoided.

Before:

sequenceDiagram
    participant G as getConfig(b)
    participant M as metastore
    participant H as change stream handler
    participant C as cache
    G->>C: get(b): miss
    G->>M: findOne(b), reads config A
    H->>C: b changed to B: b not cached, skipped
    M-->>G: config A
    G->>C: add(b, A), stale until b changes again
Loading

After:

sequenceDiagram
    participant G as getConfig(b)
    participant M as metastore
    participant H as change stream handler
    participant P as pendingLookups
    participant C as cache
    G->>C: get(b): miss
    G->>P: add lookup of b, changed: false
    G->>M: findOne(b), reads config A
    H->>P: b changed to B: flag lookup, changed: true
    M-->>G: config A
    G->>P: remove lookup of b
    Note over G,C: changed: return A, not cached, the next lookup reads B
Loading

Issue: BB-939

@bert-e

bert-e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Hello delthas,

My role is to assist you with the merge of this
pull request. Please type @bert-e help to get information
on this process, or consult the user documentation.

Available options
name description privileged authored
/after_pull_request Wait for the given pull request id to be merged before continuing with the current one.
/bypass_author_approval Bypass the pull request author's approval ⭐
/bypass_build_status Bypass the build and test status ⭐
/bypass_commit_size Bypass the check on the size of the changeset TBA ⭐
/bypass_incompatible_branch Bypass the check on the source branch prefix ⭐
/bypass_jira_check Bypass the Jira issue check ⭐
/bypass_peer_approval Bypass the pull request peers' approval ⭐
/bypass_leader_approval Bypass the pull request leaders' approval ⭐
/bypass_source_branch_lineage Bypass the cross-branch contamination check ⭐
/approve Instruct Bert-E that the author has approved the pull request. ✍️
/create_pull_requests Allow the creation of integration pull requests.
/create_integration_branches Allow the creation of integration branches.
/no_octopus Prevent Wall-E from doing any octopus merge and use multiple consecutive merge instead
/unanimity Change review acceptance criteria from one reviewer at least to all reviewers
/wait Instruct Bert-E not to run until further notice.
Available commands
name description privileged
/help Print Bert-E's manual in the pull request.
/status Print Bert-E's current status in the pull request.
/clear Remove all comments from Bert-E from the history TBA
/retry Re-start a fresh build TBA
/build Re-start a fresh build TBA
/force_reset Delete integration branches & pull requests, and restart merge process from the beginning.
/reset Try to remove integration branches unless there are commits on them which do not appear on the source branch.

Status report is not available.

@scality scality deleted a comment from bert-e Nov 23, 2026
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.76%. Comparing base (83567b4) to head (516acfc).

Additional details and impacted files

Impacted file tree graph

Files with missing lines Coverage Δ
...s/notification/configManager/MongoConfigManager.js 98.34% <100.00%> (+0.18%) ⬆️

... and 5 files with indirect coverage changes

Components Coverage Δ
Bucket Notification 85.75% <100.00%> (+0.21%) ⬆️
Core Library 87.59% <ø> (-0.01%) ⬇️
Ingestion 83.63% <ø> (-0.03%) ⬇️
Lifecycle 86.43% <ø> (ø)
Oplog Populator 94.73% <ø> (ø)
Replication 68.34% <ø> (-0.03%) ⬇️
Bucket Scanner 89.37% <ø> (ø)
@@               Coverage Diff                @@
##           development/9.6    #2939   +/-   ##
================================================
  Coverage            81.76%   81.76%           
================================================
  Files                  217      217           
  Lines                46363    46400   +37     
================================================
+ Hits                 37907    37940   +33     
- Misses                8456     8460    +4     
Flag Coverage Δ
api:retry 14.08% <0.00%> (-0.02%) ⬇️
api:routes 14.00% <0.00%> (-0.02%) ⬇️
bucket-scanner 89.37% <ø> (ø)
ft_test:queuepopulator 18.81% <11.36%> (+0.19%) ⬆️
ingestion 19.83% <11.36%> (-0.01%) ⬇️
lib 12.60% <0.00%> (-0.03%) ⬇️
lifecycle 26.16% <0.00%> (-0.04%) ⬇️
notification 1.87% <81.81%> (+0.07%) ⬆️
oplogPopulator 0.57% <0.00%> (-0.01%) ⬇️
replication 24.52% <0.00%> (-0.02%) ⬇️
unit 68.21% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bert-e

bert-e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Request integration branches

Waiting for integration branch creation to be requested by the user.

To request integration branches, please comment on this pull request with the following command:

/create_integration_branches

Alternatively, the /approve and /create_pull_requests commands will automatically
create the integration branches.

@delthas
delthas force-pushed the improvement/BB-939/skip-caching-racing-lookups branch from dc70f4c to 5970a46 Compare November 23, 2026 10:02
@delthas
delthas marked this pull request as ready for review November 23, 2026 10:07
@delthas
delthas requested review from a team, DarkIsDude and benzekrimaha November 23, 2026 10:07
@delthas
delthas force-pushed the improvement/BB-939/skip-caching-racing-lookups branch 2 times, most recently from 9436bd2 to 1110f61 Compare November 24, 2026 09:32
@delthas
delthas requested a review from benzekrimaha November 24, 2026 09:34

@DarkIsDude DarkIsDude left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We are missing a use case I guess ? With secondary (that we can have in an Artesca config?).

Still broken (lagging secondary)

  1. Bucket b changes from A to B on the primary.
  2. The change stream handler gets the event, sees b isn't cached, and does nothing. No lookup is pending, so there's nothing to flag.
  3. getConfig(b) misses the cache and adds L1 to pending[b].
  4. findOne(b) goes to a secondary that hasn't replicated the change yet and returns A.
  5. L1.changed is false, so getConfig(b) caches A. ✗

Now I have a weird feeling with this PR. IMO we have a more deeper problem of responsability and we are trying to put a patch when we should think bigger ?

The change stream handler only refreshes cached buckets. When a bucket's
configuration changed while its lookup was in flight, the handler skipped
it, and the lookup then cached what it read before the change: a stale
configuration, or since BB-908 a stale absence of one, kept until the
bucket changes again or is evicted.

Track the lookups in flight per bucket, have the handler flag those of
the bucket that changed, and return their result without caching it, so
that the next entry reads it again.

Also read the configuration from the primary, whatever the configured
read preference: the change stream reports majority-committed changes,
so a lookup started after a change was handled could otherwise still
read the previous configuration from a lagging secondary, and cache it.

Issue: BB-939
@delthas
delthas force-pushed the improvement/BB-939/skip-caching-racing-lookups branch from 1110f61 to 516acfc Compare November 25, 2026 15:31
@delthas

delthas commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

We are missing a use case I guess ? With secondary (that we can have in an Artesca config?).

We technically can have a read preference for secondary set up in Zenko but ARTESCA never sets any.

Still, this can indeed cause the issue you listed, so I'm adding an explicit "primary" read preference to the findOne call. It's cheap anyway.

@delthas

delthas commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Now I have a weird feeling with this PR. IMO we have a more deeper problem of responsability and we are trying to put a patch when we should think bigger ?

I see what you mean with 1 job filling the cache and 1 job emptying it.

But we can't just have the changestream populate the cache, since we're trying to keep it LRU (least recently used, others are dropped) with a cap.

The easy and "clean" solution would be to create a whole cache of all the configurations of all the buckets but this wouldn't work for a large XDM type install.

So I'd say take the fix and no bigger change for now.

@delthas

delthas commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Also adding @francoisferrand for an additional eye on this

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants