|
1 | 1 | package cloud.eppo;
|
2 | 2 |
|
3 |
| -import java.io.IOException; |
4 |
| -import okhttp3.Response; |
| 3 | +import cloud.eppo.api.Configuration; |
| 4 | +import java.util.concurrent.CompletableFuture; |
| 5 | +import java.util.concurrent.CompletionException; |
| 6 | +import java.util.concurrent.ExecutionException; |
| 7 | +import org.jetbrains.annotations.NotNull; |
5 | 8 | import org.slf4j.Logger;
|
6 | 9 | import org.slf4j.LoggerFactory;
|
7 | 10 |
|
8 |
| -// TODO: handle bandit stuff |
9 | 11 | public class ConfigurationRequestor {
|
10 | 12 | private static final Logger log = LoggerFactory.getLogger(ConfigurationRequestor.class);
|
| 13 | + private static final String FLAG_CONFIG_PATH = "/api/flag-config/v1/config"; |
| 14 | + private static final String BANDIT_PARAMETER_PATH = "/api/flag-config/v1/bandits"; |
11 | 15 |
|
12 | 16 | private final EppoHttpClient client;
|
13 |
| - private final ConfigurationStore configurationStore; |
| 17 | + private final IConfigurationStore configurationStore; |
14 | 18 | private final boolean expectObfuscatedConfig;
|
| 19 | + private final boolean supportBandits; |
| 20 | + |
| 21 | + private CompletableFuture<Void> remoteFetchFuture = null; |
| 22 | + private CompletableFuture<Void> configurationFuture = null; |
| 23 | + private boolean initialConfigSet = false; |
15 | 24 |
|
16 | 25 | public ConfigurationRequestor(
|
17 |
| - ConfigurationStore configurationStore, |
18 |
| - EppoHttpClient client, |
19 |
| - boolean expectObfuscatedConfig) { |
| 26 | + @NotNull IConfigurationStore configurationStore, |
| 27 | + @NotNull EppoHttpClient client, |
| 28 | + boolean expectObfuscatedConfig, |
| 29 | + boolean supportBandits) { |
20 | 30 | this.configurationStore = configurationStore;
|
21 | 31 | this.client = client;
|
22 | 32 | this.expectObfuscatedConfig = expectObfuscatedConfig;
|
| 33 | + this.supportBandits = supportBandits; |
23 | 34 | }
|
24 | 35 |
|
25 |
| - // TODO: async loading for android |
26 |
| - public void load() { |
27 |
| - // Grab hold of the last configuration in case its bandit models are useful |
28 |
| - Configuration lastConfig = configurationStore.getConfiguration(); |
| 36 | + // Synchronously set the initial configuration. |
| 37 | + public void setInitialConfiguration(@NotNull Configuration configuration) { |
| 38 | + if (initialConfigSet || this.configurationFuture != null) { |
| 39 | + throw new IllegalStateException("Initial configuration has already been set"); |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + configurationStore.saveConfiguration(configuration).join(); |
| 44 | + initialConfigSet = true; |
| 45 | + } catch (CompletionException e) { |
| 46 | + log.error("Error setting initial configuration", e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Asynchronously sets the initial configuration. |
| 51 | + public CompletableFuture<Void> setInitialConfiguration( |
| 52 | + @NotNull CompletableFuture<Configuration> configurationFuture) { |
| 53 | + if (initialConfigSet || this.configurationFuture != null) { |
| 54 | + throw new IllegalStateException("Configuration future has already been set"); |
| 55 | + } |
| 56 | + this.configurationFuture = |
| 57 | + configurationFuture.thenAccept( |
| 58 | + (config) -> { |
| 59 | + synchronized (configurationStore) { |
| 60 | + if (config == null) { |
| 61 | + log.debug("Initial configuration future returned null"); |
| 62 | + } else if (remoteFetchFuture != null |
| 63 | + && remoteFetchFuture.isDone() |
| 64 | + && !remoteFetchFuture.isCompletedExceptionally()) { |
| 65 | + // Don't clobber a successful fetch. |
| 66 | + log.debug("Fetch successfully beat the initial config; not clobbering"); |
| 67 | + } else { |
| 68 | + log.debug("saving initial configuration"); |
| 69 | + configurationStore.saveConfiguration(config); |
| 70 | + initialConfigSet = true; |
| 71 | + } |
| 72 | + } |
| 73 | + }); |
| 74 | + return this.configurationFuture; |
| 75 | + } |
29 | 76 |
|
| 77 | + /** Loads configuration synchronously from the API server. */ |
| 78 | + void fetchAndSaveFromRemote() { |
30 | 79 | log.debug("Fetching configuration");
|
31 |
| - byte[] flagConfigurationJsonBytes = requestBody("/api/flag-config/v1/config"); |
| 80 | + |
| 81 | + // Reuse the `lastConfig` as its bandits may be useful |
| 82 | + Configuration lastConfig = configurationStore.getConfiguration(); |
| 83 | + |
| 84 | + byte[] flagConfigurationJsonBytes = client.get(FLAG_CONFIG_PATH); |
32 | 85 | Configuration.Builder configBuilder =
|
33 |
| - new Configuration.Builder(flagConfigurationJsonBytes, expectObfuscatedConfig) |
| 86 | + Configuration.builder(flagConfigurationJsonBytes, expectObfuscatedConfig) |
34 | 87 | .banditParametersFromConfig(lastConfig);
|
35 | 88 |
|
36 |
| - if (configBuilder.requiresBanditModels()) { |
37 |
| - byte[] banditParametersJsonBytes = requestBody("/api/flag-config/v1/bandits"); |
| 89 | + if (supportBandits && configBuilder.requiresUpdatedBanditModels()) { |
| 90 | + byte[] banditParametersJsonBytes = client.get(BANDIT_PARAMETER_PATH); |
38 | 91 | configBuilder.banditParameters(banditParametersJsonBytes);
|
39 | 92 | }
|
40 | 93 |
|
41 |
| - configurationStore.setConfiguration(configBuilder.build()); |
| 94 | + configurationStore.saveConfiguration(configBuilder.build()); |
42 | 95 | }
|
43 | 96 |
|
44 |
| - private byte[] requestBody(String route) { |
45 |
| - Response response = client.get(route); |
46 |
| - if (!response.isSuccessful() || response.body() == null) { |
47 |
| - throw new RuntimeException("Failed to fetch from " + route); |
48 |
| - } |
49 |
| - try { |
50 |
| - return response.body().bytes(); |
51 |
| - } catch (IOException e) { |
52 |
| - throw new RuntimeException(e); |
| 97 | + /** Loads configuration asynchronously from the API server, off-thread. */ |
| 98 | + CompletableFuture<Void> fetchAndSaveFromRemoteAsync() { |
| 99 | + log.debug("Fetching configuration from API server"); |
| 100 | + final Configuration lastConfig = configurationStore.getConfiguration(); |
| 101 | + |
| 102 | + if (remoteFetchFuture != null && !remoteFetchFuture.isDone()) { |
| 103 | + log.debug("Remote fetch is active. Cancelling and restarting"); |
| 104 | + remoteFetchFuture.cancel(true); |
| 105 | + remoteFetchFuture = null; |
53 | 106 | }
|
| 107 | + |
| 108 | + remoteFetchFuture = |
| 109 | + client |
| 110 | + .getAsync(FLAG_CONFIG_PATH) |
| 111 | + .thenApply( |
| 112 | + flagConfigJsonBytes -> { |
| 113 | + synchronized (this) { |
| 114 | + Configuration.Builder configBuilder = |
| 115 | + Configuration.builder(flagConfigJsonBytes, expectObfuscatedConfig) |
| 116 | + .banditParametersFromConfig( |
| 117 | + lastConfig); // possibly reuse last bandit models loaded. |
| 118 | + |
| 119 | + if (supportBandits && configBuilder.requiresUpdatedBanditModels()) { |
| 120 | + byte[] banditParametersJsonBytes; |
| 121 | + try { |
| 122 | + banditParametersJsonBytes = client.getAsync(BANDIT_PARAMETER_PATH).get(); |
| 123 | + } catch (InterruptedException | ExecutionException e) { |
| 124 | + log.error("Error fetching from remote: " + e.getMessage()); |
| 125 | + throw new RuntimeException(e); |
| 126 | + } |
| 127 | + if (banditParametersJsonBytes != null) { |
| 128 | + configBuilder.banditParameters(banditParametersJsonBytes); |
| 129 | + } |
| 130 | + } |
| 131 | + return configBuilder.build(); |
| 132 | + } |
| 133 | + }) |
| 134 | + .thenApply( |
| 135 | + configuration -> { |
| 136 | + synchronized (configurationStore) { |
| 137 | + configurationStore.saveConfiguration(configuration); |
| 138 | + } |
| 139 | + return null; |
| 140 | + }); |
| 141 | + return remoteFetchFuture; |
54 | 142 | }
|
55 | 143 | }
|
0 commit comments