Skip to content

Refactor replicate object with async await - #2780

Merged
bert-e merged 3 commits into
development/9.5from
improvement/BB-803
Aug 12, 2026
Merged

Refactor replicate object with async await#2780
bert-e merged 3 commits into
development/9.5from
improvement/BB-803

Conversation

@SylvainSenechal

@SylvainSenechal SylvainSenechal commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Issue: BB-803

Review hints :

  • The diff is hard to read on github because indentations changes are marked as a diff, in my code editor, when looking at the diff, the display is different and it is significantly easier to review
  • Reviewable commit by commit

Imo this refactor is super overdue and not so complicated to do.

Refactor replicate object with async await

  • Also updated the async parallel util function into async await
  • Also made minimal mandatory updates to MultipleBackend class, as this class inherits ReplicateObject

@bert-e

bert-e commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Hello sylvainsenechal,

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
/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 TBA
/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.

/**
* Runs an async task for each item in a collection, up to `limit` at a time.
* On error, no new tasks are started, but already-running tasks are awaited
* before the function resolves. Returns [firstError, results] so that callers

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.

Return the first error only : Keeping same behavior as before with mapLimitWaitPendingIfError.

Could've returned all errors as a list but not needed now and prefer to keep existing behavior.
Cannot aggregate errors neither, as we need to keep the original error to check its properties to determine if its retryable or not

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

but already-running tasks are awaited what happens if an error is triggered here ? Can make sense to stop new flows on error, but for the current one we can miss an error ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ok I saw your comment. I'm not sure it's the right approach and we should return all errors ?

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.

Mhh its replicating initial pre migration behavior, I think its fine anyways as replicateObject will try to delete all orphans as soon as a single error is found

Although maybe there is a question raised about reusability of this function, maybe its built to be too custom for replicateObject and should be more generic (return array of errors) ?

Comment thread lib/util/mapLimitWaitPendingIfError.js Outdated

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.

refactored with async await, in the file runTaskWithConcurrency

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

bert-e commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • 2 peers

@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.97786% with 38 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.73%. Comparing base (27f9515) to head (ae7ea53).
⚠️ Report is 3 commits behind head on development/9.5.

Files with missing lines Patch % Lines
extensions/replication/tasks/ReplicateObject.js 88.66% 23 Missing ⚠️
...xtensions/replication/tasks/MultipleBackendTask.js 65.11% 15 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

Files with missing lines Coverage Δ
lib/tasks/BackbeatTask.js 95.83% <100.00%> (+0.71%) ⬆️
lib/util/runTasksWithConcurrency.js 100.00% <100.00%> (ø)
...xtensions/replication/tasks/MultipleBackendTask.js 60.00% <65.11%> (-0.05%) ⬇️
extensions/replication/tasks/ReplicateObject.js 92.68% <88.66%> (+0.24%) ⬆️

... and 3 files with indirect coverage changes

Components Coverage Δ
Bucket Notification 80.27% <ø> (ø)
Core Library 81.82% <100.00%> (+0.02%) ⬆️
Ingestion 70.13% <ø> (ø)
Lifecycle 80.46% <ø> (ø)
Oplog Populator 85.83% <ø> (ø)
Replication 62.01% <84.55%> (-0.01%) ⬇️
Bucket Scanner 85.76% <ø> (ø)
@@               Coverage Diff                @@
##           development/9.5    #2780   +/-   ##
================================================
  Coverage            75.72%   75.73%           
================================================
  Files                  201      201           
  Lines                13937    13937           
================================================
+ Hits                 10554    10555    +1     
+ Misses                3373     3372    -1     
  Partials                10       10           
Flag Coverage Δ
api:retry 9.06% <0.73%> (+0.01%) ⬆️
api:routes 8.82% <0.00%> (ø)
bucket-scanner 85.76% <ø> (ø)
ft_test:queuepopulator 10.95% <0.00%> (+0.26%) ⬆️
ingestion 12.26% <0.00%> (ø)
lib 8.78% <0.00%> (ø)
lifecycle 19.26% <0.73%> (+0.02%) ⬆️
notification 1.01% <0.00%> (ø)
oplogPopulator 0.13% <0.00%> (ø)
replication 18.79% <78.59%> (ø)
unit 54.79% <55.71%> (+0.06%) ⬆️

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.

Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
Comment thread extensions/replication/tasks/MultipleBackendTask.js Outdated
Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
Comment thread extensions/replication/tasks/MultipleBackendTask.js

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.

Migrations here aren't the main point of this pr, but were kinda forced as MultipleBackend extends ReplicateObject.

I did the minimum required migration on this file

@SylvainSenechal
SylvainSenechal requested review from a team, benzekrimaha and delthas July 23, 2026 20:35
@SylvainSenechal
SylvainSenechal marked this pull request as ready for review July 23, 2026 20:35
@SylvainSenechal
SylvainSenechal requested review from maeldonn and removed request for delthas July 23, 2026 20:35

@benzekrimaha benzekrimaha 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.

Good refactor overall, but one real issue with the outcome handler being called from inside the try.

await this._deleteOrphans(destEntry, destLocations, log);
throw err;
}
return this._handleReplicationOutcome(null, sourceEntry, destEntry, kafkaEntry, log, done);

@benzekrimaha benzekrimaha Jul 24, 2026

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.

_handleReplicationOutcome(null, …, done) is called from inside the try: if it throws after calling done, the catch calls it again and done fires twice. We can compute the error in the try/catch and call the handler exactly once after it. (same at line 909 and in _processQueueEntryRetryFull)

@bert-e

bert-e commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • 2 peers

The following reviewers are expecting changes from the author, or must review again:

@SylvainSenechal

Copy link
Copy Markdown
Contributor Author

Sorry I took some time to respond, you can review again next week @benzekrimaha @maeldonn

Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
@maeldonn
maeldonn requested review from a team and DarkIsDude and removed request for maeldonn August 3, 2026 16:38
@SylvainSenechal

Copy link
Copy Markdown
Contributor Author

Please try to create dedicated commit for prettier format as mention in the best practice of the migration 🙏. You can also use "Hide whitespace" on Github to ease diff if you want.

Image CI is red, maybe you should have a look ?

I didnt run the linter though, the diff is just re indentation mostly 🤔
The failure were unrelated, its rerunning but it was fine the other time so should be good here

Comment thread extensions/replication/tasks/ReplicateObject.js
Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
});
throw errors.BadRole;
}
return [this.sourceRole, entryRoles[1]];

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.

entryRoles[1] is undefined for the single-role external-backend case , the value is discarded by _setupClients, but returning a knowingly-undefined slot is confusing; a comment or returning only what's meaningful would help.

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.

Thanks for the reviews, i didnt expect to mess it up so much -_-

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'm gonna remove it, just like it was before as it's not used

Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
Comment thread extensions/replication/tasks/ReplicateObject.js Outdated
actionDesc: 'get bucket replication configuration',
logFields: { entry: entry.getLogInfo() },
actionFunc: done => this._setupRolesOnce(entry, log, done),
actionFunc: done => this._setupRolesOnce(entry, log)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The actionFunc callbacks bridge async methods back to callback-based retry(), but retry() now supports a promise mode (when done is omitted). Since you already added the promise overload in BackbeatTask.retry, these wrappers could use it directly to eliminate the manual .then(r => done(null, r), done) bridging. For example:

async _setupRoles(entry, log) {
    return await this.retry({
        actionDesc: 'get bucket replication configuration',
        logFields: { entry: entry.getLogInfo() },
        actionFunc: async () => this._setupRolesOnce(entry, log),
        shouldRetryFunc: err => err.retryable,
        log,
    });
}

This applies to all four retry wrappers (_setupRoles, _setTargetAccountMd, _getAndPutPart, _putMetadata). Not a blocker — could be a follow-up.

logFields: { entry: entry.getLogInfo() },
actionFunc: done => this._setupRolesOnce(entry, log, done),
actionFunc: done => this._setupRolesOnce(entry, log)
.then(roles => done(null, roles), done),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correction to my previous comment: actionFunc must still accept a callback since retry() internally calls actionFunc(done, nbRetries). The .then(r => done(null, r), done) bridging pattern is necessary with the current retry() implementation. Disregard the suggestion above — the current code is correct.

A cleaner solution would require refactoring retry() to natively accept an async actionFunc, which is out of scope for this PR.

Comment thread lib/tasks/BackbeatTask.js Outdated
return doneOnce(...args);
};
actionFunc(_handleRes, nbRetries);
return actionFunc(_handleRes, nbRetries);

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.

nothing consumes that return value, why change it?

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 added it because of the "consistent-return" linter but yeah better to return undefined

Comment thread lib/tasks/BackbeatTask.js Outdated
Comment thread lib/tasks/BackbeatTask.js Outdated
Comment on lines +110 to +111
actionFunc: done => this._setupRolesOnce(entry, log)
.then(roles => done(null, roles), done),

@benzekrimaha benzekrimaha Aug 10, 2026

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.

all these done => this._xxxOnce(...).then(r => done(null, r), done) bridges keep retry callback-based inside an otherwise async class , fine as a transition, but can we please create a followup ticket to convert retry internals so we don't keep both paradigms. (applies to all similar cases)

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.

yeah and i think i would even create a follow to fully move retry to be async, and to take an async action function because its really annoying to deal with this transition state

Comment on lines +600 to +602
if (err.$metadata?.httpStatusCode === 404) {
return doneOnce(err);
// eslint-disable-next-line no-param-reassign
err.origin = 'source';

@benzekrimaha benzekrimaha Aug 10, 2026

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.

behavior change: the old code rejected errors.ObjNotFound without origin, so _handleReplicationOutcome took the “target object not found, retrying with full data write” path; setting origin = 'source' here makes it take the skip path instead. Skipping looks correct for a source-side 404 mid-stream, but is the change intended? IMO it deserves a test.

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.

yes this was discussed and the change is small and intended, i added 2 tests

Comment on lines +604 to +606
err.ObjNotFound = true;
// eslint-disable-next-line no-param-reassign
err.name = 'ObjNotFound';

@benzekrimaha benzekrimaha Aug 10, 2026

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.

why mutate the SDK error instead of rejecting errors.ObjNotFound like before? this satisfies the err.name === 'ObjNotFound' checks but loses err.is.*, so any future err.is.ObjNotFound check will silently miss it. arsenal errors is the canonical module...

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.

Yeah I think i did this because right after the if statement use similar error rewrite pattern but we can use the arsenal error

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.

Addressed in another similar comment, I messed up, I think wanted to follow similar pattern to the else branch but yeah we can use arsenal error


processQueueEntry(_sourceEntry, kafkaEntry, done) {
this._processQueueEntry(_sourceEntry, kafkaEntry).then(
result => result === null ? done() : done(null, result),

@benzekrimaha benzekrimaha Aug 10, 2026

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.

does the queue processor actually distinguish done() from done(null, undefined)? if not, the null sentinel and this ternary can go away I think

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.

yeah its fine

Comment thread lib/util/runTasksWithConcurrency.js Outdated
* @param {Array} coll - collection to iterate over
* @return {Promise<[Error|null, Array]>} - always resolves, never rejects
*/
async function runTasksWithConcurrency(task, limit, coll) {

@benzekrimaha benzekrimaha Aug 10, 2026

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.

two things:

  • why swap the parameter order? both async.mapLimit and the old mapLimitWaitPendingIfError use (coll, limit, iteratee) ,keeping the conventional order would avoid silent argument swaps.
  • a never-rejecting [err, results] tuple is easy to misuse: a caller who forgets to check [0] silently ignores errors... The partial-results need is real, so OK if kept, but the doc comment should be kept prominent

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.

Yes both legit and addressed

Comment thread lib/util/runTasksWithConcurrency.js Outdated
Comment thread tests/functional/replication/streamedCopy.spec.js Outdated
Comment on lines +248 to +249
.then(() => assert.fail('expected error'))
.catch(err => {

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.

use await assert.rejects(...): as written, when the task unexpectedly succeeds, the assert.fail AssertionError is swallowed by the following .catch and re-asserted, so the test fails with a confusing message instead of “expected error”. (applies to all similar cases)

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.

yeah and many similar patterns actually

Comment thread tests/unit/replication/MultipleBackendTask.js Outdated

@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.

LGTM. Just comments from Maha 🙏

next => this._refreshSourceEntry(sourceEntry, log, (err, res) => {
if (err && err.name === 'ObjNotFound' &&
sourceEntry.getReplicationIsNFS() && !sourceEntry.getIsDeleteMarker()) {
next => this._refreshSourceEntry(sourceEntry, log)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

not really just a bit of habits to have. After 3/4 migrations you'll be an expoert

@bert-e

bert-e commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • 2 peers

The following reviewers are expecting changes from the author, or must review again:

this commit is mandatory as multipleBackend is a class that extends replicateObject and some functions are overloaded

Issue: BB-803
if (err.$metadata?.httpStatusCode === 404) {
return doneOnce(err);
const objNotFound = errors.ObjNotFound;
objNotFound.origin = 'source';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

errors.ObjNotFound is a singleton — const objNotFound = errors.ObjNotFound does not clone it. Setting .origin = 'source' here permanently mutates the shared object for the lifetime of the process, so every future consumer of errors.ObjNotFound (in any code path) will see origin === 'source'.

Suggested change
objNotFound.origin = 'source';
return reject(errors.ObjNotFound);

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.

false, errors.ObjNotFound invokes this getter :
static errors() → { get: () => new ArsenalError(...) })

@bert-e

bert-e commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • 2 peers

@SylvainSenechal

Copy link
Copy Markdown
Contributor Author

/approve

@bert-e

bert-e commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

I have successfully merged the changeset of this pull request
into targetted development branches:

  • ✔️ development/9.5

The following branches have NOT changed:

  • development/7.10
  • development/7.4
  • development/7.70
  • development/8.6
  • development/9.0
  • development/9.1
  • development/9.2
  • development/9.3
  • development/9.4

This pull request did not target the following hotfix branch(es) so they
were left untouched:

  • hotfix/7.4.1
  • hotfix/9.0.7
  • hotfix/9.0.4
  • hotfix/7.10.2
  • hotfix/7.70.15
  • hotfix/7.4.5
  • hotfix/7.4.9
  • hotfix/7.4.7
  • hotfix/7.4.0
  • hotfix/7.9.0
  • hotfix/7.70.1
  • hotfix/7.4.6
  • hotfix/7.2.0
  • hotfix/7.70.12
  • hotfix/7.4.2
  • hotfix/7.4.4
  • hotfix/7.6.0
  • hotfix/7.7.0
  • hotfix/7.8.0
  • hotfix/7.10.1
  • hotfix/7.10.12
  • hotfix/7.4.10
  • hotfix/7.4.8
  • hotfix/7.10.17
  • hotfix/7.10.3
  • hotfix/7.10.4
  • hotfix/7.4.3
  • hotfix/8.2.12
  • hotfix/7.10.0
  • hotfix/7.10.8

Please check the status of the associated issue BB-803.

Goodbye sylvainsenechal.

The following options are set: approve

@bert-e
bert-e merged commit ae7ea53 into development/9.5 Aug 12, 2026
38 of 40 checks passed
@bert-e
bert-e deleted the improvement/BB-803 branch August 12, 2026 15:12
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