Skip to content
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
35 changes: 33 additions & 2 deletions public/app/core/services/FetchQueueWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FetchQueue, FetchQueueUpdate, FetchStatus } from './FetchQueue';
import { FetchQueueWorker } from './FetchQueueWorker';
import { ResponseQueue } from './ResponseQueue';

const getTestContext = (http2Enabled = false) => {
const getTestContext = (http2Enabled = false, isHostProxied = () => false) => {
const config: GrafanaBootConfig = { http2Enabled } as unknown as GrafanaBootConfig;
const dataUrl = 'http://localhost:3000/api/ds/query?=abc';
const apiUrl = 'http://localhost:3000/api/alerts?state=all';
Expand All @@ -25,12 +25,43 @@ const getTestContext = (http2Enabled = false) => {
getResponses: jest.fn(),
} as unknown as ResponseQueue;

new FetchQueueWorker(queueMock, responseQueueMock, config);
new FetchQueueWorker(queueMock, responseQueueMock, config, isHostProxied);

return { dataUrl, apiUrl, updates, queueMock, addMock };
};

describe('FetchQueueWorker', () => {
it('uses host-proxy concurrency even when enabled after singleton construction', () => {
let hostProxied = false;
const { updates, addMock, dataUrl } = getTestContext(false, () => hostProxied);
const state = Object.fromEntries(
Array.from({ length: 22 }, (_, index) => [
`panel-${index}`,
{ state: FetchStatus.Pending, options: { url: dataUrl } },
])
);
hostProxied = true;
updates.next({ noOfPending: 22, noOfInProgress: 5, state });
expect(addMock).toHaveBeenCalledTimes(22);
});

it('retains API precedence and the existing multiplexed ceiling for host-proxied requests', () => {
const { updates, addMock, dataUrl, apiUrl } = getTestContext(false, () => true);
updates.next({
noOfPending: 3,
noOfInProgress: 998,
state: {
first: { state: FetchStatus.Pending, options: { url: dataUrl } },
second: { state: FetchStatus.Pending, options: { url: dataUrl } },
api: { state: FetchStatus.Pending, options: { url: apiUrl } },
},
});
expect(addMock.mock.calls).toEqual([
['api', { url: apiUrl }],
['first', { url: dataUrl }],
]);
});

describe('when an update is pushed in the stream', () => {
describe('and queue has no pending entries', () => {
it('then nothing should be added to the responseQueue', () => {
Expand Down
14 changes: 11 additions & 3 deletions public/app/core/services/FetchQueueWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ interface WorkerEntry {
}

export class FetchQueueWorker {
constructor(fetchQueue: FetchQueue, responseQueue: ResponseQueue, config: GrafanaBootConfig) {
const maxParallelRequests = config?.http2Enabled ? 1000 : 5; // for tests that don't mock GrafanaBootConfig the config param will be undefined

constructor(
fetchQueue: FetchQueue,
responseQueue: ResponseQueue,
config: GrafanaBootConfig,
isHostProxied: () => boolean = () => false
) {
// This will create an implicit live subscription for as long as this class lives.
// But as FetchQueueWorker is used by the singleton backendSrv that also lives for as long as Grafana app lives
// I think this ok. We could add some disposable pattern later if the need arises.
Expand All @@ -26,6 +29,11 @@ export class FetchQueueWorker {
// Using concatMap instead of mergeMap so that the order with apiRequests first is preserved
// https://rxjs.dev/api/operators/concatMap
concatMap(({ state, noOfInProgress }) => {
// The embedded app sends queries through the host's authenticated proxy,
// not Grafana's server transport. Its minimal boot config has no HTTP/2
// flag. Do not serialize those queries behind the standalone HTTP/1 cap.
// Read the mode here: the singleton is constructed before MFE startup.
const maxParallelRequests = config?.http2Enabled || isHostProxied() ? 1000 : 5;
const apiRequests = Object.keys(state)
.filter((k) => state[k].state === FetchStatus.Pending && !isDataQuery(state[k].options.url))
.reduce<WorkerEntry[]>((all, key) => {
Expand Down
2 changes: 1 addition & 1 deletion public/app/core/services/backend_srv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class BackendSrv implements BackendService {

this.initGrafanaDeviceID();

new FetchQueueWorker(this.fetchQueue, this.responseQueue, getConfig());
new FetchQueueWorker(this.fetchQueue, this.responseQueue, getConfig(), () => this.grafanaPrefix);
}

private async initGrafanaDeviceID() {
Expand Down
20 changes: 19 additions & 1 deletion public/app/core/specs/backend_srv.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, of, lastValueFrom } from 'rxjs';
import { Observable, of, lastValueFrom, NEVER } from 'rxjs';
import { fromFetch } from 'rxjs/fetch';
import { delay } from 'rxjs/operators';

Expand Down Expand Up @@ -90,7 +90,25 @@ jest.mock('app/core/utils/auth', () => ({
hasSessionExpiry: () => true,
}));

// Backend transport tests do not exercise dashboard persistence. Loading that
// adapter also initializes the complete dashboard scene/store module graph.
jest.mock('app/features/dashboard/api/dashboard_api', () => ({ getDashboardAPI: jest.fn() }));

describe('backendSrv', () => {
it.each([false, true])('dispatches dashboard queries with host proxy mode %s', (hostProxied) => {
const { backendSrv, fromFetchMock } = getTestContext();
fromFetchMock.mockReturnValue(NEVER);
backendSrv.setGrafanaPrefix(hostProxied);
const subscriptions = Array.from({ length: 22 }, (_, panel) =>
backendSrv.fetch({ url: '/api/ds/query', method: 'POST', data: { panel } }).subscribe()
);
try {
expect(fromFetchMock).toHaveBeenCalledTimes(hostProxied ? 22 : 5);
} finally {
subscriptions.forEach((subscription) => subscription.unsubscribe());
}
});

describe('parseRequestOptions', () => {
it.each`
retry | url | headers | orgId | noBackendCache | expected
Expand Down
Loading