Skip to content

Commit 1110f61

Browse files
committed
Skip caching a notification configuration that changed during its lookup
The change stream handler only refreshes cached buckets. When a bucket's configuration changed while its lookup was in flight, the handler skipped it, and the lookup then cached what it read before the change: a stale configuration, or since BB-908 a stale absence of one, kept until the bucket changes again or is evicted. Track the lookups in flight per bucket, have the handler flag those of the bucket that changed, and return their result without caching it, so that the next entry reads it again. Issue: BB-939
1 parent 83567b4 commit 1110f61

2 files changed

Lines changed: 123 additions & 5 deletions

File tree

‎extensions/notification/configManager/MongoConfigManager.js‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class MongoConfigManager extends BaseConfigManager {
8484
this._logger = params.logger;
8585
this._mongoConfig = params.mongoConfig;
8686
this._cachedConfigs = new LRUCache(params.maxCachedConfigs);
87+
this._pendingLookups = new Map();
8788
this._bucketMetastore = params.bucketMetastore;
8889
this._mongoClient = null;
8990
this._metastore = null;
@@ -142,6 +143,11 @@ class MongoConfigManager extends BaseConfigManager {
142143
* @returns {undefined}
143144
*/
144145
_handleChangeStreamChangeEvent(change) {
146+
// any change event can change the configuration. we don't know if it was an
147+
// actual change or a noop from just the event and an empty cache.
148+
for (const lookup of this._pendingLookups.get(change.documentKey._id) ?? []) {
149+
lookup.changed = true;
150+
}
145151
// invalidating cached notification configs
146152
const cachedConfig = this._cachedConfigs.get(change.documentKey._id);
147153
const bucketNotificationConfiguration = change.fullDocument
@@ -271,23 +277,30 @@ class MongoConfigManager extends BaseConfigManager {
271277
notificationConfiguration: cachedConfig,
272278
};
273279
}
280+
const lookup = { changed: false };
281+
this._addPendingLookup(bucket, lookup);
274282
try {
275283
// retreiving bucket metadata from the metastore
276284
const bucketMetadata = await this._metastore.findOne({ _id: bucket });
277285
const notificationConfiguration = bucketMetadata?.value?.notificationConfiguration;
278286
const delay = (Date.now() - startTime) / 1000;
279287
onConfigManagerConfigGet(false, delay);
280288
// when disabled after being enabled bucket notification configuration is set to an empty object
281-
if (!notificationConfiguration || !Object.keys(notificationConfiguration).length) {
289+
const hasConfig = notificationConfiguration && Object.keys(notificationConfiguration).length > 0;
290+
if (lookup.changed) {
291+
this._logger.debug('not caching configuration read while the bucket changed', {
292+
method: 'MongoConfigManager.getConfig',
293+
bucket,
294+
});
295+
} else {
282296
// caching the absence of configuration too, so that the
283297
// entries of buckets without one do not each cost a lookup
284-
this._cachedConfigs.add(bucket, NO_CONFIG);
298+
this._cachedConfigs.add(bucket, hasConfig ? notificationConfiguration : NO_CONFIG);
285299
onConfigManagerCacheUpdate('add', this._cachedConfigs.count());
300+
}
301+
if (!hasConfig) {
286302
return undefined;
287303
}
288-
// caching the bucket configuration
289-
this._cachedConfigs.add(bucket, notificationConfiguration);
290-
onConfigManagerCacheUpdate('add', this._cachedConfigs.count());
291304
return {
292305
bucket,
293306
notificationConfiguration,
@@ -299,6 +312,25 @@ class MongoConfigManager extends BaseConfigManager {
299312
error: err.message,
300313
});
301314
throw errors.InternalError.customizeDescription(err.message);
315+
} finally {
316+
this._removePendingLookup(bucket, lookup);
317+
}
318+
}
319+
320+
_addPendingLookup(bucket, lookup) {
321+
const lookups = this._pendingLookups.get(bucket);
322+
if (lookups) {
323+
lookups.add(lookup);
324+
} else {
325+
this._pendingLookups.set(bucket, new Set([lookup]));
326+
}
327+
}
328+
329+
_removePendingLookup(bucket, lookup) {
330+
const lookups = this._pendingLookups.get(bucket);
331+
lookups.delete(lookup);
332+
if (lookups.size === 0) {
333+
this._pendingLookups.delete(bucket);
302334
}
303335
}
304336

‎tests/unit/notification/configManager/MongoConfigManager.spec.js‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,92 @@ describe('MongoConfigManager ::', () => {
427427
});
428428
});
429429

