Repository navigation
fix(clusters): Restart Cluster reports a failed cluster load instead of staying disabled #1867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dawsontoth
merged 3 commits into
claude/1339-replicated-set-configuration
from
claude/1853-restart-cluster-load-failure
Oct 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
69ca8f9
fix(clusters): Restart Cluster reports a failed cluster load instead …
dawsontoth b9dfc8b
fix(clusters): honor Cancel clicked while an instance is being checked
dawsontoth 3afc3a9
refactor(restart): end the restart and license loops when Cancel land…
dawsontoth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import type { Cluster, Instance } from '@/integrations/api/api.patch'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const { fabricConnect } = vi.hoisted(() => ({ fabricConnect: { current: false } })); | ||
| vi.mock('@/config/getInstanceClient', () => ({ | ||
| getInstanceClient: vi.fn((config: unknown) => ({ config })), | ||
| })); | ||
| vi.mock('@/features/auth/store/authStore', () => ({ | ||
| authStore: { checkForFabricConnect: vi.fn(() => fabricConnect.current) }, | ||
| })); | ||
| vi.mock('@/features/cluster/queries/getClusterInfoQuery', () => ({ getClusterInfo: vi.fn() })); | ||
|
|
||
| import { getInstanceClient } from '@/config/getInstanceClient'; | ||
| import { authStore } from '@/features/auth/store/authStore'; | ||
| import { getClusterInfo } from '@/features/cluster/queries/getClusterInfoQuery'; | ||
| import { getClusterRestartTargets } from './getClusterRestartTargets'; | ||
|
|
||
| function instance(id: string, status: Instance['status'] = 'RUNNING'): Instance { | ||
| return { | ||
| id, | ||
| status, | ||
| instanceFqdn: `${id}.example.com`, | ||
| operationsApiPort: 9925, | ||
| operationsApiSecure: true, | ||
| } as Instance; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| fabricConnect.current = false; | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getClusterRestartTargets', () => { | ||
| it('builds a client for each RUNNING instance, last instance first, and keeps every instance', async () => { | ||
| const instances = [instance('ins-a'), instance('ins-b', 'STOPPED'), instance('ins-c')]; | ||
| vi.mocked(getClusterInfo).mockResolvedValue({ id: 'clu-1', instances } as Cluster); | ||
|
|
||
| const { allInstances, targets } = await getClusterRestartTargets('clu-1'); | ||
|
|
||
| expect(getClusterInfo).toHaveBeenCalledWith('clu-1'); | ||
| expect(allInstances).toBe(instances); | ||
| expect(targets.map(({ instance }) => instance.id)).toEqual(['ins-c', 'ins-a']); | ||
| expect(targets[0].instanceClient).toEqual({ | ||
| config: { id: 'ins-c', forceFabricConnect: false, operationsUrl: 'https://ins-c.example.com:9925/' }, | ||
| }); | ||
| }); | ||
|
|
||
| it("forwards the cluster's Fabric Connect mode to every client", async () => { | ||
| fabricConnect.current = true; | ||
| vi.mocked(getClusterInfo).mockResolvedValue({ id: 'clu-1', instances: [instance('ins-a')] } as Cluster); | ||
|
|
||
| await getClusterRestartTargets('clu-1'); | ||
|
|
||
| expect(authStore.checkForFabricConnect).toHaveBeenCalledWith('clu-1'); | ||
| expect(getInstanceClient).toHaveBeenCalledWith(expect.objectContaining({ forceFabricConnect: true })); | ||
| }); | ||
|
|
||
| it('treats a cluster without an instance list as empty', async () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue({ id: 'clu-1' } as Cluster); | ||
|
|
||
| expect(await getClusterRestartTargets('clu-1')).toEqual({ allInstances: [], targets: [] }); | ||
| }); | ||
|
|
||
| it('rejects when the cluster cannot be fetched, so callers can report it', async () => { | ||
| vi.mocked(getClusterInfo).mockRejectedValue(new Error('503')); | ||
|
|
||
| await expect(getClusterRestartTargets('clu-1')).rejects.toThrow('503'); | ||
| expect(getInstanceClient).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { getInstanceClient } from '@/config/getInstanceClient'; | ||
| import { authStore } from '@/features/auth/store/authStore'; | ||
| import { getClusterInfo } from '@/features/cluster/queries/getClusterInfoQuery'; | ||
| import { Instance } from '@/integrations/api/api.patch'; | ||
| import { getOperationsUrlForInstance } from '@/lib/urls/getOperationsUrlForInstance'; | ||
| import type { AxiosInstance } from 'axios'; | ||
|
|
||
| export interface ClusterRestartTarget { | ||
| instance: Instance; | ||
| instanceClient: AxiosInstance; | ||
| } | ||
|
|
||
| export interface ClusterRestartTargets { | ||
| allInstances: Instance[]; | ||
| /** A client for each RUNNING instance, in the order a rolling restart visits them. */ | ||
| targets: ClusterRestartTarget[]; | ||
| } | ||
|
|
||
| export async function getClusterRestartTargets(clusterId: string): Promise<ClusterRestartTargets> { | ||
| const cluster = await getClusterInfo(clusterId); | ||
| const allInstances = cluster?.instances ?? []; | ||
| const clusterUsesFabricConnect = !!cluster && authStore.checkForFabricConnect(cluster.id); | ||
| const targets = allInstances | ||
| .filter(instance => instance.status === 'RUNNING') | ||
| .map(instance => ({ | ||
| instance, | ||
| instanceClient: getInstanceClient({ | ||
| id: instance.id, | ||
| forceFabricConnect: clusterUsesFabricConnect, | ||
| operationsUrl: getOperationsUrlForInstance(instance), | ||
| }), | ||
| })) | ||
| .reverse(); | ||
| return { allInstances, targets }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import type { SchemaLicense } from '@/integrations/api/api.gen'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import { PropsWithChildren } from 'react'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('@/config/useInstanceClient', () => { | ||
| const instanceClient = { target: 'ins-a' }; | ||
| const params = { entityType: 'instance', entityId: 'ins-a', instanceClient }; | ||
| return { | ||
| useInstanceClient: () => instanceClient, | ||
| useInstanceClientIdParams: () => params, | ||
| }; | ||
| }); | ||
| vi.mock('@/integrations/api/instance/status/getInstanceUserInfo', () => ({ getInstanceUserInfo: vi.fn() })); | ||
| vi.mock('@/integrations/api/instance/auth/installUsageLicense', () => ({ installUsageLicense: vi.fn() })); | ||
| vi.mock('@/lib/sleep', () => ({ sleep: vi.fn(() => Promise.resolve()) })); | ||
| vi.mock('sonner', () => ({ | ||
| toast: { loading: vi.fn(() => 'toast-1'), error: vi.fn(), success: vi.fn(), dismiss: vi.fn() }, | ||
| })); | ||
|
|
||
| import { installUsageLicense } from '@/integrations/api/instance/auth/installUsageLicense'; | ||
| import { getInstanceUserInfo } from '@/integrations/api/instance/status/getInstanceUserInfo'; | ||
| import { toast } from 'sonner'; | ||
| import { useApplyLicensesClick } from './useApplyLicensesClick'; | ||
|
|
||
| const LICENSES = [{ license: 'lic-1' }, { license: 'lic-2' }] as SchemaLicense[]; | ||
|
|
||
| function wrapper() { | ||
| const queryClient = new QueryClient(); | ||
| return ({ children }: PropsWithChildren) => <QueryClientProvider client={queryClient}>{children} | ||
| </QueryClientProvider>; | ||
| } | ||
|
|
||
| async function applyLicenses() { | ||
| const { result } = renderHook(() => useApplyLicensesClick({ licenses: LICENSES }), { wrapper: wrapper() }); | ||
| await act(async () => { | ||
| await (result.current.onApplyLicensesClick() as unknown as Promise<void>); | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.mocked(getInstanceUserInfo).mockResolvedValue({} as never); | ||
| vi.mocked(installUsageLicense).mockResolvedValue({ message: 'ok' } as never); | ||
| }); | ||
|
|
||
| afterEach(() => vi.clearAllMocks()); | ||
|
|
||
| describe('useApplyLicensesClick', () => { | ||
| it('installs every license and reports success', async () => { | ||
| const result = await applyLicenses(); | ||
|
|
||
| expect(vi.mocked(installUsageLicense).mock.calls.map(([body]) => body.license)).toEqual(['lic-1', 'lic-2']); | ||
| expect(result.current.isApplyLicensesPending).toBe(false); | ||
| expect(toast.success).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not install a license when Cancel is clicked while the instance is being checked', async () => { | ||
| vi.mocked(getInstanceUserInfo).mockImplementation(async () => { | ||
| cancel(); | ||
| return {} as never; | ||
| }); | ||
|
|
||
| await applyLicenses(); | ||
|
|
||
| expect(installUsageLicense).not.toHaveBeenCalled(); | ||
| expect(toast.error).toHaveBeenCalledWith('Cancelled', expect.anything()); | ||
| }); | ||
| }); | ||
|
|
||
| function cancel() { | ||
| const [, options] = vi.mocked(toast.loading).mock.calls.at(-1)!; | ||
| (options as unknown as { action: { onClick: () => void } }).action.onClick(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import type { Cluster, Instance } from '@/integrations/api/api.patch'; | ||
| import { getRestartState } from '@/lib/restart/restartTracker'; | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import type { AxiosInstance } from 'axios'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('@tanstack/react-router', () => ({ | ||
| useParams: () => ({ clusterId: 'clu-1' }), | ||
| })); | ||
| vi.mock('@/config/getInstanceClient', () => ({ | ||
| getInstanceClient: vi.fn(({ id }: { id: string }) => ({ target: id })), | ||
| })); | ||
| vi.mock('@/features/auth/store/authStore', () => ({ | ||
| authStore: { checkForFabricConnect: () => false }, | ||
| })); | ||
| vi.mock('@/features/cluster/queries/getClusterInfoQuery', () => ({ getClusterInfo: vi.fn() })); | ||
| vi.mock('@/integrations/api/instance/status/getInstanceUserInfo', () => ({ getInstanceUserInfo: vi.fn() })); | ||
| vi.mock('@/integrations/api/instance/status/restartInstance', () => ({ restartInstance: vi.fn() })); | ||
| vi.mock('@/lib/sleep', () => ({ sleep: vi.fn(() => Promise.resolve()) })); | ||
| vi.mock('sonner', () => ({ | ||
| toast: { loading: vi.fn(() => 'toast-1'), error: vi.fn(), success: vi.fn(), dismiss: vi.fn() }, | ||
| })); | ||
|
|
||
| import { getInstanceClient } from '@/config/getInstanceClient'; | ||
| import { getClusterInfo } from '@/features/cluster/queries/getClusterInfoQuery'; | ||
| import { getInstanceUserInfo } from '@/integrations/api/instance/status/getInstanceUserInfo'; | ||
| import { restartInstance } from '@/integrations/api/instance/status/restartInstance'; | ||
| import { toast } from 'sonner'; | ||
| import { useRestartClusterClick } from './useRestartClusterClick'; | ||
|
|
||
| function instance(id: string, overrides: Partial<Instance> = {}): Instance { | ||
| return { | ||
| id, | ||
| status: 'RUNNING', | ||
| instanceFqdn: `${id}.example.com`, | ||
| operationsApiPort: 9925, | ||
| operationsApiSecure: true, | ||
| ...overrides, | ||
| } as Instance; | ||
| } | ||
|
|
||
| function targetOf(client: AxiosInstance): string { | ||
| return (client as unknown as { target: string }).target; | ||
| } | ||
|
|
||
| let restarted: string[] = []; | ||
|
|
||
| async function clickRestart() { | ||
| const { result } = renderHook(() => useRestartClusterClick()); | ||
| await act(async () => { | ||
| await (result.current.onRestartClick() as unknown as Promise<void>); | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| restarted = []; | ||
| vi.mocked(getInstanceUserInfo).mockResolvedValue({} as never); | ||
| vi.mocked(restartInstance).mockImplementation(async ({ instanceClient }) => { | ||
| restarted.push(targetOf(instanceClient)); | ||
| return { message: 'ok' }; | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => vi.clearAllMocks()); | ||
|
|
||
| describe('useRestartClusterClick', () => { | ||
| it('restarts only the RUNNING instances, last first, and reports the one it could not restart', async () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue( | ||
| { | ||
| id: 'clu-1', | ||
| instances: [instance('ins-a'), instance('ins-b', { status: 'STOPPED' }), instance('ins-c')], | ||
| } as Cluster, | ||
| ); | ||
|
|
||
| const result = await clickRestart(); | ||
|
|
||
| expect(restarted).toEqual(['ins-c', 'ins-a']); | ||
| expect(result.current.isRestartPending).toBe(false); | ||
| expect(toast.error).toHaveBeenCalledWith( | ||
| 'Error', | ||
| expect.objectContaining({ description: expect.stringContaining('Only 2 "RUNNING" instances were restarted') }), | ||
| ); | ||
| }); | ||
|
|
||
| it('reports success once every instance restarted', async () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue({ id: 'clu-1', instances: [instance('ins-a')] } as Cluster); | ||
|
|
||
| await clickRestart(); | ||
|
|
||
| expect(restarted).toEqual(['ins-a']); | ||
| expect(toast.success).toHaveBeenCalledWith('Success', expect.objectContaining({ id: 'toast-1' })); | ||
| expect(getRestartState('clu-1')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does not restart an instance when Cancel is clicked while it is being checked', async () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue( | ||
| { id: 'clu-1', instances: [instance('ins-a'), instance('ins-b')] } as Cluster, | ||
| ); | ||
| vi.mocked(getInstanceUserInfo).mockImplementation(async () => { | ||
| cancel(); | ||
| return {} as never; | ||
| }); | ||
|
|
||
| await clickRestart(); | ||
|
|
||
| expect(restartInstance).not.toHaveBeenCalled(); | ||
| expect(toast.error).toHaveBeenCalledWith('Cancelled', expect.anything()); | ||
| }); | ||
|
|
||
| it('says the RUNNING instances failed to restart, not that none were RUNNING', async () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue( | ||
| { id: 'clu-1', instances: [instance('ins-a'), instance('ins-b')] } as Cluster, | ||
| ); | ||
| vi.mocked(restartInstance).mockRejectedValue(new Error('did not come back')); | ||
|
|
||
| await clickRestart(); | ||
|
|
||
| const [, options] = vi.mocked(toast.error).mock.calls.at(-1)!; | ||
| expect(options?.description).toContain('Only 0 "RUNNING" instances were restarted of 2 instances.'); | ||
| expect(options?.description).not.toContain('No instances were in a "RUNNING" state'); | ||
| }); | ||
|
|
||
| it('reports a cluster with no instances as a failure, not a success', async () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue({ id: 'clu-1', instances: [] } as unknown as Cluster); | ||
|
|
||
| await clickRestart(); | ||
|
|
||
| expect(toast.success).not.toHaveBeenCalled(); | ||
| expect(toast.error).toHaveBeenCalledWith( | ||
| 'Error', | ||
| expect.objectContaining({ description: expect.stringContaining('No instances were found within the cluster') }), | ||
| ); | ||
| }); | ||
|
|
||
| describe('when the cluster cannot be loaded', () => { | ||
| it.each([ | ||
| ['the cluster fetch rejects', () => vi.mocked(getClusterInfo).mockRejectedValue(new Error('503'))], | ||
| ['an instance client cannot be built', () => { | ||
| vi.mocked(getClusterInfo).mockResolvedValue({ id: 'clu-1', instances: [instance('ins-a')] } as Cluster); | ||
| vi.mocked(getInstanceClient).mockImplementationOnce(() => { | ||
| throw new TypeError('Invalid URL'); | ||
| }); | ||
| }], | ||
| ])('when %s, it reports it on the restart toast, restarts nothing, and stops pending', async (_label, arrange) => { | ||
| arrange(); | ||
|
|
||
| const result = await clickRestart(); | ||
|
|
||
| expect(restartInstance).not.toHaveBeenCalled(); | ||
| expect(result.current.isRestartPending).toBe(false); | ||
| expect(getRestartState('clu-1')).toBeUndefined(); | ||
| expect(toast.success).not.toHaveBeenCalled(); | ||
| expect(toast.error).toHaveBeenCalledWith( | ||
| 'Error', | ||
| expect.objectContaining({ | ||
| id: 'toast-1', | ||
| description: "Could not load the cluster's instances, so nothing was restarted.", | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function cancel() { | ||
| const [, options] = vi.mocked(toast.loading).mock.calls.at(-1)!; | ||
| (options as unknown as { action: { onClick: () => void } }).action.onClick(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.