Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

UXW-3177 batch dashcard queries by 5 (#70542)

Denis Berezin0b65ccce3a70
diff --git a/frontend/src/metabase/dashboard/actions/data-fetching.ts b/frontend/src/metabase/dashboard/actions/data-fetching.ts
index 3e995d2f749..6afe1e058f0 100644
--- a/frontend/src/metabase/dashboard/actions/data-fetching.ts
+++ b/frontend/src/metabase/dashboard/actions/data-fetching.ts
@@ -447,6 +447,44 @@ export const fetchCardData =
);
};
+// Leave 1 connection free for interactive requests (browser HTTP/1.1 limit is 6)
+const HTTP1_CONCURRENT_CARD_FETCH_LIMIT = 5;
+
+function getCardsFetchingConcurrencyLimit(): number {
+ try {
+ const [navigationEntry] = performance.getEntriesByType(
+ "navigation",
+ ) as PerformanceNavigationTiming[];
+ const protocol = navigationEntry?.nextHopProtocol ?? "";
+ // HTTP/2 and HTTP/3 multiplex requests over a single connection,
+ // so the per-host connection limit does not apply
+ if (protocol === "h2" || protocol === "h2c" || protocol.startsWith("h3")) {
+ return Infinity;
+ }
+ } catch {
+ // Performance API unavailable; fall through to the conservative limit
+ }
+ return HTTP1_CONCURRENT_CARD_FETCH_LIMIT;
+}
+
+const CONCURRENT_CARD_FETCH_LIMIT = getCardsFetchingConcurrencyLimit();
+
+async function runWithConcurrencyLimit(
+ tasks: (() => Promise<void>)[],
+ limit: number,
+): Promise<void> {
+ let index = 0;
+ async function worker() {
+ while (index < tasks.length) {
+ const taskIndex = index++;
+ await tasks[taskIndex]();
+ }
+ }
+ await Promise.all(
+ Array.from({ length: Math.min(limit, tasks.length) }, worker),
+ );
+}
+
export const fetchDashboardCardData =
({ isRefreshing = false, reload = false, clearCache = false } = {}) =>
(dispatch: Dispatch, getState: GetState) => {
@@ -499,20 +537,21 @@ export const fetchDashboardCardData =
);
}
- const promises = nonVirtualDashcardsToFetch.map(
- async ({ card, dashcard }) => {
- await dispatch(
- // TODO: fix the return type of getAllDashboardCards to make sure
- // that the relationship between a dashcard and its card
- // is actually reflected in the type system
- fetchCardData(card as Card, dashcard, {
- reload,
- clearCache,
- dashboardLoadId,
- }),
- );
- await dispatch(updateLoadingTitle(nonVirtualDashcardsToFetch.length));
- },
+ const tasks = nonVirtualDashcardsToFetch.map(
+ ({ card, dashcard }) =>
+ async () => {
+ await dispatch(
+ // TODO: fix the return type of getAllDashboardCards to make sure
+ // that the relationship between a dashcard and its card
+ // is actually reflected in the type system
+ fetchCardData(card as Card, dashcard, {
+ reload,
+ clearCache,
+ dashboardLoadId,
+ }),
+ );
+ await dispatch(updateLoadingTitle(nonVirtualDashcardsToFetch.length));
+ },
);
if (nonVirtualDashcardsToFetch.length > 0) {
@@ -522,9 +561,11 @@ export const fetchDashboardCardData =
// TODO: There is a race condition here, when refreshing a dashboard before
// the previous API calls finished.
- return Promise.all(promises).then(() => {
- dispatch(loadingComplete());
- });
+ return runWithConcurrencyLimit(tasks, CONCURRENT_CARD_FETCH_LIMIT).then(
+ () => {
+ dispatch(loadingComplete());
+ },
+ );
}
};
@@ -536,10 +577,10 @@ export const reloadDashboardCards =
return;
}
- const reloads = getAllDashboardCards(dashboard)
+ const reloadTasks = getAllDashboardCards(dashboard)
.filter(({ dashcard }) => !isVirtualDashCard(dashcard))
- .map(({ card, dashcard }) =>
- dispatch(
+ .map(({ card, dashcard }) => async () => {
+ await dispatch(
// TODO: fix the return type of getAllDashboardCards to make sure
// that the relationship between a dashcard and its card
// is actually reflected in the type system
@@ -547,10 +588,10 @@ export const reloadDashboardCards =
reload: true,
ignoreCache: true,
}),
- ),
- );
+ );
+ });
- await Promise.all(reloads);
+ await runWithConcurrencyLimit(reloadTasks, CONCURRENT_CARD_FETCH_LIMIT);
};
export const cancelFetchDashboardCardData = createThunkAction(
diff --git a/frontend/src/metabase/dashboard/actions/data-fetching.unit.spec.ts b/frontend/src/metabase/dashboard/actions/data-fetching.unit.spec.ts
index a3978c2d51f..85edcf68dca 100644
--- a/frontend/src/metabase/dashboard/actions/data-fetching.unit.spec.ts
+++ b/frontend/src/metabase/dashboard/actions/data-fetching.unit.spec.ts
@@ -10,22 +10,28 @@ import { createMockEntitiesState } from "__support__/store";
import { Api } from "metabase/api";
import type { Dashboard } from "metabase-types/api";
import {
+ createMockCard,
createMockDashboard,
createMockDashboardCard,
createMockDashboardQueryMetadata,
- createMockSettings,
} from "metabase-types/api/mocks";
import { createSampleDatabase } from "metabase-types/api/mocks/presets";
-import type { DashboardState } from "metabase-types/store";
+import type { DashboardState, State } from "metabase-types/store";
import {
createMockDashboardState,
+ createMockSettingsState,
createMockStoreDashboard,
} from "metabase-types/store/mocks";
import { dashboardReducers } from "../reducers";
import { isQuestionDashCard } from "../utils";
-import { fetchCardDataAction, fetchDashboard } from "./data-fetching";
+import {
+ fetchCardDataAction,
+ fetchDashboard,
+ fetchDashboardCardData,
+ reloadDashboardCards,
+} from "./data-fetching";
type SetupOpts = {
dashboards?: Dashboard[];
@@ -42,7 +48,7 @@ function setup({
entities: createMockEntitiesState({
databases: [database],
}),
- settings: createMockSettings(),
+ settings: createMockSettingsState(),
};
const store = getStore(
@@ -179,3 +185,95 @@ describe("fetchDashboard", () => {
expect(thirdResult.payload).toMatchObject({ result: { foo: true } });
});
});
+
+/**
+ * Creates a mock dispatch/getState pair that executes thunks but skips the
+ * Redux store, avoiding the Immer/icepick incompatibility in dashcardData
+ * reducer when multiple fulfilled actions arrive.
+ */
+function createMockDispatch(getState: () => Partial<State>) {
+ const dispatch = (action: unknown): unknown => {
+ if (typeof action === "function") {
+ return action(dispatch, getState);
+ }
+ return Promise.resolve(action);
+ };
+ return dispatch;
+}
+
+function setupConcurrencyTest(dashboardId: number, cardCount: number) {
+ let currentConcurrent = 0;
+ let maxConcurrent = 0;
+
+ const database = createSampleDatabase();
+ const dashcards = Array.from({ length: cardCount }, (_, i) =>
+ createMockDashboardCard({
+ id: i + 1,
+ card_id: i + 1,
+ dashboard_id: dashboardId,
+ card: createMockCard({ id: i + 1 }),
+ }),
+ );
+
+ dashcards.forEach((dashcard) => {
+ fetchMock.post(
+ `/api/dashboard/${dashboardId}/dashcard/${dashcard.id}/card/${dashcard.card_id}/query`,
+ () =>
+ new Promise((resolve) => {
+ currentConcurrent++;
+ maxConcurrent = Math.max(maxConcurrent, currentConcurrent);
+ setTimeout(() => {
+ currentConcurrent--;
+ resolve({ data: [] });
+ }, 50);
+ }),
+ );
+ });
+
+ const DASHBOARD = createMockDashboard({ id: dashboardId, dashcards });
+ const state: Partial<State> = {
+ dashboard: createMockDashboardState({
+ dashboardId: DASHBOARD.id,
+ dashboards: {
+ [DASHBOARD.id]: createMockStoreDashboard({
+ ...DASHBOARD,
+ dashcards: dashcards.map((dc) => dc.id),
+ }),
+ },
+ dashcards: Object.fromEntries(dashcards.map((dc) => [dc.id, dc])),
+ }),
+ entities: createMockEntitiesState({ databases: [database] }),
+ settings: createMockSettingsState(),
+ };
+
+ const getState = () => state;
+ const dispatch = createMockDispatch(getState);
+
+ return { dispatch, getState, getMaxConcurrent: () => maxConcurrent };
+}
+
+describe("fetchDashboardCardData", () => {
+ it("should make at most 5 api calls simultaneously", async () => {
+ const { dispatch, getState, getMaxConcurrent } = setupConcurrencyTest(
+ 100,
+ 8,
+ );
+
+ await fetchDashboardCardData()(dispatch as never, getState as never);
+
+ expect(getMaxConcurrent()).toBe(5);
+ });
+});
+
+describe("reloadDashboardCards", () => {
+ it("should make at most 5 api calls simultaneously", async () => {
+ const { dispatch, getState, getMaxConcurrent } = setupConcurrencyTest(
+ 200,
+ 8,
+ );
+
+ await reloadDashboardCards()(dispatch as never, getState as never);
+
+ expect(getMaxConcurrent()).toBe(5);
+ });
+});