Skip to content

Commit

Permalink
[rego] Check store modules before skipping parsing (#5520)
Browse files Browse the repository at this point in the history
* [rego] Check store modules before skipping parsing

Fixes #5511

This change will cause the operation to be timed if the store
modules have already all been compiled and there are no
rawModules. This might be undesirable.

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 committed Jan 5, 2023
1 parent 5eccbb4 commit 8bb23ba
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions plugins/logs/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,7 @@ func TestPluginMasking(t *testing.T) {
if tc.errManager.Error() != err.Error() {
t.Fatalf("expected error %s, but got %s", tc.errManager.Error(), err.Error())
}
return
}
}

Expand Down
14 changes: 8 additions & 6 deletions rego/rego.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,14 @@ func (r *Rego) prepare(ctx context.Context, qType queryType, extras []extraStage
}

func (r *Rego) parseModules(ctx context.Context, txn storage.Transaction, m metrics.Metrics) error {
if len(r.modules) == 0 {
ids, err := r.store.ListPolicies(ctx, txn)
if err != nil {
return err
}

// if there are no raw modules, nor modules in the store, then there
// is nothing to do.
if len(r.modules) == 0 && len(ids) == 0 {
return nil
}

Expand All @@ -1700,11 +1707,6 @@ func (r *Rego) parseModules(ctx context.Context, txn storage.Transaction, m metr
// Parse any modules in the are saved to the store, but only if
// another compile step is going to occur (ie. we have parsed modules
// that need to be compiled).
ids, err := r.store.ListPolicies(ctx, txn)
if err != nil {
return err
}

for _, id := range ids {
// if it is already on the compiler we're using
// then don't bother to re-parse it from source
Expand Down
8 changes: 4 additions & 4 deletions sdk/opa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ main = true
t.Fatal("expected true but got:", decision, ok)
}

if exp, act := 4, len(m.All()); exp != act {
if exp, act := 5, len(m.All()); exp != act {
t.Fatalf("expected %d metrics, got %d", exp, act)
}

Expand Down Expand Up @@ -484,7 +484,7 @@ main = true
t.Fatal("expected true but got:", decision, ok)
}

if exp, act := 25, len(m.All()); exp != act {
if exp, act := 26, len(m.All()); exp != act {
t.Fatalf("expected %d metrics, got %d", exp, act)
}

Expand Down Expand Up @@ -932,7 +932,7 @@ allow {
t.Fatal("expected &{[2 = data.junk.x] []} true but got:", decision, ok)
}

if exp, act := 5, len(m.All()); exp != act {
if exp, act := 6, len(m.All()); exp != act {
t.Fatalf("expected %d metrics, got %d", exp, act)
}

Expand Down Expand Up @@ -1019,7 +1019,7 @@ allow {
t.Fatal("expected &{[2 = data.junk.x] []} true but got:", decision, ok)
}

if exp, act := 32, len(m.All()); exp != act {
if exp, act := 33, len(m.All()); exp != act {
t.Fatalf("expected %d metrics, got %d", exp, act)
}

Expand Down
1 change: 1 addition & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ func TestCompileV1Observability(t *testing.T) {
"timer_rego_partial_eval_ns",
"timer_rego_query_compile_ns",
"timer_rego_query_parse_ns",
"timer_rego_module_parse_ns",
"timer_server_handler_ns",
"counter_disk_read_keys",
"timer_disk_read_ns",
Expand Down

0 comments on commit 8bb23ba

Please sign in to comment.