Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.DS_Store
**/npm-debug.log
/.idea
/.sonar
**/node_modules
/.vscode

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTORS-GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Split SDK is an open source project and we welcome feedback and contribution. Th
6. Run all tests and make sure there are no failures.
7. `git push` your changes to GitHub within your topic branch.
8. Open a Pull Request(PR) from your forked repo and into the `development` branch of the original repository.
9. When creating your PR, please fill out all the fields of the PR template, as applicable, for the project.
9. When creating your PR, please describe what you accomplished, how to test your changes, and any extra notes reviewers should know about.
10. Check for conflicts once the pull request is created to make sure your PR can be merged cleanly into `development`.
11. Keep an eye out for any feedback or comments from Split's SDK team.

Expand Down
2 changes: 2 additions & 0 deletions src/sync/__tests__/syncManagerOnline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ test('syncManagerOnline should syncAll a single time when sync is disabled', asy

expect(pollingManagerMock.start).not.toBeCalled();
expect(pollingManagerMock.syncAll).toBeCalledTimes(1);
expect(pollingManagerMock.syncAll).lastCalledWith(true); // retries with backoff, since there is no polling

syncManager.stop();
expect(pollingManagerMock.stop).toBeCalled(); // cancels pending retries
await syncManager.start();

expect(pollingManagerMock.start).not.toBeCalled();
Expand Down
41 changes: 41 additions & 0 deletions src/sync/polling/__tests__/retrier.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { retrierFactory } from '../retrier';
import { Backoff } from '../../../utils/Backoff';

describe('retrier', () => {

beforeAll(() => { Backoff.__TEST__BASE_MILLIS = 10; });
afterAll(() => { Backoff.__TEST__BASE_MILLIS = undefined; });

test('retries until the task succeeds', async () => {
const task = jest.fn()
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true);

expect(await retrierFactory(task).run()).toBe(true);
expect(task).toBeCalledTimes(3);
});

test('does not retry if the task succeeds', async () => {
const task = jest.fn().mockResolvedValue(true);

expect(await retrierFactory(task).run()).toBe(true);
expect(task).toBeCalledTimes(1);
});

test('cancel stops pending retries', async () => {
const task = jest.fn().mockResolvedValue(false);
const retrier = retrierFactory(task);

const promise = retrier.run();
await new Promise(res => setTimeout(res, 0)); // first attempt fails and a retry is scheduled
retrier.cancel();

expect(await promise).toBe(false);
expect(task).toBeCalledTimes(1);

await new Promise(res => setTimeout(res, 50));
expect(task).toBeCalledTimes(1);
});

});
26 changes: 22 additions & 4 deletions src/sync/polling/pollingManagerCS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ISdkFactoryContextSync } from '../../sdkFactory/types';
import { usesSegmentsSync } from '../../storages/AbstractDefinitionsCacheSync';
import { SdkUpdateMetadata } from '../../../types/splitio';
import { splitChangesFetcherFactory } from './fetchers/splitChangesFetcher';
import { retrierFactory } from './retrier';

