Skip to content

Commit

Permalink
experimental: Allow query plan & parsed/validated document cach… (apo…
Browse files Browse the repository at this point in the history
…llographql/apollo-server#3755)

* experimental: Allow adjusting of query plan & parsed/validated document cache.

This introduces two experimental configuration options for existing internal
cache stores:

- `experimental_approximateDocumentStoreSizeMiB` is added to the `ApolloServer`
  constructor options and controls the _approximate_[1] size, in MiB, of the
  request pipeline document store, which is used to avoid repeated parsing and
  validation passes on queries which equate to the same `queryHash`[2].
- `experimental_approximateQueryPlanStoreMiB` is added to the `ApolloGateway`
  constructor options and controls the _approximate_[1] size of the query plan
  cache, which is used to avoid recalculation on repeated queries which equate
  to the same `queryHash`[2].

These are currently experimental because a more complete solution might
more dynamically account for memory of various internal caches and vary
their sizes based based on available memory and the overall server
performance, rather than needing hard-coded values which are less than
precise.

[1]: "Approximate", because it's based on the `JSON.stringify`'d byte-length
     of the value being cached.
[2]: The `queryHash` is a SHA-256 value of the incoming operation.

* Add CHANGELOG.md for apollographql/apollo-server#3755.

* docs: Add documentation for apollographql/apollo-server#3755.

Apollo-Orig-Commit-AS: apollographql/apollo-server@21651bd
  • Loading branch information
abernix committed Feb 4, 2020
1 parent c3116a5 commit 3c504f9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions gateway-js/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* Reduce interface expansion for types contained to a single service [#3582](https://github.com/apollographql/apollo-server/pull/3582)
* Instantiate one `CachedFetcher` per gateway instance. This resolves a condition where multiple federated gateways would utilize the same cache store could result in an `Expected undefined to be a GraphQLSchema` error. [#3704](https://github.com/apollographql/apollo-server/pull/3704)
* Gateway: minimize downstream request size [#3737](https://github.com/apollographql/apollo-server/pull/3737)
* experimental: Allow configuration of the query plan store by introducing an `experimental_approximateQueryPlanStoreMiB` property to the `ApolloGateway` constructor options which overrides the default cache size of 30MiB. [#3755](https://github.com/apollographql/apollo-server/pull/3755)

# v0.11.6

Expand Down
10 changes: 9 additions & 1 deletion gateway-js/src/index.ts
Expand Up @@ -58,6 +58,7 @@ interface GatewayConfigBase {
experimental_updateServiceDefinitions?: Experimental_UpdateServiceDefinitions;
experimental_didUpdateComposition?: Experimental_DidUpdateCompositionCallback;
experimental_pollInterval?: number;
experimental_approximateQueryPlanStoreMiB?: number;
}

interface RemoteGatewayConfig extends GatewayConfigBase {
Expand Down Expand Up @@ -178,6 +179,8 @@ export class ApolloGateway implements GraphQLService {
// how often service defs should be loaded/updated (in ms)
protected experimental_pollInterval?: number;

private experimental_approximateQueryPlanStoreMiB?: number;

constructor(config?: GatewayConfig) {
this.config = {
// TODO: expose the query plan in a more flexible JSON format in the future
Expand Down Expand Up @@ -219,6 +222,9 @@ export class ApolloGateway implements GraphQLService {
this.experimental_didUpdateComposition =
config.experimental_didUpdateComposition;

this.experimental_approximateQueryPlanStoreMiB =
config.experimental_approximateQueryPlanStoreMiB;

if (
isManagedConfig(config) &&
config.experimental_pollInterval &&
Expand Down Expand Up @@ -609,7 +615,9 @@ export class ApolloGateway implements GraphQLService {
// only using JSON.stringify on the DocumentNode (and thus doesn't account
// for unicode characters, etc.), but it should do a reasonable job at
// providing a caching document store for most operations.
maxSize: Math.pow(2, 20) * 30,
maxSize:
Math.pow(2, 20) *
(this.experimental_approximateQueryPlanStoreMiB || 30),
sizeCalculator: approximateObjectSize,
});
}
Expand Down

0 comments on commit 3c504f9

Please sign in to comment.