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
8 changes: 4 additions & 4 deletions apps/frontend/src/state/epics/valkeyEpics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, timeRange, clusterId, requestId },
})
} catch (error) {
console.error("[getMemoryUsageEpic] Error sending action:", error)
Expand Down
81 changes: 81 additions & 0 deletions apps/frontend/src/state/valkey-features/cpu/cpuSlice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, it, expect } from "vitest"
import cpuReducer, {
cpuUsageRequested,
cpuUsageFulfilled,
cpuUsageError
} from "./cpuSlice"

describe("cpuSlice", () => {
const connectionId = "conn-1"

it("adds a generated requestId via prepare callback when caller does not provide one", () => {
const action = cpuUsageRequested({ connectionId })
expect(action.payload.requestId).toBeDefined()
expect(typeof action.payload.requestId).toBe("string")
expect(action.payload.requestId.length).toBeGreaterThan(0)
})

it("preserves an explicitly provided requestId via prepare callback", () => {
const explicitId = "custom-req-id-123"
const action = cpuUsageRequested({ connectionId, requestId: explicitId })
expect(action.payload.requestId).toBe(explicitId)
})

it("updates state with latest requestId when fulfilled", () => {
const reqAction = cpuUsageRequested({ connectionId, requestId: "req-latest" })
let state = cpuReducer(undefined, reqAction)
expect(state[connectionId].loading).toBe(true)
expect(state[connectionId].lastRequestId).toBe("req-latest")

const mockData = [{ timestamp: 1000, value: 42.5 }]
const fulfilledAction = cpuUsageFulfilled({
connectionId,
parsedResponse: mockData,
requestId: "req-latest",
})
state = cpuReducer(state, fulfilledAction)
expect(state[connectionId].data).toEqual(mockData)
expect(state[connectionId].loading).toBe(false)
})

it("ignores fulfilled response with an old requestId, leaving data unchanged and loading true", () => {
// Initial request 1
let state = cpuReducer(undefined, cpuUsageRequested({ connectionId, requestId: "req-1" }))
// Initial fulfillment for request 1
const initialData = [{ timestamp: 1000, value: 20 }]
state = cpuReducer(state, cpuUsageFulfilled({ connectionId, parsedResponse: initialData, requestId: "req-1" }))
expect(state[connectionId].data).toEqual(initialData)

// Second request 2 sets loading to true
state = cpuReducer(state, cpuUsageRequested({ connectionId, requestId: "req-2" }))
expect(state[connectionId].loading).toBe(true)
expect(state[connectionId].lastRequestId).toBe("req-2")

// Late arriving fulfillment for request 1 should be ignored
const staleData = [{ timestamp: 2000, value: 99 }]
state = cpuReducer(state, cpuUsageFulfilled({ connectionId, parsedResponse: staleData, requestId: "req-1" }))
expect(state[connectionId].data).toEqual(initialData)
expect(state[connectionId].loading).toBe(true)
})

it("ignores error response with an old requestId, leaving error unset and loading true", () => {
// Request 1 then Request 2
let state = cpuReducer(undefined, cpuUsageRequested({ connectionId, requestId: "req-1" }))
state = cpuReducer(state, cpuUsageRequested({ connectionId, requestId: "req-2" }))
expect(state[connectionId].loading).toBe(true)

// Stale error for request 1
const staleError = { message: "stale network error" }
state = cpuReducer(state, cpuUsageError({ connectionId, error: staleError, requestId: "req-1" }))
expect(state[connectionId].error).toBeUndefined()
expect(state[connectionId].loading).toBe(true)
})

it("applies error response when requestId matches latest", () => {
let state = cpuReducer(undefined, cpuUsageRequested({ connectionId, requestId: "req-latest" }))
const validError = { message: "valid connection error" }
state = cpuReducer(state, cpuUsageError({ connectionId, error: validError, requestId: "req-latest" }))
expect(state[connectionId].error).toEqual(validError)
expect(state[connectionId].loading).toBe(false)
})
})
62 changes: 49 additions & 13 deletions apps/frontend/src/state/valkey-features/cpu/cpuSlice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSlice } from "@reduxjs/toolkit"
import { createSlice, type PayloadAction, nanoid } from "@reduxjs/toolkit"
import { type JSONObject } from "@common/src/json-utils"
import { VALKEY } from "@common/src/constants.ts"
import * as R from "ramda"
Expand All @@ -24,33 +24,66 @@ interface CpuState {
data: CpuDataPoint[]
error?: JSONObject | null
loading?: boolean
lastRequestId?: string
}
}

const initialCpuState: CpuState = {}

interface CpuUsageRequestedPayload {
connectionId: string
clusterId?: string
timeRange?: number
requestId: string
}

interface CpuUsageFulfilledPayload {
connectionId: string
parsedResponse: unknown
requestId?: string
}

interface CpuUsageErrorPayload {
connectionId: string
error: JSONObject
requestId?: string
}

