1
1
import R from 'ramda' ;
2
2
import { EventEmitter } from 'events' ;
3
3
import { getEnv , getProcessUid } from '@cubejs-backend/shared' ;
4
- import { QueueDriverInterface , QueryKey , QueryKeyHash , QueueId , QueryDef } from '@cubejs-backend/base-driver' ;
4
+ import {
5
+ QueueDriverInterface ,
6
+ QueryKey ,
7
+ QueryKeyHash ,
8
+ QueueId ,
9
+ QueryDef ,
10
+ QueryStageStateResponse
11
+ } from '@cubejs-backend/base-driver' ;
5
12
import { CubeStoreQueueDriver } from '@cubejs-backend/cubestore-driver' ;
6
13
7
14
import { TimeoutError } from './TimeoutError' ;
@@ -557,31 +564,20 @@ export class QueryQueue {
557
564
}
558
565
} ) ) ;
559
566
560
- /**
561
- * There is a bug somewhere in Redis (maybe in memory too?),
562
- * which doesn't remove queue item from pending, while it's in active state
563
- *
564
- * TODO(ovr): Check LocalQueueDriver for strict guarantees that item cannot be in active & pending in the same time
565
- * TODO(ovr): Migrate to getToProcessQueries after removal of Redis
566
- */
567
- const [ active , toProcess ] = await queueConnection . getActiveAndToProcess ( ) ;
567
+ const [ _active , toProcess ] = await queueConnection . getActiveAndToProcess ( ) ;
568
568
569
569
await Promise . all (
570
570
R . pipe (
571
571
R . filter ( ( [ queryKey , _queueId ] ) => {
572
- if ( active . findIndex ( ( [ p , _a ] ) => p === queryKey ) === - 1 ) {
573
- const subKeys = queryKey . split ( '@' ) ;
574
- if ( subKeys . length === 1 ) {
575
- // common queries
576
- return true ;
577
- } else if ( subKeys [ 1 ] === this . processUid ) {
578
- // current process persistent queries
579
- return true ;
580
- } else {
581
- // other processes persistent queries
582
- return false ;
583
- }
572
+ const subKeys = queryKey . split ( '@' ) ;
573
+ if ( subKeys . length === 1 ) {
574
+ // common queries
575
+ return true ;
576
+ } else if ( subKeys [ 1 ] === this . processUid ) {
577
+ // current process persistent queries
578
+ return true ;
584
579
} else {
580
+ // other processes persistent queries
585
581
return false ;
586
582
}
587
583
} ) ,
@@ -627,7 +623,7 @@ export class QueryQueue {
627
623
* Returns the list of queries planned to be processed and the list of active
628
624
* queries.
629
625
*
630
- * @returns {Array }
626
+ * @returns {Promise<QueryStageStateResponse> }
631
627
*/
632
628
async fetchQueryStageState ( ) {
633
629
const queueConnection = await this . queueDriver . createConnection ( ) ;
@@ -644,26 +640,27 @@ export class QueryQueue {
644
640
*
645
641
* @param {* } stageQueryKey
646
642
* @param {number= } priorityFilter
647
- * @param {Array = } queryStageState
643
+ * @param {QueryStageStateResponse = } queryStageState
648
644
* @returns {Promise<undefined> | Promise<{ stage: string, timeElapsed: number }> }
649
645
*/
650
646
async getQueryStage ( stageQueryKey , priorityFilter , queryStageState ) {
651
647
const [ active , toProcess , allQueryDefs ] = queryStageState || await this . fetchQueryStageState ( ) ;
652
648
653
- const queryDefs = toProcess . map ( k => allQueryDefs [ k ] ) . filter ( q => ! ! q ) ;
654
- const queryInQueue = queryDefs . find (
649
+ const queryInQueue = Object . values ( allQueryDefs ) . find (
655
650
q => this . redisHash ( q . stageQueryKey ) === this . redisHash ( stageQueryKey ) &&
656
651
( priorityFilter != null ? q . priority === priorityFilter : true )
657
652
) ;
658
-
659
653
if ( queryInQueue ) {
660
654
if ( active . indexOf ( this . redisHash ( queryInQueue . queryKey ) ) !== - 1 ) {
661
655
return {
662
656
stage : 'Executing query' ,
663
657
timeElapsed : queryInQueue . startQueryTime ? new Date ( ) . getTime ( ) - queryInQueue . startQueryTime : undefined
664
658
} ;
665
659
}
666
- const index = queryDefs . filter ( q => active . indexOf ( this . redisHash ( q . queryKey ) ) === - 1 ) . indexOf ( queryInQueue ) ;
660
+
661
+ const index = toProcess
662
+ . filter ( ( queryKey ) => ( priorityFilter != null ? allQueryDefs [ queryKey ] ?. priority === priorityFilter : true ) )
663
+ . indexOf ( this . redisHash ( queryInQueue . queryKey ) ) ;
667
664
if ( index !== - 1 ) {
668
665
return index !== - 1 ? { stage : `#${ index + 1 } in queue` } : undefined ;
669
666
}
0 commit comments