From fb9c8de76f9def2bcd6c2d9d2ba8c8438f06d2e0 Mon Sep 17 00:00:00 2001 From: Matthias Wessendorf Date: Thu, 12 Nov 2026 10:34:20 +0100 Subject: [PATCH] OCPBUGS-114039: stabilize useRelatedHPA ref to stop topology re-render storm The Developer perspective Topology renders one PodSet (via PodRing) per workload, and each one watches the whole namespace HorizontalPodAutoscaler list through the shared, network-deduped watch. getReduxData rebuilds that list on every HPA watch event, so the matched HPA was handed back as a new object reference on every event even when the workload's own HPA had not changed. Each event therefore re-rendered all N memoized PodStatus SVGs, pinning the browser main thread and freezing the Topology view in namespaces with hundreds of autoscaled deployments. Keep the previous reference as long as the matched HPA is unchanged (same uid and resourceVersion), so an unrelated HPA event no longer re-renders the node. Add a unit test covering reference stability on unrelated changes and a fresh reference when the matching HPA itself changes. Co-Authored-By: Claude Opus 4.8 Signed-off-by: Matthias Wessendorf --- .../src/hooks/__tests__/useRelatedHPA.spec.ts | 94 +++++++++++++++++++ .../console-shared/src/hooks/useRelatedHPA.ts | 18 +++- 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 frontend/packages/console-shared/src/hooks/__tests__/useRelatedHPA.spec.ts diff --git a/frontend/packages/console-shared/src/hooks/__tests__/useRelatedHPA.spec.ts b/frontend/packages/console-shared/src/hooks/__tests__/useRelatedHPA.spec.ts new file mode 100644 index 00000000000..8af6edf2116 --- /dev/null +++ b/frontend/packages/console-shared/src/hooks/__tests__/useRelatedHPA.spec.ts @@ -0,0 +1,94 @@ +import { renderHook } from '@testing-library/react'; +import { useK8sWatchResource } from '@console/internal/components/utils/k8s-watch-hook'; +import { useRelatedHPA } from '../useRelatedHPA'; + +jest.mock('@console/internal/components/utils/k8s-watch-hook', () => ({ + useK8sWatchResource: jest.fn(), +})); + +jest.mock('@console/internal/models', () => ({ + HorizontalPodAutoscalerModel: { kind: 'HorizontalPodAutoscaler' }, +})); + +const useK8sWatchResourceMock = useK8sWatchResource as jest.Mock; + +// The workload the topology node represents. +const WORKLOAD = ['apps/v1', 'Deployment', 'my-app', 'default'] as const; + +// Build an HPA list entry. getReduxData rebuilds the list (and its entries) on every +// watch event, so each call intentionally returns fresh object references. +const makeHpa = (uid: string, resourceVersion: string, targetName: string) => ({ + apiVersion: 'autoscaling/v2', + kind: 'HorizontalPodAutoscaler', + metadata: { uid, resourceVersion, name: `hpa-${uid}`, namespace: 'default' }, + spec: { + scaleTargetRef: { apiVersion: 'apps/v1', kind: 'Deployment', name: targetName }, + }, +}); + +describe('useRelatedHPA', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('returns the HPA whose scaleTargetRef matches the workload', () => { + useK8sWatchResourceMock.mockReturnValue([ + [makeHpa('1', '100', 'my-app'), makeHpa('2', '200', 'other-app')], + true, + undefined, + ]); + + const { result } = renderHook(() => useRelatedHPA(...WORKLOAD)); + const [hpa, loaded, error] = result.current; + + expect(loaded).toBe(true); + expect(error).toBeUndefined(); + expect(hpa?.metadata?.uid).toBe('1'); + }); + + it('returns undefined when no HPA targets the workload', () => { + useK8sWatchResourceMock.mockReturnValue([[makeHpa('2', '200', 'other-app')], true, undefined]); + + const { result } = renderHook(() => useRelatedHPA(...WORKLOAD)); + + expect(result.current[0]).toBeUndefined(); + }); + + // OCPBUGS-114039: an unrelated HPA watch event must not hand back a new object + // reference for this node's HPA, otherwise the memoized PodStatus SVG re-renders + // for every node on every event and freezes the browser in large namespaces. + it('keeps a stable reference when only an unrelated HPA changes', () => { + useK8sWatchResourceMock.mockReturnValue([ + [makeHpa('1', '100', 'my-app'), makeHpa('2', '200', 'other-app')], + true, + undefined, + ]); + + const { result, rerender } = renderHook(() => useRelatedHPA(...WORKLOAD)); + const first = result.current[0]; + + // Watch event for the *other* HPA: the list and all its entries are rebuilt with + // fresh references, but our workload's HPA is unchanged (same uid + resourceVersion). + useK8sWatchResourceMock.mockReturnValue([ + [makeHpa('1', '100', 'my-app'), makeHpa('2', '201', 'other-app')], + true, + undefined, + ]); + rerender(); + + expect(result.current[0]).toBe(first); + }); + + it('returns a new reference when the matching HPA itself changes', () => { + useK8sWatchResourceMock.mockReturnValue([[makeHpa('1', '100', 'my-app')], true, undefined]); + + const { result, rerender } = renderHook(() => useRelatedHPA(...WORKLOAD)); + const first = result.current[0]; + + useK8sWatchResourceMock.mockReturnValue([[makeHpa('1', '101', 'my-app')], true, undefined]); + rerender(); + + expect(result.current[0]).not.toBe(first); + expect(result.current[0]?.metadata?.resourceVersion).toBe('101'); + }); +}); diff --git a/frontend/packages/console-shared/src/hooks/useRelatedHPA.ts b/frontend/packages/console-shared/src/hooks/useRelatedHPA.ts index 4a7c1638844..f11d22759bb 100644 --- a/frontend/packages/console-shared/src/hooks/useRelatedHPA.ts +++ b/frontend/packages/console-shared/src/hooks/useRelatedHPA.ts @@ -35,5 +35,21 @@ export const useRelatedHPA = ( return undefined; // similar to .find(() => false) }, [hpas, loaded, error, workloadAPI, workloadKind, workloadName]); - return [matchingHpa, loaded, error]; + // The topology renders one PodSet (via PodRing) per workload, and each one watches + // the whole namespace HPA list through the shared, network-deduped watch above. + // getReduxData rebuilds that list on every HPA watch event, so `matchingHpa` is a + // brand-new object reference on every event even when this workload's HPA did not + // change. That re-rendered all N memoized PodStatus SVGs on every event and froze + // the browser main thread in large namespaces (OCPBUGS-114039). Keep the previous + // reference as long as the matched HPA is unchanged (same uid + resourceVersion), + // so an unrelated HPA event no longer re-renders this node. + const uid = matchingHpa?.metadata?.uid; + const resourceVersion = matchingHpa?.metadata?.resourceVersion; + // Intentionally keyed on the HPA identity (uid + resourceVersion) rather than on + // matchingHpa, so the returned reference stays stable while the matched HPA's + // content is unchanged. + // eslint-disable-next-line react-hooks-js/exhaustive-deps + const stableHpa = useMemo(() => matchingHpa, [uid, resourceVersion]); + + return [stableHpa, loaded, error]; };