-
Notifications
You must be signed in to change notification settings - Fork 196
Integrate JiT compilation into CLI #2110
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
Open
rafal-hawrylak
wants to merge
6
commits into
main
Choose a base branch
from
cli_jit_integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e687190
feat: implement JiT worker and RPC infrastructure
rafal-hawrylak d2f544f
JiT: protos update to enable execution in CLI
rafal-hawrylak 92736be
feat: implement JiT worker and RPC infrastructure
rafal-hawrylak a684ab1
feat: enhance JiT compiler with action descriptors and metadata support
rafal-hawrylak e51e27c
feat: integrate JiT compilation and finalize test suite
rafal-hawrylak 82adbdd
feat: integrate JiT compilation and finalize test suite
rafal-hawrylak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| import { ChildProcess, fork } from "child_process"; | ||
|
|
||
| export abstract class BaseWorker<TResponse, TMessage = any> { | ||
| protected constructor(private readonly loaderPath: string) {} | ||
|
|
||
| protected async runWorker( | ||
| timeoutMillis: number, | ||
| onBoot: (child: ChildProcess) => void, | ||
| onMessage: (message: TMessage, child: ChildProcess, resolve: (res: TResponse) => void, reject: (err: Error) => void) => void | ||
| ): Promise<TResponse> { | ||
| const forkScript = this.resolveScript(); | ||
| const child = fork(forkScript, [], { | ||
| stdio: [0, 1, 2, "ipc", "pipe"] | ||
| }); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| let completed = false; | ||
| let booted = false; | ||
|
|
||
| const terminate = (fn: () => void) => { | ||
| if (completed) { | ||
| return; | ||
| } | ||
| completed = true; | ||
| clearTimeout(timeout); | ||
| child.kill("SIGKILL"); | ||
| fn(); | ||
| }; | ||
|
|
||
| const timeout = setTimeout(() => { | ||
| terminate(() => | ||
| reject(new Error(`Worker timed out after ${timeoutMillis / 1000} seconds`)) | ||
| ); | ||
| }, timeoutMillis); | ||
|
|
||
| child.on("message", (message: any) => { | ||
| if (message.type === "worker_booted") { | ||
| if (!booted) { | ||
| booted = true; | ||
| onBoot(child); | ||
| } | ||
| return; | ||
| } | ||
| onMessage(message, child, (res) => terminate(() => resolve(res)), (err) => terminate(() => reject(err))); | ||
| }); | ||
|
|
||
| child.on("error", err => { | ||
| terminate(() => reject(err)); | ||
| }); | ||
|
|
||
| child.on("exit", (code, signal) => { | ||
| if (!completed) { | ||
| const errorMsg = | ||
| code !== 0 && code !== null | ||
| ? `Worker exited with code ${code} and signal ${signal}` | ||
| : "Worker exited without sending a response message"; | ||
| terminate(() => reject(new Error(errorMsg))); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private resolveScript() { | ||
| const pathsToTry = ["./worker_bundle.js", this.loaderPath]; | ||
| for (const p of pathsToTry) { | ||
| try { | ||
| return require.resolve(p); | ||
| } catch (e) { | ||
| // Continue to next path. | ||
| } | ||
| } | ||
| throw new Error(`Could not resolve worker script. Tried: ${pathsToTry.join(", ")}`); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
| import { ChildProcess } from "child_process"; | ||
| import * as path from "path"; | ||
|
|
||
| import { BaseWorker } from "df/cli/api/commands/base_worker"; | ||
| import { handleDbRequest } from "df/cli/api/commands/jit/rpc"; | ||
| import { IDbAdapter, IDbClient } from "df/cli/api/dbadapters"; | ||
| import { IBigQueryExecutionOptions } from "df/cli/api/dbadapters/bigquery"; | ||
| import { DEFAULT_COMPILATION_TIMEOUT_MILLIS } from "df/cli/api/utils/constants"; | ||
| import { dataform } from "df/protos/ts"; | ||
|
|
||
| export interface IJitWorkerMessage { | ||
| type: "rpc_request" | "jit_response" | "jit_error"; | ||
| method?: string; | ||
| request?: Uint8Array; | ||
| correlationId?: string; | ||
| response?: Uint8Array; | ||
| error?: string; | ||
| } | ||
|
|
||
| export class JitCompileChildProcess extends BaseWorker< | ||
| dataform.IJitCompilationResponse, | ||
| IJitWorkerMessage | ||
| > { | ||
| public static async compile( | ||
| request: dataform.IJitCompilationRequest, | ||
| projectDir: string, | ||
| dbadapter: IDbAdapter, | ||
| dbclient: IDbClient, | ||
| timeoutMillis: number = DEFAULT_COMPILATION_TIMEOUT_MILLIS, | ||
| options?: IBigQueryExecutionOptions | ||
| ): Promise<dataform.IJitCompilationResponse> { | ||
| return await new JitCompileChildProcess().run( | ||
| request, | ||
| projectDir, | ||
| dbadapter, | ||
| dbclient, | ||
| timeoutMillis, | ||
| options | ||
| ); | ||
| } | ||
|
|
||
| constructor() { | ||
| super(path.resolve(__dirname, "../../../vm/jit_loader")); | ||
| } | ||
|
|
||
| private async run( | ||
| request: dataform.IJitCompilationRequest, | ||
| projectDir: string, | ||
| dbadapter: IDbAdapter, | ||
| dbclient: IDbClient, | ||
| timeoutMillis: number, | ||
| options?: IBigQueryExecutionOptions | ||
| ): Promise<dataform.IJitCompilationResponse> { | ||
| return await this.runWorker( | ||
| timeoutMillis, | ||
| child => { | ||
| child.send({ | ||
| type: "jit_compile", | ||
| request, | ||
| projectDir | ||
| }); | ||
| }, | ||
| async (message, child, resolve, reject) => { | ||
| if (message.type === "rpc_request") { | ||
| await this.handleRpcRequest(message, child, dbadapter, dbclient, options); | ||
| } else if (message.type === "jit_response") { | ||
| resolve(dataform.JitCompilationResponse.fromObject(message.response)); | ||
| } else if (message.type === "jit_error") { | ||
| reject(new Error(message.error)); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| private async handleRpcRequest( | ||
| message: IJitWorkerMessage, | ||
| child: ChildProcess, | ||
| dbadapter: IDbAdapter, | ||
| dbclient: IDbClient, | ||
| options?: IBigQueryExecutionOptions | ||
| ) { | ||
| try { | ||
| const response = await handleDbRequest( | ||
| dbadapter, | ||
| dbclient, | ||
| message.method, | ||
| message.request, | ||
| options | ||
| ); | ||
| child.send({ | ||
| type: "rpc_response", | ||
| correlationId: message.correlationId, | ||
| response | ||
| }); | ||
| } catch (e) { | ||
| child.send({ | ||
| type: "rpc_response", | ||
| correlationId: message.correlationId, | ||
| error: e.message | ||
| }); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.