Skip to content

fix putData : detect collision on specific provided version id - #6230

Open
SylvainSenechal wants to merge 1 commit into
development/9.4from
improvement/CLDSRV-953
Open

fix putData : detect collision on specific provided version id#6230
SylvainSenechal wants to merge 1 commit into
development/9.4from
improvement/CLDSRV-953

Conversation

@SylvainSenechal

Copy link
Copy Markdown
Contributor

Issue: CLDSRV-953

The putData collision detection previously fetched the master object and compared its versionId against the incoming x-scal-version-id header. This caused issues when replicating a non-current version : the master's versionId wouldn't match, so no collision was detected and data was re-uploaded unnecessarily

Fix : decode the x-scal-version-id header in the router and use it as the metadata lookup key, so objMd is always the specific version being replicated

@bert-e

bert-e commented Jul 20, 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.

errorInstances.BadRequest.customizeDescription('bad request: invalid x-scal-version-id header'),
);
}
if (objMd && objMd.versionId === incomingVersionIdDecoded) {

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.

No need for objMd.versionId === incomingVersionIdDecoded anymore as the middleware is already fetching objMd for the specific versionID provided in the header

Comment thread lib/routes/routeBackbeat.js Outdated
@bert-e

bert-e commented Jul 20, 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 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.40%. Comparing base (d52ba09) to head (1b7b0e2).
⚠️ Report is 1 commits behind head on development/9.4.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
lib/routes/routeBackbeat.js 87.50% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

Files with missing lines Coverage Δ
lib/api/apiUtils/object/versioning.js 97.14% <100.00%> (+0.01%) ⬆️
lib/routes/routeBackbeat.js 78.83% <87.50%> (+0.14%) ⬆️
@@                 Coverage Diff                 @@
##           development/9.4    #6230      +/-   ##
===================================================
+ Coverage            86.39%   86.40%   +0.01%     
===================================================
  Files                  212      212              
  Lines                14577    14574       -3     
===================================================
- Hits                 12594    12593       -1     
+ Misses                1983     1981       -2     
Flag Coverage Δ
checksums-disabled-tests 35.33% <20.00%> (+0.01%) ⬆️
file-ft-tests 69.98% <90.00%> (-0.05%) ⬇️
file-ft-tests-null-compat 70.51% <90.00%> (+0.05%) ⬆️
kmip-ft-tests 28.08% <20.00%> (+0.01%) ⬆️
mongo-v0-ft-tests 71.10% <90.00%> (+0.02%) ⬆️
mongo-v1-ft-tests 71.07% <90.00%> (+<0.01%) ⬆️
multiple-backend 36.10% <60.00%> (+0.02%) ⬆️
s3c-ft-tests-v0 65.06% <90.00%> (-0.01%) ⬇️
s3c-ft-tests-v0-null-compat 65.11% <90.00%> (-0.01%) ⬇️
s3c-ft-tests-v1 65.05% <90.00%> (+0.02%) ⬆️
sur-tests 36.60% <30.00%> (-0.02%) ⬇️
sur-tests-inflights 39.42% <30.00%> (+0.01%) ⬆️
unit 73.96% <90.00%> (+<0.01%) ⬆️
utapi-v2-tests 35.30% <20.00%> (+0.01%) ⬆️

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

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@scality scality deleted a comment from bert-e Jul 20, 2026
@SylvainSenechal
SylvainSenechal force-pushed the improvement/CLDSRV-953 branch from 148ca98 to 18d305d Compare July 20, 2026 16:26
@SylvainSenechal
SylvainSenechal requested review from a team, benzekrimaha, Copilot, delthas and maeldonn and removed request for benzekrimaha July 20, 2026 16:37

Copilot AI 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.

Pull request overview

This PR fixes Backbeat putData collision detection for replication of non-current object versions by ensuring the metadata lookup targets the specific replicated version (from x-scal-version-id) rather than always using the master version.

Changes:

  • Decode x-scal-version-id in the Backbeat router for PUT /_/backbeat/data and use it as the versionId for metadata lookup so objMd corresponds to the replicated version.
  • Simplify collision detection in putData to treat the presence of that specific version’s objMd as a collision (while explicitly excluding ExternalNullVersionId / "null").
  • Add functional test coverage for collisions on a non-current version and adjust the "null" version test to cover cases where a master already exists.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/functional/backbeat/putData.js Adds/updates functional tests to validate collision behavior for non-current versions and "null" version behavior.
lib/routes/routeBackbeat.js Uses decoded x-scal-version-id to fetch version-specific metadata for putData, fixing collision detection semantics.

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

Logic is fine, need a little refactor

Comment thread lib/routes/routeBackbeat.js Outdated
}
if (objMd && objMd.versionId === incomingVersionIdDecoded) {
// Data already at destination for this version; return 409 with the existing
if (objMd) {

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.

if (objMd) only means "this version exists" because the router's isPutDataApi block fetched the header version instead of the master; that coupling is easy to break. Clearer to drop the router block and fetch the version here with metadataGetObject (returns undefined when missing):

const incomingVersionId = decode(incomingVersionIdEncoded);
// ... BadRequest on decode error ...
return metadataGetObject(request.bucketName, request.objectKey, incomingVersionId, null, log, (err, versionMd) => {
    if (err) {
        return callback(err);
    }
    if (versionMd) {
        // existing 409 conflict path, using versionMd.microVersionId
    }
    return writeData();
});

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 I'll let other reviewers comment on that but I don't fully agree :

Middleware/router is usually a good place to pre-extract generic info, here, the router already get the object after validating query params (bucket/object names), and it does it for all backbeat routes.
If we re add a metadataGetObject in this specific api, we are gonna call it twice because it's already called in the router.

I think it's an ok design to have the router handle the get object, the only thing a bit dirty is indeed that new logic where the router fetches an object specific version id only for putData

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.

I think this is not a theoretical problem, we need to be practical:

  • the router makes one metadata call already; we must not make a second metadata to avoid modifying the router on principle. Metadata calls are costly at scale, we should not make a useless one.
  • in addition, the router does auth check on the object it requests. So pointing at the wrong object could lead to incorrect auth (esp. in cross-account case)
  • modifying the router must be done carefully though, to ensure we don't break any case: neither putData, nor other cases
  • I think there is a case in putMetadata where we also make the call without versionId, and eventually need to make a second metadata call to get actual version. Bonus point if we can fix it at the same time (but anyway should look at it, as it could steer the change in another direction)

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.

Ok so lets keet it here, will recheck putmetadata

Comment thread lib/routes/routeBackbeat.js Outdated
// For putData api: the version to check is passed
// via x-scal-version-id header, not the URL query. Fetch that specific
// version so objMd matches the replicated version, not always the master.
const isPutDataApi = request.method === 'PUT' && request.resourceType === 'data';

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.

The router shouldn't special-case one handler, and these guards duplicate putData's. Move the version lookup into putData itself (see handler comment) so this block goes away.

@SylvainSenechal SylvainSenechal Aug 17, 2026

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.

This is debatable if we consider that the router acts as a middleware that fetches objMd for all apis, in that case, we wouldn't want each apis functions to have their own extra ways of dealing with fetching again objMd.

Let's discuss with the first 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.

the router already has many cases, which are kind of hard-coded.

Maybe we can do this based on the presence of the header (if versionIdHeader ...)? I.e. set the query.versionId field from this header if it was not present already, before calling decodeVersionId ?

either way, even if we keep the "is put api" guard, no point doing both decodeVersionId and handling if this header:

Suggested change
const isPutDataApi = request.method === 'PUT' && request.resourceType === 'data';
const versionId = ...... ? // whatever condition: header present, put api, ...
decodeVersionId(request.query) : decodeScalVersionId(header);

(or move it in a separate function, out of here)

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 checked cloudserverClient, we only have 3 other apis (MPUxx) using the versionId in header, and these request are treated differently. We can remove the "if isPutData" and do something simpler

Comment thread lib/routes/routeBackbeat.js Outdated
@SylvainSenechal
SylvainSenechal force-pushed the improvement/CLDSRV-953 branch 2 times, most recently from c3d0e4b to c9814a4 Compare August 14, 2026 16:50

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.

General comment regarding Maël 3 feedbacks : I mostly agree with him that there is weirdness in the code changes, and that the current solution doesn't feel fully satisfying, but I don't think there is any perfect way to do it, else we would need a bigger refactoring

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.

else we would need a bigger refactoring

generally : if the proper way requires a refactoring, it should be done. The risk otherwise is to end up with an accumulation of code which is just slightly off - but taken together ends up being a huge pile of mess...

so one must dig into this "not fully satisfying feeling" : is it because we are introducing debt? is that debt something that may come back at us, or something easily manageable? what refactoring would be required? would this refactoring really help the problem here, or just cleanup the rest (but the change would still be as complex)? How long would this refactor take, is it worth it? is it worth it to solve the specific problem at hand? Is it because the problem should be solved more simply? in that case, does the complexity come from the problem it self (i.e. not so easy because of some corner case/...), from some implementation choice, ...? are there some conditions (context) which could allow for a better solution? ...

Comment thread lib/routes/routeBackbeat.js Outdated
incomingVersionIdEncoded !== 'null' ? decode(incomingVersionIdEncoded) : 'null';
if (incomingVersionIdDecoded instanceof Error) {
// ExternalNullVersionId means a pre-versioning null object: collision detection is not applicable.
if (incomingVersionIdEncoded !== undefined && incomingVersionIdEncoded !== ExternalNullVersionId) {

@SylvainSenechal SylvainSenechal Aug 17, 2026

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.

abit hard to read but the diff is equivalent, in both case, the logic is :
"do not try to decode a 'null' versionId", as this would throw an error, and we dont wanna throw on 'null' versionId, they are just special case.

Prefer this new code, to avoid weird ternary and start using ExternalNullVersionId arsenal constant which will remind developers that 'null' versionId is a thing 👀

@SylvainSenechal

Copy link
Copy Markdown
Contributor Author

@francoisferrand added as Maël not here

@scality scality deleted a comment from bert-e Aug 17, 2026
Comment thread lib/routes/routeBackbeat.js Outdated
if (objMd && objMd.versionId === incomingVersionIdDecoded) {
// Data already at destination for this version; return 409 with the existing
if (objMd) {
// objMd is the specific version (fetched by versionId from x-scal-version-id header)

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.

not true : in case of decode error, you fallback to the "old" path.... so you may still receive an objMD from the master, even though a version id was provided.

i.e. same as https://github.com/scality/cloudserver/pull/6230/changes#r3637279080 : on header decode error the API should fail. It should not fallback to requesting master if a versionId is provided.

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.

you're right, considering that versionIdHeader is provided, it means the client specifically asks for a version ID, so now the middleware will return an error when failing to decode

Comment thread lib/routes/routeBackbeat.js Outdated
Comment thread lib/routes/routeBackbeat.js Outdated
@scality scality deleted a comment from bert-e Aug 18, 2026
Comment thread lib/routes/routeBackbeat.js Outdated
);
// ExternalNullVersionId means a null version, which does not need decoding
if (incomingVersionIdEncoded !== ExternalNullVersionId) {
if (objMd) {

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.

Actually removed the whole decode here : It's already done in the middleware, we could almost do the whole remaining logic in the middleware but its probably not the right place

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.

the conflict logic does not belong in middleware - it is specific to some routes only. It's not about code (we could do anything), but about abstractions and layers: and that logic certainly does not belong in the middleware, while decoding the versionId does.

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.

else we would need a bigger refactoring

generally : if the proper way requires a refactoring, it should be done. The risk otherwise is to end up with an accumulation of code which is just slightly off - but taken together ends up being a huge pile of mess...

so one must dig into this "not fully satisfying feeling" : is it because we are introducing debt? is that debt something that may come back at us, or something easily manageable? what refactoring would be required? would this refactoring really help the problem here, or just cleanup the rest (but the change would still be as complex)? How long would this refactor take, is it worth it? is it worth it to solve the specific problem at hand? Is it because the problem should be solved more simply? in that case, does the complexity come from the problem it self (i.e. not so easy because of some corner case/...), from some implementation choice, ...? are there some conditions (context) which could allow for a better solution? ...

Comment thread lib/routes/routeBackbeat.js Outdated
Comment on lines +438 to +440
// ExternalNullVersionId means a null version, which does not need decoding
if (incomingVersionIdEncoded !== ExternalNullVersionId) {
if (objMd) {

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.

should have a conflict as well in case of an existing object with null versionId!

Suggested change
// ExternalNullVersionId means a null version, which does not need decoding
if (incomingVersionIdEncoded !== ExternalNullVersionId) {
if (objMd) {
if (incomingVersionIdEncoded !== undefined && objMd) {

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.

This is true, I just verified and added a test that when provided version id is 'null', the backend does fetch the null version instead of master which i wasnt sure about

error: decodedVidResult,
});
return next(errors.InvalidArgument);
const versionIdHeader = request.headers['x-scal-version-id'];

@francoisferrand francoisferrand Aug 19, 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.

this makes the x-scal-version-id overrides the (possibly missing) query.versionId field.

  • Can you please double check which routes are affected / in which case (i.e. which route use x-scal-versionId), and especially confirm in each case that it should indeed look up this object instead of the query.versionId / master one?
  • Should we abort/fail -or at least log a warning- if the request has both x-scal-version-id and query.versionId ? If fear this code could allow abused to silently override the query.versionId field, and possibly break stuff...
  • Do we have some validation about allowed/required fields, eg. putData has an x-scal-version-id, deleteObjectFromLifecycle has a required query.versionId, etc... It does not seem to be handled here, is it handled in each handler? (maybe not a new issue, but we should consider if we need to add something for putData/putMetadata, and possibly a followup to cover the other/existing API...)

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.

Good remarks, and I talked about it in these 2 comments :
#6230 (comment)
#6230 (comment)

The bottom line is, I feel we messed up something in the design of CRR Cascade by adding version id in the header instead of query params, and well we still have time to change it so maybe we should 🤔

Comment thread lib/routes/routeBackbeat.js Outdated
Comment thread lib/routes/routeBackbeat.js Outdated
Comment on lines +2039 to +2041
if (versionIdHeader !== ExternalNullVersionId) {
const decoded = decode(versionIdHeader);
if (decoded instanceof Error) {

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.

is this not the decodeVID() function ?

@SylvainSenechal SylvainSenechal Aug 19, 2026

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.

no decodeVersionId is for request.query (helper that extract the version id from the query before decoding)

and for the header we use directly decode from arsenal

edit: ok there is a third one decodeVID...

I'm gonna check together with your other comments, I think this will be clearer if extracted into a function

Comment thread lib/routes/routeBackbeat.js Outdated
});
return next(errors.InvalidArgument);
}
versionId = decodedVidResult;

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.

nit: code is kind of duplicated, and kind of not the right level for this function

could we change "remap" the x-scal-version-id for some routes maybe?
e.g.

if (request.headers['x-scal-version-id'] !== undefined) {
   request.query = request.headers['x-scal-version-id'];
}
const decodedVidResult = decodeVersionId(request.query);
[...]

or just move this logic in a dedicated function?

func decodeRequestVersionId(request) {
   const versionIdHeader = request.headers['x-scal-version-id']
   if (versionIdHeader !== undefined) {
      return versionIdHeader;
   }
   return decodeVersionId(request.query);
}
[...]
const decodedVidResult = decodeRequestVersionId(request);
if (decodedVidResult instanceof Error) {
    return next(errors.InvalidArgument.customizeDescription(decodedVidResult.error));
}
const versionId = decodedVidResult;

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 did some refactoring, its nice the decodeVID function handle the 'null' case.
I think it's simple enough that we don't need a helper function and can inline it.

i can't help but notice we could refactor it further... between arsenal and cloudserver we have 3 decodes functions....

decodeVersionId
decodeVID
decode

Comment thread lib/routes/routeBackbeat.js Outdated
);
// ExternalNullVersionId means a null version, which does not need decoding
if (incomingVersionIdEncoded !== ExternalNullVersionId) {
if (objMd) {

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.

the conflict logic does not belong in middleware - it is specific to some routes only. It's not about code (we could do anything), but about abstractions and layers: and that logic certainly does not belong in the middleware, while decoding the versionId does.

@SylvainSenechal

Copy link
Copy Markdown
Contributor Author

@francoisferrand I think the decode version id is clearer now although as I said in one of my response comment we could probably go even further in the refactoring of this function.

What I wanna address though is this comment, not sure you saw it : #6230 (comment)

I realize the version id passed in the header was added for CRR Cascade, and it doesn't seem like we had a strong argument to do it vs just passing it in query params like the other.

If we changed cloudserver client to pass it in the query param, it would be less troublesome and cleaner, actually this whole pr wouldn't have existed because we would've fetched the right version from the query directly..

err instanceof VersionIdCollisionException,
`expected VersionIdCollisionException, got ${err.constructor.name}`,
);
}

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 other collision tests (lines 88-92 and 146) assert err.microVersionId. This test should do the same for consistency and to catch regressions where the microVersionId is incorrect for null-version collisions.

Suggested change
}
assert.ok(
err instanceof VersionIdCollisionException,
`expected VersionIdCollisionException, got ${err.constructor.name}`,
);
assert.strictEqual(err.microVersionId, '', 'microVersionId should be empty for null-version collision');

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.

6 participants