Skip to content

refactor(query-orchestrator): Local queue driver - handle processing … #9706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ class CubestoreQueueDriverConnection implements QueueDriverConnectionInterface {
toProcess.push(row.id as string);
} else if (row.status === 'active') {
active.push(row.id as string);
// TODO: getQueryStage is broken for Executing query stage...
toProcess.push(row.id as string);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ export class LocalQueueDriverConnection {
}

let added = 0;
if (!this.toProcess[key]) {

if (!this.toProcess[key] && !this.active[key]) {
this.toProcess[key] = {
order: keyScore,
queueId: options.queueId,
key
};

added = 1;
}

Expand Down Expand Up @@ -291,10 +293,14 @@ export class LocalQueueDriverConnection {
}

let added = 0;

if (Object.keys(this.active).length < this.concurrency && !this.active[key]) {
this.active[key] = { key, order: processingId };
this.active[key] = { key, order: processingId, queueId: processingId };
delete this.toProcess[key];

added = 1;
}

this.heartBeat[key] = { key, order: new Date().getTime() };

if (this.getQueueEventsBus) {
Expand Down
51 changes: 24 additions & 27 deletions packages/cubejs-query-orchestrator/src/orchestrator/QueryQueue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import R from 'ramda';
import { EventEmitter } from 'events';
import { getEnv, getProcessUid } from '@cubejs-backend/shared';
import { QueueDriverInterface, QueryKey, QueryKeyHash, QueueId, QueryDef } from '@cubejs-backend/base-driver';
import {
QueueDriverInterface,
QueryKey,
QueryKeyHash,
QueueId,
QueryDef,
QueryStageStateResponse
} from '@cubejs-backend/base-driver';
import { CubeStoreQueueDriver } from '@cubejs-backend/cubestore-driver';

import { TimeoutError } from './TimeoutError';
Expand Down Expand Up @@ -557,31 +564,20 @@ export class QueryQueue {
}
}));

/**
* There is a bug somewhere in Redis (maybe in memory too?),
* which doesn't remove queue item from pending, while it's in active state
*
* TODO(ovr): Check LocalQueueDriver for strict guarantees that item cannot be in active & pending in the same time
* TODO(ovr): Migrate to getToProcessQueries after removal of Redis
*/
const [active, toProcess] = await queueConnection.getActiveAndToProcess();
const [_active, toProcess] = await queueConnection.getActiveAndToProcess();

await Promise.all(
R.pipe(
R.filter(([queryKey, _queueId]) => {
if (active.findIndex(([p, _a]) => p === queryKey) === -1) {
const subKeys = queryKey.split('@');
if (subKeys.length === 1) {
// common queries
return true;
} else if (subKeys[1] === this.processUid) {
// current process persistent queries
return true;
} else {
// other processes persistent queries
return false;
}
const subKeys = queryKey.split('@');
if (subKeys.length === 1) {
// common queries
return true;
} else if (subKeys[1] === this.processUid) {
// current process persistent queries
return true;
} else {
// other processes persistent queries
return false;
}
}),
Expand Down Expand Up @@ -627,7 +623,7 @@ export class QueryQueue {
* Returns the list of queries planned to be processed and the list of active
* queries.
*
* @returns {Array}
* @returns {Promise<QueryStageStateResponse>}
*/
async fetchQueryStageState() {
const queueConnection = await this.queueDriver.createConnection();
Expand All @@ -644,26 +640,27 @@ export class QueryQueue {
*
* @param {*} stageQueryKey
* @param {number=} priorityFilter
* @param {Array=} queryStageState
* @param {QueryStageStateResponse=} queryStageState
* @returns {Promise<undefined> | Promise<{ stage: string, timeElapsed: number }>}
*/
async getQueryStage(stageQueryKey, priorityFilter, queryStageState) {
const [active, toProcess, allQueryDefs] = queryStageState || await this.fetchQueryStageState();

const queryDefs = toProcess.map(k => allQueryDefs[k]).filter(q => !!q);
const queryInQueue = queryDefs.find(
const queryInQueue = Object.values(allQueryDefs).find(
q => this.redisHash(q.stageQueryKey) === this.redisHash(stageQueryKey) &&
(priorityFilter != null ? q.priority === priorityFilter : true)
);

if (queryInQueue) {
if (active.indexOf(this.redisHash(queryInQueue.queryKey)) !== -1) {
return {
stage: 'Executing query',
timeElapsed: queryInQueue.startQueryTime ? new Date().getTime() - queryInQueue.startQueryTime : undefined
};
}
const index = queryDefs.filter(q => active.indexOf(this.redisHash(q.queryKey)) === -1).indexOf(queryInQueue);

const index = toProcess
.filter((queryKey) => (priorityFilter != null ? allQueryDefs[queryKey]?.priority === priorityFilter : true))
.indexOf(this.redisHash(queryInQueue.queryKey));
if (index !== -1) {
return index !== -1 ? { stage: `#${index + 1} in queue` } : undefined;
}
Expand Down
Loading