import { EventEmitter } from 'events'; import { LoggerFn } from '@cubejs-backend/shared'; import { QueueDriverInterface, QueryKey, QueryKeyHash, QueueId, QueryDef, QueryStageStateResponse, AddToQueueOptions, QueuePriority, RetrieveForProcessingSuccess } from '@cubejs-backend/base-driver'; import { QueryStream } from './QueryStream'; import { CacheAndQueryDriverType } from './QueryOrchestrator'; export type CancelHandlerFn = (query: QueryDef) => Promise; export type QueryHandlerFn = (query: QueryDef, cancelHandler: CancelHandlerFn) => Promise; export type StreamHandlerFn = (query: QueryDef, stream: QueryStream) => Promise; export type QueryHandlersMap = Record; export type SendProcessMessageFn = (queryKeyHash: QueryKeyHash, queueId: QueueId, retrieved: RetrieveForProcessingSuccess) => Promise | void; export type SendCancelMessageFn = (query: QueryDef, queueId: QueueId | null) => Promise | void; export type ExecuteInQueueOptions = Omit & { spanId?: string; }; export type QueryStreamWait = { promise: Promise; dispose: () => void; }; export type QueryQueueOptions = { cacheAndQueueDriver: CacheAndQueryDriverType; logger: (message: any, event: any) => void; sendCancelMessageFn?: SendCancelMessageFn; sendProcessMessageFn?: SendProcessMessageFn; cancelHandlers: Record; queryHandlers: QueryHandlersMap; streamHandler?: StreamHandlerFn; processUid?: string; concurrency?: number; continueWaitTimeout?: number; executionTimeout?: number; orphanedTimeout?: number; heartBeatInterval?: number; redisPool?: any; cubeStoreDriverFactory?: any; queueDriverFactory?: (cacheAndQueueDriver: string, queueDriverOptions: any) => QueueDriverInterface; skipQueue?: boolean; }; export declare function factoryQueueDriver(cacheAndQueueDriver: string, queueDriverOptions: any): QueueDriverInterface; export declare class QueryQueue { protected readonly redisQueuePrefix: string; protected concurrency: number; protected continueWaitTimeout: number; protected executionTimeout: number; protected orphanedTimeout: number; protected heartBeatInterval: number; protected readonly sendProcessMessageFn: SendProcessMessageFn; protected readonly sendCancelMessageFn: SendCancelMessageFn; protected readonly queryHandlers: QueryHandlersMap; protected readonly streamHandler: StreamHandlerFn | undefined; protected cancelHandlers: Record; protected logger: LoggerFn; protected processUid: string; protected queueDriver: QueueDriverInterface; protected skipQueue: boolean; /** * Persistent queries streams maps. */ protected readonly streams: Map; /** * Notify streaming queries when streaming has been started and stream is available. */ protected readonly streamEvents: EventEmitter<[never]>; constructor(redisQueuePrefix: string, options: QueryQueueOptions); getQueueDriver(): QueueDriverInterface; getConcurrency(): number; /** * Returns stream object which will be used to pipe data from data source. */ getQueryStream(queryKeyHash: QueryKeyHash): QueryStream | undefined; createQueryStream(key: QueryKeyHash, aliasNameToMember: Record | null): QueryStream; protected counter: number; generateQueueId(): number; /** * Push query to the queue and call `QueryQueue.reconcileQueue()` method if * `options.skipQueue` is set to `false`, execute query skipping queue * otherwise. * * @throw {ContinueWaitError} */ executeInQueue(queryHandler: string, queryKey: QueryKey, query: QueryDef, priority?: QueuePriority, executeOptions?: ExecuteInQueueOptions): Promise; /** * `dispose` releases the listener and the timer, it's a no-op once the promise resolved. */ protected waitForQueryStream(queryKeyHash: QueryKeyHash): QueryStreamWait; /** * @throw {Error} */ protected parseResult(result: any): any; protected reconcileAgain: boolean; protected reconcilePromise: Promise | null; /** * Run query queue reconciliation flow by calling internal `reconcileQueueImpl` * method. Returns promise which will be resolved with the reconciliation * result. */ reconcileQueue(): Promise; shutdown(): Promise; /** * Returns a full list of queued queries, including stalled, orphaned, active * and planned to be processed with their statuses and queries definitions. * * @returns {Promise} */ getQueries(): Promise; cancelQuery(queryKey: QueryKeyHash, queueId: QueueId | null): Promise; cancelQueryByRequestId(requestId: string): Promise; /** * Reconciliation logic: cancel stalled and orphaned queries from the queue * and pick some planned to be processed queries to process. * * @private * @returns {Promise} */ protected reconcileQueueImpl(): Promise; /** * Apply query timeout to the query. Throw if query execution time takes more * than the specified timeout. Returns resolved `promise` value. * * @throw {TimeoutError} */ protected queryTimeout(promise: Promise): Promise; /** * Returns the list of queries planned to be processed and the list of active * queries. */ fetchQueryStageState(): Promise; /** * Returns current state of the specified by the `stageQueryKey` query if it * exists. * * @returns {Promise | Promise<{ stage: string, timeElapsed: number }>} */ getQueryStage(stageQueryKey: QueryKey, priorityFilter?: number, queryStageState?: QueryStageStateResponse): Promise<{ stage: string; timeElapsed: number; } | { stage: string; timeElapsed?: undefined; }>; /** * Execute query without adding it to the queue. */ protected processQuerySkipQueue(query: QueryDef, queueId: QueueId): Promise; /** * Retrieves the query specified by the `queryKeyHashed` and hands it over for execution. */ protected processQuery(queryKeyHashed: QueryKeyHash, queueId: QueueId): Promise; protected dispatchQuery(queryKeyHash: QueryKeyHash, queueId: QueueId, retrieved: RetrieveForProcessingSuccess): Promise; /** * Atomically moves the query specified by `queryKeyHashed` to the active set. Returns `null` * when another node is already running the query or the concurrency budget is full. */ protected retrieveQueryForProcessing(queryKeyHashed: QueryKeyHash, queueId: QueueId): Promise; /** * Executes a retrieved query: runs its handler while keeping the queue heartbeat alive, then acks * the result. It's the counterpart of `sendProcessMessageFn` and the entry point for a custom * implementation which hands the query over to another process. */ executeQuery(queryKeyHashed: QueryKeyHash, queueId: QueueId, retrieved: RetrieveForProcessingSuccess): Promise; /** * Processing cancel query flow. */ protected processCancel(query: QueryDef, queueId: QueueId | null): Promise; protected redisHash(queryKey: QueryKey): QueryKeyHash; } //# sourceMappingURL=QueryQueue.d.ts.map