const cpuSlice = createSlice({
name: "cpu",
initialState: initialCpuState,
reducers: {
cpuUsageRequested: (state, action) => {
const { connectionId } = action.payload
if (!state[connectionId]) {
state[connectionId] = {
data: [],
loading: false,
cpuUsageRequested: {
reducer: (state, action: PayloadAction<CpuUsageRequestedPayload>) => {
const { connectionId, requestId } = action.payload
if (!state[connectionId]) {
state[connectionId] = {
data: [],
loading: false,
}
}
}
state[connectionId].loading = true
state[connectionId].loading = true
state[connectionId].lastRequestId = requestId
},
prepare: (payload: { connectionId: string; clusterId?: string; timeRange?: number; requestId?: string }) => ({
payload: {
...payload,
requestId: payload.requestId ?? nanoid(),
},
}),
},
cpuUsageFulfilled: (state, action) => {
const { connectionId, parsedResponse } = action.payload
cpuUsageFulfilled: (state, action: PayloadAction<CpuUsageFulfilledPayload>) => {
const { connectionId, parsedResponse, requestId } = action.payload
if (!state[connectionId]) {
state[connectionId] = {
data: [],
loading: false,
}
}
// If a requestId is provided and does not match the latest request, ignore it to prevent race conditions (#590)
if (requestId && state[connectionId].lastRequestId && requestId !== state[connectionId].lastRequestId) {
return
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Validate that parsedResponse is an array
if (Array.isArray(parsedResponse)) {
state[connectionId].data = parsedResponse
Expand All @@ -61,9 +94,12 @@ const cpuSlice = createSlice({
}
state[connectionId].loading = false
},
cpuUsageError: (state, action) => {
const { connectionId, error } = action.payload
cpuUsageError: (state, action: PayloadAction<CpuUsageErrorPayload>) => {
const { connectionId, error, requestId } = action.payload
if (state[connectionId]) {
if (requestId && state[connectionId].lastRequestId && requestId !== state[connectionId].lastRequestId) {
return
}
state[connectionId].error = error
state[connectionId].loading = false
}
Expand Down
96 changes: 96 additions & 0 deletions apps/frontend/src/state/valkey-features/memory/memorySlice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { describe, it, expect } from "vitest"
import memoryReducer, {
memoryUsageRequested,
memoryUsageFulfilled,
memoryUsageError
} from "./memorySlice"

describe("memorySlice", () => {
const connectionId = "conn-1"

it("adds a generated requestId via prepare callback when caller does not provide one", () => {
const action = memoryUsageRequested({ connectionId })
expect(action.payload.requestId).toBeDefined()
expect(typeof action.payload.requestId).toBe("string")
expect(action.payload.requestId.length).toBeGreaterThan(0)
})

it("preserves an explicitly provided requestId via prepare callback", () => {
const explicitId = "custom-mem-id-456"
const action = memoryUsageRequested({ connectionId, requestId: explicitId })
expect(action.payload.requestId).toBe(explicitId)
})

it("updates state with latest requestId when fulfilled", () => {
const reqAction = memoryUsageRequested({ connectionId, requestId: "req-latest" })
let state = memoryReducer(undefined, reqAction)
expect(state[connectionId].loading).toBe(true)
expect(state[connectionId].lastRequestId).toBe("req-latest")

const mockData = {
used_memory: {
description: "Used memory in bytes",
series: [{ timestamp: 1000, value: 1024000 }],
},
}
const fulfilledAction = memoryUsageFulfilled({
connectionId,
parsedResponse: mockData,
requestId: "req-latest",
})
state = memoryReducer(state, fulfilledAction)
expect(state[connectionId].data).toEqual(mockData)
expect(state[connectionId].loading).toBe(false)
})

it("ignores fulfilled response with an old requestId, leaving data unchanged and loading true", () => {
// Initial request 1
let state = memoryReducer(undefined, memoryUsageRequested({ connectionId, requestId: "req-1" }))
// Initial fulfillment for request 1
const initialData = {
used_memory: {
description: "Initial memory",
series: [{ timestamp: 1000, value: 500000 }],
},
}
state = memoryReducer(state, memoryUsageFulfilled({ connectionId, parsedResponse: initialData, requestId: "req-1" }))
expect(state[connectionId].data).toEqual(initialData)

// Second request 2 sets loading to true
state = memoryReducer(state, memoryUsageRequested({ connectionId, requestId: "req-2" }))
expect(state[connectionId].loading).toBe(true)
expect(state[connectionId].lastRequestId).toBe("req-2")

// Late arriving fulfillment for request 1 should be ignored
const staleData = {
used_memory: {
description: "Stale memory",
series: [{ timestamp: 2000, value: 9999999 }],
},
}
state = memoryReducer(state, memoryUsageFulfilled({ connectionId, parsedResponse: staleData, requestId: "req-1" }))
expect(state[connectionId].data).toEqual(initialData)
expect(state[connectionId].loading).toBe(true)
})

it("ignores error response with an old requestId, leaving error unset and loading true", () => {
// Request 1 then Request 2
let state = memoryReducer(undefined, memoryUsageRequested({ connectionId, requestId: "req-1" }))
state = memoryReducer(state, memoryUsageRequested({ connectionId, requestId: "req-2" }))
expect(state[connectionId].loading).toBe(true)

// Stale error for request 1
const staleError = { message: "stale memory network error" }
state = memoryReducer(state, memoryUsageError({ connectionId, error: staleError, requestId: "req-1" }))
expect(state[connectionId].error).toBeUndefined()
expect(state[connectionId].loading).toBe(true)
})

it("applies error response when requestId matches latest", () => {
let state = memoryReducer(undefined, memoryUsageRequested({ connectionId, requestId: "req-latest" }))
const validError = { message: "valid memory fetch error" }
state = memoryReducer(state, memoryUsageError({ connectionId, error: validError, requestId: "req-latest" }))
expect(state[connectionId].error).toEqual(validError)
expect(state[connectionId].loading).toBe(false)
})
})
Loading
Loading