Skip to content

Commit c0b6c85

Browse files
committed
remove support for custom decisionservice
1 parent fdbf75a commit c0b6c85

File tree

6 files changed

+81
-148
lines changed

6 files changed

+81
-148
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
import com.optimizely.ab.bucketing.FeatureDecision;
2020
import com.optimizely.ab.config.*;
2121
import com.optimizely.ab.notification.DecisionNotification;
22-
import com.optimizely.ab.optimizelyjson.OptimizelyJSON;
22+
import com.optimizely.ab.optimizelydecision.DecisionMessage;
2323
import com.optimizely.ab.optimizelydecision.DecisionReasons;
2424
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
2525
import com.optimizely.ab.optimizelydecision.OptimizelyDecision;
26+
import com.optimizely.ab.optimizelyjson.OptimizelyJSON;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
2829

@@ -41,10 +42,6 @@ public class OptimizelyUserContext {
4142

4243
private static final Logger logger = LoggerFactory.getLogger(OptimizelyUserContext.class);
4344

44-
public final static String SDK_NOT_READY = "Optimizely SDK not configured properly yet.";
45-
public final static String FLAG_KEY_INVALID = "No flag was found for key \"%s\".";
46-
public final static String VARIABLE_VALUE_INVALID = "Variable value for key \"%s\" is invalid or wrong type.";
47-
4845
public OptimizelyUserContext(@Nonnull Optimizely optimizely,
4946
@Nonnull String userId,
5047
@Nonnull Map<String, ?> attributes) {
@@ -93,12 +90,12 @@ public OptimizelyDecision decide(@Nonnull String key,
9390

9491
ProjectConfig projectConfig = optimizely.getProjectConfig();
9592
if (projectConfig == null) {
96-
return OptimizelyDecision.createErrorDecision(key, this, SDK_NOT_READY);
93+
return OptimizelyDecision.createErrorDecision(key, this, DecisionMessage.SDK_NOT_READY.reason());
9794
}
9895

9996
FeatureFlag flag = projectConfig.getFeatureKeyMapping().get(key);
10097
if (flag == null) {
101-
return OptimizelyDecision.createErrorDecision(key, this, getFlagKeyInvalidMessage(key));
98+
return OptimizelyDecision.createErrorDecision(key, this, DecisionMessage.FLAG_KEY_INVALID.reason(key));
10299
}
103100

104101
Boolean sentEvent = false;
@@ -107,7 +104,7 @@ public OptimizelyDecision decide(@Nonnull String key,
107104
Boolean includeReasons = allOptions.contains(OptimizelyDecideOption.INCLUDE_REASONS);
108105
DecisionReasons decisionReasons = new DecisionReasons(includeReasons);
109106

110-
Map<String, ?> copiedAttributes = copyAttributes();
107+
Map<String, ?> copiedAttributes = new HashMap<>(attributes);
111108
FeatureDecision flagDecision = optimizely.decisionService.getVariationForFeature(
112109
flag,
113110
userId,
@@ -287,24 +284,12 @@ public void trackEvent(@Nonnull String eventName) throws UnknownEventTypeExcepti
287284

288285
// Utils
289286

290-
private Map<String, Object> copyAttributes() {
291-
return new HashMap<>(attributes);
292-
}
293-
294287
private List<OptimizelyDecideOption> getAllOptions(List<OptimizelyDecideOption> options) {
295288
List<OptimizelyDecideOption> copiedOptions = new ArrayList(optimizely.defaultDecideOptions);
296289
copiedOptions.addAll(options);
297290
return copiedOptions;
298291
}
299292

300-
public static String getFlagKeyInvalidMessage(String flagKey) {
301-
return String.format(FLAG_KEY_INVALID, flagKey);
302-
}
303-
304-
public static String getVariableValueInvalidMessage(String variableKey) {
305-
return String.format(VARIABLE_VALUE_INVALID, variableKey);
306-
}
307-
308293
private Map<String, Object> getDecisionVariableMap(@Nonnull FeatureFlag flag,
309294
@Nonnull Variation variation,
310295
@Nonnull Boolean featureEnabled,
@@ -321,7 +306,7 @@ private Map<String, Object> getDecisionVariableMap(@Nonnull FeatureFlag flag,
321306

322307
Object convertedValue = optimizely.convertStringToType(value, variable.getType());
323308
if (convertedValue == null) {
324-
decisionReasons.addError(getVariableValueInvalidMessage(variable.getKey()));
309+
decisionReasons.addError(DecisionMessage.VARIABLE_VALUE_INVALID.reason(variable.getKey()));
325310
} else if (convertedValue instanceof OptimizelyJSON) {
326311
convertedValue = ((OptimizelyJSON) convertedValue).toMap();
327312
}

core-api/src/main/java/com/optimizely/ab/bucketing/Bucketer.java

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -139,36 +139,6 @@ public Variation bucket(@Nonnull Experiment experiment,
139139
@Nonnull ProjectConfig projectConfig,
140140
@Nonnull List<OptimizelyDecideOption> options,
141141
@Nonnull DecisionReasons reasons) {
142-
143-
// support existing custom Bucketers
144-
if (isMethodOverriden("bucket", Experiment.class, String.class, ProjectConfig.class)) {
145-
return bucket(experiment, bucketingId, projectConfig);
146-
}
147-
148-
return bucketCore(experiment, bucketingId, projectConfig, options, reasons);
149-
}
150-
151-
/**
152-
* Assign a {@link Variation} of an {@link Experiment} to a user based on hashed value from murmurhash3.
153-
*
154-
* @param experiment The Experiment in which the user is to be bucketed.
155-
* @param bucketingId string A customer-assigned value used to create the key for the murmur hash.
156-
* @param projectConfig The current projectConfig
157-
* @return Variation the user is bucketed into or null.
158-
*/
159-
@Nullable
160-
public Variation bucket(@Nonnull Experiment experiment,
161-
@Nonnull String bucketingId,
162-
@Nonnull ProjectConfig projectConfig) {
163-
return bucketCore(experiment, bucketingId, projectConfig, Collections.emptyList(), new DecisionReasons());
164-
}
165-
166-
@Nullable
167-
public Variation bucketCore(@Nonnull Experiment experiment,
168-
@Nonnull String bucketingId,
169-
@Nonnull ProjectConfig projectConfig,
170-
@Nonnull List<OptimizelyDecideOption> options,
171-
@Nonnull DecisionReasons reasons) {
172142
// ---------- Bucket User ----------
173143
String groupId = experiment.getGroupId();
174144
// check whether the experiment belongs to a group
@@ -202,6 +172,13 @@ public Variation bucketCore(@Nonnull Experiment experiment,
202172
return bucketToVariation(experiment, bucketingId, options, reasons);
203173
}
204174

175+
@Nullable
176+
public Variation bucket(@Nonnull Experiment experiment,
177+
@Nonnull String bucketingId,
178+
@Nonnull ProjectConfig projectConfig) {
179+
return bucket(experiment, bucketingId, projectConfig, Collections.emptyList(), new DecisionReasons());
180+
}
181+
205182
//======== Helper methods ========//
206183

207184
/**
@@ -217,12 +194,4 @@ int generateBucketValue(int hashCode) {
217194
return (int) Math.floor(MAX_TRAFFIC_VALUE * ratio);
218195
}
219196

220-
Boolean isMethodOverriden(String methodName, Class<?>... params) {
221-
try {
222-
return getClass() != Bucketer.class && getClass().getDeclaredMethod(methodName, params) != null;
223-
} catch (NoSuchMethodException e) {
224-
return false;
225-
}
226-
}
227-
228197
}

core-api/src/main/java/com/optimizely/ab/bucketing/DecisionService.java

Lines changed: 23 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,6 @@ public Variation getVariation(@Nonnull Experiment experiment,
9595
@Nonnull ProjectConfig projectConfig,
9696
@Nonnull List<OptimizelyDecideOption> options,
9797
@Nonnull DecisionReasons reasons) {
98-
99-
// support existing custom DecisionServices
100-
if (isMethodOverriden("getVariation", Experiment.class, String.class, Map.class, ProjectConfig.class)) {
101-
return getVariation(experiment, userId, filteredAttributes, projectConfig);
102-
}
103-
104-
return getVariationCore(experiment, userId, filteredAttributes, projectConfig, options, reasons);
105-
}
106-
107-
@Nullable
108-
public Variation getVariation(@Nonnull Experiment experiment,
109-
@Nonnull String userId,
110-
@Nonnull Map<String, ?> filteredAttributes,
111-
@Nonnull ProjectConfig projectConfig) {
112-
return getVariationCore(experiment, userId, filteredAttributes, projectConfig, Collections.emptyList(), new DecisionReasons());
113-
}
114-
115-
@Nullable
116-
public Variation getVariationCore(@Nonnull Experiment experiment,
117-
@Nonnull String userId,
118-
@Nonnull Map<String, ?> filteredAttributes,
119-
@Nonnull ProjectConfig projectConfig,
120-
@Nonnull List<OptimizelyDecideOption> options,
121-
@Nonnull DecisionReasons reasons) {
12298
if (!ExperimentUtils.isExperimentActive(experiment, options, reasons)) {
12399
return null;
124100
}
@@ -188,6 +164,14 @@ public Variation getVariationCore(@Nonnull Experiment experiment,
188164
return null;
189165
}
190166

167+
@Nullable
168+
public Variation getVariation(@Nonnull Experiment experiment,
169+
@Nonnull String userId,
170+
@Nonnull Map<String, ?> filteredAttributes,
171+
@Nonnull ProjectConfig projectConfig) {
172+
return getVariation(experiment, userId, filteredAttributes, projectConfig, Collections.emptyList(), new DecisionReasons());
173+
}
174+
191175
/**
192176
* Get the variation the user is bucketed into for the FeatureFlag
193177
*
@@ -206,30 +190,6 @@ public FeatureDecision getVariationForFeature(@Nonnull FeatureFlag featureFlag,
206190
@Nonnull ProjectConfig projectConfig,
207191
@Nonnull List<OptimizelyDecideOption> options,
208192
@Nonnull DecisionReasons reasons) {
209-
// support existing custom DecisionServices
210-
if (isMethodOverriden("getVariationForFeature", FeatureFlag.class, String.class, Map.class, ProjectConfig.class)) {
211-
return getVariationForFeature(featureFlag, userId, filteredAttributes, projectConfig);
212-
}
213-
214-
return getVariationForFeatureCore(featureFlag, userId, filteredAttributes, projectConfig, options, reasons);
215-
}
216-
217-
@Nonnull
218-
public FeatureDecision getVariationForFeature(@Nonnull FeatureFlag featureFlag,
219-
@Nonnull String userId,
220-
@Nonnull Map<String, ?> filteredAttributes,
221-
@Nonnull ProjectConfig projectConfig) {
222-
223-
return getVariationForFeatureCore(featureFlag, userId, filteredAttributes, projectConfig,Collections.emptyList(), new DecisionReasons());
224-
}
225-
226-
@Nonnull
227-
public FeatureDecision getVariationForFeatureCore(@Nonnull FeatureFlag featureFlag,
228-
@Nonnull String userId,
229-
@Nonnull Map<String, ?> filteredAttributes,
230-
@Nonnull ProjectConfig projectConfig,
231-
@Nonnull List<OptimizelyDecideOption> options,
232-
@Nonnull DecisionReasons reasons) {
233193
if (!featureFlag.getExperimentIds().isEmpty()) {
234194
for (String experimentId : featureFlag.getExperimentIds()) {
235195
Experiment experiment = projectConfig.getExperimentIdMapping().get(experimentId);
@@ -256,6 +216,15 @@ public FeatureDecision getVariationForFeatureCore(@Nonnull FeatureFlag featureFl
256216
return featureDecision;
257217
}
258218

219+
@Nonnull
220+
public FeatureDecision getVariationForFeature(@Nonnull FeatureFlag featureFlag,
221+
@Nonnull String userId,
222+
@Nonnull Map<String, ?> filteredAttributes,
223+
@Nonnull ProjectConfig projectConfig) {
224+
225+
return getVariationForFeature(featureFlag, userId, filteredAttributes, projectConfig,Collections.emptyList(), new DecisionReasons());
226+
}
227+
259228
/**
260229
* Try to bucket the user into a rollout rule.
261230
* Evaluate the user for rules in priority order by seeing if the user satisfies the audience.
@@ -598,27 +567,6 @@ public Variation getForcedVariation(@Nonnull Experiment experiment,
598567
@Nonnull String userId,
599568
@Nonnull List<OptimizelyDecideOption> options,
600569
@Nonnull DecisionReasons reasons) {
601-
602-
// support existing custom DecisionServices
603-
if (isMethodOverriden("getForcedVariation", Experiment.class, String.class)) {
604-
return getForcedVariation(experiment, userId);
605-
}
606-
607-
return getForcedVariationCore(experiment, userId, options, reasons);
608-
}
609-
610-
@Nullable
611-
public Variation getForcedVariation(@Nonnull Experiment experiment,
612-
@Nonnull String userId) {
613-
return getForcedVariationCore(experiment, userId, Collections.emptyList(), new DecisionReasons());
614-
}
615-
616-
@Nullable
617-
public Variation getForcedVariationCore(@Nonnull Experiment experiment,
618-
@Nonnull String userId,
619-
@Nonnull List<OptimizelyDecideOption> options,
620-
@Nonnull DecisionReasons reasons) {
621-
622570
// if the user id is invalid, return false.
623571
if (!validateUserId(userId)) {
624572
return null;
@@ -642,6 +590,12 @@ public Variation getForcedVariationCore(@Nonnull Experiment experiment,
642590
return null;
643591
}
644592

593+
@Nullable
594+
public Variation getForcedVariation(@Nonnull Experiment experiment,
595+
@Nonnull String userId) {
596+
return getForcedVariation(experiment, userId, Collections.emptyList(), new DecisionReasons());
597+
}
598+
645599
/**
646600
* Helper function to check that the provided userId is valid
647601
*
@@ -657,12 +611,4 @@ private boolean validateUserId(String userId) {
657611
return true;
658612
}
659613

660-
Boolean isMethodOverriden(String methodName, Class<?>... params) {
661-
try {
662-
return getClass() != DecisionService.class && getClass().getDeclaredMethod(methodName, params) != null;
663-
} catch (NoSuchMethodException e) {
664-
return false;
665-
}
666-
}
667-
668614
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
18+
package com.optimizely.ab.optimizelydecision;
19+
20+
public enum DecisionMessage {
21+
SDK_NOT_READY("Optimizely SDK not configured properly yet."),
22+
FLAG_KEY_INVALID("No flag was found for key \"%s\"."),
23+
VARIABLE_VALUE_INVALID("Variable value for key \"%s\" is invalid or wrong type.");
24+
25+
private String format;
26+
27+
DecisionMessage(String format) {
28+
this.format = format;
29+
}
30+
31+
public String reason(Object... args){
32+
return String.format(format, args);
33+
}
34+
}

core-api/src/main/java/com/optimizely/ab/optimizelydecision/DecisionReasons.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121

2222
public class DecisionReasons {
2323

24-
boolean includeReasons;
25-
List<String> errors;
26-
List<String> logs;
24+
private boolean includeReasons;
25+
private final List<String> errors = new ArrayList<>();
26+
private final List<String> logs = new ArrayList<>();
2727

2828
public DecisionReasons(boolean includeReasons) {
2929
this.includeReasons = includeReasons;
30-
this.errors = new ArrayList<String>();
31-
this.logs = new ArrayList<String>();
3230
}
3331

3432
public DecisionReasons() {

0 commit comments

Comments
 (0)