Skip to content

Commit f6397e0

Browse files
committed
polling
1 parent 960abbf commit f6397e0

File tree

1 file changed

+121
-6
lines changed
  • packages/compass-global-writes/src/store

1 file changed

+121
-6
lines changed

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

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { GlobalWritesThunkAction } from '.';
33
import { openToast, showConfirmation } from '@mongodb-js/compass-components';
44
import type { ManagedNamespace } from '../services/atlas-global-writes-service';
55

6+
export const POLLING_INTERVAL = 5000;
7+
68
export function isAction<A extends Action>(
79
action: Action,
810
type: A['type']
@@ -34,6 +36,8 @@ enum GlobalWritesActionTypes {
3436
CancellingShardingFinished = 'global-writes/CancellingShardingFinished',
3537
CancellingShardingErrored = 'global-writes/CancellingShardingErrored',
3638

39+
NextPollingTimeoutSet = 'global-writes/NextPollingTimeoutSet',
40+
3741
UnmanagingNamespaceStarted = 'global-writes/UnmanagingNamespaceStarted',
3842
UnmanagingNamespaceFinished = 'global-writes/UnmanagingNamespaceFinished',
3943
UnmanagingNamespaceErrored = 'global-writes/UnmanagingNamespaceErrored',
@@ -85,6 +89,11 @@ type CancellingShardingErroredAction = {
8589
type: GlobalWritesActionTypes.CancellingShardingErrored;
8690
};
8791

92+
type NextPollingTimeoutSetAction = {
93+
type: GlobalWritesActionTypes.NextPollingTimeoutSet;
94+
timeout: NodeJS.Timeout;
95+
};
96+
8897
type UnmanagingNamespaceStartedAction = {
8998
type: GlobalWritesActionTypes.UnmanagingNamespaceStarted;
9099
};
@@ -123,7 +132,7 @@ export enum ShardingStatuses {
123132
* State when user cancels the sharding and
124133
* we are waiting for server to accept the request.
125134
*/
126-
CANCEL_SHARDING = 'CANCEL_SHARDING',
135+
CANCELLING_SHARDING = 'CANCELLING_SHARDING',
127136

128137
/**
129138
* Sharding failed.
@@ -181,23 +190,36 @@ export type RootState = {
181190
status: ShardingStatuses.NOT_READY;
182191
shardKey?: never;
183192
shardingError?: never;
193+
pollingTimeout?: never;
184194
}
185195
| {
186196
status:
187197
| ShardingStatuses.UNSHARDED
188198
| ShardingStatuses.SUBMITTING_FOR_SHARDING
189-
| ShardingStatuses.SHARDING;
199+
| ShardingStatuses.CANCELLING_SHARDING;
190200
/**
191201
* note: shardKey might exist even for unsharded.
192202
* if the collection was sharded previously and then unmanaged
193203
*/
194204
shardKey?: ShardKey;
195205
shardingError?: never;
206+
pollingTimeout?: never;
207+
}
208+
| {
209+
status: ShardingStatuses.SHARDING;
210+
/**
211+
* note: shardKey might exist
212+
* if the collection was sharded previously and then unmanaged
213+
*/
214+
shardKey?: ShardKey;
215+
shardingError?: never;
216+
pollingTimeout?: NodeJS.Timeout;
196217
}
197218
| {
198219
status: ShardingStatuses.SHARDING_ERROR;
199220
shardKey?: never;
200221
shardingError: string;
222+
pollingTimeout?: never;
201223
}
202224
| {
203225
status:
@@ -207,6 +229,7 @@ export type RootState = {
207229
| ShardingStatuses.UNMANAGING_NAMESPACE;
208230
shardKey: ShardKey;
209231
shardingError?: never;
232+
pollingTimeout?: never;
210233
}
211234
);
212235

@@ -238,13 +261,15 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
238261
action,
239262
GlobalWritesActionTypes.NamespaceShardingErrorFetched
240263
) &&
241-
state.status === ShardingStatuses.NOT_READY
264+
(state.status === ShardingStatuses.NOT_READY ||
265+
state.status === ShardingStatuses.SHARDING)
242266
) {
243267
return {
244268
...state,
245269
status: ShardingStatuses.SHARDING_ERROR,
246270
shardKey: undefined,
247271
shardingError: action.error,
272+
pollingTimeout: undefined,
248273
};
249274
}
250275

@@ -253,13 +278,15 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
253278
action,
254279
GlobalWritesActionTypes.NamespaceShardKeyFetched
255280
) &&
256-
state.status === ShardingStatuses.NOT_READY
281+
(state.status === ShardingStatuses.NOT_READY ||
282+
state.status === ShardingStatuses.SHARDING)
257283
) {
258284
return {
259285
...state,
260286
status: getStatusFromShardKey(action.shardKey, state.managedNamespace),
261287
shardKey: action.shardKey,
262288
shardingError: undefined,
289+
pollingTimeout: undefined,
263290
};
264291
}
265292

