Skip to content

Optimize upload of deployment artifacts - attempt #2 - #9876

Closed
remi00 wants to merge 15 commits into
serverless:mainfrom
remi00:hash-artifacts-versioning-initial
Closed

remi00 wants to merge 15 commits into
serverless:mainfrom
remi00:hash-artifacts-versioning-initial

Conversation

@remi00

@remi00 remi00 commented Aug 24, 2021

Copy link
Copy Markdown
Contributor

Closes: #8666

What it does?

  • Change the artifacts naming scheme to be using content hash
  • Precede any S3.upload with S3.headObject to avoid re-upload of already existing S3 artifacts
  • Adjust the method of reading last n (by default 5) deployments to support both legacy and new artifacts naming scheme
  • Modify the package metadata: extract the timestamp value used for upload paths as it is now used just for compiled Cloudformation template files (and in future maybe also for remote serverless state file(s))

What else is important?

  • Changes are large but (1) can be easily read commit-by-commit and (2) we can split it in two stacked PRs if necessary.
  • In general, implementation follows a technical solution proposal described within Optimise upload of deployment artifacts #8666. However, one thing is different: artifacts S3 paths are not stored within local serverless state and instead is re-calculated during upload
    • The argument for picked approach is simplicity and amount of changes. Using serverless state would require to take more architectural decisions (mostly related on how to collect the path mappings?) and probably establishing some conventions in order to follow (Cleanly separate Packaging and Deployment phases #8499 is more than just this change).

More changes to come in upcoming PRs:

  • Cleanup to implementation: layers compilation and checkForChanges now can be significantly simplified. Not included here to reduce the amount of single PR changes.
  • Some guideline notes should be added to provide information how to make sure to take advantage of the new mechanism: that the artifacts ZIP files that will be deterministic and setting individually: true can significantly reduce the deployment duration, especially in CI/CD use cases.

remi00 added 7 commits August 24, 2021 11:34
This is prerequisite change. Since we change the S3 artifacts structure,
not all files will get uploaded to directory with the timestamp.
Also move it to common aws plugin utils directory
This one is based on listing Cloudformation template files from
the last n [5] deployments and reading associated artifacts from
its resources.
To be used within the commands:
- deploy list, deploy (s3 cleanup phase) and rollback
@medikoo

medikoo commented Aug 24, 2021

Copy link
Copy Markdown
Contributor

@remi00 thanks for new PR! Is this assumed as ready for review? If so can you check CI fails?
Otherwise can you convert it to draft, and once it's ready simply ping us in comment.

Thank you!

@codecov

codecov Bot commented Aug 24, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #9876 (868a243) into master (0682ba8) will increase coverage by 0.04%.
The diff coverage is 98.71%.

❗ Current head 868a243 differs from pull request most recent head ef3afce. Consider uploading reports for the commit ef3afce to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9876      +/-   ##
==========================================
+ Coverage   86.20%   86.24%   +0.04%     
==========================================
  Files         329      329              
  Lines       12811    12858      +47     
==========================================
+ Hits        11044    11090      +46     
- Misses       1767     1768       +1     
Impacted Files Coverage Δ
lib/cli/interactive-setup/dashboard-set-org.js 95.20% <ø> (ø)
lib/plugins/aws/package/lib/saveServiceState.js 100.00% <ø> (ø)
scripts/serverless.js 53.10% <ø> (ø)
lib/classes/Variables.js 99.46% <75.00%> (-0.27%) ⬇️
lib/plugins/aws/deploy/lib/uploadArtifacts.js 90.80% <95.45%> (+1.33%) ⬆️
lib/cli/interactive-setup/dashboard-login.js 88.88% <100.00%> (ø)
...on/variables/sources/instance-dependent/get-sls.js 100.00% <100.00%> (ø)
lib/plugins/aws/customResources/index.js 98.52% <100.00%> (-0.05%) ⬇️
lib/plugins/aws/deploy/index.js 100.00% <100.00%> (ø)
lib/plugins/aws/deploy/lib/checkForChanges.js 98.60% <100.00%> (+0.10%) ⬆️
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0682ba8...ef3afce. Read the comment docs.

@remi00

remi00 commented Aug 24, 2021

Copy link
Copy Markdown
Contributor Author

@medikoo some broken tests which was not expected... Apologies for that. I can't find the capability to switch the PR into draft once it's created. Maybe I miss the permission. Hopefully I will address the test issues shortly.

@medikoo
medikoo marked this pull request as draft August 24, 2021 11:33
@medikoo

medikoo commented Aug 24, 2021

Copy link
Copy Markdown
Contributor

@remi00 not a problem, again great thanks for working on this, as it's a highly anticipated optimization we'd love to have in.

I've converted it to draft. Let us know, once you feel it's ready, and if you need any help, don't hesitate to ask.

@remi00
remi00 force-pushed the hash-artifacts-versioning-initial branch from c2f67e9 to 58d911d Compare August 24, 2021 13:05
@remi00
remi00 force-pushed the hash-artifacts-versioning-initial branch from 58d911d to 4cb5090 Compare August 24, 2021 13:14
@remi00
remi00 marked this pull request as ready for review August 24, 2021 13:21
@remi00

remi00 commented Aug 24, 2021

Copy link
Copy Markdown
Contributor Author

@medikoo Tests are now ✅ PR ready for review ☺

Comment thread lib/plugins/aws/lib/findDeployments.js Outdated

const BbPromise = require('bluebird');

const MAX_CONCURRENT_S3_DOWNLOADS = Number(process.env.SLS_MAX_CONCURRENT_S3_DOWNLOADS) || 7;

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.

Do we need to introduce an option for this? Or could we go with a sane default?

(any extra option is BC surface, as well as documentation, so if we can avoid it that's best :) )

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 removed this env variable 👍

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

@remi00 great thanks for this PR!

Please see my comments, I think we can minimize needed set of changes, and ideally would be that we go as close to spec as possible, Ifyou feel that we need to derive from it, can we first discuss it (?) with this approach I think we may save time on producing unnecessary changes.

Anyway, great work! Let me know your thoughts

let params = {
Bucket: this.bucketName,
Key: `${this.serverless.service.package.artifactDirectoryName}/${compiledTemplateFileName}`,
Key: `${deploymentDirectoryPrefix}/${timestamp}/${compiledTemplateFileName}`,

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 we should not do any changes here (it's about storing CF template, and we do not alter this location in any way)

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.

It is about the fact that previously artifactDirectoryName included the timestamp. E.g.:
serverless/test/2021-10-21T20:12:00
Now, CFN template lands in the same place. But other artifacts and pieces land in
serverless/test/code-artifacts
Common part of both paths is now deploymentDirectoryPrefix and timestamp is just timestamp.

Presumably, we could just generate and store the timestamp, and create the destination S3 prefix ad hoc (when needed) with serviceName and getStage(). WDYT?

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.

We actually cannot change value or remove this.serverless.service.package.artifactDirectoryName property, as it's used by some plugins (just checked that).

To avoid confusion we may stop using internallythis.serverless.service.package.artifactDirectoryName (it'll be just good to add comment above it's assignment that's left for legacy purposes), and maybe introduce a more accurate:

serverless.getProvider('aws').s3CodeArtifactsDirectoryPath = `${prefix}/${service}/${stage}/code-artifacts`
serverless.getProvider('aws').s3DeploymentDirectoryPath = `${prefix}/${service}/${stage}/${timestamp}`

Still I would precede such change with a separate PR, that:

  • Introduces serverless.getProvider('aws').s3DeploymentDirectoryPath
  • Replaces service.package.artifactDirectoryName usage with serverless.getProvider('aws').s3DeploymentDirectoryPath

WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks a lot for checking with plugins. I will apply the change as suggested within a dedicated PR preceding this one.

Comment on lines 83 to 84
const data = fs.readFileSync(artifactFilePath);
const fileHash = crypto.createHash('sha256').update(data).digest('base64');

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 will be nice to introduce (with other PR) a refactor where we rely on memoized getHashForFilePath util

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.

Sounds like a good idea. Other PR sounds good too :)

Comment thread lib/plugins/aws/provider.js Resolved
Comment thread lib/plugins/aws/lib/findDeployments.js Outdated
@@ -0,0 +1,63 @@
'use strict';

const BbPromise = require('bluebird');

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.

Let's stick to native promises

Comment thread lib/plugins/aws/lib/findDeployments.js Outdated
}
},
{
concurrency: MAX_CONCURRENT_S3_DOWNLOADS,

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.

Once we will limit the amount of CF stacks we check, to inspect them only when needed, I think there's no need for extra environment flag to control that.

If users will stick to default of 5 versions max, it will be at most 5 request, which I think can be done in parallel. Still just to be on safe side I would probably still limit the concurrency, to hardocded 5, and let's limit it with ext/promise/limit

Bucket: this.bucketName,
Key: key,
};
await this.provider.request('S3', 'headObject', params);

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 guess it's more efficient to resolve all file names with one call and confirm against that, instead of issuing AWS request per each file name

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.

As in my previous response, changing this sounds like premature optimization. AWS SDK for JS is by far robust. It reuses connections from HTTP pool, and since we limit number of simultaneous upload processes, it takes advantage of that mechanism very nicely. Doing headObject is seamless. Checked within the project using Serverless with more than 25 Lambda functions.

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.

@remi00 ok, so situation with AWS SDK, is that it creates some sort of long-polling connection? and then works in similar way as e.g. SSE on browser side?

What are the observed times of above requests when done locally? (e.g. if you read time at 59 line and then check difference at 61 or 63, what difference you will observe)

Note that reusing collection of obtained names will make this logic also simpler, this try/catch looks a bit convoluted

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I will revisit this suggestion.

Here some excerpt from the logs as advised. No patterns on the length I think.

serverless/***/dev/code-artifacts/83f3ab22220623a3e0a1c1006019aa0ffabf38f4a45968dfc4bc4502c7ab61d0.zip 218
serverless/***/dev/code-artifacts/83f3ab22220623a3e0a1c1006019aa0ffabf38f4a45968dfc4bc4502c7ab61d0.zip 176
serverless/***/dev/code-artifacts/b511cc239fc18dc2d18346fe920c3d0e121b91d20c81ac618c1d7b0f769959e7.zip 117
serverless/***/dev/code-artifacts/485c82da1d982767d9eb21fade33ba806a7abd8dd771ef3de8543eb31629f669.zip 288
serverless/***/dev/code-artifacts/1b833dd49100cb6c9e36b6c6d20415e7c466d6bb5f6923140cc819b9abc04103.zip 230
serverless/***/dev/code-artifacts/dffd763a7238dbe18460add36070d3209ddac83cba220d3f5ecd8022a390601d.zip 153
serverless/***/dev/code-artifacts/da861085fbf6494e427b50f32f9c927e7464d8e520c9713d5f09b7cc54ab8808.zip 204
serverless/***/dev/code-artifacts/575d4ad4cec1c94d98e10093db22647a03bef195ba8af3cfedd5d5aac23f57af.zip 161
serverless/***/dev/code-artifacts/d74729b6433436d3cceb680e14beec0395de1ff8caec18a2281839fc11cb7424.zip 234
serverless/***/dev/code-artifacts/b63d6a4580589d317eeb70fc42a9af30f84fdb24cab4942231107cf83c5618c7.zip 175
serverless/***/dev/code-artifacts/e4b3bac7adf8c7225cb85f1e325035c54a619da6826c300684a787f83c3e12e2.zip 259
serverless/***/dev/code-artifacts/8ba0344dbe71bc345b24578f1c47bfac539d4cdb39fe28f82db1bf2977add7e2.zip 172
serverless/***/dev/code-artifacts/77439124b0d653e31791603625324e307caeb512f6a4e56f741ec22823714f2d.zip 265
serverless/***/dev/code-artifacts/54894d0ef4600e96d1daccd3b7f7044e594875e2f517876500dc2e2041d77ac1.zip 159
serverless/***/dev/code-artifacts/fc90c0887ffb90dcd665f75b34fb6217d79af15dd933d2abe2205918e8db6172.zip 252
serverless/***/dev/code-artifacts/bd026b838d11e1eb1dc94ba15cf4fe8d69914418a6526aee00e54daed314d40f.zip 222
serverless/***/dev/code-artifacts/3ada44b12dcb546cd20822d72299e60ea65ed0afe34b87af5c43c61bfdc184a0.zip 358
serverless/***/dev/code-artifacts/61462cfdf3d771a3db7ec9800e662a3adc628703e19d9937979639cbbf030c4b.zip 173
serverless/***/dev/code-artifacts/49219f38ee34b704f85b97c703254f4a221daa0df66d057a0725bc0922c508f5.zip 245
serverless/***/dev/code-artifacts/41ee01fc2e0ba0a24754b5fd0b9e624e8da4643f926fd2ac629312e8590d01cb.zip 146
serverless/***/dev/code-artifacts/ca1c96a41c510f746cb672a4754814e3adeaf7f8c7e0d3e4988bdb88c7779054.zip 241
serverless/***/dev/code-artifacts/351e89375570a91b23b6c95b395adbe9a1ba0f19930cc44e7521bf54f19809b5.zip 127
serverless/***/dev/code-artifacts/3fd826bcaa3151bc6686d6fc71ffa22f217d5f3fa36aeefcf24ccf20078a1fbc.zip 224
serverless/***/dev/code-artifacts/d16fbf824d82059a66774daa640b0eb86a5959547597357646a8711a09005029.zip 127
serverless/***/dev/code-artifacts/43aac618fcbdc02de0c3c305c066304d161007671a33f52b2a031e79da77ca56.zip 232
serverless/***/dev/code-artifacts/793a9307b335f1c3983c3a0772bdde95cd9f13f6a1d2749c8e7f2990c79f059d.zip 127
serverless/***/dev/code-artifacts/f1277ced42f21780f1bb700c83e527e841cf5cf86568867059de5bb51dfb6fc3.zip 231
serverless/***/dev/code-artifacts/9191bc6f87cc446b5c1443311b674ca0699ec8004a702a5fdd3c64ef8f79704f.zip 148
serverless/***/dev/code-artifacts/17c0bfd498cd9e1099893c7239d541466db18d979f192bacdd1bebc0e3357ce1.zip 150
serverless/***/dev/code-artifacts/799693245bf0be41ec88c98a9b6abb29bc23d39bf75fb9f994ef98b95020548d.zip 333
serverless/***/dev/code-artifacts/5a047b1f7d7740e90d2c70c1d6dba9c08818423c845a88bdb6505a5608e7ad7c.zip 315
serverless/***/dev/code-artifacts/4d4b58c3a0669b8aa0651926bc43fa7d9def1d709b6df22b1492f0eecb470604.zip 119
serverless/***/dev/code-artifacts/c3bba113d4fa343160618174c07f5558386f00bffe6a78d4c55aac0afe77743d.zip 117
serverless/***/dev/code-artifacts/70b74fd9325fbeaa9d358db1c462ce2c763ccce00c0044c595182627ef5257ae.zip 236
serverless/***/dev/code-artifacts/4117fda13f45243c05b873400f1793863f03abde1417bf36eb63e6a88cf8aa69.zip 288
serverless/***/dev/code-artifacts/15687690138badea2a00a73a72cfe55444e1b3d807688614a9aa0db657477706.zip 193
serverless/***/dev/code-artifacts/1f446e0670f8b80edf91de08684bbfcab2779e82486037b09787498d1ade3868.zip 219
serverless/***/dev/code-artifacts/fa28e148a46344b92cfad5072d689b05239cf60381fabeaa0b1c4e375b6882b5.zip 140
serverless/***/dev/code-artifacts/82e85fa46cfc7824ba73d80675aa17dfdc5cdac36c241065cadd4828eee4f8cc.zip 223
serverless/***/dev/code-artifacts/7b42199932a71a568a8bc009b4c73e38075f4a520b00740dcba77d023983069e.zip 140
serverless/***/dev/code-artifacts/e622158507b1b09ab1dd38ec8a32deaeff5ecc5df747d64b31d45509ed3b728a.zip 212
serverless/***/dev/code-artifacts/7452b7ed022dccfe826c1525bcd8dde91c52ec0f49a8602338aecaca66f6dd0b.zip 139
serverless/***/dev/code-artifacts/4ab258938a9fe37d835411179248303cb3888e6d0525f0305902346179e32ea5.zip 208
serverless/***/dev/code-artifacts/336fb9ecd70f00118f67d2be150b4fd924f5d8cf5cc517ab5c03d476f350fb31.zip 131

const validate = require('./lib/validate');
const findAndGroupDeployments = require('./utils/findAndGroupDeployments');
const setBucketName = require('./lib/setBucketName');
const findDeployments = require('./lib/findDeployments');

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 we do not need any changes here (?)

const compiledTemplateFileName = this.provider.naming.getCompiledTemplateS3Suffix();
const s3Endpoint = getS3EndpointForRegion(this.provider.getRegion());
const templateUrl = `https://${s3Endpoint}/${this.bucketName}/${this.serverless.service.package.artifactDirectoryName}/${compiledTemplateFileName}`;
const { deploymentDirectoryPrefix, timestamp } = this.serverless.service.package;

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.

We don't need any changes here I think (?)

@@ -0,0 +1,18 @@
'use strict';

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 introduction is not really necessary (?) What does it aid?

individually: this.serverless.service.package.individually,
artifactDirectoryName: this.serverless.service.package.artifactDirectoryName,
deploymentDirectoryPrefix: this.serverless.service.package.deploymentDirectoryPrefix,
timestamp: this.serverless.service.package.timestamp,

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.

Per spec initially we agreed simply to add list file hash list (still I've proposed to simplify that in last comment in an issue). What's the reasoning behind different approach?

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.

As part of the rework I eventually added the artifactsMap. My previous idea to rely on compiled Cloudformation template failed due to the inconsistency between layers and lambda functions and also separate approach to custom resources. I think I found relatively lightweight approach to store the artifacts mapping as you initially suggested. Please take a look on added commits.

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.

My previous idea to rely on compiled Cloudformation template failed due to the inconsistency between layers and lambda functions and also separate approach to custom resources

Do you mean that it's difficult to retrieve generated paths for those resources from CF template?

Ideally if any deviations from specifications are signaled with proper reasoning at the origin issue: #8666

Flow we usually practice (documented at CONTRIBUTING.md) is that first we agree on implementation spec and it's after that when we jump to implementation.

I understand that some things are hard to resolve without actually trying to implement the solution, but in such case best if outcome of a tries is signaled at the issue spec with comments as e.g. X is probably better to be approached via Y, due A and B, and not proposed via PR's.

Currently this PR derives from spec in many places and it's hard for me to understand why. If you feel that spec at #8666 is insufficient can you comment over there what's missing? It'll be great if over there we first clarify all details, and only after this PR matches the spec we jump into reviewing it.

@remi00
remi00 requested review from medikoo and mnapoli August 27, 2021 14:09
individually: this.serverless.service.package.individually,
artifactDirectoryName: this.serverless.service.package.artifactDirectoryName,
deploymentDirectoryPrefix: this.serverless.service.package.deploymentDirectoryPrefix,
timestamp: this.serverless.service.package.timestamp,

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.

My previous idea to rely on compiled Cloudformation template failed due to the inconsistency between layers and lambda functions and also separate approach to custom resources

Do you mean that it's difficult to retrieve generated paths for those resources from CF template?

Ideally if any deviations from specifications are signaled with proper reasoning at the origin issue: #8666

Flow we usually practice (documented at CONTRIBUTING.md) is that first we agree on implementation spec and it's after that when we jump to implementation.

I understand that some things are hard to resolve without actually trying to implement the solution, but in such case best if outcome of a tries is signaled at the issue spec with comments as e.g. X is probably better to be approached via Y, due A and B, and not proposed via PR's.

Currently this PR derives from spec in many places and it's hard for me to understand why. If you feel that spec at #8666 is insufficient can you comment over there what's missing? It'll be great if over there we first clarify all details, and only after this PR matches the spec we jump into reviewing it.

@remi00

remi00 commented Aug 31, 2021

Copy link
Copy Markdown
Contributor Author

Currently this PR derives from spec in many places and it's hard for me to understand why. If you feel that spec at #8666 is insufficient can you comment over there what's missing? It'll be great if over there we first clarify all details, and only after this PR matches the spec we jump into reviewing it.

I appreciate this feedback and agree with that. I will take a step back and elaborate on the challenges faced within the issue thread.

@wwwehr

wwwehr commented Oct 20, 2021

Copy link
Copy Markdown
Contributor

Hi guys, it looks like this PR stalled. Can I help?

@remi00

remi00 commented Oct 20, 2021

Copy link
Copy Markdown
Contributor Author

Hi @wwwehr I am okay with that. We actually have to conclude this PR: #9904 first and then proceed with this one here. Would you like to address comments on #9904?

@wwwehr

wwwehr commented Nov 2, 2021

Copy link
Copy Markdown
Contributor

Hi @wwwehr I am okay with that. We actually have to conclude this PR: #9904 first and then proceed with this one here. Would you like to address comments on #9904?

@remi00 I looked at 9904 and started with trying to merge in latest master. It appears there's some upstream work that accomplishes what you were after... I think. Perhaps take a look and tell me what I might be able to assist with.

Best

@remi00

remi00 commented Nov 27, 2023

Copy link
Copy Markdown
Contributor Author

This is already far away from possibility of merging to main

@remi00 remi00 closed this Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimise upload of deployment artifacts

4 participants