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

[WIP] feat(gatsby): worker-pool for custom GraphQL field resolvers #10938

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f5c4334
first shot at parallel workers
Moocar Dec 18, 2018
f4d816c
stuff
Moocar Dec 18, 2018
7b3fca9
async getNode
Moocar Dec 18, 2018
25a524d
asyncPlugin
Moocar Dec 18, 2018
e6beb71
fix pluginFields
Moocar Dec 18, 2018
aef1e43
use jest-worker for IPC
Moocar Dec 19, 2018
8596eda
remove console.logs
Moocar Dec 19, 2018
e7cbeac
jest workers work!
Moocar Dec 19, 2018
6863ca8
Merge branch 'master' into parallel-sketch
Moocar Jan 8, 2019
242420c
store resolvers against their type, not just fieldName
Moocar Jan 8, 2019
a7559d5
fixed up rpc api
Moocar Jan 8, 2019
fa5ec9f
replace isAsync with workerPlugin
Moocar Jan 8, 2019
3cf872e
document jest-worker IPC code 3 in async/worker code
Moocar Jan 8, 2019
1508298
change async-resolvers -> worker-resolvers
Moocar Jan 8, 2019
a889664
error checking
Moocar Jan 8, 2019
38e0370
resolver-worker docs and error checking
Moocar Jan 8, 2019
70e7aaa
docs
Moocar Jan 9, 2019
c5655ec
use @moocar/jest-worker
Moocar Jan 9, 2019
a77c1d4
yarn.lock for @moocar/jest-worker
Moocar Jan 9, 2019
7a86ee1
end worker pool after bootstrap
Moocar Jan 9, 2019
f629eaa
Merge branch 'master' into parallel-sketch
Moocar Jan 9, 2019
6767039
introduce getCache mocker
Moocar Jan 9, 2019
d50efb2
fix unsupported properties, and general cleanup
Moocar Jan 9, 2019
21d5aba
use plugin.name for cache in resolver-worker
Moocar Jan 11, 2019
98fbec2
Merge branch 'master' into parallel-sketch
Moocar Jan 21, 2019
15099d3
Merge branch 'master' into parallel-sketch
Moocar Jan 30, 2019
61b0168
Merge branch 'master' into parallel-sketch
Moocar Feb 4, 2019
0aca069
step.toString() in markdown benchmark
Moocar Feb 5, 2019
21bfc69
Merge branch 'master' into parallel-sketch
Moocar Feb 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolver-worker docs and error checking
  • Loading branch information
Moocar committed Jan 8, 2019
commit 38e03705ac3a54d2b1692d5a7f923b1ffd8e8ac9
92 changes: 58 additions & 34 deletions packages/gatsby/src/schema/resolver-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,36 @@ const JEST_WORKER_CHILD_MESSAGE_IPC = 3
// requests for field resolvers for.
const types = {}

// List of all in-flight RPCs
const rpcs = new Map()

