-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema-compiler): Move transpiling to worker threads (under the …
…flag) (#9188) * move js transpilation to worker threads * hide worker threads transpile under the flag * add useful comment * tune Push CI to test with CUBEJS_WORKER_THREADS_TRANSPILATION=true * add workerpool pkg * use workerpool pkg * add max workers cfg * update env in ci * remove obsolete stuff
- Loading branch information
Showing
13 changed files
with
264 additions
and
356 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
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
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
14 changes: 14 additions & 0 deletions
14
packages/cubejs-schema-compiler/src/compiler/transpilers/LightweightNodeCubeDictionary.ts
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,14 @@ | ||
import { TranspilerCubeResolver } from './transpiler.interface'; | ||
|
||
export class LightweightNodeCubeDictionary implements TranspilerCubeResolver { | ||
public constructor(private cubeNames: string[] = []) { | ||
} | ||
|
||
public resolveCube(name: string): boolean { | ||
return this.cubeNames.includes(name); | ||
} | ||
|
||
public setCubeNames(cubeNames: string[]): void { | ||
this.cubeNames = cubeNames; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/cubejs-schema-compiler/src/compiler/transpilers/LightweightSymbolResolver.ts
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,30 @@ | ||
import { TranspilerSymbolResolver } from './transpiler.interface'; | ||
import { CONTEXT_SYMBOLS, CURRENT_CUBE_CONSTANTS } from '../CubeSymbols'; | ||
|
||
type CubeSymbols = Record<string, Record<string, boolean>>; | ||
|
||
export class LightweightSymbolResolver implements TranspilerSymbolResolver { | ||
public constructor(private symbols: CubeSymbols = {}) { | ||
} | ||
|
||
public setSymbols(symbols: CubeSymbols) { | ||
this.symbols = symbols; | ||
} | ||
|
||
public isCurrentCube(name): boolean { | ||
return CURRENT_CUBE_CONSTANTS.indexOf(name) >= 0; | ||
} | ||
|
||
public resolveSymbol(cubeName, name): any { | ||
if (name === 'USER_CONTEXT') { | ||
throw new Error('Support for USER_CONTEXT was removed, please migrate to SECURITY_CONTEXT.'); | ||
} | ||
|
||
if (CONTEXT_SYMBOLS[name]) { | ||
return true; | ||
} | ||
|
||
const cube = this.symbols[this.isCurrentCube(name) ? cubeName : name]; | ||
return cube || (this.symbols[cubeName] && this.symbols[cubeName][name]); | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
packages/cubejs-schema-compiler/src/compiler/transpilers/transpiler_worker.ts
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,67 @@ | ||
import workerpool from 'workerpool'; | ||
import { parse } from '@babel/parser'; | ||
import babelGenerator from '@babel/generator'; | ||
import babelTraverse from '@babel/traverse'; | ||
|
||
import { ValidationTranspiler } from './ValidationTranspiler'; | ||
import { ImportExportTranspiler } from './ImportExportTranspiler'; | ||
import { CubeCheckDuplicatePropTranspiler } from './CubeCheckDuplicatePropTranspiler'; | ||
import { CubePropContextTranspiler } from './CubePropContextTranspiler'; | ||
import { ErrorReporter } from '../ErrorReporter'; | ||
import { LightweightSymbolResolver } from './LightweightSymbolResolver'; | ||
import { LightweightNodeCubeDictionary } from './LightweightNodeCubeDictionary'; | ||
|
||
type TransferContent = { | ||
fileName: string; | ||
content: string; | ||
transpilers: string[]; | ||
cubeNames: string[]; | ||
cubeSymbolsNames: Record<string, Record<string, boolean>>; | ||
}; | ||
|
||
const cubeDictionary = new LightweightNodeCubeDictionary(); | ||
const cubeSymbols = new LightweightSymbolResolver(); | ||
const errorsReport = new ErrorReporter(null, []); | ||
|
||
const transpilers = { | ||
ValidationTranspiler: new ValidationTranspiler(), | ||
ImportExportTranspiler: new ImportExportTranspiler(), | ||
CubeCheckDuplicatePropTranspiler: new CubeCheckDuplicatePropTranspiler(), | ||
CubePropContextTranspiler: new CubePropContextTranspiler(cubeSymbols, cubeDictionary, cubeSymbols), | ||
}; | ||
|
||
const transpile = (data: TransferContent) => { | ||
cubeDictionary.setCubeNames(data.cubeNames); | ||
cubeSymbols.setSymbols(data.cubeSymbolsNames); | ||
|
||
const ast = parse( | ||
data.content, | ||
{ | ||
sourceFilename: data.fileName, | ||
sourceType: 'module', | ||
plugins: ['objectRestSpread'] | ||
}, | ||
); | ||
|
||
data.transpilers.forEach(transpilerName => { | ||
if (transpilers[transpilerName]) { | ||
errorsReport.inFile(data); | ||
babelTraverse(ast, transpilers[transpilerName].traverseObject(errorsReport)); | ||
errorsReport.exitFile(); | ||
} else { | ||
throw new Error(`Transpiler ${transpilerName} not supported`); | ||
} | ||
}); | ||
|
||
const content = babelGenerator(ast, {}, data.content).code; | ||
|
||
return { | ||
content, | ||
errors: errorsReport.getErrors(), | ||
warnings: errorsReport.getWarnings() | ||
}; | ||
}; | ||
|
||
workerpool.worker({ | ||
transpile, | ||
}); |
Oops, something went wrong.