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

Improve performance of AttachResourceConfigTransformer on big graphs #35088

Merged
merged 1 commit into from Apr 29, 2024

Conversation

alexott
Copy link
Contributor

@alexott alexott commented Apr 26, 2024

The current implementation of AttachResourceConfigTransformer has performance of O(N^2) to the number of vertices in the graph, and this leads to significant performance degradation on huge graphs.

The given implementation decreases the complexity to O(N) by performing direct lookup of a resource in the ManagedResources or DataResources depending on the object type.

Fixes #35087

Target Release

1.9.x

Draft CHANGELOG entry

NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES | EXPERIMENTS

Copy link

hashicorp-cla-app bot commented Apr 26, 2024

CLA assistant check
All committers have signed the CLA.

@crw
Copy link
Collaborator

crw commented Apr 26, 2024

Thanks for this submission! I'll raise it in triage, to see if we can review it for inclusion. Thanks!

Note, please sign the CLA when you get a chance per the above comment.

@alexott
Copy link
Contributor Author

alexott commented Apr 26, 2024

The use case is a bit of an outlier; it's more related to the migrations where we have many thousands of resources, like, Databricks directory, etc.

Before the change:

2024-04-26T13:34:57.445+0200 [DEBUG] AttachResourceConfigTransformer: start transform. 71966 vertices in the graph
2024-04-26T13:38:03.860+0200 [DEBUG] AttachResourceConfigTransformer: transform is finished
2024-04-26T13:38:15.777+0200 [DEBUG] AttachResourceConfigTransformer: start transform. 71966 vertices in the graph
2024-04-26T13:41:17.448+0200 [DEBUG] AttachResourceConfigTransformer: transform is finished
2024-04-26T13:41:43.523+0200 [DEBUG] AttachResourceConfigTransformer: start transform. 143932 vertices in the graph
2024-04-26T13:47:47.509+0200 [DEBUG] AttachResourceConfigTransformer: transform is finished

After the change:

2024-04-26T13:53:34.208+0200 [DEBUG] AttachResourceConfigTransformer: start transform. 71966 vertices in the graph
2024-04-26T13:53:34.393+0200 [DEBUG] AttachResourceConfigTransformer: transform is finished
2024-04-26T13:53:45.527+0200 [DEBUG] AttachResourceConfigTransformer: start transform. 71966 vertices in the graph
2024-04-26T13:53:45.755+0200 [DEBUG] AttachResourceConfigTransformer: transform is finished
2024-04-26T13:54:08.622+0200 [DEBUG] AttachResourceConfigTransformer: start transform. 143932 vertices in the graph
2024-04-26T13:54:09.070+0200 [DEBUG] AttachResourceConfigTransformer: transform is finished

The current implementation of `AttachResourceConfigTransformer` has performance of `O(N^2)` to the number of vertices in the graph, and this leads to significant performance degradation on huge graphs.

The given implementation decreases the complexity to `O(N)` by performing direct lookup of a resource in the `ManagedResources` or `DataResources` depending on the object type.

Signed-off-by: Alex Ott <alexott@gmail.com>
@jbardin
Copy link
Member

jbardin commented Apr 29, 2024

Thanks @alexott! This was never looked at for optimization, because you normally overwhelm other parts of the system long before this becomes a bottleneck. Is there anything particularly unusual about the configuration, other than having a huge number of individual resources blocks?

@alexott
Copy link
Contributor Author

alexott commented Apr 29, 2024

Just big number of resources in the plan

Copy link
Collaborator

@nfagerlund nfagerlund left a comment

Choose a reason for hiding this comment

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

This seems like a perfectly sound change, regardless of whether the performance problem you saw can be widely observed or not. Maps are for fetching stuff by key, so I'm in favor of fetching stuff out of the map by key. 😎

continue
}

coord := addr.Resource.String()
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's "coord" mean in this context? 😅

I guess it doesn't matter, since we just use it as a map key on the next line and never observe it again, but the name was a bit disorienting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about it as about coordinate, but key could be a good name as well

if config.Module.ProviderMetas == nil {
log.Printf("[TRACE] AttachResourceConfigTransformer: no provider metas defined for %s", dag.VertexName(v))
continue
if config.Module.ProviderMetas != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This commit deletes two additional nil checks (config and config.Module) — for posterity's sake, I'll note that they were both, indeed, useless. 👍🏼 (There's already an early exit on nil config above, and the existing code was already assuming that config.Module is never nil when it iterated over two of Module's fields.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yex. The config deletion was highlighted by VSCode itself

@nfagerlund nfagerlund merged commit f2906a1 into hashicorp:main Apr 29, 2024
5 of 6 checks passed
Copy link

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@crw
Copy link
Collaborator

crw commented Apr 29, 2024

@alexott @nfagerlund Just an FYI there is not currently a Changelog entry submitted for this change, I'd imagine one will be necessary for an eventual release. Thanks!

@nfagerlund
Copy link
Collaborator

I added one :)

@alexott alexott deleted the attach-config-performance branch April 30, 2024 06:00
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.

Performance degradation of AttachResourceConfigTransformer on big graphs
4 participants