function handleRpcResponse(rpc) {
invariant(rpc, `rpc`)
const { id, response } = rpc
invariant(id, `id`)
const { resolve } = rpcs.get(id)
invariant(resolve, `resolve`)
rpcs.delete(id)
resolve(response)
// List of all in-flight RPCs. keys are RPC ids, and values are
// objects containing the `time` the RPC was sent, and the `resolve`
// and `reject` callbacks.
const inFlightRpcs = new Map()

/**
* Handle rpc response by removing the original rpc from in flight
* rpcs and calling its `resolve` with the response
*/
function handleRpcResponse(rpcResponse) {
const { id, response } = rpcResponse
invariant(
id && response,
`RPC response should contain the "id" of the original RPC and a "response".`
)
const rpc = inFlightRpcs.get(id)
invariant(rpc, `RPC for id [${id}] not found`)
// Response for RPC has been received, so remove it from
// inFlightRpcs.
// TODO: Periodically expire old in flight RPCs
inFlightRpcs.delete(id)
rpc.resolve(response)
}

function handleIpc(ipc) {
invariant(ipc, `handle IPC`)
const [rpc] = ipc
invariant(rpc, `handle rpc`)
invariant(
rpc,
`IPC request should be an array with a single element representing the "rpc"`
)
if (rpc.type === `response`) {
handleRpcResponse(rpc)
} else {
Expand All @@ -40,14 +53,9 @@ function sendRpc({ name, args, resolve, reject }) {
invariant(name, `rpc name`)
invariant(resolve, `rpc resolve`)
invariant(reject, `rpc reject`)
// TODO id should be composite of processId and incrementing number. Perhaps
const id = uuidv4()
const rpc = {
name,
args,
id,
}
rpcs.set(id, {
const rpc = { name, args, id }
inFlightRpcs.set(id, {
time: new Date(),
resolve,
reject,
Expand Down Expand Up @@ -103,6 +111,15 @@ function makeUnsupportedProps(o, props) {
return o
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

My branch makes it so that all resolvers use stuff passed to context, instead of global ones. So here you could modify context to have the worker resolver stuff.

Copy link
Contributor Author

@Moocar Moocar Feb 7, 2019

Choose a reason for hiding this comment

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

@freiksenet This isn't a breaking change right? I.e will field resolvers still be able to use the closed over getNode etc supplied by setFieldsOnGraphQLNodeType in addition to those supplied in the context?

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 just had a read through of the branch. It looks the only api functions injected into context are nodeModel, which will supply getNode etc. Is there also a plan to include things like getCache, reporter, createContentDigest etc as well?

* GraphQL fields are normally created by plugins during the
* `setFieldsOnGraphQLNodeType` API, and as such they expect API
* functions such as `getNode()` to be available. But workers operate
* outside of the main process so can't call these
* functions. Therefore we supply implementations for these APIs that
* result in RPC calls back to the main process. This goes for static
* values such as `type`.
*/
function makeApi(type, pathPrefix) {
const cache = new Cache({ name: `some cache` }).init()
const api = {
Expand Down Expand Up @@ -152,6 +169,12 @@ async function initModule(
})
}

/**
* Called by jest-worker when the worker is created. Setup involves
* requiring each field's resolver module and storing it in the
* `fields` global so that they can be found when `execResolver` is
* called
*/
async function setup(args) {
const { pathPrefix, fields } = args
invariant(fields, `setup fields`)
Expand All @@ -160,21 +183,22 @@ async function setup(args) {
}
}

/**
* main function exported for jest-worker. Takes a typeName and
* fieldName and finds previously configured resolver for it, then
* calls it with node and args. The response will be sent back to the
* main process by jest-worker
*/
async function execResolver(typeName, fieldName, node, args) {
try {
invariant(typeName, `execResolver typeName`)
invariant(fieldName, `execResolver fieldName`)
const type = types[typeName]
invariant(type, `execResolver type`)
const field = type.fields[fieldName]
invariant(field, `execResolver field`)
const resolver = field.resolve
invariant(resolver, `execResolver resolver`)
return await resolver(node, args)
} catch (e) {
console.log(e)
return null
}
invariant(typeName, `execResolver typeName`)
invariant(fieldName, `execResolver fieldName`)
const type = types[typeName]
invariant(type, `execResolver type`)
const field = type.fields[fieldName]
invariant(field, `execResolver field`)
const resolver = field.resolve
invariant(resolver, `execResolver resolver`)
return await resolver(node, args)
}

process.on(`ipc`, handleIpc)
Expand Down
7 changes: 2 additions & 5 deletions packages/gatsby/src/schema/worker-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,8 @@ function workerResolver({ typeName, fieldName }) {
return async (node, args) => {
try {
return await pool.execResolver(typeName, fieldName, node, args)
} catch (e) {
reporter.panicOnBuild(
`Error calling worker resolver. type = [${typeName}], field = [${fieldName}].\n
${e.message}`
)
} catch (err) {
reporter.panicOnBuild(err)
return null // Never reached. for linter
}
}
Expand Down