-
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(query-orchestrator): Debounce updates for table used/touch
- Loading branch information
Showing
2 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/cubejs-query-orchestrator/src/orchestrator/MemoryQueue.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,75 @@ | ||
import { QueryCache } from './QueryCache'; | ||
|
||
export abstract class AbstractSetMemoryQueue { | ||
protected readonly queue: Set<string> = new Set(); | ||
|
||
public constructor( | ||
protected readonly capacity: number, | ||
protected readonly concurrency: number, | ||
) { | ||
|
||
} | ||
|
||
protected execution: boolean = false; | ||
|
||
public addToQueue(item: string) { | ||
this.queue.add(item); | ||
|
||
if (this.queue.size > 100) { | ||
console.log('Too large capacity', this.queue.size); | ||
} | ||
|
||
this.run().catch(e => console.log(e)); | ||
} | ||
|
||
public async run(): Promise<void> { | ||
if (this.execution) { | ||
return; | ||
} | ||
|
||
this.execution = true; | ||
|
||
try { | ||
let toExecute: string[] = []; | ||
|
||
do { | ||
for (const item of this.queue) { | ||
toExecute.push(item); | ||
this.queue.delete(item); | ||
|
||
if (toExecute.length > this.concurrency) { | ||
break; | ||
} | ||
} | ||
|
||
console.log('toExecute', toExecute.length, { | ||
toExecute | ||
}); | ||
|
||
await Promise.all(toExecute.map(async (item) => this.execute(item))); | ||
toExecute = []; | ||
} while (toExecute.length > 0); | ||
} finally { | ||
this.execution = false; | ||
} | ||
} | ||
|
||
abstract execute(item: string): Promise<void>; | ||
} | ||
|
||
export class TableTouchMemoryQueue extends AbstractSetMemoryQueue { | ||
public constructor( | ||
capacity: number, | ||
concurrency: number, | ||
protected readonly queryCache: QueryCache, | ||
protected readonly touchTablePersistTime: number | ||
) { | ||
super(capacity, concurrency); | ||
} | ||
|
||
public async execute(tableName: string): Promise<void> { | ||
const key = this.queryCache.getKey('SQL_PRE_AGGREGATIONS_TABLES_TOUCH', tableName); | ||
console.log('touch', key); | ||
await this.queryCache.getCacheDriver().set(key, new Date().getTime(), this.touchTablePersistTime); | ||
} | ||
} |
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