Skip to content
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

feat(aspects): priority-ordered aspect invocation #32097

Merged
merged 76 commits into from
Nov 29, 2024

Conversation

sumupitchayan
Copy link
Contributor

@sumupitchayan sumupitchayan commented Nov 12, 2024

Closes #21341

This feature was designed in RFC648

Reason for this change

The current algorithm for invoking aspects (see invokeAspects in synthesis.ts) does not handle all use cases — specifically, when an Aspect adds a new node to the Construct tree and when Aspects are applied out of order.

Description of changes

This PR introduces a priority-based ordering system for aspects in the CDK to allow users to control the order in which aspects are applied on the construct tree. This PR also adds a stabilization loop for invoking aspects that can be enabled via the feature flag @aws-cdk/core:aspectStabilization - the stabilization loop ensures that newly added Aspects to the construct tree are visited and nested Aspects are invoked.

Description of how you validated changes

Plenty of unit tests - see aspects.test.ts.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

…t tests to aspect.test.ts

Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
@github-actions github-actions bot added the p2 label Nov 12, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team November 12, 2024 14:01
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 12, 2024
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed add Clarification Request to a comment.

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

I think this PR implicitly changes iteration order of different aspects at the same priority level.

Haven't decided yet how big of a deal that is. In principle we don't make any guarantees either way, so one could argue that it doesn't matter. In practice, as usual, it could be that someone somewhere has taken a dependency on that specific order (if only by accident)

image

packages/aws-cdk-lib/core/lib/aspect.ts Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/aspect.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/aspect.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/private/synthesis.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/private/synthesis.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/private/synthesis.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/private/synthesis.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/private/synthesis.ts Outdated Show resolved Hide resolved
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Copy link

codecov bot commented Nov 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.46%. Comparing base (0e79874) to head (ed93f22).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #32097      +/-   ##
==========================================
+ Coverage   78.42%   78.46%   +0.04%     
==========================================
  Files         106      106              
  Lines        7208     7208              
  Branches     1323     1323              
==========================================
+ Hits         5653     5656       +3     
+ Misses       1368     1364       -4     
- Partials      187      188       +1     
Flag Coverage Δ
suite.unit 78.46% <ø> (+0.04%) ⬆️

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

Components Coverage Δ
packages/aws-cdk 78.46% <ø> (+0.04%) ⬆️

Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
@github-actions github-actions bot added bug This issue is a bug. effort/large Large work item – several weeks of effort p1 and removed p2 labels Nov 13, 2024
@sumupitchayan sumupitchayan marked this pull request as ready for review November 27, 2024 18:08
@sumupitchayan sumupitchayan requested a review from a team as a code owner November 27, 2024 18:08
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Good stuff! Nearly there. I have some comments on the documentation and some implementation cleanup, but after that I'm good to ship.

A question of a somewhat sizeable set of remaining work is how we're going to add priorities to our existing aspects. It needs to happen before our next release. But at the same time, applying priorities to all aspects we currently have the in code base probably changes infrastructure for someone somewhere.

Thoughts?

```ts
const MUTATING_PRIORITY = 200;
const READONLY_PRIORITY = 1000;
const DEFAULT_PRIORITY = 600;
Copy link
Contributor

Choose a reason for hiding this comment

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

💅 Nit: 500 seems like a more logical choice?

Also, these are not public constants, they are on a class, aren't they?

Copy link
Contributor

Choose a reason for hiding this comment

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

We should be applying correct priorities to all aspects we apply in the construct library. Are we doing that in this PR? If not, it should be an extremely fast follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, these are not public constants, they are on a class, aren't they?

Yes I'll fix this and will change it to 500

We should be applying correct priorities to all aspects we apply in the construct library. Are we doing that in this PR? If not, it should be an extremely fast follow.

I can do it here or in a fast-follow. To respond to your point above Aspect priorities changing infrastructure, I've searched the CDK repo for Aspect applications (Aspects.of(<scope>).add()) and every application I found seems to be a mutating Aspect - none that are validation/readonly.

So I'm not sure if that'll change customers' infrastructure ... (I guess it potentially could if we define these as mutating and they run before Aspects with the default priority value).

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's do it!

packages/aws-cdk-lib/core/README.md Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/README.md Show resolved Hide resolved
packages/aws-cdk-lib/core/README.md Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/README.md Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/lib/private/synthesis.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/test/aspect.prop.test.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/test/aspect.prop.test.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/core/test/aspect.prop.test.ts Outdated Show resolved Hide resolved
packages/aws-cdk-lib/cx-api/lib/features.ts Outdated Show resolved Hide resolved
@rix0rrr rix0rrr added the pr/do-not-merge This PR should not be merged at this time. label Nov 29, 2024
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

I'll give you a conditional approval given that you address everything in my previous comment

sumupitchayan and others added 9 commits November 29, 2024 10:50
Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
…n from README

Signed-off-by: Sumu <sumughan@amazon.com>
…ity being invoked on node

Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
Co-authored-by: Rico Hermans <rix0rrr@gmail.com>
…flag is enabled by default now

Signed-off-by: Sumu <sumughan@amazon.com>
Signed-off-by: Sumu <sumughan@amazon.com>
@sumupitchayan sumupitchayan removed the pr/do-not-merge This PR should not be merged at this time. label Nov 29, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: ed93f22
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented Nov 29, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 8ccdff4 into main Nov 29, 2024
17 checks passed
@mergify mergify bot deleted the sumughan/priority-ordered-aspects branch November 29, 2024 17:33
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/large Large work item – several weeks of effort p1 pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
3 participants