Skip to content

loader+internal: Add bundle lazy loading mode across the runtime. #7768

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

Merged

Conversation

philipaconrad
Copy link
Contributor

What Changed?

This PR comprehensively plumbs in the bundle lazy loading mode option in the compile, runtime, rego, and bundle packages. It also includes the bare minimum plumbing to allow the path watcher utilities to also toggle the option on.

In nearly all places where a default is expected, the lazy loading mode is set to false (disabled) to avoid behavior changes.

Copy link

netlify bot commented Jul 11, 2025

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit b55440f
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/68716f1d6247110008829502
😎 Deploy Preview https://deploy-preview-7768--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@philipaconrad philipaconrad force-pushed the philip/lazy-bundle-loading branch from cbd81c3 to d6c782e Compare July 11, 2025 19:05
Copy link
Contributor Author

@philipaconrad philipaconrad left a comment

Choose a reason for hiding this comment

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

Some comments for reviewers:

@@ -46,6 +46,7 @@ func bundleOrDirInfoForRegoVersion(regoVersion ast.RegoVersion, path string, inc
b, err := loader.NewFileLoader().
WithRegoVersion(regoVersion).
WithSkipBundleVerification(true).
WithBundleLazyLoadingMode(true). // Bundle lazy loading mode skips parsing data files
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the one place we currently set Bundle Lazy Loading mode to true. It has the potential to be helpful in this function because we only care about the metadata, manifest, signatures, and Rego code in this function-- the data isn't inspected at all.

@@ -122,10 +122,11 @@ func LoadPaths(paths []string,
asBundle bool,
bvc *bundle.VerificationConfig,
skipVerify bool,
bundleLazyLoading bool,
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 chose to put the bundleLazyLoading parameter immediately after skipVerify where possible. This results in cleaner-looking invocations in places where we're unpacking parameters from an options struct, like in v1/runtime/runtime.go.

Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Do you reckon there's a good way to test at least some of this? I mean I have had a hard time coming up with a good test recently, too, #7767, and ended up not doing it... so either way is fine, but perhaps you have an idea.

@@ -216,6 +217,12 @@ func (c *Compiler) WithBundleVerificationKeyID(keyID string) *Compiler {
return c
}

// WithBundleLazyLoadingMode sets the additional data to be included in .manifest
Copy link
Contributor

Choose a reason for hiding this comment

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

You had me thinking there for a moment. C/P mishap from WithMetadata() below 😄

@@ -46,6 +46,7 @@ func bundleOrDirInfoForRegoVersion(regoVersion ast.RegoVersion, path string, inc
b, err := loader.NewFileLoader().
WithRegoVersion(regoVersion).
WithSkipBundleVerification(true).
WithBundleLazyLoadingMode(true). // Bundle lazy loading mode skips parsing data files
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm this will actually make bundle inspection a tad faster, wouldn't it? Not like it's a performance-critical path, just thinking about the impact of this.

@@ -124,7 +124,7 @@ p = true { 1 = 2 }`
store := inmem.New()

err := storage.Txn(ctx, store, storage.WriteParams, func(txn storage.Transaction) error {
loaded, err := LoadPaths(paths, nil, tc.asBundle, nil, true, false, nil, fsys)
loaded, err := LoadPaths(paths, nil, tc.asBundle, nil, false, true, false, nil, fsys)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not now, but eventually, we should figure out these arguments better. nil, nil, false, true, false. It's like morse code or a drum line. 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't love it either. I think future maintainers might end up rolling all these options up into a params/options struct of some sort.

@philipaconrad philipaconrad force-pushed the philip/lazy-bundle-loading branch from d6c782e to 5d2726f Compare July 11, 2025 19:11
@philipaconrad
Copy link
Contributor Author

Do you reckon there's a good way to test at least some of this?

The only kind of testcase I can think of would be one where we intentionally put non-JSON data into a bundle's data file, and check that the bundle isn't eagerly trying to parse that data.

I'll spend a few minutes to see if I can hack up a testcase-- I might be able to stick one in the v1/loader package's tests. 🤔

@philipaconrad philipaconrad force-pushed the philip/lazy-bundle-loading branch from 5d2726f to 288d438 Compare July 11, 2025 19:55
@philipaconrad
Copy link
Contributor Author

Okay, I've added a test to v1/loader/loader_test.go that cannot pass unless lazy loading mode is disabling data validation checks.

This commit comprehensively plumbs in the bundle lazy loading mode
option in the compile, runtime, rego, and bundle packages. It also
includes the bare minimum plumbing to allow the path watcher utilities
to also toggle the option on.

In nearly all places where a default is expected, the lazy loading mode
is set to false (disabled) to avoid behavior changes.

Signed-off-by: Philip Conrad <[email protected]>
@philipaconrad philipaconrad force-pushed the philip/lazy-bundle-loading branch from c7940d7 to b55440f Compare July 11, 2025 20:07
@philipaconrad philipaconrad enabled auto-merge (squash) July 11, 2025 20:09
@philipaconrad philipaconrad merged commit 70e5ad1 into open-policy-agent:main Jul 11, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants