Skip to content

Commit 77957f4

Browse files
authored
[FSSDK-13432] Don't treat the first invalid datafile from datafileManager as fatal (#1188)
## Summary - `ProjectConfigManagerImpl` no longer fails to start on the first invalid datafile from the `datafileManager`. Startup now fails only after `MAX_INVALID_DATAFILE_EMIT` (3) invalid datafiles arrive before a valid one. A valid datafile within that limit starts the manager as usual. - An invalid datafile with no `datafileManager` is still a fatal init error, as before. - `jsonSchemaValidator` and `datafileManager` are now `private`. A single bad response, such as a truncated or corrupted payload, used to stop the SDK from starting even though the next poll would have returned a valid datafile.
1 parent 241c9c9 commit 77957f4

2 files changed

Lines changed: 80 additions & 15 deletions

File tree

‎lib/project_config/project_config_manager.spec.ts‎

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2020, 2022, 2024, Optimizely
2+
* Copyright 2019-2020, 2022, 2024, 2026, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@ import { resolvablePromise } from '../utils/promise/resolvablePromise';
2323
import { getMockDatafileManager } from '../tests/mock/mock_datafile_manager';
2424
import { wait } from '../tests/testUtils';
2525

26+
import { MAX_INVALID_DATAFILE_EMIT } from './project_config_manager';
27+
2628
describe('ProjectConfigManagerImpl', () => {
2729
describe('a logger is passed in the constructor', () => {
2830
it('should set name on the logger passed into the constructor', () => {
@@ -201,14 +203,14 @@ describe('ProjectConfigManagerImpl', () => {
201203
describe('when datafile is invalid', () => {
202204
it('should reject onRunning() with the same error if datafileManager.onRunning() rejects', async () => {
203205
const datafileManager = getMockDatafileManager({ onRunning: Promise.reject(new Error('test error')) });
204-
const manager = new ProjectConfigManagerImpl({ datafile: JSON.stringify({}), datafileManager });
206+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
205207
manager.start();
206208
await expect(manager.onRunning()).rejects.toThrow('DatafileManager failed to start, reason: test error');
207209
});
208210

209211
it('should resolve onRunning() if datafileManager.onUpdate() is fired and should update config', async () => {
210212
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
211-
const manager = new ProjectConfigManagerImpl({ datafile: JSON.stringify({}), datafileManager });
213+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
212214
manager.start();
213215
datafileManager.pushUpdate(JSON.stringify(testData.getTestProjectConfig()));
214216
await expect(manager.onRunning()).resolves.not.toThrow();
@@ -217,7 +219,7 @@ describe('ProjectConfigManagerImpl', () => {
217219

218220
it('should resolve onRunning(), update config and call onUpdate listeners if datafileManager.onUpdate() is fired', async () => {
219221
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
220-
const manager = new ProjectConfigManagerImpl({ datafile: JSON.stringify({}), datafileManager });
222+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
221223
manager.start();
222224
const listener = vi.fn();
223225
manager.onUpdate(listener);
@@ -230,20 +232,52 @@ describe('ProjectConfigManagerImpl', () => {
230232

231233
it('should return undefined from getConfig() before onRunning() resolves', async () => {
232234
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
233-
const manager = new ProjectConfigManagerImpl({ datafile: JSON.stringify({}), datafileManager });
235+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
234236
manager.start();
235237
expect(manager.getConfig()).toBeUndefined();
236238
});
237239

238240
it('should return the correct config from getConfig() after onRunning() resolves', async () => {
239241
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
240-
const manager = new ProjectConfigManagerImpl({ datafile: JSON.stringify({}), datafileManager });
242+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
241243
manager.start();
242244

243245
datafileManager.pushUpdate(JSON.stringify(testData.getTestProjectConfig()));
244246
await expect(manager.onRunning()).resolves.not.toThrow();
245247
expect(manager.getConfig()).toEqual(createProjectConfig(JSON.stringify(testData.getTestProjectConfig())));
246248
});
249+
250+
it('should reject onRunning() and onTerminated() if datafileManger emits invalid datafile \
251+
MAX_INVALID_DATAFILE_EMIT times before running', async () => {
252+
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
253+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
254+
manager.start();
255+
256+
for(let i = 0; i < MAX_INVALID_DATAFILE_EMIT; i++) {
257+
// emit invalid datafile
258+
datafileManager.pushUpdate('ab');
259+
}
260+
261+
await expect(manager.onRunning()).rejects.toThrow();
262+
await expect(manager.onTerminated()).rejects.toThrow();
263+
});
264+
265+
it('should resolve onRunning and update config when datafileManager emits valid datafile before \
266+
emitting invalid datafile MAX_INVALID_DATAFILE_EMIT times', async () => {
267+
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
268+
const manager = new ProjectConfigManagerImpl({ datafile: 'ab', datafileManager });
269+
manager.start();
270+
271+
for(let i = 0; i < MAX_INVALID_DATAFILE_EMIT - 1; i++) {
272+
// emit invalid datafile
273+
datafileManager.pushUpdate('ab');
274+
}
275+
276+
datafileManager.pushUpdate(JSON.stringify(testData.getTestProjectConfig()));
277+
278+
await expect(manager.onRunning()).resolves.not.toThrow();
279+
expect(manager.getConfig()).toEqual(createProjectConfig(JSON.stringify(testData.getTestProjectConfig())));
280+
});
247281
});
248282
});
249283

@@ -255,16 +289,40 @@ describe('ProjectConfigManagerImpl', () => {
255289
await expect(manager.onRunning()).rejects.toThrow('DatafileManager failed to start, reason: test error');
256290
});
257291

258-
it('should reject onRunning() and onTerminated if datafileManager emits an invalid datafile in the first onUpdate', async () => {
292+
it('should reject onRunning() and onTerminated() if datafileManger emits invalid datafile \
293+
MAX_INVALID_DATAFILE_EMIT times before running', async () => {
259294
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
260295
const manager = new ProjectConfigManagerImpl({ datafileManager });
261296
manager.start();
262-
datafileManager.pushUpdate('foo');
297+
298+
for(let i = 0; i < MAX_INVALID_DATAFILE_EMIT; i++) {
299+
// emit invalid datafile
300+
datafileManager.pushUpdate('ab');
301+
}
302+
263303
await expect(manager.onRunning()).rejects.toThrow();
264304
await expect(manager.onTerminated()).rejects.toThrow();
265305
});
266306

267-
it('should resolve onRunning(), update config and call onUpdate listeners if datafileManager.onUpdate() is fired', async () => {
307+
it('should resolve onRunning and update config when datafile manger emits valid datafile before \
308+
emitting inbalid datafile MAX_INVALID_DATAFILE_EMIT times', async () => {
309+
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
310+
const manager = new ProjectConfigManagerImpl({ datafileManager });
311+
manager.start();
312+
313+
for(let i = 0; i < MAX_INVALID_DATAFILE_EMIT - 1; i++) {
314+
// emit invalid datafile
315+
datafileManager.pushUpdate('ab');
316+
}
317+
318+
datafileManager.pushUpdate(JSON.stringify(testData.getTestProjectConfig()));
319+
320+
await expect(manager.onRunning()).resolves.not.toThrow();
321+
expect(manager.getConfig()).toEqual(createProjectConfig(JSON.stringify(testData.getTestProjectConfig())));
322+
});
323+
324+
it('should resolve onRunning(), update config and call onUpdate listeners if datafileManager.onUpdate() is fired \
325+
with valid datafile on the first call', async () => {
268326
const datafileManager = getMockDatafileManager({ onRunning: Promise.resolve() });
269327
const manager = new ProjectConfigManagerImpl({ datafileManager });
270328
manager.start();

‎lib/project_config/project_config_manager.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2022, 2024, Optimizely
2+
* Copyright 2019-2022, 2024, 2026, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,13 +56,16 @@ export interface ProjectConfigManager extends Service {
5656

5757
export const LOGGER_NAME = 'ProjectConfigManager';
5858

59+
export const MAX_INVALID_DATAFILE_EMIT = 3;
60+
5961
export class ProjectConfigManagerImpl extends BaseService implements ProjectConfigManager {
6062
private datafile?: string;
6163
private projectConfig?: ProjectConfig;
6264
private optimizelyConfig?: OptimizelyConfig;
63-
public jsonSchemaValidator?: Transformer<unknown, boolean>;
64-
public datafileManager?: DatafileManager;
65+
private jsonSchemaValidator?: Transformer<unknown, boolean>;
66+
private datafileManager?: DatafileManager;
6567
private eventEmitter: EventEmitter<{ update: ProjectConfig }> = new EventEmitter();
68+
private datafileManagerInvalidEmit = 0;
6669

6770
constructor(config: ProjectConfigManagerConfig) {
6871
super();
@@ -170,14 +173,18 @@ export class ProjectConfigManagerImpl extends BaseService implements ProjectConf
170173
} catch (err) {
171174
this.logger?.error(err);
172175

176+
if (!fromConfig) {
177+
this.datafileManagerInvalidEmit++;
178+
}
179+
173180
// if the state is starting and no datafileManager is provided, we cannot recover.
174181
// If the state is starting and the datafileManager has emitted a datafile,
175182
// that means a datafile was not provided in config or an invalid datafile was provided,
176183
// otherwise the state would have already been set to running synchronously.
177-
// If the first datafile emitted by the datafileManager is invalid,
184+
// If MAX_INVALID_DATAFILE_EMIT number of datafiles emitted by the datafileManager is invalid,
178185
// we consider this to be an initialization error as well.
179-
const fatalError = (this.isStarting() && !this.datafileManager) ||
180-
(this.isStarting() && !fromConfig);
186+
const fatalError = this.isStarting()
187+
&& (!this.datafileManager || this.datafileManagerInvalidEmit >= MAX_INVALID_DATAFILE_EMIT);
181188
if (fatalError) {
182189
this.handleInitError(new Error(GOT_INVALID_DATAFILE));
183190
}

0 commit comments

Comments
 (0)