diff --git a/packages/cubejs-backend-shared/src/promises.ts b/packages/cubejs-backend-shared/src/promises.ts index d458296060554..5dd71d729fc7b 100644 --- a/packages/cubejs-backend-shared/src/promises.ts +++ b/packages/cubejs-backend-shared/src/promises.ts @@ -265,7 +265,7 @@ export const retryWithTimeout = ( ); /** - * High order function that makes to debounce multi async calls to single call at one time + * Creates a debounced version of an asynchronous function. */ export const asyncDebounce = ( fn: (...args: Arguments[]) => Promise, diff --git a/packages/cubejs-query-orchestrator/src/orchestrator/QueryCache.ts b/packages/cubejs-query-orchestrator/src/orchestrator/QueryCache.ts index d24bc065663be..035c8ea40adc1 100644 --- a/packages/cubejs-query-orchestrator/src/orchestrator/QueryCache.ts +++ b/packages/cubejs-query-orchestrator/src/orchestrator/QueryCache.ts @@ -2,7 +2,7 @@ import crypto from 'crypto'; import csvWriter from 'csv-write-stream'; import { LRUCache } from 'lru-cache'; import { pipeline } from 'stream'; -import { getEnv, MaybeCancelablePromise, streamToArray } from '@cubejs-backend/shared'; +import { asyncDebounce, getEnv, MaybeCancelablePromise, streamToArray } from '@cubejs-backend/shared'; import { CubeStoreCacheDriver, CubeStoreDriver } from '@cubejs-backend/cubestore-driver'; import { BaseDriver, @@ -34,6 +34,12 @@ export type QueryWithParams = [ options?: QueryOptions ]; +export type LoadRefreshKeyOptions = { + requestId?: string; + skipRefreshKeyWaitForRenew?: boolean; + dataSource: string +}; + export type Query = { requestId?: string; dataSource: string; @@ -771,32 +777,31 @@ export class QueryCache { public loadRefreshKeys( cacheKeyQueries: QueryWithParams[], expireSecs: number, - options: { - requestId?: string; - skipRefreshKeyWaitForRenew?: boolean; - dataSource: string - } + options: LoadRefreshKeyOptions ) { - return cacheKeyQueries.map((q) => { - const [query, values, queryOptions]: QueryWithParams = Array.isArray(q) ? q : [q, [], {}]; - return this.cacheQueryResult( - query, - values, - [query, values], - expireSecs, - { - renewalThreshold: this.options.refreshKeyRenewalThreshold || queryOptions?.renewalThreshold || 2 * 60, - renewalKey: q, - waitForRenew: !options.skipRefreshKeyWaitForRenew, - requestId: options.requestId, - dataSource: options.dataSource, - useInMemory: true, - external: queryOptions?.external, - }, - ); - }); + return cacheKeyQueries.map((q) => this.loadRefreshKey(q, expireSecs, options)); } + public loadRefreshKey = asyncDebounce(async (q: QueryWithParams, expireSecs: number, options: LoadRefreshKeyOptions) => { + const [query, values, queryOptions]: QueryWithParams = Array.isArray(q) ? q : [q, [], {}]; + + return this.cacheQueryResult( + query, + values, + [query, values], + expireSecs, + { + renewalThreshold: this.options.refreshKeyRenewalThreshold || queryOptions?.renewalThreshold || 2 * 60, + renewalKey: q, + waitForRenew: !options.skipRefreshKeyWaitForRenew, + requestId: options.requestId, + dataSource: options.dataSource, + useInMemory: true, + external: queryOptions?.external, + }, + ); + }); + public withLock = ( key: string, ttl: number, diff --git a/packages/cubejs-schema-compiler/test/integration/postgres/pre-aggregations.test.ts b/packages/cubejs-schema-compiler/test/integration/postgres/pre-aggregations.test.ts index 81ea1daee1e3e..f793fa86b02f1 100644 --- a/packages/cubejs-schema-compiler/test/integration/postgres/pre-aggregations.test.ts +++ b/packages/cubejs-schema-compiler/test/integration/postgres/pre-aggregations.test.ts @@ -1922,7 +1922,7 @@ describe('PreAggregations', () => { }, { id: 'visitors.source' }], - cubestoreSupportMultistage: getEnv("nativeSqlPlanner") + cubestoreSupportMultistage: getEnv('nativeSqlPlanner') }); const queryAndParams = query.buildSqlAndParams(); @@ -2000,7 +2000,7 @@ describe('PreAggregations', () => { }, { id: 'visitors.source' }], - cubestoreSupportMultistage: getEnv("nativeSqlPlanner") + cubestoreSupportMultistage: getEnv('nativeSqlPlanner') }); const queryAndParams = query.buildSqlAndParams(); diff --git a/packages/cubejs-testing/test/smoke-cubesql.test.ts b/packages/cubejs-testing/test/smoke-cubesql.test.ts index 9b50ae6b531d1..49533434f697e 100644 --- a/packages/cubejs-testing/test/smoke-cubesql.test.ts +++ b/packages/cubejs-testing/test/smoke-cubesql.test.ts @@ -156,7 +156,7 @@ describe('SQL API', () => { Authorization: token, }, body: JSON.stringify({ - query: `SELECT orderDate FROM ECommerce LIMIT 0;`, + query: 'SELECT orderDate FROM ECommerce LIMIT 0;', }), });