Skip to content
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

fix(compass-global-writes): try/catch and show error when global-writes fetch fails COMPASS-8333 #6408

Merged
merged 9 commits into from
Nov 7, 2024
25 changes: 24 additions & 1 deletion packages/compass-global-writes/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,28 @@ function createStore({
hasShardingError = () => false,
hasShardKey = () => false,
failsOnShardingRequest = () => false,
failsOnShardZoneRequest = () => false,
authenticatedFetchStub,
}:
| {
isNamespaceManaged?: () => boolean;
hasShardingError?: () => boolean;
hasShardKey?: () => boolean | AtlasShardKey;
failsOnShardingRequest?: () => boolean;
failsOnShardZoneRequest?: () => boolean;
authenticatedFetchStub?: never;
}
| {
isNamespaceManaged?: never;
hasShardingError?: never;
hasShardKey?: () => boolean | ShardKey;
failsOnShardingRequest?: never;
failsOnShardZoneRequest?: () => never;
authenticatedFetchStub?: () => void;
} = {}): GlobalWritesStore {
const atlasService = {
authenticatedFetch: (uri: string) => {
if (uri.endsWith(`/geoSharding`) && failsOnShardingRequest()) {
if (uri.endsWith('/geoSharding') && failsOnShardingRequest()) {
return Promise.reject(new Error('Failed to shard'));
}

Expand All @@ -112,6 +115,13 @@ function createStore({
});
}

if (
/geoSharding.*newFormLocationMapping/.test(uri) &&
failsOnShardZoneRequest()
) {
return Promise.reject(new Error('Failed to fetch shard zones'));
}

return createAuthFetchResponse({});
},
automationAgentRequest: (_meta: unknown, type: string) => ({
Expand Down Expand Up @@ -266,6 +276,7 @@ describe('GlobalWritesStore Store', function () {
const store = createStore({
isNamespaceManaged: () => true,
hasShardKey: Sinon.fake(() => mockShardKey),
failsOnShardZoneRequest: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING');
Expand Down Expand Up @@ -312,6 +323,18 @@ describe('GlobalWritesStore Store', function () {
expect(store.getState().managedNamespace).to.equal(managedNamespace);
});
});

it('valid shard key -> failsOnShardZoneRequest', async function () {
const store = createStore({
isNamespaceManaged: () => true,
hasShardKey: () => true,
failsOnShardZoneRequest: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
expect(store.getState().managedNamespace).to.equal(managedNamespace);
});
});

it('incomplete setup -> sharding -> shard key correct', async function () {
// initial state -> incomplete shardingSetup
Expand Down
50 changes: 43 additions & 7 deletions packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum GlobalWritesActionTypes {
NamespaceShardKeyFetched = 'global-writes/NamespaceShardKeyFetched',

ShardZonesFetched = 'global-writes/ShardZonesFetched',
ShardZonesFetchedError = 'global-writes/ShardZonesFetchedError',

SubmittingForShardingStarted = 'global-writes/SubmittingForShardingStarted',
SubmittingForShardingFinished = 'global-writes/SubmittingForShardingFinished',
Expand Down Expand Up @@ -67,6 +68,10 @@ type ShardZonesFetchedAction = {
shardZones: ShardZoneData[];
};

type ShardZonesFetchedErrorAction = {
type: GlobalWritesActionTypes.ShardZonesFetchedError;
};

type SubmittingForShardingStartedAction = {
type: GlobalWritesActionTypes.SubmittingForShardingStarted;
};
Expand Down Expand Up @@ -350,6 +355,18 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
};
}

if (
isAction<ShardZonesFetchedErrorAction>(
action,
GlobalWritesActionTypes.ShardZonesFetchedError
)
) {
return {
...state,
shardZones: [],
};
}

if (
isAction<SubmittingForShardingStartedAction>(
action,
Expand Down Expand Up @@ -880,18 +897,37 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<

export const fetchShardingZones = (): GlobalWritesThunkAction<
Promise<void>,
ShardZonesFetchedAction
ShardZonesFetchedAction | ShardZonesFetchedErrorAction
> => {
return async (dispatch, getState, { atlasGlobalWritesService }) => {
return async (
dispatch,
getState,
{ atlasGlobalWritesService, connectionInfoRef }
) => {
const { shardZones } = getState();
if (shardZones.length > 0) {
return;
}
const shardingZones = await atlasGlobalWritesService.getShardingZones();
dispatch({
type: GlobalWritesActionTypes.ShardZonesFetched,
shardZones: shardingZones,
});
try {
const shardingZones = await atlasGlobalWritesService.getShardingZones();
dispatch({
type: GlobalWritesActionTypes.ShardZonesFetched,
shardZones: shardingZones,
});
} catch (error) {
dispatch({
type: GlobalWritesActionTypes.ShardZonesFetchedError,
});
openToast(
`global-writes-fetch-sharding-zones-error-${connectionInfoRef.current.id}`,
{
title: `Failed to fetch sharding zones: ${(error as Error).message}`,
dismissible: true,
timeout: 5000,
variant: 'important',
}
);
}
};
};

Expand Down
Loading