From 3c10025e3fcc1574ffa4bc35aceaaea1325eb7d4 Mon Sep 17 00:00:00 2001 From: Nicolas Humbert Date: Thu, 19 Nov 2026 22:09:25 +0100 Subject: [PATCH] CLDSRV-1083 add a replication compatibility mode During a node-by-node upgrade, the nodes that are not upgraded yet read what an upgraded cloudserver writes, and they can't read the new replication format: an old backbeat doesn't replicate the objects, and an old cloudserver stops on the bucket replication configuration. With replicationCascadeCompatMode set, cloudserver stores and does what a cloudserver older than 9.4 does for replication: - the replicationInfo of an object is written in the old format, and no microVersionId is set on a metadata change - the bucket replication configuration is stored in the old shape. We refuse what an old cloudserver refuses: a single role for a CRR site, rules to different buckets, overlapping prefixes. Like it, we ignore the account of a rule - a replica is stored as backbeat sent it, with the status REPLICA. We ignore x-scal-micro-version-id and the version check of putData, so we don't compare revisions and we don't replicate a replica further - on an ACL change, we tell CRR from cloud destinations with the location type and not with the role of the backend: the old format has no role per backend. No change of behaviour outside the mode When the setting is absent or false, nothing changes. --- lib/Config.js | 10 +++ .../bucket/getReplicationConfiguration.js | 64 ++++++++++++++ lib/api/apiUtils/object/bumpMicroVersionId.js | 4 + lib/api/apiUtils/object/getReplicationInfo.js | 59 ++++++++++++- lib/api/bucketPutReplication.js | 15 +++- lib/metadata/acl.js | 17 ++-- lib/routes/routeBackbeat.js | 15 +++- tests/unit/api/apiUtils/getReplicationInfo.js | 34 +++++++ tests/unit/api/bucketPutReplication.js | 88 ++++++++++++++++++- tests/unit/api/objectReplicationMD.js | 34 +++++++ tests/unit/routes/routeBackbeat.js | 77 ++++++++++++++++ 11 files changed, 402 insertions(+), 15 deletions(-) diff --git a/lib/Config.js b/lib/Config.js index aa11d0f2e5..0fa186537e 100644 --- a/lib/Config.js +++ b/lib/Config.js @@ -1832,6 +1832,16 @@ class Config extends EventEmitter { this.testingMode = config.testingMode || false; + // When true, replication behaves like in a cloudserver older than 9.4: + // no multi-destination and no cascade. + if (config.replicationCascadeCompatMode !== undefined) { + assert( + typeof config.replicationCascadeCompatMode === 'boolean', + 'bad config: replicationCascadeCompatMode must be a boolean', + ); + } + this.replicationCascadeCompatMode = config.replicationCascadeCompatMode === true; + this.maxScannedLifecycleListingEntries = constants.maxScannedLifecycleListingEntries; if (config.maxScannedLifecycleListingEntries !== undefined) { // maxScannedLifecycleListingEntries > 2 is required as a minimum because we must diff --git a/lib/api/apiUtils/bucket/getReplicationConfiguration.js b/lib/api/apiUtils/bucket/getReplicationConfiguration.js index 9f6bf2c34c..41d1d6485d 100644 --- a/lib/api/apiUtils/bucket/getReplicationConfiguration.js +++ b/lib/api/apiUtils/bucket/getReplicationConfiguration.js @@ -2,6 +2,69 @@ const config = require('../../../Config').config; const parseXML = require('../../../utilities/parseXML'); const ReplicationConfiguration = require('arsenal').models.ReplicationConfiguration; +/** + * We tell if a rule replicates to a CRR site: a replication endpoint with no + * type. A storageClass that is not an endpoint (none, or STANDARD) means the + * default endpoint. + * @param {object[]} rules - The rules of the replication configuration + * @return {boolean} true when at least one rule replicates to a CRR site + */ +function _hasCrrSite(rules) { + const { replicationEndpoints } = config; + const defaultEndpoint = replicationEndpoints.find(endpoint => endpoint.default) ?? replicationEndpoints[0]; + const storageClasses = rules.flatMap(rule => (rule.storageClass ?? '').split(',')); + return storageClasses.some(storageClass => { + const site = storageClass.split(':')[0]; + const endpoint = replicationEndpoints.find(endpoint => endpoint.site === site) ?? defaultEndpoint; + return endpoint !== undefined && endpoint.type === undefined; + }); +} + +/** + * We rewrite the replication configuration like a cloudserver older than + * 9.4 stores it: one destination bucket for all the rules. An old + * cloudserver crashes on the new shape. + * We refuse what it refuses: a single role for a CRR site, rules to + * different buckets, overlapping prefixes. Like it, we ignore the account + * of a rule. + * @param {object} replicationConfig - The configuration in the current shape + * @return {object} { refused: { code, description } }, or { replicationConfig } in the old shape + */ +function toLegacyConfiguration(replicationConfig) { + const { role, rules, preferredReadLocation } = replicationConfig; + const refuse = (code, description) => ({ refused: { code, description } }); + if (role.split(',').length !== 2 && _hasCrrSite(rules)) { + return refuse( + 'InvalidArgument', + 'Invalid Role specified in replication configuration: ' + + 'Role must be a comma-separated list of two IAM roles', + ); + } + if (rules.some(rule => rule.destination !== rules[0].destination)) { + return refuse('InvalidRequest', 'The destination bucket must be the same for all rules'); + } + const prefixes = rules.map(rule => rule.prefix ?? ''); + for (let i = 0; i < prefixes.length; i++) { + const overlapping = prefixes.slice(0, i).find(p => p.startsWith(prefixes[i]) || prefixes[i].startsWith(p)); + if (overlapping !== undefined) { + return refuse('InvalidRequest', `Found overlapping prefixes '${overlapping}' and '${prefixes[i]}'`); + } + } + return { + replicationConfig: { + role, + destination: rules[0].destination, + rules: rules.map(({ prefix, enabled, id, storageClass }) => ({ + prefix: prefix ?? '', + enabled, + id, + storageClass, + })), + preferredReadLocation, + }, + }; +} + // Handle the steps for returning a valid replication configuration object. function getReplicationConfiguration(xml, log, cb) { return parseXML(xml, log, (err, result) => { @@ -27,5 +90,6 @@ function getReplicationConfigurationXML(config) { module.exports = { getReplicationConfiguration, + toLegacyConfiguration, getReplicationConfigurationXML, }; diff --git a/lib/api/apiUtils/object/bumpMicroVersionId.js b/lib/api/apiUtils/object/bumpMicroVersionId.js index 1a76970726..3cfcf3bb1f 100644 --- a/lib/api/apiUtils/object/bumpMicroVersionId.js +++ b/lib/api/apiUtils/object/bumpMicroVersionId.js @@ -17,6 +17,10 @@ function bumpMicroVersionId(objectMD, force) { if (!force && !objectMD?.replicationInfo) { return; } + // A cloudserver older than 9.4 doesn't set a microVersionId here. + if (config.replicationCascadeCompatMode) { + return; + } const { instanceId, replicationGroupId } = config; diff --git a/lib/api/apiUtils/object/getReplicationInfo.js b/lib/api/apiUtils/object/getReplicationInfo.js index 92f1153223..1bb2a7b2e3 100644 --- a/lib/api/apiUtils/object/getReplicationInfo.js +++ b/lib/api/apiUtils/object/getReplicationInfo.js @@ -4,6 +4,17 @@ const { constants, models } = require('arsenal'); const { replicationBackends } = constants; const { ReplicationConfiguration } = models; +/** + * A cloud site has the location type aws_s3, azure or gcp. Any other site + * is another cloudserver (CRR). + * @param {object} s3config - Cloudserver configuration object + * @param {string} site - The site name + * @return {boolean} true for a cloud backend + */ +function isCloudSite(s3config, site) { + return !!replicationBackends[s3config.locationConstraints[site]?.type]; +} + /** * Apply the default replication endpoint as a fallback storageClass * for rules that don't specify one. Returns a new rules array; rules @@ -45,6 +56,43 @@ function _canUserReplicate(authInfo) { return !!props?.canReplicate; } +/** + * We rewrite the replicationInfo like a cloudserver older than 9.4 writes + * it: destination, site and roles at the top, not in each backend. A + * backbeat older than 9.5.0 only reads them at the top. + * Example: one rule replicates to the bucket arn:aws:s3:::backup on site sfb. + * We return { role: 'src-role,dst-role', destination: 'arn:aws:s3:::backup', + * storageClass: 'sfb', storageType: '', + * backends: [{ site: 'sfb', status: 'PENDING', dataStoreVersionId: '' }] }. + * @param {object} replicationInfo - The replicationInfo in the current format + * @param {object} config - The bucket replication configuration + * @param {object} rule - The rule that applies to the object + * @param {object} s3config - Cloudserver configuration object + * @param {function} isCloud - Tells if a site is a cloud backend + * @return {object} The replicationInfo in the old format + */ +function _toLegacyFormat(replicationInfo, config, rule, s3config, isCloud) { + const backends = replicationInfo.backends.map(({ site, status, dataStoreVersionId }) => ({ + site, + status, + dataStoreVersionId, + })); + return { + status: replicationInfo.status, + backends, + content: replicationInfo.content, + destination: config.destination, + storageClass: rule.storageClass, + role: config.role, + storageType: backends + .filter(backend => isCloud(backend.site)) + .map(backend => s3config.locationConstraints[backend.site].type) + .join(','), + dataStoreVersionId: '', + isNFS: replicationInfo.isNFS, + }; +} + /** * Get the object replicationInfo to replicate data and metadata, or only * metadata if the operation only changes metadata or the object is 0 bytes. @@ -88,7 +136,7 @@ function getReplicationInfo(params) { return undefined; } - const isCloud = site => !!replicationBackends[s3config.locationConstraints[site]?.type]; + const isCloud = site => isCloudSite(s3config, site); const rules = _withDefaultStorageClass(config.rules || [], s3config); const allBackends = ReplicationConfiguration.resolveBackends( { ...config, rules }, @@ -116,7 +164,7 @@ function getReplicationInfo(params) { content.push(operationType); } - return { + const replicationInfo = { status: 'PENDING', backends, content, @@ -125,6 +173,13 @@ function getReplicationInfo(params) { // `undefined` so JSON/BSON drop the field on persist (saves bytes). isReplica: undefined, }; + if (!s3config.replicationCascadeCompatMode) { + return replicationInfo; + } + // No overlapping prefixes in compatibility mode, so one rule applies. + const rule = rules.find(r => r.enabled && objKey.startsWith(r.prefix ?? '')); + return _toLegacyFormat(replicationInfo, config, rule, s3config, isCloud); } module.exports = getReplicationInfo; +module.exports.isCloudSite = isCloudSite; diff --git a/lib/api/bucketPutReplication.js b/lib/api/bucketPutReplication.js index 9ff1f73bbd..b845d3e035 100644 --- a/lib/api/bucketPutReplication.js +++ b/lib/api/bucketPutReplication.js @@ -4,10 +4,11 @@ const { errorInstances } = require('arsenal'); const metadata = require('../metadata/wrapper'); const { standardMetadataValidateBucket } = require('../metadata/metadataUtils'); const { pushMetric } = require('../utapi/utilities'); -const { getReplicationConfiguration } = require('./apiUtils/bucket/getReplicationConfiguration'); +const { getReplicationConfiguration, toLegacyConfiguration } = require('./apiUtils/bucket/getReplicationConfiguration'); const validateConfiguration = require('./apiUtils/bucket/validateReplicationConfig'); const collectCorsHeaders = require('../utilities/collectCorsHeaders'); const monitoring = require('../utilities/monitoringHandler'); +const { config: s3config } = require('../Config'); // The error response when a bucket does not have versioning 'Enabled'. const versioningNotEnabledError = errorInstances.InvalidRequest.customizeDescription( @@ -36,6 +37,18 @@ function bucketPutReplication(authInfo, request, log, callback) { [ // Validate the request XML and return the replication configuration. next => getReplicationConfiguration(post, log, next), + // In compatibility mode, we store the configuration like a + // cloudserver older than 9.4, or we refuse it. + (config, next) => { + if (!s3config.replicationCascadeCompatMode) { + return next(null, config); + } + const { refused, replicationConfig } = toLegacyConfiguration(config); + if (refused) { + return next(errorInstances[refused.code].customizeDescription(refused.description)); + } + return next(null, replicationConfig); + }, // Check bucket user privileges and ensure versioning is 'Enabled'. (config, next) => // TODO: Validate that destination bucket exists and has versioning. diff --git a/lib/metadata/acl.js b/lib/metadata/acl.js index f649c27371..ab8a7e8884 100644 --- a/lib/metadata/acl.js +++ b/lib/metadata/acl.js @@ -48,14 +48,15 @@ const acl = { objectMD.originOp = 's3:ObjectAcl:Put'; // Rebuild replication info from the current bucket config to - // pick up any new destinations. A "role" on the entry means - // backbeat uses IAM role-based auth on the destination - // (CRR). No role means credentials live in the location - // configuration (cloud, including scality-to-scality via - // stored S3 creds), and ACL replication is not supported — - // preserve the existing status instead of resetting to - // PENDING. - const hasDestRole = b => !!b.role; + // pick up any new destinations. On a CRR destination backbeat + // uses IAM role-based auth. On a cloud destination the + // credentials live in the location configuration (cloud, + // including scality-to-scality via stored S3 creds), and ACL + // replication is not supported: we preserve the existing + // status instead of resetting to PENDING. + // We check the location type, not the backend role: in the old + // format a backend has no role. + const hasDestRole = b => !getReplicationInfo.isCloudSite(config, b.site); const sameBackend = (a, b) => a.site === b.site && a.destination === b.destination && a.role === b.role; const replicationInfo = getReplicationInfo({ diff --git a/lib/routes/routeBackbeat.js b/lib/routes/routeBackbeat.js index 64de690774..3950078635 100644 --- a/lib/routes/routeBackbeat.js +++ b/lib/routes/routeBackbeat.js @@ -428,7 +428,9 @@ function putData(request, response, bucketInfo, objMd, log, callback) { return callback(errorInstances.BadRequest.customizeDescription(errMessage)); } - const incomingVersionIdEncoded = request.query?.versionId; + // In compatibility mode we always write the data, like a cloudserver older + // than 9.4: the check below needs a microVersionId and we store none. + const incomingVersionIdEncoded = config.replicationCascadeCompatMode ? undefined : request.query?.versionId; if (incomingVersionIdEncoded !== undefined && objMd) { // objMd is the specific version requested via the versionId query param. // Its existence means the data is already at the destination. Return 409 with the @@ -564,7 +566,11 @@ function getCanonicalIdsByAccountId(accountId, log, cb) { function putMetadata(request, response, bucketInfo, objMd, log, callback) { const { bucketName, objectKey } = request; - const encodedMicroVersionId = request.headers['x-scal-micro-version-id']; + // In compatibility mode we ignore x-scal-micro-version-id, so we don't + // compare microVersionId and we don't replicate the replica further. + const encodedMicroVersionId = config.replicationCascadeCompatMode + ? undefined + : request.headers['x-scal-micro-version-id']; // When the header is present this is a conditional write: accept only if // the incoming microVersionId is newer than the one already stored. // '' is a valid value meaning the source has no microVersionId (null revision). @@ -829,7 +835,10 @@ function putMetadata(request, response, bucketInfo, objMd, log, callback) { } const isReplicationWrite = !!headers['x-scal-replication-content']; - if (isReplicationWrite) { + if (config.replicationCascadeCompatMode) { + // A cloudserver older than 9.4 does not store isReplica, so we remove it. + delete omVal.replicationInfo?.isReplica; + } else if (isReplicationWrite) { const isMDOnly = headers['x-scal-replication-content'] === 'METADATA'; const objSize = omVal['content-length'] || 0; diff --git a/tests/unit/api/apiUtils/getReplicationInfo.js b/tests/unit/api/apiUtils/getReplicationInfo.js index 78ec6ddff2..1ed4ef318b 100644 --- a/tests/unit/api/apiUtils/getReplicationInfo.js +++ b/tests/unit/api/apiUtils/getReplicationInfo.js @@ -395,6 +395,40 @@ describe('getReplicationInfo helper', () => { }); }); + describe('replication compatibility mode', () => { + const compatS3Config = { ...TEST_CONFIG, replicationCascadeCompatMode: true }; + + it('should write what a cloudserver older than 9.4 writes', () => { + const replicationConfig = { + role: TWO_PART_ROLE, + destination: 'arn:aws:s3:::backup', + rules: [{ prefix: '', enabled: true, storageClass: 'crr-site' }], + }; + assert.deepStrictEqual(_getObjectReplicationInfo(compatS3Config, replicationConfig), { + status: 'PENDING', + backends: [{ site: 'crr-site', status: 'PENDING', dataStoreVersionId: '' }], + content: ['METADATA'], + destination: 'arn:aws:s3:::backup', + storageClass: 'crr-site', + role: TWO_PART_ROLE, + storageType: '', + dataStoreVersionId: '', + isNFS: undefined, + }); + }); + + it('should write the rule storageClass as it is, and the types of the cloud backends', () => { + const replicationConfig = { + role: TWO_PART_ROLE, + destination: 'tosomewhere', + rules: [{ prefix: '', enabled: true, storageClass: 'awsbackend:preferred_read,azurebackend' }], + }; + const replicationInfo = _getObjectReplicationInfo(compatS3Config, replicationConfig); + assert.strictEqual(replicationInfo.storageClass, 'awsbackend:preferred_read,azurebackend'); + assert.strictEqual(replicationInfo.storageType, 'aws_s3,azure'); + }); + }); + // --- V2 Format Tests (multi-rule matching) --- describe('V2 format (multi-rule matching)', () => { const V2_ROLE = 'arn:aws:iam::123456:role/src-role,arn:aws:iam::111111:role/dst-role'; diff --git a/tests/unit/api/bucketPutReplication.js b/tests/unit/api/bucketPutReplication.js index acd07404af..6a0d326c9b 100644 --- a/tests/unit/api/bucketPutReplication.js +++ b/tests/unit/api/bucketPutReplication.js @@ -2,9 +2,13 @@ const assert = require('assert'); const bucketPutReplication = require('../../../lib/api/bucketPutReplication'); const { DummyRequestLogger, makeAuthInfo } = require('../helpers'); -const { getReplicationConfiguration } = require('../../../lib/api/apiUtils/bucket/getReplicationConfiguration'); +const { + getReplicationConfiguration, + toLegacyConfiguration, +} = require('../../../lib/api/apiUtils/bucket/getReplicationConfiguration'); const validateReplicationConfig = require('../../../lib/api/apiUtils/bucket/validateReplicationConfig'); const replicationUtils = require('../../functional/aws-node-sdk/lib/utility/replication'); +const { config } = require('../../../lib/Config'); const log = new DummyRequestLogger(); // Check for the expected error response code and status code. @@ -100,6 +104,70 @@ describe('getReplicationConfiguration function', () => { }); }); +describe('toLegacyConfiguration function', () => { + const role = 'arn:aws:iam::account-id:role/src-resource,arn:aws:iam::account-id:role/dest-resource'; + const rule = (prefix, bucket) => ({ + prefix, + enabled: true, + id: prefix, + storageClass: 'STANDARD', + destination: `arn:aws:s3:::${bucket}`, + }); + + it('should return the shape a cloudserver older than 9.4 stores', () => { + const config = { role, rules: [rule('a', 'backup'), rule('b', 'backup')], preferredReadLocation: null }; + assert.deepStrictEqual(toLegacyConfiguration(config), { + replicationConfig: { + role, + destination: 'arn:aws:s3:::backup', + rules: [ + { prefix: 'a', enabled: true, id: 'a', storageClass: 'STANDARD' }, + { prefix: 'b', enabled: true, id: 'b', storageClass: 'STANDARD' }, + ], + preferredReadLocation: null, + }, + }); + }); + + it('should refuse rules that replicate to different buckets', () => { + const config = { role, rules: [rule('a', 'backup'), rule('b', 'backup2')] }; + assert.deepStrictEqual(toLegacyConfiguration(config), { + refused: { code: 'InvalidRequest', description: 'The destination bucket must be the same for all rules' }, + }); + }); + + it('should refuse rules with overlapping prefixes', () => { + const config = { role, rules: [rule('logs', 'backup'), rule('logs/2026', 'backup')] }; + assert.deepStrictEqual(toLegacyConfiguration(config), { + refused: { code: 'InvalidRequest', description: "Found overlapping prefixes 'logs' and 'logs/2026'" }, + }); + }); + + it('should ignore the account of a rule', () => { + const config = { role, rules: [{ ...rule('a', 'backup'), account: '123456789012' }] }; + assert.deepStrictEqual(toLegacyConfiguration(config).replicationConfig.rules, [ + { prefix: 'a', enabled: true, id: 'a', storageClass: 'STANDARD' }, + ]); + }); + + it('should refuse a single role when a rule replicates to a CRR site', () => { + const config = { role: role.split(',')[0], rules: [rule('a', 'backup')] }; + assert.deepStrictEqual(toLegacyConfiguration(config), { + refused: { + code: 'InvalidArgument', + description: + 'Invalid Role specified in replication configuration: ' + + 'Role must be a comma-separated list of two IAM roles', + }, + }); + }); + + it('should accept a single role when the rules replicate to cloud sites only', () => { + const config = { role: role.split(',')[0], rules: [{ ...rule('a', 'backup'), storageClass: 'us-east-2' }] }; + assert.strictEqual(toLegacyConfiguration(config).refused, undefined); + }); +}); + describe('validateReplicationConfig function', () => { const nonTransientBucket = { getLocationConstraint: () => 'us-east-1', @@ -183,4 +251,22 @@ describe('bucketPutReplication API', () => { done(); }); }); + + it('should refuse two destination buckets in the replication compatibility mode', done => { + const oneRule = createReplicationXML().match(/.*<\/Rule>/)[0]; + const otherBucket = oneRule.replace('destination-bucket', 'other-bucket').replace('foo', 'bar'); + const request = { + bucketName: 'test-bucket', + post: createReplicationXML().replace(oneRule, oneRule + otherBucket), + headers: {}, + actionImplicitDenies: false, + }; + config.replicationCascadeCompatMode = true; + bucketPutReplication(makeAuthInfo('accessKey1'), request, log, err => { + config.replicationCascadeCompatMode = false; + assert(err.is.InvalidRequest); + assert.strictEqual(err.description, 'The destination bucket must be the same for all rules'); + done(); + }); + }); }); diff --git a/tests/unit/api/objectReplicationMD.js b/tests/unit/api/objectReplicationMD.js index 11422e5b47..b73cda7221 100644 --- a/tests/unit/api/objectReplicationMD.js +++ b/tests/unit/api/objectReplicationMD.js @@ -403,6 +403,40 @@ describe('Replication object MD without bucket replication config', () => { ); }); + it('should update metadata if putting object ACL and CRR replication in the compatibility mode', done => { + config.locationConstraints['zenko'] = { + ...config.locationConstraints['zenko'], + type: '', + }; + config.replicationCascadeCompatMode = true; + + async.series( + [ + next => objectPut(authInfo, getObjectPutReq(keyA, true), undefined, log, next), + next => { + const objectMD = metadata.keyMaps.get(bucketName).get(keyA); + // Update metadata to a status after replication has occurred. + objectMD.replicationInfo.status = 'COMPLETED'; + objectPutACL(authInfo, objectACLReq, log, next); + }, + ], + err => { + config.replicationCascadeCompatMode = false; + if (err) { + return done(err); + } + const { replicationInfo } = metadata.keyMaps.get(bucketName).get(keyA); + assert.strictEqual(replicationInfo.status, 'PENDING'); + assert.deepStrictEqual(replicationInfo.content, ['METADATA']); + assert.strictEqual(replicationInfo.storageClass, 'zenko'); + assert.strictEqual(replicationInfo.backends[0].role, undefined); + // A cloudserver older than 9.4 sets no microVersionId here. + assert.strictEqual(metadata.keyMaps.get(bucketName).get(keyA).microVersionId, undefined); + return done(); + }, + ); + }); + it('should not update metadata if putting object ACL and cloud replication', done => { // Set 'zenko' as a typical cloud location (i.e. type) config.locationConstraints['zenko'] = { diff --git a/tests/unit/routes/routeBackbeat.js b/tests/unit/routes/routeBackbeat.js index 623c33d0ab..572e6d434a 100644 --- a/tests/unit/routes/routeBackbeat.js +++ b/tests/unit/routes/routeBackbeat.js @@ -219,6 +219,40 @@ describe('routeBackbeat', () => { assert.strictEqual(mockResponse.body.code, 'BadDigest'); }); + it('should write the data again in the replication compatibility mode when the version exists', async () => { + // Outside the compatibility mode this request is answered 409 VersionIdCollisionException. + const rawVersionId = versioning.VersionID.generateVersionId('test', 'RG001'); + mockRequest = prepareDummyRequest({ + 'x-scal-canonical-id': 'id', + 'content-md5': '1234', + 'content-length': '0', + 'x-scal-versioning-required': 'true', + }); + mockRequest.method = 'PUT'; + mockRequest.url = `/_/backbeat/data/bucket0/key0?versionId=${versioning.VersionID.encode(rawVersionId)}`; + mockRequest.destroy = () => {}; + metadataUtils.standardMetadataValidateBucketAndObj.callsFake((params, denies, log, callback) => { + callback( + null, + { + getVersioningConfiguration: () => ({ Status: 'Enabled' }), + isVersioningEnabled: () => true, + getLocationConstraint: () => undefined, + }, + { versionId: rawVersionId }, + ); + }); + storeObject.dataStore.callsFake((...args) => args[args.length - 1](null, {}, '1234')); + + config.replicationCascadeCompatMode = true; + routeBackbeat('127.0.0.1', mockRequest, mockResponse, log); + void (await endPromise); + config.replicationCascadeCompatMode = false; + + sinon.assert.calledOnce(storeObject.dataStore); + assert.strictEqual(mockResponse.statusCode, 200); + }); + it('should return 409 VersionIdCollisionException when versionId matches master, no microVersionId', async () => { // Old objects without microVersionId: data is already at destination. // Cloudserver returns 409 VersionIdCollisionException with empty x-scal-micro-version-id. @@ -577,6 +611,49 @@ describe('routeBackbeat', () => { assert.deepStrictEqual(omVal.replicationInfo.backends, []); assert.strictEqual(omVal.replicationInfo.isReplica, true); }); + + describe('in the replication compatibility mode', () => { + beforeEach(() => { + config.replicationCascadeCompatMode = true; + }); + + afterEach(() => { + config.replicationCascadeCompatMode = false; + }); + + it('should store the update as a REPLICA and not replicate it further', async () => { + mockRequest = prepareTagUpdate(versioning.VersionID.generateVersionId('test', 'RG001')); + + routeBackbeat('127.0.0.1', mockRequest, mockResponse, log); + void (await endPromise); + + assert.strictEqual(mockResponse.statusCode, 200); + const omVal = putObjectMDStub.firstCall.args[2]; + assert.deepStrictEqual(omVal.tags, { t: '1' }); + // The replica keeps the status backbeat sent, like on a + // cloudserver older than 9.4. + assert.strictEqual(omVal.replicationInfo.status, 'REPLICA'); + assert.strictEqual(omVal.replicationInfo.backends, undefined); + assert.strictEqual('isReplica' in omVal.replicationInfo, false); + }); + + it('should not compare the microVersionId with the one we store', async () => { + // Outside the compatibility mode, the same microVersionId on both + // sides is answered 409 MicroVersionIdAlreadyStored. + const microVersionId = versioning.VersionID.generateVersionId('test', 'RG001'); + mockRequest = prepareTagUpdate(microVersionId); + metadataUtils.standardMetadataValidateBucketAndObj.callsFake((_params, _denies, _log, callback) => { + callback(null, bucketInfoWithNextHop, { tags: {}, microVersionId }); + }); + + routeBackbeat('127.0.0.1', mockRequest, mockResponse, log); + void (await endPromise); + + assert.strictEqual(mockResponse.statusCode, 200); + sinon.assert.calledOnce(putObjectMDStub); + assert.strictEqual(putObjectMDStub.firstCall.args[3].conditions, undefined); + }); + }); }); it('should handle error when putting metadata', async () => {