Do not pass ProjectMetadata to lazy index permissions builder #135337
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.
During a serverless incident (INC-4832) that was caused by frequent OOM exceptions it was discovered that ~30% of the heap was occupied by
ProjectMetadatainstances.The

ProjectMetadatainstances were retained by a lambda inIndicesPermission, see this example of a path to gc root:The reason the lambda exists is to make the index access control lazy. Because the lambda is lazy, it will hold on to the reference to
ProjectMetadatafor the full request life cycle (as opposed to building the index permissions and dropping the reference). This becomes a problem when there are many concurrent searches (index actions requiring us to check index permissions) coupled with frequentProjectMetadataupdates. Since the lambda holds a reference toProjectMetadatait can't be garbage collected.I've proven this by:
TransportSearchActionto simulate slow searchesProjectMetadataupdates in between (triggered by creating new indices)ProjectMetadatawhile the searches are hanging (to simulate request in flight)Before any requests
While requests are in flight
Fix
To fix this issue I've moved the part that needed
ProjectMetadataoutside of the lambda.ProjectMetadatawas needed to resolved failure store indices. With this PR we will do some more work that #88708 tried to remove, but I think it's acceptable for the memory gain.To validate that this fixed the issue I ran the same test as above and could see that
ProjectMetadatacould be garbaged collected as soon as authorization was finished.