|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2017, 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.bucketing; |
| 18 | + |
| 19 | +import com.optimizely.ab.config.Experiment; |
| 20 | +import com.optimizely.ab.config.ProjectConfig; |
| 21 | +import com.optimizely.ab.config.Variation; |
| 22 | +import com.optimizely.ab.internal.ExperimentUtils; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import javax.annotation.Nonnull; |
| 27 | +import javax.annotation.Nullable; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * Optimizely's decision service that determines which variation of an experiment the user will be allocated to. |
| 32 | + * |
| 33 | + * The decision service contains all logic around how a user decision is made. This includes all of the following: |
| 34 | + * 1. Checking experiment status |
| 35 | + * 2. Checking whitelisting |
| 36 | + * 3. Checking sticky bucketing |
| 37 | + * 4. Checking audience targeting |
| 38 | + * 5. Using Murmurhash3 to bucket the user. |
| 39 | + */ |
| 40 | +public class DecisionService { |
| 41 | + |
| 42 | + private final Bucketer bucketer; |
| 43 | + private final ProjectConfig projectConfig; |
| 44 | + private final UserProfile userProfile; |
| 45 | + private static final Logger logger = LoggerFactory.getLogger(DecisionService.class); |
| 46 | + |
| 47 | + /** |
| 48 | + * Initialize a decision service for the Optimizely client. |
| 49 | + * @param bucketer Base bucketer to allocate new users to an experiment. |
| 50 | + * @param projectConfig Optimizely Project Config representing the datafile. |
| 51 | + * @param userProfile UserProfile implementation for storing decisions. |
| 52 | + */ |
| 53 | + public DecisionService(@Nonnull Bucketer bucketer, |
| 54 | + @Nonnull ProjectConfig projectConfig, |
| 55 | + @Nullable UserProfile userProfile) { |
| 56 | + this.bucketer = bucketer; |
| 57 | + this.projectConfig = projectConfig; |
| 58 | + this.userProfile = userProfile; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get a {@link Variation} of an {@link Experiment} for a user to be allocated into. |
| 63 | + * |
| 64 | + * @param experiment The Experiment the user will be bucketed into. |
| 65 | + * @param userId The userId of the user. |
| 66 | + * @param filteredAttributes The user's attributes. This should be filtered to just attributes in the Datafile. |
| 67 | + * @return The {@link Variation} the user is allocated into. |
| 68 | + */ |
| 69 | + public @Nullable Variation getVariation(@Nonnull Experiment experiment, |
| 70 | + @Nonnull String userId, |
| 71 | + @Nonnull Map<String, String> filteredAttributes) { |
| 72 | + |
| 73 | + if (!ExperimentUtils.isExperimentActive(experiment)) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + Variation variation; |
| 78 | + |
| 79 | + // check for whitelisting |
| 80 | + variation = getWhitelistedVariation(experiment, userId); |
| 81 | + if (variation != null) { |
| 82 | + return variation; |
| 83 | + } |
| 84 | + |
| 85 | + // check if user exists in user profile |
| 86 | + variation = getStoredVariation(experiment, userId); |
| 87 | + if (variation != null) { |
| 88 | + return variation; |
| 89 | + } |
| 90 | + |
| 91 | + if (ExperimentUtils.isUserInExperiment(projectConfig, experiment, filteredAttributes)) { |
| 92 | + Variation bucketedVariation = bucketer.bucket(experiment, userId); |
| 93 | + |
| 94 | + if (bucketedVariation != null) { |
| 95 | + storeVariation(experiment, bucketedVariation, userId); |
| 96 | + } |
| 97 | + |
| 98 | + return bucketedVariation; |
| 99 | + } |
| 100 | + logger.info("User \"{}\" does not meet conditions to be in experiment \"{}\".", userId, experiment.getKey()); |
| 101 | + |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the variation the user has been whitelisted into. |
| 107 | + * @param experiment {@link Experiment} in which user is to be bucketed. |
| 108 | + * @param userId User Identifier |
| 109 | + * @return null if the user is not whitelisted into any variation |
| 110 | + * {@link Variation} the user is bucketed into if the user has a specified whitelisted variation. |
| 111 | + */ |
| 112 | + @Nullable Variation getWhitelistedVariation(@Nonnull Experiment experiment, @Nonnull String userId) { |
| 113 | + // if a user has a forced variation mapping, return the respective variation |
| 114 | + Map<String, String> userIdToVariationKeyMap = experiment.getUserIdToVariationKeyMap(); |
| 115 | + if (userIdToVariationKeyMap.containsKey(userId)) { |
| 116 | + String forcedVariationKey = userIdToVariationKeyMap.get(userId); |
| 117 | + Variation forcedVariation = experiment.getVariationKeyToVariationMap().get(forcedVariationKey); |
| 118 | + if (forcedVariation != null) { |
| 119 | + logger.info("User \"{}\" is forced in variation \"{}\".", userId, forcedVariationKey); |
| 120 | + } else { |
| 121 | + logger.error("Variation \"{}\" is not in the datafile. Not activating user \"{}\".", forcedVariationKey, |
| 122 | + userId); |
| 123 | + } |
| 124 | + |
| 125 | + return forcedVariation; |
| 126 | + } |
| 127 | + |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get the {@link Variation} that has been stored for the user in the {@link UserProfile} implementation. |
| 133 | + * @param experiment {@link Experiment} in which the user was bucketed. |
| 134 | + * @param userId User Identifier |
| 135 | + * @return null if the {@link UserProfile} implementation is null or the user was not previously bucketed. |
| 136 | + * else return the {@link Variation} the user was previously bucketed into. |
| 137 | + */ |
| 138 | + @Nullable Variation getStoredVariation(@Nonnull Experiment experiment, @Nonnull String userId) { |
| 139 | + // ---------- Check User Profile for Sticky Bucketing ---------- |
| 140 | + // If a user profile instance is present then check it for a saved variation |
| 141 | + String experimentId = experiment.getId(); |
| 142 | + String experimentKey = experiment.getKey(); |
| 143 | + if (userProfile != null) { |
| 144 | + String variationId = userProfile.lookup(userId, experimentId); |
| 145 | + if (variationId != null) { |
| 146 | + Variation savedVariation = projectConfig |
| 147 | + .getExperimentIdMapping() |
| 148 | + .get(experimentId) |
| 149 | + .getVariationIdToVariationMap() |
| 150 | + .get(variationId); |
| 151 | + logger.info("Returning previously activated variation \"{}\" of experiment \"{}\" " |
| 152 | + + "for user \"{}\" from user profile.", |
| 153 | + savedVariation.getKey(), experimentKey, userId); |
| 154 | + // A variation is stored for this combined bucket id |
| 155 | + return savedVariation; |
| 156 | + } else { |
| 157 | + logger.info("No previously activated variation of experiment \"{}\" " |
| 158 | + + "for user \"{}\" found in user profile.", |
| 159 | + experimentKey, userId); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Store a {@link Variation} of an {@link Experiment} for a user in the {@link UserProfile}. |
| 168 | + * |
| 169 | + * @param experiment The experiment the user was buck |
| 170 | + * @param variation The Variation to store. |
| 171 | + * @param userId The ID of the user. |
| 172 | + */ |
| 173 | + void storeVariation(@Nonnull Experiment experiment, @Nonnull Variation variation, @Nonnull String userId) { |
| 174 | + String experimentId = experiment.getId(); |
| 175 | + // ---------- Save Variation to User Profile ---------- |
| 176 | + // If a user profile is present give it a variation to store |
| 177 | + if (userProfile != null) { |
| 178 | + String bucketedVariationId = variation.getId(); |
| 179 | + boolean saved = userProfile.save(userId, experimentId, bucketedVariationId); |
| 180 | + if (saved) { |
| 181 | + logger.info("Saved variation \"{}\" of experiment \"{}\" for user \"{}\".", |
| 182 | + bucketedVariationId, experimentId, userId); |
| 183 | + } else { |
| 184 | + logger.warn("Failed to save variation \"{}\" of experiment \"{}\" for user \"{}\".", |
| 185 | + bucketedVariationId, experimentId, userId); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments