Skip to content

Commit 9c75ee1

Browse files
[FSSDK-11981] bug bash addressed (#1097)
1 parent bfdcfad commit 9c75ee1

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

lib/client_factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const getOptimizelyInstance = (config: OptimizelyFactoryConfig): Optimize
6666
const cmabService = new DefaultCmabService({
6767
cmabClient,
6868
cmabCache,
69+
logger: logger?.child()
6970
});
7071

7172
const optimizelyOptions = {

lib/core/decision_service/cmab/cmab_service.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ import { LoggerFacade } from "../../../logging/logger";
1818
import { IOptimizelyUserContext } from "../../../optimizely_user_context";
1919
import { ProjectConfig } from "../../../project_config/project_config"
2020
import { OptimizelyDecideOption, UserAttributes } from "../../../shared_types"
21-
import { Cache, CacheWithRemove } from "../../../utils/cache/cache";
21+
import { CacheWithRemove } from "../../../utils/cache/cache";
2222
import { CmabClient } from "./cmab_client";
2323
import { v4 as uuidV4 } from 'uuid';
2424
import murmurhash from "murmurhash";
2525
import { DecideOptionsMap } from "..";
2626
import { SerialRunner } from "../../../utils/executor/serial_runner";
27+
import {
28+
CMAB_CACHE_ATTRIBUTES_MISMATCH,
29+
CMAB_CACHE_HIT,
30+
CMAB_CACHE_MISS,
31+
IGNORE_CMAB_CACHE,
32+
INVALIDATE_CMAB_CACHE,
33+
RESET_CMAB_CACHE,
34+
} from 'log_message';
2735

2836
export type CmabDecision = {
2937
variationId: string,
@@ -59,6 +67,7 @@ export type CmabServiceOptions = {
5967
}
6068

6169
const SERIALIZER_BUCKETS = 1000;
70+
const LOGGER_NAME = 'CmabService';
6271

6372
export class DefaultCmabService implements CmabService {
6473
private cmabCache: CacheWithRemove<CmabCacheValue>;
@@ -72,6 +81,7 @@ export class DefaultCmabService implements CmabService {
7281
this.cmabCache = options.cmabCache;
7382
this.cmabClient = options.cmabClient;
7483
this.logger = options.logger;
84+
this.logger?.setName(LOGGER_NAME);
7585
}
7686

7787
private getSerializerIndex(userId: string, experimentId: string): number {
@@ -98,19 +108,23 @@ export class DefaultCmabService implements CmabService {
98108
ruleId: string,
99109
options: DecideOptionsMap,
100110
): Promise<CmabDecision> {
111+
const userId = userContext.getUserId();
101112
const filteredAttributes = this.filterAttributes(projectConfig, userContext, ruleId);
102113

103114
if (options[OptimizelyDecideOption.IGNORE_CMAB_CACHE]) {
104-
return this.fetchDecision(ruleId, userContext.getUserId(), filteredAttributes);
115+
this.logger?.debug(IGNORE_CMAB_CACHE, userId, ruleId);
116+
return this.fetchDecision(ruleId, userId, filteredAttributes);
105117
}
106118

107119
if (options[OptimizelyDecideOption.RESET_CMAB_CACHE]) {
120+
this.logger?.debug(RESET_CMAB_CACHE, userId, ruleId);
108121
this.cmabCache.reset();
109122
}
110123

111-
const cacheKey = this.getCacheKey(userContext.getUserId(), ruleId);
124+
const cacheKey = this.getCacheKey(userId, ruleId);
112125

113126
if (options[OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE]) {
127+
this.logger?.debug(INVALIDATE_CMAB_CACHE, userId, ruleId);
114128
this.cmabCache.remove(cacheKey);
115129
}
116130

@@ -121,13 +135,17 @@ export class DefaultCmabService implements CmabService {
121135

122136
if (cachedValue) {
123137
if (cachedValue.attributesHash === attributesHash) {
138+
this.logger?.debug(CMAB_CACHE_HIT, userId, ruleId);
124139
return { variationId: cachedValue.variationId, cmabUuid: cachedValue.cmabUuid };
125140
} else {
141+
this.logger?.debug(CMAB_CACHE_ATTRIBUTES_MISMATCH, userId, ruleId);
126142
this.cmabCache.remove(cacheKey);
127143
}
144+
} else {
145+
this.logger?.debug(CMAB_CACHE_MISS, userId, ruleId);
128146
}
129147

130-
const variation = await this.fetchDecision(ruleId, userContext.getUserId(), filteredAttributes);
148+
const variation = await this.fetchDecision(ruleId, userId, filteredAttributes);
131149
this.cmabCache.save(cacheKey, {
132150
attributesHash,
133151
variationId: variation.variationId,

lib/index.browser.tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var getLogger = () => ({
6767
warn: () => {},
6868
error: () => {},
6969
child: () => getLogger(),
70+
setName: () => {},
7071
})
7172

7273
describe('javascript-sdk (Browser)', function() {

lib/message/log_message.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,11 @@ export const USER_HAS_NO_FORCED_VARIATION_FOR_EXPERIMENT =
6161
export const INVALID_EXPERIMENT_KEY_INFO =
6262
'Experiment key %s is not in datafile. It is either invalid, paused, or archived.';
6363
export const EVENT_STORE_FULL = 'Event store is full. Not saving event with id %d.';
64+
export const IGNORE_CMAB_CACHE = 'Ignoring CMAB cache for user %s and rule %s.';
65+
export const RESET_CMAB_CACHE = 'Resetting CMAB cache for user %s and rule %s.';
66+
export const INVALIDATE_CMAB_CACHE = 'Invalidating CMAB cache for user %s and rule %s.';
67+
export const CMAB_CACHE_HIT = 'Cache hit for user %s and rule %s.';
68+
export const CMAB_CACHE_ATTRIBUTES_MISMATCH = 'CMAB cache attributes mismatch for user %s and rule %s, fetching new decision.';
69+
export const CMAB_CACHE_MISS = 'Cache miss for user %s and rule %s.';
6470

6571
export const messages: string[] = [];

0 commit comments

Comments
 (0)