Skip to content

Commit 4b35768

Browse files
update: refactor DefaultCmabService to streamline logger initialization and enhance cache handling logging
1 parent 4feb857 commit 4b35768

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

core-api/src/main/java/com/optimizely/ab/cmab/service/DefaultCmabService.java

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ public class DefaultCmabService implements CmabService {
4242
private final CmabClient cmabClient;
4343
private final Logger logger;
4444

45-
// public DefaultCmabService(CmabClient cmabClient, DefaultLRUCache<CmabCacheValue> cmabCache, Logger logger) {
46-
// this.cmabCache = cmabCache;
47-
// this.cmabClient = cmabClient;
48-
// this.logger = logger;
49-
// }
45+
public DefaultCmabService(CmabClient cmabClient, CacheWithRemove<CmabCacheValue> cmabCache) {
46+
this(cmabClient, cmabCache, null);
47+
}
5048

5149
public DefaultCmabService(CmabClient cmabClient, CacheWithRemove<CmabCacheValue> cmabCache, Logger logger) {
5250
this.cmabCache = cmabCache;
5351
this.cmabClient = cmabClient;
54-
this.logger = logger;
52+
this.logger = logger != null ? logger : LoggerFactory.getLogger(DefaultCmabService.class);
5553
}
5654

5755
@Override
@@ -61,15 +59,18 @@ public CmabDecision getDecision(ProjectConfig projectConfig, OptimizelyUserConte
6159
Map<String, Object> filteredAttributes = filterAttributes(projectConfig, userContext, ruleId);
6260

6361
if (options.contains(OptimizelyDecideOption.IGNORE_CMAB_CACHE)) {
62+
logger.debug("Ignoring CMAB cache for user '{}' and rule '{}'", userId, ruleId);
6463
return fetchDecision(ruleId, userId, filteredAttributes);
6564
}
6665

6766
if (options.contains(OptimizelyDecideOption.RESET_CMAB_CACHE)) {
67+
logger.debug("Resetting CMAB cache for user '{}' and rule '{}'", userId, ruleId);
6868
cmabCache.reset();
6969
}
7070

7171
String cacheKey = getCacheKey(userContext.getUserId(), ruleId);
7272
if (options.contains(OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE)) {
73+
logger.debug("Invalidating CMAB cache for user '{}' and rule '{}'", userId, ruleId);
7374
cmabCache.remove(cacheKey);
7475
}
7576

@@ -79,13 +80,19 @@ public CmabDecision getDecision(ProjectConfig projectConfig, OptimizelyUserConte
7980

8081
if (cachedValue != null) {
8182
if (cachedValue.getAttributesHash().equals(attributesHash)) {
83+
logger.debug("CMAB cache hit for user '{}' and rule '{}'", userId, ruleId);
8284
return new CmabDecision(cachedValue.getVariationId(), cachedValue.getCmabUuid());
8385
} else {
86+
logger.debug("CMAB cache attributes mismatch for user '{}' and rule '{}', fetching new decision", userId, ruleId);
8487
cmabCache.remove(cacheKey);
8588
}
89+
} else {
90+
logger.debug("CMAB cache miss for user '{}' and rule '{}'", userId, ruleId);
8691
}
8792

8893
CmabDecision cmabDecision = fetchDecision(ruleId, userId, filteredAttributes);
94+
logger.debug("CMAB decision is {}", cmabDecision);
95+
8996
cmabCache.save(cacheKey, new CmabCacheValue(attributesHash, cmabDecision.getVariationId(), cmabDecision.getCmabUUID()));
9097

9198
return cmabDecision;
@@ -104,18 +111,13 @@ private Map<String, Object> filterAttributes(ProjectConfig projectConfig, Optimi
104111
// Get experiment by rule ID
105112
Experiment experiment = projectConfig.getExperimentIdMapping().get(ruleId);
106113
if (experiment == null) {
107-
if (logger != null) {
108-
logger.debug("Experiment not found for rule ID: {}", ruleId);
109-
}
114+
logger.debug("Experiment not found for rule ID: {}", ruleId);
110115
return filteredAttributes;
111116
}
112117

113118
// Check if experiment has CMAB configuration
114-
// Add null check for getCmab()
115119
if (experiment.getCmab() == null) {
116-
if (logger != null) {
117-
logger.debug("No CMAB configuration found for experiment: {}", ruleId);
118-
}
120+
logger.debug("No CMAB configuration found for experiment: {}", ruleId);
119121
return filteredAttributes;
120122
}
121123

@@ -125,11 +127,8 @@ private Map<String, Object> filterAttributes(ProjectConfig projectConfig, Optimi
125127
}
126128

127129
Map<String, Attribute> attributeIdMapping = projectConfig.getAttributeIdMapping();
128-
// Add null check for attributeIdMapping
129130
if (attributeIdMapping == null) {
130-
if (logger != null) {
131-
logger.debug("No attribute mapping found in project config for rule ID: {}", ruleId);
132-
}
131+
logger.debug("No attribute mapping found in project config for rule ID: {}", ruleId);
133132
return filteredAttributes;
134133
}
135134

@@ -139,10 +138,10 @@ private Map<String, Object> filterAttributes(ProjectConfig projectConfig, Optimi
139138
if (attribute != null) {
140139
if (userAttributes.containsKey(attribute.getKey())) {
141140
filteredAttributes.put(attribute.getKey(), userAttributes.get(attribute.getKey()));
142-
} else if (logger != null) {
141+
} else {
143142
logger.debug("User attribute '{}' not found for attribute ID '{}'", attribute.getKey(), attributeId);
144143
}
145-
} else if (logger != null) {
144+
} else {
146145
logger.debug("Attribute configuration not found for ID: {}", attributeId);
147146
}
148147
}
@@ -202,7 +201,6 @@ public static class Builder {
202201
private int cmabCacheTimeoutInSecs = DEFAULT_CMAB_CACHE_TIMEOUT_SECS;
203202
private CacheWithRemove<CmabCacheValue> customCache;
204203
private CmabClient client;
205-
private Logger logger;
206204

207205
/**
208206
* Set the maximum size of the CMAB cache.
@@ -256,33 +254,15 @@ public Builder withCustomCache(CacheWithRemove<CmabCacheValue> cache) {
256254
return this;
257255
}
258256

259-
/**
260-
* Provide a custom {@link Logger} instance for logging CMAB service operations.
261-
*
262-
* If not provided, a default SLF4J logger will be used.
263-
*
264-
* @param logger The logger instance
265-
* @return Builder instance
266-
*/
267-
public Builder withLogger(Logger logger) {
268-
this.logger = logger;
269-
return this;
270-
}
271-
272257
public DefaultCmabService build() {
273258
if (client == null) {
274259
throw new IllegalStateException("CmabClient is required");
275260
}
276261

277-
if (logger == null) {
278-
logger = LoggerFactory.getLogger(DefaultCmabService.class);
279-
}
280-
281262
CacheWithRemove<CmabCacheValue> cache = customCache != null ? customCache :
282263
new DefaultLRUCache<>(cmabCacheSize, cmabCacheTimeoutInSecs);
283264

284-
285-
return new DefaultCmabService(client, cache, logger);
265+
return new DefaultCmabService(client, cache);
286266
}
287267
}
288268
}

0 commit comments

Comments
 (0)