Skip to content

Commit 2e63da9

Browse files
fayyazarshadzashraf1985
authored andcommitted
feat: Added caching for OptimizelyConfig object (#352)
## Summary Updated the `OptimizelyConfig` implementation to always return cached object. `OptimizelyConfig` object will only be updated only when new `ProjectConfig` is received. ## Test Plan Added Unit tests
1 parent 239d877 commit 2e63da9

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.optimizely.ab.event.internal.payload.EventBatch;
3030
import com.optimizely.ab.notification.*;
3131
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
32+
import com.optimizely.ab.optimizelyconfig.OptimizelyConfigManager;
3233
import com.optimizely.ab.optimizelyconfig.OptimizelyConfigService;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
@@ -87,6 +88,9 @@ public class Optimizely implements AutoCloseable {
8788

8889
private final ProjectConfigManager projectConfigManager;
8990

91+
@Nullable
92+
private final OptimizelyConfigManager optimizelyConfigManager;
93+
9094
// TODO should be private
9195
public final NotificationCenter notificationCenter;
9296

@@ -99,6 +103,7 @@ private Optimizely(@Nonnull EventHandler eventHandler,
99103
@Nonnull DecisionService decisionService,
100104
@Nullable UserProfileService userProfileService,
101105
@Nonnull ProjectConfigManager projectConfigManager,
106+
@Nullable OptimizelyConfigManager optimizelyConfigManager,
102107
@Nonnull NotificationCenter notificationCenter
103108
) {
104109
this.eventHandler = eventHandler;
@@ -107,6 +112,7 @@ private Optimizely(@Nonnull EventHandler eventHandler,
107112
this.decisionService = decisionService;
108113
this.userProfileService = userProfileService;
109114
this.projectConfigManager = projectConfigManager;
115+
this.optimizelyConfigManager = optimizelyConfigManager;
110116
this.notificationCenter = notificationCenter;
111117
}
112118

@@ -884,20 +890,6 @@ public Variation getForcedVariation(@Nonnull String experimentKey,
884890
return decisionService.getForcedVariation(experiment, userId);
885891
}
886892

887-
/**
888-
* Get {@link OptimizelyConfig} containing experiments and features map
889-
*
890-
* @return {@link OptimizelyConfig}
891-
*/
892-
public OptimizelyConfig getOptimizelyConfig() {
893-
ProjectConfig projectConfig = getProjectConfig();
894-
if (projectConfig == null) {
895-
logger.error("Optimizely instance is not valid, failing getOptimizelyConfig call.");
896-
return null;
897-
}
898-
return new OptimizelyConfigService(projectConfig).getConfig();
899-
}
900-
901893
/**
902894
* @return the current {@link ProjectConfig} instance.
903895
*/
@@ -928,6 +920,25 @@ private boolean validateUserId(String userId) {
928920
return true;
929921
}
930922

923+
/**
924+
* Get {@link OptimizelyConfig} containing experiments and features map
925+
*
926+
* @return {@link OptimizelyConfig}
927+
*/
928+
public OptimizelyConfig getOptimizelyConfig() {
929+
ProjectConfig projectConfig = getProjectConfig();
930+
if (projectConfig == null) {
931+
logger.error("Optimizely instance is not valid, failing getOptimizelyConfig call.");
932+
return null;
933+
}
934+
if (optimizelyConfigManager != null) {
935+
return optimizelyConfigManager.getOptimizelyConfig();
936+
}
937+
// Generate and return a new OptimizelyConfig object as a fallback when consumer implements their own ProjectConfigManager without implementing OptimizelyConfigManager.
938+
logger.debug("optimizelyConfigManager is null, generating new OptimizelyConfigObject as a fallback");
939+
return new OptimizelyConfigService(projectConfig).getConfig();
940+
}
941+
931942
/**
932943
* Helper method which makes separate copy of attributesMap variable and returns it
933944
*
@@ -1029,6 +1040,7 @@ public static class Builder {
10291040
private EventProcessor eventProcessor;
10301041
private ProjectConfig projectConfig;
10311042
private ProjectConfigManager projectConfigManager;
1043+
private OptimizelyConfigManager optimizelyConfigManager;
10321044
private UserProfileService userProfileService;
10331045
private NotificationCenter notificationCenter;
10341046

@@ -1155,6 +1167,12 @@ public Optimizely build() {
11551167
projectConfigManager = fallbackConfigManager;
11561168
}
11571169

1170+
// PollingProjectConfigManager now also implements OptimizelyConfigManager interface to support OptimizelyConfig API.
1171+
// This check is needed in case a consumer provides their own ProjectConfigManager which does nt implement OptimizelyConfigManager interface
1172+
if (projectConfigManager instanceof OptimizelyConfigManager) {
1173+
optimizelyConfigManager = (OptimizelyConfigManager) projectConfigManager;
1174+
}
1175+
11581176
if (notificationCenter == null) {
11591177
notificationCenter = new NotificationCenter();
11601178
}
@@ -1164,7 +1182,7 @@ public Optimizely build() {
11641182
eventProcessor = new ForwardingEventProcessor(eventHandler, notificationCenter);
11651183
}
11661184

1167-
return new Optimizely(eventHandler, eventProcessor, errorHandler, decisionService, userProfileService, projectConfigManager, notificationCenter);
1185+
return new Optimizely(eventHandler, eventProcessor, errorHandler, decisionService, userProfileService, projectConfigManager, optimizelyConfigManager, notificationCenter);
11681186
}
11691187
}
11701188
}

core-api/src/main/java/com/optimizely/ab/config/PollingProjectConfigManager.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2019, Optimizely and contributors
3+
* Copyright 2019-2020, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818

1919
import com.optimizely.ab.notification.NotificationCenter;
2020
import com.optimizely.ab.notification.UpdateConfigNotification;
21+
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
22+
import com.optimizely.ab.optimizelyconfig.OptimizelyConfigManager;
23+
import com.optimizely.ab.optimizelyconfig.OptimizelyConfigService;
2124
import org.slf4j.Logger;
2225
import org.slf4j.LoggerFactory;
2326

@@ -37,12 +40,13 @@
3740
* is initially set. A default ProjectConfig can be provided to bootstrap the initial ProjectConfig
3841
* return value and prevent blocking.
3942
*/
40-
public abstract class PollingProjectConfigManager implements ProjectConfigManager, AutoCloseable {
43+
public abstract class PollingProjectConfigManager implements ProjectConfigManager, AutoCloseable, OptimizelyConfigManager {
4144

4245
private static final Logger logger = LoggerFactory.getLogger(PollingProjectConfigManager.class);
4346
private static final UpdateConfigNotification SIGNAL = new UpdateConfigNotification();
4447

4548
private final AtomicReference<ProjectConfig> currentProjectConfig = new AtomicReference<>();
49+
private final AtomicReference<OptimizelyConfig> currentOptimizelyConfig = new AtomicReference<>();
4650
private final ScheduledExecutorService scheduledExecutorService;
4751
private final long period;
4852
private final TimeUnit timeUnit;
@@ -99,6 +103,7 @@ void setConfig(ProjectConfig projectConfig) {
99103
logger.info("New datafile set with revision: {}. Old revision: {}", projectConfig.getRevision(), previousRevision);
100104

101105
currentProjectConfig.set(projectConfig);
106+
currentOptimizelyConfig.set(new OptimizelyConfigService(projectConfig).getConfig());
102107
countDownLatch.countDown();
103108
notificationCenter.send(SIGNAL);
104109
}
@@ -132,6 +137,15 @@ public ProjectConfig getConfig() {
132137
return projectConfig == null ? currentProjectConfig.get() : projectConfig;
133138
}
134139

140+
/**
141+
* Returns the cached {@link OptimizelyConfig}
142+
* @return {@link OptimizelyConfig}
143+
*/
144+
@Override
145+
public OptimizelyConfig getOptimizelyConfig() {
146+
return currentOptimizelyConfig.get();
147+
}
148+
135149
public synchronized void start() {
136150
if (started) {
137151
logger.warn("Manager already started.");

core-api/src/main/java/com/optimizely/ab/optimizelyconfig/OptimizelyConfig.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ public Map<String, OptimizelyFeature> getFeaturesMap() {
4545
public String getRevision() {
4646
return revision;
4747
}
48+
49+
@Override
50+
public boolean equals(Object obj) {
51+
if (obj == null || getClass() != obj.getClass()) return false;
52+
if (obj == this) return true;
53+
OptimizelyConfig optimizelyConfig = (OptimizelyConfig) obj;
54+
return revision.equals(optimizelyConfig.getRevision()) &&
55+
experimentsMap.equals(optimizelyConfig.getExperimentsMap()) &&
56+
featuresMap.equals(optimizelyConfig.getFeaturesMap());
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
int hash = revision.hashCode();
62+
hash = 31 * hash + experimentsMap.hashCode();
63+
return hash;
64+
}
4865
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
*
3+
* Copyright 2020, Optimizely and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.optimizely.ab.optimizelyconfig;
18+
19+
public interface OptimizelyConfigManager {
20+
/**
21+
* Implementations of this method should return {@link OptimizelyConfig}
22+
*
23+
* @return {@link OptimizelyConfig}
24+
*/
25+
OptimizelyConfig getOptimizelyConfig();
26+
}

core-api/src/test/java/com/optimizely/ab/config/PollingProjectConfigManagerTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright 2019, Optimizely and contributors
3+
* Copyright 2019-2020, Optimizely and contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -150,6 +150,26 @@ public ProjectConfig poll() {
150150
assertEquals(newerProjectConfig, testProjectConfigManager.getConfig());
151151
}
152152

153+
@Test
154+
public void testSetOptimizelyConfig(){
155+
assertNull(testProjectConfigManager.getOptimizelyConfig());
156+
157+
testProjectConfigManager.setConfig(projectConfig);
158+
assertEquals("1480511547", testProjectConfigManager.getOptimizelyConfig().getRevision());
159+
160+
// cached config because project config is null
161+
testProjectConfigManager.setConfig(null);
162+
assertEquals("1480511547", testProjectConfigManager.getOptimizelyConfig().getRevision());
163+
164+
// created config with new revision
165+
ProjectConfig newerProjectConfig = mock(ProjectConfig.class);
166+
when(newerProjectConfig.getRevision()).thenReturn("new");
167+
168+
// verify the new optimizely config
169+
testProjectConfigManager.setConfig(newerProjectConfig);
170+
assertEquals("new", testProjectConfigManager.getOptimizelyConfig().getRevision());
171+
}
172+
153173
@Test
154174
public void testErroringProjectConfigManagerWithTimeout() throws Exception {
155175
testProjectConfigManager = new TestProjectConfigManager() {

0 commit comments

Comments
 (0)