430+
describe('getConfig of buckets changed during the lookup ::', () => {
431+
const updateEvent = (bucket, config) => ({
432+
_id: 'resumeToken',
433+
operationType: 'update',
434+
documentKey: { _id: bucket },
435+
fullDocument: {
436+
_id: bucket,
437+
value: { notificationConfiguration: config },
438+
},
439+
});
440+
441+
it('should return but not cache a configuration that changed during its lookup', async () => {
442+
const manager = new MongoConfigManager(params);
443+
const findOne = sinon.stub().callsFake(async () => {
444+
manager._handleChangeStreamChangeEvent(
445+
updateEvent('example-bucket-1', notificationConfigurationVariant),
446+
);
447+
return { value: { notificationConfiguration } };
448+
});
449+
manager._metastore = { findOne };
450+
assert.deepStrictEqual(await manager.getConfig('example-bucket-1'), {
451+
bucket: 'example-bucket-1',
452+
notificationConfiguration,
453+
});
454+
assert.strictEqual(manager._cachedConfigs.get('example-bucket-1'), undefined);
455+
await manager.getConfig('example-bucket-1');
456+
assert.strictEqual(findOne.callCount, 2);
457+
});
458+
459+
it('should not cache a missing configuration when one got enabled during its lookup', async () => {
460+
const manager = new MongoConfigManager(params);
461+
const findOne = sinon.stub();
462+
findOne.onFirstCall().callsFake(async () => {
463+
manager._handleChangeStreamChangeEvent(updateEvent('example-bucket-1', notificationConfiguration));
464+
return { value: {} };
465+
});
466+
findOne.onSecondCall().resolves({ value: { notificationConfiguration } });
467+
manager._metastore = { findOne };
468+
assert.strictEqual(await manager.getConfig('example-bucket-1'), undefined);
469+
assert.strictEqual(manager._cachedConfigs.get('example-bucket-1'), undefined);
470+
assert.deepStrictEqual(await manager.getConfig('example-bucket-1'), {
471+
bucket: 'example-bucket-1',
472+
notificationConfiguration,
473+
});
474+
});
475+
476+
it('should cache a configuration when another bucket changed during its lookup', async () => {
477+
const manager = new MongoConfigManager(params);
478+
manager._metastore = {
479+
findOne: async () => {
480+
manager._handleChangeStreamChangeEvent(
481+
updateEvent('example-bucket-2', notificationConfigurationVariant),
482+
);
483+
return { value: { notificationConfiguration } };
484+
},
485+
};
486+
await manager.getConfig('example-bucket-1');
487+
assert.deepStrictEqual(manager._cachedConfigs.get('example-bucket-1'), notificationConfiguration);
488+
});
489+
490+
it('should cache a lookup started after the change', async () => {
491+
const manager = new MongoConfigManager(params);
492+
let second;
493+
const findOne = sinon.stub();
494+
findOne.onFirstCall().callsFake(async () => {
495+
manager._handleChangeStreamChangeEvent(
496+
updateEvent('example-bucket-1', notificationConfigurationVariant),
497+
);
498+
second = manager.getConfig('example-bucket-1');
499+
return { value: { notificationConfiguration } };
500+
});
501+
findOne.onSecondCall().resolves({ value: { notificationConfiguration: notificationConfigurationVariant } });
502+
manager._metastore = { findOne };
503+
await manager.getConfig('example-bucket-1');
504+
await second;
505+
assert.deepStrictEqual(manager._cachedConfigs.get('example-bucket-1'), notificationConfigurationVariant);
506+
});
507+
508+
it('should forget the lookup when it fails', async () => {
509+
const manager = new MongoConfigManager(params);
510+
manager._metastore = { findOne: sinon.stub().rejects(new Error('read failed')) };
511+
await assert.rejects(manager.getConfig('example-bucket-1'));
512+
assert.strictEqual(manager._pendingLookups.size, 0);
513+
});
514+
});
515+
430516
describe('setConfig ::', () => {
431517
it('should always return false', async () => {
432518
const manager = new MongoConfigManager(params);

0 commit comments

Comments
 (0)