Skip to content

Commit

Permalink
Update SplitFactoryImpl for localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayorsplit committed Sep 1, 2023
1 parent ed595a1 commit 07ab95f
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.stream.JsonReader;
import io.split.client.dtos.SplitChange;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.Json;
import io.split.client.utils.LocalhostSanitizer;
Expand Down Expand Up @@ -30,13 +29,11 @@ public JsonLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider)
}

@Override
public SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException {
public SplitChange fetch(long since, FetchOptions options) {
try {
JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(_inputStreamProvider.get(), StandardCharsets.UTF_8)));
SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class);
return processSplitChange(splitChange, since);
} catch (InputStreamProviderException i) {
throw i;
} catch (Exception e) {
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
Expand Down
69 changes: 36 additions & 33 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.split.client;

import com.google.common.io.Files;
import io.split.client.dtos.Metadata;
import io.split.client.events.EventsSender;
import io.split.client.events.EventsStorage;
Expand Down Expand Up @@ -32,6 +33,7 @@
import io.split.client.interceptors.SdkMetadataInterceptorFilter;
import io.split.client.utils.FileInputStreamProvider;
import io.split.client.utils.FileTypeEnum;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.InputStreamProviderImp;
import io.split.client.utils.SDKMetadata;
import io.split.engine.SDKReadinessGates;
Expand Down Expand Up @@ -101,6 +103,7 @@
import org.slf4j.LoggerFactory;
import pluggable.CustomStorageWrapper;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
Expand Down Expand Up @@ -645,48 +648,48 @@ private SplitChangeFetcher createSplitChangeFetcher(SplitClientConfig splitClien
String splitFile = splitClientConfig.splitFile();
InputStream inputStream = splitClientConfig.inputStream();
FileTypeEnum fileType = splitClientConfig.fileType();
if (splitFile != null && inputStream != null) {
_log.warn("splitFile or inputStreamProvider should have a value, not both");
_log.warn(LEGACY_LOG_MESSAGE);
return new LegacyLocalhostSplitChangeFetcher(splitFile);
}
if (inputStream != null && fileType == null) {
_log.warn("If inputStreamProvider is not null, then fileType must also have a non-null value");
_log.warn(LEGACY_LOG_MESSAGE);
return new LegacyLocalhostSplitChangeFetcher(splitFile);
}
if (inputStream == null && splitFile == null){
_log.warn("splitFile or inputStreamProvider should have a value");
_log.warn(LEGACY_LOG_MESSAGE);
return new LegacyLocalhostSplitChangeFetcher(splitFile);
}
if (splitFile != null) {
try {
if (splitFile.toLowerCase().endsWith(".json")) {
return new JsonLocalhostSplitChangeFetcher(new FileInputStreamProvider(splitFile));
} else if (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml")) {
return new YamlLocalhostSplitChangeFetcher(new FileInputStreamProvider(splitFile));
}
} catch (Exception e) {
_log.warn(String.format("There was no file named %s found. " +
"We created a split client that returns default treatments for all feature flags for all of your users. " +
"If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " +
"treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " +
"considered comments",
splitFile, splitFile), e);
InputStreamProvider inputStreamProvider;
if (splitFile != null || !isInputStreamConfigValid(inputStream, fileType)) {
if (splitFile == null) {
_log.warn("The InputStream config is invalid");
}
fileType = getFileTypeFromFileName(splitFile);
inputStreamProvider = new FileInputStreamProvider(splitFile);
} else {
switch (fileType) {
inputStreamProvider = new InputStreamProviderImp(inputStream);
}
try {
switch (fileType){
case JSON:
return new JsonLocalhostSplitChangeFetcher(new InputStreamProviderImp(inputStream));
return new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
case YAML:
return new YamlLocalhostSplitChangeFetcher(new InputStreamProviderImp(inputStream));
return new YamlLocalhostSplitChangeFetcher(inputStreamProvider);
default:
_log.warn("The sdk initialize in localhost mode using Legacy file. The splitFile or inputStream doesn't add it to the config.");
_log.warn(LEGACY_LOG_MESSAGE);
return new LegacyLocalhostSplitChangeFetcher(splitFile);
}
} catch (Exception e) {
_log.warn(String.format("There was no file named %s found. " +
"We created a split client that returns default treatments for all feature flags for all of your users. " +
"If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " +
"treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " +
"considered comments",
splitFile, splitFile), e);
}
_log.warn(LEGACY_LOG_MESSAGE);
return new LegacyLocalhostSplitChangeFetcher(splitFile);
}

private Boolean isInputStreamConfigValid(InputStream inputStream, FileTypeEnum fileType) {
return inputStream != null && fileType != null;
}

private FileTypeEnum getFileTypeFromFileName(String fileName) {
try {
return FileTypeEnum.valueOf(Files.getFileExtension(fileName).toUpperCase());
} catch (Exception e) {
return FileTypeEnum.LEGACY;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public YamlLocalhostSplitChangeFetcher(InputStreamProvider inputStreamProvider)
}

@Override
public SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException {
public SplitChange fetch(long since, FetchOptions options) {
try {
Yaml yaml = new Yaml();
List<Map<String, Map<String, Object>>> yamlSplits = yaml.load(_inputStreamProvider.get());
Expand Down Expand Up @@ -75,8 +75,6 @@ public SplitChange fetch(long since, FetchOptions options) throws InputStreamPro
splitChange.till = since;
splitChange.since = since;
return splitChange;
} catch (InputStreamProviderException i) {
throw i;
} catch (Exception e) {
throw new IllegalStateException("Problem fetching splitChanges using a yaml file: " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.split.engine.experiments;

import io.split.client.dtos.SplitChange;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.engine.common.FetchOptions;

/**
Expand Down Expand Up @@ -33,5 +32,5 @@ public interface SplitChangeFetcher {
* @return SegmentChange
* @throws java.lang.RuntimeException if there was a problem computing split changes
*/
SplitChange fetch(long since, FetchOptions options) throws InputStreamProviderException;
SplitChange fetch(long since, FetchOptions options);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.split.engine.experiments;

import io.split.client.dtos.SplitChange;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.client.utils.FeatureFlagsToUpdate;
import io.split.storages.SplitCacheProducer;
import io.split.telemetry.domain.enums.LastSynchronizationRecordsEnum;
Expand Down Expand Up @@ -90,7 +89,7 @@ public void run() {
this.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(false).build());
}

private Set<String> runWithoutExceptionHandling(FetchOptions options) throws InterruptedException, InputStreamProviderException {
private Set<String> runWithoutExceptionHandling(FetchOptions options) throws InterruptedException {
SplitChange change = _splitChangeFetcher.fetch(_splitCacheProducer.getChangeNumber(), options);
Set<String> segments = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.split.client.dtos.Split;
import io.split.client.dtos.SplitChange;
import io.split.client.dtos.Status;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.client.utils.FileInputStreamProvider;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.InputStreamProviderImp;
Expand Down Expand Up @@ -34,7 +33,7 @@ public class JsonLocalhostSplitChangeFetcherTest {
private String TEST_4 = "{\"splits\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"since\":-1,\"till\":445345}";
private String TEST_5 = "{\"splits\":[{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_1\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]},{\"trafficTypeName\":\"user\",\"name\":\"SPLIT_2\",\"trafficAllocation\":100,\"trafficAllocationSeed\":-1780071202,\"seed\":-1442762199,\"status\":\"ACTIVE\",\"killed\":false,\"defaultTreatment\":\"off\",\"changeNumber\":1675443537882,\"algo\":2,\"configurations\":{},\"conditions\":[{\"conditionType\":\"ROLLOUT\",\"matcherGroup\":{\"combiner\":\"AND\",\"matchers\":[{\"keySelector\":{\"trafficType\":\"user\",\"attribute\":null},\"matcherType\":\"ALL_KEYS\",\"negate\":false,\"userDefinedSegmentMatcherData\":null,\"whitelistMatcherData\":null,\"unaryNumericMatcherData\":null,\"betweenMatcherData\":null,\"booleanMatcherData\":null,\"dependencyMatcherData\":null,\"stringMatcherData\":null}]},\"partitions\":[{\"treatment\":\"on\",\"size\":0},{\"treatment\":\"off\",\"size\":100}],\"label\":\"default rule\"}]}],\"since\":-1,\"till\":-1}";
@Test
public void testParseSplitChange() throws FileNotFoundException, InputStreamProviderException {
public void testParseSplitChange() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/split_init.json");
InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream);
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
Expand All @@ -49,7 +48,7 @@ public void testParseSplitChange() throws FileNotFoundException, InputStreamProv
}

@Test
public void testSinceAndTillSanitization() throws FileNotFoundException, InputStreamProviderException {
public void testSinceAndTillSanitization() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeTillSanitization.json");
InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream);
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
Expand All @@ -62,7 +61,7 @@ public void testSinceAndTillSanitization() throws FileNotFoundException, InputSt
}

@Test
public void testSplitChangeWithoutSplits() throws FileNotFoundException, InputStreamProviderException {
public void testSplitChangeWithoutSplits() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeWithoutSplits.json");
InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream);
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
Expand All @@ -74,7 +73,7 @@ public void testSplitChangeWithoutSplits() throws FileNotFoundException, InputSt
}

@Test
public void testSplitChangeSplitsToSanitize() throws FileNotFoundException, InputStreamProviderException {
public void testSplitChangeSplitsToSanitize() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangeSplitsToSanitize.json");
InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream);
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
Expand All @@ -91,7 +90,7 @@ public void testSplitChangeSplitsToSanitize() throws FileNotFoundException, Inpu
}

@Test
public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException, InputStreamProviderException {
public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundException {
InputStream inputStream = new FileInputStream("src/test/resources/sanitizer/splitChangerMatchersNull.json");
InputStreamProvider inputStreamProvider = new InputStreamProviderImp(inputStream);
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
Expand All @@ -108,7 +107,7 @@ public void testSplitChangeSplitsToSanitizeMatchersNull() throws FileNotFoundExc
}

@Test
public void testSplitChangeSplitsDifferentScenarios() throws IOException, InputStreamProviderException {
public void testSplitChangeSplitsDifferentScenarios() throws IOException {

File file = folder.newFile("test_0.json");

Expand Down Expand Up @@ -171,8 +170,8 @@ public void testSplitChangeSplitsDifferentScenarios() throws IOException, InputS
Assert.assertEquals(2323, splitChange.since);
}

@Test(expected = InputStreamProviderException.class)
public void processTestForException() throws InputStreamProviderException {
@Test(expected = IllegalStateException.class)
public void processTestForException() {
InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.json");
JsonLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new JsonLocalhostSplitChangeFetcher(inputStreamProvider);
FetchOptions fetchOptions = Mockito.mock(FetchOptions.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.split.client.dtos.Split;
import io.split.client.dtos.SplitChange;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.client.utils.FileInputStreamProvider;
import io.split.client.utils.InputStreamProvider;
import io.split.client.utils.LocalhostUtils;
Expand All @@ -27,7 +26,7 @@ public class YamlLocalhostSplitChangeFetcherTest {
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void testParseSplitChange() throws IOException, InputStreamProviderException {
public void testParseSplitChange() throws IOException {
File file = folder.newFile(SplitClientConfig.LOCALHOST_DEFAULT_FILE);

List<Map<String, Object>> allSplits = new ArrayList();
Expand Down Expand Up @@ -76,8 +75,8 @@ public void testParseSplitChange() throws IOException, InputStreamProviderExcept
}
}

@Test(expected = InputStreamProviderException.class)
public void processTestForException() throws InputStreamProviderException {
@Test(expected = IllegalStateException.class)
public void processTestForException() {
InputStreamProvider inputStreamProvider = new FileInputStreamProvider("src/test/resources/notExist.yaml");
YamlLocalhostSplitChangeFetcher localhostSplitChangeFetcher = new YamlLocalhostSplitChangeFetcher(inputStreamProvider);
FetchOptions fetchOptions = Mockito.mock(FetchOptions.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.split.client.LocalhostSegmentChangeFetcher;
import io.split.client.JsonLocalhostSplitChangeFetcher;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.client.utils.FileInputStreamProvider;
import io.split.client.utils.InputStreamProvider;
import io.split.engine.experiments.SplitChangeFetcher;
Expand Down Expand Up @@ -51,7 +50,7 @@ public void testSyncAll(){
}

@Test
public void testPeriodicFetching() throws InterruptedException, InputStreamProviderException {
public void testPeriodicFetching() throws InterruptedException {
SplitCache splitCacheProducer = new InMemoryCacheImp();

SplitChangeFetcher splitChangeFetcher = Mockito.mock(JsonLocalhostSplitChangeFetcher.class);
Expand All @@ -78,7 +77,7 @@ public void testPeriodicFetching() throws InterruptedException, InputStreamProvi
}

@Test
public void testRefreshSplits() throws InputStreamProviderException {
public void testRefreshSplits() {
SplitCacheProducer splitCacheProducer = new InMemoryCacheImp();
SplitChangeFetcher splitChangeFetcher = Mockito.mock(SplitChangeFetcher.class);
SplitParser splitParser = new SplitParser();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.split.engine.experiments;

import com.google.common.collect.Lists;
import io.split.client.exceptions.InputStreamProviderException;
import io.split.storages.memory.InMemoryCacheImp;
import io.split.storages.SegmentCache;
import io.split.storages.memory.SegmentCacheInMemoryImpl;
Expand Down Expand Up @@ -88,7 +87,7 @@ private void works(long startingChangeNumber) throws InterruptedException {
}

@Test
public void when_parser_fails_we_remove_the_experiment() throws InterruptedException, InputStreamProviderException {
public void when_parser_fails_we_remove_the_experiment() throws InterruptedException {
Split validSplit = new Split();
validSplit.status = Status.ACTIVE;
validSplit.seed = (int) -1;
Expand Down Expand Up @@ -214,7 +213,7 @@ public void works_with_user_defined_segments() throws Exception {
}

@Test
public void testBypassCdnClearedAfterFirstHit() throws InputStreamProviderException {
public void testBypassCdnClearedAfterFirstHit() {
SplitChangeFetcher mockFetcher = Mockito.mock(SplitChangeFetcher.class);
SplitParser mockParser = new SplitParser();
SplitCache mockCache = new InMemoryCacheImp();
Expand Down
Loading

0 comments on commit 07ab95f

Please sign in to comment.