-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add PPL query cancellation via Discover cancel button #11593
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
mengweieric
merged 5 commits into
opensearch-project:main
from
ahkcs:feature/ppl-query-cancellation
Mar 26, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d6a77bb
[Query] Add PPL query cancellation support via Discover cancel button
ahkcs a50537a
Merge branch 'main' into feature/ppl-query-cancellation
ahkcs a47e90c
Fire-and-forget PPL cancel request to avoid blocking UI
ahkcs 0216993
Use exact PPL action name instead of wildcard in task filter
ahkcs 95a6f6d
Revert action filter back to wildcard *ppl*
ahkcs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
267 changes: 267 additions & 0 deletions
267
src/plugins/query_enhancements/server/routes/ppl_cancel.test.ts
This file contains hidden or 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,267 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { loggingSystemMock } from '../../../../core/server/mocks'; | ||
| import { registerPPLCancelRoute } from './ppl_cancel'; | ||
|
|
||
| describe('registerPPLCancelRoute', () => { | ||
| let handler: any; | ||
| let logger: ReturnType<typeof loggingSystemMock.create>['get']; | ||
|
|
||
| const createResponse = () => ({ | ||
| ok: jest.fn((v) => v), | ||
| notFound: jest.fn((v) => v), | ||
| custom: jest.fn((v) => v), | ||
| }); | ||
|
|
||
| const createContext = (transportRequestMock: jest.Mock) => | ||
| ({ | ||
| core: { | ||
| opensearch: { | ||
| client: { | ||
| asCurrentUser: { | ||
| transport: { | ||
| request: transportRequestMock, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| dataSource: { | ||
| opensearch: { | ||
| getClient: jest.fn().mockResolvedValue({ | ||
| transport: { | ||
| request: transportRequestMock, | ||
| }, | ||
| }), | ||
| }, | ||
| }, | ||
| } as any); | ||
|
|
||
| const tasksResponseWithMatch = (queryId: string) => ({ | ||
| body: { | ||
| nodes: { | ||
| node1: { | ||
| tasks: { | ||
| 'node1:12345': { | ||
| description: `PPL [queryId=${queryId}]: source=test`, | ||
| action: 'indices:data/read/ppl', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const tasksResponseNoMatch = { | ||
| body: { | ||
| nodes: { | ||
| node1: { | ||
| tasks: { | ||
| 'node1:99999': { | ||
| description: 'PPL [queryId=other-id]: source=test', | ||
| action: 'indices:data/read/ppl', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const tasksResponseEmpty = { | ||
| body: { | ||
| nodes: {}, | ||
| }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| const router = { | ||
| post: jest.fn((_, h) => { | ||
| handler = h; | ||
| }), | ||
| } as any; | ||
| logger = loggingSystemMock.create().get(); | ||
| registerPPLCancelRoute(router, logger); | ||
| }); | ||
|
|
||
| it('should cancel a matching PPL task successfully', async () => { | ||
| const queryId = 'test-uuid-1234'; | ||
| const transportRequest = jest | ||
| .fn() | ||
| .mockResolvedValueOnce(tasksResponseWithMatch(queryId)) | ||
| .mockResolvedValueOnce({ body: { nodes: {} } }); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| expect(transportRequest).toHaveBeenCalledWith({ | ||
| method: 'GET', | ||
| path: '/_tasks', | ||
| querystring: { actions: '*ppl*', detailed: 'true' }, | ||
| }); | ||
| expect(transportRequest).toHaveBeenCalledWith({ | ||
| method: 'POST', | ||
| path: '/_tasks/node1:12345/_cancel', | ||
| }); | ||
| expect(res.ok).toHaveBeenCalledWith({ | ||
| body: { | ||
| cancelled: true, | ||
| queryId, | ||
| tasks: [ | ||
| expect.objectContaining({ | ||
| taskId: 'node1:12345', | ||
| nodeId: 'node1', | ||
| }), | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should return 404 when no matching task is found', async () => { | ||
| const queryId = 'nonexistent-uuid'; | ||
| const transportRequest = jest.fn().mockResolvedValueOnce(tasksResponseNoMatch); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| expect(transportRequest).toHaveBeenCalledTimes(1); | ||
| expect(res.notFound).toHaveBeenCalledWith({ | ||
| body: { message: `No running PPL task found for queryId: ${queryId}` }, | ||
| }); | ||
| }); | ||
|
|
||
| it('should return 404 when no tasks exist', async () => { | ||
| const queryId = 'some-uuid'; | ||
| const transportRequest = jest.fn().mockResolvedValueOnce(tasksResponseEmpty); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| expect(res.notFound).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should use data source client when dataSourceId is provided', async () => { | ||
| const queryId = 'ds-uuid'; | ||
| const dataSourceId = 'ds-1'; | ||
| const transportRequest = jest | ||
| .fn() | ||
| .mockResolvedValueOnce(tasksResponseWithMatch(queryId)) | ||
| .mockResolvedValueOnce({ body: {} }); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId, dataSourceId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| expect(context.dataSource.opensearch.getClient).toHaveBeenCalledWith(dataSourceId); | ||
| expect(res.ok).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should use core client when dataSourceId is not provided', async () => { | ||
| const queryId = 'local-uuid'; | ||
| const transportRequest = jest | ||
| .fn() | ||
| .mockResolvedValueOnce(tasksResponseWithMatch(queryId)) | ||
| .mockResolvedValueOnce({ body: {} }); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| expect(context.dataSource.opensearch.getClient).not.toHaveBeenCalled(); | ||
| expect(res.ok).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle OpenSearch errors and coerce 500 to 503', async () => { | ||
| const queryId = 'error-uuid'; | ||
| const transportRequest = jest.fn().mockRejectedValueOnce({ | ||
| statusCode: 500, | ||
| message: 'internal server error', | ||
| }); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| expect(res.custom).toHaveBeenCalledWith({ | ||
| statusCode: 503, | ||
| body: 'internal server error', | ||
| }); | ||
| }); | ||
|
|
||
| it('should cancel all matching tasks across multiple nodes', async () => { | ||
| const queryId = 'multi-task-uuid'; | ||
| const transportRequest = jest | ||
| .fn() | ||
| .mockResolvedValueOnce({ | ||
| body: { | ||
| nodes: { | ||
| node1: { | ||
| tasks: { | ||
| 'node1:111': { | ||
| description: `PPL [queryId=${queryId}]: source=test`, | ||
| }, | ||
| }, | ||
| }, | ||
| node2: { | ||
| tasks: { | ||
| 'node2:222': { | ||
| description: `PPL [queryId=${queryId}]: source=test | stats count()`, | ||
| }, | ||
| 'node2:333': { | ||
| description: 'PPL [queryId=other]: source=other', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| .mockResolvedValue({ body: {} }); | ||
|
|
||
| const context = createContext(transportRequest); | ||
| const req = { body: { queryId } } as any; | ||
| const res = createResponse(); | ||
|
|
||
| await handler(context, req, res); | ||
|
|
||
| // Should cancel both matching tasks, not the non-matching one | ||
| expect(transportRequest).toHaveBeenCalledWith({ | ||
| method: 'POST', | ||
| path: '/_tasks/node1:111/_cancel', | ||
| }); | ||
| expect(transportRequest).toHaveBeenCalledWith({ | ||
| method: 'POST', | ||
| path: '/_tasks/node2:222/_cancel', | ||
| }); | ||
| expect(transportRequest).not.toHaveBeenCalledWith({ | ||
| method: 'POST', | ||
| path: '/_tasks/node2:333/_cancel', | ||
| }); | ||
| expect(res.ok).toHaveBeenCalledWith({ | ||
| body: { | ||
| cancelled: true, | ||
| queryId, | ||
| tasks: expect.arrayContaining([ | ||
| expect.objectContaining({ taskId: 'node1:111', nodeId: 'node1' }), | ||
| expect.objectContaining({ taskId: 'node2:222', nodeId: 'node2' }), | ||
| ]), | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it better to not await this request? since frontend can't do much if cancel failed, it's job is to notify backend to cancel query, and it can move on. no need to block here in case PPL cancel API is stuck
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call — updated to fire-and-forget. The cancel request now uses a .catch() chain instead of await, so the AbortError propagates immediately and the UI isn't blocked