Skip to content

Commit 41e491a

Browse files
Add support for CMAB traffic allocation in Bucketer class
1 parent fd959ed commit 41e491a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.optimizely.ab.bucketing;
1818

19+
import java.util.Collections;
1920
import java.util.List;
2021

2122
import javax.annotation.Nonnull;
@@ -111,8 +112,23 @@ private DecisionResponse<Variation> bucketToVariation(@Nonnull ExperimentCore ex
111112
int bucketValue = generateBucketValue(hashCode);
112113
logger.debug("Assigned bucket {} to user with bucketingId \"{}\" when bucketing to a variation.", bucketValue, bucketingId);
113114

115+
if (experiment instanceof Experiment && ((Experiment) experiment).getCmab() != null) {
116+
// Override traffic allocations for CMAB experiments
117+
String message = reasons.addInfo("Using CMAB traffic allocation for experiment \"%s\"", experimentKey);
118+
logger.info(message);
119+
trafficAllocations = Collections.singletonList(
120+
new TrafficAllocation("$", ((Experiment) experiment).getCmab().getTrafficAllocation())
121+
);
122+
}
123+
114124
String bucketedVariationId = bucketToEntity(bucketValue, trafficAllocations);
115-
if (bucketedVariationId != null) {
125+
if ("$".equals(bucketedVariationId)) {
126+
// for cmab experiments
127+
String message = reasons.addInfo("User with bucketingId \"%s\" is bucketed into CMAB for experiment \"%s\"", bucketingId, experimentKey);
128+
logger.info(message);
129+
return new DecisionResponse(new Variation("$", "$"), reasons);
130+
}
131+
else if (bucketedVariationId != null) {
116132
Variation bucketedVariation = experiment.getVariationIdToVariationMap().get(bucketedVariationId);
117133
String variationKey = bucketedVariation.getKey();
118134
String message = reasons.addInfo("User with bucketingId \"%s\" is in variation \"%s\" of experiment \"%s\".", bucketingId, variationKey,

0 commit comments

Comments
 (0)