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

[Fleet] Avoid loading package saved objects into memory before deleting them #188004

Merged
merged 1 commit into from
Jul 12, 2024

Conversation

xcrzx
Copy link
Contributor

@xcrzx xcrzx commented Jul 10, 2024

Resolves: #187975

Summary

When upgrading or re-installing a package, all saved objects from a previous package are loaded into memory using bulkResolve. This creates unnecessary memory pressure for packages containing thousands of saved objects, like the security_detection_engine package.

To mitigate that, we are now skipping saved object resolution for packages known to be installed in 8.x.

While testing locally on a package containing ~5000 detection rules, I observed a significant drop in memory usage, from 1.17GB to 1.05GB at peak.

Before:
Screenshot 2024-07-11 at 11 11 06

After:
Screenshot 2024-07-11 at 11 02 32

@xcrzx xcrzx self-assigned this Jul 10, 2024
@xcrzx xcrzx marked this pull request as ready for review July 11, 2024 08:54
@xcrzx xcrzx requested a review from a team as a code owner July 11, 2024 08:54
@botelastic botelastic bot added the Team:Fleet Team label for Observability Data Collection Fleet team label Jul 11, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Team:Fleet)

@xcrzx xcrzx added release_note:skip Skip the PR/issue when compiling release notes Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules v8.16.0 Team:Fleet Team label for Observability Data Collection Fleet team Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Detection Rule Management Security Detection Rule Management Team and removed Team:Fleet Team label for Observability Data Collection Fleet team labels Jul 11, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detections-response (Team:Detections and Resp)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detection-rule-management (Team:Detection Rule Management)

Copy link
Contributor Author

@xcrzx xcrzx Jul 11, 2024

Choose a reason for hiding this comment

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

I've refactored getKibanaAssets a little bit to avoid intermediate array creation and reduce memory consumption when installing big packages.

@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

✅ unchanged

cc @xcrzx

@@ -237,9 +266,9 @@ async function deleteAssets(
// then the other asset types
await Promise.all([
...deleteESAssets(otherAssets, esClient),
deleteKibanaAssets(installedKibana, spaceId),
resolveAndDeleteKibanaAssets(installedKibana, spaceId),
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the reason the resolve is needed here, don't we need the version check here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably we can add the check here too. Where is this code path used? I'm not super familiar with this code, so I'd rely on your opinion.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is being called when deleting a package or when rolling back a package installation. I think it would be good to add the same version check here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll be introducing another optimization in the remove method soon. I'll address your suggestion in that PR.

Copy link
Contributor

@juliaElastic juliaElastic left a comment

Choose a reason for hiding this comment

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

LGTM, asked a clarifying question.

// only in 8.x or later. If so, we can skip SO resolution step altogether
// and delete the assets directly. Otherwise, we need to resolve the assets
// which might create high memory pressure if a package has a lot of assets.
if (minKibana && minKibana.major >= 8) {
Copy link
Member

@nchaulet nchaulet Jul 11, 2024

Choose a reason for hiding this comment

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

I am not even sure we need that condition, we have a constant that we bump to force reinstall package when upgrading Kibana, and I am pretty sure this one as been bumped in the 8.x cycle. The condition is the package has been installed in 8.x correct ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we need this check to account for cases when users upgrade directly from 7.17 to 8.current. Our official documentation suggests upgrading to 8.0 first, but I'm not sure if that is enforced in any way. If there's a possibility our users can upgrade to 8.current directly without installing 8.0 first, I think we should account for that.

@xcrzx xcrzx merged commit 415ed2e into elastic:main Jul 12, 2024
37 of 38 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Jul 12, 2024
@xcrzx xcrzx deleted the memory-optimization branch July 12, 2024 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules release_note:skip Skip the PR/issue when compiling release notes Team:Detection Rule Management Security Detection Rule Management Team Team:Detections and Resp Security Detection Response Team Team:Fleet Team label for Observability Data Collection Fleet team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. v8.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Fleet] All package SOs are being read into memory during package upgrade or force installation
5 participants