Skip to content

Commit

Permalink
Rollout config
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 3, 2024
1 parent 015180d commit f4ea326
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 53 deletions.
10 changes: 9 additions & 1 deletion src/androidTest/java/helper/TestableSplitConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Constructor;

import io.split.android.client.RolloutCacheConfiguration;
import io.split.android.client.ServiceEndpoints;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SyncConfig;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class TestableSplitConfigBuilder {
private String mPrefix = "";
private CertificatePinningConfiguration mCertificatePinningConfiguration;
private long mImpressionsDedupeTimeInterval = ServiceConstants.DEFAULT_IMPRESSIONS_DEDUPE_TIME_INTERVAL;
private RolloutCacheConfiguration mRolloutCacheConfiguration = RolloutCacheConfiguration.builder().build();

public TestableSplitConfigBuilder() {
mServiceEndpoints = ServiceEndpoints.builder().build();
Expand Down Expand Up @@ -274,6 +276,11 @@ public TestableSplitConfigBuilder impressionsDedupeTimeInterval(long impressions
return this;
}

public TestableSplitConfigBuilder rolloutCacheConfiguration(RolloutCacheConfiguration rolloutCacheConfiguration) {
this.mRolloutCacheConfiguration = rolloutCacheConfiguration;
return this;
}

public SplitClientConfig build() {
Constructor constructor = SplitClientConfig.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
Expand Down Expand Up @@ -329,7 +336,8 @@ public SplitClientConfig build() {
mPrefix,
mObserverCacheExpirationPeriod,
mCertificatePinningConfiguration,
mImpressionsDedupeTimeInterval);
mImpressionsDedupeTimeInterval,
mRolloutCacheConfiguration);

Logger.instance().setLevel(mLogLevel);
return config;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.split.android.client;

import io.split.android.client.service.ServiceConstants;
import io.split.android.client.utils.logger.Logger;

public class RolloutCacheConfiguration {

private final int mExpiration;
private final boolean mClearOnInit;

private RolloutCacheConfiguration(int expiration, boolean clearOnInit) {
mExpiration = expiration;
mClearOnInit = clearOnInit;
}

public int getExpiration() {
return mExpiration;
}

public boolean clearOnInit() {
return mClearOnInit;
}

public static Builder builder() {
return new Builder();
}

public static class Builder {

private static final int MIN_EXPIRATION_DAYS = 1;

private int mExpiration = ServiceConstants.DEFAULT_ROLLOUT_CACHE_EXPIRATION;
private boolean mClearOnInit = false;

private Builder() {

}

/**
* Set the expiration time for the rollout definitions cache, in days. Default is 10 days.
* @param expiration in days
* @return This builder
*/
public Builder expiration(int expiration) {
if (expiration < MIN_EXPIRATION_DAYS) {
Logger.w("Cache expiration must be at least 1 day. Using default value.");
mExpiration = ServiceConstants.DEFAULT_ROLLOUT_CACHE_EXPIRATION;
} else {
mExpiration = expiration;
}

return this;
}

/**
* Set if the rollout definitions cache should be cleared on initialization. Default is false.
* @param clearOnInit whether to clear cache on initialization.
* @return This builder
*/
public Builder clearOnInit(boolean clearOnInit) {
mClearOnInit = clearOnInit;
return this;
}

public RolloutCacheConfiguration build() {
return new RolloutCacheConfiguration(mExpiration, mClearOnInit);
}
}
}
31 changes: 27 additions & 4 deletions src/main/java/io/split/android/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public class SplitClientConfig {
private final long mObserverCacheExpirationPeriod;
private final CertificatePinningConfiguration mCertificatePinningConfiguration;
private final long mImpressionsDedupeTimeInterval;
@NonNull
private final RolloutCacheConfiguration mRolloutCacheConfiguration;

public static Builder builder() {
return new Builder();
Expand Down Expand Up @@ -185,7 +187,8 @@ private SplitClientConfig(String endpoint,
String prefix,
long observerCacheExpirationPeriod,
CertificatePinningConfiguration certificatePinningConfiguration,
long impressionsDedupeTimeInterval) {
long impressionsDedupeTimeInterval,
RolloutCacheConfiguration rolloutCacheConfiguration) {
mEndpoint = endpoint;
mEventsEndpoint = eventsEndpoint;
mTelemetryEndpoint = telemetryEndpoint;
Expand Down Expand Up @@ -243,6 +246,7 @@ private SplitClientConfig(String endpoint,
mObserverCacheExpirationPeriod = observerCacheExpirationPeriod;
mCertificatePinningConfiguration = certificatePinningConfiguration;
mImpressionsDedupeTimeInterval = impressionsDedupeTimeInterval;
mRolloutCacheConfiguration = rolloutCacheConfiguration;
}

public String trafficType() {
Expand Down Expand Up @@ -486,8 +490,8 @@ public long impressionsDedupeTimeInterval() {
return mImpressionsDedupeTimeInterval;
}

public boolean clearOnInit() {
return false; // TODO: to be implemented in the future
public RolloutCacheConfiguration rolloutCacheConfiguration() {
return mRolloutCacheConfiguration;
}

public static final class Builder {
Expand Down Expand Up @@ -566,6 +570,8 @@ public static final class Builder {

private long mImpressionsDedupeTimeInterval = ServiceConstants.DEFAULT_IMPRESSIONS_DEDUPE_TIME_INTERVAL;

private RolloutCacheConfiguration mRolloutCacheConfiguration = RolloutCacheConfiguration.builder().build();

public Builder() {
mServiceEndpoints = ServiceEndpoints.builder().build();
}
Expand Down Expand Up @@ -1106,6 +1112,22 @@ public Builder impressionsDedupeTimeInterval(long impressionsDedupeTimeInterval)
return this;
}

/**
* Configuration for rollout definitions cache.
*
* @param rolloutCacheConfiguration Configuration object
* @return This builder
*/
public Builder rolloutCacheConfiguration(@NonNull RolloutCacheConfiguration rolloutCacheConfiguration) {
if (rolloutCacheConfiguration == null) {
Logger.w("Rollout cache configuration is null. Setting to default value.");
mRolloutCacheConfiguration = RolloutCacheConfiguration.builder().build();
} else {
mRolloutCacheConfiguration = rolloutCacheConfiguration;
}
return this;
}

public SplitClientConfig build() {
Logger.instance().setLevel(mLogLevel);

Expand Down Expand Up @@ -1237,7 +1259,8 @@ public SplitClientConfig build() {
mPrefix,
mObserverCacheExpirationPeriod,
mCertificatePinningConfiguration,
mImpressionsDedupeTimeInterval);
mImpressionsDedupeTimeInterval,
mRolloutCacheConfiguration);
}

private HttpProxy parseProxyHost(String proxyUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ServiceConstants {
public static final long MIN_INITIAL_DELAY = 5L;
public static final int DEFAULT_RECORDS_PER_PUSH = 100;
public static final long DEFAULT_SPLITS_CACHE_EXPIRATION_IN_SECONDS = TimeUnit.DAYS.toSeconds(10); // 10 days
public static final int DEFAULT_ROLLOUT_CACHE_EXPIRATION = 10; // 10 days

public static final int MAX_ROWS_PER_QUERY = 100;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.concurrent.TimeUnit;

import io.split.android.client.RolloutCacheConfiguration;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.service.CleanUpDatabaseTask;
import io.split.android.client.service.executor.SplitTask;
Expand All @@ -27,19 +28,20 @@ public class RolloutCacheManagerImpl implements RolloutCacheManager, SplitTask {
@NonNull
private final GeneralInfoStorage mGeneralInfoStorage;
@NonNull
private final RolloutCacheManagerConfig mConfig;
private final RolloutCacheConfiguration mConfig;
@NonNull
private final RolloutDefinitionsCache[] mStorages;
@NonNull
private final CleanUpDatabaseTask mCleanUpDatabaseTask;
@NonNull
private final EncryptionMigrationTask mEncryptionMigrationTask;

public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @NonNull SplitStorageContainer storageContainer,
public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig,
@NonNull SplitStorageContainer storageContainer,
@NonNull CleanUpDatabaseTask cleanUpDatabaseTask,
@NonNull EncryptionMigrationTask encryptionMigrationTask) {
this(storageContainer.getGeneralInfoStorage(),
RolloutCacheManagerConfig.from(splitClientConfig),
splitClientConfig.rolloutCacheConfiguration(),
cleanUpDatabaseTask,
encryptionMigrationTask,
storageContainer.getSplitsStorage(),
Expand All @@ -49,7 +51,7 @@ public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @No

@VisibleForTesting
RolloutCacheManagerImpl(@NonNull GeneralInfoStorage generalInfoStorage,
@NonNull RolloutCacheManagerConfig config,
@NonNull RolloutCacheConfiguration config,
@NonNull CleanUpDatabaseTask clean,
@NonNull EncryptionMigrationTask encryptionMigrationTask,
@NonNull RolloutDefinitionsCache... storages) {
Expand Down Expand Up @@ -99,10 +101,10 @@ private boolean validateExpiration() {
long lastUpdateTimestamp = mGeneralInfoStorage.getSplitsUpdateTimestamp();
long daysSinceLastUpdate = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - lastUpdateTimestamp);

if (daysSinceLastUpdate > mConfig.getCacheExpirationInDays()) {
if (daysSinceLastUpdate > mConfig.getExpiration()) {
Logger.v("Clearing rollout definitions cache due to expiration");
return true;
} else if (mConfig.isClearOnInit()) {
} else if (mConfig.clearOnInit()) {
long lastCacheClearTimestamp = mGeneralInfoStorage.getRolloutCacheLastClearTimestamp();
long daysSinceCacheClear = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - lastCacheClearTimestamp);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.split.android.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class RolloutCacheConfigurationTest {

@Test
public void defaultValues() {
RolloutCacheConfiguration config = RolloutCacheConfiguration.builder().build();
assertEquals(10, config.getExpiration());
assertFalse(config.clearOnInit());
}

@Test
public void expirationIsCorrectlySet() {
RolloutCacheConfiguration.Builder builder = RolloutCacheConfiguration.builder();
builder.expiration(1);
RolloutCacheConfiguration config = builder.build();
assertEquals(1, config.getExpiration());
}

@Test
public void clearOnInitIsCorrectlySet() {
RolloutCacheConfiguration.Builder builder = RolloutCacheConfiguration.builder();
builder.clearOnInit(true);
RolloutCacheConfiguration config = builder.build();
assertTrue(config.clearOnInit());
}

@Test
public void negativeExpirationIsSetToDefault() {
RolloutCacheConfiguration.Builder builder = RolloutCacheConfiguration.builder();
builder.expiration(-1);
RolloutCacheConfiguration config = builder.build();
assertEquals(10, config.getExpiration());
}
}
32 changes: 32 additions & 0 deletions src/test/java/io/split/android/client/SplitClientConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static junit.framework.Assert.assertFalse;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -224,6 +225,37 @@ public void observerCacheExpirationPeriodMatchesDedupeTimeIntervalWhenDedupeTime
assertEquals(TimeUnit.HOURS.toMillis(4), config3.observerCacheExpirationPeriod());
}

@Test
public void rolloutCacheConfigurationDefaults() {
RolloutCacheConfiguration config = SplitClientConfig.builder().build().rolloutCacheConfiguration();

assertEquals(10, config.getExpiration());
assertFalse(config.clearOnInit());
}

@Test
public void rolloutCacheConfigurationExpirationIsCorrectlySet() {
RolloutCacheConfiguration config = SplitClientConfig.builder()
.rolloutCacheConfiguration(RolloutCacheConfiguration.builder().expiration(1).clearOnInit(true).build())
.build().rolloutCacheConfiguration();

assertEquals(1, config.getExpiration());
assertTrue(config.clearOnInit());
}

@Test
public void nullRolloutCacheConfigurationSetsDefault() {
Queue<String> logMessages = getLogMessagesQueue();
RolloutCacheConfiguration config = SplitClientConfig.builder()
.logLevel(SplitLogLevel.WARNING)
.rolloutCacheConfiguration(null)
.build().rolloutCacheConfiguration();

assertEquals(10, config.getExpiration());
assertFalse(config.clearOnInit());
assertEquals(1, logMessages.size());
}

@NonNull
private static Queue<String> getLogMessagesQueue() {
Queue<String> logMessages = new LinkedList<>();
Expand Down
Loading

0 comments on commit f4ea326

Please sign in to comment.