-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces #23301 Closes #23080 --- This is a minimal threading implementation for Canvas. There's still a lot to be done to make this concept great, but this is a start. What it does: - Creates a server side abstraction on top of the interpreter - Determines where to send the expression by checking the first function to be run - Loads common functions in a separate worker thread on the server. - Routes to a single forked worker (thread), the main thread (server), or the browser (browser), in that order - Defers back to the router when a function isn't found. Fails if the function isn't found in any of the above 3 environments - Times out the worker if it takes too long, and respawns it as needed. - Simplifies the error dialog to remove the stack. What is does not.: - Round robin a pool of workers - Queue. If one expression in the threaded env fails then anything sent to it in the meantime will fail. The upstream environment handles managing timeouts. I think this would only make sense todo with a pool. - Client side. This doesn't implement web workers, but we could use roughly the same architecture. - Implement a specific, pluggable `worker` environment on the server. Right now it's just common functions, so plugin authors will always end up in a thread if they put their function in the common directory. What I don't like: - The socketProvider code. This was reused across the server & browser, but now that it's only used in the browser there's no good reason for the abstraction - The serialize/deserialize stuff feels messy. Do we really need serialization?
- Loading branch information
Showing
21 changed files
with
496 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export const createError = err => ({ | ||
type: 'error', | ||
error: { | ||
stack: process.env.NODE_ENV === 'production' ? undefined : err.stack, | ||
message: typeof err === 'string' ? err : err.message, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import chrome from 'ui/chrome'; | ||
import $script from 'scriptjs'; | ||
import { typesRegistry } from '../../common/lib/types_registry'; | ||
import { | ||
argTypeRegistry, | ||
datasourceRegistry, | ||
transformRegistry, | ||
modelRegistry, | ||
viewRegistry, | ||
} from '../expression_types'; | ||
import { elementsRegistry } from './elements_registry'; | ||
import { renderFunctionsRegistry } from './render_functions_registry'; | ||
import { functionsRegistry as browserFunctions } from './functions_registry'; | ||
import { loadPrivateBrowserFunctions } from './load_private_browser_functions'; | ||
|
||
const registries = { | ||
browserFunctions: browserFunctions, | ||
commonFunctions: browserFunctions, | ||
elements: elementsRegistry, | ||
types: typesRegistry, | ||
renderers: renderFunctionsRegistry, | ||
transformUIs: transformRegistry, | ||
datasourceUIs: datasourceRegistry, | ||
modelUIs: modelRegistry, | ||
viewUIs: viewRegistry, | ||
argumentUIs: argTypeRegistry, | ||
}; | ||
|
||
let resolve = null; | ||
let called = false; | ||
|
||
const populatePromise = new Promise(_resolve => { | ||
resolve = _resolve; | ||
}); | ||
|
||
export const getBrowserRegistries = () => { | ||
return populatePromise; | ||
}; | ||
|
||
export const populateBrowserRegistries = () => { | ||
if (called) throw new Error('function should only be called once per process'); | ||
called = true; | ||
|
||
// loadPrivateBrowserFunctions is sync. No biggie. | ||
loadPrivateBrowserFunctions(); | ||
|
||
const remainingTypes = Object.keys(registries); | ||
const populatedTypes = {}; | ||
|
||
function loadType() { | ||
const type = remainingTypes.pop(); | ||
window.canvas = window.canvas || {}; | ||
window.canvas.register = d => registries[type].register(d); | ||
|
||
// Load plugins one at a time because each needs a different loader function | ||
// $script will only load each of these once, we so can call this as many times as we need? | ||
const pluginPath = chrome.addBasePath(`/api/canvas/plugins?type=${type}`); | ||
$script(pluginPath, () => { | ||
populatedTypes[type] = registries[type]; | ||
|
||
if (remainingTypes.length) loadType(); | ||
else resolve(populatedTypes); | ||
}); | ||
} | ||
|
||
if (remainingTypes.length) loadType(); | ||
return populatePromise; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.