Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extensions/lifecycle/bucketProcessor/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"Effect": "Allow",
"Action": [
"s3:GetLifecycleConfiguration",
"s3:GetBucketLocation",
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 need extra permission to get "archive info" in HeadObject

"s3:GetBucketVersioning",
"s3:ListBucket",
"s3:ListBucketVersions",
Expand Down
6 changes: 5 additions & 1 deletion extensions/lifecycle/tasks/LifecycleDeleteObjectTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { HeadObjectCommand, AbortMultipartUploadCommand, DeleteObjectCommand } =

const BackbeatTask = require('../../../lib/tasks/BackbeatTask');
const { LifecycleMetrics } = require('../LifecycleMetrics');
const { resolveLifecycleMetricObjectLocation } = require('../util/lifecycleLocation');
const {
DeleteObjectFromExpirationCommand,
attachReqUids,
Expand Down Expand Up @@ -195,7 +196,10 @@ class LifecycleDeleteObjectTask extends BackbeatTask {

const actionType = entry.getActionType();
const transitionTime = entry.getAttribute('transitionTime');
const location = this.objectMD?.dataStoreName || entry.getAttribute('details.dataStoreName');
const location = resolveLifecycleMetricObjectLocation(
this.objectMD,
entry.getAttribute('details.dataStoreName')
);

let reqMethod = 'deleteObject';
let actionMethod = this._deleteObject.bind(this);
Expand Down
224 changes: 198 additions & 26 deletions extensions/lifecycle/tasks/LifecycleTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
GetObjectTaggingCommand,
HeadObjectCommand,
GetBucketVersioningCommand,
GetBucketLocationCommand,
} = require('@aws-sdk/client-s3');
const { attachReqUids } = require('@scality/cloudserverclient');
const config = require('../../../lib/Config');
Expand All @@ -24,6 +25,11 @@ const ReplicationAPI = require('../../replication/ReplicationAPI');
const { LifecycleMetrics, LIFECYCLE_MARKER_METRICS_LOCATION } = require('../LifecycleMetrics');
const locationsConfig = require('../../../conf/locationConfig.json') || {};
const { rulesSupportTransition } = require('../util/rules');
const {
isRealLocation,
shouldResolveLifecycleMetricLocation,
resolveLifecycleMetricObjectLocation,
} = require('../util/lifecycleLocation');
const { decode } = versioning.VersionID;

const errorTransitionInProgress = errors.InternalError.
Expand Down Expand Up @@ -58,6 +64,19 @@ const MAX_RETRIES = 4;
// parallel tasks, so the total delay of retries should about 1m30s.
const MAX_RETRIES_TOTAL = CONCURRENCY_DEFAULT * MAX_RETRIES * 10;

function attachArchiveInfoHeader(command) {
command.middlewareStack.add(next => async args => {
if (args.request && args.request.headers) {
// eslint-disable-next-line no-param-reassign
args.request.headers['x-amz-scal-archive-info'] = 'true';
}
return next(args);
}, {
step: 'build',
name: 'attachArchiveInfoHeader',
});
}

/**
* compare 2 version by their stale dates returning:
* - LT (-1) if v1 is less than v2
Expand Down Expand Up @@ -104,6 +123,7 @@ class LifecycleTask extends BackbeatTask {

this.setSupportedRules(this.supportedRules);
this._totalRetries = 0;
this._bucketLocationCache = new Map();
}

setSupportedRules(supportedRules) {
Expand Down Expand Up @@ -197,30 +217,176 @@ class LifecycleTask extends BackbeatTask {
* @return {undefined}
*/
_sendObjectAction(entry, cb) {
const location = entry.getAttribute('details.dataStoreName');
const initialLocation = entry.getAttribute('details.dataStoreName');
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 is not the "initialLocation" : if the location is already set (e.g. object has been transitioned already or written directly to a non-default location), this value should be correct already : and it must be used.
→ this is really the "real" location, except sometimes it is "STANDARD" (i.e. default bucket location) and needs to be resolved

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 sometimes needs to be resolved because it is a "cold" location, according to the ticket.

Though practically, not sure what the problem really is - of if there one : sendObjectAction would be called on expiration, and seems ok if we report the expiration of a cold object as happening on the "cold" location itself?


const shouldBreak = this.circuitBreakers.tripped(
'expiration',
location,
this.objectTasksTopic,
);
if (shouldBreak) {
process.nextTick(() => cb(errorCircuitBreakerTripped));
return;
this._resolveLifecycleMetricLocation(entry, initialLocation, this.log, (err, location) => {
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.

instead of the extra metadata call, could we change one (existing) call?
i.e. if we already call HeadObject, we could instead pass the appropriate flag (x-amz-scal-archive-info) to get more details? Or use getObjectAttribute/listObjectV2 with attributes to get the required info more efficiently?

if (err) {
return cb(err);
}
const metricLocation = location || '';
entry.setAttribute('details.dataStoreName', metricLocation);

const shouldBreak = this.circuitBreakers.tripped(
'expiration',
metricLocation,
this.objectTasksTopic,
);
if (shouldBreak) {
return process.nextTick(() => cb(errorCircuitBreakerTripped));
}

LifecycleMetrics.onLifecycleTriggered(this.log, 'bucket',
entry.getActionType() === 'deleteMPU' ? 'expiration:mpu' : 'expiration',
metricLocation,
Date.now() - entry.getAttribute('transitionTime'));

const entries = [{ message: entry.toKafkaMessage() }];
return this.producer.sendToTopic(this.objectTasksTopic, entries, err => {
LifecycleMetrics.onKafkaPublish(null, 'ObjectTopic', 'bucket', err, 1);
return cb(err);
});
});
}

_getBucketLocationConstraint(bucket, log, cb) {
if (this._bucketLocationCache.has(bucket)) {
return process.nextTick(cb, null, this._bucketLocationCache.get(bucket));
}

LifecycleMetrics.onLifecycleTriggered(this.log, 'bucket',
entry.getActionType() === 'deleteMPU' ? 'expiration:mpu' : 'expiration',
location,
Date.now() - entry.getAttribute('transitionTime'));
if (!this.s3target) {
return process.nextTick(cb);
}

const entries = [{ message: entry.toKafkaMessage() }];
this.producer.sendToTopic(this.objectTasksTopic, entries, err => {
LifecycleMetrics.onKafkaPublish(null, 'ObjectTopic', 'bucket', err, 1);
return cb(err);
const command = new GetBucketLocationCommand({ Bucket: bucket });
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.

getBucketLocation() returns the default location of a bucket, which could change (it happens in S3C, in some cases):

  • so would be a race condition here...
  • the cache makes this issue worse, as it may get poisoned by the "old" value of the location

attachReqUids(command, log.getSerializedUids());
return this.s3target.send(command)
.then(data => {
LifecycleMetrics.onS3Request(log, 'getBucketLocation', 'bucket', null);
const location = data && data.LocationConstraint;
this._bucketLocationCache.set(bucket, location);
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.

there may be many buckets, not sure we should allow unbounded cache

return cb(null, location);
})
.catch(err => {
LifecycleMetrics.onS3Request(log, 'getBucketLocation', 'bucket', err);
log.debug('failed to get bucket location for lifecycle metrics', {
method: 'LifecycleTask._getBucketLocationConstraint',
bucket,
error: err.message,
});
return cb();
});
}

_resolveLifecycleMetricLocationFromBucket(entry, fallbackLocation, log, cb) {
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 so-called "fallbackLocation" is NOT a fallback, this is the actual location
  • parameter is redundant : this is part of entry already

if (!shouldResolveLifecycleMetricLocation(fallbackLocation)) {
return process.nextTick(cb, null, fallbackLocation);
}

const bucket = entry.getAttribute('target.bucket');
return this._getBucketLocationConstraint(bucket, log, (err, bucketLocation) => {
if (err || !isRealLocation(bucketLocation)) {
return cb(null, fallbackLocation);
}
return cb(null, bucketLocation);
});
}

_getArchiveInfoLocation(entry, log, cb) {
if (!this.s3target) {
return process.nextTick(cb);
}

const params = {
Bucket: entry.getAttribute('target.bucket'),
Key: entry.getAttribute('target.key'),
};
const versionId = entry.getAttribute('target.version');
if (versionId) {
params.VersionId = versionId;
}

const command = new HeadObjectCommand(params);
attachArchiveInfoHeader(command);
attachReqUids(command, log.getSerializedUids());
return this.s3target.send(command)
.then(data => {
LifecycleMetrics.onS3Request(log, 'headObjectArchiveInfo', 'bucket', null);
return cb(null, data.StorageClass);
})
.catch(err => {
LifecycleMetrics.onS3Request(log, 'headObjectArchiveInfo', 'bucket', err);
log.debug('failed to get object archive info for lifecycle metric location', {
method: 'LifecycleTask._getArchiveInfoLocation',
bucket: params.Bucket,
objectKey: params.Key,
versionId,
error: err.message,
});
return cb();
});
}

_resolveLifecycleMetricLocationFromArchiveInfo(entry, fallbackLocation, log, cb) {
if (!shouldResolveLifecycleMetricLocation(fallbackLocation)) {
return process.nextTick(cb, null, fallbackLocation);
}

return this._getArchiveInfoLocation(entry, log, (err, archiveInfoLocation) => {
if (err || !isRealLocation(archiveInfoLocation)) {
return this._resolveLifecycleMetricLocationFromBucket(entry, fallbackLocation, log, cb);
}
return cb(null, archiveInfoLocation);
});
}

_resolveLifecycleMetricLocationFromMetadata(entry, fallbackLocation, log, cb) {
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 a "fallback" location...

if (!this.backbeatMetadataProxy) {
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 do we need 3 cases?

  • backbeatMetadataProxy is always available AFAICS (always initialized in Bucket Processor)
  • resolving from ObjectMD or Bucket should work in all cases : but at the cost of extra IO (read ObjectMD) or race condition (read default bucket location)

return this._resolveLifecycleMetricLocationFromArchiveInfo(entry, fallbackLocation, log, cb);
}

if (entry.getActionType() === 'deleteMPU') {
return this._resolveLifecycleMetricLocationFromBucket(entry, fallbackLocation, log, cb);
}

const params = {
bucket: entry.getAttribute('target.bucket'),
objectKey: entry.getAttribute('target.key'),
versionId: entry.getAttribute('target.version'),
};

return this._getObjectMD(params, log, (err, objectMD) => {
LifecycleMetrics.onS3Request(log, 'getMetadata', 'bucket', err);
if (err) {
log.debug('failed to get object metadata for lifecycle metric location', {
method: 'LifecycleTask._resolveLifecycleMetricLocationFromMetadata',
bucket: params.bucket,
objectKey: params.objectKey,
versionId: params.versionId,
error: err.message,
});
return this._resolveLifecycleMetricLocationFromArchiveInfo(entry, fallbackLocation, log, cb);
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 make the call to getObjectMD if we already had the data in entry ?

}

const metadataLocation = resolveLifecycleMetricObjectLocation(objectMD, fallbackLocation);
if (!shouldResolveLifecycleMetricLocation(metadataLocation)) {
return cb(null, metadataLocation);
}
return this._resolveLifecycleMetricLocationFromArchiveInfo(entry, metadataLocation, log, cb);
}, false);
}

_resolveLifecycleMetricLocation(entry, fallbackLocation, log, cb) {
Copy link
Copy Markdown
Contributor

@SylvainSenechal SylvainSenechal May 22, 2026

Choose a reason for hiding this comment

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

i feel like something is off around here :
We have
_resolveLifecycleMetricLocation calling

  • _resolveLifecycleMetricLocationFromBucket
  • _resolveLifecycleMetricLocationFromMetadata which calls these 2
  • _resolveLifecycleMetricLocationFromArchiveInfo
  • _resolveLifecycleMetricLocationFromBucket

So Frombucket can be called twice, and we have something else smelly : the if entry action type == deleteMpu is checked in this function, but also in _resolveLifecycleMetricLocationFromMetadata

I have a feeling it would be better to have a single function, even if larger, that can be read more naturally with less indirection.

Discussed this with Claude and providing his answer here :

Image

if (!shouldResolveLifecycleMetricLocation(fallbackLocation)) {
return process.nextTick(cb, null, fallbackLocation);
}

if (entry.getActionType() === 'deleteMPU') {
return this._resolveLifecycleMetricLocationFromBucket(entry, fallbackLocation, log, cb);
}

return this._resolveLifecycleMetricLocationFromMetadata(entry, fallbackLocation, log, cb);
}

/**
* Handles non-versioned objects
* @param {object} bucketData - bucket data
Expand Down Expand Up @@ -1103,16 +1269,18 @@ class LifecycleTask extends BackbeatTask {
return false;
}

_getObjectMD(params, log, cb) {
_getObjectMD(params, log, cb, logError = true) {
this.backbeatMetadataProxy.getMetadata(params, log, (err, blob) => {
if (err) {
log.error('failed to get object metadata', {
method: 'LifecycleTask._getObjectMD',
error: err,
bucket: params.bucket,
objectKey: params.objectKey,
versionId: params.versionId,
});
if (logError) {
log.error('failed to get object metadata', {
method: 'LifecycleTask._getObjectMD',
error: err,
bucket: params.bucket,
objectKey: params.objectKey,
versionId: params.versionId,
});
}
return cb(err);
}
const { error, result } = ObjectMD.createFromBlob(blob.Body);
Expand Down Expand Up @@ -1580,12 +1748,16 @@ class LifecycleTask extends BackbeatTask {
params.VersionId = obj.VersionId;
}
const command = new HeadObjectCommand(params);
attachArchiveInfoHeader(command);
attachReqUids(command, log.getSerializedUids());
return this.s3target.send(command)
.then(data => {
LifecycleMetrics.onS3Request(log, 'headObject', 'bucket', null);
const object = Object.assign({}, obj,
{ LastModified: data.LastModified });
{
LastModified: data.LastModified,
StorageClass: data.StorageClass || obj.StorageClass,
});

// There is an order of importance in cases of conflicts
// Expiration and NoncurrentVersionExpiration should be priority
Expand Down
50 changes: 50 additions & 0 deletions extensions/lifecycle/util/lifecycleLocation.js
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.

Can discuss with other reviewers : its a bit annoying to do one more pr but, should all these functions belongs to Arsenal ?

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

const STANDARD_LOCATION = 'STANDARD';

function _getObjectMDValue(objectMD, getterName, fieldName) {
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.

Haven't digged too much into this, but I'm surprised we have to create a helper function for this, since there must already be some existing code trying to access object metadatas, I wonder how it's being done ?

Don't we have getters functions in Arsenal for this already ?

Looking a bit more into it : The function is only called locally from resolveLifecycleMetricObjectLocation, we can check that objectMD is non nil directly at the beginning of resolveLifecycleMetricObjectLocation,
then when it's not nil, I think we don't need this helper and can directly call
objectMD.getArchive()
objectMD..getDatastoreName()
etc

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I agree with Sylvain on this one, something is not clear

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think the idea is to be able to manage when the value are wrapped in a class (Arsenal) or when it's a raw data. But we should try to always manipulate class ?

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 possible. When you call a function, you MUST know what the parameters are : and then you implement the way which is needed, instead of trying to cope with various cases dynamically...

if (!objectMD) {
return undefined;
}
if (typeof objectMD[getterName] === 'function') {
return objectMD[getterName]();
}
return objectMD[fieldName];
}

function isRealLocation(location) {
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.

Still reading the pr, but I dont fully understand the link between the function name and what its doing.

Basically checking that a given location is non null and not equal to 'STANDARD'. So why not name it "isStandardLocation" or "isNotStandardLocation" ?

Reading a bit further, I think the standard location is some kind of default value, and we try to resolve some locations, so maybe the intent is "isResolvedLocations" ? I think some of this can be documented by code comments too

Copy link
Copy Markdown
Contributor

@SylvainSenechal SylvainSenechal May 22, 2026

Choose a reason for hiding this comment

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

update : looking at the jira ticket, it gives more context : "In some cases, we have STANDARD as location in lifecycle metrics : as we get object information through regular S3 API."

I think this is the kind of context that could be added as code 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.

it is already a "real" location : shoud be isStandardLocation or isDefaultLocation

return !!location && location !== STANDARD_LOCATION;
}

function shouldResolveLifecycleMetricLocation(location) {
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.

after reading the pr some more, I think this is more a
needsResolveLifecycleMetricLocation
than
shouldResolveLifecycleMetricLocation

It makes the function calling it easier to understand what they are doing

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not sure we need a new function to just call an another one ?

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 resolve lifecycle metric location" is really just confusing, as their is no real notion behind : the code (in lifecycle) would be much easier to read without this abstraction

const location = entry.getAttribute('details.dataStoreName');
if (isDefaultLocation(location) ) {
    location = getDefaultBucketLocation(bucket);
}
[...]

return !isRealLocation(location);
}

function resolveLifecycleMetricObjectLocation(objectMD, fallbackLocation) {
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 find it a bit weird that in this function, we are calling isRealLocation on different object metadata attributes. Maybe its fine but it probably deserve some code comments to explain a bit more what its doing

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.

Can also explain why we only look at the first locations[0], of course future developers can always ask an llm to know (claude said its because mpu object have an array of locations, but all locations are the same so its ok to look at the first only) but I think its nicer to have a human written comment for things that can be confusing. Maybe here it does deserves a helper function like "getMpuObjectLocation" or more generic "getObjectLocation" that handle both mpu and non mpu objects

const archive = _getObjectMDValue(objectMD, 'getArchive', 'archive');
const amzStorageClass = _getObjectMDValue(objectMD, 'getAmzStorageClass', 'x-amz-storage-class');
if (archive && isRealLocation(amzStorageClass)) {
return amzStorageClass;
}

const dataStoreName = _getObjectMDValue(objectMD, 'getDataStoreName', 'dataStoreName');
if (isRealLocation(dataStoreName)) {
return dataStoreName;
}

const locations = _getObjectMDValue(objectMD, 'getLocation', 'location');
const locationDataStoreName = Array.isArray(locations) && locations[0]
&& locations[0].dataStoreName;
if (isRealLocation(locationDataStoreName)) {
return locationDataStoreName;
}

return fallbackLocation;
}

module.exports = {
STANDARD_LOCATION,
isRealLocation,
shouldResolveLifecycleMetricLocation,
resolveLifecycleMetricObjectLocation,
};
13 changes: 13 additions & 0 deletions tests/unit/lifecycle/LifecycleBucketProcessorPolicy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require('assert');

const bucketProcessorPolicy = require('../../../extensions/lifecycle/bucketProcessor/policy.json');

describe('LifecycleBucketProcessor policy', () => {
it('should allow S3 actions required for lifecycle metric location resolution', () => {
const actions = bucketProcessorPolicy.Statement
.find(statement => statement.Sid === 'LifecycleExpirationBucketProcessor')
.Action;

assert(actions.includes('s3:GetBucketLocation'));
});
});
Loading
Loading