Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 5, 2024
1 parent 48a311f commit 6234fa7
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void clearOnInitClearsCacheOnStartup() throws InterruptedException {

@Test
public void repeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElapsed() throws InterruptedException {
// Preload DB with update timestamp of 1 day ago
// Preload DB with update timestamp of now
long oldTimestamp = System.currentTimeMillis();
preloadDb(oldTimestamp, 0L, 8000L);

Expand All @@ -102,6 +102,7 @@ public void repeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElaps
factory.client().on(SplitEvent.SDK_READY, TestingHelper.testTask(readyLatch));
boolean readyAwait = readyLatch.await(10, TimeUnit.SECONDS);

// Destroy factory
factory.destroy();
mRequestCountdownLatch = new CountDownLatch(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ public void scheduleWorkSchedulesSplitsJob() {
mWrapper.scheduleWork();

Data inputData = new Data.Builder()
.putLong("splitCacheExpiration", 864000)
.putString("endpoint", "https://test.split.io/api")
.putBoolean("shouldRecordTelemetry", true)
.putStringArray("configuredFilterValues", new String[]{"set_1", "set_2"})
Expand Down Expand Up @@ -252,7 +251,6 @@ public void schedulingWithoutCertificatePinning() {
mWrapper.scheduleWork();

Data inputData = new Data.Builder()
.putLong("splitCacheExpiration", 864000)
.putString("endpoint", "https://test.split.io/api")
.putBoolean("shouldRecordTelemetry", true)
.putStringArray("configuredFilterValues", new String[]{"set_1", "set_2"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class ServiceConstants {
public static final long MIN_INITIAL_DELAY = 5L;
public static final int DEFAULT_RECORDS_PER_PUSH = 100;
public static final int DEFAULT_ROLLOUT_CACHE_EXPIRATION = 10; // 10 days
public static final long DEFAULT_SPLITS_CACHE_EXPIRATION_IN_SECONDS = TimeUnit.DAYS.toSeconds(DEFAULT_ROLLOUT_CACHE_EXPIRATION); // 10 days

public static final int MAX_ROWS_PER_QUERY = 100;

Expand All @@ -28,7 +27,6 @@ public class ServiceConstants {
public final static String WORKER_PARAM_ENDPOINT = "endpoint";
public final static String WORKER_PARAM_IMPRESSIONS_PER_PUSH = "impressionsPerPush";
public final static String WORKER_PARAM_EVENTS_PER_PUSH = "eventsPerPush";
public final static String WORKER_PARAM_SPLIT_CACHE_EXPIRATION = "splitCacheExpiration";
public static final String WORKER_PARAM_UNIQUE_KEYS_PER_PUSH = "unique_keys_per_push";
public static final String WORKER_PARAM_UNIQUE_KEYS_ESTIMATED_SIZE_IN_BYTES = "unique_keys_estimated_size_in_bytes";
public static final String WORKER_PARAM_ENCRYPTION_ENABLED = "encryptionEnabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public ImpressionsRecorderTask createImpressionsRecorderTask() {

@Override
public SplitsSyncTask createSplitsSyncTask(boolean checkCacheExpiration) {
return SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorageContainer.getSplitsStorage(), checkCacheExpiration,
mSplitClientConfig.cacheExpirationInSeconds(), mSplitsFilterQueryStringFromConfig, mEventsManager, mSplitsStorageContainer.getTelemetryStorage());
return SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorageContainer.getSplitsStorage(),
mSplitsFilterQueryStringFromConfig, mEventsManager, mSplitsStorageContainer.getTelemetryStorage());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,8 @@ private void updateStorage(boolean clearBeforeUpdate, SplitChange splitChange) {
mSplitsStorage.update(mSplitChangeProcessor.process(splitChange));
}

public boolean cacheHasExpired(long storedChangeNumber, long updateTimestamp, long cacheExpirationInSeconds) {
long elapsed = now() - TimeUnit.MILLISECONDS.toSeconds(updateTimestamp);
return storedChangeNumber > -1
&& updateTimestamp > 0
&& (elapsed > cacheExpirationInSeconds);
public boolean cacheHasExpired() {
return false;
}

private long now() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class SplitsSyncTask implements SplitTask {
private final String mSplitsFilterQueryStringFromConfig;

private final SplitsStorage mSplitsStorage;
private final boolean mCheckCacheExpiration;
private final long mCacheExpirationInSeconds;
private final SplitsSyncHelper mSplitsSyncHelper;
@Nullable
private final ISplitEventsManager mEventsManager; // Should only be null on background sync
Expand All @@ -32,36 +30,28 @@ public class SplitsSyncTask implements SplitTask {

public static SplitsSyncTask build(@NonNull SplitsSyncHelper splitsSyncHelper,
@NonNull SplitsStorage splitsStorage,
boolean checkCacheExpiration,
long cacheExpirationInSeconds,
String splitsFilterQueryString,
@NonNull ISplitEventsManager eventsManager,
@NonNull TelemetryRuntimeProducer telemetryRuntimeProducer) {
return new SplitsSyncTask(splitsSyncHelper, splitsStorage, checkCacheExpiration, cacheExpirationInSeconds, splitsFilterQueryString, telemetryRuntimeProducer, eventsManager, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES);
return new SplitsSyncTask(splitsSyncHelper, splitsStorage, splitsFilterQueryString, telemetryRuntimeProducer, eventsManager, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES);
}

public static SplitTask buildForBackground(@NonNull SplitsSyncHelper splitsSyncHelper,
@NonNull SplitsStorage splitsStorage,
boolean checkCacheExpiration,
long cacheExpirationInSeconds,
String splitsFilterQueryString,
String splitsFilterQueryString,
@NonNull TelemetryRuntimeProducer telemetryRuntimeProducer) {
return new SplitsSyncTask(splitsSyncHelper, splitsStorage, checkCacheExpiration, cacheExpirationInSeconds, splitsFilterQueryString, telemetryRuntimeProducer, null, 1);
return new SplitsSyncTask(splitsSyncHelper, splitsStorage, splitsFilterQueryString, telemetryRuntimeProducer, null, 1);
}

private SplitsSyncTask(@NonNull SplitsSyncHelper splitsSyncHelper,
@NonNull SplitsStorage splitsStorage,
boolean checkCacheExpiration,
long cacheExpirationInSeconds,
String splitsFilterQueryString,
@NonNull TelemetryRuntimeProducer telemetryRuntimeProducer,
@Nullable ISplitEventsManager eventsManager,
int onDemandFetchBackoffMaxRetries) {

mSplitsStorage = checkNotNull(splitsStorage);
mSplitsSyncHelper = checkNotNull(splitsSyncHelper);
mCacheExpirationInSeconds = cacheExpirationInSeconds;
mCheckCacheExpiration = checkCacheExpiration;
mSplitsFilterQueryStringFromConfig = splitsFilterQueryString;
mEventsManager = eventsManager;
mChangeChecker = new SplitsChangeChecker();
Expand All @@ -73,10 +63,6 @@ private SplitsSyncTask(@NonNull SplitsSyncHelper splitsSyncHelper,
@NonNull
public SplitTaskExecutionInfo execute() {
long storedChangeNumber = mSplitsStorage.getTill();
long updateTimestamp = mSplitsStorage.getUpdateTimestamp();

boolean shouldClearExpiredCache = mCheckCacheExpiration &&
mSplitsSyncHelper.cacheHasExpired(storedChangeNumber, updateTimestamp, mCacheExpirationInSeconds);

boolean splitsFilterHasChanged = splitsFilterHasChanged(mSplitsStorage.getSplitsFilterQueryString());

Expand All @@ -87,8 +73,8 @@ public SplitTaskExecutionInfo execute() {

long startTime = System.currentTimeMillis();
SplitTaskExecutionInfo result = mSplitsSyncHelper.sync(storedChangeNumber,
splitsFilterHasChanged || shouldClearExpiredCache,
splitsFilterHasChanged || shouldClearExpiredCache, mOnDemandFetchBackoffMaxRetries);
splitsFilterHasChanged,
splitsFilterHasChanged, mOnDemandFetchBackoffMaxRetries);
mTelemetryRuntimeProducer.recordSyncLatency(OperationType.SPLITS, System.currentTimeMillis() - startTime);

if (result.getStatus() == SplitTaskExecutionStatus.SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ private SplitTaskType taskTypeFromTags(Set<String> tags) {

private Data buildSplitSyncInputData() {
Data.Builder dataBuilder = new Data.Builder();
dataBuilder.putLong(ServiceConstants.WORKER_PARAM_SPLIT_CACHE_EXPIRATION, mSplitClientConfig.cacheExpirationInSeconds());
dataBuilder.putString(ServiceConstants.WORKER_PARAM_ENDPOINT, mSplitClientConfig.endpoint());
dataBuilder.putBoolean(ServiceConstants.SHOULD_RECORD_TELEMETRY, mSplitClientConfig.shouldRecordTelemetry());
dataBuilder.putString(ServiceConstants.WORKER_PARAM_CONFIGURED_FILTER_TYPE, (mFilter != null) ? mFilter.getType().queryStringField() : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public abstract class SplitWorker extends Worker {
private final SplitRoomDatabase mDatabase;
private final HttpClient mHttpClient;
private final String mEndpoint;
private final long mCacheExpirationInSeconds;

protected SplitTask mSplitTask;

Expand All @@ -36,8 +35,6 @@ public SplitWorker(@NonNull Context context,
String apiKey = inputData.getString(ServiceConstants.WORKER_PARAM_API_KEY);
mEndpoint = inputData.getString(ServiceConstants.WORKER_PARAM_ENDPOINT);
mDatabase = SplitRoomDatabase.getDatabase(context, databaseName);
mCacheExpirationInSeconds = inputData.getLong(ServiceConstants.WORKER_PARAM_SPLIT_CACHE_EXPIRATION,
ServiceConstants.DEFAULT_SPLITS_CACHE_EXPIRATION_IN_SECONDS);
mHttpClient = buildHttpClient(apiKey, buildCertPinningConfig(inputData.getString(ServiceConstants.WORKER_PARAM_CERTIFICATE_PINS)));
}

Expand All @@ -64,10 +61,6 @@ public String getEndPoint() {
return mEndpoint;
}

public long getCacheExpirationInSeconds() {
return mCacheExpirationInSeconds;
}

private static HttpClient buildHttpClient(String apiKey, @Nullable CertificatePinningConfiguration certificatePinningConfiguration) {
HttpClientImpl.Builder builder = new HttpClientImpl.Builder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public SplitsSyncWorker(@NonNull Context context,
new FetcherProvider(getHttpClient(), getEndPoint()),
new SplitChangeProcessorProvider().provideSplitChangeProcessor(params.configuredFilterType(), params.configuredFilterValues()),
new SyncHelperProvider(),
getCacheExpirationInSeconds(),
params.flagsSpec());

mSplitTask = builder.getTask();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
class SplitsSyncWorkerTaskBuilder {

private final long mCacheExpirationInSeconds;
private final StorageProvider mStorageProvider;
private final FetcherProvider mFetcherProvider;
private final SplitChangeProcessor mSplitChangeProcessor;
Expand All @@ -26,13 +25,11 @@ class SplitsSyncWorkerTaskBuilder {
FetcherProvider fetcherProvider,
SplitChangeProcessor splitChangeProcessor,
SyncHelperProvider splitsSyncHelperProvider,
long cacheExpirationInSeconds,
String flagsSpec) {
mStorageProvider = storageProvider;
mFetcherProvider = fetcherProvider;
mSplitsSyncHelperProvider = splitsSyncHelperProvider;
mSplitChangeProcessor = splitChangeProcessor;
mCacheExpirationInSeconds = cacheExpirationInSeconds;
mFlagsSpec = flagsSpec;
}

Expand All @@ -51,8 +48,6 @@ SplitTask getTask() {

return SplitsSyncTask.buildForBackground(splitsSyncHelper,
splitsStorage,
false,
mCacheExpirationInSeconds,
splitsFilterQueryString,
telemetryStorage);
} catch (URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void correctExecution() throws HttpFetcherException {
// And updateTimestamp is 0
// Retry is off, so splitSyncHelper.sync should be called
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(-1L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
Expand All @@ -77,38 +77,6 @@ public void correctExecution() throws HttpFetcherException {
verify(mSplitsSyncHelper, times(1)).sync(-1, false, false, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES);
}

@Test
public void cleanOldCacheDisabled() throws HttpFetcherException {
// Cache should not be cleared when cache expired
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
false, 100L, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(300L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(100L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
// This value function checks that cache is expired, here we simulate cache expired
when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(true);

mTask.execute();

verify(mSplitsStorage, never()).clear();
}

@Test
public void cleanOldCacheEnabled() throws HttpFetcherException {

// Cache should be cleared when cache expired
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
true, 100L, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(100L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(100L); // Dummy value clearing depends on cacheHasExpired function value
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(true);

mTask.execute();

verify(mSplitsSyncHelper, times(1)).sync(100L, true, true, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES);
}

@Test
public void cleanSplitsWhenQueryStringHasChanged() throws HttpFetcherException {
// Splits have to be cleared when query string on db is != than current one on current sdk client instance
Expand All @@ -119,11 +87,11 @@ public void cleanSplitsWhenQueryStringHasChanged() throws HttpFetcherException {
Map<String, Object> params = new HashMap<>();
params.put("since", 100L);
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
true, 100L, otherQs, mEventsManager, mTelemetryRuntimeProducer);
otherQs, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(100L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(1111L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(false);
when(mSplitsSyncHelper.cacheHasExpired()).thenReturn(false);

mTask.execute();

Expand All @@ -137,11 +105,11 @@ public void noClearSplitsWhenQueryStringHasNotChanged() throws HttpFetcherExcept
// Setting up cache not expired

mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
true,100L, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(100L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(1111L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(false);
when(mSplitsSyncHelper.cacheHasExpired()).thenReturn(false);

mTask.execute();

Expand All @@ -156,7 +124,7 @@ public void splitUpdatedNotified() throws HttpFetcherException {
// And updateTimestamp is 0
// Retry is off, so splitSyncHelper.sync should be called
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(-1L).thenReturn(100L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
Expand All @@ -174,7 +142,7 @@ public void splitFetchdNotified() throws HttpFetcherException {
// And updateTimestamp is 0
// Retry is off, so splitSyncHelper.sync should be called
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(100L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
Expand All @@ -188,7 +156,7 @@ public void splitFetchdNotified() throws HttpFetcherException {
@Test
public void syncIsTrackedInTelemetry() {
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(100L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
Expand All @@ -202,7 +170,7 @@ public void syncIsTrackedInTelemetry() {
@Test
public void recordSuccessInTelemetry() {
mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage,
false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer);
mQueryString, mEventsManager, mTelemetryRuntimeProducer);
when(mSplitsStorage.getTill()).thenReturn(-1L);
when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L);
when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString);
Expand Down
Loading

0 comments on commit 6234fa7

Please sign in to comment.