Skip to content

Commit 6300679

Browse files
committed
polling
1 parent 2c7bb9d commit 6300679

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.
@@ -180,23 +189,36 @@ export type RootState = {
180189
status: ShardingStatuses.NOT_READY;
181190
shardKey?: never;
182191
shardingError?: never;
192+
pollingTimeout?: never;
183193
}
184194
| {
185195
status:
186196
| ShardingStatuses.UNSHARDED
187197
| ShardingStatuses.SUBMITTING_FOR_SHARDING
188-
| ShardingStatuses.SHARDING;
198+
| ShardingStatuses.CANCELLING_SHARDING;
189199
/**
190200
* note: shardKey might exist even for unsharded.
191201
* if the collection was sharded previously and then unmanaged
192202
*/
193203
shardKey?: ShardKey;
194204
shardingError?: never;
205+
pollingTimeout?: never;
206+
}
207+
| {
208+
status: ShardingStatuses.SHARDING;
209+
/**
210+
* note: shardKey might exist
211+
* if the collection was sharded previously and then unmanaged
212+
*/
213+
shardKey?: ShardKey;
214+
shardingError?: never;
215+
pollingTimeout?: NodeJS.Timeout;
195216
}
196217
| {
197218
status: ShardingStatuses.SHARDING_ERROR;
198219
shardKey?: never;
199220
shardingError: string;
221+
pollingTimeout?: never;
200222
}
201223
| {
202224
status:
@@ -206,6 +228,7 @@ export type RootState = {
206228
| ShardingStatuses.UNMANAGING_NAMESPACE;
207229
shardKey: ShardKey;
208230
shardingError?: never;
231+
pollingTimeout?: never;
209232
}
210233
);
211234

@@ -237,13 +260,15 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
237260
action,
238261
GlobalWritesActionTypes.NamespaceShardingErrorFetched
239262
) &&
240-
state.status === ShardingStatuses.NOT_READY
263+
(state.status === ShardingStatuses.NOT_READY ||
264+
state.status === ShardingStatuses.SHARDING)
241265
) {
242266
return {
243267
...state,
244268
status: ShardingStatuses.SHARDING_ERROR,
245269
shardKey: undefined,
246270
shardingError: action.error,
271+
pollingTimeout: undefined,
247272
};
248273
}
249274

@@ -252,13 +277,15 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
252277
action,
253278
GlobalWritesActionTypes.NamespaceShardKeyFetched
254279
) &&
255-
state.status === ShardingStatuses.NOT_READY
280+
(state.status === ShardingStatuses.NOT_READY ||
281+
state.status === ShardingStatuses.SHARDING)
256282
) {
257283
return {
258284
...state,
259285
status: getStatusFromShardKey(action.shardKey, state.managedNamespace),
260286
shardKey: action.shardKey,
261287
shardingError: undefined,
288+
pollingTimeout: undefined,
262289
};
263290
}
264291

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

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

473556
if (!confirmed) return;
474557

475-
const { namespace } = getState();
558+
const { namespace, status } = getState();
559+
560+
if (status === ShardingStatuses.SHARDING) {
561+
stopPollingForShardKey(getState);
562+
}
476563
dispatch({
477564
type: GlobalWritesActionTypes.CancellingShardingStarted,
478565
});
@@ -514,9 +601,31 @@ const setNamespaceBeingSharded = (
514601
type: GlobalWritesActionTypes.SubmittingForShardingFinished,
515602
managedNamespace,
516603
});
604+
dispatch(startPollingForShardKey());
517605
};
518606
};
519607

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

531640
try {
532641
const [shardingError, shardKey] = await Promise.all([
@@ -535,6 +644,9 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
535644
]);
536645

537646
if (shardingError) {
647+
if (status === ShardingStatuses.SHARDING) {
648+
stopPollingForShardKey(getState);
649+
}
538650
dispatch({
539651
type: GlobalWritesActionTypes.NamespaceShardingErrorFetched,
540652
error: shardingError,
@@ -547,6 +659,9 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
547659
return;
548660
}
549661

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

0 commit comments

Comments
 (0)