From 2a10c2fab092622f16533ed10a065dc6be5282ac Mon Sep 17 00:00:00 2001 From: Raj Gahoi Date: Wed, 25 Nov 2026 04:07:02 +0530 Subject: [PATCH 1/2] fix(dashboard): eliminate time range race condition with requestId tagging (#590) Signed-off-by: Raj Gahoi --- .../components/dashboard/cpu-memory-usage.tsx | 4 ++-- apps/frontend/src/state/epics/valkeyEpics.ts | 8 ++++---- .../src/state/valkey-features/cpu/cpuSlice.ts | 15 ++++++++++++--- .../state/valkey-features/memory/memorySlice.ts | 15 ++++++++++++--- apps/server/src/actions/cpuUsage.ts | 17 +++++++++++------ apps/server/src/actions/memoryUsage.ts | 17 +++++++++++------ 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/apps/frontend/src/components/dashboard/cpu-memory-usage.tsx b/apps/frontend/src/components/dashboard/cpu-memory-usage.tsx index f55499b4..dde510fa 100644 --- a/apps/frontend/src/components/dashboard/cpu-memory-usage.tsx +++ b/apps/frontend/src/components/dashboard/cpu-memory-usage.tsx @@ -31,14 +31,14 @@ export default function CpuMemoryUsage() { // for cpu useEffect(() => { if (id) { - dispatch(cpuUsageRequested({ connectionId: id, clusterId, timeRange: timeRangeToMs(cpuTimeRange) })) + dispatch(cpuUsageRequested({ connectionId: id, clusterId, timeRange: timeRangeToMs(cpuTimeRange), requestId: `${id}-cpu-${Date.now()}` })) } }, [id, clusterId, dispatch, cpuTimeRange]) // for memory useEffect(() => { if (id) { - dispatch(memoryUsageRequested({ connectionId: id, clusterId, timeRange: timeRangeToMs(memoryTimeRange) })) + dispatch(memoryUsageRequested({ connectionId: id, clusterId, timeRange: timeRangeToMs(memoryTimeRange), requestId: `${id}-mem-${Date.now()}` })) } }, [id, clusterId, dispatch, memoryTimeRange]) diff --git a/apps/frontend/src/state/epics/valkeyEpics.ts b/apps/frontend/src/state/epics/valkeyEpics.ts index eaccd167..a2da314e 100644 --- a/apps/frontend/src/state/epics/valkeyEpics.ts +++ b/apps/frontend/src/state/epics/valkeyEpics.ts @@ -609,12 +609,12 @@ export const getCpuUsageEpic = () => select(cpuUsageRequested), tap((action) => { try { - const { timeRange, connectionId, clusterId } = action.payload + const { timeRange, connectionId, clusterId, requestId } = action.payload const socket = getSocket() socket.next({ type: action.type, - payload: { connectionId, clusterId, timeRange }, + payload: { connectionId, clusterId, timeRange, requestId }, }) } catch (error) { console.error("[getCpuUsageEpic] Error sending action:", error) @@ -628,12 +628,12 @@ export const getMemoryUsageEpic = () => select(memoryUsageRequested), tap((action) => { try { - const { timeRange, connectionId, clusterId } = action.payload + const { timeRange, connectionId, clusterId, requestId } = action.payload const socket = getSocket() socket.next({ type: action.type, - payload: { connectionId, timeRange, clusterId }, + payload: { connectionId, clusterId, timeRange, requestId }, }) } catch (error) { console.error("[getMemoryUsageEpic] Error sending action:", error) diff --git a/apps/frontend/src/state/valkey-features/cpu/cpuSlice.ts b/apps/frontend/src/state/valkey-features/cpu/cpuSlice.ts index f1ca345f..1046610e 100644 --- a/apps/frontend/src/state/valkey-features/cpu/cpuSlice.ts +++ b/apps/frontend/src/state/valkey-features/cpu/cpuSlice.ts @@ -24,6 +24,7 @@ interface CpuState { data: CpuDataPoint[] error?: JSONObject | null loading?: boolean + requestId?: string } } @@ -34,7 +35,7 @@ const cpuSlice = createSlice({ initialState: initialCpuState, reducers: { cpuUsageRequested: (state, action) => { - const { connectionId } = action.payload + const { connectionId, requestId } = action.payload if (!state[connectionId]) { state[connectionId] = { data: [], @@ -42,15 +43,20 @@ const cpuSlice = createSlice({ } } state[connectionId].loading = true + state[connectionId].requestId = requestId }, cpuUsageFulfilled: (state, action) => { - const { connectionId, parsedResponse } = action.payload + const { connectionId, parsedResponse, requestId } = action.payload if (!state[connectionId]) { state[connectionId] = { data: [], loading: false, } } + // Drop out-of-order response if a newer request was dispatched + if (requestId && state[connectionId].requestId && requestId !== state[connectionId].requestId) { + return + } // Validate that parsedResponse is an array if (Array.isArray(parsedResponse)) { state[connectionId].data = parsedResponse @@ -62,8 +68,11 @@ const cpuSlice = createSlice({ state[connectionId].loading = false }, cpuUsageError: (state, action) => { - const { connectionId, error } = action.payload + const { connectionId, error, requestId } = action.payload if (state[connectionId]) { + if (requestId && state[connectionId].requestId && requestId !== state[connectionId].requestId) { + return + } state[connectionId].error = error state[connectionId].loading = false } diff --git a/apps/frontend/src/state/valkey-features/memory/memorySlice.ts b/apps/frontend/src/state/valkey-features/memory/memorySlice.ts index 46b3dbea..df9203c2 100644 --- a/apps/frontend/src/state/valkey-features/memory/memorySlice.ts +++ b/apps/frontend/src/state/valkey-features/memory/memorySlice.ts @@ -31,6 +31,7 @@ interface MemoryState { data: MemoryData | null error?: JSONObject | null loading?: boolean + requestId?: string } } @@ -41,7 +42,7 @@ const memorySlice = createSlice({ initialState: initialMemoryState, reducers: { memoryUsageRequested: (state, action) => { - const { connectionId } = action.payload + const { connectionId, requestId } = action.payload if (!state[connectionId]) { state[connectionId] = { data: null, @@ -49,15 +50,20 @@ const memorySlice = createSlice({ } } state[connectionId].loading = true + state[connectionId].requestId = requestId }, memoryUsageFulfilled: (state, action) => { - const { connectionId, parsedResponse } = action.payload + const { connectionId, parsedResponse, requestId } = action.payload if (!state[connectionId]) { state[connectionId] = { data: null, loading: false, } } + // Drop out-of-order response if a newer request was dispatched + if (requestId && state[connectionId].requestId && requestId !== state[connectionId].requestId) { + return + } // validating that parsedResponse is an object (not an array, not null) if (parsedResponse && typeof parsedResponse === "object" && !Array.isArray(parsedResponse)) { state[connectionId].data = parsedResponse @@ -69,8 +75,11 @@ const memorySlice = createSlice({ state[connectionId].loading = false }, memoryUsageError: (state, action) => { - const { connectionId, error } = action.payload + const { connectionId, error, requestId } = action.payload if (state[connectionId]) { + if (requestId && state[connectionId].requestId && requestId !== state[connectionId].requestId) { + return + } state[connectionId].error = error state[connectionId].loading = false } diff --git a/apps/server/src/actions/cpuUsage.ts b/apps/server/src/actions/cpuUsage.ts index 931dbea7..3442cad8 100644 --- a/apps/server/src/actions/cpuUsage.ts +++ b/apps/server/src/actions/cpuUsage.ts @@ -11,6 +11,7 @@ const sendCpuUsageFulfilled = ( ws: WebSocket, connectionId: string, parsedResponse: CpuUsageResponse, + requestId?: string, ) => { ws.send( JSON.stringify({ @@ -18,6 +19,7 @@ const sendCpuUsageFulfilled = ( payload: { connectionId, parsedResponse, + ...(requestId ? { requestId } : {}), }, }), ) @@ -27,6 +29,7 @@ const sendCpuUsageError = ( ws: WebSocket, connectionId: string, error: unknown, + requestId?: string, ) => { console.error(error) ws.send( @@ -35,6 +38,7 @@ const sendCpuUsageError = ( payload: { connectionId, error: error instanceof Error ? error.message : String(error), + ...(requestId ? { requestId } : {}), }, }), ) @@ -43,14 +47,15 @@ const sendCpuUsageError = ( type RequestPayload = { connectionId: string, clusterId: string, - timeRange?: number + timeRange?: number, + requestId?: string } export const cpuUsageRequested = withDeps( async ({ ws, metricsServerMap, action, connectedNodesByCluster }) => { - const { connectionId, clusterId, timeRange = 12 * MILLISECONDS_IN_AN_HOUR } = action.payload as unknown as RequestPayload + const { connectionId, clusterId, timeRange = 12 * MILLISECONDS_IN_AN_HOUR, requestId } = action.payload as unknown as RequestPayload if (collectorFeaturesDisabled()) { - sendCpuUsageError(ws, connectionId, new Error(KUBERNETES_METRICS_UNAVAILABLE)) + sendCpuUsageError(ws, connectionId, new Error(KUBERNETES_METRICS_UNAVAILABLE), requestId) return } const connectionIds = clusterId ? connectedNodesByCluster.get(clusterId as string) ?? [] : [connectionId] @@ -61,7 +66,7 @@ export const cpuUsageRequested = withDeps( const metricsServerURI = metricsServerMap.get(toNodeId(connectionId))?.metricsURI if (!metricsServerURI) { - sendCpuUsageError(ws, connectionId, new Error("Metrics server URI not found")) + sendCpuUsageError(ws, connectionId, new Error("Metrics server URI not found"), requestId) return } @@ -73,9 +78,9 @@ export const cpuUsageRequested = withDeps( const response = await fetchWithTimeout(url) const parsedResponse: CpuUsageResponse = await response.json() as CpuUsageResponse - sendCpuUsageFulfilled(ws, connectionId, parsedResponse) + sendCpuUsageFulfilled(ws, connectionId, parsedResponse, requestId) } catch (error) { - sendCpuUsageError(ws, connectionId, error) + sendCpuUsageError(ws, connectionId, error, requestId) } }) await Promise.all(promises) diff --git a/apps/server/src/actions/memoryUsage.ts b/apps/server/src/actions/memoryUsage.ts index 7870248d..4edb1744 100644 --- a/apps/server/src/actions/memoryUsage.ts +++ b/apps/server/src/actions/memoryUsage.ts @@ -18,6 +18,7 @@ const sendMemoryUsageFulfilled = ( ws: WebSocket, connectionId: string, parsedResponse: MemoryUsageResponse, + requestId?: string, ) => { ws.send( JSON.stringify({ @@ -25,6 +26,7 @@ const sendMemoryUsageFulfilled = ( payload: { connectionId, parsedResponse, + ...(requestId ? { requestId } : {}), }, }), ) @@ -34,6 +36,7 @@ const sendMemoryUsageError = ( ws: WebSocket, connectionId: string, error: unknown, + requestId?: string, ) => { console.error(error) ws.send( @@ -42,6 +45,7 @@ const sendMemoryUsageError = ( payload: { connectionId, error: error instanceof Error ? error.message : String(error), + ...(requestId ? { requestId } : {}), }, }), ) @@ -50,14 +54,15 @@ const sendMemoryUsageError = ( type RequestPayload = { connectionId: string, clusterId: string, - timeRange?: number + timeRange?: number, + requestId?: string } export const memoryUsageRequested = withDeps( async ({ ws, metricsServerMap, action, connectedNodesByCluster }) => { - const { connectionId, clusterId, timeRange = 12 * MILLISECONDS_IN_AN_HOUR } = action.payload as unknown as RequestPayload + const { connectionId, clusterId, timeRange = 12 * MILLISECONDS_IN_AN_HOUR, requestId } = action.payload as unknown as RequestPayload if (collectorFeaturesDisabled()) { - sendMemoryUsageError(ws, connectionId, new Error(KUBERNETES_METRICS_UNAVAILABLE)) + sendMemoryUsageError(ws, connectionId, new Error(KUBERNETES_METRICS_UNAVAILABLE), requestId) return } const connectionIds = clusterId ? connectedNodesByCluster.get(clusterId as string) ?? [] : [connectionId] @@ -67,7 +72,7 @@ export const memoryUsageRequested = withDeps( const metricsServerURI = metricsServerMap.get(toNodeId(connectionId))?.metricsURI if (!metricsServerURI) { - sendMemoryUsageError(ws, connectionId, new Error("Metrics server URI not found")) + sendMemoryUsageError(ws, connectionId, new Error("Metrics server URI not found"), requestId) return } @@ -79,9 +84,9 @@ export const memoryUsageRequested = withDeps( const response = await fetchWithTimeout(url) const parsedResponse: MemoryUsageResponse = await response.json() as MemoryUsageResponse - sendMemoryUsageFulfilled(ws, connectionId, parsedResponse) + sendMemoryUsageFulfilled(ws, connectionId, parsedResponse, requestId) } catch (error) { - sendMemoryUsageError(ws, connectionId, error) + sendMemoryUsageError(ws, connectionId, error, requestId) } }) await Promise.all(promises) From 056da3a208d95f22b95d3d43f0812c6449f328ff Mon Sep 17 00:00:00 2001 From: Raj Gahoi Date: Wed, 25 Nov 2026 15:33:41 +0530 Subject: [PATCH 2/2] fix(dashboard): tag metricsReadinessRetryEpic dispatches with requestId --- apps/frontend/src/state/epics/valkeyEpics.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/frontend/src/state/epics/valkeyEpics.ts b/apps/frontend/src/state/epics/valkeyEpics.ts index a2da314e..abe11225 100644 --- a/apps/frontend/src/state/epics/valkeyEpics.ts +++ b/apps/frontend/src/state/epics/valkeyEpics.ts @@ -717,8 +717,8 @@ export const metricsReadinessRetryEpic = (store: Store) => filter((a) => a.payload.connectionId === connectionId), tap(() => { const clusterId = selectConnectionDetails(connectionId)(store.getState())?.clusterId - store.dispatch(cpuUsageRequested({ connectionId, clusterId, timeRange: MILLISECONDS_IN_AN_HOUR })) - store.dispatch(memoryUsageRequested({ connectionId, clusterId, timeRange: MILLISECONDS_IN_AN_HOUR })) + store.dispatch(cpuUsageRequested({ connectionId, clusterId, timeRange: MILLISECONDS_IN_AN_HOUR, requestId: `${connectionId}-cpu-${Date.now()}` })) + store.dispatch(memoryUsageRequested({ connectionId, clusterId, timeRange: MILLISECONDS_IN_AN_HOUR, requestId: `${connectionId}-mem-${Date.now()}` })) }), ), ),