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

remove extra state.DeepCopy from updateStateHook #35164

Merged
merged 1 commit into from
May 16, 2024

Conversation

jbardin
Copy link
Member

@jbardin jbardin commented May 15, 2024

All state implementations copy the given state when storing it to temporary or permanent storage1234, either explicitly with DeepCopy, or implicitly by serializing the state. Since updateStateHook is called with every resource instance state change, a second call to DeepCopy with that hook doubles the overhead of PostStateUpdate which may be a significant portion of the running time.

Since all Hook implementations which handle PostStateUpdate (which is exactly 1 in non-test code5) only call state methods that eventually copy the state, we can remove the extra copy from the updateStateHook itself. The state was already locked for the duration of the PostStateUpdate call previously, so no additional delay should result.

For consistency this also updates the documentation for PostStateUpdate to record the new caller and callee expectations based on the existing implementation. The Hook interface predated the statemgr which provides a better interface for synchronizing state access and therefor required extra copying at that time, while now we can more easily declare that the method requires concurrent access be locked for the duration of the call.

Fixes #35113

Target Release

v1.9

Footnotes

  1. https://github.com/hashicorp/terraform/blob/0efd586490cd84aa735517c291c2b5ad63e0b009/internal/cloud/state.go#L150

  2. https://github.com/hashicorp/terraform/blob/716fcce2397c996893eff7cfa6d02a9f944e1bc0/internal/states/remote/state.go#L85

  3. https://github.com/hashicorp/terraform/blob/716fcce2397c996893eff7cfa6d02a9f944e1bc0/internal/states/statemgr/filesystem.go#L126

  4. https://github.com/hashicorp/terraform/blob/53c34ff49cfbc1f70d7cdd3dca8040551c53737a/internal/states/statemgr/transient_inmem.go#L38

  5. https://github.com/hashicorp/terraform/blob/f058de612c56066803aa46b2ec7bfec8181acd76/internal/backend/local/hook_state.go#L42

All production state implementations copy the given state when storing
it to temporary or permanent storage, either explicitly with DeepCopy,
or implicitly by serializing the state. Since `updateStateHook` is
called with every state change, a second call to `DeepCopy` with that
hook doubles the overhead of `PostStateUpdate`.

Since all Hook implementations which handle `PostStateUpdate` (which is
exactly 1 in non-test code) only call state methods that eventually copy
the state, we can remove the extra copy from the `updateStateHook`
itself. The state was already locked for the duration of the
`PostStateUpdate` call previously, so no additional delay should result.

For consistency this also updates the documentation for
`PostStateUpdate` to record the new caller and callee expectations based
on the existing implementation. The Hook interface predated the
`statemgr` which provides a better interface for synchronizing state
access and therefor required extra copying at that time, while now we
can more easily declare that the method requires concurrent access be
locked for the duration of the call.
@alexott
Copy link
Contributor

alexott commented May 15, 2024

That’s great! Thank you very much! We initially looked into updateStateHook, but weren’t sure that all implementations will have their own deep copy…

@crw crw added the bug label May 15, 2024
Copy link
Contributor

@alexott alexott left a comment

Choose a reason for hiding this comment

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

Looks great! Thank you very much

Copy link

@mgyucht mgyucht left a comment

Choose a reason for hiding this comment

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

Thank you!

@jbardin jbardin merged commit 72c2597 into main May 16, 2024
6 checks passed
@jbardin jbardin deleted the jbardin/remove-state-copies branch May 16, 2024 12:50
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Excessive DeepCopy usage in remote state backend leads to performance degradation
5 participants