/**
* Expose start / stop mechanism for polling data from services.
Expand All @@ -29,6 +30,9 @@ export function pollingManagerCSFactory(

// Map of matching keys to their corresponding MySegmentsSyncTask.
const mySegmentsSyncTasks: Record<string, IMySegmentsSyncTask> = {};
// Map of matching keys to retriers of their MySegmentsSyncTask, used to retry the initial sync with backoff.
const mySegmentsRetriers: Record<string, ReturnType<typeof retrierFactory>> = {};
const definitionsRetrier = retrierFactory(() => definitionsSyncTask.execute());

const matchingKey = getMatching(settings.core.key);
const mySegmentsSyncTask = add(matchingKey, readiness, storage);
Expand Down Expand Up @@ -70,6 +74,7 @@ export function pollingManagerCSFactory(
else readiness.definitions.once(SDK_DEFINITIONS_ARRIVED, smartReady);

mySegmentsSyncTasks[matchingKey] = mySegmentsSyncTask;
mySegmentsRetriers[matchingKey] = retrierFactory(() => mySegmentsSyncTask.execute());
return mySegmentsSyncTask;
}

Expand All @@ -91,24 +96,37 @@ export function pollingManagerCSFactory(

if (definitionsSyncTask.isRunning()) definitionsSyncTask.stop();
stopMySegmentsSyncTasks();

// cancel pending retries of `syncAll`
definitionsRetrier.cancel();
forOwn(mySegmentsRetriers, (retrier) => retrier.cancel());
},

// Used by SyncManager to know if running in polling mode.
isRunning: definitionsSyncTask.isRunning,

// fetch definitions and segments
syncAll() {
const promises = [definitionsSyncTask.execute()];
forOwn(mySegmentsSyncTasks, (mySegmentsSyncTask) => {
promises.push(mySegmentsSyncTask.execute());
syncAll(retryWithBackoff) {
const promises = [retryWithBackoff ? definitionsRetrier.run() : definitionsSyncTask.execute()];
forOwn(mySegmentsSyncTasks, (mySegmentsSyncTask, matchingKey) => {
promises.push(retryWithBackoff ? mySegmentsRetriers[matchingKey].run() : mySegmentsSyncTask.execute());
});
return Promise.all(promises);
},

// fetch memberships of a shared client
syncMySegments(matchingKey, retryWithBackoff) {
const mySegmentsSyncTask = mySegmentsSyncTasks[matchingKey];
if (!mySegmentsSyncTask) return Promise.resolve(false);
return retryWithBackoff ? mySegmentsRetriers[matchingKey].run() : mySegmentsSyncTask.execute();
},

// Support for handling mySegments sync of multiple clients
add,

remove(matchingKey: string) {
mySegmentsRetriers[matchingKey].cancel();
delete mySegmentsRetriers[matchingKey];
delete mySegmentsSyncTasks[matchingKey];
},

Expand Down
13 changes: 12 additions & 1 deletion src/sync/polling/pollingManagerSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { POLLING_START, POLLING_STOP, LOG_PREFIX_SYNC_POLLING } from '../../logg
import { ISdkFactoryContextSync } from '../../sdkFactory/types';
import { segmentChangesFetcherFactory } from './fetchers/segmentChangesFetcher';
import { splitChangesFetcherFactory } from './fetchers/splitChangesFetcher';
import { retrierFactory } from './retrier';

/**
* Expose start / stop mechanism for pulling data from services.
Expand All @@ -23,6 +24,9 @@ export function pollingManagerSSFactory(
const segmentsSyncTask: ISegmentsSyncTask = segmentsSyncTaskFactory(segmentChangesFetcher, storage, readiness, settings);
const definitionsSyncTask: IDefinitionsSyncTask = definitionsSyncTaskFactory(definitionChangesFetcher, storage, readiness, settings, segmentsSyncTask);

const definitionsRetrier = retrierFactory(() => definitionsSyncTask.execute());
const segmentsRetrier = retrierFactory(() => segmentsSyncTask.execute());

return {
definitionsSyncTask,
segmentsSyncTask,
Expand All @@ -47,13 +51,20 @@ export function pollingManagerSSFactory(

if (definitionsSyncTask.isRunning()) definitionsSyncTask.stop();
if (segmentsSyncTask.isRunning()) segmentsSyncTask.stop();

// cancel pending retries of `syncAll`
definitionsRetrier.cancel();
segmentsRetrier.cancel();
},

// Used by SyncManager to know if running in polling mode.
isRunning: definitionsSyncTask.isRunning,

syncAll() {
syncAll(retryWithBackoff) {
// fetch definitions and segments. There is no need to catch this promise (`DefinitionChangesUpdater` is always resolved with a boolean value)
if (retryWithBackoff) {
return definitionsRetrier.run().then((success) => success && segmentsRetrier.run());
}
return definitionsSyncTask.execute().then(() => {
return segmentsSyncTask.execute();
});
Expand Down
36 changes: 36 additions & 0 deletions src/sync/polling/retrier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Backoff } from '../../utils/Backoff';

/**
* Creates a retrier that executes a task until it succeeds, waiting with exponential backoff between attempts.
* Used for the initial synchronization when periodic polling is disabled (i.e., `single_sync` mode), since otherwise there is no other attempt.
*
* @param task - Task that returns a promise that NEVER REJECTS, and resolves with `false` on failure.
* @returns An object with a `run` method that returns a promise resolved with `true` when the task succeeds,
* or `false` if the retrier is cancelled while retrying; and a `cancel` method that stops any pending retry.
*/
export function retrierFactory(task: () => Promise<boolean>) {

let cancelled = false;
const backoff = new Backoff(attempt);

function attempt(): Promise<boolean> {
return task().then((success) => {
if (success) {
backoff.reset();
return true;
}
return cancelled ? false : backoff.scheduleCallAsync<boolean>();
});
}

return {
run() {
cancelled = false;
return attempt();
},
cancel() {
cancelled = true;
backoff.reset({ value: false });
}
};
}
7 changes: 6 additions & 1 deletion src/sync/polling/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export type MySegmentsData = {
export interface IMySegmentsSyncTask extends ISyncTask<[segmentsData?: MySegmentsData, noCache?: boolean, till?: number], boolean> { }

export interface IPollingManager extends ITask {
syncAll(): Promise<any>
/**
* Fetches definitions and segments.
* @param retryWithBackoff - if true, retries each fetch with exponential backoff until it succeeds, or `stop` is called.
*/
syncAll(retryWithBackoff?: boolean): Promise<any>
definitionsSyncTask: IDefinitionsSyncTask
segmentsSyncTask: ISyncTask
}
Expand All @@ -48,6 +52,7 @@ export interface IPollingManager extends ITask {
* PollingManager for client-side with support for multiple clients
*/
export interface IPollingManagerCS extends IPollingManager {
syncMySegments(matchingKey: string, retryWithBackoff?: boolean): Promise<any>
add(matchingKey: string, readiness: IReadinessManager, storage: IStorageSync): IMySegmentsSyncTask
remove(matchingKey: string): void;
get(matchingKey: string): IMySegmentsSyncTask | undefined
Expand Down
7 changes: 4 additions & 3 deletions src/sync/syncManagerOnline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function syncManagerOnlineFactory(
}
} else {
if (startFirstTime) {
pollingManager.syncAll();
pollingManager.syncAll(true);
}
}
}
Expand All @@ -135,7 +135,8 @@ export function syncManagerOnlineFactory(

// stop syncing
if (pushManager) pushManager.stop();
if (pollingManager && pollingManager.isRunning()) pollingManager.stop();
// if sync is disabled (single sync), polling is not running but `stop` must cancel the pending retries of `syncAll`
if (pollingManager && (pollingManager.isRunning() || !syncEnabled)) pollingManager.stop();

// stop periodic data recording (events, impressions, telemetry).
submitterManager.stop();
Expand Down Expand Up @@ -172,7 +173,7 @@ export function syncManagerOnlineFactory(
if (usesSegmentsSync(storage)) mySegmentsSyncTask.start();
}
} else {
if (!readinessManager.isReady()) mySegmentsSyncTask.execute();
if (!readinessManager.isReady()) (pollingManager as IPollingManagerCS).syncMySegments(matchingKey, true);
}
}

Expand Down