-
Notifications
You must be signed in to change notification settings - Fork 23
BB-721: Properly get location in lifecycle metrics #2736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: improvement/BB-740-monitor-lifecycle-conductor
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ const { | |
| GetObjectTaggingCommand, | ||
| HeadObjectCommand, | ||
| GetBucketVersioningCommand, | ||
| GetBucketLocationCommand, | ||
| } = require('@aws-sdk/client-s3'); | ||
| const { attachReqUids } = require('@scality/cloudserverclient'); | ||
| const config = require('../../../lib/Config'); | ||
|
|
@@ -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. | ||
|
|
@@ -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 | ||
|
|
@@ -104,6 +123,7 @@ class LifecycleTask extends BackbeatTask { | |
|
|
||
| this.setSupportedRules(this.supportedRules); | ||
| this._totalRetries = 0; | ||
| this._bucketLocationCache = new Map(); | ||
| } | ||
|
|
||
| setSupportedRules(supportedRules) { | ||
|
|
@@ -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'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of the extra metadata call, could we change one (existing) call? |
||
| 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 }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a "fallback" location... |
||
| if (!this.backbeatMetadataProxy) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need 3 cases?
|
||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i feel like something is off around here :
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 :
|
||
| 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 | ||
|
|
@@ -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); | ||
|
|
@@ -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 | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with Sylvain on this one, something is not clear There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is already a "real" location : shoud be |
||
| return !!location && location !== STANDARD_LOCATION; | ||
| } | ||
|
|
||
| function shouldResolveLifecycleMetricLocation(location) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after reading the pr some more, I think this is more a It makes the function calling it easier to understand what they are doing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| return !isRealLocation(location); | ||
| } | ||
|
|
||
| function resolveLifecycleMetricObjectLocation(objectMD, fallbackLocation) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| }; | ||
| 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')); | ||
| }); | ||
| }); |

There was a problem hiding this comment.
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