Skip to content

Commit f9a0b8f

Browse files
v1/topdown/graphql: Cache GraphQL schema parse results (open-policy-agent#5377)
This commit stores parsed GraphQL schemas to the cache, which improves the performance of GraphQL operations that parse the schema more than once. Queries are not cached. BenchmarkGraphQLSchemaIsValid/Trivial_Schema_-_string-10 23740 51418 ns/op BenchmarkGraphQLSchemaIsValid/Trivial_Schema_with_cache_-_string-10 965624 1187 ns/op BenchmarkGraphQLSchemaIsValid/Schema_w/_1000_types_-_string-10 751 1578718 ns/op BenchmarkGraphQLSchemaIsValid/Schema_w/_1000_types_with_cache_-_string-10 33801 34915 ns/op BenchmarkGraphQLSchemaIsValid/Trivial_Schema_-_AST_object-10 17491 67845 ns/op BenchmarkGraphQLSchemaIsValid/Trivial_Schema_with_cache_-_AST_object-10 124808 9801 ns/op BenchmarkGraphQLParseSchema/Trivial_Schema_-_string-10 9914 116169 ns/op BenchmarkGraphQLParseSchema/Trivial_Schema_with_cache_-_string-10 678207 1640 ns/op BenchmarkGraphQLParseQuery/Trivial_Query_-_string-10 26994 44328 ns/op BenchmarkGraphQLParseQuery/Trivial_Query_with_cache_-_string-10 26922 44386 ns/op BenchmarkGraphQLIsValid/Trivial_Schema_-_string-10 21441 55487 ns/op BenchmarkGraphQLIsValid/Trivial_Schema_with_cache_-_string-10 152800 7384 ns/op BenchmarkGraphQLIsValid/Schema_w/_1000_types_-_string-10 758 1568036 ns/op BenchmarkGraphQLIsValid/Schema_w/_1000_types_with_cache_-_string-10 28108 43051 ns/op BenchmarkGraphQLParse/Trivial_Schema_-_string-10 5119 216610 ns/op BenchmarkGraphQLParse/Trivial_Schema_with_cache_-_string-10 24186 49551 ns/op BenchmarkGraphQLParseAndVerify/Trivial_Schema_-_string-10 5241 217419 ns/op BenchmarkGraphQLParseAndVerify/Trivial_Schema_with_cache_-_string-10 23911 50004 ns/op Resolves: open-policy-agent#5377 Signed-off-by: Rob Myers <[email protected]>
1 parent a3be450 commit f9a0b8f

File tree

5 files changed

+1715
-103
lines changed

5 files changed

+1715
-103
lines changed

Diff for: docs/content/configuration.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -863,17 +863,19 @@ Caching represents the configuration of the inter-query cache that built-in func
863863
functions provided by OPA, `http.send` is currently the only one to utilize the inter-query cache. See the documentation
864864
on the [http.send built-in function](../policy-reference/#http) for information about the available caching options.
865865

866-
It also represents the configuration of the inter-query _value_ cache that built-in functions can utilize. Currently,
866+
It also represents the configuration of the inter-query _value_ cache that built-in functions can utilize. Currently,
867867
this cache is utilized by the `regex` and `glob` built-in functions for compiled regex and glob match patterns
868-
respectively, and the `json.schema_match` built-in function for compiled JSON schemas.
868+
respectively, the `json.schema_match` built-in function for compiled JSON schemas, and any `graphql` built-in function
869+
that requires GraphQL schemas.
869870

870871
| Field | Type | Required | Description |
871872
|--------------------------------------------------------------------------|---------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
872873
| `caching.inter_query_builtin_cache.max_size_bytes` | `int64` | No | Inter-query cache size limit in bytes. OPA will drop old items from the cache if this limit is exceeded. By default, no limit is set. |
873-
| `caching.inter_query_builtin_cache.forced_eviction_threshold_percentage` | `int64` | No | Threshold limit configured as percentage of `caching.inter_query_builtin_cache.max_size_bytes`, when exceeded OPA will start dropping old items permaturely. By default, set to `100`. |
874+
| `caching.inter_query_builtin_cache.forced_eviction_threshold_percentage` | `int64` | No | Threshold limit configured as percentage of `caching.inter_query_builtin_cache.max_size_bytes`, when exceeded OPA will start dropping old items prematurely. By default, set to `100`. |
874875
| `caching.inter_query_builtin_cache.stale_entry_eviction_period_seconds` | `int64` | No | Stale entry eviction period in seconds. OPA will drop expired items from the cache every `stale_entry_eviction_period_seconds`. By default, set to `0` indicating stale entry eviction is disabled. |
875876
| `caching.inter_query_builtin_value_cache.max_num_entries` | `int` | No | Maximum number of entries in the Inter-query value cache. OPA will drop random items from the cache if this limit is exceeded. By default, set to `0` indicating unlimited size. |
876877
| `caching.inter_query_builtin_value_cache.named.io_jwt.max_num_entries` | `int` | No | Maximum number of entries in the `io_jwt` cache, used by the [`io.jwt` token verification](../policy-reference/#tokens) built-in functions. OPA will drop random items from the cache if this limit is exceeded. By default, this cache is disabled. |
878+
| `caching.inter_query_builtin_value_cache.named.graphql.max_num_entries` | `int` | No | Maximum number of entries in the `graphql` cache, used by the [`graphql` builtins](../policy-reference/#graphql) built-in functions to cache parsed schemas. OPA will drop random items from the cache if this limit is exceeded. By default, this cache is set to a maximum of 10 entries. |
877879

878880
## Distributed tracing
879881

Diff for: v1/plugins/discovery/discovery_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -1721,8 +1721,9 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
17211721
"inter_query_builtin_cache": {"max_size_bytes": 10000000, "forced_eviction_threshold_percentage": 90},
17221722
"inter_query_builtin_value_cache": {
17231723
"named": {
1724-
"io_jwt": {"max_num_entries": 55}
1725-
}
1724+
"io_jwt": {"max_num_entries": 55},
1725+
"graphql": {"max_num_entries": 10}
1726+
}
17261727
}
17271728
}
17281729
}`)
@@ -1888,8 +1889,9 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
18881889
"inter_query_builtin_cache": {"max_size_bytes": 200, "stale_entry_eviction_period_seconds": 10, "forced_eviction_threshold_percentage": 200},
18891890
"inter_query_builtin_value_cache": {
18901891
"named": {
1891-
"io_jwt": {"max_num_entries": 10}
1892-
}
1892+
"io_jwt": {"max_num_entries": 10},
1893+
"graphql": {"max_num_entries": 11}
1894+
}
18931895
}
18941896
}
18951897
}
@@ -1906,6 +1908,7 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
19061908
"caching.inter_query_builtin_cache.max_size_bytes",
19071909
"caching.inter_query_builtin_cache.forced_eviction_threshold_percentage",
19081910
"caching.inter_query_builtin_value_cache.named.io_jwt.max_num_entries",
1911+
"caching.inter_query_builtin_value_cache.named.graphql.max_num_entries",
19091912
}
19101913
for _, k := range expectedOverriddenKeys {
19111914
if !strings.Contains(disco.status.Message, k) {
@@ -1928,6 +1931,8 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
19281931
*maxNumEntriesInterQueryValueCache = 0
19291932
maxNumEntriesJWTValueCache := new(int)
19301933
*maxNumEntriesJWTValueCache = 55
1934+
maxNumEntriesGraphQLValueCache := new(int)
1935+
*maxNumEntriesGraphQLValueCache = 10
19311936

19321937
expectedCacheConf := &cache.Config{
19331938
InterQueryBuiltinCache: cache.InterQueryBuiltinCacheConfig{
@@ -1941,6 +1946,9 @@ func TestReconfigureWithLocalOverride(t *testing.T) {
19411946
"io_jwt": {
19421947
MaxNumEntries: maxNumEntriesJWTValueCache,
19431948
},
1949+
"graphql": {
1950+
MaxNumEntries: maxNumEntriesGraphQLValueCache,
1951+
},
19441952
},
19451953
},
19461954
}

0 commit comments

Comments
 (0)