@@ -302,6 +329,62 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
302329
};
303330
}
304331

332+
if (
333+
isAction<NextPollingTimeoutSetAction>(
334+
action,
335+
GlobalWritesActionTypes.NextPollingTimeoutSet
336+
) &&
337+
state.status === ShardingStatuses.SHARDING
338+
) {
339+
return {
340+
...state,
341+
pollingTimeout: action.timeout,
342+
};
343+
}
344+
345+
if (
346+
isAction<CancellingShardingStartedAction>(
347+
action,
348+
GlobalWritesActionTypes.CancellingShardingStarted
349+
) &&
350+
state.status === ShardingStatuses.SHARDING
351+
) {
352+
return {
353+
...state,
354+
status: ShardingStatuses.CANCELLING_SHARDING,
355+
pollingTimeout: undefined,
356+
};
357+
}
358+
359+
if (
360+
isAction<CancellingShardingErroredAction>(
361+
action,
362+
GlobalWritesActionTypes.CancellingShardingErrored
363+
) &&
364+
state.status === ShardingStatuses.CANCELLING_SHARDING
365+
) {
366+
return {
367+
...state,
368+
status: ShardingStatuses.SHARDING,
369+
};
370+
}
371+
372+
if (
373+
isAction<CancellingShardingFinishedAction>(
374+
action,
375+
GlobalWritesActionTypes.CancellingShardingFinished
376+
) &&
377+
(state.status === ShardingStatuses.CANCELLING_SHARDING ||
378+
state.status === ShardingStatuses.SHARDING_ERROR)
379+
// the error might come before the cancel request was processed
380+
) {
381+
return {
382+
...state,
383+
status: ShardingStatuses.UNSHARDED,
384+
shardingError: undefined,
385+
};
386+
}
387+
305388
if (
306389
isAction<SubmittingForShardingErroredAction>(
307390
action,
@@ -473,7 +556,11 @@ export const cancelSharding = ():
473556

474557
if (!confirmed) return;
475558

476-
const { namespace } = getState();
559+
const { namespace, status } = getState();
560+
561+
if (status === ShardingStatuses.SHARDING) {
562+
stopPollingForShardKey(getState);
563+
}
477564
dispatch({
478565
type: GlobalWritesActionTypes.CancellingShardingStarted,
479566
});
@@ -515,9 +602,31 @@ const setNamespaceBeingSharded = (
515602
type: GlobalWritesActionTypes.SubmittingForShardingFinished,
516603
managedNamespace,
517604
});
605+
dispatch(startPollingForShardKey());
518606
};
519607
};
520608

609+
const startPollingForShardKey = (): GlobalWritesThunkAction<
610+
void,
611+
NextPollingTimeoutSetAction
612+
> => {
613+
return (dispatch) => {
614+
const timeout = setTimeout(() => {
615+
void dispatch(fetchNamespaceShardKey());
616+
}, POLLING_INTERVAL);
617+
dispatch({
618+
type: GlobalWritesActionTypes.NextPollingTimeoutSet,
619+
timeout,
620+
});
621+
};
622+
};
623+
624+
const stopPollingForShardKey = (getState: () => RootState) => {
625+
const { pollingTimeout } = getState();
626+
if (!pollingTimeout) return;
627+
clearTimeout(pollingTimeout);
628+
};
629+
521630
export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
522631
Promise<void>,
523632
NamespaceShardingErrorFetchedAction | NamespaceShardKeyFetchedAction
@@ -527,7 +636,7 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
527636
getState,
528637
{ atlasGlobalWritesService, logger, connectionInfoRef }
529638
) => {
530-
const { namespace } = getState();
639+
const { namespace, status } = getState();
531640

532641
try {
533642
const [shardingError, shardKey] = await Promise.all([
@@ -536,6 +645,9 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
536645
]);
537646

538647
if (shardingError) {
648+
if (status === ShardingStatuses.SHARDING) {
649+
stopPollingForShardKey(getState);
650+
}
539651
dispatch({
540652
type: GlobalWritesActionTypes.NamespaceShardingErrorFetched,
541653
error: shardingError,
@@ -548,6 +660,9 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
548660
return;
549661
}
550662

663+
if (status === ShardingStatuses.SHARDING) {
664+
stopPollingForShardKey(getState);
665+
}
551666
dispatch({
552667
type: GlobalWritesActionTypes.NamespaceShardKeyFetched,
553668
shardKey,

0 commit comments

Comments
 (0)