Skip to content
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

feat: Remove job from pre-aggregations queue, Debug API #3148

Merged
merged 5 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/cubejs-api-gateway/src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
normalizeQuery,
normalizeQueryPreAggregations,
normalizeQueryPreAggregationPreview,
normalizeQueryCancelPreAggregations,
QUERY_TYPE
} from './query';
import {
Expand Down Expand Up @@ -405,6 +406,14 @@ export class ApiGateway {
res: this.resToResultFn(res)
});
}));

app.post('/cubejs-system/v1/pre-aggregations/cancel', jsonParser, systemMiddlewares, (async (req, res) => {
await this.cancelPreAggregationsFromQueue({
query: req.body.query,
context: req.context,
res: this.resToResultFn(res)
});
}));
}

app.get('/readyz', guestMiddlewares, cachedHandler(this.readiness));
Expand Down Expand Up @@ -602,6 +611,23 @@ export class ApiGateway {
}
}

public async cancelPreAggregationsFromQueue(
{ query, context, res }: { query: any, context: RequestContext, res: ResponseResultFn }
) {
const requestStarted = new Date();
try {
const { queryKeys, dataSource } = normalizeQueryCancelPreAggregations(this.parseQueryParam(query));
const orchestratorApi = this.getAdapterApi(context);
res({
result: await orchestratorApi.cancelPreAggregationQueriesFromQueue(queryKeys, dataSource)
});
} catch (e) {
this.handleError({
e, context, res, requestStarted
});
}
}

protected async getNormalizedQueries(query, context: RequestContext): Promise<any> {
query = this.parseQueryParam(query);
let queryType = QUERY_TYPE.REGULAR_QUERY;
Expand Down
14 changes: 14 additions & 0 deletions packages/cubejs-api-gateway/src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,17 @@ export const normalizeQueryPreAggregationPreview = (query) => {

return query;
};

const queryCancelPreAggregationPreviewSchema = Joi.object().keys({
dataSource: Joi.string(),
queryKeys: Joi.array().items(Joi.string())
});

export const normalizeQueryCancelPreAggregations = query => {
const { error } = Joi.validate(query, queryCancelPreAggregationPreviewSchema);
if (error) {
throw new UserError(`Invalid query format: ${error.message || error.toString()}`);
}

return query;
};
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ export class LocalQueueDriverConnection {
return [query];
}

async cancelQuery(queryKey) {
const query = await this.getQueryAndRemove(queryKey);

if (this.getQueueEventsBus) {
this.getQueueEventsBus().emit({
event: 'cancelQuery',
redisQueuePrefix: this.redisQueuePrefix,
queryKey: this.redisHash(queryKey),
payload: query
});
}

return true;
}

async setResultAndRemoveQuery(queryKey, executionResult, processingId) {
const key = this.redisHash(queryKey);
if (this.processingLocks[key] !== processingId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,8 +1457,13 @@ export class PreAggregations {
return data.filter(res => res);
}

public async getQueueState(dataSource = undefined) {
public async getQueueState(dataSource: string) {
const queries = await this.getQueue(dataSource).getQueries();
return queries;
}

public async cancelQueriesFromQueue(queryKeys: string[], dataSource: string) {
const queue = this.getQueue(dataSource);
return Promise.all(queryKeys.map(queryKey => queue.cancelQuery(queryKey)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,12 @@ export class QueryOrchestrator {
return this.preAggregations.expandPartitionsInPreAggregations(queryBody);
}

public async getPreAggregationQueueStates() {
return this.preAggregations.getQueueState();
public async getPreAggregationQueueStates(dataSource = 'default') {
return this.preAggregations.getQueueState(dataSource);
}

public async cancelPreAggregationQueriesFromQueue(queryKeys: string[], dataSource = 'default') {
return this.preAggregations.cancelQueriesFromQueue(queryKeys, dataSource);
}

public async subscribeQueueEvents(id, callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ export class QueryQueue {
}
}

async cancelQuery(queryKey) {
const redisClient = await this.queueDriver.createConnection();
try {
return redisClient.cancelQuery(queryKey);
} finally {
this.queueDriver.release(redisClient);
}
}

async reconcileQueueImpl() {
const redisClient = await this.queueDriver.createConnection();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class RedisQueueDriverConnection {
.zcard(this.toProcessRedisKey());

if (this.getQueueEventsBus) {
console.log('addedToQueue', this.redisHash(queryKey));
tx.publish(
this.getQueueEventsBus().eventsChannel,
JSON.stringify({
Expand Down Expand Up @@ -87,6 +88,24 @@ export class RedisQueueDriverConnection {
return [JSON.parse(query), ...restResult];
}

async cancelQuery(queryKey) {
const [query] = await this.getQueryAndRemove(queryKey);

if (this.getQueueEventsBus) {
await this.redisClient.publish(
this.getQueueEventsBus().eventsChannel,
JSON.stringify({
event: 'cancelQuery',
redisQueuePrefix: this.redisQueuePrefix,
queryKey: this.redisHash(queryKey),
payload: query
})
);
}

return true;
}

async setResultAndRemoveQuery(queryKey, executionResult, processingId) {
try {
await this.redisClient.watchAsync(this.queryProcessingLockKey(queryKey));
Expand Down
5 changes: 5 additions & 0 deletions packages/cubejs-server-core/src/core/OrchestratorApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ export class OrchestratorApi {
return result;
}

public async cancelPreAggregationQueriesFromQueue(queryKeys: string[], dataSource: string) {
const result = await this.orchestrator.cancelPreAggregationQueriesFromQueue(queryKeys, dataSource);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const result = await this.orchestrator.cancelPreAggregationQueriesFromQueue(queryKeys, dataSource);
return this.orchestrator.cancelPreAggregationQueriesFromQueue(queryKeys, dataSource);

return result;
}

public async subscribeQueueEvents(id, callback) {
return this.orchestrator.subscribeQueueEvents(id, callback);
}
Expand Down