backend/s3: optimize StateMgr workspace existence check#38214
Open
joewrightfd wants to merge 2 commits intohashicorp:mainfrom
Open
backend/s3: optimize StateMgr workspace existence check#38214joewrightfd wants to merge 2 commits intohashicorp:mainfrom
joewrightfd wants to merge 2 commits intohashicorp:mainfrom
Conversation
Replace O(n) ListObjectsV2 pagination with O(1) HeadObject call when checking if a workspace state file exists in StateMgr. Previously, StateMgr called Workspaces() which listed ALL objects matching the workspace prefix, then linearly searched the results. With 15,000 workspaces this meant 15 API calls and 15,000 string comparisons just to check if one workspace exists. Now we call HeadObject directly on the specific state file path. Benchmark with 15,000 workspace objects: - Before: 14.4s avg (15 paginated ListObjectsV2 calls) - After: 10.9s avg (1 HeadObject call) - Improvement: 24-33% faster
Contributor
|
Thanks for this submission! The S3 backend is maintained by the AWS Provider team at HashiCorp, an IBM company. They review PRs infrequently, as their schedule allows. Please expect any updates to come from someone on that team. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a performance improvement for
terraform initfor the S3 backend when there are a large (10k+) amount of workspaces in that bucket.Issue: #33137
Optimization
Replace O(n) ListObjectsV2 pagination with O(1) HeadObject call when checking if a workspace state file exists in StateMgr.
Previously, StateMgr() called Workspaces() which listed all objects matching the workspace prefix (
env:), then linearly searched the results. With 15,000 workspaces this meant 15 API calls and 15,000 string comparisons just to check if one workspace exists.Now we call HeadObject directly on the specific state file path since we have enough information to know the exact path we need.
Benchmark
The "After" time remains constant (~12s) regardless of workspace count, while "Before" scales linearly.
The time saved scales linearly with workspace count - patched version stays constant while the original grows with O(n) pagination.
Steps to Reproduce
Target Release
1.15.x
Rollback Plan
Changes to Security Controls
Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.
None.
CHANGELOG entry