Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,13 @@ const variables: Record<string, (...args: any) => any> = {
.default(8192)
.asInt(),

/**
* Max number of elements
*/
usedPreAggregationCacheMaxCount: (): number => get('CUBEJS_USED_PRE_AGG_CACHE_MAX_COUNT')
.default(8192)
.asInt(),

/**
* Max number of elements
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,11 @@ type PreAggregationQueryBody = QueryBody & {
};

export class PreAggregations {
public options: PreAggregationsOptions;
public readonly options: PreAggregationsOptions;

public externalDriverFactory: DriverFactory;
public readonly externalDriverFactory: DriverFactory;

public structureVersionPersistTime: any;
public readonly structureVersionPersistTime: any;

private readonly touchTablePersistTime: number;

Expand All @@ -260,6 +260,8 @@ export class PreAggregations {

private readonly queue: Record<string, QueryQueue> = {};

private readonly usedCache: LRUCache<string, true>;

private readonly touchCache: LRUCache<string, true>;

public constructor(
Expand All @@ -277,6 +279,25 @@ export class PreAggregations {
this.dropPreAggregationsWithoutTouch = options.dropPreAggregationsWithoutTouch || getEnv('dropPreAggregationsWithoutTouch');
this.usedTablePersistTime = options.usedTablePersistTime || getEnv('dbQueryTimeout');
this.externalRefresh = options.externalRefresh;

/**
* Comment for both caches: used and touch:
*
* Memory usage: By default, it defines max as 8096 keys,
* let's assume that avg key size is 64 symbols, it's around 100 bytes
* with V8's internal stuff:
*
* 100 x 8096 = 809 bytes, max 1Mb in memory.
*
* However, this is a theoretical limit. In practice, even a couple of thousand
* is a very large number
*/
this.usedCache = new LRUCache({
max: getEnv('usedPreAggregationCacheMaxCount'),
ttl: Math.round(this.usedTablePersistTime / 2) * 1000,
allowStale: false,
updateAgeOnGet: false
});
this.touchCache = new LRUCache({
max: getEnv('touchPreAggregationCacheMaxCount'),
ttl: getEnv('touchPreAggregationCacheMaxAge') * 1000,
Expand All @@ -301,11 +322,23 @@ export class PreAggregations {
}

public async addTableUsed(tableName: string): Promise<void> {
await this.queryCache.getCacheDriver().set(
this.tablesUsedRedisKey(tableName),
true,
this.usedTablePersistTime
);
if (this.usedCache.has(tableName)) {
return;
}

try {
this.usedCache.set(tableName, true);

await this.queryCache.getCacheDriver().set(
this.tablesUsedRedisKey(tableName),
true,
this.usedTablePersistTime
);
} catch (e: unknown) {
this.usedCache.delete(tableName);

throw e;
}
}

public async tablesUsed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class QueryCache {
protected memoryCache: LRUCache<string, CacheEntry>;

public constructor(
protected readonly redisPrefix: string,
protected readonly cachePrefix: string,
protected readonly driverFactory: DriverFactoryByDataSource,
protected readonly logger: any,
public readonly options: QueryCacheOptions
Expand Down Expand Up @@ -165,11 +165,7 @@ export class QueryCache {
}

public getKey(catalog: string, key: string): string {
if (this.cacheDriver instanceof CubeStoreCacheDriver) {
return `${this.redisPrefix}#${catalog}:${key}`;
} else {
return `${catalog}_${this.redisPrefix}_${key}`;
}
return `${this.cachePrefix}#${catalog}:${key}`;
}

/**
Expand Down Expand Up @@ -469,7 +465,7 @@ export class QueryCache {
const queueOptions = await this.options.queueOptions(dataSource);
if (!this.queue[dataSource]) {
this.queue[dataSource] = QueryCache.createQueue(
`SQL_QUERY_${this.redisPrefix}_${dataSource}`,
`SQL_QUERY_${this.cachePrefix}_${dataSource}`,
() => this.driverFactory(dataSource),
(client, req) => {
this.logger('Executing SQL', { ...req });
Expand Down Expand Up @@ -537,7 +533,7 @@ export class QueryCache {
public getExternalQueue() {
if (!this.externalQueue) {
this.externalQueue = QueryCache.createQueue(
`SQL_QUERY_EXT_${this.redisPrefix}`,
`SQL_QUERY_EXT_${this.cachePrefix}`,
this.options.externalDriverFactory,
(client, q) => {
this.logger('Executing SQL', {
Expand Down
Loading