Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 1 addition & 1 deletion packages/cubejs-query-orchestrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@cubejs-backend/shared": "1.2.29",
"csv-write-stream": "^2.0.0",
"generic-pool": "^3.8.2",
"lru-cache": "^5.1.1",
"lru-cache": "^11.1.0",
"ramda": "^0.27.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getEnv, } from '@cubejs-backend/shared';

import { BaseDriver, InlineTable, } from '@cubejs-backend/base-driver';
import { CubeStoreDriver } from '@cubejs-backend/cubestore-driver';
import LRUCache from 'lru-cache';
import { LRUCache } from 'lru-cache';

import { PreAggTableToTempTable, Query, QueryBody, QueryCache, QueryWithParams } from './QueryCache';
import { DriverFactory, DriverFactoryByDataSource } from './DriverFactory';
Expand Down Expand Up @@ -282,8 +282,8 @@ export class PreAggregations {
this.getQueueEventsBus = options.getQueueEventsBus;
this.touchCache = new LRUCache({
max: getEnv('touchPreAggregationCacheMaxCount'),
maxAge: getEnv('touchPreAggregationCacheMaxAge') * 1000,
stale: false,
ttl: getEnv('touchPreAggregationCacheMaxAge') * 1000,
allowStale: false,
updateAgeOnGet: false
});
}
Expand Down Expand Up @@ -330,7 +330,7 @@ export class PreAggregations {
this.touchTablePersistTime
);
} catch (e: unknown) {
this.touchCache.del(tableName);
this.touchCache.delete(tableName);

throw e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from 'crypto';
import csvWriter from 'csv-write-stream';
import LRUCache from 'lru-cache';
import { LRUCache } from 'lru-cache';
import { pipeline } from 'stream';
import { getEnv, MaybeCancelablePromise, streamToArray } from '@cubejs-backend/shared';
import { CubeStoreCacheDriver, CubeStoreDriver } from '@cubejs-backend/cubestore-driver';
Expand Down Expand Up @@ -909,7 +909,7 @@ export class QueryCache {
inMemoryValue.renewalKey !== renewalKey
) || renewedAgo > expiration * 1000 || renewedAgo > inMemoryCacheDisablePeriod
) {
this.memoryCache.del(redisKey);
this.memoryCache.delete(redisKey);
} else {
this.logger('Found in memory cache entry', {
cacheKey,
Expand Down
3 changes: 1 addition & 2 deletions packages/cubejs-schema-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"inflection": "^1.12.0",
"joi": "^17.8.3",
"js-yaml": "^4.1.0",
"lru-cache": "^5.1.1",
"lru-cache": "^11.1.0",
"moment-timezone": "^0.5.46",
"node-dijkstra": "^2.5.0",
"ramda": "^0.27.2",
Expand All @@ -66,7 +66,6 @@
"@types/babel__traverse": "^7.20.5",
"@types/inflection": "^1.5.28",
"@types/jest": "^27",
"@types/lru-cache": "^5.1.0",
"@types/node": "^18",
"@types/ramda": "^0.27.34",
"@types/sqlstring": "^2.3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LRUCache from 'lru-cache';
import { LRUCache } from 'lru-cache';
import { QueryCache } from '../adapter/QueryCache';

export class CompilerCache extends QueryCache {
Expand All @@ -11,13 +11,13 @@ export class CompilerCache extends QueryCache {

this.queryCache = new LRUCache({
max: maxQueryCacheSize || 10000,
maxAge: (maxQueryCacheAge * 1000) || 1000 * 60 * 10,
ttl: (maxQueryCacheAge * 1000) || 1000 * 60 * 10,
updateAgeOnGet: true
});

this.rbacCache = new LRUCache({
max: 10000,
maxAge: 1000 * 60 * 5, // 5 minutes
ttl: 1000 * 60 * 5, // 5 minutes
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'crypto';
import vm from 'vm';
import fs from 'fs';
import path from 'path';
Expand Down Expand Up @@ -47,6 +48,7 @@ export class DataSchemaCompiler {
this.pythonContext = null;
this.workerPool = null;
this.compilerId = options.compilerId;
this.compiledScriptCache = options.compiledScriptCache;
}

compileObjects(compileServices, objects, errorsReport) {
Expand Down Expand Up @@ -370,6 +372,18 @@ export class DataSchemaCompiler {
}
}

getJsScript(file) {
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');

if (this.compiledScriptCache.has(cacheKey)) {
return this.compiledScriptCache.get(cacheKey);
}

const script = new vm.Script(file.content, { filename: file.fileName, timeout: 15000 });
this.compiledScriptCache.set(cacheKey, script);
return script;
}

compileJsFile(file, errorsReport, cubes, contexts, exports, asyncModules, toCompile, compiledFiles, { doSyntaxCheck } = { doSyntaxCheck: false }) {
if (doSyntaxCheck) {
// There is no need to run syntax check for data model files
Expand All @@ -382,7 +396,9 @@ export class DataSchemaCompiler {
}

try {
vm.runInNewContext(file.content, {
const script = this.getJsScript(file);

script.runInNewContext({
view: (name, cube) => (
!cube ?
this.cubeFactory({ ...name, fileName: file.fileName, isView: true }) :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SchemaFileRepository } from '@cubejs-backend/shared';
import { NativeInstance } from '@cubejs-backend/native';
import { v4 as uuidv4 } from 'uuid';
import { LRUCache } from 'lru-cache';
import vm from 'vm';

import { CubeValidator } from './CubeValidator';
import { DataSchemaCompiler } from './DataSchemaCompiler';
Expand Down Expand Up @@ -32,6 +34,7 @@ export type PrepareCompilerOptions = {
standalone?: boolean;
headCommitId?: string;
adapter?: string;
compiledScriptCache?: LRUCache<string, vm.Script>;
};

export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareCompilerOptions = {}) => {
Expand All @@ -49,6 +52,8 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
const compilerCache = new CompilerCache({ maxQueryCacheSize, maxQueryCacheAge });
const yamlCompiler = new YamlCompiler(cubeSymbols, cubeDictionary, nativeInstance, viewCompiler);

const compiledScriptCache = options.compiledScriptCache || new LRUCache<string, vm.Script>({ max: 250 });

const transpilers: TranspilerInterface[] = [
new ValidationTranspiler(),
new ImportExportTranspiler(),
Expand All @@ -66,6 +71,7 @@ export const prepareCompiler = (repo: SchemaFileRepository, options: PrepareComp
preTranspileCubeCompilers: [cubeSymbols, cubeValidator],
transpilers,
viewCompilationGate,
compiledScriptCache,
viewCompilers: [viewCompiler],
cubeCompilers: [cubeEvaluator, joinGraph, metaTransformer],
contextCompilers: [contextEvaluator],
Expand Down
3 changes: 1 addition & 2 deletions packages/cubejs-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"joi": "^17.8.3",
"jsonwebtoken": "^9.0.2",
"lodash.clonedeep": "^4.5.0",
"lru-cache": "^5.1.1",
"lru-cache": "^11.1.0",
"moment": "^2.29.1",
"node-fetch": "^2.6.0",
"p-limit": "^3.1.0",
Expand All @@ -67,7 +67,6 @@
"@types/fs-extra": "^9.0.8",
"@types/jest": "^27",
"@types/jsonwebtoken": "^9.0.2",
"@types/lru-cache": "^5.1.0",
"@types/node": "^18",
"@types/node-fetch": "^2.5.7",
"@types/ramda": "^0.27.34",
Expand Down
9 changes: 8 additions & 1 deletion packages/cubejs-server-core/src/core/CompilerApi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import crypto from 'crypto';
import R from 'ramda';
import { createQuery, compile, queryClass, PreAggregations, QueryFactory } from '@cubejs-backend/schema-compiler';
import { v4 as uuidv4, parse as uuidParse, stringify as uuidStringify } from 'uuid';
import { v4 as uuidv4, parse as uuidParse } from 'uuid';
import { LRUCache } from 'lru-cache';
import { NativeInstance } from '@cubejs-backend/native';

export class CompilerApi {
Expand Down Expand Up @@ -29,6 +30,11 @@ export class CompilerApi {
this.sqlCache = options.sqlCache;
this.standalone = options.standalone;
this.nativeInstance = this.createNativeInstance();
this.compiledScriptCache = new LRUCache({
max: options.compilerCacheSize || 250,
ttl: options.maxCompilerCacheKeepAlive,
updateAgeOnGet: options.updateCompilerCacheKeepAlive
});
}

setGraphQLSchema(schema) {
Expand Down Expand Up @@ -83,6 +89,7 @@ export class CompilerApi {
allowJsDuplicatePropsInSchema: this.allowJsDuplicatePropsInSchema,
standalone: this.standalone,
nativeInstance: this.nativeInstance,
compiledScriptCache: this.compiledScriptCache,
});
this.queryFactory = await this.createQueryFactory(compilers);

Expand Down
11 changes: 7 additions & 4 deletions packages/cubejs-server-core/src/core/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable global-require,no-return-assign */
import crypto from 'crypto';
import fs from 'fs-extra';
import LRUCache from 'lru-cache';
import { LRUCache } from 'lru-cache';
import isDocker from 'is-docker';
import pLimit from 'p-limit';

Expand Down Expand Up @@ -203,7 +203,7 @@ export class CubejsServerCore {

this.compilerCache = new LRUCache<string, CompilerApi>({
max: this.options.compilerCacheSize || 250,
maxAge: this.options.maxCompilerCacheKeepAlive,
ttl: this.options.maxCompilerCacheKeepAlive,
updateAgeOnGet: this.options.updateCompilerCacheKeepAlive
});

Expand All @@ -224,7 +224,7 @@ export class CubejsServerCore {
// proactively free up old cache values occasionally
if (this.options.maxCompilerCacheKeepAlive) {
this.maxCompilerCacheKeep = setInterval(
() => this.compilerCache.prune(),
() => this.compilerCache.purgeStale(),
this.options.maxCompilerCacheKeepAlive
);
}
Expand Down Expand Up @@ -554,7 +554,7 @@ export class CubejsServerCore {
await this.orchestratorStorage.releaseConnections();

this.orchestratorStorage.clear();
this.compilerCache.reset();
this.compilerCache.clear();

this.reloadEnvVariables();

Expand Down Expand Up @@ -714,6 +714,9 @@ export class CubejsServerCore {
standalone: this.standalone,
allowNodeRequire: options.allowNodeRequire,
fastReload: options.fastReload || getEnv('fastReload'),
compilerCacheSize: this.options.compilerCacheSize || 250,
maxCompilerCacheKeepAlive: this.options.maxCompilerCacheKeepAlive,
updateCompilerCacheKeepAlive: this.options.updateCompilerCacheKeepAlive
},
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cubejs-templates/src/PackageFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class PackageFetcher {

protected repoArchivePath: string;

public constructor(private repo: Repository) {
public constructor(private readonly repo: Repository) {
this.tmpFolderPath = path.resolve('.', 'node_modules', '.tmp');

this.init();
Expand Down Expand Up @@ -53,7 +53,7 @@ export class PackageFetcher {
(await proxyFetch(url)).body.pipe(writer);

return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('finish', resolve as () => void);
writer.on('error', reject);
});
}
Expand Down
26 changes: 7 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9601,11 +9601,6 @@
resolved "https://registry.yarnpkg.com/@types/jwk-to-pem/-/jwk-to-pem-2.0.1.tgz#ba6949f447e02cb7bebf101551e3a4dea5f4fde4"
integrity sha512-QXmRPhR/LPzvXBHTPfG2BBfMTkNLUD7NyRcPft8m5xFCeANa1BZyLgT0Gw+OxdWx6i1WCpT27EqyggP4UUHMrA==

"@types/lru-cache@^5.1.0":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-5.1.1.tgz#c48c2e27b65d2a153b19bfc1a317e30872e01eef"
integrity sha512-ssE3Vlrys7sdIzs5LOxCzTVMsU7i9oa/IaW92wF32JFb3CVczqOkru2xspuKczHEbG3nvmPY7IFqVmGGHdNbYw==

"@types/mime@^1":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
Expand Down Expand Up @@ -17154,7 +17149,7 @@ generate-object-property@^1.0.0:
dependencies:
is-property "^1.0.0"

generic-pool@*, generic-pool@^3.6.0, generic-pool@^3.8.2:
generic-pool@*, generic-pool@^3.8.2:
version "3.9.0"
resolved "https://registry.yarnpkg.com/generic-pool/-/generic-pool-3.9.0.tgz#36f4a678e963f4fdb8707eab050823abc4e8f5e4"
integrity sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==
Expand Down Expand Up @@ -20685,16 +20680,16 @@ lower-case@^2.0.2:
dependencies:
tslib "^2.0.3"

lru-cache@^10.0.1:
version "10.1.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.1.0.tgz#2098d41c2dc56500e6c88584aa656c84de7d0484"
integrity sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==

lru-cache@^10.2.0, lru-cache@^10.2.2:
lru-cache@^10.0.1, lru-cache@^10.2.0, lru-cache@^10.2.2, "lru-cache@^9.1.1 || ^10.0.0":
version "10.4.3"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.4.3.tgz#410fc8a17b70e598013df257c2446b7f3383f119"
integrity sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==

lru-cache@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-11.1.0.tgz#afafb060607108132dbc1cf8ae661afb69486117"
integrity sha512-QIXZUBJUx+2zHUdQujWejBkcD9+cs94tLn0+YL8UrCh+D5sCXZ4c7LaEH48pNwRY3MLDgqUFyhlCyjJPf1WP0A==

lru-cache@^4.0.1, lru-cache@^4.1.2, lru-cache@^4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.5.tgz#8bbe50ea85bed59bc9e33dcab8235ee9bcf443cd"
Expand Down Expand Up @@ -20722,13 +20717,6 @@ lru-cache@^7.14.1, lru-cache@^7.7.1:
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.18.3.tgz#f793896e0fd0e954a59dfdd82f0773808df6aa89"
integrity sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==

"lru-cache@^9.1.1 || ^10.0.0":
version "10.0.2"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.0.2.tgz#34504678cc3266b09b8dfd6fab4e1515258271b7"
integrity sha512-Yj9mA8fPiVgOUpByoTZO5pNrcl5Yk37FcSHsUINpAsaBIEZIuqcCclDZJCVxqQShDsmYX8QG63svJiTbOATZwg==
dependencies:
semver "^7.3.5"

lru.min@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/lru.min/-/lru.min-1.1.1.tgz#146e01e3a183fa7ba51049175de04667d5701f0e"
Expand Down