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

fix(shardQuerySplitting): do not emit empty data #793

Merged
merged 1 commit into from
Sep 20, 2024
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
17 changes: 15 additions & 2 deletions src/services/shardQuerySplitting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { runShardSplitQuery } from './shardQuerySplitting';
import { DataSourceWithBackend } from '@grafana/runtime';

import { LokiQuery } from './query';
import { getMockFrames } from './combineResponses.test';

jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('uuid'),
Expand Down Expand Up @@ -49,9 +50,10 @@ describe('runShardSplitQuery()', () => {
request = createRequest([{ expr: 'count_over_time($SELECTOR[1m])', refId: 'A' }]);
datasource = createLokiDatasource();
datasource.languageProvider.fetchLabelValues.mockResolvedValue(['1', '10', '2', '20', '3']);
const { metricFrameA } = getMockFrames();
// @ts-expect-error
jest.spyOn(datasource, 'runQuery').mockReturnValue(of({ data: [] }));
jest.spyOn(datasource, 'query').mockReturnValue(of({ data: [] }));
jest.spyOn(datasource, 'runQuery').mockReturnValue(of({ data: [metricFrameA] }));
jest.spyOn(datasource, 'query').mockReturnValue(of({ data: [metricFrameA] }));
});

test('Splits datasource queries', async () => {
Expand All @@ -62,6 +64,17 @@ describe('runShardSplitQuery()', () => {
});
});

test('Does not report missing data while streaming', async () => {
// @ts-expect-error
jest.spyOn(datasource, 'runQuery').mockReturnValue(of({ status: 200 }));
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith((response: DataQueryResponse[]) => {
// 4 shard requests
// @ts-expect-error
expect(datasource.runQuery).toHaveBeenCalledTimes(4);
expect(response).toHaveLength(1);
});
});

test('Interpolates queries before running', async () => {
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
expect(datasource.interpolateVariablesInQueries).toHaveBeenCalledTimes(1);
Expand Down
8 changes: 4 additions & 4 deletions src/services/shardQuerySplitting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@
// @ts-expect-error
subquerySubscription = datasource.runQuery(subRequest).subscribe({
next: (partialResponse: DataQueryResponse) => {
if ((partialResponse.errors ?? []).length > 0 || partialResponse.error != null) {

Check warning on line 127 in src/services/shardQuerySplitting.ts

View workflow job for this annotation

GitHub Actions / build

'error' is deprecated. use errors instead -- will be removed in Grafana 10+
if (retry(partialResponse)) {
return;
}
}
if (mergedResponse.data.length) {
mergedResponse.state = LoadingState.Streaming;
}
mergedResponse = combineResponses(mergedResponse, partialResponse);
},
complete: () => {
subscriber.next(mergedResponse);
// Prevent flashing "no data"
if (mergedResponse.data.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if we only ever get empty frames. I think we need to at least call subscriber.next(mergedResponse); when the state is LoadingState.Done or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

subscriber.next(mergedResponse);
}
nextRequest();
if (retryTimer) {
clearTimeout(retryTimer);
Expand Down
Loading