From 428bb5b4a4145d19f74ff7ed2a9f71168e7952df Mon Sep 17 00:00:00 2001 From: Gaston Thea Date: Mon, 2 Dec 2024 13:07:15 -0300 Subject: [PATCH] WIP --- .../android/client/SplitFactoryImpl.java | 11 ++++--- .../service/executor/SplitTaskFactory.java | 5 +++ .../executor/SplitTaskFactoryImpl.java | 8 +++++ .../synchronizer/RolloutCacheManagerImpl.java | 31 ++++++++++++++++--- .../synchronizer/RolloutCacheManagerTest.kt | 28 ++++++++++++++--- 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/split/android/client/SplitFactoryImpl.java b/src/main/java/io/split/android/client/SplitFactoryImpl.java index 21524d2c2..b401b36a0 100644 --- a/src/main/java/io/split/android/client/SplitFactoryImpl.java +++ b/src/main/java/io/split/android/client/SplitFactoryImpl.java @@ -50,7 +50,6 @@ import io.split.android.client.shared.SplitClientContainer; import io.split.android.client.shared.SplitClientContainerImpl; import io.split.android.client.shared.UserConsent; -import io.split.android.client.storage.cipher.EncryptionMigrationTask; import io.split.android.client.storage.cipher.SplitCipher; import io.split.android.client.storage.common.SplitStorageContainer; import io.split.android.client.storage.db.SplitRoomDatabase; @@ -186,7 +185,6 @@ private SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull Sp config, splitApiFacade, mStorageContainer, splitsFilterQueryStringFromConfig, getFlagsSpec(testingConfig), mEventsManagerCoordinator, filters, flagSetsFilter, testingConfig); - cleanUpDabase(splitTaskExecutor, splitTaskFactory); WorkManagerWrapper workManagerWrapper = factoryHelper.buildWorkManagerWrapper(context, config, apiToken, databaseName, filters); SplitSingleThreadTaskExecutor splitSingleThreadTaskExecutor = new SplitSingleThreadTaskExecutor(); splitSingleThreadTaskExecutor.pause(); @@ -326,14 +324,19 @@ public void run() { Runnable initializer = new Runnable() { @Override public void run() { - Logger.v("Running initialization thread"); - RolloutCacheManagerImpl rolloutCacheManager = new RolloutCacheManagerImpl(config, mStorageContainer, new EncryptionMigrationTask(apiToken, splitDatabase, config.encryptionEnabled(), splitCipher)); + RolloutCacheManagerImpl rolloutCacheManager = new RolloutCacheManagerImpl(config, + mStorageContainer, + splitTaskFactory.createCleanUpDatabaseTask(System.currentTimeMillis() / 1000), + splitTaskFactory.createEncryptionMigrationTask(apiToken, splitDatabase, config.encryptionEnabled(), splitCipher)); + rolloutCacheManager.validateCache(new SplitTaskExecutionListener() { @Override public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) { mEventsManagerCoordinator.notifyInternalEvent(SplitInternalEvent.ENCRYPTION_MIGRATION_DONE); + splitTaskExecutor.resume(); splitSingleThreadTaskExecutor.resume(); + mSyncManager.start(); mLifecycleManager.register(mSyncManager); diff --git a/src/main/java/io/split/android/client/service/executor/SplitTaskFactory.java b/src/main/java/io/split/android/client/service/executor/SplitTaskFactory.java index e23407f55..989557cd9 100644 --- a/src/main/java/io/split/android/client/service/executor/SplitTaskFactory.java +++ b/src/main/java/io/split/android/client/service/executor/SplitTaskFactory.java @@ -11,6 +11,9 @@ import io.split.android.client.service.splits.SplitsSyncTask; import io.split.android.client.service.splits.SplitsUpdateTask; import io.split.android.client.service.telemetry.TelemetryTaskFactory; +import io.split.android.client.storage.cipher.EncryptionMigrationTask; +import io.split.android.client.storage.cipher.SplitCipher; +import io.split.android.client.storage.db.SplitRoomDatabase; public interface SplitTaskFactory extends TelemetryTaskFactory, ImpressionsTaskFactory { @@ -29,4 +32,6 @@ public interface SplitTaskFactory extends TelemetryTaskFactory, ImpressionsTaskF FilterSplitsInCacheTask createFilterSplitsInCacheTask(); CleanUpDatabaseTask createCleanUpDatabaseTask(long maxTimestamp); + + EncryptionMigrationTask createEncryptionMigrationTask(String sdkKey, SplitRoomDatabase splitRoomDatabase, boolean encryptionEnabled, SplitCipher splitCipher); } diff --git a/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java b/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java index 2c167cda0..75ecc45fc 100644 --- a/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java +++ b/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java @@ -44,7 +44,10 @@ import io.split.android.client.service.telemetry.TelemetryStatsRecorderTask; import io.split.android.client.service.telemetry.TelemetryTaskFactory; import io.split.android.client.service.telemetry.TelemetryTaskFactoryImpl; +import io.split.android.client.storage.cipher.EncryptionMigrationTask; +import io.split.android.client.storage.cipher.SplitCipher; import io.split.android.client.storage.common.SplitStorageContainer; +import io.split.android.client.storage.db.SplitRoomDatabase; import io.split.android.client.telemetry.storage.TelemetryRuntimeProducer; import io.split.android.client.telemetry.storage.TelemetryStorage; @@ -203,6 +206,11 @@ public SplitInPlaceUpdateTask createSplitsUpdateTask(Split featureFlag, long sin return new SplitInPlaceUpdateTask(mSplitsStorageContainer.getSplitsStorage(), mSplitChangeProcessor, mEventsManager, mTelemetryRuntimeProducer, featureFlag, since); } + @Override + public EncryptionMigrationTask createEncryptionMigrationTask(String sdkKey, SplitRoomDatabase splitRoomDatabase, boolean encryptionEnabled, SplitCipher splitCipher) { + return new EncryptionMigrationTask(sdkKey, splitRoomDatabase, encryptionEnabled, splitCipher); + } + @NonNull private TelemetryTaskFactory initializeTelemetryTaskFactory(@NonNull SplitClientConfig splitClientConfig, @Nullable Map filters, TelemetryStorage telemetryStorage) { final TelemetryTaskFactory mTelemetryTaskFactory; diff --git a/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java b/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java index 3e5b00b08..f211ae62b 100644 --- a/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java +++ b/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java @@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit; import io.split.android.client.SplitClientConfig; +import io.split.android.client.service.CleanUpDatabaseTask; import io.split.android.client.service.executor.SplitTask; import io.split.android.client.service.executor.SplitTaskExecutionInfo; import io.split.android.client.service.executor.SplitTaskExecutionListener; @@ -30,11 +31,16 @@ public class RolloutCacheManagerImpl implements RolloutCacheManager, SplitTask { @NonNull private final RolloutDefinitionsCache[] mStorages; @NonNull + private final CleanUpDatabaseTask mCleanUpDatabaseTask; + @NonNull private final EncryptionMigrationTask mEncryptionMigrationTask; - public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @NonNull SplitStorageContainer storageContainer, @NonNull EncryptionMigrationTask encryptionMigrationTask) { + public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @NonNull SplitStorageContainer storageContainer, + @NonNull CleanUpDatabaseTask cleanUpDatabaseTask, + @NonNull EncryptionMigrationTask encryptionMigrationTask) { this(storageContainer.getGeneralInfoStorage(), RolloutCacheManagerConfig.from(splitClientConfig), + cleanUpDatabaseTask, encryptionMigrationTask, storageContainer.getSplitsStorage(), storageContainer.getMySegmentsStorageContainer(), @@ -42,8 +48,13 @@ public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @No } @VisibleForTesting - RolloutCacheManagerImpl(@NonNull GeneralInfoStorage generalInfoStorage, @NonNull RolloutCacheManagerConfig config, @NonNull EncryptionMigrationTask encryptionMigrationTask, @NonNull RolloutDefinitionsCache... storages) { + RolloutCacheManagerImpl(@NonNull GeneralInfoStorage generalInfoStorage, + @NonNull RolloutCacheManagerConfig config, + @NonNull CleanUpDatabaseTask clean, + @NonNull EncryptionMigrationTask encryptionMigrationTask, + @NonNull RolloutDefinitionsCache... storages) { mGeneralInfoStorage = checkNotNull(generalInfoStorage); + mCleanUpDatabaseTask = checkNotNull(clean); mEncryptionMigrationTask = checkNotNull(encryptionMigrationTask); mStorages = checkNotNull(storages); mConfig = checkNotNull(config); @@ -52,9 +63,19 @@ public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @No @WorkerThread @Override public void validateCache(SplitTaskExecutionListener listener) { - execute(); - mEncryptionMigrationTask.execute(); - listener.taskExecuted(SplitTaskExecutionInfo.success(SplitTaskType.GENERIC_TASK)); + try { + Logger.v("Rollout cache manager: Executing clearing task"); + mCleanUpDatabaseTask.execute(); + Logger.v("Rollout cache manager: Validating cache"); + execute(); + Logger.v("Rollout cache manager: Migrating encryption"); + mEncryptionMigrationTask.execute(); + Logger.v("Rollout cache manager: validation finished"); + listener.taskExecuted(SplitTaskExecutionInfo.success(SplitTaskType.GENERIC_TASK)); + } catch (Exception ex) { + Logger.e("Error occurred validating cache: " + ex.getMessage()); + listener.taskExecuted(SplitTaskExecutionInfo.error(SplitTaskType.GENERIC_TASK)); + } } @NonNull diff --git a/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt b/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt index 7bb93671b..20e1b2c24 100644 --- a/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt +++ b/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt @@ -1,16 +1,14 @@ package io.split.android.client.service.synchronizer +import io.split.android.client.service.CleanUpDatabaseTask import io.split.android.client.service.executor.SplitTaskExecutionListener -import io.split.android.client.service.executor.SplitTaskExecutor import io.split.android.client.storage.RolloutDefinitionsCache import io.split.android.client.storage.cipher.EncryptionMigrationTask import io.split.android.client.storage.general.GeneralInfoStorage -import io.split.android.fake.SplitTaskExecutorStub import org.junit.Before import org.junit.Test import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.anyLong -import org.mockito.ArgumentMatchers.argThat import org.mockito.Mockito.longThat import org.mockito.Mockito.mock import org.mockito.Mockito.times @@ -25,11 +23,13 @@ class RolloutCacheManagerTest { private lateinit var mSplitsCache: RolloutDefinitionsCache private lateinit var mSegmentsCache: RolloutDefinitionsCache private lateinit var mEncryptionMigrationTask: EncryptionMigrationTask + private lateinit var mCleanUpDatabaseTask: CleanUpDatabaseTask @Before fun setup() { mGeneralInfoStorage = mock(GeneralInfoStorage::class.java) - mEncryptionMigrationTask = mock(EncryptionMigrationTask::class.java); + mEncryptionMigrationTask = mock(EncryptionMigrationTask::class.java) + mCleanUpDatabaseTask = mock(CleanUpDatabaseTask::class.java) mSplitsCache = mock(RolloutDefinitionsCache::class.java) mSegmentsCache = mock(RolloutDefinitionsCache::class.java) } @@ -133,8 +133,26 @@ class RolloutCacheManagerTest { verify(mGeneralInfoStorage, times(0)).setRolloutCacheLastClearTimestamp(anyLong()) } + @Test + fun `validateCache executes cleanUpDatabaseTask`() { + mRolloutCacheManager = getCacheManager(10L, false) + + mRolloutCacheManager.validateCache(mock(SplitTaskExecutionListener::class.java)) + + verify(mCleanUpDatabaseTask).execute() + } + + @Test + fun `validateCache executes encryptionMigrationTask`() { + mRolloutCacheManager = getCacheManager(10L, false) + + mRolloutCacheManager.validateCache(mock(SplitTaskExecutionListener::class.java)) + + verify(mEncryptionMigrationTask).execute() + } + private fun getCacheManager(expiration: Long, clearOnInit: Boolean): RolloutCacheManager { - return RolloutCacheManagerImpl(mGeneralInfoStorage, RolloutCacheManagerConfig(expiration, clearOnInit), mEncryptionMigrationTask, mSplitsCache, mSegmentsCache) + return RolloutCacheManagerImpl(mGeneralInfoStorage, RolloutCacheManagerConfig(expiration, clearOnInit), mCleanUpDatabaseTask, mEncryptionMigrationTask, mSplitsCache, mSegmentsCache) } private fun createMockedTimestamp(period: Long): Long {