|
| 1 | +package cloud.eppo; |
| 2 | + |
| 3 | +import static cloud.eppo.Utils.getMD5Hex; |
| 4 | + |
| 5 | +import cloud.eppo.ufc.dto.*; |
| 6 | +import cloud.eppo.ufc.dto.adapters.EppoModule; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | + |
| 16 | +/** |
| 17 | + * Encapsulates the Flag Configuration and Bandit parameters in an immutable object with a complete |
| 18 | + * and coherent state. |
| 19 | + * |
| 20 | + * <p>A Builder is used to prepare and then create am immutable data structure containing both flag |
| 21 | + * and bandit configurations. An intermediate step is required in building the configuration to |
| 22 | + * accommodate the as-needed loading of bandit parameters as a network call may not be needed if |
| 23 | + * there are no bandits referenced by the flag configuration. |
| 24 | + * |
| 25 | + * <p>Usage: Building with just flag configuration (unobfuscated is default) <code> |
| 26 | + * Configuration config = new Configuration.Builder(flagConfigJsonString).build(); |
| 27 | + * </code> |
| 28 | + * |
| 29 | + * <p>Building with bandits (known configuration) <code> |
| 30 | + * Configuration config = new Configuration.Builder(flagConfigJsonString).banditParameters(banditConfigJson).build(); |
| 31 | + * </code> |
| 32 | + * |
| 33 | + * <p>Conditionally loading bandit models (with or without an existing bandit config JSON string). |
| 34 | + * <code> |
| 35 | + * Configuration.Builder configBuilder = new Configuration.Builder(flagConfigJsonString).banditParameters(banditConfigJson); |
| 36 | + * if (configBuilder.requiresBanditModels()) { |
| 37 | + * // Load the bandit parameters encoded in a JSON string |
| 38 | + * configBuilder.banditParameters(banditParameterJsonString); |
| 39 | + * } |
| 40 | + * Configuration config = configBuilder.build(); |
| 41 | + * </code> |
| 42 | + * |
| 43 | + * <p> |
| 44 | + * |
| 45 | + * <p>Hint: when loading new Flag configuration values, set the current bandit models in the builder |
| 46 | + * then check `requiresBanditModels()`. |
| 47 | + */ |
| 48 | +public class Configuration { |
| 49 | + private static final Logger log = LoggerFactory.getLogger(Configuration.class); |
| 50 | + private final Map<String, BanditReference> banditReferences; |
| 51 | + private final Map<String, FlagConfig> flags; |
| 52 | + private final Map<String, BanditParameters> bandits; |
| 53 | + private final boolean isConfigObfuscated; |
| 54 | + |
| 55 | + @SuppressWarnings("unused") |
| 56 | + private final byte[] flagConfigJson; |
| 57 | + |
| 58 | + private final byte[] banditParamsJson; |
| 59 | + |
| 60 | + private Configuration( |
| 61 | + Map<String, FlagConfig> flags, |
| 62 | + Map<String, BanditReference> banditReferences, |
| 63 | + Map<String, BanditParameters> bandits, |
| 64 | + boolean isConfigObfuscated, |
| 65 | + byte[] flagConfigJson, |
| 66 | + byte[] banditParamsJson) { |
| 67 | + this.flags = flags; |
| 68 | + this.banditReferences = banditReferences; |
| 69 | + this.bandits = bandits; |
| 70 | + this.isConfigObfuscated = isConfigObfuscated; |
| 71 | + this.flagConfigJson = flagConfigJson; |
| 72 | + this.banditParamsJson = banditParamsJson; |
| 73 | + } |
| 74 | + |
| 75 | + public static Configuration emptyConfig() { |
| 76 | + return new Configuration( |
| 77 | + Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), false, null, null); |
| 78 | + } |
| 79 | + |
| 80 | + public FlagConfig getFlag(String flagKey) { |
| 81 | + String flagKeyForLookup = flagKey; |
| 82 | + if (isConfigObfuscated()) { |
| 83 | + flagKeyForLookup = getMD5Hex(flagKey); |
| 84 | + } |
| 85 | + |
| 86 | + if (flags == null) { |
| 87 | + log.warn("Request for flag {} before flags have been loaded", flagKey); |
| 88 | + return null; |
| 89 | + } else if (flags.isEmpty()) { |
| 90 | + log.warn("Request for flag {} with empty flags", flagKey); |
| 91 | + } |
| 92 | + return flags.get(flagKeyForLookup); |
| 93 | + } |
| 94 | + |
| 95 | + public String banditKeyForVariation(String flagKey, String variationValue) { |
| 96 | + // Note: In practice this double loop should be quite quick as the number of bandits and bandit |
| 97 | + // variations will be small. Should this ever change, we can optimize things. |
| 98 | + for (Map.Entry<String, BanditReference> banditEntry : banditReferences.entrySet()) { |
| 99 | + BanditReference banditReference = banditEntry.getValue(); |
| 100 | + for (BanditFlagVariation banditFlagVariation : banditReference.getFlagVariations()) { |
| 101 | + if (banditFlagVariation.getFlagKey().equals(flagKey) |
| 102 | + && banditFlagVariation.getVariationValue().equals(variationValue)) { |
| 103 | + return banditEntry.getKey(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + public BanditParameters getBanditParameters(String banditKey) { |
| 111 | + return bandits.get(banditKey); |
| 112 | + } |
| 113 | + |
| 114 | + public boolean isConfigObfuscated() { |
| 115 | + return isConfigObfuscated; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Builder to create the immutable config object. |
| 120 | + * |
| 121 | + * @see cloud.eppo.Configuration for usage. |
| 122 | + */ |
| 123 | + public static class Builder { |
| 124 | + private static final ObjectMapper mapper = |
| 125 | + new ObjectMapper().registerModule(EppoModule.eppoModule()); |
| 126 | + |
| 127 | + private final boolean isConfigObfuscated; |
| 128 | + private final Map<String, FlagConfig> flags; |
| 129 | + private Map<String, BanditReference> banditReferences; |
| 130 | + private Map<String, BanditParameters> bandits = Collections.emptyMap(); |
| 131 | + private final byte[] flagJson; |
| 132 | + private byte[] banditParamsJson; |
| 133 | + |
| 134 | + public Builder(String flagJson, boolean isConfigObfuscated) { |
| 135 | + this(flagJson.getBytes(), isConfigObfuscated); |
| 136 | + } |
| 137 | + |
| 138 | + public Builder(byte[] flagJson, boolean isConfigObfuscated) { |
| 139 | + this.isConfigObfuscated = isConfigObfuscated; |
| 140 | + |
| 141 | + if (flagJson == null || flagJson.length == 0) { |
| 142 | + throw new RuntimeException( |
| 143 | + "Null or empty configuration string. Call `Configuration.Empty()` instead"); |
| 144 | + } |
| 145 | + |
| 146 | + // Build the flags config from the json string. |
| 147 | + FlagConfigResponse config; |
| 148 | + try { |
| 149 | + config = mapper.readValue(flagJson, FlagConfigResponse.class); |
| 150 | + } catch (IOException e) { |
| 151 | + throw new RuntimeException(e); |
| 152 | + } |
| 153 | + |
| 154 | + if (config == null || config.getFlags() == null) { |
| 155 | + log.warn("'flags' map missing in flag definition JSON"); |
| 156 | + flags = Collections.emptyMap(); |
| 157 | + this.flagJson = null; |
| 158 | + } else { |
| 159 | + flags = Collections.unmodifiableMap(config.getFlags()); |
| 160 | + banditReferences = Collections.unmodifiableMap(config.getBanditReferences()); |
| 161 | + this.flagJson = flagJson; |
| 162 | + log.debug("Loaded {} flag definitions from flag definition JSON", flags.size()); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public boolean requiresBanditModels() { |
| 167 | + Set<String> neededModelVersions = referencedBanditModelVersion(); |
| 168 | + return !loadedBanditModelVersions().containsAll(neededModelVersions); |
| 169 | + } |
| 170 | + |
| 171 | + public Set<String> loadedBanditModelVersions() { |
| 172 | + return bandits.values().stream() |
| 173 | + .map(BanditParameters::getModelVersion) |
| 174 | + .collect(Collectors.toSet()); |
| 175 | + } |
| 176 | + |
| 177 | + public Set<String> referencedBanditModelVersion() { |
| 178 | + return banditReferences.values().stream() |
| 179 | + .map(BanditReference::getModelVersion) |
| 180 | + .collect(Collectors.toSet()); |
| 181 | + } |
| 182 | + |
| 183 | + public Builder banditParametersFromConfig(Configuration currentConfig) { |
| 184 | + if (currentConfig == null || currentConfig.bandits == null) { |
| 185 | + bandits = Collections.emptyMap(); |
| 186 | + } else { |
| 187 | + bandits = currentConfig.bandits; |
| 188 | + banditParamsJson = currentConfig.banditParamsJson; |
| 189 | + } |
| 190 | + return this; |
| 191 | + } |
| 192 | + |
| 193 | + public Builder banditParameters(String banditParameterJson) { |
| 194 | + return banditParameters(banditParameterJson.getBytes()); |
| 195 | + } |
| 196 | + |
| 197 | + public Builder banditParameters(byte[] banditParameterJson) { |
| 198 | + BanditParametersResponse config; |
| 199 | + try { |
| 200 | + config = mapper.readValue(banditParameterJson, BanditParametersResponse.class); |
| 201 | + } catch (IOException e) { |
| 202 | + log.error("Unable to parse bandit parameters JSON"); |
| 203 | + throw new RuntimeException(e); |
| 204 | + } |
| 205 | + |
| 206 | + if (config == null || config.getBandits() == null) { |
| 207 | + log.warn("`bandits` map missing in bandit parameters JSON"); |
| 208 | + bandits = Collections.emptyMap(); |
| 209 | + } else { |
| 210 | + bandits = Collections.unmodifiableMap(config.getBandits()); |
| 211 | + log.debug("Loaded {} bandit models from bandit parameters JSON", bandits.size()); |
| 212 | + } |
| 213 | + |
| 214 | + return this; |
| 215 | + } |
| 216 | + |
| 217 | + public Configuration build() { |
| 218 | + return new Configuration( |
| 219 | + flags, banditReferences, bandits, isConfigObfuscated, flagJson, banditParamsJson); |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments