Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
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 @@ -71,25 +71,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.includes(`/geoSharding`) && failsOnShardingRequest()) {
if (uri.endsWith('/geoSharding') && failsOnShardingRequest()) {
return Promise.reject(new Error('Failed to shard'));
}

Expand All @@ -111,6 +114,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 @@ -262,6 +272,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 @@ -309,6 +320,18 @@ describe('GlobalWritesStore Store', function () {
});
});

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('valid shard key -> not managed', async function () {
// initial state === shard key correct
const store = createStore({
Expand Down
51 changes: 44 additions & 7 deletions packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
showConfirmation as showConfirmationModal,
} from '@mongodb-js/compass-components';
import type { ManagedNamespace } from '../services/atlas-global-writes-service';
import { GlobalWrites } from '../components';

export const POLLING_INTERVAL = 5000;

Expand All @@ -30,6 +31,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 +69,10 @@ type ShardZonesFetchedAction = {
shardZones: ShardZoneData[];
};

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

type SubmittingForShardingStartedAction = {
type: GlobalWritesActionTypes.SubmittingForShardingStarted;
};
Expand Down Expand Up @@ -322,6 +328,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 @@ -781,18 +799,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