shardQuerySplitting: group has pending requests or no shards to run (#108500)

* shardQuerySplitting: group has pending requests or no shards to run

* Comment

* Regression test
This commit is contained in:
Matias Chomicki 2025-07-23 18:12:12 +02:00 committed by GitHub
parent e3c1e75da5
commit d3a445409b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -6,7 +6,7 @@ import { LokiDatasource } from './datasource';
import { createLokiDatasource } from './mocks/datasource';
import { getMockFrames } from './mocks/frames';
import { runShardSplitQuery } from './shardQuerySplitting';
import { LokiQuery, LokiQueryDirection } from './types';
import { LokiQuery, LokiQueryDirection, LokiQueryType } from './types';
jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('uuid'),
@ -100,6 +100,26 @@ describe('runShardSplitQuery()', () => {
});
});
test('Runs multiple non-sharded queries', async () => {
const request = createRequest([
{ expr: 'count_over_time({a="b"}[$__auto])', refId: 'A' },
{ expr: 'count_over_time({a="b"}[$__auto])', refId: 'B', queryType: LokiQueryType.Instant },
]);
datasource = createLokiDatasource({
replace: (input = '') => {
return input.replace('$__auto', '5m').replace('$step', '5m');
},
getVariables: () => [],
});
jest.spyOn(datasource, 'runQuery').mockReturnValue(of({ data: [] }));
datasource.languageProvider.fetchLabelValues = jest.fn();
jest.mocked(datasource.languageProvider.fetchLabelValues).mockResolvedValue([]);
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
// 5 shards, 3 groups + empty shard group, 4 requests
expect(datasource.runQuery).toHaveBeenCalledTimes(2);
});
});
test('Users query splitting for querying over a day', async () => {
await expect(runShardSplitQuery(datasource, request)).toEmitValuesWith(() => {
// 5 shards, 3 groups + empty shard group, 4 requests

View File

@ -87,8 +87,9 @@ function splitQueriesByStreamShard(
}
const nextRequest = () => {
// Find the next group to execute, which can be queries with pending shards to execute, or the next query with no shards.
const nextGroup =
groups[group + 1] && groupHasPendingRequests(groups[group + 1])
groups[group + 1] && (groups[group + 1].shards === undefined || groupHasPendingRequests(groups[group + 1]))
? groups[group + 1]
: groups.find((shardGroup) => groupHasPendingRequests(shardGroup));