Skip to content

Commit 90a3ecb

Browse files
committed
fix scenarios
1 parent 19402ff commit 90a3ecb

File tree

5 files changed

+75
-26
lines changed

5 files changed

+75
-26
lines changed

packages/compass-global-writes/src/components/states/incomplete-sharding-setup.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { renderWithStore } from '../../../tests/create-store';
1010
import { type ConnectionInfo } from '@mongodb-js/compass-connections/provider';
1111
import { type ShardZoneData } from '../../store/reducer';
1212

13-
describe.only('IncompleteShardingSetup', function () {
13+
describe('IncompleteShardingSetup', function () {
1414
const shardZones: ShardZoneData[] = [
1515
{
1616
zoneId: '45893084',

packages/compass-global-writes/src/components/states/incomplete-sharding-setup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const containerStyles = css({
2727
marginBottom: spacing[400],
2828
});
2929

30-
const unmanageBtnStyles = css({
30+
const manageBtnStyles = css({
3131
marginTop: spacing[100],
3232
});
3333

@@ -68,7 +68,7 @@ export function IncompleteShardingSetup({
6868
onClick={onResume}
6969
variant={ButtonVariant.Default}
7070
isLoading={isSubmittingForSharding}
71-
className={unmanageBtnStyles}
71+
className={manageBtnStyles}
7272
>
7373
Enable Global Writes
7474
</Button>

packages/compass-global-writes/src/components/states/shard-key-correct.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ import {
1818
} from '../../store/reducer';
1919
import { ShardZonesTable } from '../shard-zones-table';
2020
import ShardKeyMarkup from '../shard-key-markup';
21-
import {
22-
containerStyles,
23-
bannerStyles,
24-
} from '../common-styles';
25-
import ExampleCommandsMarkup from '../example-commands-markup'
21+
import { containerStyles, bannerStyles } from '../common-styles';
22+
import ExampleCommandsMarkup from '../example-commands-markup';
2623

2724
const nbsp = '\u00a0';
2825

packages/compass-global-writes/src/store/index.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type CreateShardKeyData,
77
unmanageNamespace,
88
cancelSharding,
9+
resumeManagedNamespace,
910
POLLING_INTERVAL,
1011
type ShardKey,
1112
} from './reducer';
@@ -179,9 +180,11 @@ describe('GlobalWritesStore Store', function () {
179180
context('scenarios', function () {
180181
it('not managed -> sharding -> valid shard key', async function () {
181182
let mockShardKey = false;
183+
let mockManagedNamespace = false;
182184
// initial state === unsharded
183185
const store = createStore({
184186
hasShardKey: Sinon.fake(() => mockShardKey),
187+
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
185188
});
186189
await waitFor(() => {
187190
expect(store.getState().status).to.equal('UNSHARDED');
@@ -194,6 +197,7 @@ describe('GlobalWritesStore Store', function () {
194197
});
195198
const promise = store.dispatch(createShardKey(shardKeyData));
196199
expect(store.getState().status).to.equal('SUBMITTING_FOR_SHARDING');
200+
mockManagedNamespace = true;
197201
await promise;
198202
expect(store.getState().status).to.equal('SHARDING');
199203

@@ -309,6 +313,34 @@ describe('GlobalWritesStore Store', function () {
309313
});
310314
});
311315

316+
it('incomplete setup -> shard key correct', async function () {
317+
// initial state -> incomplete shardingSetup
318+
clock = sinon.useFakeTimers({
319+
shouldAdvanceTime: true,
320+
});
321+
let mockManagedNamespace = false;
322+
const store = createStore({
323+
isNamespaceManaged: Sinon.fake(() => mockManagedNamespace),
324+
hasShardKey: () => true,
325+
});
326+
await waitFor(() => {
327+
expect(store.getState().status).to.equal('INCOMPLETE_SHARDING_SETUP');
328+
expect(store.getState().managedNamespace).to.be.undefined;
329+
});
330+
331+
// user asks to resume geosharding
332+
const promise = store.dispatch(resumeManagedNamespace());
333+
mockManagedNamespace = true;
334+
expect(store.getState().status).to.equal(
335+
'SUBMITTING_FOR_SHARDING_INCOMPLETE'
336+
);
337+
await promise;
338+
clock.tick(POLLING_INTERVAL);
339+
await waitFor(() => {
340+
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
341+
});
342+
});
343+
312344
it('valid shard key -> not managed', async function () {
313345
// initial state === shard key correct
314346
const store = createStore({

packages/compass-global-writes/src/store/reducer.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,25 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
321321
};
322322
}
323323

324+
if (
325+
isAction<NamespaceShardKeyFetchedAction>(
326+
action,
327+
GlobalWritesActionTypes.NamespaceShardKeyFetched
328+
) &&
329+
state.status === ShardingStatuses.NOT_READY &&
330+
!action.shardKey &&
331+
!state.managedNamespace
332+
) {
333+
if (state.pollingTimeout) {
334+
throw new Error('Polling was not stopped');
335+
}
336+
return {
337+
...state,
338+
status: ShardingStatuses.UNSHARDED,
339+
pollingTimeout: state.pollingTimeout,
340+
};
341+
}
342+
324343
if (
325344
isAction<ShardZonesFetchedAction>(
326345
action,
@@ -759,15 +778,23 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
759778
getState,
760779
{ atlasGlobalWritesService, logger, connectionInfoRef }
761780
) => {
762-
const { namespace, status } = getState();
781+
const { namespace, status, managedNamespace } = getState();
763782

764783
try {
765784
const [shardingError, shardKey] = await Promise.all([
766785
atlasGlobalWritesService.getShardingError(namespace),
767786
atlasGlobalWritesService.getShardingKeys(namespace),
768787
]);
769788

770-
if (shardingError && !shardKey) {
789+
if (status === ShardingStatuses.SHARDING && (shardKey || shardingError)) {
790+
dispatch(stopPollingForShardKey());
791+
}
792+
793+
if (managedNamespace && !shardKey) {
794+
if (!shardingError) {
795+
dispatch(setNamespaceBeingSharded());
796+
return;
797+
}
771798
// if there is an existing shard key and an error both,
772799
// means we have a key mismatch
773800
// this will be handled in NamespaceShardKeyFetched
@@ -781,14 +808,10 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
781808
return;
782809
}
783810

784-
if (status === ShardingStatuses.SHARDING) {
785-
dispatch(stopPollingForShardKey());
786-
}
787811
dispatch({
788812
type: GlobalWritesActionTypes.NamespaceShardKeyFetched,
789813
shardKey,
790814
});
791-
792815
// if there is a key, we fetch sharding zones
793816
if (!shardKey) return;
794817
void dispatch(fetchShardingZones());
@@ -874,36 +897,33 @@ export function getStatusFromShardKeyAndManaged(
874897
) {
875898
const [firstShardKey, secondShardKey] = shardKey.fields;
876899

877-
if (!shardKey && !managedNamespace) {
878-
return ShardingStatuses.UNSHARDED;
879-
}
880-
881900
// For a shard key to be valid:
882901
// 1. The first key must be location and of type RANGE.
883902
// 2. The second key name must match managedNamespace.customShardKey and
884903
// the type must match the managedNamespace.isCustomShardKeyHashed.
885904

886905
const isLocatonKeyValid =
887906
firstShardKey.name === 'location' && firstShardKey.type === 'RANGE';
907+
908+
if (!isLocatonKeyValid || !secondShardKey) {
909+
return ShardingStatuses.SHARD_KEY_INVALID;
910+
}
911+
912+
if (!managedNamespace) {
913+
return ShardingStatuses.INCOMPLETE_SHARDING_SETUP;
914+
}
915+
888916
const isCustomKeyValid =
889917
managedNamespace &&
890918
managedNamespace.isShardKeyUnique === shardKey.isUnique &&
891919
secondShardKey.name === managedNamespace.customShardKey &&
892920
secondShardKey.type ===
893921
(managedNamespace.isCustomShardKeyHashed ? 'HASHED' : 'RANGE');
894922

895-
if (!isLocatonKeyValid || !secondShardKey) {
896-
return ShardingStatuses.SHARD_KEY_INVALID;
897-
}
898-
899923
if (!isCustomKeyValid) {
900924
return ShardingStatuses.SHARD_KEY_MISMATCH;
901925
}
902926

903-
if (!managedNamespace) {
904-
return ShardingStatuses.INCOMPLETE_SHARDING_SETUP;
905-
}
906-
907927
return ShardingStatuses.SHARD_KEY_CORRECT;
908928
}
909929

0 commit comments

Comments
 (0)