This is an automated email from the ASF dual-hosted git repository.
adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new 0fb9901ae74 HDDS-16877. Avoid duplicate initial requests on Recon
Containers page (#11484)
0fb9901ae74 is described below
commit 0fb9901ae74e727e19b81e39078ec6b1bfb6459a
Author: Andy Huang <[email protected]>
AuthorDate: Thu Nov 12 19:39:51 2026 +0800
HDDS-16877. Avoid duplicate initial requests on Recon Containers page
(#11484)
---
.../src/__tests__/containers/Containers.test.tsx | 44 +++++++++-----
.../src/__tests__/datanodes/Datanodes.test.tsx | 25 ++++++++
.../src/__tests__/hooks/useAutoReload.test.tsx | 70 ++++++++++++++++++++++
.../src/__tests__/pipelines/Pipelines.test.tsx | 11 ++++
.../src/v2/hooks/useAutoReload.hook.tsx | 4 +-
.../src/v2/pages/containers/containers.tsx | 7 +--
.../src/v2/pages/datanodes/datanodes.tsx | 8 ---
7 files changed, 139 insertions(+), 30 deletions(-)
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
index 9b6538341f1..722591f0862 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/containers/Containers.test.tsx
@@ -50,21 +50,6 @@ vi.mock('@/v2/hooks/useAPIData.hook', async (importOriginal)
=> {
return {
...actual,
useApiData: <T,>(url: string, defaultValue: T, options = {}) => {
- // Containers fetches clusterState with initialFetch: true and again via
useAutoReload on mount;
- // the second call aborts the first, which surfaces as unhandled
CanceledError in tests.
- if (url === '/api/v1/clusterState') {
- return {
- data: ClusterState as T,
- loading: false,
- error: null,
- lastUpdated: Date.now(),
- success: true,
- execute: vi.fn(),
- refetch: vi.fn(() => Promise.resolve({ data: ClusterState })),
- clearError: vi.fn(),
- reset: vi.fn(),
- };
- }
return actual.useApiData(url, defaultValue, {
...options,
retryAttempts: 0,
@@ -92,6 +77,7 @@ async function waitForLoadingToFinish(container: HTMLElement)
{
describe('Containers Component', () => {
beforeEach(() => {
+ sessionStorage.removeItem('autoReloadEnabled');
vi.mocked(commonUtils.showDataFetchError).mockClear();
});
beforeAll(() => containersServer.listen());
@@ -111,6 +97,34 @@ describe('Containers Component', () => {
expect(getEnabledSearchInput()).toBeInTheDocument();
});
+ test.each([null, 'true', 'false'])('loads each initial endpoint once with
auto reload %s', async (enabled) => {
+ if (enabled !== null) {
+ sessionStorage.setItem('autoReloadEnabled', enabled);
+ }
+ const requests: string[] = [];
+ const countRequest = ({ request }: { request: Request }) => {
+ requests.push(new URL(request.url).pathname);
+ };
+ containersServer.events.on('request:start', countRequest);
+ try {
+ render(<Containers />);
+ await waitForContainerRows();
+ await waitFor(() => expect(highlightValue('Total
Containers')).toHaveTextContent(
+ new RegExp(`^${ClusterState.containers}$`)));
+ await waitFor(() => expect(highlightValue('Quasi
Closed')).toHaveTextContent(/^2$/));
+ for (const endpoint of [
+ '/api/v1/clusterState',
+ '/api/v1/containers/unhealthy/MISSING',
+ '/api/v1/containers/quasiClosed',
+ ]) {
+ expect(requests.filter(url => url === endpoint)).toHaveLength(1);
+ }
+ } finally {
+ containersServer.events.removeListener('request:start', countRequest);
+ sessionStorage.removeItem('autoReloadEnabled');
+ }
+ });
+
test('Loads data on mount', async () => {
render(<Containers />);
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/datanodes/Datanodes.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/datanodes/Datanodes.test.tsx
index 79fe8042287..333ce62e1fd 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/datanodes/Datanodes.test.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/datanodes/Datanodes.test.tsx
@@ -19,6 +19,8 @@ import React from 'react';
import {fireEvent, render, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {vi} from 'vitest';
+import axios from 'axios';
+import { AUTO_RELOAD_INTERVAL_DEFAULT } from
'@/constants/autoReload.constants';
import Datanodes from '@/v2/pages/datanodes/datanodes';
import * as commonUtils from '@/utils/common';
@@ -78,6 +80,29 @@ describe('Datanodes Component', () => {
expect(rows).toHaveLength(5); // Based on the mocked DatanodeResponse
});
+ test('loads each endpoint once and does not poll when auto refresh is
disabled', async () => {
+ sessionStorage.setItem('autoReloadEnabled', 'false');
+ const requests: string[] = [];
+ const interceptor = axios.interceptors.request.use(config => {
+ requests.push(config.url || '');
+ return config;
+ });
+ const setTimeoutSpy = vi.spyOn(window, 'setTimeout');
+ const { unmount } = render(<Datanodes />);
+ try {
+ await waitForDNTable();
+ for (const endpoint of ['/api/v1/datanodes',
'/api/v1/datanodes/decommission/info']) {
+ expect(requests.filter(url => url === endpoint)).toHaveLength(1);
+ }
+ expect(setTimeoutSpy).not.toHaveBeenCalledWith(expect.any(Function),
AUTO_RELOAD_INTERVAL_DEFAULT);
+ } finally {
+ unmount();
+ setTimeoutSpy.mockRestore();
+ axios.interceptors.request.eject(interceptor);
+ sessionStorage.removeItem('autoReloadEnabled');
+ }
+ });
+
test('Loads data on mount', async () => {
render(<Datanodes />);
// Wait for the data to be loaded into the table
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/hooks/useAutoReload.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/hooks/useAutoReload.test.tsx
new file mode 100644
index 00000000000..9009bbdfeac
--- /dev/null
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/hooks/useAutoReload.test.tsx
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import React from 'react';
+import { act, fireEvent, render, screen } from '@testing-library/react';
+import { vi } from 'vitest';
+import { useAutoReload } from '@/v2/hooks/useAutoReload.hook';
+
+function TestAutoReload({ refresh }: { refresh: () => void }) {
+ const autoReload = useAutoReload(refresh, 1000);
+ return <button onClick={() =>
autoReload.handleAutoReloadToggle(!autoReload.isPolling)}>
+ {autoReload.isPolling ? 'Stop' : 'Start'}
+ </button>;
+}
+
+describe('useAutoReload', () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ sessionStorage.removeItem('autoReloadEnabled');
+ });
+ afterEach(() => {
+ vi.useRealTimers();
+ sessionStorage.removeItem('autoReloadEnabled');
+ });
+
+ test.each([null, 'true', 'false'])('loads once on mount with auto reload
%s', (enabled) => {
+ if (enabled !== null) {
+ sessionStorage.setItem('autoReloadEnabled', enabled);
+ }
+ const refresh = vi.fn();
+ const { unmount } = render(<TestAutoReload refresh={refresh} />);
+ expect(refresh).toHaveBeenCalledTimes(1);
+ act(() => { vi.advanceTimersByTime(1000); });
+ expect(refresh).toHaveBeenCalledTimes(enabled === 'false' ? 1 : 2);
+ unmount();
+ act(() => { vi.advanceTimersByTime(2000); });
+ expect(refresh).toHaveBeenCalledTimes(enabled === 'false' ? 1 : 2);
+ });
+
+ test('stops and resumes polling with the latest refresh callback', () => {
+ const refresh = vi.fn();
+ const latestRefresh = vi.fn();
+ const { rerender, unmount } = render(<TestAutoReload refresh={refresh} />);
+ fireEvent.click(screen.getByText('Stop'));
+ act(() => { vi.advanceTimersByTime(1000); });
+ expect(refresh).toHaveBeenCalledTimes(1);
+ rerender(<TestAutoReload refresh={latestRefresh} />);
+ fireEvent.click(screen.getByText('Start'));
+ expect(latestRefresh).toHaveBeenCalledTimes(1);
+ act(() => { vi.advanceTimersByTime(1000); });
+ expect(latestRefresh).toHaveBeenCalledTimes(2);
+ unmount();
+ expect(vi.getTimerCount()).toBe(0);
+ });
+});
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/pipelines/Pipelines.test.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/pipelines/Pipelines.test.tsx
index 076f09d25a7..6e36fd2a405 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/pipelines/Pipelines.test.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/__tests__/pipelines/Pipelines.test.tsx
@@ -87,6 +87,17 @@ describe('Pipelines Component', () => {
expect(table).toHaveTextContent('CLOSED');
});
+ test('loads data when auto refresh is disabled', async () => {
+ sessionStorage.setItem('autoReloadEnabled', 'false');
+ try {
+ render(<Pipelines />);
+ const rows = await waitFor(() =>
screen.getAllByTestId(pipelineLocators.pipelineRowRegex));
+ expect(rows).toHaveLength(3);
+ } finally {
+ sessionStorage.removeItem('autoReloadEnabled');
+ }
+ });
+
test('Renders table with correct number of rows', async () => {
render(<Pipelines />);
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAutoReload.hook.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAutoReload.hook.tsx
index c18b478798c..0b60fc6a14c 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAutoReload.hook.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/hooks/useAutoReload.hook.tsx
@@ -71,11 +71,13 @@ export function useAutoReload(
}
};
- // Initialize polling on mount if auto-reload is enabled
+ // Load on mount and initialize polling if auto-reload is enabled
useEffect(() => {
const autoReloadEnabled = sessionStorage.getItem('autoReloadEnabled') !==
'false';
if (autoReloadEnabled) {
startPolling();
+ } else {
+ refreshFunctionRef.current();
}
return () => {
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/containers/containers.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/containers/containers.tsx
index 75496c1a916..9b00f0e1366 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/containers/containers.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/containers/containers.tsx
@@ -164,7 +164,7 @@ const Containers: React.FC<{}> = () => {
CONSTANTS.DEFAULT_CLUSTER_STATE,
{
retryAttempts: 2,
- initialFetch: true,
+ initialFetch: false,
onError: (error) => showDataFetchError(error)
}
);
@@ -425,11 +425,6 @@ const Containers: React.FC<{}> = () => {
}
};
- useEffect(() => {
- fetchTabData('1', 0, DEFAULT_PAGE_SIZE);
- fetchQuasiClosedCount();
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
-
function handleColumnChange(selected: ValueType<Option, true>) {
setSelectedColumns(selected as Option[]);
}
diff --git
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/datanodes/datanodes.tsx
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/datanodes/datanodes.tsx
index 8920988fc4f..4aeed168cb6 100644
---
a/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/datanodes/datanodes.tsx
+++
b/hadoop-ozone/recon/src/main/resources/webapps/recon/ozone-recon-web/src/v2/pages/datanodes/datanodes.tsx
@@ -193,14 +193,6 @@ const Datanodes: React.FC<{}> = () => {
const autoReload = useAutoReload(loadData);
- useEffect(() => {
- autoReload.startPolling();
-
- return (() => {
- autoReload.stopPolling();
- });
- }, []);
-
function handleSelectionChange(rows: React.Key[]) {
setSelectedRows(rows);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]